dilevin / CSC417-a2-mass-spring-3d

Assignment 2 for CSC2549
11 stars 11 forks source link

Sparse-dense Matrix Addition #23

Open koichishi opened 3 years ago

koichishi commented 3 years ago

In the documentation https://eigen.tuxfamily.org/dox/group__TutorialSparse.html the addition between sparse and dense matrix is supported. However in assemble_stiffness, when I construct Eigen::SparseMatrixd Ej; Eigen::MatrixXd Vj (correct dimension) and apply K += Ej.transpose() * Vj * Ej, an error occurs saying no '+=' matches Eigen::SparseMatrixd += Eigen::MatrixXd.

This error does not appear when I switch the order - add a sparse matrix to a dense matrix. I have looked up for a way to solve this but fails. This bug report is potentially helpful though: https://eigen.tuxfamily.org/bz/show_bug.cgi?id=632

Could you help me out?

koichishi commented 3 years ago

PS: K is the passed-in sparse stiffness matrix so I don't think I should change its type if I implement it correctly...

cetiniz commented 3 years ago

You can just have the large sparse matrix 'K' as the sparse matrix. When constructing it make a vector<Eigen::Triplet<>> to store the coordinates of all the non zero positions and then you can set the sparse matrix from that vector.

abhimadan commented 3 years ago

Adding a dense matrix to a sparse matrix is supported, but it produces a dense matrix, which you can see from the error message no '+=' matches Eigen::SparseMatrixd += Eigen::MatrixXd (try thinking about why this must be the case - what problems would you get if you tried to add a sparse matrix to a dense matrix and stored it in a sparse format?).

At any rate, you don't need to implement assembly using projection matrices. While that's the representation used in the notes, that's just for concise notation - the key thing it's doing is placing the per-spring Hessian entries in the right locations in the big Hessian matrix. One student has said they encountered performance problems doing this placement with matrix multiplication, so try thinking of a way to place the entries directly into K with a triplet list (entries with the same row and column indices are added together by default when they're put into the sparse matrix).