andreaferretti / neo

A matrix library
https://andreaferretti.github.io/neo/
Apache License 2.0
244 stars 20 forks source link

[Question] How to Calculate Eigenvectors and Eigenvalues #36

Closed GaudiestTooth17 closed 3 years ago

GaudiestTooth17 commented 3 years ago

Has anyone done this before or have any tips about how to proceed? The documentation says that this feature still needs to be documented and I'm new to nim and neo.

andreaferretti commented 3 years ago

For eigenvalues, there is a dedicated function https://play.nim-lang.org/#ix=2LSo

import neo

let m = matrix(@[
    @[1f64, 2, 3],
    @[4f64, 5, 6],
    @[7f64, 8, 9]])
let e = m.eigenvalues()
echo e.real
echo e.img

For normal matrices, the eigenvectors can be read from the column vectors of its Schur decomposition (m.schur()), but I do not recall whether there is something more general than that in Neo

andreaferretti commented 3 years ago

Something like this should work:

import neo

let
  m = matrix(@[
    @[1f64, 4, 7],
    @[4f64, 5, 8],
    @[7f64, 8, 9]
  ])
  s = m.schur()
  e = s.eigenvalues.real
  f = s.factorization
for i in 0 .. 2:
  echo e[i]
  echo f.column(i)

https://play.nim-lang.org/#ix=2LSx

GaudiestTooth17 commented 3 years ago

Thank you!