1

I am trying to update a variable within a loop but I am receiving the error

static assertion failed: cannot convert type to SEXP

I am trying to reproduce the following R code in Rcpp:

> v = rep(1, 5)
> for(k in 0:3){
+   v = cumsum(v)
+ }
> print(v)
[1]  1  5 15 35 70

I have gone through the following attempts (uncommenting / commenting the relevant chunks of code) but all give the same error. How can I do this and what am I doing wrong please?

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector fun() {

  IntegerVector v = rep(1, 5);

  // Attempt 1. 
  for(int k = 0; k < 4; ++k){
    v = cumsum(v);
  }

  // Attempt 2.
  // IntegerVector tempv;
  // for(int k = 0; k < 4; ++k){
  //   tempv = cumsum(v);
  //   v = tempv;
  // }

  // can reproduce error more simply with the following: 
  // so issue is assigning back to variable or change of class?
  // v = cumsum(v);

  // Attempt 3.
  // IntegerVector tempv;
  // for(int k = 0; k < 4; ++k){
  //   tempv = cumsum(v);
  //   v = as<IntegerVector>(tempv);
  // }  

  return v;
}

EDIT:

Okay, so I have something working (thanks to this)

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector fun() {

  IntegerVector v = rep(1, 5);
     for(int k = 0; k < 4; ++k){
        std::partial_sum(v.begin(), v.end(), v.begin());
     }
   return v;
}

So I suppose my question is now what I was doing wrong previously? Thanks

3
  • Looks like a bug. If you check the unit tests for the package, you see that cumsum() is used each time---but only on a NumericVector. So if you swap your IntegerVector for NumericVector, it works. It should of course also work for IntegerVector. Commented Jan 27, 2020 at 19:22
  • Thanks for your comment Dirk. Apologies, I should of checked that but still trying to find my feet. Commented Jan 27, 2020 at 19:29
  • 1
    No apologies needed--it is a bug. That should probably have worked. Commented Jan 27, 2020 at 19:31

1 Answer 1

2

As I hinted in my earlier comment, that is supposed to work. As it is not, you found a bug.

Whether it is worth fixing it is another manner. Whenvever I compute on or with vectors, I typically reach for RcppArmadillo. So here is a minimal (working) version of your first attempt, in RcppArmadillo.

Code

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

// [[Rcpp::export]]
arma::ivec fun() {
  arma::ivec v(5, arma::fill::ones);

  for (int k=0; k<3; k++) {
    v = arma::cumsum(v);
  }

  return(v);
}

/*** R
fun()
*/

Output

R> sourceCpp("~/git/stackoverflow/59936632/answer.cpp")

R> fun()
     [,1]
[1,]    1
[2,]    4
[3,]   10
[4,]   20
[5,]   35
R> 

Edit

Made one smal fix and replace the earlier C++11 curly-init with a call to ones to replicate rep(1,5).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.