Extracting Matrices from the Output of eigenvectors()

In class I sometimes need to use matrices of eigenvalues and eigenvectors, but the output of eigenvectors() isn’t particularly helpful for that out of the box.

Here are two one-liners that work in the case of simple eigenvalues.  I’ll post updates as needed:

First eigU(), takes the output of eigenvectors() and returns matrix of eigenvectors:

eigU(v):=transpose(apply('matrix,makelist(part(v,2,i,1),i,1,length(part(v,2)),1)));

And eigdiag(), which takes the output of eigenvectors() and returns diagonal matrix of eigenvalues:

eigdiag(v):=apply('diag_matrix,part(v,1,1));

eigs

For a matrix with a full set of eigenvectors but eigenvalues of multiplicity greater than one, the lines above fail.  A version of the above that works correctly in that case could look like:

eigdiag(v):=apply('diag_matrix,flatten(makelist(makelist(part(v,1,1,j),i,1,part(v,1,2,j)),j,1,length(part(v,1,1)))));

eigU(v):=transpose(apply('matrix,makelist(makelist(flatten(part(v,2))[i],i,lsum(i,i,part(v,1,2))*j+1,lsum(i,i,part(v,1,2))*j+lsum(i,i,part(v,1,2))),j,0,lsum(i,i,part(v,1,2))-1)));

5 thoughts on “Extracting Matrices from the Output of eigenvectors()”

  1. Hi,
    Thanks for the post describing how to use Maxima’s eigenvector and value functions. Unfortunately, there’s still a subtlety in Maxima’s output format that causes the macros you gave above to fail – that is, when Maxima returns vectors that have multiplicities greater than 1.

    For example, consider the matrix:
    M : matrix (
    [1, kb, 0, 0],
    [ka, 1, 0, 0],
    [0, kb * (1 – kb), 1, 0],
    [ka * (1 – ka), 0, 0, 1]
    );

    Maxima returns three eigenvalues for this matrix and lists the last as having two multiplicities. This breaks your macro.

    Do you have any alternate versions that support multiplicities?

    Thanks,
    Larry

    Like

    1. Right!

      For a matrix like you describe, I think you could use the lines below
      I know they look awful and don’t really fit the description of “quick one-liner”

      eigdiag(v):=apply(‘diag_matrix,flatten(makelist(makelist(part(v,1,1,j),i,1,part(v,1,2,j)),j,1,length(part(v,1,1)))));

      and

      eigU(v):=transpose(apply(‘matrix,makelist(makelist(flatten(part(v,2))[i],i,lsum(i,i,part(v,1,2))*j+1,lsum(i,i,part(v,1,2))*j+lsum(i,i,part(v,1,2))),j,0,lsum(i,i,part(v,1,2))-1)));

      These aren’t entirely general either—
      This fails in the event that maxima can’t find the eigenvectors, as with
      A:matrix([3,6,9],
      [2,4,5],
      [3,2,1]);

      and also fails in the case of a jordan block where there isn’t a full set of eigenvectors
      A:matrix([1,1],[0,1])

      Like

  2. Found this while working with STACK for Moodle, can confirm that your code works there as well. Thank you very much, I was looking for something like this to create online excercises on diagonalization of (symmetric) matrices.

    Like

Leave a comment