eigen library selfadjointView problem - c++

I am persistently getting error messages whenever I try to use the selfadjointView property of any matrix or sparse matrix using the eigen library. Below is a simple code to check that. In my program I do try with self-adjoint matrix:
#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <Eigen/Sparse>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <iostream>
using namespace Eigen;
int main ()
{
SparseMatrix<float> mat(3,3);
Matrix<float, 3, 1> vec;
std::cout<<mat.selfadjointView<>()*vec;
}
The error message I get is:
error: no matching function for call to ‚'Eigen::SparseMatrix::selfadjointView()‚

You have to specify the template argument, so it should read mat.selfadjointView<Upper>() or mat.selfadjointView<Lower>() . The first one means that it should use the entries in the upper triangular part of mat and fill the lower triangular part to make the matrix self-adjoint. The second one is the other way around.

Related

Why isn't my mkl sparse matrix module working properly?

I first created a CSR matrix using the mkl sparse matrix module.
This part is normal and can be created.
Then I used mkl_sparse_s_add for matrix addition, and then the program reported an error.
The content of the error report is
Exception thrown at 0x00007FFDA75F478C (KernelBase.dll) (in mkl.exe): 0xC06D007E: Module not found (parameter: 0x000000CEB30FF5B0).
Here's my code
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "mkl_spblas.h"
#include <mkl.h>
int main() {
MKL_INT rowPtr[6] = { 0,3,5,8,11,13 };
MKL_INT columns[13] = { 0,1,3,0,1,2,3,4,0,2,3,1,4 };
float values[13] = { 1,-1,-3,-2,5,4,6,4,-4,2,7,8,-5 };
sparse_matrix_t elementMatrix2; sparse_matrix_t elementMatrix3;
mkl_sparse_s_create_csr(&elementMatrix2,SPARSE_INDEX_BASE_ZERO,5,5,rowPtr,rowPtr+1,columns,values);
mkl_sparse_s_add(SPARSE_OPERATION_NON_TRANSPOSE, elementMatrix2, 1, elementMatrix2, &elementMatrix3);
}
Helps me run the program normally
Anyone else who is looking into this with similar issues can refer to the Intel communities for the solution as this query has been addressed here.
In this case, the issue got resolved after reinstalling the MKL.

How do I calculate the exponential of a complex matrix?

I'm having trouble trying to calculate the exponential of a complex matrix with the C++ Eigen library.
Below is an example code I try to make work.
#include <iostream>
#include "Dense"
#include <complex>
#include "unsupported/Eigen/src/MatrixFunctions/MatrixExponential.h"
int main()
{
using namespace std::complex_literals;
Eigen::MatrixXcd test(2,2);
test(0,0)=1i+std::complex<double>(5);
test(1,0)=1i*2.;
test(0,1)=std::complex<double>(2);
test(1,1)=3.*1i+std::complex<double>(3);
std::cout << "The matrix exponential is:\n"
<< test.exp() << "\n\n";
}
When I run this program I get the error:
Implicit instantiation of undefined template 'Eigen::MatrixFunctionReturnValue<Eigen::Matrix<std::__1::complex<double>, -1, -1, 0, -1, -1> >'
I have tried to find an answer but I haven't found one yet.
Any help would be greatly appreciated.
Edit:
The standard matrix operations in Eigen work and the Eigen file/folder are located in my project folder. The only functions that don't seem to work are the matrix functions in the unsupported folder for complex matrixes (they do work for real ones).
You must not directly include headers from the Eigen/src or unsupported/Eigen/src subdirectories. Also, instead of #include "Dense" use #include <Eigen/Dense> (in many cases <Eigen/Core> is actually sufficient).
In your case you actually just need these includes, because all necessary dependencies are included by MatrixFunctions:
#include <iostream>
#include <unsupported/Eigen/MatrixFunctions>
Godbolt-Demo: https://godbolt.org/z/PmJWP3 (compilation may occasionally time out).

Why Scalar does not work?

Following code results in build success, but no window. Without "m = Scalar(255,0,0);", it creates black window. Why including scalar does not work?
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
Mat m = Mat::zeros(200,200,CV_8UC3);
m = Scalar(255,0,0); //without this, it creates window.
imshow("m", m);
waitKey();
}
It will not give you any compilation error as Mat and Scalar are basically array types of OpenCV.
This is a possible duplicate of How to set all pixels of an OpenCV Mat to a specific value?
Your code have to work!
take a look at the doc:
C++: void imshow(const string& winname, InputArray mat)
and I can confirm you is working fine on my environment(opencv320, VisualStudio2017):
you need to clean the project and build again.

How to save Eigen::DiagonalMatrix in MarketIO format in Eigen?

Using Eigen 3.2.1, I am trying to save an Eigen::DiagonalMatrix in MarketIO format as below:
#include <Eigen/Sparse>
#include <Unsupported/Eigen/SparseExtra>
using namespace Eigen;
...
size_t n = XX;
DiagonalMatrix<num_t, Dynamic> W(n);
...
saveMarket(W, "W.txt"); // error propagates from here
However, I am getting the following error:
MarketIO.h|236|error: 'const class Eigen::DiagonalMatrix<double, -1>' has no
member named 'nonZeros'
What is the problem here? Is this implemented at all for Diagonal Matrices?
Thanks in advance for any help.
Okay! The only solution for now with minimal effort is to use the following:
saveMarketVector(W.diagonal(), "W.txt");

Calculating Covarince matrix using Eigen library, C++ Linux

I'm trying to do some operations on matrices using a matrix library named, "Eigen Library". I've a 50 X 100000 matrix in size and I want to find its covariance matrix. So, can I get a built_in function to to find covariance matrix?
C++ sample code:
#include "Eigen/Core"
#include "Eigen/Dense"
using namespace Eigen;
using namespace std;
int main()
{
MatrixXf my_matrx = MatrixXf::Random(50,100000);
//Now, i want to find a function to find the covariance matrix of "my_matrx", like below:
MatrixXf my_cov_matrix = any_function(my_matrx);
}