[Rcpp-devel] NumericVector wrapping an Armadillo vec is returned as a matrix

2010-04-27 Thread Davor Cubranic
In my code, I have an Armadillo column vector v (i.e., an 'arma::vec') that I 
return as a member of a list:

   arma::vec v(...);
   List result = List::create(_["v"] = v);

In R, result$v becomes a matrix, so I need to call 'as.vector(v)' to turn it 
into a vector. Even explicitly returning a NumericVector doesn't help, as in:

   arma::vec v(...);
   List result = List::create(_["v"] = NumericVector(wrap(v)));

Is this by design, a bug, or am I misusing the API?

Davor
___
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] RcppMAtrix ?

2010-04-27 Thread baptiste auguie
Thank you both for the valuable info, I am looking forward to trying
this out as soon as I can (i've had no time in the past few days).

All the best,

baptiste


On 26 April 2010 10:09, Romain Francois  wrote:
> And now, with the new macros (see the thread "[Rcpp-devel] any
> preprocessor expert around ?" ), one can write the function as:
>
> #include 
> using namespace Rcpp ;
>
> RCPP_FUNCTION_2( arma::mat, add_mat,
>        arma::mat y, arma::mat x ){
>
>        arma::mat result = x+y;
>        return result ;
> }
>
> although in this case x and y would be copies of the R data, so it would
> be less efficient than your code.
>
> Romain
>
> Le 23/04/10 19:55, Davor Cubranic a écrit :
>>
>> Baptiste,
>>
>> Do have a look at RcppArmadillo, it makes matrix code very easy to write
>> (and read!). For example, this is a function callable from R to add two
>> matrices:
>>
>> #include
>>
>> using namespace Rcpp;
>>
>> RcppExport SEXP add_mat(SEXP y_in,
>>                       SEXP x_in) {
>>    NumericMatrix y_r(y_in);
>>    arma::mat y(y_r.begin(), y_r.nrow, y_r.ncol(), false);
>>
>>    NumericMatrix x_r(x_in);
>>    arma::mat x(x_r.begin(), x_r.nrow(), x_r.ncol(), false);
>>
>>    arma::mat result = x+y;
>>    return wrap(result);
>> }
>>
>> In this case, the matrices are real, not complex. but I believe simply
>> replacing 'arma::mat' with 'arma::cx_mat' and 'NumericMatrix' with
>> 'ComplexMatrix' will work for the complex case.
>>
>> Davor
>>
>>
>> On April 22, 2010 01:22:58 pm baptiste auguie wrote:
>>> OK, thanks for the information. I guess I was lead to believe that
>>> such operations were of common use because I worked previously with
>>> colleagues who had defined their own C++ complex class (and I'm
>>> guessing it was precisely for this purpose of operator overloading).
>>> Sadly the code was not open source. I'll look into armadillo,
>>> hopefully it provides an alternative. I might also need to rethink
>>> what portion of the code I should really be porting out of R; this
>>> function was clearly the bottleneck of my code but it looks like it
>>> will be painful to write it in a lower-level language.
>>>
>>> Thanks,
>>>
>>> baptiste
>>>
>>> On 22 April 2010 18:42, Romain Francois
>> wrote:
 Le 22/04/10 18:19, baptiste auguie a écrit :
> Thanks for the example, it is useful indeed. However, I am having
> difficulties with operations involving whole matrices, rather than
> matrix elements.

 We don't currently have those.

> Again I must warn that I don't know C++ ; but the
> addition of two matrices does not seem to work out-of-the-box, as
> well as more complicated functions. The dispatch of these
> functions might not exist for the complex matrix class, or maybe
> it is not implemented in Rcpp?
>
> The operations I would need to perform with complex matrices are,
>
> +, -, *, transpose,
> as well as operations on 1-column and one-row matrices (==
> vectors?) such as exp().

 Those are things you'd typically do in R, not in C/C++

> Working component by component is not a very attractive option

 That is what you usually do in a C/C++ world

> so I'm
> hoping there is an easy way to define operations between matrices,
> matrices and vectors, and matrices and scalars.

 One thing you can do perhaps is look into armadillo, which we wrap
 nicely with the RcppArmadillo package. and thanks to Doug, the
 wrapping is even nicer now since armadillo is packed up inside
 RcppArmadillo (but this version is not released yet).

 We will not implement these operators soon because it is very easy
 to not do it right. armadillo does it nicely.

> Thanks,
>
> baptiste
>
> PS: The problem with my previous email was in the gmail vs.
> googlemail domain, as Dirk pointed out (I had had that same
> problem before, but I forgot!).
>
> On 22 April 2010 13:17, Romain Francois
>>   wrote:
>> Thank you for reposting here.
>>
>> It is not trivial to see what is happening in your example, so
>> I'll just give you some tools.
>>
>> The old api (which the classicRcppMatrixExample example uses)
>> does not have
>> support for complex vectors or matrices.
>>
>> The new api does have support for complex vectors and complex
>> matrices. The
>> unit test file runit.ComplexVector.R does indeed contain some
>> very basic examples of using ComplexVector, but not complex
>> matrices. However, you can
>> use Rcpp::ComplexMatrix.
>>
>> Here is an example that calculates the sum of the real part of
>> the elements
>> of a complex matrix diagonal and the sum of the imaginary part:
>>
>> require( Rcpp )
>> require( inline)
>>
>> fx<- cfunction( signature( x = "matrix" ), '
>>         /* grab the R object as a complex matrix */
>>         ComplexMatrix m(x)