Open DanOvando opened 4 years ago
I think the key here is using the setfromtriplets thing to assign the non-zero values to the sparse matrix
Oh, you might be able to do this easier, and actually just pass it the sparse matrix....
Well I'll be damned, chatgpt to the rescue here
#include <RcppEigen.h>
#include <Rcpp.h>
// [[Rcpp::depends(RcppEigen)]]
using Eigen::SparseMatrix;
using Eigen::Map;
using Eigen::VectorXd;
using Rcpp::NumericMatrix;
using Rcpp::NumericVector;
using Rcpp::as;
// [[Rcpp::export]]
NumericMatrix sparse_mat_mult(NumericMatrix A, NumericMatrix B) {
const Map<SparseMatrix<double> > matA(as<Map<SparseMatrix<double> > >(A));
const Map<SparseMatrix<double> > matB(as<Map<SparseMatrix<double> > >(B));
SparseMatrix<double> res = matA * matB;
return Rcpp::wrap(res);
}
from chatgpt This code defines a function sparse_mat_mult that takes in two NumericMatrix objects A and B, converts them to SparseMatrix objects using the Map function from Eigen, performs the matrix multiplication using the * operator, and returns the result as a NumericMatrix using the wrap function from Rcpp.
Note that this code assumes that the input matrices A and B are already in sparse matrix format. If they are not, you can convert them to sparse matrix format using the sparseMatrix function from the Matrix package in R. Also note that the RcppEigen and Rcpp packages need to be installed and loaded in R before running this code.
Idea to make movement way faster. Define a propobability of movement threshold, where once it drops be low X, the probability of moving to a cell is zero. Convert all probabilities of movement below that threshold, and make movement a sparse matrix instead of a normal matrix, and voila!
Eigen::SparseMatrix
https://eigen.tuxfamily.org/dox/group__TutorialSparse.html
https://cran.r-project.org/web/packages/RcppEigen/vignettes/RcppEigen-Introduction.pdf