[Rcpp-devel] Multiplication of ComplexVector?

2010-08-16 Thread Christian Gunning
Dear list,

I'm trying to use the ComplexVector class, and I'm having difficulty
with multiplication. I sugar multiplication, element-by-element
multiplication (no * operator for either), and using temporary
Rcomplex variables (don't really understand what I'm doing here).

I was able to successfully import get("*") as an Rcpp::Function and do
the multiplication through R. Is this the best option for now?

Thanks,
Christian Gunning
University of New Mexico

// doesn't work
SEXP rcpp_hello_world(SEXP i, SEXP ii){
using namespace Rcpp ;

ComplexVector y1(i), y2(ii);
int n(y1.size()), j;
ComplexVector y3(n), y4(n);
y3 = y1 * y2;  // no operator
for (j = 0; j++; jhttps://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Multiplication of ComplexVector?

2010-08-16 Thread Dirk Eddelbuettel

Hi Christian,

Thanks for your interest in Rcpp, and for posting here.

On 16 August 2010 at 17:19, Christian Gunning wrote:
| Dear list,
| 
| I'm trying to use the ComplexVector class, and I'm having difficulty
| with multiplication. I sugar multiplication, element-by-element
| multiplication (no * operator for either), and using temporary
| Rcomplex variables (don't really understand what I'm doing here).

Well, NumericVector et al aren't really made for all possible math ops done
on vectors, real or complex. They are first and foremost storage types. But
for the math we do have Armadillo and RcppArmadillo. So in the short run, I
would suggest the following two-step procedure:

   i)   have a good look at Armadillo (http://arma.sf.net) and use the cx_mat
or cx_vec types for mulitplication; write a small test program to see
that everything works

   ii)  use RcppArmadillo to from R via Rcpp / RcppArmadillo to this
functionality

| I was able to successfully import get("*") as an Rcpp::Function and do
| the multiplication through R. Is this the best option for now?

I am sure we can do better than this but I am not sure all pieces are in
place just yet.  

Cheers, Dirk


| Thanks,
| Christian Gunning
| University of New Mexico
| 
| // doesn't work
| SEXP rcpp_hello_world(SEXP i, SEXP ii){
| using namespace Rcpp ;
| 
| ComplexVector y1(i), y2(ii);
| int n(y1.size()), j;
| ComplexVector y3(n), y4(n);
| y3 = y1 * y2;  // no operator
| for (j = 0; j++; jhttps://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
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] Multiplication of ComplexVector?

2010-08-16 Thread Dirk Eddelbuettel

On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:
| Hi Christian,
| 
| Thanks for your interest in Rcpp, and for posting here.
| 
| On 16 August 2010 at 17:19, Christian Gunning wrote:
| | Dear list,
| | 
| | I'm trying to use the ComplexVector class, and I'm having difficulty
| | with multiplication. I sugar multiplication, element-by-element
| | multiplication (no * operator for either), and using temporary
| | Rcomplex variables (don't really understand what I'm doing here).
| 
| Well, NumericVector et al aren't really made for all possible math ops done
| on vectors, real or complex. They are first and foremost storage types. But
| for the math we do have Armadillo and RcppArmadillo. So in the short run, I
| would suggest the following two-step procedure:
| 
|i)   have a good look at Armadillo (http://arma.sf.net) and use the cx_mat
| or cx_vec types for mulitplication; write a small test program to see
| that everything works
| 
|ii)  use RcppArmadillo to from R via Rcpp / RcppArmadillo to this
| functionality
| 
| | I was able to successfully import get("*") as an Rcpp::Function and do
| | the multiplication through R. Is this the best option for now?
| 
| I am sure we can do better than this but I am not sure all pieces are in
| place just yet.  

Looks like I wasn't all that far off.  This builds:

  library(inline)
  
  code <- '
  ComplexVector y1(i), y2(ii);
  //arma::cx_vec a1(y1.begin(), y1.size(), false);
  //arma::cx_vec a2(y2.begin(), y2.size(), false);
  arma::cx_vec a1(y1.size());
  arma::cx_vec a2(y2.size());
  arma::cx_vec a3 = a1 * a2;
  arma::cx_vec a4 = a1 + a2;
  List z = List::create(a1, a2, a3, a4);
  return z;
  '
  
  fun <- cxxfunction(signature(i="compex", ii="comples"), code, 
plugin="RcppArmadillo")
  
The only trouble is that nobody has written the corresponding 'glue' code to
make

  arma::cx_vec a1(y1.begin(), y1.size(), false);

happen: create an Armadillo complex vector from an Rcpp::ComplexVector.  We
can init by scalar size, what you'd need to insert for now is a simply (and
very pedestrian) copy-loop.

Hth, Dirk

  
| 
| Cheers, Dirk
| 
| 
| | Thanks,
| | Christian Gunning
| | University of New Mexico
| | 
| | // doesn't work
| | SEXP rcpp_hello_world(SEXP i, SEXP ii){
| | using namespace Rcpp ;
| | 
| | ComplexVector y1(i), y2(ii);
| | int n(y1.size()), j;
| | ComplexVector y3(n), y4(n);
| | y3 = y1 * y2;  // no operator
| | for (j = 0; j++; jhttps://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| 
| -- 
| 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

-- 
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] Multiplication of ComplexVector?

2010-08-16 Thread baptiste auguie
Hi,

On 17 August 2010 03:24, Dirk Eddelbuettel  wrote:
>
> On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:

[...]

> The only trouble is that nobody has written the corresponding 'glue' code to
> make
>
>      arma::cx_vec a1(y1.begin(), y1.size(), false);
>
> happen: create an Armadillo complex vector from an Rcpp::ComplexVector.  We
> can init by scalar size, what you'd need to insert for now is a simply (and
> very pedestrian) copy-loop.

I'm confused, isn't

 arma::cx_colvec a1 = Rcpp::as< arma::cx_vec >( y1 );

aimed at doing this kind of conversion? (that's what I use, following
Romain's tip)

baptiste
___
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] Multiplication of ComplexVector?

2010-08-16 Thread Romain Francois

Thank you for your interest in Rcpp.

Le 17/08/10 02:19, Christian Gunning a écrit :

Dear list,

I'm trying to use the ComplexVector class, and I'm having difficulty
with multiplication. I sugar multiplication, element-by-element
multiplication (no * operator for either), and using temporary
Rcomplex variables (don't really understand what I'm doing here).


