#include // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::export]] Eigen::SparseMatrix wrapSparseDouble() { Eigen::SparseMatrix mm(9,3); mm.reserve(9); for (int j = 0; j < 3; ++j) { mm.startVec(j); for (int i = 3 * j; i < 3 * (j + 1); ++i) mm.insertBack(i, j) = 1.; } mm.finalize(); return mm; } // [[Rcpp::export]] Eigen::SparseMatrix wrapSparseDoubleColumnMajor() { Eigen::SparseMatrix mm(9,3); mm.reserve(9); for (int j = 0; j < 3; ++j) { mm.startVec(j); for (int i = 3 * j; i < 3 * (j + 1); ++i) mm.insertBack(i, j) = 1.; } mm.finalize(); return mm; } // [[Rcpp::export]] Eigen::SparseMatrix wrapSparseDoubleRowMajor() { Eigen::SparseMatrix mm(9,3); mm.reserve(9); for (int irow = 0; irow < 9; ++irow) { mm.startVec(irow); mm.insertBack(irow, irow / 3) = static_cast( 9 - irow ); } mm.finalize(); return mm; } // [[Rcpp::export]] Eigen::SparseMatrix asSparseDoubleColumnMajor(Eigen::SparseMatrix mm) { return mm; } // [[Rcpp::export]] double asMappedSparseDoubleColMajor(Eigen::Map > mm) { double s = mm.sum(); // access instantiated sparse matrix return s; } // [[Rcpp::export]] double asMappedSparseDeprecatedDoubleColMajor(Eigen::MappedSparseMatrix mm) { // Deprecated double s = mm.sum(); // access instantiated sparse matrix return s; } // [[Rcpp::export]] double asSparseDoubleRowMajor(Eigen::SparseMatrix mm) { double s = mm.sum(); // access instantiated sparse matrix return s; } // [[Rcpp::export]] double asMappedSparseDoubleRowMajor(Eigen::Map > mm) { double s = mm.sum(); // access instantiated sparse matrix return s; } // [[Rcpp::export]] double asMappedSparseDeprecatedDoubleRowMajor(Eigen::MappedSparseMatrix mm) { double s = mm.sum(); // access instantiated sparse matrix return s; } // [[Rcpp::export]] Rcpp::List sparseCholesky(Rcpp::List input) { using Eigen::VectorXd; using Eigen::MatrixXd; using Eigen::Lower; using Eigen::Map; using Eigen::SparseMatrix; using Eigen::SimplicialLDLT; using Eigen::Success; const Map > m1 = input[0]; const Map v1 = input[1]; SparseMatrix m2(m1.cols(), m1.cols()); m2.selfadjointView().rankUpdate(m1.adjoint()); SimplicialLDLT > ff(m2); VectorXd res = ff.solve(m1.adjoint() * v1); return Rcpp::List::create(Rcpp::Named("res") = res, Rcpp::Named("rows") = double(ff.rows()), Rcpp::Named("cols") = double(ff.cols())); }