I need to compute two equations related with trace and inverse of a around 30000 x 30000 density matrices. The equations are
-trace( W_i %*% C)
and
-trace(W_i %*% C %*% W_j C)
I know W_i, W_j and inverse of C. These equations are related with Pearson estimating functions. I am trying to use R and package Matrix, but I couldn't compute the C matrix, using solve() or chol() and chol2inv(). I do not know with is possible using solve() to solve a system of equation and after compute the trace. It is common to use solve function to compute something like C^{-1} W = solve(C, W), but my equation is a little bit different. Any help is welcome. I am thinking about to use RcppArmadillo, but I am not sure that it is able to compute my equations.
You can use RcppArmadillo, but you have to be careful about memory usage. If you save the below code as arma_test.cpp it can be sourced via Rcpp::sourceCpp('w_graph_class.cpp'). Obviously the matrix data is dummy, but it should give you a starting point. Also check out alternative methods to invert C rather than using .i(), such as pinv(), etc.
See this question about large matrices with RcppArmadillo
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
Rcpp::List arma_calc() {
arma::mat C_inv = arma::mat(30000,30000,arma::fill::randu);
arma::mat W_i = arma::mat(30000,30000,arma::fill::randu);
arma::mat W_j = arma::mat(30000,30000,arma::fill::randu);
double tr_1=-arma::trace(W_i*C_inv.i());
double tr_2=-arma::trace(W_i*C_inv.i()*W_j*C_inv.i());
return Rcpp::List::create(Rcpp::Named("Trace1")=tr_1,Rcpp::Named("Trace2")=tr_2);
}
Related
The following code works as expected:
matrix.cpp
// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
// [[Rcpp::export]]
SEXP eigenMatTrans(Eigen::MatrixXd A){
Eigen::MatrixXd C = A.transpose();
return Rcpp::wrap(C);
}
// [[Rcpp::export]]
SEXP eigenMatMult(Eigen::MatrixXd A, Eigen::MatrixXd B){
Eigen::MatrixXd C = A * B;
return Rcpp::wrap(C);
}
// [[Rcpp::export]]
SEXP eigenMapMatMult(const Eigen::Map<Eigen::MatrixXd> A, Eigen::Map<Eigen::MatrixXd> B){
Eigen::MatrixXd C = A * B;
return Rcpp::wrap(C);
}
This is using the C++ eigen class for matrices, See https://eigen.tuxfamily.org/dox
In R, I can access those functions.
library(Rcpp);
Rcpp::sourceCpp('matrix.cpp');
A <- matrix(rnorm(10000), 100, 100);
B <- matrix(rnorm(10000), 100, 100);
library(microbenchmark);
microbenchmark(eigenMatTrans(A), t(A), A%*%B, eigenMatMult(A, B), eigenMapMatMult(A, B))
This shows that R performs pretty well on resorting (transpose). Multiplying has some advantages with eigen.
Using the Matrix library, I can convert a normal matrix to a sparse matrix.
Example from https://cmdlinetips.com/2019/05/introduction-to-sparse-matrices-in-r/
library(Matrix);
data<- rnorm(1e6)
zero_index <- sample(1e6)[1:9e5]
data[zero_index] <- 0
A = matrix(data, ncol=1000)
A.csr = as(A, "dgRMatrix");
B.csr = t(A.csr);
A.csc = as(A, "dgCMatrix");
B.csc = t(A.csc);
So if I wanted to multiply A.csr times B.csr using eigen, how to do that in C++? I do not want to have to convert types if I don't have to. It is a memory size thing.
The A.csr %*% B.csr is not-yet-implemented.
The A.csc %*% B.csc is working.
I would like to microbenchmark the different options, and see how matrix size will be most efficient. In the end, I will have a matrix that is about 1% sparse and have 5 million rows and cols ...
There's a reason that dgRMatrix crossproduct functions are not yet implemented, in fact, they should not be implemented because otherwise they would enable bad practice.
There are a few performance considerations when working with sparse matrices:
Accessing marginal views against the major marginal orientation is highly inefficient. For instance, a column iterator in a dgRMatrix and a row iterator in a dgCMatrix need to loop through almost all elements of the matrix to find the ones in just that column or row. See this Rcpp gallery post for additional enlightenment.
A matrix cross-product is simply a dot product between all combinations of columns. This means the penalty of using a column iterator in a dgRMatrix (vs. a column iterator in a dgCMatrix) is multiplied by the number of column combinations.
Cross-product functions in R are highly optimized, and are not (in my experience) significantly faster than Eigen, Armadillo, equivalent STL variants. They are parallelized, and the Matrix package takes wonderful advantage of these optimized algorithms. I have written C++ parallelized STL cross-product variants using Rcpp structures and I don't see any increase in performance.
If you're really going this route, check out my Rcpp gallery post on Sparse Matrix structures in Rcpp. This is to be preferred to Eigen and Armadillo Sparse Matrices if memory is a concern, as Eigen and Armadillo perform a deep copy rather than a reference to an R object already existing in memory.
At 1% density, the inefficiencies of row iterators will be greater than at say 5 or 10% density. I do most of my tests at 5% density and generally binary operations take 5-10x longer for row iterators than for column iterators.
There may be applications where row-major ordering shines (i.e. see the work by Dmitry Selivanov on CSR matrices and irlba svd), but this is absolutely not one of them, in fact, so much so you are better off doing in-place conversion to get to a CSC matrix.
tl;dr: column-wise cross-product in row-major matrices is the ultimatum of inefficiency.
I'm doing some physics simulation in C++ using Armadillo. I need to calculate a product looking like:
Q = R * exp(neg_i*Lambda*t) * R.t() * Q
Where Q,R are cx_mat class of the same size, Lambda is a mat class of the same size as Q,R and is diagonal, neg_i is -i the complex number and t is a double. I should get a unitary matrix as a solution but what I'm getting is non unitary. I was wondering if the exponential function works well with complex matrices? or if not what should I replace it with?
You need to use the expmat() function for a matrix exponential, exp() calculates an element-wise exponential.
For example, some code I'm currently using for a physics simulation:
arma::cx_mat f; // A hermitian matrix
double delta_t ; // A time step
std::complex<double> i_imag(0.0,1.0) ; // i, the imaginary number
std::vector<arma::cx_mat> U; // A vector of complex unitary matrices.
U.push_back(arma::expmat(-i_imag * delta_t * f));
Have tested this code, taking the matrix exponential of anti-hermitian matrices to get unitary transformation and works fine.
I have a question on why matrix multiplication is %*% in R but just * in C++.
Example:
in R script:
FunR <- function(mX, mY) {
mZ = mX %*% mY
mZInv = solve(mZ)
return(mZInv)
}
in C++ script:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
mat FunC(mat mX, mat mY) {
mat mZ = mX * mY;
mat mZInv = mZ.i();
return mZInv;
}
I ask because C++ can be easily incorporated into R documents.
Also, the "*" character is used to multiply matrices in R but it is not the standard matrix product as we know it. How are you supposed to know this stuff?
R and C++ are different languages. There is no reason to expect them to share syntax. You should be more surprised when the syntax matches than when it differs.
That being said, when you have a package, like Rcpp, that integrates languages, there usually is some attempt to make the syntax consistent. So why not use the same operator as R in this case? Because it is not possible. The list of operators in C++ is fixed, and %*% is not on that list. The operator * is on the list, though, so that operator could be chosen. Always better to choose something that can be chosen than to have nothing work. :)
(In case it got missed along the way: C++ has no native support for matrix operations. There is no matrix multiplication "in C++", only in specific libraries, such as Armadillo.)
I'm a new to Eigen and I'm working with sparse LU problem.
I found that if I create a vector b(n), Eigen could compute the x(n) for the Ax=b equation.
Questions:
How to display the L & U, which is the factorization result of the original matrix A?
How to insert non-zeros in Eigen? Right now I just test with some small sparse matrix so I insert non-zeros one by one, but if I have a large-scale matrix, how can I input the matrix in my program?
I realize that this question was asked a long time ago. Apparently, referring to Eigen documentation:
an expression of the matrix L, internally stored as supernodes The only operation available with this expression is the triangular solve
So there is no way to actually convert this to an actual sparse matrix to display it. Eigen::FullPivLU performs dense decomposition and is of no use to us here. Using it on a large sparse matrix, we would quickly run out of memory while trying to convert it to dense, and the time required to compute the factorization would increase several orders of magnitude.
An alternative solution is using the CSparse library from the Suite Sparse as:
extern "C" { // we are in C++ now, since you are using Eigen
#include <csparse/cs.h>
}
const cs *p_matrix = ...; // perhaps possible to use Eigen::internal::viewAsCholmod()
css *p_symbolic_decomposition;
csn *p_factor;
p_symbolic_decomposition = cs_sqr(2, p_matrix, 0); // 1 = ordering A + AT, 2 = ATA
p_factor = cs_lu(p_matrix, m_p_symbolic_decomposition, 1.0); // tol = 1.0 for ATA ordering, or use A + AT with a small tol if the matrix has amostly symmetric nonzero pattern and large enough entries on its diagonal
// calculate ordering, symbolic decomposition and numerical decomposition
cs *L = p_factor->L, *U = p_factor->U;
// there they are (perhaps can use Eigen::internal::viewAsEigen())
cs_sfree(p_symbolic_decomposition); cs_nfree(p_factor);
// clean up (deletes the L and U matrices)
Note that although this does not use expliit vectorization as some Eigen functions do, it is still fairly fast. CSparse is also very compact, it is just a single header and about thirty .c files with no external dependencies. Easy to incorporate in any C++ project. There is no need to actually include all of Suite Sparse.
If you'll use Eigen::FullPivLU::matrixLU() to the original matrix, you'll receive LU decomposition matrix. To display L and U separately, you can use method triangularView<mode>. In Eigen wiki you can find good example of it. Inserting nonzeros into matrices depends on numbers, which you wan't to put. Eigen has convenient syntax, so you can easily insert values in loop:
for(int i=0;i<size;i++)
{
for(int j=size;j>someNumber;j--)
{
matrix(i,j)=yourClass.getNextNumber();
}
}
I need to convert a MATLAB code into C++, and I'm stuck with this instruction:
a = K\F
, where K is a sparse matrix of size n x n, and F is a column vector of size n.
I know it's easy to solve that using the Eigen library - I have tried the fullPivLu() method, and I've been able to built a working snippet, using a Matrix and a Vector.
However, my K is a SparseMatrix<double> (while F is a VectorXd). My declarations:
SparseMatrix<double> K(nec, nec);
VectorXd F(nec);
and it seems that SparseMatrix doesn't have the fullPivLu() method, nor the lu() one.
I've tried, in fact, these two different approaches, taken from the documentation:
//1.
MatrixXd x = K.fullPivLu().solve(F);
//2.
VectorXf x;
K.lu().solve(F, &x);
They don't work, because fullPivLu() and lu() are not members of 'Eigen::SparseMatrix<_Scalar>'
So, I am asking: is there a way to solve a system of linear equations (the MATLAB's mldivide, or '\'), using Eigen for C++, with K being a sparse matrix?
Thank you for any help.
Would Eigen::SparseLU work for you?