All is needed are binary operators on Rcomplex, I will add them in Rcpp 
before the next release. Or maybe someone else will follow my footsteps 
and do it.


One other thing about your code is that your loop is wrong :

for (j = 0; j++; j fx( y1, y2 )
$`*`
 [1] 0+  4i 0+ 16i 0+ 36i 0+ 64i 0+100i 0+144i 0+196i 0+256i 0+324i 0+400i

$`+`
 [1]  3+ 3i  6+ 6i  9+ 9i 12+12i 15+15i 18+18i 21+21i 24+24i 27+27i 30+30i

$`y1*y2 + y1`
 [1]  1+  5i  2+ 18i  3+ 39i  4+ 68i  5+105i  6+150i  7+203i  8+264i 
9+333i

[10] 10+410i

$`y1 + y2*y2`
 [1]  1+  9i  2+ 34i  3+ 75i  4+132i  5+205i  6+294i  7+399i  8+520i 
9+657i

[10] 10+810i


Romain


I was able to successfully import get("*") as an Rcpp::Function and do
the multiplication through R. Is this the best option for now?

Thanks,
Christian Gunning
University of New Mexico

// doesn't work
SEXP rcpp_hello_world(SEXP i, SEXP ii){
 using namespace Rcpp ;

 ComplexVector y1(i), y2(ii);
 int n(y1.size()), j;
 ComplexVector y3(n), y4(n);
 y3 = y1 * y2;  // no operator
 for (j = 0; j++; j


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-17 Thread Romain Francois

Le 17/08/10 02:43, Dirk Eddelbuettel a écrit :


Hi Christian,

Thanks for your interest in Rcpp, and for posting here.

On 16 August 2010 at 17:19, Christian Gunning wrote:
| Dear list,
|
| I'm trying to use the ComplexVector class, and I'm having difficulty
| with multiplication. I sugar multiplication, element-by-element
| multiplication (no * operator for either), and using temporary
| Rcomplex variables (don't really understand what I'm doing here).

Well, NumericVector et al aren't really made for all possible math ops done
on vectors, real or complex. They are first and foremost storage types.


Maybe that was true two months ago before the sugar rush. We just 
overlooked that we needed operator+(Rcomplex, Rcomplex), etc ...


see my direct response to Christian.


But for the math we do have Armadillo and RcppArmadillo. So in the short run, I
would suggest the following two-step procedure:

i)   have a good look at Armadillo (http://arma.sf.net) and use the cx_mat
 or cx_vec types for mulitplication; write a small test program to see
 that everything works

ii)  use RcppArmadillo to from R via Rcpp / RcppArmadillo to this
 functionality

| I was able to successfully import get("*") as an Rcpp::Function and do
| the multiplication through R. Is this the best option for now?

I am sure we can do better than this but I am not sure all pieces are in
place just yet.

Cheers, Dirk


| Thanks,
| Christian Gunning
| University of New Mexico
|
| // doesn't work
| SEXP rcpp_hello_world(SEXP i, SEXP ii){
| using namespace Rcpp ;
|
| ComplexVector y1(i), y2(ii);
| int n(y1.size()), j;
| ComplexVector y3(n), y4(n);
| y3 = y1 * y2;  // no operator
| for (j = 0; j++; jhttps://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/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-17 Thread Romain Francois

Le 17/08/10 03:24, Dirk Eddelbuettel a écrit :


On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:
| Hi Christian,
|
| Thanks for your interest in Rcpp, and for posting here.
|
| On 16 August 2010 at 17:19, Christian Gunning wrote:
| | Dear list,
| |
| | I'm trying to use the ComplexVector class, and I'm having difficulty
| | with multiplication. I sugar multiplication, element-by-element
| | multiplication (no * operator for either), and using temporary
| | Rcomplex variables (don't really understand what I'm doing here).
|
| Well, NumericVector et al aren't really made for all possible math ops done
| on vectors, real or complex. They are first and foremost storage types. But
| for the math we do have Armadillo and RcppArmadillo. So in the short run, I
| would suggest the following two-step procedure:
|
|i)   have a good look at Armadillo (http://arma.sf.net) and use the cx_mat
| or cx_vec types for mulitplication; write a small test program to see
| that everything works
|
|ii)  use RcppArmadillo to from R via Rcpp / RcppArmadillo to this
| functionality
|
| | I was able to successfully import get("*") as an Rcpp::Function and do
| | the multiplication through R. Is this the best option for now?
|
| I am sure we can do better than this but I am not sure all pieces are in
| place just yet.

Looks like I wasn't all that far off.  This builds:

   library(inline)

   code<- '
   ComplexVector y1(i), y2(ii);
   //arma::cx_vec a1(y1.begin(), y1.size(), false);
   //arma::cx_vec a2(y2.begin(), y2.size(), false);
   arma::cx_vec a1(y1.size());
   arma::cx_vec a2(y2.size());
   arma::cx_vec a3 = a1 * a2;
   arma::cx_vec a4 = a1 + a2;
   List z = List::create(a1, a2, a3, a4);
   return z;
   '

   fun<- cxxfunction(signature(i="compex", ii="comples"), code, 
plugin="RcppArmadillo")

The only trouble is that nobody has written the corresponding 'glue' code to
make

   arma::cx_vec a1(y1.begin(), y1.size(), false);


cx_mat holds an array of std::complex, not an array of Rcomplex. 
so the compiler is unhappy.


However, Rcomplex and std::complex have the same layout in 
memory, so you are one reinterpret_cast away :



require( inline )

code<- '
ComplexVector y1(i), y2(ii);
arma::cx_vec a1( reinterpret_cast< std::complex* 
>(y1.begin()), y1.size(), false);
arma::cx_vec a2( reinterpret_cast< std::complex* 
>(y2.begin()), y2.size(), false);

arma::cx_vec a3 = a1 % a2; // element-wise multiplication in armadillo
arma::cx_vec a4 = a1 + a2;
List z = List::create(a1, a2, a3, a4);
return z;
'
fun<- cxxfunction(signature(i="complex", ii="complex"), code, 
plugin="RcppArmadillo")


fun( 1:10*(1+1i), (1:10)*2+2i )


happen: create an Armadillo complex vector from an Rcpp::ComplexVector.  We
can init by scalar size, what you'd need to insert for now is a simply (and
very pedestrian) copy-loop.

Hth, Dirk


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-17 Thread Romain Francois

Le 17/08/10 07:43, baptiste auguie a écrit :

Hi,

On 17 August 2010 03:24, Dirk Eddelbuettel  wrote:


On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:


[...]


The only trouble is that nobody has written the corresponding 'glue' code to
make

  arma::cx_vec a1(y1.begin(), y1.size(), false);

happen: create an Armadillo complex vector from an Rcpp::ComplexVector.  We
can init by scalar size, what you'd need to insert for now is a simply (and
very pedestrian) copy-loop.


I'm confused, isn't

  arma::cx_colvec a1 = Rcpp::as<  arma::cx_vec>( y1 );

aimed at doing this kind of conversion? (that's what I use, following
Romain's tip)

baptiste


I don't think you are confused. This works well, but it does require 
some extra copying, which Dirk was trying to spare.


Using the "advanced" constructor in armadillo means that both the 
ComplexVector and the cx_vec use the same memory. although it says loud 
and clear in armadillo documentation :


"""
This is faster, but can be dangerous unless you know what you're doing!
"""

Romain

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-17 Thread baptiste auguie
On 17 August 2010 09:20, Romain Francois  wrote:
> Le 17/08/10 07:43, baptiste auguie a écrit :
>>
>> Hi,
>>
>> On 17 August 2010 03:24, Dirk Eddelbuettel  wrote:
>>>
>>> On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:
>>
>> [...]
>>
>>> The only trouble is that nobody has written the corresponding 'glue' code
>>> to
>>> make
>>>
>>>      arma::cx_vec a1(y1.begin(), y1.size(), false);
>>>
>>> happen: create an Armadillo complex vector from an Rcpp::ComplexVector.
>>>  We
>>> can init by scalar size, what you'd need to insert for now is a simply
>>> (and
>>> very pedestrian) copy-loop.
>>
>> I'm confused, isn't
>>
>>  arma::cx_colvec a1 = Rcpp::as<  arma::cx_vec>( y1 );
>>
>> aimed at doing this kind of conversion? (that's what I use, following
>> Romain's tip)
>>
>> baptiste
>
> I don't think you are confused. This works well, but it does require some
> extra copying, which Dirk was trying to spare.
>
> Using the "advanced" constructor in armadillo means that both the
> ComplexVector and the cx_vec use the same memory. although it says loud and
> clear in armadillo documentation :
>
> """
> This is faster, but can be dangerous unless you know what you're doing!
> """


Thanks. Two questions then,

1- reinterpret_cast< std::complex* > seems like a good option,
does it work with real / complex matrices as well? The only things
that came up from googling it were related to RcppGSL, not
RcppArmadillo.

2- when is it not safe? ;)

Thanks!

baptiste

PS: i wouldn't want to hijack the original query; I'll open a new
thread if this seems to raise further questions.
___
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] Multiplication of ComplexVector?

2010-08-17 Thread Romain Francois

Le 17/08/10 10:00, baptiste auguie a écrit :

On 17 August 2010 09:20, Romain Francois  wrote:

Le 17/08/10 07:43, baptiste auguie a écrit :


Hi,

On 17 August 2010 03:24, Dirk Eddelbuettelwrote:


On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:


[...]


The only trouble is that nobody has written the corresponding 'glue' code
to
make

  arma::cx_vec a1(y1.begin(), y1.size(), false);

happen: create an Armadillo complex vector from an Rcpp::ComplexVector.
  We
can init by scalar size, what you'd need to insert for now is a simply
(and
very pedestrian) copy-loop.


I'm confused, isn't

  arma::cx_colvec a1 = Rcpp::as( y1 );

aimed at doing this kind of conversion? (that's what I use, following
Romain's tip)

baptiste


I don't think you are confused. This works well, but it does require some
extra copying, which Dirk was trying to spare.

Using the "advanced" constructor in armadillo means that both the
ComplexVector and the cx_vec use the same memory. although it says loud and
clear in armadillo documentation :

"""
This is faster, but can be dangerous unless you know what you're doing!
"""



Thanks. Two questions then,

1- reinterpret_cast<  std::complex*>  seems like a good option,
does it work with real / complex matrices as well? The only things
that came up from googling it were related to RcppGSL, not
RcppArmadillo.


Sure.

code<- '
ComplexMatrix y1(i), y2(ii);
arma::cx_mat a1( reinterpret_cast< std::complex* 
>(y1.begin()), y1.nrow(), y1.ncol(), false);
arma::cx_mat a2( reinterpret_cast< std::complex* 
>(y2.begin()), y2.nrow(), y2.ncol(), false);

return wrap( a1 + a2);
'
fun<- cxxfunction(signature(i="complex", ii = "complex"), code, 
plugin="RcppArmadillo")


x1 <- matrix( 1:10*(1+1i), 5, 2 )
x2 <- matrix( 1*1:10*(1+1i), 5, 2 )
fun( x1, x2 )

You don't need reinterpret_cast for converting NumericMatrix to a 
Mat aka mat because they both use the same type and layout for 
storage : double with column major order layout



2- when is it not safe? ;)


When people don't know what they are doing.

For example : R has no knowledge that armadillo shares the memory with 
it, so if for some reason R thinks it can garbage collect the SEXP that 
came in ("i" and "ii"), it won't ask around for permission and so the 
armadillo matrix/vector will contain a pointer to wonderland, and then 
killing the jabbawockee bug is not easy.


So if all you do is share the memory inside the same function/scope you 
should be fine.



Thanks!

baptiste


And sorry it did not occur to me before.


PS: i wouldn't want to hijack the original query; I'll open a new
thread if this seems to raise further questions.


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-17 Thread Dirk Eddelbuettel

On 17 August 2010 at 09:06, Romain Francois wrote:
| Le 17/08/10 02:43, Dirk Eddelbuettel a écrit :
| > Well, NumericVector et al aren't really made for all possible math ops done
| > on vectors, real or complex. They are first and foremost storage types.
| 
| Maybe that was true two months ago before the sugar rush. We just 
| overlooked that we needed operator+(Rcomplex, Rcomplex), etc ...

Very true indeed -- but our code is also somewhat newer and less well-tested
at this point.

So the performance guarantee of Armadillo may be a little more compelling. 

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] Multiplication of ComplexVector?

2010-08-17 Thread baptiste auguie
Hi,

On 17 August 2010 10:04, Romain Francois  wrote:
> Le 17/08/10 10:00, baptiste auguie a écrit :
>>
>> On 17 August 2010 09:20, Romain Francois  wrote:
>>>
>>> Le 17/08/10 07:43, baptiste auguie a écrit :

 Hi,

 On 17 August 2010 03:24, Dirk Eddelbuettel    wrote:
>
> On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:

 [...]

> The only trouble is that nobody has written the corresponding 'glue'
> code
> to
> make
>
>      arma::cx_vec a1(y1.begin(), y1.size(), false);
>
> happen: create an Armadillo complex vector from an Rcpp::ComplexVector.
>  We
> can init by scalar size, what you'd need to insert for now is a simply
> (and
> very pedestrian) copy-loop.

 I'm confused, isn't

  arma::cx_colvec a1 = Rcpp::as<    arma::cx_vec>( y1 );

 aimed at doing this kind of conversion? (that's what I use, following
 Romain's tip)

 baptiste
>>>
>>> I don't think you are confused. This works well, but it does require some
>>> extra copying, which Dirk was trying to spare.
>>>
>>> Using the "advanced" constructor in armadillo means that both the
>>> ComplexVector and the cx_vec use the same memory. although it says loud
>>> and
>>> clear in armadillo documentation :
>>>
>>> """
>>> This is faster, but can be dangerous unless you know what you're doing!
>>> """
>>
>>
>> Thanks. Two questions then,
>>
>> 1- reinterpret_cast<  std::complex*>  seems like a good option,
>> does it work with real / complex matrices as well? The only things
>> that came up from googling it were related to RcppGSL, not
>> RcppArmadillo.
>
> Sure.
>
> code<- '
>    ComplexMatrix y1(i), y2(ii);
>    arma::cx_mat a1( reinterpret_cast< std::complex* >(y1.begin()),
> y1.nrow(), y1.ncol(), false);
>    arma::cx_mat a2( reinterpret_cast< std::complex* >(y2.begin()),
> y2.nrow(), y2.ncol(), false);
>    return wrap( a1 + a2);
>    '
> fun<- cxxfunction(signature(i="complex", ii = "complex"), code,
> plugin="RcppArmadillo")
>
> x1 <- matrix( 1:10*(1+1i), 5, 2 )
> x2 <- matrix( 1*1:10*(1+1i), 5, 2 )
> fun( x1, x2 )
>
> You don't need reinterpret_cast for converting NumericMatrix to a
> Mat aka mat because they both use the same type and layout for
> storage : double with column major order layout

Thanks for the nice examples.

>
>> 2- when is it not safe? ;)
>
> When people don't know what they are doing.

