Skip to content
Prev 2437 / 10988 Next

[Rcpp-devel] Wrapping uBlas Matrices into Rcpp Matrices

Le 02/06/11 14:43, Cedric Ginestet a ?crit :
Again untested, but you might like this constructor from Rcpp::Matrix:

	template <typename Iterator>
	Matrix( const int& nrows_, const int& ncols, Iterator start ) :
	    VECTOR( start, start + (nrows_*ncols) ),
	    nrows(nrows_)
	{
		VECTOR::attr( "dim" ) = Dimension( nrows, ncols ) ;
	}
	

So you'd use it something like this:


template <typename T>
Rcpp::Matrix< Rcpp::traits::r_sexptype_traits<T>::rtype >
ublas2rcpp( const matrix<T>& x ){
     return Rcpp::Matrix< Rcpp::traits::r_sexptype_traits<T>::rtype >(
         x.size1(), x.size2(), x.begin1()
     );
}

This is untested by guessing what would do the functions found in the 
documentation of uBlas: 
http://www.boost.org/doc/libs/1_42_0/libs/numeric/ublas/doc/matrix.htm

Romain