@d4c/numjs
Version:
Like NumPy, in TypeScript and JavaScript
272 lines (271 loc) • 9.57 kB
TypeScript
/// <reference types="node" />
import { NdArray as BaseNdArray } from "ndarray";
import util from "util";
export interface ArbitraryDimArray<T> extends Array<T | ArbitraryDimArray<T>> {
}
export declare type ArbDimNumArray = ArbitraryDimArray<number>;
export declare type ArrayLikeConstructor = ArrayConstructor | Int8ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | Uint8ClampedArrayConstructor;
export declare type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
export declare type OneDimNumArray = Array<number> | TypedArray;
export declare type DType<D = OneDimNumArray> = D extends Int8Array ? "int8" : D extends Int16Array ? "int16" : D extends Int32Array ? "int32" : D extends Uint8Array ? "uint8" : D extends Uint8ClampedArray ? "uint8_clamped" : D extends Uint16Array ? "uint16" : D extends Uint32Array ? "uint32" : D extends Float32Array ? "float32" : D extends Float64Array ? "float64" : "array";
/**
* Multidimensional, homogeneous array of fixed-size items
*
* The number of dimensions and items in an array is defined by its shape, which is a tuple of N positive
* integers that specify the sizes of each dimension. The type of items in the array is specified by a separate
* data-type object (dtype), one of which is associated with each NdArray.
*/
export declare class NdArray {
selection: BaseNdArray;
constructor(data: BaseNdArray);
constructor(data: OneDimNumArray, shape: number[], stride?: number[], offset?: number);
/**
* @property NdArray#size - Number of elements in the array.
*/
get size(): number;
/**
* The shape of the array
*
* @name NdArray#shape
* @readonly
*/
get shape(): number[];
/**
* Number of array dimensions.
*
* @name NdArray#ndim
* @readonly
*/
get ndim(): number;
/**
* Data-type of the array’s elements.
*/
get dtype(): "int8" | "int16" | "int32" | "uint8" | "uint8_clamped" | "uint16" | "uint32" | "float32" | "float64" | "generic" | "array";
set dtype(dtype: "int8" | "int16" | "int32" | "uint8" | "uint8_clamped" | "uint16" | "uint32" | "float32" | "float64" | "generic" | "array");
/**
* Permute the dimensions of the array.
*
* @name NdArray#T
* @readonly
*/
get T(): NdArray;
get(...args: number[]): number;
set(...args: number[]): number;
slice(...args: Array<number | number[]>): NdArray;
/**
* Return a subarray by fixing a particular axis
* @param axis a array whose element could be `null` or `number`
*
* @example
* ```typescript
* arr = nj.arange(4*4).reshape(4,4)
* // array([[ 0, 1, 2, 3],
* // [ 4, 5, 6, 7],
* // [ 8, 9, 10, 11],
* // [ 12, 13, 14, 15]])
*
* arr.pick(1)
* // array([ 4, 5, 6, 7])
*
* arr.pick(null, 1)
* // array([ 1, 5, 9, 13])
* ```
**/
pick(...axis: number[]): NdArray;
/**
* Return a shifted view of the array. Think of it as taking the upper left corner of the image and dragging it inward
*
* @example
* ```typescript
* arr = nj.arange(4*4).reshape(4,4)
* // array([[ 0, 1, 2, 3],
* // [ 4, 5, 6, 7],
* // [ 8, 9, 10, 11],
* // [ 12, 13, 14, 15]])
* arr.lo(1,1)
* // array([[ 5, 6, 7],
* // [ 9, 10, 11],
* // [ 13, 14, 15]])
* ```
**/
lo(...args: number[]): NdArray;
/**
* Return a sliced view of the array.
*
* @example
* ```typescript
* arr = nj.arange(4*4).reshape(4,4)
* // array([[ 0, 1, 2, 3],
* // [ 4, 5, 6, 7],
* // [ 8, 9, 10, 11],
* // [ 12, 13, 14, 15]])
*
* arr.hi(3,3)
* // array([[ 0, 1, 2],
* // [ 4, 5, 6],
* // [ 8, 9, 10]])
*
* arr.lo(1,1).hi(2,2)
* // array([[ 5, 6],
* // [ 9, 10]])
* ```
*/
hi(...args: number[]): NdArray;
step(...args: number[]): NdArray;
/**
* Return a copy of the array collapsed into one dimension using row-major order (C-style)
*/
flatten(): NdArray;
/**
* Gives a new shape to the array without changing its data.
* @param shape - The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
* @returns a new view object if possible, a copy otherwise,
*/
reshape(...shape: number[]): NdArray;
reshape(shape: number[]): NdArray;
/**
* Permute the dimensions of the array.
* @example
* ```typescript
* arr = nj.arange(6).reshape(1,2,3)
* // array([[[ 0, 1, 2],
* // [ 3, 4, 5]]])
*
* arr.T
* // array([[[ 0],
* // [ 3]],
* // [[ 1],
* // [ 4]],
* // [[ 2],
* // [ 5]]])
*
* arr.transpose(1,0,2)
* // array([[[ 0, 1, 2]],
* // [[ 3, 4, 5]]])
* ```
*/
transpose(...axes: number[]): NdArray;
transpose(axes?: number[]): NdArray;
/**
* Dot product of two arrays.
*/
dot(x: ArbDimNumArray | NdArray): NdArray;
/**
* Assign `x` to the array, element-wise.
*/
assign(x: NdArray | ArbDimNumArray | number, copy?: boolean): NdArray;
/**
* Add `x` to the array, element-wise.
*/
add(x: NdArray | ArbDimNumArray | number, copy?: boolean): NdArray;
/**
* Subtract `x` to the array, element-wise.
*/
subtract(x: NdArray | ArbDimNumArray | number, copy?: boolean): NdArray;
/**
* Multiply array by `x`, element-wise.
*/
multiply(x: NdArray | ArbDimNumArray | number, copy?: boolean): NdArray;
/**
* Divide array by `x`, element-wise.
*/
divide(x: NdArray | ArbDimNumArray | number, copy?: boolean): NdArray;
/**
* Raise array elements to powers from given array, element-wise.
*
* @param x
* @param copy - set to false to modify the array rather than create a new one
*/
pow(x: NdArray | ArbDimNumArray | number, copy?: boolean): NdArray;
/**
* Calculate the exponential of all elements in the array, element-wise.
*
* @param copy - set to false to modify the array rather than create a new one
*/
exp(copy?: boolean): NdArray;
/**
* Calculate the natural logarithm of all elements in the array, element-wise.
*
* @param copy - set to false to modify the array rather than create a new one
*/
log(copy?: boolean): NdArray;
/**
* Calculate the positive square-root of all elements in the array, element-wise.
*
* @param copy set to false to modify the array rather than create a new one
*/
sqrt(copy?: boolean): NdArray;
/**
* Return the maximum value of the array
*/
max(): number;
/**
* Return the minimum value of the array
*/
min(): number;
/**
* Sum of array elements.
*/
sum(): number;
/**
* Returns the standard deviation, a measure of the spread of a distribution, of the array elements.
*
* @param {object} options default {ddof:0}
*/
std(options?: {
ddof: number;
}): number;
/**
* Return the arithmetic mean of array elements.
*/
mean(): number;
/**
* Return element-wise remainder of division.
*/
mod(x: NdArray | ArbDimNumArray | number, copy?: boolean): NdArray;
/**
* Converts {NdArray} to a native JavaScript {Array}
*/
tolist(): ArbDimNumArray;
valueOf(): ArbDimNumArray;
/**
* Stringify the array to make it readable in the console, by a human.
*/
[util.inspect.custom](): string;
/**
* Stringify the array to make it readable by a human.
*/
toString(): string;
/**
* Stringify object to JSON
*/
toJSON(): string;
/**
* Create a full copy of the array
*/
clone(): NdArray;
/**
* Return true if two arrays have the same shape and elements, false otherwise.
*/
equal(array: ArbDimNumArray | NdArray): boolean;
/**
* Round array to the to the nearest integer.
*/
round(copy?: boolean): NdArray;
/**
* Return the inverse of the array, element-wise.
*/
negative(): NdArray;
diag(): NdArray;
iteraxis(axis: number, cb: (xi: NdArray, i: number) => void): void;
/**
* Returns the discrete, linear convolution of the array using the given filter.
*
* @note: Arrays must have the same dimensions and `filter` must be smaller than the array.
* @note: The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect. This behaviour is known as the 'valid' mode.
* @note: Use optimized code for 3x3, 3x3x1, 5x5, 5x5x1 filters, FFT otherwise.
*/
convolve(filter: ArbDimNumArray | NdArray): NdArray;
fftconvolve(filter: ArbDimNumArray | NdArray): NdArray;
static new(arr: NdArray | ArbDimNumArray | number | TypedArray, dtype?: DType | ArrayLikeConstructor): NdArray;
}