Oops, that'd be me :)

>
> For example : R has no knowledge that armadillo shares the memory with it,
> so if for some reason R thinks it can garbage collect the SEXP that came in
> ("i" and "ii"), it won't ask around for permission and so the armadillo
> matrix/vector will contain a pointer to wonderland, and then killing the
> jabbawockee bug is not easy.
>
> So if all you do is share the memory inside the same function/scope you
> should be fine.
>

I'll need to get bitten by the bug before I can really absorb the
meaning of this. I basically have no clue as to who owns the data /
pointers, underneath layers of Rcpp and Armadillo.


Thanks,

baptiste
___
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] Multiplication of ComplexVector?

2010-08-17 Thread Christian Gunning
Thanks to all for the helpful suggestions. I was excited to learn
about Armadillo's plans for fft/ifft -- my original interest in Rcpp
was to facilitate the R fft call from C...

For conceptual simplicity, I'm opting for manually adding an operator*
for now. Benchmarks show a ~10% speedup over sending large vectors (2
million) to R for multiplication.

One question that I ran into that I don't quite understand -- there
are several permutations of an Rcomplex-returning operator*, depending
upon the type of lhs and rhs (e.g Rcomplex and Rcomplex; Rcomplex and
double; etc.).  I admit, the template system is over my head for now,
and this has a low priority for me, but perhaps there's a simple
explanation asides from using separate operator symbols?

Here are 2 definitions that vary in input type:

Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
   Rcomplex y ;
   y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
   y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
   return y ;
}

Rcomplex operator*( const Rcomplex& lhs, const double& rhs){
   Rcomplex y ;
   y.r = lhs.r * rhs ;
   y.i = lhs.i * rhs ;
   return y ;
}

yielding the following compiler error:

rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
operator*(const Rcomplex&, const double&)’ conflicts with
rcpp_operators.h:6: error: previous declaration ‘Rcomplex
operator*(const Rcomplex&, const Rcomplex&)’ here

best,
Christian
-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
___
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] Multiplication of ComplexVector?

2010-08-17 Thread Romain Francois

Le 17/08/10 23:09, baptiste auguie a écrit :

Hi,

On 17 August 2010 10:04, Romain Francois  wrote:

Le 17/08/10 10:00, baptiste auguie a écrit :


On 17 August 2010 09:20, Romain Francoiswrote:


Le 17/08/10 07:43, baptiste auguie a écrit :


Hi,

On 17 August 2010 03:24, Dirk Eddelbuettel  wrote:


On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:


[...]


The only trouble is that nobody has written the corresponding 'glue'
code
to
make

  arma::cx_vec a1(y1.begin(), y1.size(), false);

happen: create an Armadillo complex vector from an Rcpp::ComplexVector.
  We
can init by scalar size, what you'd need to insert for now is a simply
(and
very pedestrian) copy-loop.


I'm confused, isn't

  arma::cx_colvec a1 = Rcpp::as<  arma::cx_vec>( y1 );

aimed at doing this kind of conversion? (that's what I use, following
Romain's tip)

baptiste


I don't think you are confused. This works well, but it does require some
extra copying, which Dirk was trying to spare.

Using the "advanced" constructor in armadillo means that both the
ComplexVector and the cx_vec use the same memory. although it says loud
and
clear in armadillo documentation :

"""
This is faster, but can be dangerous unless you know what you're doing!
"""



Thanks. Two questions then,

1- reinterpret_castseems like a good option,
does it work with real / complex matrices as well? The only things
that came up from googling it were related to RcppGSL, not
RcppArmadillo.


Sure.

code<- '
ComplexMatrix y1(i), y2(ii);
arma::cx_mat a1( reinterpret_cast<  std::complex*>(y1.begin()),
y1.nrow(), y1.ncol(), false);
arma::cx_mat a2( reinterpret_cast<  std::complex*>(y2.begin()),
y2.nrow(), y2.ncol(), false);
return wrap( a1 + a2);
'
fun<- cxxfunction(signature(i="complex", ii = "complex"), code,
plugin="RcppArmadillo")

x1<- matrix( 1:10*(1+1i), 5, 2 )
x2<- matrix( 1*1:10*(1+1i), 5, 2 )
fun( x1, x2 )

You don't need reinterpret_cast for converting NumericMatrix to a
Mat  aka mat because they both use the same type and layout for
storage : double with column major order layout


Thanks for the nice examples.




2- when is it not safe? ;)


