Sidechair

matrix

Browse the reference

The type of a two-dimensional grid of values with a fixed element type, such as matrix<float>.

Try it in the editor

A matrix holds rows and columns of items that all share one type. Declare one with the generic form, matrix<float> m = matrix.new<float>(3, 3, 0.0), and address an item by row and column with matrix.get() and matrix.set(). Indexes start at 0.

Beyond storage, matrices come with linear algebra: products, transposes, determinants, inverses, eigenvalues and tests for special shapes. Rows and columns can be pulled out as arrays and added or removed while the script runs.

Remarks

  • A matrix holds up to 100,000 elements.
  • Matrices are reference types: a second variable assigned from a matrix shares it, and matrix.copy() makes an independent grid.

Example

Pine ScriptA 2 by 2 matrix and its determinantOpen in editor
//@version=6
indicator("matrix type: determinant", overlay = false)
matrix<float> m = matrix.new<float>(2, 2, 0.0)
matrix.set(m, 0, 0, 2.0)
matrix.set(m, 0, 1, 1.0)
matrix.set(m, 1, 0, 1.0)
matrix.set(m, 1, 1, 3.0)
plot(matrix.det(m), "Determinant")

See also

Runs on Sidechair