Matlab code here
N = 4096;
x = linspace(-1,1,N);
A = sparse(100000,100000);
index = 1:32;
A(index,index) = kernel(x(index),x(index));
//kernel here outputs a 32 x 32 matrix
I have to convert the MATLAB code to C++,but I am stuck at sparse function, I tried using:
N=4096;
Eigen::VectorXd x = Eigen::VectorXd::LinSpaced(N,-1,1);
Eigen::SparseMatrix<double> A(Asize,Asize);
A.block(1,1,index.size(), index.size()) = Kernel();
But SparseMatrix has block as read only function and therefore can't be useful for updating the matrix.
Another Point:
I have gone through the Eigen documentation and checked a different form of SparseMatrix declaration :
typedef Eigen::Triplet<double> T;
std::vector<T>tripleList;
tripleList.reserve(nnz);
for(...)
{
// ...
tripletList.push_back(T(i,j,v_ij)); //?? what are these values?
}
A.setFromTriplets(tripleList.begin(), tripleList.end());
But I don't understand what should be value of nnz, should it be the value that I get from Matlab code and what value should I push through the for loop? Will they be random, how do I select the "pushed values" given that matrix size is so large.
Also, the last question remains, how will the sparse matrix after declaration gets updated block-wise?
nnz
means "number of non zeroes". I assume that is for memory allocation in Eigen. Additionally, ` A(index,index) = kernel(x(index),x(index));` this can be done with a simple double nested loop in C++, you dont need a one-liner – Ander Biguri