ndarray-ts
Version:
A basic N-dimensional array library in TypeScript, similar to NumPy.
125 lines (124 loc) • 5.15 kB
TypeScript
/**
* @file NdArray.ts
* @description The core NdArray class for N-dimensional array operations.
*/
/**
* Represents an N-dimensional array, providing functionalities similar to NumPy's ndarray.
*/
export declare class NdArray {
data: number[];
shape: number[];
strides: number[];
size: number;
ndim: number;
/**
* Creates an instance of NdArray.
* @param data The flat array of numbers.
* @param shape The shape (dimensions) of the array.
* @throws {Error} If data length does not match the product of shape dimensions.
*/
constructor(data: number[], shape: number[]);
/**
* Calculates the strides for each dimension.
* Strides indicate how many elements to skip in the flat data array to move to the next element along a dimension.
* @param shape The shape of the array.
* @returns An array of strides.
*/
private calculateStrides;
/**
* Calculates the flat index in the data array from multi-dimensional indices.
* @param indices An array of indices for each dimension.
* @returns The flat index.
* @throws {Error} If the number of indices does not match the number of dimensions,
* or if any index is out of bounds.
*/
private getFlatIndex;
/**
* Creates a new NdArray filled with zeros.
* @param shape The shape of the new array.
* @returns A new NdArray instance.
*/
static zeros(shape: number[]): NdArray;
/**
* Creates a new NdArray filled with ones.
* @param shape The shape of the new array.
* @returns A new NdArray instance.
*/
static ones(shape: number[]): NdArray;
/**
* Creates a new NdArray with evenly spaced values within a given interval.
* Similar to NumPy's `arange`.
* @param start The start of the interval (inclusive).
* @param stop The end of the interval (exclusive).
* @param step The spacing between values (default: 1).
* @returns A new NdArray instance.
*/
static arange(start: number, stop: number, step?: number): NdArray;
/**
* Returns the element at the specified multi-dimensional indices.
* @param indices An array of indices for each dimension.
* @returns The value at the given indices.
*/
get(...indices: number[]): number;
/**
* Sets the element at the specified multi-dimensional indices to a new value.
* @param indices An array of indices for each dimension.
* @param value The new value to set.
*/
set(indices: number[], value: number): void;
/**
* Reshapes the array to a new shape. The total number of elements must remain the same.
* @param newShape The new shape (dimensions) for the array.
* @returns A new NdArray instance with the reshaped view of the same data.
* @throws {Error} If the new shape does not result in the same total number of elements.
*/
reshape(newShape: number[]): NdArray;
/**
* Performs element-wise addition with another NdArray or a scalar.
* @param other The other NdArray or a number to add.
* @returns A new NdArray with the result.
* @throws {Error} If shapes are not compatible for element-wise operation.
*/
add(other: NdArray | number): NdArray;
/**
* Performs element-wise subtraction with another NdArray or a scalar.
* @param other The other NdArray or a number to subtract.
* @returns A new NdArray with the result.
* @throws {Error} If shapes are not compatible for element-wise operation.
*/
subtract(other: NdArray | number): NdArray;
/**
* Performs element-wise multiplication with another NdArray or a scalar.
* @param other The other NdArray or a number to multiply.
* @returns A new NdArray with the result.
* @throws {Error} If shapes are not compatible for element-wise operation.
*/
multiply(other: NdArray | number): NdArray;
/**
* Performs element-wise division with another NdArray or a scalar.
* @param other The other NdArray or a number to divide by.
* @returns A new NdArray with the result.
* @throws {Error} If shapes are not compatible for element-wise operation, or division by zero occurs.
*/
divide(other: NdArray | number): NdArray;
/**
* Performs matrix multiplication (dot product) for 2D arrays.
* @param other The other NdArray to multiply with.
* @returns A new NdArray with the result of the dot product.
* @throws {Error} If arrays are not 2D or shapes are incompatible for dot product.
*/
dot(other: NdArray): NdArray;
/**
* Checks if the shape of another NdArray is compatible for element-wise operations.
* For simplicity, this implementation only checks for exact shape match.
* A full NumPy-like broadcasting mechanism would be more complex.
* @param other The other NdArray.
* @returns True if shapes are compatible, false otherwise.
*/
private isShapeCompatible;
/**
* Returns a string representation of the NdArray.
* @returns A string representing the array.
*/
toString(): string;
}