Skip to content

Matrix

Representation of a rectangle of (maybe boxed) cell values. For the purposes of compact representation of a common redundancy structure, the rectangle should be thought of as consisting of four quadrants whose contents are specified as follows:

  • the top-left quadrant is arbitrary data, specified with a dense array of dense arrays of MaybeBoxed<ArrayValue>, each array representing one row of that quadrant, all arrays having the same length.
  • the bottom-right quadrant has the same value in all its elements
  • the top-right quadrant either has the same value as the bottom-right quadrant in all its elements (if _defaultColumn is empty), or else consists of constant-value rows, i.e. each row has a single value repeating in all its elements (but this value can differ between rows)
  • the bottom-left quadrant likewise either has the same value as the bottom-right quadrant in all its elements (if _defaultRow is empty), or else consists of constant-value columns, i.e. each column has a single value repeating in all its elements (but this value can differ between columns)

The populatedHeight and populatedWidth properties specify the dimensions of the top left quadrant, and width and height then specify the remaining quadrants, so if populatedHeight === height, then the bottom quadrants are empty, and if populatedWidth === width, then the rightmost quadrants are empty.

new Matrix(width, height, defaultValue): Matrix

Construct a new Matrix with a given width and height and default value, and populatedHeight and populatedWidth of 0. The default value applies at any coordinates to the right of, and/or downwards of, the largest X and Y coordinates for which a value has been set, with .set or .setData. Within the range (0,0) to (populatedWidth, populatedHeight), any coordinates at which a value has not been set is blank (the value is null).

number = 0

number = 0

MaybeBoxed<ArrayValue> = null

Matrix

_data: MaybeBoxed<ArrayValue>[][]

Values of the elements in the top-left quadrant. Invariant: all rows are the same length.


_defaultColumn: MaybeBoxed<ArrayValue>[]

Values that apply in the rows of the top-right quadrant. This is either:

  • the same length as _data, in which case each value in this array specifies the value of every element along the corresponding row of the top-right quadrant
  • or empty, in which case _defaultValue specifies the value of every element in all rows of the top-right quadrant.

_defaultRow: MaybeBoxed<ArrayValue>[]

Values that apply in the columns of the bottom-left quadrant. This is either:

  • the same length as each row of _data, in which case each value in this array specifies the value of every element along the corresponding column of the bottom-left quadrant
  • or empty, in which case _defaultValue specifies the value of every element in all columns of the bottom-left quadrant.

_defaultValue: MaybeBoxed<ArrayValue>

The value of:

  • every element of the bottom-right quadrant
  • every element of the top-right quadrant, if _defaultColumn is empty
  • every element of the bottom-left quadrant, if _defaultRow is empty

get height(): number

number

set height(newHeight): void

number

void


get isPartiallyPopulated(): boolean

Indicates whether or not this matrix is partially populated

boolean


get populatedHeight(): number

number


get populatedWidth(): number

number


get size(): number

number


get valid(): boolean

boolean


get width(): number

number

set width(newWidth): void

number

void

_ensureSizeAttributesEncompass(x, y): void

number

number

void


_get(x, y, strict, leaveBoxed): MaybeBoxed<ArrayValue>

number

number

boolean = false

boolean = false

MaybeBoxed<ArrayValue>


_resolveArea<O>(options): AreaArray<AreaArrayElement<O>>

O extends Readonly<{ returnBoxed: boolean; returnCells: boolean; returnLambda: boolean; }>

ResolveAreaOptions<O>

AreaArray<AreaArrayElement<O>>


_resolveRange<Boxed>(opts?): Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>[]

Boxed extends boolean = false

IterationOptions<Boxed>

Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>[]


applyTrim(trim): FormulaError | Matrix

number

FormulaError | Matrix


clone(): Matrix

Matrix


collapseToCell(r, c): FormulaError | Matrix

number = 0

number = 0

FormulaError | Matrix


collapseToColumn(c): FormulaError | Matrix

number = 0

FormulaError | Matrix


collapseToNthCell(n): FormulaError | Matrix

Return a matrix of the zero-based nth element in a left-to-right-then-top-down traversal of this matrix. Note that bounds checking is not performed; this can yield a cell outside this range. The returned instance is guaranteed to be new, even if it is identical to this.

number = 0

zero-based index of the value to return

FormulaError | Matrix

matrix containing the single value specified, or error if n out of bounds


collapseToRow(r): FormulaError | Matrix

number = 0

FormulaError | Matrix


every(predicate): boolean

(value) => boolean

boolean


expand(paddingValue, toRows, toCols): Matrix

