Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definition missing for matrixFrom* #3115 #3146

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 58 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends (number | BigNumber)[]> {
(value: T): number | BigNumber
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type FactoryFunction<T> = (scope: any) => T

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1203,6 +1210,56 @@ export interface MathJsInstance extends MathJsFactory {
hypot<T extends number | BigNumber>(...args: T[]): T
hypot<T extends number | BigNumber>(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<T extends (number | BigNumber)[] | Matrix>(...rows: T[]): T[]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use MathCollection here? Looking at the examples of these functions, they also support nested arrays for example, not only a flat array.

matrixFromRows<T extends (number | BigNumber)[] | Matrix>(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<T extends (number | BigNumber)[] | Matrix>(
...cols: T[]
): T[] | Matrix
matrixFromColumns<T extends (number | BigNumber)[] | Matrix>(
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<T extends (number | BigNumber)[] | Matrix>(
size: T,
fn: MatrixFromFunctionCallback<(number | BigNumber)[]>
): T[] | Matrix
matrixFromFunction<T extends (number | BigNumber)[] | Matrix>(
size: T,
fn: MatrixFromFunctionCallback<(number | BigNumber)[]>,
format: MatrixStorageFormat
): T[] | Matrix
matrixFromFunction<T extends (number | BigNumber)[] | Matrix>(
size: T,
fn: MatrixFromFunctionCallback<(number | BigNumber)[]>,
format: MatrixStorageFormat,
datatype: string
): T[] | Matrix
matrixFromFunction<T extends (number | BigNumber)[] | Matrix>(
size: T,
format: MatrixStorageFormat,
fn: MatrixFromFunctionCallback<(number | BigNumber)[]>
): T[] | Matrix
matrixFromFunction<T extends (number | BigNumber)[] | Matrix>(
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,
Expand Down