# Symmetric Matrix Computation (Eigen & C++)

I want to compute a symmetric matrix A from a vector b by: A = b * b'.

Does the Eigen library automatically take into account that it does not need to do all calculations to get A (because of the symmetry which repeats most of the matrix entries)?

If not, I could take advantage of the symmetry and write my own function for the computation.

• What do you need $A$ for? If $b$ is a slim matrix and you want to use $A$ it in an iterative algorithm, you will be more efficient by writing a function that for a given vector $v$ computes $Av = b*(b'*v)$. – Jan Sep 9 '13 at 10:18
• A is needed in a generalized eigenvalue problem - so yes, I do need all the entries. – Armin Meisterhirn Sep 9 '13 at 13:09
• If your generalized eigenvalue problem solver wants a matrix with all of the entries, then I wouldn't worry about the speed of constructing this outer product matrix. I have to imagine that the time required to solve the generalized ev problem will dwarf the time required to make this outer product. – k20 Sep 9 '13 at 13:46
• Why not solve the generalized eigenvalue problem with a Krylov or Davidson method that would allow you to exploit the efficient computation of Av? – Jeff Sep 11 '13 at 17:15
• Thanks for the input, Jeff. I'll have a look at these methods. – Armin Meisterhirn Sep 20 '13 at 19:02

This is a typical case where it does not make sense to actually store the matrix. Rather, consider the matrix to be an operator where

• if you need to multiply by it, i.e., form $y=Ax$, you do $y=b (b\cdot x)$ instead

• if you need to access an element $A_{ij}$, you instead compute it as $b_ib_j$.

In other words, knowing what $A$ is, you should just not store it as a matrix but simply as a single vector. You can't expect libraries to do these things for you, but you should do it yourself.

• This matrix is needed in a generalized eigenvalue problem - so yes, I do need all the entries. – Armin Meisterhirn Sep 9 '13 at 13:09
• But what I'm saying is that you need the matrix, but you may not need to store every element of it. You can compute the elements on the fly, and you can multiply with the matrix, without actually storing all $n\times n$ elements. – Wolfgang Bangerth Sep 9 '13 at 13:52
• @WolfgangBangerth In theory this is true, but which software packages will deal with this? If you use something like LAPACK for the generalized eigenvalue solver, then it will not let you represent matrices in such a clever way, right? – k20 Sep 9 '13 at 13:58
• Yes, k20. That's exactly what I'm using. Maybe it's useful to store it like WolfgangBangerth describes if you write your own generalized eigenvalue solver (which is a lot of work). – Armin Meisterhirn Sep 9 '13 at 15:45
• PETSc and its eigenvalue solver SLEPc allow you to pass in operators as opposed to matrices. You can represent your outer-product matrix as an operator, and SLEPc will be able to give you eigenvalues for it. It's true that LAPACK doesn't. I don't know about Eigen. – Wolfgang Bangerth Sep 10 '13 at 4:02

The Eigen library cannot "know" a priori if the product b*b' will result in a symmetric matrix, therefore you can write your own method (although it'll be somewhat useless if not working with large matrices)

However, a routine called isSymmetric, is used within some linear solvers to check whether the input matrix is symmetric or not, but of course, that does not fulfill your need.

• But you can tell it do so by using views, for example, the triangularView. – Armin Meisterhirn Sep 9 '13 at 21:43