[Rcpp-devel] function modifying it self its argument
Pierre GLOAGUEN
Pierre.Gloaguen at ifremer.fr
Thu Feb 26 17:13:36 CET 2015
Ok, i'll do this.
Thanks and have a nice day,
Pierre
Le 26/02/2015 16:51, Dirk Eddelbuettel a écrit :
> On 26 February 2015 at 16:37, Pierre GLOAGUEN wrote:
> | Thanks, it works!
> | I'm not familiar with C++, is it necessary to always use such a function in C++
> | or is it because of R/C++ interface?
>
> Please use a Google search (or equivalent) for Rcpp::clone. This has been
> discussed extensively on numerous occassions.
>
> Dirk
>
> | Thanks again for your help,
> |
> | Pierre
> |
> | Le 26/02/2015 16:30, Jeffrey Pollock a écrit :
> |
> | Perhaps use the clone() function?
> |
> | library(Rcpp)
> |
> | cppFunction("
> | NumericVector par_CMAtR(NumericVector vec_CMA) {
> | NumericVector out = clone(vec_CMA);
> | out[5] = exp(out[5]);
> | return out;
> | }
> | ")
> |
> | vec_C<- rep(1, 6)
> | par_CMAtR(vec_C)
> | print(vec_C)
> |
> | On Thu, Feb 26, 2015 at 3:16 PM, Pierre GLOAGUEN<
> | Pierre.Gloaguen at ifremer.fr> wrote:
> |
> |
> | Hello everybody,
> |
> | I have a very simple example
> | I have a vector vec_CMA which length is a multiple of 6.
> | I want to get the exact same vector, except the last element which is
> | the exponential of the last element of vec_CMA
> | The code is the following
> |
> | //myfun.cpp
> | #include<Rcpp.h>
> | using namespace Rcpp;
> |
> | // [[Rcpp::export]]
> | NumericVector par_CMAtR(NumericVector vec_CMA){
> | int K = (vec_CMA.size())/6;
> | NumericVector out(6*K);
> | out = vec_CMA;
> | out[6*K-1] = exp(vec_CMA[6*K-1]);
> | return out;
> | }
> |
> | I apply the function with the R code
> | sourceCpp("myfun.cpp")
> | vec_C<- rep(1,6)
> | par_CMAtR(vec_C)
> | [1] 1 1 1 1 1 2.718282
> |
> | 8
> |
> | works fine. Except the vec_C is modified too!
> | vec_C
> | [1] 1 1 1 1 1 2.718282
> |
> | It's the first time I have this kind of problem. What is wrong in my
> | code?
> | Thanks for your help,
> |
> | Pierre Gloaguen
> |
> |
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> |
> |
> |
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
More information about the Rcpp-devel
mailing list