Sidechair

matrix.mult()

Browse the reference

Multiplies a matrix by another matrix, by an array of numbers, or by a single number.

Try it in the editor

With a second matrix it is the matrix product: the first matrix needs as many columns as the second has rows, and the result has the first's row count and the second's column count. With an array it is the product of the matrix and a column vector, and the result is an array. With a number every item is scaled.

Order matters: A times B is generally not B times A, and the shapes may allow only one of the two. A worked case: the matrix [[1, 2], [3, 4]] times the array [1, 1] gives [3, 7], each row totalled against the vector.

Syntax

Pine Script
matrix.mult(id1: matrix<int>, id2: series int/float/matrix<int>) -> matrix<int>
matrix.mult(id1: matrix<int/float>, id2: series int/float/matrix<int/float>) -> matrix<float>
matrix.mult(id1: matrix<int>, id2: array<int>) -> array<int>
matrix.mult(id1: matrix<int/float>, id2: array<int/float>) -> array<float>
matrix.mult(id2: series int/float/matrix<int>) -> matrix<int>
matrix.mult(id2: series int/float/matrix<int/float>) -> matrix<float>
matrix.mult(id2: array<int>) -> array<int>
matrix.mult(id2: array<int/float>) -> array<float>

Arguments

NameTypeDefaultDescription
id1matrix<int> or matrix<int/float>requiredThe left-hand matrix. Its column count must equal the row count of id2 when id2 is a matrix, or the length of id2 when it is an array.
id2series int/float/matrix<int> or series int/float/matrix<int/float> or array<int> or array<int/float>requiredThe right-hand operand: a matrix with as many rows as id1 has columns, an array as long as id1's column count, or one number that scales every item.

Returns

  • matrix<int>: A new integer matrix with the row count of the first matrix and the column count of the second; neither input changes.
  • matrix<float>: A new float matrix with the same shape rules, returned whenever either operand holds floats.
  • array<int>: An array of integers with one item per row of the matrix, the product of the matrix and the array read as a column.
  • array<float>: An array of floats with one item per row of the matrix, the product of the matrix and the array read as a column.
  • matrix<int>: A new integer matrix holding the receiving matrix multiplied by id2.
  • matrix<float>: A new float matrix holding the receiving matrix multiplied by id2.
  • array<int>: An array of integers, the receiving matrix multiplied by id2 read as a column.
  • array<float>: An array of floats, the receiving matrix multiplied by id2 read as a column.

Example

Pine ScriptScale a matrix and multiply by a vectorOpen in editor
//@version=6
indicator("matrix.mult: product", overlay = false)
matrix<float> a = matrix.new<float>(2, 2, 1.0)
scaled = matrix.mult(a, 3.0)
v = matrix.mult(scaled, array.from(1.0, 2.0))
plot(array.get(v, 0), "First component")

See also

Runs on Sidechair