No subject
Mon Oct 17 11:22:36 CEST 2011
not copied and is multiplied by 2 in place. However, it's not the case,
matrix m is the same after calling .C("modify"...)
as it was before. Am i calling incorrectly, or is it just impossible to
modify R matrix in place from C++? Would greatly appreciate any pointers.
--001517588cfa41b44704b0b5fd7c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hi, guys. I have some pretty large arrays in R and i wanted to do some time=
consuming modifications of these arrays in C++ without actually copying th=
em, just by passing pointers to them. Since i don't know internal data =
structures of R, i am not sure it's possible, but i thought it was. Her=
e is some toy code that i thought should work, but doesn't. Maybe someo=
ne could point out the error i am making<br>
<br>i have the following in the passptr.cpp to multiply array elements by 2=
<br>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D<br>extern "C"{<br>
=A0void modify(double *mem, int *nr, int *nc){<br>
=A0 for(int i=3D0; i< (*nr)*(*nc); i++)<br>
=A0=A0=A0 mem[i]=3D2*mem[i];<br>
=A0=A0=A0}<br>
}<br><br>----------------------------------------------<br>I compile it int=
o a shared library using <br>R CMD SHLIB passptr.cpp<br>load and run from R=
as follows<br><br>--------------------------------<br><p>>dyn.load(&quo=
t;/home/az05625/testarma/passptr.so")</p>
<p>>m<-matrix(1:10,nr=3D2)<br>
</p><p>>.C("modify", as.double(m), as.integer(2), as.integer(5=
), DUP=3DFALSE)</p><p>From reading docs i thought that DUP=3DFALSE would en=
sure that R matrix is not copied and is multiplied by 2 in place. However, =
it's not the case, matrix m is the same after calling .C("modify&q=
uot;...)<br>
</p><p>as it was before. Am i calling incorrectly, or is it just impossible=
to modify R matrix in place from C++? Would greatly appreciate any pointer=
s.</p><p><br></p><p><br></p>
--001517588cfa41b44704b0b5fd7c--
More information about the Rcpp-devel
mailing list