Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move movement to sparse matrix format #29

Open
DanOvando opened this issue Aug 13, 2020 · 4 comments
Open

move movement to sparse matrix format #29

DanOvando opened this issue Aug 13, 2020 · 4 comments

Comments

@DanOvando
Copy link
Owner

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

@DanOvando
Copy link
Owner Author

I think the key here is using the setfromtriplets thing to assign the non-zero values to the sparse matrix

@DanOvando
Copy link
Owner Author

Oh, you might be able to do this easier, and actually just pass it the sparse matrix....

https://gallery.rcpp.org/articles/sparse-iterators/

@DanOvando
Copy link
Owner Author

@DanOvando
Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant