Function russell_lab::matrix::mat_eigen_sym

source ·
pub fn mat_eigen_sym(
    l: &mut Vector,
    a: &mut Matrix,
    upper: bool
) -> Result<(), StrError>
Expand description

(dsyev) Calculates the eigenvalues and eigenvectors of a symmetric matrix

Computes the eigenvalues l and eigenvectors v, such that:

A ⋅ vj = lj ⋅ vj

where lj is the component j of l and vj is the column j of v.

The computed eigenvectors are normalized to have Euclidean norm equal to 1 and largest component real.

See also: https://netlib.org/lapack/explore-html/dd/d4c/dsyev_8f.html

§Input

  • A – (modified on exit) matrix to compute eigenvalues (SYMMETRIC and SQUARE)
  • upper – Whether the upper triangle of A must be considered instead of the lower triangle.

§Output

  • l – (lambda) will hold the eigenvalues
  • a – will hold the eigenvectors as columns

§Examples

use russell_lab::{mat_eigen_sym, Matrix, Vector};
use russell_lab::StrError;

fn main() -> Result<(), StrError> {
    // set matrix
    let sym = 0.0;
    let data = [
        [2.0, sym, sym],
        [0.0, 3.0, sym],
        [0.0, 4.0, 9.0],
    ];
    let mut a = Matrix::from(&data);

    // perform the eigen-decomposition
    let upper = false;
    let mut l = Vector::new(3);
    mat_eigen_sym(&mut l, &mut a, upper)?;
    println!("eigenvalues =\n{}", l);
    println!("eigenvectors =\n{:.5}", a);

    // check results
    assert_eq!(
        format!("{}", l),
        "┌    ┐\n\
         │  1 │\n\
         │  2 │\n\
         │ 11 │\n\
         └    ┘"
    );
    Ok(())
}