3

In the case of multiple of same matrix matA, like

matA.transpose()*matA, 

You don't have to compute all result product, because the result matrix is symmetric(so only if the m>n), in my specific case is always symmetric! square.

So its enough the compute only for. ex. lower triangular part and rest only copy..... because the results of the multiple 2nd and 3rd row, resp.col, is the same like 3rd and 2nd.....And etc....

So my question is , exist way how to tell Eigen, to compute only lower part. and optionally save to only lower trinaguler part the product?

    DATA = SparseMatrix<double>((SparseMatrix<double>(matA.transpose()) * matA).pruned()).toDense();
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
user2165656
  • 445
  • 1
  • 3
  • 11

2 Answers2

4

https://eigen.tuxfamily.org/dox/classEigen_1_1SparseSelfAdjointView.html

The symmetric rank update is defined as:

B = B + alpha * A * A^T

where alpha is a scalar. In your case, you are doing A^T * A, so you should pass the transposed matrix instead. The resulting matrix will only store the upper or lower portion of the matrix, whichever you prefer. For example:

SparseMatrix<double> B;
B.selfadjointView<Lower>().rankUpdate(A.transpose());
Charlie S
  • 305
  • 1
  • 2
  • 7
3

According to the documentation, you can evaluate the lower triangle of a matrix with:

m1.triangularView<Eigen::Lower>() = m2 + m3;

or in your case:

m1.triangularView<Eigen::Lower>() = matA.transpose()*matA;

(where it says "Writing to a specific triangular part: (only the referenced triangular part is evaluated)"). Otherwise, in the line you've written Eigen will calculate the entire sparse matrix matA.transpose()*matA.

Regarding saving the resulting m1 matrix, it is the same as saving whatever type of matrix it is (Eigen::MatrixXt or Eigen::SparseMatrix<t>). If m1 is sparse, then it will be only half the size of a straightforward matA.transpose()*matA. If m1 is dense, then it will be the full square matrix.

Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56
  • So the when I save only .triangularView(), it will be automatically coompute half of result product? like O(n^3 /2), – user2165656 Jun 12 '15 at 13:40
  • I don't know what you mean by "save". In the above example, only the lower triangle will be calculated, whatever the complexity. – Avi Ginsburg Jun 12 '15 at 13:55
  • it looks like some incorrect values if I store to dense product of this multiplactaion,..all zeros – user2165656 Jun 12 '15 at 14:04
  • is this correct?: DATA.triangularView()-= largestEvalue * approx * approx.transpose(); – user2165656 Jun 12 '15 at 15:14
  • Again, I have no idea what "correct" is. I don't know what any of those variables are. There is no information as to what they are. For all I know, `largestEvalue` is 0. Questions need context. – Avi Ginsburg Jun 14 '15 at 05:50