TradingCharts

Matrices

A matrix is a rectangular, two-dimensional, mutable collection of values of one type.

Creation

Call Result
matrix.new<T>(rows, cols) all na
matrix.new<T>(rows, cols, default) all default
matrix.copy(src) shallow copy

Element access

v = m.get(row, col)
m.set(row, col, v + 1)

Zero-indexed; out-of-range reads return na, writes raise an error.

Shape

Call Meaning
.rows() row count
.columns() column count
.row(i) copy of row i as array<T>
.col(j) copy of column j as array<T>

Reshaping

Call Meaning
.add_row(i, values?) insert row values (or all na) at index i
.add_col(j, values?) insert column at index j
.remove_row(i) / .remove_col(j) delete
.swap_rows(a, b) / .swap_cols(a, b) in-place swap
.reshape(rows, cols) in-place reshape (row-major)
.reverse() reverse row order
.transpose() returns a transposed copy

Numeric helpers

For matrix<float> / matrix<int>:

.sum()  .avg()  .max()  .min()  .mode()  .median()
.sort(order?, byRow?, byCol?)
.mult(other)         // matrix × matrix, or matrix × scalar
.inv()               // inverse (square, non-singular)
.pinv()              // Moore–Penrose pseudo-inverse
.rank()              // rank
.trace()             // sum of the main diagonal
.eigenvalues()       // array<float>

When to prefer an array of arrays

  • Rows share the same length? → matrix.
  • Rows are of different lengths, or you walk one row heavily? → array<array<float>>.

Matrices are the natural shape for small linear-algebra problems. For large data processing use arrays — they are faster for pure walks.

Size caps

A matrix may have at most 1,000 rows and 1,000 columns.

See also