arma::mat pointer method worked; while I trying to get the same result with
arma::mat view without copying data, or passing the entire matrix seemed to
be laborious.
The following patter provides read/write access to some memory location
using armadillo matrix operations.


/*
 * Contact: Steven Varga
 *          [email protected]
 *          2013 Toronto, On Canada
 */

#include <R.h>
#include <stdlib.h>
#include <RcppArmadillo.h>
using namespace std;

// [[Rcpp::depends(RcppArmadillo)]]
struct Example {

    Example(double *ptr) {

        A= new arma::mat(ptr, 3,2, false, true ); // no copy contstructor
                arma::mat &a = *A; // dereferencing A gets rid of A-> syntax
                a(1,1) = 1; // convenient matlab like syntax
    }

        ~Example(){ delete A; } // clean up
    arma::mat *A;
};

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void  arma_example() {

    arma::mat M(3,10); // implicit memory allocation

    M.zeros(); // paint with zeros
    M.print(); cout<<endl<<endl;

    Example E(M.memptr());
    M.print(); // altered as planned
}
// EOF


On Tue, Nov 26, 2013 at 8:11 AM, Steven Varga <[email protected]>wrote:

> Thank you Simon for suggesting pointer then dereferencing it later in
> class methods.
>
> Dirk also suggested using matrix views which may have the same performance
> while keeping syntax neat.
>
> Will repost both working solution later on for the record.
>
> Thanks again, I found all suggestion very helpful,
> Steven
> On Nov 26, 2013 6:19 AM, <[email protected]> wrote:
>
>> Steve,
>>
>> If you want to use a variable being an arma::mat obejct with varying
>> memory, you could use an arma::mat pointer. Otherwise if memory stays the
>> same but should be reusable in R, Dirk's suggestion initialising memory in
>> R and passing it to C++ is the most practicable way and very fast.
>>
>> Best
>> Simon
>> Gesendet über den BlackBerry® Service von E-Plus.
>>
>> -----Original Message-----
>> From: Steven Varga <[email protected]>
>> Sender: [email protected]: Mon, 25 Nov
>> 2013 23:23:24
>> To: <[email protected]>
>> Reply-To: [email protected]
>> Subject: Re: [Rcpp-devel] RcppArmadillo matrix with writeable auxiliary
>>         memory
>>
>> _______________________________________________
>> Rcpp-devel mailing list
>> [email protected]
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>
>
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to