I am trying to use Armadillo to decompose a matrix consisting of integers (i.e. arma::Mat<int>) into eigenvalues and eigenvectors
However, it always gives me compile error no matter what I put as input matrix and output vector/matrix type
It works when i declare the input matrix as arma::Mat<double>, output vector(eigenvalues) as arma::Col<std::complex<double>> and output matrix(eigenvectors) as arma::Mat<std::complex<double>>
I have tried using int and/or std::complex<int> as types for the inputs and the outputs but neither of them worked.
Is there a way that I can do decompose matrices of integer values?
Thanks
First convert the integer matrix to a double matrix using the conv_to function. For example, imat A = ...; mat B = conv_to<mat>::from(A);. Then you can do eigen decomposition on the converted matrix.
Related
I am using Armadillo for some linear algebra problems. It has SpMat<float> for sparse matrices and Mat<float> for dense matrices.
Suppose I have sparse matricesS_a and S_b, and a dense matrix D. I need to compute the produces S_a*S_b and S_a*D, the results will be dense in both cases.
I can convert the sparse matrices into dense matrices and then multiply, but that will be inefficient (these matrices are very large). Is there a way to tell Armadillo to store the results into a dense matrix without performing an intermediate conversion step?
You can use the mat constructor which takes a sparse matrix and converts its data to a dense one:
arma::mat out1(S_a * S_b);
arma::mat out2(S_b * D);
Both multiplication operators for the sparse class (sparse-sparse and sparse-dense) will produce a sparse matrix object as output. (Whether or not it's really sparse will depend on the structure of the inputs.) This can be converted to a dense matrix using the dense matrix constructor with signature: arma::mat(arma::sp_mat).
I wish to convert a simple 2D array into a SparseMatrix, in order to improve performance and run time, since I am dealing with an array of a size around 50,000-70,000.
So far what I have:
SparseMatrix<double> sp;
sp.resize(numCells,numCells);
double Matrix[numCells,numCells];
Matrix = Map<SparseMatrix>(Matrix,numCells,numCells);
The compiler returns type mismatch value at argument 1 in template parameter list for 'template class Eigen::Map'.
I understand I am missing something here, but I can not figure it out.
Make a dense matrix and convert it into a sparse matrix:
double matrix[numCells * numCells]; // 1d array representation of your matrix
SparseMatrix<double> sp = Map<MatrixXd>(matrix,numCells,numCells).sparseView();
I need to calculate power of some matrix and then get its eigenvectors. I know that there is method pow() but it is unclear for me how to use it.
For now, my code is:
Eigen::Matrix3d mat2 = mat1.pow(0.5);
return getEigenvalues(mat2);
Method getEigenvalues() takes Eigen::Matrix type which is not what pow() returns.
That's the wrong order of operations. You first calculate the eigenvalues and next exponentiate those.
The reason is that eigenvalues of the exponentiated matrix are equal to the exponentiated eigenvalues of the original matrix. EDIT: provided the eigenvalues of the original matrix exist.
So, for example, to get the eigenvalues of your matrix mat2 you write:
Eigen::VectorXd ev = getEigenvalues(mat1).unaryExpr([](double d) {return std::pow(d, 0.5);});
In case of exponent one-half, you can also better use std::sqrt.
I forgot to mention that the eigenvectors are identical for the original and the exponentiated matrix, see here for example.
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 am trying to solve a simple least square of type Ax = b. The c++ eigen library offers several functionalities regarding this and I have seen some kind of solutions here: Solving system Ax=b in linear least squares fashion with complex elements and lower-triangular square A matrix and here: Least Squares Solution of Linear Algerbraic Equation Ax = By in Eigen C++
What I want to do is that using dynamic version of the matrix A and b. The elements of matrix A are floating points in my case and has 3 columns, but the number of data items (i.e. rows) will be dynamic (inside a loop).
It will be helpful to have a short code snippet of basic declaration of A, b and filling out values.
If you need dynamic matrices/vectors, just use:
MatrixXd m1(5,7); // double
VectorXd v1(23); // double
MatrixXf m2(3,5); // floating
VectorXf v2(12); // floating
Those variables will all be saved in heap.
If you need square matrices or vectors with fixed size (but be careful, they aren't dynamic!) use the following syntax:
Matrix3d m3; // double, size 3x3
Vector3d v3; // double, size 1x3
Matrix4d m4; // double, size 4x4
Vector4d v4; // double, size 1x4