Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

c++ - Using dnorm with RcppArmadillo

From R, I'm trying to run sourceCpp on this file:

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace arma; 
using namespace Rcpp;

// [[Rcpp::export]]
vec dnormLog(vec x, vec means, vec sds) {
    int n = x.size();
    vec res(n);
    for(int i = 0; i < n; i++) {
        res[i] = log(dnorm(x[i], means[i], sds[i]));
    }
return res;
}

See this answer to see where I got the function from. This throws the error:

no matching function for call to 'dnorm4'

Which is the exact error I was hoping to prevent by using the loop, since the referenced answer mentions that dnorm is only vectorized with respect to its first argument. I fear the answer is obvious, but I've tried adding R:: before the dnorm, tried using NumericVector instead of vec, without using log() in front. No luck. However, adding R:: before dnorm does produce a separate error:

too few arguments to function call, expected 4, have 3; did you mean '::dnorm4'?

Which is not fixed by replacing dnorm above with R::dnorm4.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two nice teachable moments here:

  1. Pay attention to namespaces. If in doubt, don't go global.
  2. Check the headers for the actual definitions. You missed the fourth argument in the scalar version R::dnorm().

Here is a repaired version, included is a second variant you may find interesting:

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::vec dnormLog(arma::vec x, arma::vec means, arma::vec sds) {
  int n = x.size();
  arma::vec res(n);
  for(int i = 0; i < n; i++) {
    res[i] = std::log(R::dnorm(x[i], means[i], sds[i], FALSE));
  }
  return res;
}

// [[Rcpp::export]]
arma::vec dnormLog2(arma::vec x, arma::vec means, arma::vec sds) {
  int n = x.size();
  arma::vec res(n);
  for(int i = 0; i < n; i++) {
    res[i] = R::dnorm(x[i], means[i], sds[i], TRUE);
  }
  return res;
}


/*** R
dnormLog( c(0.1,0.2,0.3), rep(0.0, 3), rep(1.0, 3))
dnormLog2(c(0.1,0.2,0.3), rep(0.0, 3), rep(1.0, 3))
*/

When we source this, both return the same result because the R API allows us to ask for logarithms to be taken.

R> sourceCpp("/tmp/dnorm.cpp")

R> dnormLog( c(0.1,0.2,0.3), rep(0.0, 3), rep(1.0, 3))
          [,1]
[1,] -0.923939
[2,] -0.938939
[3,] -0.963939

R> dnormLog2(c(0.1,0.2,0.3), rep(0.0, 3), rep(1.0, 3))
          [,1]
[1,] -0.923939
[2,] -0.938939
[3,] -0.963939
R> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...