Using stan::math::multinomial_log directly

48 views
Skip to first unread message

David Knowles

unread,
Jan 18, 2016, 6:13:56 PM1/18/16
to stan-...@googlegroups.com
I'm having a problem using stan::math::multinomial_log. My test script
(taken from the stan math test script) looks like this:

#include <stan/math/prim/mat/prob/multinomial_log.hpp>

#include <RcppEigen.h>
using namespace Eigen;

// [[Rcpp::export]]
double test_multinomial_log() {
std::vector<int> ns;
ns.push_back(1);
ns.push_back(2);
Eigen::Matrix<double,Dynamic,1> theta(2,1);
theta << 0.2, 0.8;
return stan::math::multinomial_log<true>(ns,theta);
}

/*** R
test_multinomial_log()
*/

The error I get is

In file included from test.cpp:1:
In file included from
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/mat/prob/multinomial_log.hpp:9:
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/StanHeaders/include/stan/math/prim/scal/err/check_nonnegative.hpp:40:38:
error: invalid operands to binary expression ('std::__1::vector<int,
std::__1::allocator<int> >' and 'int')
&& !(stan::get(y, n) >= 0))
~~~~~~~~~~~~~~~ ^ ~

Apologies if there is a specific stan-math mailing list I should be
asking this on!

Thank you in advance,

David.

--
David A. Knowles,
Stanford University.
E-mail: know...@gmail.com
Web: http://cs.stanford.edu/~davidknowles/

Daniel Lee

unread,
Jan 18, 2016, 6:21:08 PM1/18/16
to stan-...@googlegroups.com
Hi David,

Try including this instead:
#include <stan/math.hpp>

That should include all of the template metaprograms in the correct order and make it work. Could you let us know if it doesn't work with that include?


Daniel

P.S. We don't have a separate mailing list yet.
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

David Knowles

unread,
Jan 18, 2016, 7:53:42 PM1/18/16
to Stan users mailing list
Nope, still the same error :/ 

Should have included:
R version 3.2.2
Rcpp_0.12.1
rstan_2.9.0
Mac OS X 10.10.2

Jiqiang Guo

unread,
Jan 18, 2016, 8:31:05 PM1/18/16
to stan-...@googlegroups.com
How do you run it?  I cannot do sourceCpp directly as it does not put StanHeaders in it includes. 

However what Daniel suggested should work as I can compile the generated cpp file directly using clang++.

Jiqiang 

--

Ben Goodrich

unread,
Jan 18, 2016, 10:05:24 PM1/18/16
to Stan users mailing list
On Monday, January 18, 2016 at 8:31:05 PM UTC-5, Jiqiang Guo wrote:
How do you run it?  I cannot do sourceCpp directly as it does not put StanHeaders in it includes. 

FYI, I discovered that if you put

// [[Rcpp::depends(rstan)]]

at the top of a C++ file and do sourceCpp() it reads rstan's plugin.R and uses its PKG_CPPFLAGS.

Ben


David Knowles

unread,
Jan 19, 2016, 1:25:16 AM1/19/16
to stan-...@googlegroups.com
Yup I'm using sourceCpp through RStudio with [[Rcpp::depends(RcppEigen,rstan)]]

For now I just made my own version of multinomial_log without the
parameter checks. That seems to be working fine.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Stan users mailing list" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/stan-users/8gob3XKT8Vs/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages