Re: [Rcpp-devel] Rcpp ISNAN slower than C ISNAN?

2016-12-13 Thread Johannes Kruisselbrink
Here is a further reduced example (see below). Now it is a function to 
count NaNs in a vector, and it shows the same behaviour. Code is also 
available at 
https://github.com/jwkruisselbrink/rcpp-timings/tree/master/minimal.


Regarding your question:
| Why not drop data and codes and use  sData1(i,k) - sData2(j,k)

Well, I wanted to rule out that Rcpp sugar was causing the slowdown. If 
it isn't we'll put it back in after having a fix for this issue.


/=== call.c ===/

#include 
#include 

SEXP CountNans(SEXP sX, SEXP sLength) {
  int i, n = *INTEGER(sLength);
  int *nanCount;
  double *x = REAL(sX);

  SEXP output = PROTECT(allocVector(INTSXP, 1));
  nanCount = INTEGER(output);
  *nanCount = 0;
  for (i = 0; i < n; i++) {
if (ISNAN(x[i])) {
  *nanCount += 1;
}
  }
  UNPROTECT(1);
  return output;
}

/=== rcpp.cpp ===/

#include 
#include 
using namespace Rcpp;

// [[Rcpp::export]]
int CountNans(NumericVector x) {
  int n = x.length();
  int nanCount = 0;
  for (int i = 0; i < n; i++) {
if (ISNAN(x[i])) {
  nanCount++;
}
  }
  return nanCount;
}

/=== R code ===/

library(Rcpp)
library(microbenchmark)
library(ggplot2)

sourceCpp('rcpp.cpp')

if (is.loaded(paste("call", .Platform$dynlib.ext, sep=""))) {
  dyn.unload(paste("call", .Platform$dynlib.ext, sep=""))
}
system(paste("R CMD SHLIB call.c", sep=""))
dyn.load(paste("call", .Platform$dynlib.ext, sep=""))

n <- as.integer(10)
x <- rnorm(n)
mb <- microbenchmark(
  rcpp <- CountNans(x),
  call <- .Call("CountNans", x, n),
  times = 1
)

autoplot(mb)


___
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] Rcpp ISNAN slower than C ISNAN?

2016-12-13 Thread Romain Francois
I’d be interesting to see what this more C++idomatic version would perform 

nanCount = std::count_if( x.begin(), x.end(), ISNAN ) ;

Because despite all the inlining efforts that has been put in the 
implementation of NumericVector, its operator[] might not perform as well as 
using [] on a double*. 

Romain


> Le 13 déc. 2016 à 12:38, Johannes Kruisselbrink  a 
> écrit :
> 
> Here is a further reduced example (see below). Now it is a function to count 
> NaNs in a vector, and it shows the same behaviour. Code is also available at 
> https://github.com/jwkruisselbrink/rcpp-timings/tree/master/minimal.
> 
> Regarding your question:
> | Why not drop data and codes and use  sData1(i,k) - sData2(j,k)
> 
> Well, I wanted to rule out that Rcpp sugar was causing the slowdown. If it 
> isn't we'll put it back in after having a fix for this issue.
> 
> /=== call.c ===/
> 
> #include 
> #include 
> 
> SEXP CountNans(SEXP sX, SEXP sLength) {
>  int i, n = *INTEGER(sLength);
>  int *nanCount;
>  double *x = REAL(sX);
> 
>  SEXP output = PROTECT(allocVector(INTSXP, 1));
>  nanCount = INTEGER(output);
>  *nanCount = 0;
>  for (i = 0; i < n; i++) {
>if (ISNAN(x[i])) {
>  *nanCount += 1;
>}
>  }
>  UNPROTECT(1);
>  return output;
> }
> 
> /=== rcpp.cpp ===/
> 
> #include 
> #include 
> using namespace Rcpp;
> 
> // [[Rcpp::export]]
> int CountNans(NumericVector x) {
>  int n = x.length();
>  int nanCount = 0;
>  for (int i = 0; i < n; i++) {
>if (ISNAN(x[i])) {
>  nanCount++;
>}
>  }
>  return nanCount;
> }
> 
> /=== R code ===/
> 
> library(Rcpp)
> library(microbenchmark)
> library(ggplot2)
> 
> sourceCpp('rcpp.cpp')
> 
> if (is.loaded(paste("call", .Platform$dynlib.ext, sep=""))) {
>  dyn.unload(paste("call", .Platform$dynlib.ext, sep=""))
> }
> system(paste("R CMD SHLIB call.c", sep=""))
> dyn.load(paste("call", .Platform$dynlib.ext, sep=""))
> 
> n <- as.integer(10)
> x <- rnorm(n)
> mb <- microbenchmark(
>  rcpp <- CountNans(x),
>  call <- .Call("CountNans", x, n),
>  times = 1
> )
> 
> autoplot(mb)
> 
> 
> ___
> 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 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] Rcpp ISNAN slower than C ISNAN?

