Sidechair

matrix.eigenvectors()

Browse the reference

Returns the eigenvectors of a square matrix, one in each column of a new matrix.

Try it in the editor

The column at position i belongs to the eigenvalue at position i from matrix.eigenvalues(). An eigenvector keeps its direction when the matrix is applied to it.

For [[3, 0], [0, 1]] the columns are the two unit directions [1, 0] and [0, 1], ordered to match the eigenvalues. Use it with a covariance matrix to find the principal directions of a set of returns.

Syntax

Pine Script
matrix.eigenvectors(id: matrix<int/float>) -> matrix<float>
matrix.eigenvectors(id: matrix<int>) -> matrix<int>
matrix.eigenvectors() -> matrix<float>
matrix.eigenvectors() -> matrix<int>

Arguments

NameTypeDefaultDescription
idmatrix<int/float> or matrix<int>requiredThe square matrix to decompose; its eigenvectors come back as columns of a matrix the same size.

Returns

  • matrix<float>: A float matrix the same size as id, where column i is the eigenvector for eigenvalue i.
  • matrix<int>: A matrix typed as integers, where column i is the eigenvector for eigenvalue i.
  • matrix<float>: A float matrix whose columns are the eigenvectors of the receiving matrix.
  • matrix<int>: A matrix typed as integers whose columns are the eigenvectors of the receiving matrix.

Remarks

  • On Sidechair: the columns follow the eigenvalues, smallest first, and a complex pair gives na columns.

Example

Pine ScriptFirst principal directionOpen in editor
//@version=6
indicator("matrix.eigenvectors: direction", overlay = false)
matrix<float> cov = matrix.new<float>(2, 2, 0.0)
matrix.set(cov, 0, 0, 3.0)
matrix.set(cov, 1, 1, 1.0)
vecs = matrix.eigenvectors(cov)
plot(array.get(matrix.col(vecs, 0), 0), "First component")

See also

Runs on Sidechair