Expand matrix to the given dimensions, using fillWith as a padding value. If either dimension of the matrix is already the given size or larger, it is not changed.

MaybeBoxed<ArrayValue>

Value to use for padding the expanded matrix

number

Desired number of rows in the expanded matrix.

number

Desired number of columns in the expanded matrix.

Matrix


get(x, y, strict): ArrayValue

number

number

boolean = false

ArrayValue


getBoxed(x, y): MaybeBoxed<ArrayValue>

number

number

MaybeBoxed<ArrayValue>


getByIndex(i): ArrayValue

Get the i’th element of this Matrix in by-rows iteration order.

number

ArrayValue


getColumnBoxed(c, includeDefaulted): MaybeBoxed<ArrayValue>[]

Get the (possibly boxed) values of column c as a simple (not area) array.

number

boolean = true

MaybeBoxed<ArrayValue>[]


getRowBoxed(r, includeDefaulted): MaybeBoxed<ArrayValue>[]

Get the (possibly boxed) values of row r as a simple (not area) array.

number

boolean = true

MaybeBoxed<ArrayValue>[]


hasDefaultValueFulfilling(predicate): boolean

Does this Matrix have a default value which fulfills the given predicate?

(value) => boolean

boolean


hstack(other): Matrix

Stack matrices in a sequence horizontally (column wise). See e.g. https://numpy.org/doc/stable/reference/generated/numpy.hstack.html

Invariant: this.height === other.height

Matrix

Other matrix. Must have the same height as this.

Matrix

Horizontally stacked matrix


is1D(): boolean

boolean


iterAll<Boxed>(opts?): IterableIterator<{ value: Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>; x: number; y: number; }>

Yield all values in rows-first order, including default values for unpopulated areas (unless they are blank and skipBlanks is not 'none').

Boxed extends boolean = false

IterationOptions<Boxed>

IterableIterator<{ value: Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>; x: number; y: number; }>


iterAllColumnWise(leaveBoxed): IterableIterator<{ value: MaybeBoxed<ArrayValue>; x: number; y: number; }>

boolean = false

IterableIterator<{ value: MaybeBoxed<ArrayValue>; x: number; y: number; }>


iterAllWithCount(): IterableIterator<{ count: number; value: ArrayValue; }>

Yield all values, including default values for unpopulated areas (but only once, with a count for how many times they are repeated), in unspecified order. The same value may be yielded multiple times, so the count each time is not necessarily the total number of occurrences of that value in the matrix; this is just run-length encoding to cut down on repetition.

IterableIterator<{ count: number; value: ArrayValue; }>


iterColumnsBoxed(): IterableIterator<{ column: MaybeBoxed<ArrayValue>[]; x: number; }>

IterableIterator<{ column: MaybeBoxed<ArrayValue>[]; x: number; }>


iterPopulated<Boxed>(opts?): IterableIterator<{ value: Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>; x: number; y: number; }>

Boxed extends boolean = false

IterationOptions<Boxed>

IterableIterator<{ value: Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>; x: number; y: number; }>


iterRowsBoxed(): IterableIterator<{ row: MaybeBoxed<ArrayValue>[]; y: number; }>

IterableIterator<{ row: MaybeBoxed<ArrayValue>[]; y: number; }>


map(fn): Matrix

(element, coords) => MaybeBoxed<ArrayValue>

Matrix


permuteColumns(permutation): Matrix

When providing the permutations for a matrix where width is greater than populatedWidth, the length of permutation should be populatedWidth + 1. Given a Matrix like so:

Matrix { populatedWidth: 3, width: 1000 }

permutation should should be of length 4, where

  • indices 0, 1, 2 represent the indices of the populated rows, and
  • index 3 represents all of the indices in the default quadrant.

Providing permutation = [ 0, 2, 1, 3 ] would create a matrix of width 1000 with a populatedWidth of 3.

However, providing permutation = [ 0, 3, 1, 2 ] would create a matrix of width 1000 where the populatedWidth is 1000 as well. permutation = [0, 3, 1, 2] is equivalent to:

permutation = [ 0, 3, 3, 3, (…994 more 3’s), 1, 2 ]

number[]

Matrix


permuteRows(permutation): Matrix

When providing the permutations for a matrix where height is greater than populatedHeight, the length of permutation should be populatedHeight + 1. Given a Matrix like so:

Matrix { populatedHeight: 3, height: 1000 }

permutation should should be of length 4, where

  • indices 0, 1, 2 represent the indices of the populated rows, and
  • index 3 represents all of the indices in the default quadrant.