When people don't know what they are doing.


Oops, that'd be me :)



For example : R has no knowledge that armadillo shares the memory with it,
so if for some reason R thinks it can garbage collect the SEXP that came in
("i" and "ii"), it won't ask around for permission and so the armadillo
matrix/vector will contain a pointer to wonderland, and then killing the
jabbawockee bug is not easy.

So if all you do is share the memory inside the same function/scope you
should be fine.



I'll need to get bitten by the bug before I can really absorb the
meaning of this. I basically have no clue as to who owns the data /
pointers, underneath layers of Rcpp and Armadillo.


Rcpp runs the show, so as long as your Rcpp object lives longer than the 
armadillo object, you are fine.


armadillo has no clue it gets the data from Rcpp and will not interfere, 
it will not attempt to delete the data or whatever, just access it.


The way RcppArmadillo is usually used : "get SEXP, convert to Rcpp data 
(e.g. NumericVector), create a armadillo object, do some stuff with the 
armadillo object, convert the result back to Rcpp somehow and return 
something to R", this should be ok.


Romain

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-17 Thread Romain Francois

Le 18/08/10 07:44, Christian Gunning a écrit :

Thanks to all for the helpful suggestions. I was excited to learn
about Armadillo's plans for fft/ifft -- my original interest in Rcpp
was to facilitate the R fft call from C...

For conceptual simplicity, I'm opting for manually adding an operator*
for now.


Note that they will appear in the next version of Rcpp, which is 
scheduled (loosely) for the end of august.



Benchmarks show a ~10% speedup over sending large vectors (2
million) to R for multiplication.

One question that I ran into that I don't quite understand -- there
are several permutations of an Rcomplex-returning operator*, depending
upon the type of lhs and rhs (e.g Rcomplex and Rcomplex; Rcomplex and
double; etc.).  I admit, the template system is over my head for now,
and this has a low priority for me, but perhaps there's a simple
explanation asides from using separate operator symbols?

Here are 2 definitions that vary in input type:

Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
Rcomplex y ;
y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
return y ;
}

Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
Rcomplex y ;
y.r = lhs.r * rhs ;
y.i = lhs.i * rhs ;
return y ;
}

yielding the following compiler error:

rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
operator*(const Rcomplex&, const double&)’ conflicts with
rcpp_operators.h:6: error: previous declaration ‘Rcomplex
operator*(const Rcomplex&, const Rcomplex&)’ here

best,
Christian


Do you include this from a .c file or a .cpp file ? C has no concept of 
overloading.


Can you share a small reproducible example that shows the problem.

Romain

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-17 Thread Romain Francois

Le 18/08/10 08:19, Romain Francois a écrit :

Le 18/08/10 07:44, Christian Gunning a écrit :

Thanks to all for the helpful suggestions. I was excited to learn
about Armadillo's plans for fft/ifft -- my original interest in Rcpp
was to facilitate the R fft call from C...

For conceptual simplicity, I'm opting for manually adding an operator*
for now.


Note that they will appear in the next version of Rcpp, which is
scheduled (loosely) for the end of august.


Benchmarks show a ~10% speedup over sending large vectors (2
million) to R for multiplication.

One question that I ran into that I don't quite understand -- there
are several permutations of an Rcomplex-returning operator*, depending
upon the type of lhs and rhs (e.g Rcomplex and Rcomplex; Rcomplex and
double; etc.). I admit, the template system is over my head for now,
and this has a low priority for me, but perhaps there's a simple
explanation asides from using separate operator symbols?

Here are 2 definitions that vary in input type:

Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
Rcomplex y ;
y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
return y ;
}

Rcomplex operator*( const Rcomplex& lhs, const double& rhs){
Rcomplex y ;
y.r = lhs.r * rhs ;
y.i = lhs.i * rhs ;
return y ;
}

yielding the following compiler error:

rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
operator*(const Rcomplex&, const double&)’ conflicts with
rcpp_operators.h:6: error: previous declaration ‘Rcomplex
operator*(const Rcomplex&, const Rcomplex&)’ here

best,
Christian


Do you include this from a .c file or a .cpp file ? C has no concept of
overloading.

Can you share a small reproducible example that shows the problem.

Romain


It seems to work fine for me :

require( inline )
require( Rcpp )

inc <- '
Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
Rcomplex y ;
y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
return y ;
}
Rcomplex operator*( const Rcomplex& lhs, const double& rhs){
   Rcomplex y ;
   y.r = lhs.r * rhs ;
   y.i = lhs.i * rhs ;
   return y ;
}

'
fx <- cxxfunction( signature( ), '
using namespace Rcpp ;
Rcomplex x, y ;
x.r = 2.0 ; x.i = 2.0 ;
y.r = 1.0 ; y.i = 1.0 ;

Rcomplex z = x * y ;
Rcomplex a = x * 3 ;
return ComplexVector::create( x, y, z, a ) ;

', plugin = "Rcpp", includes = inc )



> fx()
[1] 2+2i 1+1i 0+4i 6+6i

so I'm staying with my guess that this is a C vs C++ issue.

Romain


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-18 Thread Christian Gunning
I'm using a package generated by Rcpp.package.skeleton.  No C files.
Upon reflection, I'm not entirely sure I have the header right,
though.

///
// rcpp_operators.h:
///
#ifndef _Rcwttest_RCPP_OPERATORS_H
#define _Rcwttest_RCPP_OPERATORS_H
#include 
RcppExport Rcomplex operator*(const Rcomplex&, const Rcomplex&);
RcppExport Rcomplex operator*(const Rcomplex&, const double&);
#endif

///
// rcpp_operators.cpp
///
#include "rcpp_operators.h"
Rcomplex operator*( const Rcomplex& lhs, const double& rhs){
   Rcomplex y ;
   y.r = lhs.r * rhs;
   y.i = lhs.i * rhs ;
   return y ;
}
Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
   Rcomplex y ;
   y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
   y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
   return y ;
}


