Sidechair

matrix.is_identity()

Browse the reference

Returns true when a matrix is square with ones on the main diagonal and zeros everywhere else.

Try it in the editor

An identity matrix has 1 on the main diagonal and 0 everywhere else: [[1, 0], [0, 1]] passes, [[1, 0], [0, 2]] fails because of the 2, and a 2 by 3 grid always fails. Multiplying any matrix by an identity matrix of matching size leaves it unchanged.

Matrices built with floating-point arithmetic, such as a matrix times its inverse, can land a hair away from 1 and 0, so inspect such a result before trusting the test.

Syntax

Pine Script
matrix.is_identity(id: matrix<int/float>) -> series bool
matrix.is_identity() -> series bool

Arguments

NameTypeDefaultDescription
idmatrix<int/float>requiredThe matrix tested for ones on the main diagonal and zeros elsewhere; a non-square matrix is never identity.

Returns

  • series bool: true when the diagonal is all ones and everything else is zero, otherwise false.
  • series bool: true when the receiving matrix is an identity matrix, otherwise false.

Example

Pine ScriptVerify an inverseOpen in editor
//@version=6
indicator("matrix.is_identity: verify", overlay = false)
matrix<float> a = matrix.new<float>(2, 2, 0.0)
matrix.set(a, 0, 0, 2.0)
matrix.set(a, 1, 1, 2.0)
check = matrix.mult(a, matrix.inv(a))
plot(matrix.is_identity(check) ? 1 : 0, "A times its inverse is identity")

See also

Runs on Sidechair