Sidechair

matrix.is_square()

Browse the reference

Returns true when a matrix has as many rows as columns.

Try it in the editor

A matrix is square when its row count equals its column count: a 2 by 2 or 3 by 3 grid passes and a 2 by 3 grid does not.

Determinants, inverses, traces and eigenvalues only exist for square matrices, so this is the guard to run before calling them. Compare matrix.rows() with matrix.columns() when you need the two counts themselves.

Syntax

Pine Script
matrix.is_square(id: any matrix type) -> series bool
matrix.is_square() -> series bool

Arguments

NameTypeDefaultDescription
idany matrix typerequiredThe matrix whose row count and column count are compared; it is only read, never changed.

Returns

  • series bool: true when the rows and columns match in number, otherwise false.
  • series bool: true when the receiving matrix has as many rows as columns, otherwise false.

Example

Pine ScriptGuard a determinantOpen in editor
//@version=6
indicator("matrix.is_square: guard", overlay = false)
matrix<float> m = matrix.new<float>(2, 3, 1.0)
plot(matrix.is_square(m) ? matrix.det(m) : na, "Determinant if square")

See also

Runs on Sidechair