Multiplies a matrix by another matrix, by an array of numbers, or by a single number.
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
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
| Name | Type | Default | Description |
|---|---|---|---|
| id1 | matrix<int> or matrix<int/ | required | The 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. |
| id2 | series int/ | required | The 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
//@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