Cyforanumera
npm

API reference

Triangle and diagonal indices

NameSummary
np.trilIndicesRow and column indices of the lower triangle of an n × m array.
np.triuIndicesRow and column indices of the upper triangle of an n × m array.
np.trilIndicesFromtrilIndices for the shape of a 2-D array.
np.triuIndicesFromtriuIndices for the shape of a 2-D array.
np.diagIndicesIndices of the main diagonal of an ndim-dimensional array with all sides n.
np.diagIndicesFromdiagIndices for an array whose sides are all equal (at least 2-D).
np.maskIndicesIndices where maskFunc(np.ones([n, n]), k) is nonzero, e.g.
np.fillDiagonalFills the main diagonal of a in place (values repeat cyclically).

np.trilIndices

#
np.trilIndices(n, [k], [m])

Row and column indices of the lower triangle of an n × m array.

Parameters

nnumber
Rows.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.
[m]number | null
Columns. Default n.

Returns

[NDArray, NDArray]

Example

TypeScript
np.trilIndices(3).map((a) => a.toArray()); // => [[0, 1, 1, 2, 2, 2], [0, 0, 1, 0, 1, 2]]
TypeScript declaration
np.trilIndices(n: number, k?: number | undefined, m?: number | null | undefined): NDArray[]

np.triuIndices

#
np.triuIndices(n, [k], [m])

Row and column indices of the upper triangle of an n × m array.

Parameters

nnumber
Rows.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.
[m]number | null
Columns. Default n.

Returns

[NDArray, NDArray]

Example

TypeScript
np.triuIndices(3, 1).map((a) => a.toArray()); // => [[0, 0, 1], [1, 2, 2]]
TypeScript declaration
np.triuIndices(n: number, k?: number | undefined, m?: number | null | undefined): NDArray[]

np.trilIndicesFrom

#
np.trilIndicesFrom(arr, [k])

trilIndices for the shape of a 2-D array.

Parameters

arrNDArray
2-D array.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.

Returns

[NDArray, NDArray]

Example

TypeScript
np.trilIndicesFrom(np.zeros([2, 3])).map((a) => a.toArray()); // => [[0, 1, 1], [0, 0, 1]]
TypeScript declaration
np.trilIndicesFrom(arr: NDArray, k?: number | undefined): NDArray[]

np.triuIndicesFrom

#
np.triuIndicesFrom(arr, [k])

triuIndices for the shape of a 2-D array.

Parameters

arrNDArray
2-D array.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.

Returns

[NDArray, NDArray]

Example

TypeScript
np.triuIndicesFrom(np.zeros([2, 3])).map((a) => a.toArray()); // => [[0, 0, 0, 1, 1], [0, 1, 2, 1, 2]]
TypeScript declaration
np.triuIndicesFrom(arr: NDArray, k?: number | undefined): NDArray[]

np.diagIndices

#
np.diagIndices(n, [ndim])

Indices of the main diagonal of an ndim-dimensional array with all sides n.

Parameters

nnumber
Side length.
[ndim]number
Dimensions. Default 2.

Returns

NDArray[]

Example

TypeScript
np.diagIndices(3).map((a) => a.toArray()); // => [[0, 1, 2], [0, 1, 2]]
TypeScript declaration
np.diagIndices(n: number, ndim?: number | undefined): NDArray[]

np.diagIndicesFrom

#
np.diagIndicesFrom(arr)

diagIndices for an array whose sides are all equal (at least 2-D).

Parameters

arrNDArray
Input array.

Returns

NDArray[]

Example

TypeScript
np.diagIndicesFrom(np.zeros([2, 2, 2])).map((a) => a.toArray()); // => [[0, 1], [0, 1], [0, 1]]
TypeScript declaration
np.diagIndicesFrom(arr: NDArray): NDArray[]

np.maskIndices

#
np.maskIndices(n, maskFunc, [k])

Indices where maskFunc(np.ones([n, n]), k) is nonzero, e.g. np.triu.

Parameters

nnumber
Side length.
maskFunc(m: NDArray, k: number) => NDArray
Mask function such as np.tril.
[k]number
Diagonal offset: 0 main, > 0 above, < 0 below. Default 0.

Returns

NDArray[]

Example

TypeScript
np.maskIndices(3, np.triu, 1).map((a) => a.toArray()); // => [[0, 0, 1], [1, 2, 2]]
TypeScript declaration
np.maskIndices(n: number, maskFunc: (m: NDArray, k: number) => NDArray, k?: number | undefined): NDArray[]

np.fillDiagonal

#
np.fillDiagonal(a, val, [options])

Fills the main diagonal of a in place (values repeat cyclically). For tall 2-D arrays, wrap: true restarts the diagonal after every ncols + 1 rows, like NumPy.

Parameters

aNDArray
Writeable array, at least 2-D (all sides equal when > 2-D).
valArrayLike
Scalar or values to write.
[options.wrap]boolean
Default false.

Returns

void

Example

TypeScript
const a = np.zeros([3, 3]);
np.fillDiagonal(a, [1, 2]);
a; // => [[1, 0, 0], [0, 2, 0], [0, 0, 1]]
const b = np.zeros([5, 2]);
np.fillDiagonal(b, 7, { wrap: true });
b; // => [[7, 0], [0, 7], [0, 0], [7, 0], [0, 7]]
TypeScript declaration
np.fillDiagonal(a: NDArray, val: ArrayInput, options?: { wrap?: boolean | undefined; } | undefined): void