covfefe
covfefe

Reputation: 2675

How to get eigenvalues as a Vector listed in order of magnitude using MathNet Numerics?

I used the Evd<> class of MathNet Numerics to get the eigenvector of a matrix but it turned out to be of type Vector<Complex> and I was unable to cast that into Vector<double>, which is what I need for my operations.

This is how I got the eigenvector:

DenseMatrix processedData = someData;
Evd<> eigen = processedData.evd();
Vector<Complex> eigenvector = (Vector<Complex>)eigen.EigenValues;

When I tried casting as 'Vector<double>' the program wouldn't accept it. Is there a way to get the eigenvector of a matrix in Vector<double>?

Upvotes: 2

Views: 5734

Answers (2)

Christoph R&#252;egg
Christoph R&#252;egg

Reputation: 4736

If you'd like to declare all the types explicitly (instead of use var, selectively) with Math.NET Numerics v3 you need to open the following namespaces:

using System.Numerics
using MathNet.Numerics
using MathNet.Numerics.LinearAlgebra
using MathNet.Numerics.LinearAlgebra.Factorization

There is usually no need to open the type-specific namespaces like MathNet.Numerics.LinearAlgebra.Double as it is recommended to use the generic Matrix<T> type only when refering to a matrix or vector. This way there is no need to cast between them (as you did in your example) at all.

Then the example looks like this:

Matrix<double> processedData = Matrix<double>.Build.Random(5,5);
Evd<double> eigen = processedData.Evd();
Vector<Complex> eigenvector = eigen.EigenValues;

Upvotes: 4

dbc
dbc

Reputation: 117105

Isn't it just the EigenVectors property of the same class?

public abstract class Evd<T> : ISolver<T>
where T : struct, IEquatable<T>, IFormattable
{
    /// <summary>
    /// Gets or sets the eigen values (λ) of matrix in ascending value.
    /// </summary>
    public Vector<Complex> EigenValues { get; private set; }

    /// <summary>
    /// Gets or sets eigenvectors.
    /// </summary>
    public Matrix<T> EigenVectors { get; private set; }
}

Docs here: http://numerics.mathdotnet.com/api/MathNet.Numerics.LinearAlgebra.Factorization/Evd%601.htm#EigenVectors

A real NxN matrix will have up to N (not necessarily unique) real eigenvalues and corresponding eigenvectors, thus both need to be returned in arrays; a complex NxN matrix will have exactly N (not necessarily unique) eigenvalues with corresponding eigenvectors.

Upvotes: 2

Related Questions