Home » Mathemtical Functions » Understanding matrix.eigenvalues() Function in Pine Script

Understanding matrix.eigenvalues() Function in Pine Script

Photo of author
Published on

Pine Script’s matrix.eigenvalues() function is a powerful tool for financial analysts and traders who use technical analysis and mathematical models to make informed decisions. This function returns the eigenvalues of a square matrix, which are crucial in various applications, including but not limited to, system stability analysis, optimization problems, and in the financial domain, for portfolio optimization and risk assessment.


Pine Script provides a straightforward syntax for extracting the eigenvalues from a matrix:

  • matrix.eigenvalues(id) → array<float>
  • matrix.eigenvalues(id) → array<int>


  • id (matrix<int/float>): This argument specifies the matrix object from which you want to extract the eigenvalues. The matrix must be a square matrix (i.e., having the same number of rows and columns).


Let’s delve into an example to understand how to implement and use the matrix.eigenvalues() function effectively in Pine Script.

indicator("Eigenvalues Demonstration")

// Execute this code only once for efficiency.
if barstate.islastconfirmedhistory
    // Initialize a 2x2 integer matrix.
    var matrix2x2 = matrix.new<int>(2, 2, na)
    // Populate the matrix with integers.
    matrix.set(matrix2x2, 0, 0, 2)
    matrix.set(matrix2x2, 0, 1, 4)
    matrix.set(matrix2x2, 1, 0, 6)
    matrix.set(matrix2x2, 1, 1, 8)
    // Retrieve the eigenvalues of the matrix.
    eigenValuesArray = matrix.eigenvalues(matrix2x2)
    // Display the matrix and its eigenvalues.
    var tableDisplay = table.new(position.top_right, 2, 2, color.green)
    table.cell(tableDisplay, 0, 0, "Matrix Elements:")
    table.cell(tableDisplay, 0, 1, str.tostring(matrix2x2))
    table.cell(tableDisplay, 1, 0, "Eigenvalues:")
    table.cell(tableDisplay, 1, 1, str.tostring(eigenValuesArray)) 

Detailed Explanation

  • Initialization: We initiate a matrix with matrix.new<int>(2, 2, na) to create a 2×2 matrix filled initially with na (not available) values.
  • Population: The matrix.set() function is used to fill the matrix with integer values, specifying each element’s row and column indices followed by the value.
  • Eigenvalues Extraction: matrix.eigenvalues(matrix2x2) is called to get the array of eigenvalues of the matrix.
  • Display: We utilize Pine Scripts table.new() and table.cell() functions to display the matrix and its eigenvalues on the chart for visualization and analysis purposes.

Key Features

  • Function Usability: matrix.eigenvalues() is used to compute the eigenvalues of a square matrix, which can be critical in financial modeling and quantitative analysis.
  • Syntax and Application: The function supports both integer and float matrices, making it versatile for various data types and applications.
  • The Implicit QL Algorithm: This function relies on the Implicit QL Algorithm for calculating the eigenvalues, ensuring efficient and accurate computations.


  • matrix.eigenvalues() is essential for quantitative analysis in trading and financial modeling, providing valuable insights into matrix characteristics.
  • The function supports matrices of integers and floats, enhancing its applicability across different scenarios and data types.
  • Understanding how to manipulate and analyze matrices with Pine Script can significantly broaden the analytical capabilities available to traders and analysts.

Leave a Comment