Views and wrappers

Views (zero-copy)

A view is an AbstractMatrix that remaps indices onto an underlying matrix without copying. Writing through a view mutates the underlying matrix. Use .clone() (or a static op) if you need an independent copy.

Created from instance methods:

const t = m.transpose(); // note: this returns a *new* Matrix, not a view
m.subMatrix(startRow, endRow, startColumn, endColumn); // new Matrix
m.subMatrixRow(rowIndices, startColumn?, endColumn?);
m.subMatrixColumn(columnIndices, startRow?, endRow?);
m.selection(rowIndices, columnIndices); // new Matrix

The view classes themselves are exported for direct construction:

import {
  MatrixTransposeView,
  MatrixSubView,
  MatrixSelectionView,
  MatrixRowView,
  MatrixColumnView,
  MatrixRowSelectionView,
  MatrixColumnSelectionView,
  MatrixFlipRowView,
  MatrixFlipColumnView,
} from "@teakit/matrix";

const tv = new MatrixTransposeView(m); // tv.set(...) writes into m
const sv = new MatrixSubView(m, 0, 1, 0, 1);
const cv = new MatrixColumnView(m, 2);

Wrappers

Wrap existing arrays as a matrix without copying.

import { wrap, WrapperMatrix1D, WrapperMatrix2D } from "@teakit/matrix";

wrap([[1, 2], [3, 4]]); // -> WrapperMatrix2D
wrap([1, 2, 3, 4], { rows: 2 }); // -> WrapperMatrix1D (2×2)

new WrapperMatrix2D(twoDArray);
new WrapperMatrix1D(oneDArray, { rows: 1 });

wrap throws if the argument is not an array. WrapperMatrix1D throws if the data length is not divisible by rows. Mutating a wrapper writes back into the wrapped array.