matrix.inv() - Pine Script Function
matrix.inv()
Section titled “matrix.inv()”Overview
Section titled “Overview”The function returns the inverse of a square matrix.
Syntax
Section titled “Syntax”matrix.inv(id) → matrix<float>Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| id | matrix<int/float> | A matrix object. |
Returns
Section titled “Returns”A new matrix, which is the inverse of the id matrix.
Remarks
Section titled “Remarks”- The function is calculated using the LU decomposition algorithm.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6indicator("`matrix.inv()` Example")// For efficiency, execute this code only once.if barstate.islastconfirmedhistory // Create a 2x2 matrix.var m1 = matrix.new<int>(2, 2, na) // Fill the matrix with values. matrix.set(m1, 0, 0, 1)matrix.set(m1, 0, 1, 2)matrix.set(m1, 1, 0, 3)matrix.set(m1, 1, 1, 4) // Inverse of the matrix.var m2 = matrix.inv(m1) // Display matrix elements.var t = table.new(position.top_right, 2, 2, color.green)table.cell(t, 0, 0, "Original Matrix:")table.cell(t, 0, 1, str.tostring(m1))table.cell(t, 1, 0, "Inverse matrix:")table.cell(t, 1, 1, str.tostring(m2))