diff --git a/types/index.d.ts b/types/index.d.ts index b015c4e68e..4a15ee77f5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -18,6 +18,13 @@ export type MathCollection = MathArray | Matrix export type MathType = MathScalarType | MathCollection export type MathExpression = string | string[] | MathCollection +// add type for Matrix Callback Function and Matrix Storage Format +export type MatrixStorageFormat = 'dense' | 'sparse' + +export interface MatrixFromFunctionCallback { + (value: T): number | BigNumber +} + // eslint-disable-next-line @typescript-eslint/no-explicit-any export type FactoryFunction = (scope: any) => T @@ -698,7 +705,7 @@ export interface MathJsInstance extends MathJsFactory { * @param format The Matrix storage format * @returns The created Matrix */ - matrix(format?: 'sparse' | 'dense'): Matrix + matrix(format?: MatrixStorageFormat): Matrix /** * @param data A multi dimensional array * @param format The Matrix storage format @@ -1203,6 +1210,56 @@ export interface MathJsInstance extends MathJsFactory { hypot(...args: T[]): T hypot(args: T[]): T + /** + * Create a dense matrix from vectors as individual rows. If you pass column vectors, they will be transposed (but not conjugated!) + * @param rows - a multi-dimensional number array or matrix + */ + matrixFromRows(...rows: T[]): T[] + matrixFromRows(rows: T): T[] + + /** + * Create a dense matrix from vectors as individual columns. If you pass row vectors, they will be transposed (but not conjugated!) + * @param cols - a multi-dimensional number array or matrix + */ + matrixFromColumns( + ...cols: T[] + ): T[] | Matrix + matrixFromColumns( + cols: T + ): T[] | Matrix + /** + * Create a matrix by evaluating a generating function at each index. The simplest overload returns a multi-dimensional array as long as size is an array. Passing size as a Matrix or specifying a format will result in returning a Matrix. + * @param size - the size of the matrix to be created + * @param fn - Callback function invoked for every entry in the matrix + * @param format - The Matrix storage format, either 'dense' or 'sparse' + * @param datatype - Type of the values + */ + matrixFromFunction( + size: T, + fn: MatrixFromFunctionCallback<(number | BigNumber)[]> + ): T[] | Matrix + matrixFromFunction( + size: T, + fn: MatrixFromFunctionCallback<(number | BigNumber)[]>, + format: MatrixStorageFormat + ): T[] | Matrix + matrixFromFunction( + size: T, + fn: MatrixFromFunctionCallback<(number | BigNumber)[]>, + format: MatrixStorageFormat, + datatype: string + ): T[] | Matrix + matrixFromFunction( + size: T, + format: MatrixStorageFormat, + fn: MatrixFromFunctionCallback<(number | BigNumber)[]> + ): T[] | Matrix + matrixFromFunction( + size: T, + format: MatrixStorageFormat, + datatype: string, + fn: MatrixFromFunctionCallback<(number | BigNumber)[]> + ): T[] | Matrix /** * Calculate the least common multiple for two or more values or arrays. * lcm is defined as: lcm(a, b) = abs(a * b) / gcd(a, b) For matrices,