Skip to content

Commit a5f64bf

Browse files
authored
Merge branch 'develop' into array-flatten-infinity
2 parents 5534e3c + b56fbcb commit a5f64bf

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
lines changed

src/function/matrix/matrixFromFunction.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ const dependencies = ['typed', 'matrix', 'isZero']
66
export const createMatrixFromFunction = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, isZero }) => {
77
/**
88
* Create a matrix by evaluating a generating function at each index.
9-
* The simplest overload returns a multi-dimensional array as long as `size` is an array.
10-
* Passing `size` as a Matrix or specifying a `format` will result in returning a Matrix.
9+
* The simplest overload returns a multi-dimensional array as long as `size`
10+
* is an array.
11+
* Passing `size` as a Matrix or specifying a `format` will result in
12+
* returning a Matrix.
1113
*
1214
* Syntax:
1315
*
@@ -17,6 +19,37 @@ export const createMatrixFromFunction = /* #__PURE__ */ factory(name, dependenci
1719
* math.matrixFromFunction(size, format, fn)
1820
* math.matrixFromFunction(size, format, datatype, fn)
1921
*
22+
* Where:
23+
*
24+
* - `size: (number[] | Matrix)`
25+
* A vector giving the extent of the array to be created in each
26+
* dimension. If size has one entry, a vector is created; if it
27+
* has two, a rectangular array/Matrix is created; if three, a
28+
* three-dimensional array/Matrix is created; and so on.
29+
* - `fn: (index: number[]) => MathType`
30+
* The callback function that will generate the entries of the
31+
* matrix. It is called in turn with the index of each entry of
32+
* the matrix. The index is always an ordinary array of numbers
33+
* with the same length as _size_. So for vectors, you will get
34+
* indices like `[0]` or `[1]`, whereas for matrices, you will
35+
* get indices like `[2, 0]` or `[1,3]`. The return value may
36+
* be any type that can go in an array or Matrix entry, although
37+
* if you supply the _datatype_ argument, you must yourself ensure
38+
* the type of the return value matches. Note that currently,
39+
* your callback _fn_ will receive 0-based indices for the matrix
40+
* entries, regardless of whether matrixFromFunction is invoked
41+
* directly from JavaScript or via the mathjs expression language.
42+
* - `format: 'dense'|'sparse'`
43+
* Specifies the storage format for the resulting Matrix. Note that
44+
* if this argument is given, the return value will always be a
45+
* Matrix (rather than possibly an Array).
46+
* - `datatype: string`
47+
* Specifies the data type of entries of the new matrix. If given,
48+
* it should be the name of a data type that mathjs supports, as
49+
* returned by the math.typeOf function. It is up to the caller
50+
* to make certain that all values returned by _fn_ are consistent
51+
* with this datatype if specified.
52+
*
2053
* Examples:
2154
*
2255
* math.matrixFromFunction([3,3], i => i[0] - i[1]) // an antisymmetric matrix
@@ -25,7 +58,7 @@ export const createMatrixFromFunction = /* #__PURE__ */ factory(name, dependenci
2558
*
2659
* See also:
2760
*
28-
* matrix, zeros
61+
* matrix, typeOf, zeros
2962
*
3063
* @param {Array | Matrix} size The size of the matrix to be created
3164
* @param {function} fn Callback function invoked for every entry in the matrix

test/typescript-tests/testTypes.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,28 @@ Matrices examples
12681268
const _c = math.multiply(a, b)
12691269
const f: Matrix = math.matrix([1, 0])
12701270
const _d: Matrix = f.subset(math.index(1))
1271+
const g: number[] = math.matrixFromFunction(
1272+
[3],
1273+
(i: number[]) => i[0] * i[0]
1274+
)
1275+
assert.strictEqual(g[2], 4)
1276+
const h: Matrix = math.matrixFromFunction(
1277+
[2, 2],
1278+
(i: number[]) => math.fraction(i[0], i[1] + 1),
1279+
'dense'
1280+
)
1281+
const j: number[][] = math.matrixFromRows(
1282+
[1, 2, 3],
1283+
math.matrix([[4], [5], [6]])
1284+
)
1285+
assert.strictEqual(j[1][2], 6)
1286+
const _k: Matrix = math.matrixFromRows(f, math.row(h, 1))
1287+
const l: number[][] = math.matrixFromColumns(
1288+
[1, 2, 3],
1289+
math.matrix([[4], [5], [6]])
1290+
)
1291+
assert.strictEqual(l[2][1], 6)
1292+
const _m: Matrix = math.matrixFromColumns(f, math.row(h, 1))
12711293
}
12721294

12731295
// get a sub matrix

types/index.d.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export type MathCollection<T = MathGeneric> = MathArray<T> | Matrix<T>
2121
export type MathType = MathScalarType | MathCollection
2222
export type MathExpression = string | string[] | MathCollection
2323

24+
// add type for Matrix Callback Function and Matrix Storage Format
25+
export type MatrixStorageFormat = 'dense' | 'sparse'
26+
export type MatrixFromFunctionCallback<T extends MathScalarType> = (
27+
index: number[]
28+
) => T
29+
2430
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2531
export type FactoryFunction<T> = (scope: any) => T
2632

@@ -743,7 +749,7 @@ export interface MathJsInstance extends MathJsFactory {
743749
* @param format The Matrix storage format
744750
* @returns The created Matrix
745751
*/
746-
matrix(format?: 'sparse' | 'dense'): Matrix
752+
matrix(format?: MatrixStorageFormat): Matrix
747753
/**
748754
* @param data A multi dimensional array
749755
* @param format The Matrix storage format
@@ -752,12 +758,12 @@ export interface MathJsInstance extends MathJsFactory {
752758
*/
753759
matrix(
754760
data: MathCollection | string[],
755-
format?: 'sparse' | 'dense',
761+
format?: MatrixStorageFormat,
756762
dataType?: string
757763
): Matrix
758764
matrix<T extends MathScalarType>(
759765
data: MathCollection<T>,
760-
format?: 'sparse' | 'dense',
766+
format?: MatrixStorageFormat,
761767
dataType?: string
762768
): Matrix<T>
763769

@@ -1322,6 +1328,58 @@ export interface MathJsInstance extends MathJsFactory {
13221328
hypot<T extends number | BigNumber>(...args: T[]): T
13231329
hypot<T extends number | BigNumber>(args: T[]): T
13241330

1331+
/**
1332+
* Create a dense matrix from vectors as individual rows. If you pass column vectors, they will be transposed (but not conjugated!)
1333+
* @param rows - a multi-dimensional number array or matrix
1334+
*/
1335+
matrixFromRows(...rows: Matrix[]): Matrix
1336+
matrixFromRows<T extends MathScalarType>(
1337+
...rows: (T[] | [T][] | Matrix)[]
1338+
): T[][]
1339+
1340+
/**
1341+
* Create a dense matrix from vectors as individual columns. If you pass row vectors, they will be transposed (but not conjugated!)
1342+
* @param cols - a multi-dimensional number array or matrix
1343+
*/
1344+
matrixFromColumns(...cols: Matrix[]): Matrix
1345+
matrixFromColumns<T extends MathScalarType>(
1346+
...cols: (T[] | [T][] | Matrix)[]
1347+
): T[][]
1348+
/**
1349+
* 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.
1350+
* @param size - the size of the matrix to be created
1351+
* @param fn - Callback function invoked for every entry in the matrix
1352+
* @param format - The Matrix storage format, either 'dense' or 'sparse'
1353+
* @param datatype - Type of the values
1354+
*/
1355+
matrixFromFunction<T extends MathScalarType>(
1356+
size: [number],
1357+
fn: MatrixFromFunctionCallback<T>
1358+
): T[]
1359+
matrixFromFunction<T extends MathScalarType>(
1360+
size: [number, number],
1361+
fn: MatrixFromFunctionCallback<T>
1362+
): T[][]
1363+
matrixFromFunction<T extends MathScalarType>(
1364+
size: number[],
1365+
fn: MatrixFromFunctionCallback<T>
1366+
): MathArray<T>
1367+
matrixFromFunction(
1368+
size: Matrix<number>,
1369+
fn: MatrixFromFunctionCallback<MathScalarType>
1370+
): Matrix
1371+
matrixFromFunction(
1372+
size: number[] | Matrix<number>,
1373+
fn: MatrixFromFunctionCallback<MathScalarType>,
1374+
format: MatrixStorageFormat,
1375+
datatype?: string
1376+
): Matrix
1377+
matrixFromFunction(
1378+
size: number[] | Matrix<number>,
1379+
format: MatrixStorageFormat,
1380+
fn: MatrixFromFunctionCallback<MathScalarType>,
1381+
datatype?: string
1382+
): Matrix
13251383
/**
13261384
* Calculate the least common multiple for two or more values or arrays.
13271385
* lcm is defined as: lcm(a, b) = abs(a * b) / gcd(a, b) For matrices,

0 commit comments

Comments
 (0)