Providing permutation = [ 0, 2, 1, 3 ] would create a matrix of height 1000 with a populatedHeight of 3.

However, providing permutation = [ 0, 3, 1, 2 ] would create a matrix of height 1000 where the populatedHeight is 1000 as well. permutation = [0, 3, 1, 2] is equivalent to:

permutation = [ 0, 3, 3, 3, (…994 more 3’s), 1, 2 ]

number[]

Matrix


resolveAreaBoxed(): AreaBoxedValueArray

AreaBoxedValueArray


resolveAreaCells(cropTo?): AreaCellArray

"any-cell-information" | "cells-with-non-blank-values"

AreaCellArray


resolveAreaValues(): AreaValueArray

AreaValueArray


resolveCell(): CellItem

CellItem


resolveRange<Boxed>(opts?): Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>[]

Boxed extends boolean = false

IterationOptions<Boxed>

Boxed extends false ? ArrayValue : MaybeBoxed<ArrayValue>[]


resolveSingle(): ArrayValue

ArrayValue


resolveSingleBoxed(): MaybeBoxed<ArrayValue>

MaybeBoxed<ArrayValue>


set(x, y, value): void

number

number

MaybeBoxed<ArrayValue>

void


setData(data): Matrix

Populate this matrix with the given data array. The rows in data will be referenced, not copied, so data should be considered to belong to this matrix henceforth, i.e. not mutated elsewhere.

MaybeBoxed<ArrayValue>[][] | AreaBoxedValueArray | AreaValueArray

Matrix


shrink(): void

Shrink the _data property (populated top-left quadrant) if possible, by pruning away bottommost rows and rightmost columns in which all elements are exactly equal to _defaultValue. If the matrix starts out fully populated (i.e. has only a top-left quadrant, no defaulted quadrants), then first check whether the bottom row (if tall) or rightmost column (if wide) is all the same value, and if so, set _defaultValue to that.

void


take(startRow, startCol, numRows, numCols): FormulaError | Matrix

number

number

number

number

FormulaError | Matrix


toMatrix(): Matrix

Matrix


toString(): string

string


trimBottom(): void

void


uniqueValues(): Set<ArrayValue>

Set<ArrayValue>


visitAllWithCount(visitor, minVisitWidth?, minVisitHeight?): boolean

Visit all cells with count-based run-length encoding. Unlike iterAllWithCount (which yields a flat iterator of { value, count } without coordinates), this method calls the visitor with x,y coordinates for each visited region.

Optional minVisitWidth and minVisitHeight parameters extend the individual-visit region beyond this matrix’s populated dimensions. This is useful when other matrices (e.g., criteria matrices) have larger populated regions - we need to visit each coordinate individually where ANY matrix has non-default values.

The visitor receives (x, y, value, count, regionWidth, regionHeight):

  • x, y: top-left coordinates of the region
  • value: the value at (x, y), which applies to all cells in the region
  • count: total number of cells in the region (= regionWidth * regionHeight)
  • regionWidth, regionHeight: dimensions of the region (useful for dependency tracking)

The visitor may return true to stop iteration early.

Calls visitor for:

  1. Each cell in the individual-visit region (count: 1, regionWidth: 1, regionHeight: 1)
  2. Each row’s remaining column region (count: width - visitWidth, regionHeight: 1)
  3. Each column’s remaining row region (count: height - visitHeight, regionWidth: 1)
  4. The remaining corner region (count: remainingWidth * remainingHeight)

(x, y, value, count, regionWidth, regionHeight) => boolean | void

number

number

boolean

true if visitor returned true (early termination), false otherwise


vstack(other): Matrix

Stack matrices in a sequence vertically (row wise). See e.g. https://numpy.org/doc/stable/reference/generated/numpy.vstack.html

Invariant: this.width === other.width

Matrix

Other matrix. Must have the same width as this.

Matrix

Vertically stacked matrix


static createColumn(columnValues): Matrix

MaybeBoxed<ArrayValue>[]

Matrix


static createRow(rowValues): Matrix

MaybeBoxed<ArrayValue>[]

Matrix


static new<T>(data): Matrix

T extends MatrixNewData

T & RequireWidthForDefaultColumn<T> & RequireHeightForDefaultRow<T> & RequireDimensionForDefaultValue<T>

Matrix


static of(value): Matrix

MaybeBoxed<ArrayValue> | (undefined | null | MaybeBoxed<ArrayValue> | undefined[])[]

Matrix


static ofTransposed(value): Matrix

Matrix | MaybeBoxed<ArrayValue> | MaybeBoxed<ArrayValue>[][]

Matrix