Re: [Rcpp-devel] Matrix assignment with inline

2010-11-04 Thread Kaveh Vakili
Thanks for the advice Dirk, 



>
>Kaveh,
>
>If you are having problems with things like matrix indexing I would recommend
>a somewhat more incremental approach:
>
> pass a matrix from R, find the maximum element and return it
>
> pass a matrix from R, find the min and maximum elements and return both
>
> multiply two (small) matrices by doing the looping yourself and return
> the new matrix
>
>etc pp before trying to port code.  
>
>Cheers, Dirk
>
>-- 
>Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com
>
>


___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Matrix assignment with inline

2010-11-04 Thread Dirk Eddelbuettel

Kaveh,

If you are having problems with things like matrix indexing I would recommend
a somewhat more incremental approach:

 pass a matrix from R, find the maximum element and return it

 pass a matrix from R, find the min and maximum elements and return both

 multiply two (small) matrices by doing the looping yourself and return
 the new matrix

etc pp before trying to port code.  

Cheers, Dirk

-- 
Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Matrix assignment with inline

2010-11-04 Thread Romain Francois

Le 03/11/10 12:16, Kaveh Vakili a écrit :

Hi list,

I'm trying to learn inline by translating snippets of c++ code from
the book "Numerical Recipes".

in page 44-45, there is a c++ code to perform Gauss-Jordan elimination of a 
matrix (see below). I understand that they have a large template of additional 
operator/function/class (it is online: www.nr.com/codefile.php?nr3 ) which 
means the codes in the book have to be slightly 'translated' (correct me if i'm 
wrong) unto proper inline inputs. I think i have a problem with translating the 
matrix functions. I have searched the devel archives, but have only found codes 
to assign full row/column (not individual matrix entries).

Anyhow, my use of 'a[j][k]' in line 44 (and similar threats later) seem to be 
an issue: none of the examples in Rcpp seems to use it. My question is how do 
you translate this construct (i.e. a[j][k]) so that inline can 'eat' it ?


Hi,

You can use a(j,k)

There are a lot of places in this code where Rcpp sugar might help. I 
don't want to go through all of it right now, but for example this line:


for(l=0;l system.file( "unitTests/runit.sugar.R", package = "Rcpp" )



Also, I'm not sure that this code is valid:

throw ("gaussj: Singular Matrix");

Maybe you mean :

throw std::range_error("gaussj: Singular Matrix");

or perhaps using some other exception class instead of range_error.

HTH,

Romain


library(Rcpp)
library(inline)


fun3<-'
NumericMatrix a(x) ;
NumericMatrix b = clone(a);
int i,icol,irow,j,k,l,ll,n=a.nrow(),m=a.ncol();
std::vector  indxc(n);
std::vector  indxr(n);
std::vector  ipiv(n);
double big,dum,piinv;
for(j=0;j= big) {
big = abs(a[j][k]);
irow = j;
icol = k;
}
}
}
++(ipiv[icol]);
if(irow != icol){
for (l=0;l=0;l--){
if(indxr[l] != indxc[l])
for(k=0;khttps://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel




--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/czHPM7 : Rcpp Google tech talk on youtube
|- http://bit.ly/9P0eF9 : Google slides
`- http://bit.ly/cVPjPe : Chicago R Meetup slides


___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Matrix assignment with inline

2010-11-04 Thread Kaveh Vakili
Hi list,

I'm trying to learn inline by translating snippets of c++ code from 
the book "Numerical Recipes".

in page 44-45, there is a c++ code to perform Gauss-Jordan elimination of a 
matrix (see below). I understand that they have a large template of additional 
operator/function/class (it is online: www.nr.com/codefile.php?nr3 ) which 
means the codes in the book have to be slightly 'translated' (correct me if i'm 
wrong) unto proper inline inputs. I think i have a problem with translating the 
matrix functions. I have searched the devel archives, but have only found codes 
to assign full row/column (not individual matrix entries). 

Anyhow, my use of 'a[j][k]' in line 44 (and similar threats later) seem to be 
an issue: none of the examples in Rcpp seems to use it. My question is how do 
you translate this construct (i.e. a[j][k]) so that inline can 'eat' it ?


library(Rcpp)
library(inline)


fun3<-'
NumericMatrix a(x) ;
NumericMatrix b = clone(a);
int i,icol,irow,j,k,l,ll,n=a.nrow(),m=a.ncol();
std::vector indxc(n); 
std::vector indxr(n);
std::vector ipiv(n);
double big,dum,piinv;
for(j=0;j= big) {
big = abs(a[j][k]);
irow = j;
icol = k;
}
}
}
++(ipiv[icol]);
if(irow != icol){
for (l=0;l=0;l--){
if(indxr[l] != indxc[l])
for(k=0;khttps://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel