Discussion:
[Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp
akshay kulkarni
2018-03-20 11:10:51 UTC
Permalink
dear members,

I came to know from stackoverflow that the following references a row in a matrix in C++:

M[2] references 2nd row of the Matrix.


I am using Rcpp to write C++ code in R.


However, I ended up with the following inconsistency:

> M
x y z
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
[4,] 4 4 4
[5,] 5 5 5
[6,] 6 6 6
> cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; return x;}')
> tccp3(M)
[1] 0 0 0
> cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; return x;}')
> tccp4(M)
[1] 0 0

tccp3 should return (3,3,3) and tccp4 should return (2,2,2). Can you please shed light on what is going on?

very many thanks for your time and effort....

Yours sincerely,
AKSHAY M KULKARNI
Serguei Sokol
2018-03-20 13:56:51 UTC
Permalink
Le 20/03/2018 à 12:10, akshay kulkarni a écrit :
> dear members,
>
>                              I came to know from stackoverflow that the following references a row in a matrix in C++:
>
> M[2] references 2nd row of the Matrix.
SO is too big to check this assertion by ourself. Do you have a link?

>
>
> I am using Rcpp to write C++ code in R.
>
>
> However, I ended up with the following inconsistency:
>
> > M
>      x y z
> [1,] 1 1 1
> [2,] 2 2 2
> [3,] 3 3 3
> [4,] 4 4 4
> [5,] 5 5 5
> [6,] 6 6 6
> > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; return x;}')
Try

cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); return x;}')

Best,
Serguei.


> > tccp3(M)
> [1] 0 0 0
> > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; return x;}')
> > tccp4(M)
> [1] 0 0
>
> tccp3 should return (3,3,3) and tccp4 should return  (2,2,2). Can you please shed light on what is going on?
>
> very many thanks for your time and effort....
>
> Yours sincerely,
> AKSHAY M KULKARNI
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-***@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
akshay kulkarni
2018-03-20 15:54:51 UTC
Permalink
dear serguei,

Thanks...

Can I also write this:

M[1:i, n:m] as we write in R?

If not, are there any packages that do it for me?

what about RcppEigen?


Basically, I want to write the following R code:

{ p <- 1;

for(i in 550:nrow(x)){

y[p] <- f(x[1:i,])

p <- p+1;

return(y)

}

in C++ using Rcpp. what is the best way to do that?

x is an xts object containing OHLC historical stock prices of the stock x and f acts on the daily increments of x.

very many thanks for your time and effort....


yours sincerely,

AKSHAY M KULKARNI

________________________________
From: Serguei Sokol <***@gmail.com>
Sent: Tuesday, March 20, 2018 7:26:51 PM
To: akshay kulkarni; Rcpp R
Subject: Re: [Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp

Le 20/03/2018 à 12:10, akshay kulkarni a écrit :
> dear members,
>
> I came to know from stackoverflow that the following references a row in a matrix in C++:
>
> M[2] references 2nd row of the Matrix.
SO is too big to check this assertion by ourself. Do you have a link?

>
>
> I am using Rcpp to write C++ code in R.
>
>
> However, I ended up with the following inconsistency:
>
> > M
> x y z
> [1,] 1 1 1
> [2,] 2 2 2
> [3,] 3 3 3
> [4,] 4 4 4
> [5,] 5 5 5
> [6,] 6 6 6
> > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; return x;}')
Try

cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); return x;}')

Best,
Serguei.


> > tccp3(M)
> [1] 0 0 0
> > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; return x;}')
> > tccp4(M)
> [1] 0 0
>
> tccp3 should return (3,3,3) and tccp4 should return (2,2,2). Can you please shed light on what is going on?
>
> very many thanks for your time and effort....
>
> Yours sincerely,
> AKSHAY M KULKARNI
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-***@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
Dirk Eddelbuettel
2018-03-20 16:22:45 UTC
Permalink
Akshay,

On 20 March 2018 at 15:54, akshay kulkarni wrote:
| Can I also write this:
|
| M[1:i, n:m] as we write in R?

Please consider to stop posting elementary C++ beginners question to the
list. We do not provide as a C++ tutorial or training service here. Please
learn at least /some/ C++ after which a re-reading of the Rcpp documentation
will make more sense to you.

Thanks, Dirk

--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Serguei Sokol
2018-03-20 16:32:11 UTC
Permalink
Le 20/03/2018 à 17:22, Dirk Eddelbuettel a écrit :
>
> Akshay,
>
> On 20 March 2018 at 15:54, akshay kulkarni wrote:
> | Can I also write this:
> |
> | M[1:i, n:m] as we write in R?
>
> Please consider to stop posting elementary C++ beginners question to the
> list. We do not provide as a C++ tutorial or training service here. Please
> learn at least /some/ C++ after which a re-reading of the Rcpp documentation
> will make more sense to you.
+1

However, to reach a point I can say that certainly RcppEigen will allow you to do this
with block operations you have already mentioned. But also RcppArmadillo is another
alternative well suited for this kind of basic operations.
You'll have some homework on your side ;)

Best,
Serguei.

>
> Thanks, Dirk
>
akshay kulkarni
2018-03-21 08:40:42 UTC
Permalink
dear serguei,

I have another question:

If M is a matrix, M.nrow() should return the number of rows...but I encountered the following inconsistency:


> M
x y z
[1,] 1 0 0
[2,] 4 3 0
[3,] 3 1 1
[4,] 2 90 87
[5,] 9 76 23
[6,] 5 23 13
> cppFunction('IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}')
> tccp5(M)
[1] 0 0 0 0 0 0

can you please explain what is happening( I am very new to Rcpp and C++)?

very many thanks for your time and effort...
yours sincerely,
AKSHAY M KULKARNI



________________________________
From: Serguei Sokol <***@gmail.com>
Sent: Tuesday, March 20, 2018 7:26 PM
To: akshay kulkarni; Rcpp R
Subject: Re: [Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp

Le 20/03/2018 à 12:10, akshay kulkarni a écrit :
> dear members,
>
> I came to know from stackoverflow that the following references a row in a matrix in C++:
>
> M[2] references 2nd row of the Matrix.
SO is too big to check this assertion by ourself. Do you have a link?

>
>
> I am using Rcpp to write C++ code in R.
>
>
> However, I ended up with the following inconsistency:
>
> > M
> x y z
> [1,] 1 1 1
> [2,] 2 2 2
> [3,] 3 3 3
> [4,] 4 4 4
> [5,] 5 5 5
> [6,] 6 6 6
> > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; return x;}')
Try

cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); return x;}')

Best,
Serguei.


> > tccp3(M)
> [1] 0 0 0
> > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; return x;}')
> > tccp4(M)
> [1] 0 0
>
> tccp3 should return (3,3,3) and tccp4 should return (2,2,2). Can you please shed light on what is going on?
>
> very many thanks for your time and effort....
>
> Yours sincerely,
> AKSHAY M KULKARNI
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-***@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Rcpp-devel Info Page<https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
lists.r-forge.r-project.org
Discussion list for Rcpp, RInside and various packages using Rcpp or RInside. This list is subscriber-only, in other words in order to post to the list you must be ...



>
Serguei Sokol
2018-03-21 09:39:30 UTC
Permalink
Dear Akshay,

In previous messages, Dirk and I told you that this list was not
the right place for beginner questions. Please stop asking that kind
of questions here.

Best,
Serguei.