2016-12-13 Thread Christian Gunning
> |for (i = 0; i < numObjects; i++) {
> |  for (j = 0; j < numCodes; j++) {
> |dist = 0;
> |for (k = 0; k < numVars; k++) {
> |  if (!ISNAN(data[i * numVars + k])) {
> |tmp = data[i * numVars + k] - codes[j * numVars + k];
>
> Why not drop data and codes and use  sData1(i,k) - sData2(j,k)  ?

Or better yet, just use the original code with NumericMatrix:
sData1[i * numVars + k] does the right thing.
I don't get any timing difference based on this change.

Using Rcpp sugar
(https://cran.r-project.org/package=Rcpp/vignettes/Rcpp-sugar.pdf),
and moving the call outside the loop, appears to do the right thing.

## modified example
## see edits here:
https://github.com/helmingstay/rcpp-timings/blob/master/diff/rcppdist.cpp#L24
git clone https://github.com/helmingstay/rcpp-timings
cd rcpp-timings/diff
R --vanilla < glue.R

best,
Christian

>
> That still doesn't explain the slowdowns though.  Could you prepare a
> _complete_ yet minimal example along with mock data?
>
> Dirk
>
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
>

-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
http://www.x14n.org
___
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] Rcpp question regarding compilation of another package

2016-12-13 Thread Dirk Eddelbuettel

Thanks for posting here, and showing complete references. Your earlier
StackOverflow questions went nowhere.  Hopefully we can tackle this here.

On 12 December 2016 at 17:32, khagay nagdimov wrote:
| I am trying to create a package that uses Rcpp to translate code from SeqLib 
( 
| https://github.com/walaj/SeqLib) into R. I have compiled SeqLib but, I still
| have problems referencing the function definitions. The current structure of 
my
| R package is shown here: https://github.com/KhagayN/SeqLibr

A bit of a mess. The repo is full of object files (.o), editor backups (*~),
editor temp files (#*#).  But at least you are using the right editor.

I have no experience with git submodules and cannot say wether SeqLib again
in SeqLibR is a good idea.
 
| My makevars links to the location of the header files and to the static
| libraries. To my understanding, the header files linked via PKG_CXXFLAGS point
| to the header files and PKG_LIBS points to the static libraries. Then, in

The Makevars is suspicious:

## the following flags are for PreProcessor.
PKG_CPPFLAGS= -I../bwa/ -I../ -I../src/SeqLib/* 

First, the ../bwa/ directory does not work.  R includes _everything below
inst/_ but only a few known directories at the top-level.  This is why we all
put headers into inst/include/ --- so I would make **very strongly** suggest
you move bwa/ into inst/include/bwa and then use -I../inst/include/bwa.

Second, you are in src/ so ../src/ makes no sense. Make that -ISeqLib/

Third, there is no notion of wildcards in include directories.  So -ISeqLib/

Fourth, for SeqLib you do

   ( cd SeqLib; ./configure; make; make install; )

and you _almost surely_ should not run 'make install'.  The rest of your
src/Makevars is trying to use the static library from that directory; make
install would attempt to write the library somewhere else.  "Writing R
Extensions* _explicitly_ prohibits that for good reason.

I stop here.  Other failures related to loading object are likely due to
these issues which you probably want to correct first.

Dirk


| rcpp_hello_world.cpp, I reference a function called Open within the 
FastqReader
| class within the SeqLib namespace but, when I run load_all() I get an effor
| saying:
| 
| 
| Error in dyn.load(dllfile);
| 
|unable to load shared object 'subsetSeqLib.so': undefined symbol:
| _ZN6SeqLib11FastqReader40OpenERKSs, which references that Open function from
| FastqReader. I don't understand why this is happening since I am linked to the
| static libraries that have the definition of that function.
| 
| 
| Thanks for all the help. 
| 
| 
| 
| ___
| 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

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
___
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] Rcpp ISNAN slower than C ISNAN?

2016-12-13 Thread William Dunlap
Could the c++ slowdown be due to the fact that Rinternals.h defines ISNAN
differently for C and C++?  For C it uses the compiler's isnan macro, for
C++ it calls the function R_isnancpp.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Dec 13, 2016 at 5:04 AM, Christian Gunning  wrote:

> > |for (i = 0; i < numObjects; i++) {
> > |  for (j = 0; j < numCodes; j++) {
> > |dist = 0;
> > |for (k = 0; k < numVars; k++) {
> > |  if (!ISNAN(data[i * numVars + k])) {
> > |tmp = data[i * numVars + k] - codes[j * numVars + k];
> >
> > Why not drop data and codes and use  sData1(i,k) - sData2(j,k)  ?
>
> Or better yet, just use the original code with NumericMatrix:
> sData1[i * numVars + k] does the right thing.
> I don't get any timing difference based on this change.
>
> Using Rcpp sugar
> (https://cran.r-project.org/package=Rcpp/vignettes/Rcpp-sugar.pdf),
> and moving the call outside the loop, appears to do the right thing.
>
> ## modified example
> ## see edits here:
> https://github.com/helmingstay/rcpp-timings/blob/
> master/diff/rcppdist.cpp#L24
> git clone https://github.com/helmingstay/rcpp-timings
> cd rcpp-timings/diff
> R --vanilla < glue.R
>
> best,
> Christian
>
> >
> > That still doesn't explain the slowdowns though.  Could you prepare a
> > _complete_ yet minimal example along with mock data?
> >
> > Dirk
> >
> > --
> > http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
> >
>
> --
> A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
> http://www.x14n.org
> ___
> 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 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] Rcpp question regarding compilation of another package

2016-12-13 Thread khagay nagdimov
Thank you Dirk for all the help, it is very much appreciated.


I have made the changes you recommend and pushed them onto the GitHub page 
(https://github.com/KhagayN/SeqLibr). I realized that the bwa directory isn't 
necessary because src/SeqLib/bwa is an entire copy of it. I changed the 
PKG_CPPFLAGS to reflect that bwa directory via -ISeqLib/bwa/


Do you think that is acceptable? I will follow your guidance.


If so, I get an error after running load_all()
   Error in dyn.load(dllfile)

unable to load shared object 'subsetSeqLib.so': mem_opt_init


That function is defined in src/SeqLib/bwa/bwamem.c.



From: Dirk Eddelbuettel 
Sent: Tuesday, December 13, 2016 8:31:11 AM
To: khagay nagdimov
Cc: rcpp-devel@lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Rcpp question regarding compilation of another package


Thanks for posting here, and showing complete references. Your earlier
StackOverflow questions went nowhere.  Hopefully we can tackle this here.

On 12 December 2016 at 17:32, khagay nagdimov wrote:
| I am trying to create a package that uses Rcpp to translate code from SeqLib (
| https://github.com/walaj/SeqLib) into R. I have compiled SeqLib but, I still
| have problems referencing the function definitions. The current structure of 
my
| R package is shown here: https://github.com/KhagayN/SeqLibr

A bit of a mess. The repo is full of object files (.o), editor backups (*~),
editor temp files (#*#).  But at least you are using the right editor.

I have no experience with git submodules and cannot say wether SeqLib again
in SeqLibR is a good idea.

| My makevars links to the location of the header files and to the static
| libraries. To my understanding, the header files linked via PKG_CXXFLAGS point
| to the header files and PKG_LIBS points to the static libraries. Then, in

The Makevars is suspicious:

## the following flags are for PreProcessor.
PKG_CPPFLAGS= -I../bwa/ -I../ -I../src/SeqLib/*

First, the ../bwa/ directory does not work.  R includes _everything below
inst/_ but only a few known directories at the top-level.  This is why we all
put headers into inst/include/ --- so I would make **very strongly** suggest
you move bwa/ into inst/include/bwa and then use -I../inst/include/bwa.

Second, you are in src/ so ../src/ makes no sense. Make that -ISeqLib/

Third, there is no notion of wildcards in include directories.  So -ISeqLib/

Fourth, for SeqLib you do

   ( cd SeqLib; ./configure; make; make install; )

and you _almost surely_ should not run 'make install'.  The rest of your
src/Makevars is trying to use the static library from that directory; make
install would attempt to write the library somewhere else.  "Writing R
Extensions* _explicitly_ prohibits that for good reason.

I stop here.  Other failures related to loading object are likely due to
these issues which you probably want to correct first.

Dirk


| rcpp_hello_world.cpp, I reference a function called Open within the 
FastqReader
| class within the SeqLib namespace but, when I run load_all() I get an effor
| saying:
|
|
| Error in dyn.load(dllfile);
|
|unable to load shared object 'subsetSeqLib.so': undefined symbol:
| _ZN6SeqLib11FastqReader40OpenERKSs, which references that Open function from
| FastqReader. I don't understand why this is happening since I am linked to the
| static libraries that have the definition of that function.
|
|
| Thanks for all the help.
|
|
|
| ___
| 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

--
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
___
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] a variable for loop

2016-12-13 Thread Amina Shahzadi
Hello Friends and Prof. Dirk

I am pasting here a code which has a for loop depending on another for
loop.
I am getting zeros for cube c. I tried and searched a lot but did not get
an example of this type. Would you please help in this regard?


#include 
using namespace Rcpp;
using namespace RcppArmadillo;
using namespace arma;
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]


arma::cube  exam(arma::vec a,  arma::mat b)
{
  int m = a.size();
  int n = b.n_rows;
  arma::cube c = zeros(n, m, n);
  for(int i=0; i___
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] a variable for loop

2016-12-13 Thread Avraham Adler
On Tue, Dec 13, 2016 at 9:51 PM, Amina Shahzadi 
wrote:

> Hello Friends and Prof. Dirk
>
> I am pasting here a code which has a for loop depending on another for
> loop.
> I am getting zeros for cube c. I tried and searched a lot but did not get
> an example of this type. Would you please help in this regard?
>
>
> #include 
> using namespace Rcpp;
> using namespace RcppArmadillo;
> using namespace arma;
> //[[Rcpp::depends(RcppArmadillo)]]
> //[[Rcpp::export]]
>
>
> arma::cube  exam(arma::vec a,  arma::mat b)
> {
>   int m = a.size();
>   int n = b.n_rows;
>   arma::cube c = zeros(n, m, n);
>   for(int i=0; i for(int j=0; j   for(int k=0; k   if(k==0) {
> c(i, j ,k) = c(i, j, k) + b(i, j);
>   }
>   else{
> c(i, j, k) = c(i, j, k) +  c(i-1, j, k) *b(i, j);
>   }
>  }
> }
>   }
>   return c;
> }
>
>
> Thank You
> --
> *Amina Shahzadi*
>


Hello.

I haven't run your code, but it strikes me that I cannot see where are you
capturing the number of columns of b. It's a bit confusing as I was always
taught a matrix has m rows and n columns. Be that as it may, your k==0 loop
looks like it's trying to copy over the original matrix to the first slice,
but how do you know that b has m columns, which is what you're assuming by
letting j loop to m. Unless you are assuming a square matrix?

Even if you are, if your matrix is not the same length as your vector, I
think there is an issue with your loop boundaries, unless I've
misunderstood something, which is certainly possible.

For example, assume a is {1, 2, 3} and b is the 2 x 2 of row 1: [1 2] and
row 2: [3 4]. Thus m = 3 and n = 2.

Step 1: i = j = k = 0: c(0, 0, 0) becomes b(0, 0) or 1.

Step 2: i = 0, j = 1, k = 0: c(0, 1, 0) becomes b(0, 1) or 2.

Step 3: i = 0, j = 2, k = 0: c(0, 2, 0) becomes b(0, 2) ?!?! There is no
b(0, 2), it's only a 2x2 matrix?


Similar to your previous questions, instead of posting code, can you please
describe in words what it is you are trying to do? That may help.

Avi
___
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] a variable for loop

2016-12-13 Thread Amina Shahzadi
Hello Avraham --Happy to see you

My code is trying to produce a cube c which is going to be constructed by a
vector a and matrix b.
And the number of rows in b and size of a must be same.

So we can assume that if a is a vector of size 3, Then b must be 2 x 3 or 3
X 3 etc.

Thank you Avraham for quick response. I hope this will make my question
more clear.

Best regards


On Wed, Dec 14, 2016 at 4:46 PM, Avraham Adler 
wrote:

> On Tue, Dec 13, 2016 at 9:51 PM, Amina Shahzadi 
> wrote:
>
>> Hello Friends and Prof. Dirk
>>
>> I am pasting here a code which has a for loop depending on another for
>> loop.
>> I am getting zeros for cube c. I tried and searched a lot but did not get
>> an example of this type. Would you please help in this regard?
>>
>>
>> #include 
>> using namespace Rcpp;
>> using namespace RcppArmadillo;
>> using namespace arma;
>> //[[Rcpp::depends(RcppArmadillo)]]
>> //[[Rcpp::export]]
>>
>>
>> arma::cube  exam(arma::vec a,  arma::mat b)
>> {
>>   int m = a.size();
>>   int n = b.n_rows;
>>   arma::cube c = zeros(n, m, n);
>>   for(int i=0; i> for(int j=0; j>   for(int k=0; k>   if(k==0) {
>> c(i, j ,k) = c(i, j, k) + b(i, j);
>>   }
>>   else{
>> c(i, j, k) = c(i, j, k) +  c(i-1, j, k) *b(i, j);
>>   }
>>  }
>> }
>>   }
>>   return c;
>> }
>>
>>
>> Thank You
>> --
>> *Amina Shahzadi*
>>
>
>
> Hello.
>
> I haven't run your code, but it strikes me that I cannot see where are you
> capturing the number of columns of b. It's a bit confusing as I was always
> taught a matrix has m rows and n columns. Be that as it may, your k==0 loop
> looks like it's trying to copy over the original matrix to the first slice,
> but how do you know that b has m columns, which is what you're assuming by
> letting j loop to m. Unless you are assuming a square matrix?
>
> Even if you are, if your matrix is not the same length as your vector, I
> think there is an issue with your loop boundaries, unless I've
> misunderstood something, which is certainly possible.
>
> For example, assume a is {1, 2, 3} and b is the 2 x 2 of row 1: [1 2] and
> row 2: [3 4]. Thus m = 3 and n = 2.
>
> Step 1: i = j = k = 0: c(0, 0, 0) becomes b(0, 0) or 1.
>
> Step 2: i = 0, j = 1, k = 0: c(0, 1, 0) becomes b(0, 1) or 2.
>
> Step 3: i = 0, j = 2, k = 0: c(0, 2, 0) becomes b(0, 2) ?!?! There is no
> b(0, 2), it's only a 2x2 matrix?
>
>
> Similar to your previous questions, instead of posting code, can you
> please describe in words what it is you are trying to do? That may help.
>
> Avi
>
>


-- 
*Amina Shahzadi*
___
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] Rcpp ISNAN slower than C ISNAN?

2016-12-13 Thread Johannes Kruisselbrink
Moving the call outside the main loop would be effective for some 
scenarios (i.e, the scenarios where the data objects do not contain 
NaNs). However, once they do we still want to compute a distance based 
on the values and "correct" for the NaNs in some way, so skipping the 
entire object is not really an option. Including a switch between the 
cases of objects with and objects without NaNs is probably something 
worthwhile (that and using more rcpp-sugar).


Nevertheless, the question still remains why the rcpp isNaN call is so 
much slower.


On 12/13/2016 2:04 PM, xian at unm.edu (Christian Gunning) wrote:

|for (i = 0; i < numObjects; i++) {
|  for (j = 0; j < numCodes; j++) {
|dist = 0;
|for (k = 0; k < numVars; k++) {
|  if (!ISNAN(data[i * numVars + k])) {
|tmp = data[i * numVars + k] - codes[j * numVars + k];

Why not drop data and codes and use  sData1(i,k) - sData2(j,k)  ?

Or better yet, just use the original code with NumericMatrix:
sData1[i * numVars + k] does the right thing.
I don't get any timing difference based on this change.

Using Rcpp sugar
(https://cran.r-project.org/package=Rcpp/vignettes/Rcpp-sugar.pdf),
and moving the call outside the loop, appears to do the right thing.

## modified example
## see edits here:
https://github.com/helmingstay/rcpp-timings/blob/master/diff/rcppdist.cpp#L24
git clone https://github.com/helmingstay/rcpp-timings
cd rcpp-timings/diff
R --vanilla < glue.R

___
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] a variable for loop

2016-12-13 Thread Avraham Adler
On Wed, Dec 14, 2016 at 1:24 AM Amina Shahzadi 
wrote:

> Hello Avraham --Happy to see you
>
> My code is trying to produce a cube c which is going to be constructed by
> a vector a and matrix b.
> And the number of rows in b and size of a must be same.
>
> So we can assume that if a is a vector of size 3, Then b must be 2 x 3 or
> 3 X 3 etc.
>
> Thank you Avraham for quick response. I hope this will make my question
> more clear.
>
> Best regards
>
>
> On Wed, Dec 14, 2016 at 4:46 PM, Avraham Adler 
> wrote:
>
> On Tue, Dec 13, 2016 at 9:51 PM, Amina Shahzadi 
> wrote:
>
> Hello Friends and Prof. Dirk
>
> I am pasting here a code which has a for loop depending on another for
> loop.
> I am getting zeros for cube c. I tried and searched a lot but did not get
> an example of this type. Would you please help in this regard?
>
>
> #include 
> using namespace Rcpp;
> using namespace RcppArmadillo;
> using namespace arma;
> //[[Rcpp::depends(RcppArmadillo)]]
> //[[Rcpp::export]]
>
>
> arma::cube  exam(arma::vec a,  arma::mat b)
> {
>   int m = a.size();
>   int n = b.n_rows;
>   arma::cube c = zeros(n, m, n);
>   for(int i=0; i for(int j=0; j   for(int k=0; k   if(k==0) {
> c(i, j ,k) = c(i, j, k) + b(i, j);
>   }
>   else{
> c(i, j, k) = c(i, j, k) +  c(i-1, j, k) *b(i, j);
>   }
>  }
> }
>   }
>   return c;
> }
>
>
> Thank You
> --
> *Amina Shahzadi*
>
>
>
>
> Hello.
>
> I haven't run your code, but it strikes me
>
> that I cannot see where are you capturing the number of columns of b.
>
> It's a bit confusing as I was always taught a matrix has m rows and n
>
> columns. Be that as it may, your k==0 loop looks like it's trying to
>
> copy over the original matrix to the first slice, but how do you know
>
> that b has m columns, which is what you're assuming by letting j loop to
>
> m. Unless you are assuming a square matrix?
>
> Even if you are, if your matrix is not the same length as your vector, I
> think there is an issue with your loop boundaries, unless I've
> misunderstood something, which is certainly possible.
>
> For example, assume a is {1, 2, 3} and b is the 2 x 2 of row 1: [1 2] and
> row 2: [3 4]. Thus m = 3 and n = 2.
>
> Step 1: i = j = k = 0: c(0, 0, 0) becomes b(0, 0) or 1.
>
> Step 2: i = 0, j = 1, k = 0: c(0, 1, 0) becomes b(0, 1) or 2.
>
> Step 3: i = 0, j = 2, k = 0: c(0, 2, 0) becomes b(0, 2) ?!?! There is no
> b(0, 2), it's only a 2x2 matrix?
>
>
> Similar to your previous questions, instead of posting code, can you
> please describe in words what it is you are trying to do? That may help.
>
> Avi
>
>
>
>
>
>
> --
> *Amina Shahzadi*
>
>
> Constructed how? Can you provide a simple set of inputs and the expected
output?

Avi
-- 
Sent from Gmail Mobile
___
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] a variable for loop

2016-12-13 Thread Amina Shahzadi
Oh sorry. No. of columns in b and size of a must always be same.
I have made an r code to show the output.


a = c(1, 2, 3)
b = matrix(1:30, nrow=10, ncol=3)
c = array(as.double(0), dim=c(10, 3, 10))
for(i in 1:10){
  for(j in 1:3){
for(k in 1:i){
  if(k==1) c[i, j, k] = b[i, j]
  else c[i, j, k] = c[i-1, j, k-1] * b[i, j]
}
  }
}

Output:

> c, , 1

  [,1] [,2] [,3]
 [1,]1   11   21
 [2,]2   12   22
 [3,]3   13   23
 [4,]4   14   24
 [5,]5   15   25
 [6,]6   16   26
 [7,]7   17   27
 [8,]8   18   28
 [9,]9   19   29
[10,]   10   20   30

, , 2

  [,1] [,2] [,3]
 [1,]000
 [2,]2  132  462
 [3,]6  156  506
 [4,]   12  182  552
 [5,]   20  210  600
 [6,]   30  240  650
 [7,]   42  272  702
 [8,]   56  306  756
 [9,]   72  342  812
[10,]   90  380  870

, , 3

  [,1] [,2]  [,3]
 [1,]00 0
 [2,]00 0
 [3,]6 1716 10626
 [4,]   24 2184 12144
 [5,]   60 2730 13800
 [6,]  120 3360 15600
 [7,]  210 4080 17550
 [8,]  336 4896 19656
 [9,]  504 5814 21924
[10,]  720 6840 24360

, , 4

  [,1]   [,2]   [,3]
 [1,]0  0  0
 [2,]0  0  0
 [3,]0  0  0
 [4,]   24  24024 255024
 [5,]  120  32760 303600
 [6,]  360  43680 358800
 [7,]  840  57120 421200
 [8,] 1680  73440 491400
 [9,] 3024  93024 570024
[10,] 5040 116280 657720

, , 5

   [,1][,2] [,3]
 [1,] 0   00
 [2,] 0   00
 [3,] 0   00
 [4,] 0   00
 [5,]   120  360360  6375600
 [6,]   720  524160  7893600
 [7,]  2520  742560  9687600
 [8,]  6720 1028160 11793600
 [9,] 15120 1395360 14250600
[10,] 30240 1860480 17100720

, , 6

[,1] [,2]  [,3]
 [1,]  00 0
 [2,]  00 0
 [3,]  00 0
 [4,]  00 0
 [5,]  00 0
 [6,]720  5765760 165765600
 [7,]   5040  8910720 213127200
 [8,]  20160 13366080 271252800
 [9,]  60480 19535040 342014400
[10,] 151200 27907200 427518000

, , 7

[,1]  [,2][,3]
 [1,]  0 0   0
 [2,]  0 0   0
 [3,]  0 0   0
 [4,]  0 0   0
 [5,]  0 0   0
 [6,]  0 0   0
 [7,]   5040  98017920  4475671200
 [8,]  40320 160392960  5967561600
 [9,] 181440 253955520  7866331200
[10,] 604800 390700800 10260432000

, , 8

 [,1]   [,2] [,3]
 [1,]   0  00
 [2,]   0  00
 [3,]   0  00
 [4,]   0  00
 [5,]   0  00
 [6,]   0  00
 [7,]   0  00
 [8,]   40320 1764322560 125318793600
 [9,]  362880 3047466240 173059286400
[10,] 1814400 5079110400 235989936000

, , 9

 [,1][,2] [,3]
 [1,]   0   0 0.00e+00
 [2,]   0   0 0.00e+00
 [3,]   0   0 0.00e+00
 [4,]   0   0 0.00e+00
 [5,]   0   0 0.00e+00
 [6,]   0   0 0.00e+00
 [7,]   0   0 0.00e+00
 [8,]   0   0 0.00e+00
 [9,]  362880 33522128640 3.634245e+12
[10,] 3628800 60949324800 5.191779e+12

, , 10

 [,1] [,2] [,3]
 [1,]   00 0.00e+00
 [2,]   00 0.00e+00
 [3,]   00 0.00e+00
 [4,]   00 0.00e+00
 [5,]   00 0.00e+00
 [6,]   00 0.00e+00
 [7,]   00 0.00e+00
 [8,]   00 0.00e+00
 [9,]   00 0.00e+00
[10,] 3628800 670442572800 1.090274e+14




On Wed, Dec 14, 2016 at 7:27 PM, Avraham Adler 
wrote:

>
>
> On Wed, Dec 14, 2016 at 1:24 AM Amina Shahzadi 
> wrote:
>
>> Hello Avraham --Happy to see you
>>
>> My code is trying to produce a cube c which is going to be constructed by
>> a vector a and matrix b.
>> And the number of rows in b and size of a must be same.
>>
>> So we can assume that if a is a vector of size 3, Then b must be 2 x 3 or
>> 3 X 3 etc.
>>
>> Thank you Avraham for quick response. I hope this will make my question
>> more clear.
>>
>> Best regards
>>
>>
>> On Wed, Dec 14, 2016 at 4:46 PM, Avraham Adler 
>> wrote:
>>
>> On Tue, Dec 13, 2016 at 9:51 PM, Amina Shahzadi 
>> wrote:
>>
>> Hello Friends and Prof. Dirk
>>
>> I am pasting here a code which has a for loop depending on another for
>> loop.
>> I am getting zeros for cube c. I tried and searched a lot but did not get
>> an example of this type. Would you please help in this regard?
>>
>>
>> #include 
>> using namespace Rcpp;
>> using namespace RcppArmadillo;
>> using namespace arma;
>> //[[Rcpp::depends(RcppArmadillo)]]
>> //[[Rcpp::export]]
>>
>>
>> arma::cube  exam(arma::vec a,  arma::mat b)
>> {
>>   int m = a.size();
>>   int n = b.n_rows;
>>   arma::cube c = zeros(n, m, n);
>>