Compiler output (from "R CMD build Rcwttest; R CMD INSTALL Rcwttest;"):

g++ -I/usr/share/R/include
-I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -c
rcpp_cwt_thierry.cpp -o rcpp_cwt_thierry.o
In file included from rcpp_cwt_thierry.cpp:2:
rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
operator*(const Rcomplex&, const double&)’ conflicts with
rcpp_operators.h:6: error: previous declaration ‘Rcomplex
operator*(const Rcomplex&, const Rcomplex&)’ here


-xian

On Tue, Aug 17, 2010 at 11:30 PM, Romain Francois
 wrote:
> Le 18/08/10 08:19, Romain Francois a écrit :
>>
>> Le 18/08/10 07:44, Christian Gunning a écrit :
>>>
>>> Thanks to all for the helpful suggestions. I was excited to learn
>>> about Armadillo's plans for fft/ifft -- my original interest in Rcpp
>>> was to facilitate the R fft call from C...
>>>
>>> For conceptual simplicity, I'm opting for manually adding an operator*
>>> for now.
>>
>> Note that they will appear in the next version of Rcpp, which is
>> scheduled (loosely) for the end of august.
>>
>>> Benchmarks show a ~10% speedup over sending large vectors (2
>>> million) to R for multiplication.
>>>
>>> One question that I ran into that I don't quite understand -- there
>>> are several permutations of an Rcomplex-returning operator*, depending
>>> upon the type of lhs and rhs (e.g Rcomplex and Rcomplex; Rcomplex and
>>> double; etc.). I admit, the template system is over my head for now,
>>> and this has a low priority for me, but perhaps there's a simple
>>> explanation asides from using separate operator symbols?
>>>
>>> Here are 2 definitions that vary in input type:
>>>
>>> Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
>>> Rcomplex y ;
>>> y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
>>> y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
>>> return y ;
>>> }
>>>
>>> Rcomplex operator*( const Rcomplex& lhs, const double& rhs){
>>> Rcomplex y ;
>>> y.r = lhs.r * rhs ;
>>> y.i = lhs.i * rhs ;
>>> return y ;
>>> }
>>>
>>> yielding the following compiler error:
>>>
>>> rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
>>> operator*(const Rcomplex&, const double&)’ conflicts with
>>> rcpp_operators.h:6: error: previous declaration ‘Rcomplex
>>> operator*(const Rcomplex&, const Rcomplex&)’ here
>>>
>>> best,
>>> Christian
>>
>> Do you include this from a .c file or a .cpp file ? C has no concept of
>> overloading.
>>
>> Can you share a small reproducible example that shows the problem.
>>
>> Romain
>
> It seems to work fine for me :
>
> require( inline )
> require( Rcpp )
>
> inc <- '
> Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs){
>        Rcomplex y ;
>        y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
>        y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
>        return y ;
> }
> Rcomplex operator*( const Rcomplex& lhs, const double& rhs){
>       Rcomplex y ;
>       y.r = lhs.r * rhs ;
>       y.i = lhs.i * rhs ;
>       return y ;
> }
>
> '
> fx <- cxxfunction( signature( ), '
> using namespace Rcpp ;
>    Rcomplex x, y ;
>    x.r = 2.0 ; x.i = 2.0 ;
>    y.r = 1.0 ; y.i = 1.0 ;
>
>    Rcomplex z = x * y ;
>    Rcomplex a = x * 3 ;
>    return ComplexVector::create( x, y, z, a ) ;
>
> ', plugin = "Rcpp", includes = inc )
>
>
>
>> fx()
> [1] 2+2i 1+1i 0+4i 6+6i
>
> so I'm staying with my guess that this is a C vs C++ issue.
>
> Romain
>
>
> --
> Romain Francois
> Professional R Enthusiast
> +33(0) 6 28 91 30 30
> http://romainfrancois.blog.free.fr
> |- http://bit.ly/bzoWrs : Rcpp svn revision 2000
> |- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
> `- http://bit.ly/aAyra4 : highlight 0.2-2
>
>



-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
___
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] Multiplication of ComplexVector?

2010-08-18 Thread Romain Francois
Yep, that is because of RcppExport. You only use RcppExport when you 
call the function from .Call in R.


RcppExport is an alias to extern "C" so it essentially voids the 
C++-ness of the function it is applied to.


Romain

Le 18/08/10 09:05, Christian Gunning a écrit :

I'm using a package generated by Rcpp.package.skeleton.  No C files.
Upon reflection, I'm not entirely sure I have the header right,
though.

///
// rcpp_operators.h:
///
#ifndef _Rcwttest_RCPP_OPERATORS_H
#define _Rcwttest_RCPP_OPERATORS_H
#include
RcppExport Rcomplex operator*(const Rcomplex&, const Rcomplex&);
RcppExport Rcomplex operator*(const Rcomplex&, const double&);
#endif

///
// rcpp_operators.cpp
///
#include "rcpp_operators.h"
Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
Rcomplex y ;
y.r = lhs.r * rhs;
y.i = lhs.i * rhs ;
return y ;
}
Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
Rcomplex y ;
y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
return y ;
}


Compiler output (from "R CMD build Rcwttest; R CMD INSTALL Rcwttest;"):

g++ -I/usr/share/R/include
-I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -c
rcpp_cwt_thierry.cpp -o rcpp_cwt_thierry.o
In file included from rcpp_cwt_thierry.cpp:2:
rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
operator*(const Rcomplex&, const double&)’ conflicts with
rcpp_operators.h:6: error: previous declaration ‘Rcomplex
operator*(const Rcomplex&, const Rcomplex&)’ here


-xian

On Tue, Aug 17, 2010 at 11:30 PM, Romain Francois
  wrote:

Le 18/08/10 08:19, Romain Francois a écrit :


Le 18/08/10 07:44, Christian Gunning a écrit :


Thanks to all for the helpful suggestions. I was excited to learn
about Armadillo's plans for fft/ifft -- my original interest in Rcpp
was to facilitate the R fft call from C...

For conceptual simplicity, I'm opting for manually adding an operator*
for now.


Note that they will appear in the next version of Rcpp, which is
scheduled (loosely) for the end of august.


Benchmarks show a ~10% speedup over sending large vectors (2
million) to R for multiplication.

One question that I ran into that I don't quite understand -- there
are several permutations of an Rcomplex-returning operator*, depending
upon the type of lhs and rhs (e.g Rcomplex and Rcomplex; Rcomplex and
double; etc.). I admit, the template system is over my head for now,
and this has a low priority for me, but perhaps there's a simple
explanation asides from using separate operator symbols?

Here are 2 definitions that vary in input type:

Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
Rcomplex y ;
y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
return y ;
}

Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
Rcomplex y ;
y.r = lhs.r * rhs ;
y.i = lhs.i * rhs ;
return y ;
}

yielding the following compiler error:

rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
operator*(const Rcomplex&, const double&)’ conflicts with
rcpp_operators.h:6: error: previous declaration ‘Rcomplex
operator*(const Rcomplex&, const Rcomplex&)’ here

best,
Christian


Do you include this from a .c file or a .cpp file ? C has no concept of
overloading.

Can you share a small reproducible example that shows the problem.

Romain


It seems to work fine for me :

require( inline )
require( Rcpp )

inc<- '
Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
Rcomplex y ;
y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
return y ;
}
Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
   Rcomplex y ;
   y.r = lhs.r * rhs ;
   y.i = lhs.i * rhs ;
   return y ;
}

'
fx<- cxxfunction( signature( ), '
using namespace Rcpp ;
Rcomplex x, y ;
x.r = 2.0 ; x.i = 2.0 ;
y.r = 1.0 ; y.i = 1.0 ;

Rcomplex z = x * y ;
Rcomplex a = x * 3 ;
return ComplexVector::create( x, y, z, a ) ;

', plugin = "Rcpp", includes = inc )




fx()

[1] 2+2i 1+1i 0+4i 6+6i

so I'm staying with my guess that this is a C vs C++ issue.

Romain


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-18 Thread Christian Gunning
Got it, thanks!

On Wed, Aug 18, 2010 at 12:10 AM, Romain Francois
 wrote:
> Yep, that is because of RcppExport. You only use RcppExport when you call
> the function from .Call in R.
>
> RcppExport is an alias to extern "C" so it essentially voids the C++-ness of
> the function it is applied to.
>
> Romain
>
> Le 18/08/10 09:05, Christian Gunning a écrit :
>>
>> I'm using a package generated by Rcpp.package.skeleton.  No C files.
>> Upon reflection, I'm not entirely sure I have the header right,
>> though.
>>
>> ///
>> // rcpp_operators.h:
>> ///
>> #ifndef _Rcwttest_RCPP_OPERATORS_H
>> #define _Rcwttest_RCPP_OPERATORS_H
>> #include
>> RcppExport Rcomplex operator*(const Rcomplex&, const Rcomplex&);
>> RcppExport Rcomplex operator*(const Rcomplex&, const double&);
>> #endif
>>
>> ///
>> // rcpp_operators.cpp
>> ///
>> #include "rcpp_operators.h"
>> Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
>>        Rcomplex y ;
>>        y.r = lhs.r * rhs;
>>        y.i = lhs.i * rhs ;
>>        return y ;
>> }
>> Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
>>        Rcomplex y ;
>>        y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
>>        y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
>>        return y ;
>> }
>>
>>
>> Compiler output (from "R CMD build Rcwttest; R CMD INSTALL Rcwttest;"):
>>
>> g++ -I/usr/share/R/include
>> -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -c
>> rcpp_cwt_thierry.cpp -o rcpp_cwt_thierry.o
>> In file included from rcpp_cwt_thierry.cpp:2:
>> rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
>> operator*(const Rcomplex&, const double&)’ conflicts with
>> rcpp_operators.h:6: error: previous declaration ‘Rcomplex
>> operator*(const Rcomplex&, const Rcomplex&)’ here
>>
>>
>> -xian
>>
>> On Tue, Aug 17, 2010 at 11:30 PM, Romain Francois
>>   wrote:
>>>
>>> Le 18/08/10 08:19, Romain Francois a écrit :

 Le 18/08/10 07:44, Christian Gunning a écrit :
>
> Thanks to all for the helpful suggestions. I was excited to learn
> about Armadillo's plans for fft/ifft -- my original interest in Rcpp
> was to facilitate the R fft call from C...
>
> For conceptual simplicity, I'm opting for manually adding an operator*
> for now.

 Note that they will appear in the next version of Rcpp, which is
 scheduled (loosely) for the end of august.

> Benchmarks show a ~10% speedup over sending large vectors (2
> million) to R for multiplication.
>
> One question that I ran into that I don't quite understand -- there
> are several permutations of an Rcomplex-returning operator*, depending
> upon the type of lhs and rhs (e.g Rcomplex and Rcomplex; Rcomplex and
> double; etc.). I admit, the template system is over my head for now,
> and this has a low priority for me, but perhaps there's a simple
> explanation asides from using separate operator symbols?
>
> Here are 2 definitions that vary in input type:
>
> Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
> Rcomplex y ;
> y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
> y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
> return y ;
> }
>
> Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
> Rcomplex y ;
> y.r = lhs.r * rhs ;
> y.i = lhs.i * rhs ;
> return y ;
> }
>
> yielding the following compiler error:
>
> rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
> operator*(const Rcomplex&, const double&)’ conflicts with
> rcpp_operators.h:6: error: previous declaration ‘Rcomplex
> operator*(const Rcomplex&, const Rcomplex&)’ here
>
> best,
> Christian

 Do you include this from a .c file or a .cpp file ? C has no concept of
 overloading.

 Can you share a small reproducible example that shows the problem.

 Romain
>>>
>>> It seems to work fine for me :
>>>
>>> require( inline )
>>> require( Rcpp )
>>>
>>> inc<- '
>>> Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
>>>        Rcomplex y ;
>>>        y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
>>>        y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
>>>        return y ;
>>> }
>>> Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
>>>       Rcomplex y ;
>>>       y.r = lhs.r * rhs ;
>>>       y.i = lhs.i * rhs ;
>>>       return y ;
>>> }
>>>
>>> '
>>> fx<- cxxfunction( signature( ), '
>>> using namespace Rcpp ;
>>>    Rcomplex x, y ;
>>>    x.r = 2.0 ; x.i = 2.0 ;
>>>    y.r = 1.0 ; y.i = 1.0 ;
>>>
>>>    Rcomplex z = x * y ;
>>>    Rcomplex a = x * 3 ;
>>>    return ComplexVector::create( x, y, z, a ) ;
>>>
>>> ', plugin = "Rcpp", includes = inc )
>>>
>>>
>>>
 fx()
>>>
>>> [1] 2+2i 1+1i 0+4i 6+6i
>>>
>>> so I'm staying with my guess that this is a C vs C++ issue.
>>>
>>> Romain
>
> --
> Romain Francois
> Professional R Enthusiast
> +33(0) 6 28 91 30 30
> http://romainfrancois.blog.free.fr
> |- ht

Re: [Rcpp-devel] Multiplication of ComplexVector?

2010-08-18 Thread Dirk Eddelbuettel

On 18 August 2010 at 09:10, Romain Francois wrote:
| Yep, that is because of RcppExport. You only use RcppExport when you 
| call the function from .Call in R.
| 
| RcppExport is an alias to extern "C" so it essentially voids the 
| C++-ness of the function it is applied to.

For completeness:  It also provided a hook to add __declspec(dllexport) for
the one operating system that needed it when building DLLs.  We currently
don't build DLLs there so this is moot but the option may be useful.  Hence
RcppExport and not just extern "C".


Also:

On 18 August 2010 at 08:41, Romain Francois wrote:
[...]
| Should we have something similar for Rcpp ?
| 
| #if defined(RCPP_VERSION) && RCPP_VERSION >= Rcpp_Version(0, 8, 5)
| ...
| #endif

Sounds good to me!

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] Multiplication of ComplexVector?

2010-08-18 Thread Romain Francois

Le 18/08/10 13:22, Dirk Eddelbuettel a écrit :


On 18 August 2010 at 09:10, Romain Francois wrote:
| Yep, that is because of RcppExport. You only use RcppExport when you
| call the function from .Call in R.
|
| RcppExport is an alias to extern "C" so it essentially voids the
| C++-ness of the function it is applied to.

For completeness:  It also provided a hook to add __declspec(dllexport) for
the one operating system that needed it when building DLLs.  We currently
don't build DLLs there so this is moot but the option may be useful.  Hence
RcppExport and not just extern "C".


Not the way it is currently defined ;

// #ifdef BUILDING_DLL
// #define RcppExport extern "C" __declspec(dllexport)
// #else
#define RcppExport extern "C"
// #endif


Also:

On 18 August 2010 at 08:41, Romain Francois wrote:
[...]
| Should we have something similar for Rcpp ?
|
| #if defined(RCPP_VERSION)&&  RCPP_VERSION>= Rcpp_Version(0, 8, 5)
| ...
| #endif

Sounds good to me!

Dirk


Great. I'll figure how to do it, unless someone else does it before me ...

BTW, why did you not reply to each question on its own thread ?

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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] Multiplication of ComplexVector?

2010-08-18 Thread Dirk Eddelbuettel

On 18 August 2010 at 13:31, Romain Francois wrote:
| Le 18/08/10 13:22, Dirk Eddelbuettel a écrit :
| >
| > On 18 August 2010 at 09:10, Romain Francois wrote:
| > | Yep, that is because of RcppExport. You only use RcppExport when you
| > | call the function from .Call in R.
| > |
| > | RcppExport is an alias to extern "C" so it essentially voids the
| > | C++-ness of the function it is applied to.
| >
| > For completeness:  It also provided a hook to add __declspec(dllexport) for
| > the one operating system that needed it when building DLLs.  We currently
| > don't build DLLs there so this is moot but the option may be useful.  Hence
| > RcppExport and not just extern "C".
| 
| Not the way it is currently defined ;
| 
| // #ifdef BUILDING_DLL
| // #define RcppExport extern "C" __declspec(dllexport)
| // #else
| #define RcppExport extern "C"
| // #endif

Yes -- which is entirely consistent with what I wrote using past tense. 

We used to use this, we don't currently do as the hook is commented-out (and
not even OS-dependent but that is bad trace from past incarnations).  Yet it
may be useful one day if we get back to DLL building on that one beloved (or
not) OS that calls them DLLS.

| Great. I'll figure how to do it, unless someone else does it before me ...
| 
| BTW, why did you not reply to each question on its own thread ?

Sometimes I find it easier to regroup as that keeps the number of messages
down.  And as this was a minor issue and a minor thumbs-up I thought it would
be a suitable candidate for not requiring a separate message. The practice is
frowned-upon by some in the context of mailing lists, but as we so liberally
use it in off-list traffic I allowed myself this one disgression.

Anyway...

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] Multiplication of ComplexVector?

2010-08-18 Thread Dominick Samperi
On Wed, Aug 18, 2010 at 7:22 AM, Dirk Eddelbuettel  wrote:

>
> On 18 August 2010 at 09:10, Romain Francois wrote:
> | Yep, that is because of RcppExport. You only use RcppExport when you
> | call the function from .Call in R.
> |
> | RcppExport is an alias to extern "C" so it essentially voids the
> | C++-ness of the function it is applied to.
>
> For completeness:  It also provided a hook to add __declspec(dllexport) for
> the one operating system that needed it when building DLLs.  We currently
> don't build DLLs there so this is moot but the option may be useful.  Hence
> RcppExport and not just extern "C".
>

Using the decoration __declspec(dllexport) is no longer needed because the
R build process uses Rtools/bin/nm.exe to extract *all* symbols to be
exported, not just the symbols that are intended to be called from 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] Multiplication of ComplexVector?

2010-08-20 Thread Romain Francois

Hi,

I have now added these 4 operators into Rcpp :

Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs) ;
Rcomplex operator+( const Rcomplex& lhs, const Rcomplex& rhs) ;
Rcomplex operator-( const Rcomplex& lhs, const Rcomplex& rhs) ;
Rcomplex operator/( const Rcomplex& a, const Rcomplex& b) ;

So they will definitely be included in the next version 0.8.6 when we 
release it.


Romain

Le 17/08/10 02:19, Christian Gunning a écrit :

Dear list,

I'm trying to use the ComplexVector class, and I'm having difficulty
with multiplication. I sugar multiplication, element-by-element
multiplication (no * operator for either), and using temporary
Rcomplex variables (don't really understand what I'm doing here).

I was able to successfully import get("*") as an Rcpp::Function and do
the multiplication through R. Is this the best option for now?

Thanks,
Christian Gunning
University of New Mexico

// doesn't work
SEXP rcpp_hello_world(SEXP i, SEXP ii){
 using namespace Rcpp ;

 ComplexVector y1(i), y2(ii);
 int n(y1.size()), j;
 ComplexVector y3(n), y4(n);
 y3 = y1 * y2;  // no operator
 for (j = 0; j++; j


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

___
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