#include // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::export]] Rcpp::List fx() { Rcpp::List vecs = Rcpp::List::create( Rcpp::_["Vec"] = Eigen::VectorXcd::Zero(5), Rcpp::_["Vec"] = Eigen::VectorXd::Zero(5), Rcpp::_["Vec"] = Eigen::VectorXf::Zero(5), Rcpp::_["Vec"] = Eigen::VectorXi::Zero(5) ); // A VectorX behaves as a matrix with one column but is converted to // a vector object in R, not a matrix of one column. The distinction is // that VectorX objects are defined at compile time to have one column, // whereas a MatrixX has a dynamic number of columns that is set to 1 // during execution of the code. A MatrixX object can be resized to have // a different number of columns. A VectorX object cannot. Rcpp::List cols = Rcpp::List::create( Rcpp::_["Col"] = Eigen::MatrixXcd::Zero(5, 1), Rcpp::_["Col"] = Eigen::MatrixXd::Zero(5, 1), Rcpp::_["Col"] = Eigen::MatrixXf::Zero(5, 1), Rcpp::_["Col"] = Eigen::MatrixXi::Zero(5, 1) ); Rcpp::List rows = Rcpp::List::create( Rcpp::_["Row"] = Eigen::RowVectorXcd::Zero(5), Rcpp::_["Row"] = Eigen::RowVectorXd::Zero(5), Rcpp::_["Row"] = Eigen::RowVectorXf::Zero(5), Rcpp::_["Row"] = Eigen::RowVectorXi::Zero(5) ); Rcpp::List matrices = Rcpp::List::create( Rcpp::_["Mat"] = Eigen::MatrixXcd::Identity(3, 3), Rcpp::_["Mat"] = Eigen::MatrixXd::Identity(3, 3), Rcpp::_["Mat"] = Eigen::MatrixXf::Identity(3, 3), Rcpp::_["Mat"] = Eigen::MatrixXi::Identity(3, 3) ); // ArrayXX objects have the same structure as matrices but allow // componentwise arithmetic. A * B is matrix multiplication for // matrices and componentwise multiplication for arrays. Rcpp::List arrays2 = Rcpp::List::create( Rcpp::_["Arr2"] = Eigen::ArrayXXcd::Zero(3, 3), Rcpp::_["Arr2"] = Eigen::ArrayXXd::Zero(3, 3), Rcpp::_["Arr2"] = Eigen::ArrayXXf::Zero(3, 3), Rcpp::_["Arr2"] = Eigen::ArrayXXi::Zero(3, 3) ); // ArrayX objects have the same structure as VectorX objects // but allow componentwise arithmetic, including functions like exp, log, // sqrt, ... Rcpp::List arrays1 = Rcpp::List::create( Rcpp::_["Arr1"] = Eigen::ArrayXcd::Zero(5), Rcpp::_["Arr1"] = Eigen::ArrayXd::Zero(5), Rcpp::_["Arr1"] = Eigen::ArrayXf::Zero(5), Rcpp::_["Arr1"] = Eigen::ArrayXi::Zero(5) ); Rcpp::List operations = Rcpp::List::create( Rcpp::_["Op_seq"] = Eigen::ArrayXd::LinSpaced(6, 1, 10), // arguments are length.out, start, end Rcpp::_["Op_log"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).log(), Rcpp::_["Op_exp"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).exp(), Rcpp::_["Op_sqrt"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).sqrt(), Rcpp::_["Op_cos"] = Eigen::ArrayXd::LinSpaced(6, 1, 10).cos() ); Rcpp::List output = Rcpp::List::create( Rcpp::_["vectors : VectorX"] = vecs, Rcpp::_["matrices : MatrixX"] = matrices, Rcpp::_["rows : RowVectorX"] = rows, Rcpp::_["columns : MatrixX"] = cols, Rcpp::_["arrays2d : ArrayXX"] = arrays2, Rcpp::_["arrays1d : ArrayX"] = arrays1, Rcpp::_["operations : ArrayXd"] = operations ); return output ; } // [[Rcpp::export]] Rcpp::List fx2(Rcpp::List input) { Eigen::VectorXi m1 = input[0] ; /* implicit as */ Eigen::VectorXd m2 = input[1] ; /* implicit as */ Eigen::Matrix m3 = input[0] ; /* implicit as */ Eigen::VectorXf m4 = input[1] ; /* implicit as */ Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum(), m3.sum(), m4.sum()); return res ; } // [[Rcpp::export]] Rcpp::List fx3(Rcpp::List input) { const Eigen::Map m1 = input[0] ; // maps share storage and do not allow conversion const Eigen::Map m2 = input[1] ; Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum()); return res ; } // [[Rcpp::export]] Rcpp::List fx4(Rcpp::List input) { const Eigen::Map m1 = input[0] ; // maps share storage, do not allow conversion const Eigen::Map m2 = input[1] ; Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum()); return res ; } // [[Rcpp::export]] Rcpp::List fx5(Rcpp::List input) { const Eigen::Map m1 = input[0]; // maps share storage, do not allow conversion const Eigen::Map m2 = input[1] ; // FIXME: Write a version of as specifically for complex matrices. // const Eigen::Map m3 = input[2] ; Rcpp::List res = Rcpp::List::create(m1.sum(), m2.sum());//, m3.sum()); return res ; } // [[Rcpp::export]] Rcpp::List fx6(Rcpp::List input) { const Eigen::MappedSparseMatrix m1 = input[0]; // maps share storage and do not allow conversion Rcpp::List res = Rcpp::List::create(Rcpp::_["nnz"] = double(m1.nonZeros()), Rcpp::_["nr"] = double(m1.rows()), Rcpp::_["nc"] = double(m1.cols()), Rcpp::_["inSz"] = double(m1.innerSize()), Rcpp::_["outSz"] = double(m1.outerSize()), Rcpp::_["sum"] = m1.sum()); return res ; } // [[Rcpp::export]] Rcpp::List fx7(Rcpp::List input) { const Eigen::SparseMatrix m1 = input[0]; Rcpp::List res = Rcpp::List::create(Rcpp::_["nnz"] = double(m1.nonZeros()), Rcpp::_["nr"] = double(m1.rows()), Rcpp::_["nc"] = double(m1.cols()), Rcpp::_["inSz"] = double(m1.innerSize()), Rcpp::_["outSz"] = double(m1.outerSize()), Rcpp::_["sum"] = m1.sum()); return res ; }