Skip to content

Matrix eigs eigenvectors are out of order compared to eigenvalues #1844

@Lazersmoke

Description

@Lazersmoke

The documentation example has this line:

const ans = math.eigs(H) // returns {values: [E1,E2...sorted], vectors: [v1,v2.... corresponding vectors]}

Which tells us mathematically that:

H * v1 = E1 * v1

However, this doesn't actually work:

>>> H = [[1,0,0],[0,1,0],[0,0,-1]]; e =math.eigs(H)
>>> math.multiply(H,e.vectors[0]) 
[0,1,0]
>>> math.multiply(e.values[0],e.vectors[0])
[-0,-1,-0]

These should give the same result! In this particular case, e.vectors[1] goes with e.values[0]:

>>> math.multiply(H,e.vectors[1])
[0,0,-1]
>>> math.multiply(e.values[0],e.vectors[1])
[-0,-0,-1]

The reason is that the eigenvalues are sorted, but the eigenvectors are still in the original, unsorted order, and thus not "corresponding" as implied by the documentation.

Slow workaround:

var e = math.eigs(H)
// Sort in place
e.vectors.sort((v,w) => {
  // Compare by eigenvalues
  return math.dot(math.multiply(H,v),v) - math.dot(math.multiply(H,w),w)
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions