# How to use log\_sum\_exp in C++ without stan::math::var?

**URL:** <https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695>\
**Category:** Modeling\
**Tags:** stan-math, r\
**Created:** [July 28, 2021, 10:56am UTC](https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695 "2021-07-28T10:56:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADFun](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/adfun/32/12326_2.png) [@ADFun](https://discourse.mc-stan.org/u/ADFun)\
**Post date:** [July 28, 2021, 10:56am UTC](https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695/1 "2021-07-28T10:56:04Z")

</div>

I am trying to use the `log_sum_exp` function in my `Rcpp` function, but cannot compile the `.cpp` file using `sourceCpp` unless I use the `stan::math::var` type. Example `.cpp` file as follows:

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

#include <Rcpp.h>
#include <stan/math.hpp>

using namespace Rcpp;

// [[Rcpp::export]]
double tmp_fn_log_sum_exp_bug(
        NumericVector tmp_vector) {
    
    
    // does not compile
    // return log_sum_exp(tmp_vector);
    
    
    // // compiles
    std::vector<stan::math::var> tmp_vector_(tmp_vector.size());
    
    for (int i = 0; i < tmp_vector.size(); i++) {
     
     
     tmp_vector_[i] = tmp_vector[i];
        
    }
    
    return value_of(log_sum_exp(tmp_vector_));
    
    
    
}

```

I am a stan and c++ newbie, so apologies if I have missed anything.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [July 31, 2021, 4:03pm UTC](https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695/2 "2021-07-31T16:03:27Z")

</div>

In the math library, the non-variable version `log_sum_exp` is defined for `std::vector<double>` which is a different type from `NumericVector` (see [stdvector - Convert R::vector to std::vector - Stack Overflow](https://stackoverflow.com/questions/37328978/convert-rvector-to-stdvector) for instructions to convert).

Additionally, I think you need to qualify the `log_sum_exp` with the `stan::math` namespace (i.e. use `stan::math::log_sum_exp`). You don’t need to this when passing `stan::math::var` as the parameter as C++ will “magically” look for the function also in the same namespace as the parameters are defined.

(I can’t currently test the code locally, so hope this would make it work).

Best of luck!

---

<div class="post-metadata">

**Author:** ![stevebronder](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stevebronder/32/17242_2.png) [@stevebronder](https://discourse.mc-stan.org/u/stevebronder)\
**Post date:** [July 31, 2021, 4:08pm UTC](https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695/3 "2021-07-31T16:08:01Z")

</div>

Can you try it with an Eigen vector? That should work as well

---

<div class="post-metadata">

**Author:** ![ADFun](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/adfun/32/12326_2.png) [@ADFun](https://discourse.mc-stan.org/u/ADFun)\
**Post date:** [August 1, 2021, 11:25am UTC](https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695/4 "2021-08-01T11:25:23Z")

</div>

Thank you, I would have never got it without you: I had been trying my best to assign outputs to the right type, and may have eventually hit on the fact that I was passing in the wrong type, but I would never have worked out that you needed to preface the namespace. For the record, both

```
    std::vector<double> tmp_vector_ = Rcpp::as<std::vector<double> >(tmp_vector);

    return stan::math::log_sum_exp(tmp_vector_);

```

and

```
    std::vector<double> tmp_vector_(tmp_vector.size());

    for (int i = 0; i < tmp_vector.size(); i++) {

        tmp_vector_[i] = tmp_vector[i];

    }

    return stan::math::log_sum_exp(tmp_vector_);

```

compile.

Sorry I do not know what Eigen vectors are, or I would attempt them.
