Sidechair

matrix.is_diagonal()

Browse the reference

Returns true when every item off the main diagonal is zero.

Try it in the editor

A diagonal matrix has zeros everywhere off the main diagonal: [[2, 0], [0, 3]] passes, while [[2, 1], [0, 3]] fails because of the 1 above the diagonal. The diagonal items themselves can be anything.

Every identity matrix is diagonal, and so is the covariance matrix of uncorrelated series. A zero matrix also counts, since the test looks only at the layout of zeros.

Syntax

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

Arguments

NameTypeDefaultDescription
idmatrix<int/float>requiredThe matrix whose off-diagonal items are checked for zero; its main diagonal items may hold any value.

Returns

  • series bool: true when every item off the main diagonal is zero, otherwise false.
  • series bool: true when the receiving matrix is diagonal, otherwise false.

Example

Pine ScriptUncorrelated checkOpen in editor
//@version=6
indicator("matrix.is_diagonal: uncorrelated", overlay = false)
matrix<float> cov = matrix.new<float>(2, 2, 0.0)
matrix.set(cov, 0, 0, 2.0)
matrix.set(cov, 1, 1, 3.0)
plot(matrix.is_diagonal(cov) ? 1 : 0, "Diagonal")

See also

Runs on Sidechair