Le 21/03/2018 à 09:40, akshay kulkarni a écrit :
> dear serguei,
>
>                          I have another question:
>
> If M is a matrix, M.nrow() should return the number of rows...but I encountered the following inconsistency:
>
>
> > M
>      x  y  z
> [1,] 1  0  0
> [2,] 4  3  0
> [3,] 3  1  1
> [4,] 2 90 87
> [5,] 9 76 23
> [6,] 5 23 13
> > cppFunction('IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}')
> > tccp5(M)
> [1] 0 0 0 0 0 0
>
> can you please explain what is happening( I am very new to Rcpp and C++)?
>
> very many thanks for your time and effort...
> yours sincerely,
> AKSHAY M KULKARNI
>
>
>
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------
> *From:* Serguei Sokol <***@gmail.com>
> *Sent:* Tuesday, March 20, 2018 7:26 PM
> *To:* akshay kulkarni; Rcpp R
> *Subject:* Re: [Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp
> Le 20/03/2018 à 12:10, akshay kulkarni a écrit :
>> dear members,
>>
>>                               I came to know from stackoverflow that the following references a row in a matrix in C++:
>>
>> M[2] references 2nd row of the Matrix.
> SO is too big to check this assertion by ourself. Do you have a link?
>
>>
>>
>> I am using Rcpp to write C++ code in R.
>>
>>
>> However, I ended up with the following inconsistency:
>>
>>  > M
>>       x y z
>> [1,] 1 1 1
>> [2,] 2 2 2
>> [3,] 3 3 3
>> [4,] 4 4 4
>> [5,] 5 5 5
>> [6,] 6 6 6
>>  > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; return x;}')
> Try
>
> cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); return x;}')
>
> Best,
> Serguei.
>
>
>>  > tccp3(M)
>> [1] 0 0 0
>>  > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; return x;}')
>>  > tccp4(M)
>> [1] 0 0
>>
>> tccp3 should return (3,3,3) and tccp4 should return  (2,2,2). Can you please shed light on what is going on?
>>
>> very many thanks for your time and effort....
>>
>> Yours sincerely,
>> AKSHAY M KULKARNI
>>
>>
>>
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-***@lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> Rcpp-devel Info Page <https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
> lists.r-forge.r-project.org
> Discussion list for Rcpp, RInside and various packages using Rcpp or RInside. This list is subscriber-only, in other words in order to post to the list you must
> be ...
>
>
>
>>
>
Dirk Eddelbuettel
2018-03-21 13:20:11 UTC
Permalink
On 21 March 2018 at 10:39, Serguei Sokol wrote:
| Dear Akshay,
|
| In previous messages, Dirk and I told you that this list was not
| the right place for beginner questions. Please stop asking that kind
| of questions here.

Or I will unsubscribe you here. Your choice.

Dirk

| Best,
| Serguei.
|
| Le 21/03/2018 à 09:40, akshay kulkarni a écrit :
| > dear serguei,
| >
| >                          I have another question:
| >
| > If M is a matrix, M.nrow() should return the number of rows...but I encountered the following inconsistency:
| >
| >
| > > M
| >      x  y  z
| > [1,] 1  0  0
| > [2,] 4  3  0
| > [3,] 3  1  1
| > [4,] 2 90 87
| > [5,] 9 76 23
| > [6,] 5 23 13
| > > cppFunction('IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return x;}')
| > > tccp5(M)
| > [1] 0 0 0 0 0 0
| >
| > can you please explain what is happening( I am very new to Rcpp and C++)?
| >
| > very many thanks for your time and effort...
| > yours sincerely,
| > AKSHAY M KULKARNI
| >
| >
| >
| > ----------------------------------------------------------------------------------------------------------------------------------------------------------------
| > *From:* Serguei Sokol <***@gmail.com>
| > *Sent:* Tuesday, March 20, 2018 7:26 PM
| > *To:* akshay kulkarni; Rcpp R
| > *Subject:* Re: [Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp
| > Le 20/03/2018 à 12:10, akshay kulkarni a écrit :
| >> dear members,
| >>
| >>                               I came to know from stackoverflow that the following references a row in a matrix in C++:
| >>
| >> M[2] references 2nd row of the Matrix.
| > SO is too big to check this assertion by ourself. Do you have a link?
| >
| >>
| >>
| >> I am using Rcpp to write C++ code in R.
| >>
| >>
| >> However, I ended up with the following inconsistency:
| >>
| >>  > M
| >>       x y z
| >> [1,] 1 1 1
| >> [2,] 2 2 2
| >> [3,] 3 3 3
| >> [4,] 4 4 4
| >> [5,] 5 5 5
| >> [6,] 6 6 6
| >>  > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; return x;}')
| > Try
| >
| > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); return x;}')
| >
| > Best,
| > Serguei.
| >
| >
| >>  > tccp3(M)
| >> [1] 0 0 0
| >>  > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; return x;}')
| >>  > tccp4(M)
| >> [1] 0 0
| >>
| >> tccp3 should return (3,3,3) and tccp4 should return  (2,2,2). Can you please shed light on what is going on?
| >>
| >> very many thanks for your time and effort....
| >>
| >> Yours sincerely,
| >> AKSHAY M KULKARNI
| >>
| >>
| >>
| >> _______________________________________________
| >> Rcpp-devel mailing list
| >> Rcpp-***@lists.r-forge.r-project.org
| >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| > Rcpp-devel Info Page <https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
| > lists.r-forge.r-project.org
| > Discussion list for Rcpp, RInside and various packages using Rcpp or RInside. This list is subscriber-only, in other words in order to post to the list you must
| > be ...
| >
| >
| >
| >>
| >
|
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-***@lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Loading...