Sidechair

matrix.sum()

Browse the reference

Adds two matrices item by item, or adds one number to every item.

Try it in the editor

When the second argument is a matrix, both need the same number of rows and columns, and the result holds the sums in matching positions. When it is a number, that number is added to each item.

The inputs are unchanged and a new matrix is returned. For example, [[1, 2], [3, 4]] plus [[10, 20], [30, 40]] gives [[11, 22], [33, 44]], and the first matrix plus the number 5 gives [[6, 7], [8, 9]].

Syntax

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

Arguments

NameTypeDefaultDescription
id1matrix<int> or matrix<int/float>requiredThe first matrix of the addition; id2 has to match its row and column counts when id2 is also a matrix.
id2series int/float/matrix<int> or series int/float/matrix<int/float>requiredA matrix with exactly the rows and columns of id1, or one number added to every item of id1.

Returns

  • matrix<int>: A new integer matrix of item-by-item sums, the same size as the inputs.
  • matrix<float>: A new float matrix of item-by-item sums, the same size as the inputs.
  • matrix<int>: A new integer matrix of sums between the receiving matrix and id2.
  • matrix<float>: A new float matrix of sums between the receiving matrix and id2.

Example

Pine ScriptAdd two matricesOpen in editor
//@version=6
indicator("matrix.sum: elementwise", overlay = false)
matrix<float> a = matrix.new<float>(2, 2, 1.0)
matrix<float> b = matrix.new<float>(2, 2, 2.0)
c = matrix.sum(a, b)
plot(matrix.get(c, 0, 0), "1 + 2")

See also

Runs on Sidechair