mathjs 11.0.0 → 11.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CONTRIBUTING.md +1 -4
- package/HISTORY.md +27 -0
- package/lib/browser/math.js +1 -1
- package/lib/browser/math.js.LICENSE.txt +2 -2
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/core/create.js +1 -0
- package/lib/cjs/core/function/typed.js +3 -0
- package/lib/cjs/entry/typeChecks.js +6 -0
- package/lib/cjs/expression/embeddedDocs/construction/index.js +1 -1
- package/lib/cjs/function/algebra/derivative.js +3 -3
- package/lib/cjs/function/algebra/simplify.js +3 -1
- package/lib/cjs/function/algebra/symbolicEqual.js +5 -6
- package/lib/cjs/function/arithmetic/fix.js +1 -1
- package/lib/cjs/function/arithmetic/floor.js +1 -1
- package/lib/cjs/function/arithmetic/invmod.js +1 -1
- package/lib/cjs/function/arithmetic/nthRoot.js +3 -3
- package/lib/cjs/function/arithmetic/nthRoots.js +2 -2
- package/lib/cjs/function/bitwise/bitNot.js +1 -1
- package/lib/cjs/function/bitwise/leftShift.js +1 -1
- package/lib/cjs/function/bitwise/rightArithShift.js +1 -1
- package/lib/cjs/function/bitwise/rightLogShift.js +1 -1
- package/lib/cjs/function/geometry/distance.js +2 -2
- package/lib/cjs/function/matrix/diff.js +1 -1
- package/lib/cjs/function/matrix/rotate.js +5 -5
- package/lib/cjs/function/string/format.js +2 -2
- package/lib/cjs/function/string/hex.js +1 -2
- package/lib/cjs/function/trigonometry/acot.js +2 -3
- package/lib/cjs/function/trigonometry/acsc.js +2 -3
- package/lib/cjs/function/trigonometry/asec.js +2 -2
- package/lib/cjs/function/trigonometry/asin.js +1 -1
- package/lib/cjs/function/trigonometry/atan.js +1 -2
- package/lib/cjs/function/trigonometry/atan2.js +1 -1
- package/lib/cjs/function/utils/clone.js +1 -1
- package/lib/cjs/function/utils/hasNumericValue.js +1 -1
- package/lib/cjs/function/utils/isInteger.js +1 -1
- package/lib/cjs/function/utils/isNaN.js +1 -1
- package/lib/cjs/function/utils/isNegative.js +1 -1
- package/lib/cjs/function/utils/isNumeric.js +1 -1
- package/lib/cjs/function/utils/isPositive.js +1 -1
- package/lib/cjs/function/utils/isZero.js +11 -11
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/type/unit/Unit.js +30 -25
- package/lib/cjs/type/unit/function/unit.js +5 -2
- package/lib/cjs/utils/is.js +5 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/core/create.js +2 -1
- package/lib/esm/core/function/typed.js +4 -1
- package/lib/esm/entry/typeChecks.js +1 -1
- package/lib/esm/expression/embeddedDocs/construction/index.js +1 -1
- package/lib/esm/function/algebra/derivative.js +3 -3
- package/lib/esm/function/algebra/simplify.js +3 -1
- package/lib/esm/function/algebra/symbolicEqual.js +5 -6
- package/lib/esm/function/arithmetic/fix.js +1 -1
- package/lib/esm/function/arithmetic/floor.js +1 -1
- package/lib/esm/function/arithmetic/invmod.js +1 -1
- package/lib/esm/function/arithmetic/nthRoot.js +3 -3
- package/lib/esm/function/arithmetic/nthRoots.js +2 -2
- package/lib/esm/function/bitwise/bitNot.js +1 -1
- package/lib/esm/function/bitwise/leftShift.js +1 -1
- package/lib/esm/function/bitwise/rightArithShift.js +1 -1
- package/lib/esm/function/bitwise/rightLogShift.js +1 -1
- package/lib/esm/function/geometry/distance.js +2 -2
- package/lib/esm/function/matrix/diff.js +1 -1
- package/lib/esm/function/matrix/rotate.js +5 -5
- package/lib/esm/function/string/format.js +2 -2
- package/lib/esm/function/string/hex.js +1 -2
- package/lib/esm/function/trigonometry/acot.js +2 -3
- package/lib/esm/function/trigonometry/acsc.js +2 -3
- package/lib/esm/function/trigonometry/asec.js +2 -2
- package/lib/esm/function/trigonometry/asin.js +1 -1
- package/lib/esm/function/trigonometry/atan.js +1 -2
- package/lib/esm/function/trigonometry/atan2.js +1 -1
- package/lib/esm/function/utils/clone.js +1 -1
- package/lib/esm/function/utils/hasNumericValue.js +1 -1
- package/lib/esm/function/utils/isInteger.js +1 -1
- package/lib/esm/function/utils/isNaN.js +1 -1
- package/lib/esm/function/utils/isNegative.js +1 -1
- package/lib/esm/function/utils/isNumeric.js +1 -1
- package/lib/esm/function/utils/isPositive.js +1 -1
- package/lib/esm/function/utils/isZero.js +11 -11
- package/lib/esm/type/unit/Unit.js +25 -24
- package/lib/esm/type/unit/function/unit.js +5 -2
- package/lib/esm/utils/is.js +3 -0
- package/lib/esm/version.js +1 -1
- package/package.json +17 -18
- package/types/index.d.ts +33 -19
- package/types/index.ts +123 -95
- package/types/tsconfig.json +4 -1
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "mathjs",
|
3
|
-
"version": "11.
|
3
|
+
"version": "11.2.0",
|
4
4
|
"description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.",
|
5
5
|
"author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
|
6
6
|
"homepage": "https://mathjs.org",
|
@@ -25,42 +25,42 @@
|
|
25
25
|
"unit"
|
26
26
|
],
|
27
27
|
"dependencies": {
|
28
|
-
"@babel/runtime": "^7.
|
28
|
+
"@babel/runtime": "^7.19.0",
|
29
29
|
"complex.js": "^2.1.1",
|
30
|
-
"decimal.js": "^10.
|
30
|
+
"decimal.js": "^10.4.0",
|
31
31
|
"escape-latex": "^1.2.0",
|
32
32
|
"fraction.js": "^4.2.0",
|
33
33
|
"javascript-natural-sort": "^0.7.1",
|
34
34
|
"seedrandom": "^3.0.5",
|
35
35
|
"tiny-emitter": "^2.1.0",
|
36
|
-
"typed-function": "^
|
36
|
+
"typed-function": "^4.1.0"
|
37
37
|
},
|
38
38
|
"devDependencies": {
|
39
|
-
"@babel/core": "7.
|
39
|
+
"@babel/core": "7.19.0",
|
40
40
|
"@babel/plugin-transform-object-assign": "7.18.6",
|
41
|
-
"@babel/plugin-transform-runtime": "7.18.
|
42
|
-
"@babel/preset-env": "7.
|
41
|
+
"@babel/plugin-transform-runtime": "7.18.10",
|
42
|
+
"@babel/preset-env": "7.19.0",
|
43
43
|
"@babel/register": "7.18.9",
|
44
44
|
"@types/assert": "1.5.6",
|
45
45
|
"@types/mocha": "9.1.1",
|
46
|
-
"@typescript-eslint/eslint-plugin": "5.
|
47
|
-
"@typescript-eslint/parser": "5.
|
46
|
+
"@typescript-eslint/eslint-plugin": "5.36.2",
|
47
|
+
"@typescript-eslint/parser": "5.36.2",
|
48
48
|
"assert": "2.0.0",
|
49
49
|
"babel-loader": "8.2.5",
|
50
50
|
"benchmark": "2.1.4",
|
51
51
|
"codecov": "3.8.3",
|
52
|
-
"core-js": "3.
|
52
|
+
"core-js": "3.25.1",
|
53
53
|
"del": "6.1.1",
|
54
54
|
"dtslint": "4.2.1",
|
55
|
-
"eslint": "8.
|
55
|
+
"eslint": "8.23.1",
|
56
56
|
"eslint-config-prettier": "8.5.0",
|
57
57
|
"eslint-config-standard": "17.0.0",
|
58
58
|
"eslint-plugin-import": "2.26.0",
|
59
59
|
"eslint-plugin-mocha": "10.1.0",
|
60
|
-
"eslint-plugin-n": "15.2.
|
60
|
+
"eslint-plugin-n": "15.2.5",
|
61
61
|
"eslint-plugin-prettier": "4.2.1",
|
62
|
-
"eslint-plugin-promise": "6.0.
|
63
|
-
"expect-type": "0.
|
62
|
+
"eslint-plugin-promise": "6.0.1",
|
63
|
+
"expect-type": "0.14.2",
|
64
64
|
"expr-eval": "2.0.2",
|
65
65
|
"fancy-log": "2.0.0",
|
66
66
|
"glob": "8.0.3",
|
@@ -90,8 +90,8 @@
|
|
90
90
|
"process": "0.11.10",
|
91
91
|
"sylvester": "0.0.21",
|
92
92
|
"ts-node": "10.9.1",
|
93
|
-
"typescript": "4.
|
94
|
-
"webpack": "5.
|
93
|
+
"typescript": "4.8.3",
|
94
|
+
"webpack": "5.74.0",
|
95
95
|
"zeros": "1.0.0"
|
96
96
|
},
|
97
97
|
"type": "module",
|
@@ -161,10 +161,9 @@
|
|
161
161
|
"test:all": "npm run test:src && npm run test:generated && npm run test:node && npm run test:types",
|
162
162
|
"test:browser": "karma start test/browser-test-config/local-karma.js",
|
163
163
|
"test:browserstack": "karma start test/browser-test-config/browserstack-karma.js",
|
164
|
-
"test:types": "cd types && node --loader ts-node/esm ./index.ts",
|
164
|
+
"test:types": "cd types && tsc -p ./tsconfig.json && node --loader ts-node/esm ./index.ts",
|
165
165
|
"coverage": "nyc --reporter=lcov --reporter=text-summary mocha test/unit-tests && echo \"\nDetailed coverage report is available at ./coverage/lcov-report/index.html\"",
|
166
166
|
"prepublishOnly": "npm run test:all && npm run lint",
|
167
|
-
"prepare": "npm run build",
|
168
167
|
"update-authors": "node ./tools/update-authors.js"
|
169
168
|
},
|
170
169
|
"bin": {
|
package/types/index.d.ts
CHANGED
@@ -283,6 +283,7 @@ declare namespace math {
|
|
283
283
|
type OperatorNodeMap = {
|
284
284
|
xor: 'xor'
|
285
285
|
and: 'and'
|
286
|
+
or: 'or'
|
286
287
|
bitOr: '|'
|
287
288
|
bitXor: '^|'
|
288
289
|
bitAnd: '&'
|
@@ -291,6 +292,7 @@ declare namespace math {
|
|
291
292
|
smaller: '<'
|
292
293
|
larger: '>'
|
293
294
|
smallerEq: '<='
|
295
|
+
largerEq: '>='
|
294
296
|
leftShift: '<<'
|
295
297
|
rightArithShift: '>>'
|
296
298
|
rightLogShift: '>>>'
|
@@ -478,6 +480,12 @@ declare namespace math {
|
|
478
480
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
479
481
|
reviver(): (key: any, value: any) => any
|
480
482
|
|
483
|
+
/**
|
484
|
+
* Returns replacer function that can be used as replacer in JSON.stringify function.
|
485
|
+
*/
|
486
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
487
|
+
replacer(): (key: any, value: any) => any
|
488
|
+
|
481
489
|
/*************************************************************************
|
482
490
|
* Core functions
|
483
491
|
************************************************************************/
|
@@ -890,8 +898,11 @@ declare namespace math {
|
|
890
898
|
*/
|
891
899
|
simplify: Simplify
|
892
900
|
|
893
|
-
simplifyConstant(
|
894
|
-
|
901
|
+
simplifyConstant(
|
902
|
+
expr: MathNode | string,
|
903
|
+
options?: SimplifyOptions
|
904
|
+
): MathNode
|
905
|
+
simplifyCore(expr: MathNode | string, options?: SimplifyOptions): MathNode
|
895
906
|
|
896
907
|
/**
|
897
908
|
* Replaces variable nodes with their scoped values
|
@@ -2192,11 +2203,9 @@ declare namespace math {
|
|
2192
2203
|
* undefined. Returns an array with the configured number of elements
|
2193
2204
|
* when number is > 1.
|
2194
2205
|
*/
|
2195
|
-
pickRandom(
|
2196
|
-
|
2197
|
-
|
2198
|
-
weights?: number[]
|
2199
|
-
): number | number[]
|
2206
|
+
pickRandom<T>(array: T[]): T
|
2207
|
+
pickRandom<T>(array: T[], number: number): T[]
|
2208
|
+
pickRandom<T>(array: T[], number: number, weights: number[]): T[]
|
2200
2209
|
|
2201
2210
|
/**
|
2202
2211
|
* Return a random number larger or equal to min and smaller than max
|
@@ -2567,9 +2576,9 @@ declare namespace math {
|
|
2567
2576
|
median(...args: MathType[]): any
|
2568
2577
|
|
2569
2578
|
/**
|
2570
|
-
* Compute the
|
2571
|
-
* a multi dimensional array, the
|
2572
|
-
* calculated. When dim is provided, the
|
2579
|
+
* Compute the minimum value of a matrix or a list of values. In case of
|
2580
|
+
* a multi dimensional array, the minimun of the flattened array will be
|
2581
|
+
* calculated. When dim is provided, the minimun over the selected
|
2573
2582
|
* dimension will be calculated. Parameter dim is zero-based.
|
2574
2583
|
* @param args A single matrix or or multiple scalar values
|
2575
2584
|
* @returns The minimum value
|
@@ -3149,6 +3158,8 @@ declare namespace math {
|
|
3149
3158
|
|
3150
3159
|
isRangeNode(x: unknown): x is RangeNode
|
3151
3160
|
|
3161
|
+
isRelationalNode(x: unknown): x is RelationalNode
|
3162
|
+
|
3152
3163
|
isSymbolNode(x: unknown): x is SymbolNode
|
3153
3164
|
|
3154
3165
|
isChain(x: unknown): x is MathJsChain<unknown>
|
@@ -4407,11 +4418,12 @@ declare namespace math {
|
|
4407
4418
|
* pass a custom set of rules to the function as second argument. A rule
|
4408
4419
|
* can be specified as an object, string, or function.
|
4409
4420
|
* @param scope Scope to variables
|
4421
|
+
* @param options Options to configure the behavior of simplify
|
4410
4422
|
*/
|
4411
4423
|
simplify(
|
4412
4424
|
this: MathJsChain<MathNode | string>,
|
4413
4425
|
rules?: SimplifyRule[],
|
4414
|
-
scope?: Map | object,
|
4426
|
+
scope?: Map<string, MathType> | object,
|
4415
4427
|
options?: SimplifyOptions
|
4416
4428
|
): MathJsChain<MathNode>
|
4417
4429
|
|
@@ -5511,11 +5523,13 @@ declare namespace math {
|
|
5511
5523
|
* @param number An int or float
|
5512
5524
|
* @param weights An array of ints or floats
|
5513
5525
|
*/
|
5514
|
-
pickRandom(
|
5515
|
-
|
5516
|
-
|
5517
|
-
|
5518
|
-
|
5526
|
+
pickRandom<T>(this: MathJsChain<T[]>): MathJsChain<T>
|
5527
|
+
pickRandom<T>(this: MathJsChain<T[]>, number: number): MathJsChain<T[]>
|
5528
|
+
pickRandom<T>(
|
5529
|
+
this: MathJsChain<T[]>,
|
5530
|
+
number: number,
|
5531
|
+
weights: number[]
|
5532
|
+
): MathJsChain<T[]>
|
5519
5533
|
|
5520
5534
|
/**
|
5521
5535
|
* Return a random number larger or equal to min and smaller than max
|
@@ -5863,9 +5877,9 @@ declare namespace math {
|
|
5863
5877
|
median(this: MathJsChain<MathCollection>, dim?: number): MathJsChain<any>
|
5864
5878
|
|
5865
5879
|
/**
|
5866
|
-
* Compute the
|
5867
|
-
* a multi dimensional array, the
|
5868
|
-
* calculated. When dim is provided, the
|
5880
|
+
* Compute the minimum value of a matrix or a list of values. In case of
|
5881
|
+
* a multi dimensional array, the minimum of the flattened array will be
|
5882
|
+
* calculated. When dim is provided, the minimum over the selected
|
5869
5883
|
* dimension will be calculated. Parameter dim is zero-based.
|
5870
5884
|
* @param dim The minimum over the selected dimension
|
5871
5885
|
*/
|
package/types/index.ts
CHANGED
@@ -1,35 +1,48 @@
|
|
1
1
|
import {
|
2
|
-
|
3
|
-
factory,
|
4
|
-
all,
|
5
|
-
MathJsFunctionName,
|
6
|
-
fractionDependencies,
|
2
|
+
AccessorNode,
|
7
3
|
addDependencies,
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
MathJsChain,
|
4
|
+
all,
|
5
|
+
ArrayNode,
|
6
|
+
AssignmentNode,
|
12
7
|
BigNumber,
|
13
|
-
|
8
|
+
BlockNode,
|
14
9
|
Complex,
|
15
|
-
|
10
|
+
ConditionalNode,
|
11
|
+
ConstantNode,
|
12
|
+
create,
|
13
|
+
divideDependencies,
|
14
|
+
EvalFunction,
|
15
|
+
factory,
|
16
|
+
formatDependencies,
|
16
17
|
Fraction,
|
17
|
-
|
18
|
+
fractionDependencies,
|
19
|
+
FunctionAssignmentNode,
|
20
|
+
FunctionNode,
|
21
|
+
Help,
|
18
22
|
Index,
|
19
|
-
|
20
|
-
EvalFunction,
|
23
|
+
IndexNode,
|
21
24
|
LUDecomposition,
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
MathArray,
|
26
|
+
MathCollection,
|
27
|
+
MathJsChain,
|
28
|
+
MathJsFunctionName,
|
29
|
+
MathNode,
|
30
|
+
MathNodeCommon,
|
25
31
|
MathNumericType,
|
26
|
-
|
32
|
+
MathType,
|
33
|
+
Matrix,
|
34
|
+
ObjectNode,
|
27
35
|
OperatorNode,
|
28
36
|
OperatorNodeFn,
|
29
37
|
OperatorNodeOp,
|
30
|
-
SymbolNode,
|
31
38
|
ParenthesisNode,
|
39
|
+
PolarCoordinates,
|
40
|
+
QRDecomposition,
|
41
|
+
RangeNode,
|
32
42
|
SimplifyRule,
|
43
|
+
SLUDecomposition,
|
44
|
+
SymbolNode,
|
45
|
+
Unit,
|
33
46
|
} from 'mathjs'
|
34
47
|
import * as assert from 'assert'
|
35
48
|
import { expectTypeOf } from 'expect-type'
|
@@ -125,8 +138,8 @@ Basic usage examples
|
|
125
138
|
) // Matrix + Array
|
126
139
|
|
127
140
|
// narrowed type inference
|
128
|
-
const _b:
|
129
|
-
const _c:
|
141
|
+
const _b: Matrix = math.add(math.matrix([2]), math.matrix([3]))
|
142
|
+
const _c: Matrix = math.subtract(math.matrix([4]), math.matrix([5]))
|
130
143
|
}
|
131
144
|
|
132
145
|
/*
|
@@ -944,18 +957,18 @@ Complex numbers examples
|
|
944
957
|
|
945
958
|
// create a complex number from polar coordinates
|
946
959
|
{
|
947
|
-
const p:
|
960
|
+
const p: PolarCoordinates = {
|
948
961
|
r: math.sqrt(2) as number, // must be real but a sqrt could be Complex
|
949
962
|
phi: math.pi / 4,
|
950
963
|
}
|
951
|
-
const c:
|
964
|
+
const c: Complex = math.complex(p)
|
952
965
|
assert.strictEqual(c.im, 1)
|
953
966
|
assert.ok(Math.abs(c.re - 1) < 1e-12)
|
954
967
|
}
|
955
968
|
|
956
969
|
// get polar coordinates of a complex number
|
957
970
|
{
|
958
|
-
const _p:
|
971
|
+
const _p: PolarCoordinates = math.complex(3, 4).toPolar()
|
959
972
|
}
|
960
973
|
}
|
961
974
|
|
@@ -1014,7 +1027,7 @@ Expressions examples
|
|
1014
1027
|
|
1015
1028
|
{
|
1016
1029
|
const node2 = math.parse('x^a')
|
1017
|
-
const _code2:
|
1030
|
+
const _code2: EvalFunction = node2.compile()
|
1018
1031
|
node2.toString()
|
1019
1032
|
}
|
1020
1033
|
|
@@ -1117,8 +1130,8 @@ Matrices examples
|
|
1117
1130
|
|
1118
1131
|
// create matrices and arrays. a matrix is just a wrapper around an Array,
|
1119
1132
|
// providing some handy utilities.
|
1120
|
-
const a:
|
1121
|
-
const b:
|
1133
|
+
const a: Matrix = math.matrix([1, 4, 9, 16, 25])
|
1134
|
+
const b: Matrix = math.matrix(math.ones([2, 3]))
|
1122
1135
|
b.size()
|
1123
1136
|
|
1124
1137
|
// @ts-expect-error ... ones() in a chain cannot take more dimensions
|
@@ -1130,7 +1143,7 @@ Matrices examples
|
|
1130
1143
|
const _array = a.valueOf()
|
1131
1144
|
|
1132
1145
|
// Matrices can be cloned
|
1133
|
-
const _clone:
|
1146
|
+
const _clone: Matrix = a.clone()
|
1134
1147
|
|
1135
1148
|
// perform operations with matrices
|
1136
1149
|
math.map(a, math.sqrt)
|
@@ -1142,22 +1155,22 @@ Matrices examples
|
|
1142
1155
|
[1, 2],
|
1143
1156
|
[3, 4],
|
1144
1157
|
]
|
1145
|
-
const b:
|
1158
|
+
const b: Matrix = math.matrix([
|
1146
1159
|
[5, 6],
|
1147
1160
|
[1, 1],
|
1148
1161
|
])
|
1149
1162
|
|
1150
1163
|
b.subset(math.index(1, [0, 1]), [[7, 8]])
|
1151
1164
|
const _c = math.multiply(a, b)
|
1152
|
-
const f:
|
1153
|
-
const _d:
|
1165
|
+
const f: Matrix = math.matrix([1, 0])
|
1166
|
+
const _d: Matrix = f.subset(math.index(1))
|
1154
1167
|
}
|
1155
1168
|
|
1156
1169
|
// get a sub matrix
|
1157
1170
|
{
|
1158
|
-
const a:
|
1171
|
+
const a: Matrix = math.diag(math.range(1, 4))
|
1159
1172
|
a.subset(math.index([1, 2], [1, 2]))
|
1160
|
-
const b:
|
1173
|
+
const b: Matrix = math.range(1, 6)
|
1161
1174
|
b.subset(math.index(math.range(1, 4)))
|
1162
1175
|
}
|
1163
1176
|
|
@@ -1203,7 +1216,7 @@ Matrices examples
|
|
1203
1216
|
[6, 4, 3]
|
1204
1217
|
)
|
1205
1218
|
assert.deepStrictEqual(
|
1206
|
-
math.filter(['23', 'foo', '100', '55', 'bar'],
|
1219
|
+
math.filter(['23', 'foo', '100', '55', 'bar'], /\d+/),
|
1207
1220
|
['23', '100', '55']
|
1208
1221
|
)
|
1209
1222
|
}
|
@@ -1308,7 +1321,7 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices
|
|
1308
1321
|
|
1309
1322
|
// 1D JS Array
|
1310
1323
|
const r3 = math.multiply(abc, bcd)
|
1311
|
-
r3[1] // By default least promised valid syntax
|
1324
|
+
const _r31 = r3[1] // By default least promised valid syntax
|
1312
1325
|
|
1313
1326
|
// 2D JS Array
|
1314
1327
|
const r12 = math.multiply(bcd, bcd)
|
@@ -1316,9 +1329,9 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices
|
|
1316
1329
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
1317
1330
|
const multiDimensional = (x: any): x is any[][] => x.length && x[0].length
|
1318
1331
|
if (multiDimensional(r12)) {
|
1319
|
-
r12[1][1]
|
1332
|
+
const _r1211 = r12[1][1]
|
1320
1333
|
}
|
1321
|
-
r12[1] // Valid syntax
|
1334
|
+
const _r121 = r12[1] // Valid syntax
|
1322
1335
|
|
1323
1336
|
// Matrix: matrix * vector
|
1324
1337
|
const r7 = math.multiply(Mabc, bcd)
|
@@ -1450,29 +1463,27 @@ Expression tree examples
|
|
1450
1463
|
const math = create(all, {})
|
1451
1464
|
|
1452
1465
|
// Filter an expression tree
|
1453
|
-
const node:
|
1454
|
-
const filtered:
|
1455
|
-
(node:
|
1466
|
+
const node: MathNode = math.parse('x^2 + x/4 + 3*y')
|
1467
|
+
const filtered: MathNode[] = node.filter(
|
1468
|
+
(node: MathNode) => node.type === 'SymbolNode' && node.name === 'x'
|
1456
1469
|
)
|
1457
1470
|
|
1458
|
-
const _arr: string[] = filtered.map((node:
|
1471
|
+
const _arr: string[] = filtered.map((node: MathNode) => node.toString())
|
1459
1472
|
|
1460
1473
|
// Traverse an expression tree
|
1461
|
-
const node1:
|
1462
|
-
node1.traverse(
|
1463
|
-
(node
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
return
|
1473
|
-
}
|
1474
|
+
const node1: MathNode = math.parse('3 * x + 2')
|
1475
|
+
node1.traverse((node: MathNode, _path: string, _parent: MathNode) => {
|
1476
|
+
switch (node.type) {
|
1477
|
+
case 'OperatorNode':
|
1478
|
+
return node.type === 'OperatorNode'
|
1479
|
+
case 'ConstantNode':
|
1480
|
+
return node.type === 'ConstantNode'
|
1481
|
+
case 'SymbolNode':
|
1482
|
+
return node.type === 'SymbolNode'
|
1483
|
+
default:
|
1484
|
+
return
|
1474
1485
|
}
|
1475
|
-
)
|
1486
|
+
})
|
1476
1487
|
}
|
1477
1488
|
|
1478
1489
|
/*
|
@@ -1535,9 +1546,9 @@ Function ceil examples
|
|
1535
1546
|
)
|
1536
1547
|
|
1537
1548
|
// numeric input, array or matrix of decimals
|
1538
|
-
const numCeiled:
|
1549
|
+
const numCeiled: MathArray = math.ceil(math.tau, [2, 3])
|
1539
1550
|
assert.deepStrictEqual(numCeiled, [6.29, 6.284])
|
1540
|
-
const bigCeiled:
|
1551
|
+
const bigCeiled: Matrix = math.ceil(
|
1541
1552
|
math.bignumber(6.28318),
|
1542
1553
|
math.matrix([2, 3])
|
1543
1554
|
)
|
@@ -1611,9 +1622,9 @@ Function fix examples
|
|
1611
1622
|
)
|
1612
1623
|
|
1613
1624
|
// numeric input, array or matrix of decimals
|
1614
|
-
const numFixed:
|
1625
|
+
const numFixed: MathArray = math.fix(math.tau, [2, 3])
|
1615
1626
|
assert.deepStrictEqual(numFixed, [6.28, 6.283])
|
1616
|
-
const bigFixed:
|
1627
|
+
const bigFixed: Matrix = math.fix(
|
1617
1628
|
math.bignumber(6.28318),
|
1618
1629
|
math.matrix([2, 3])
|
1619
1630
|
)
|
@@ -1687,9 +1698,9 @@ Function floor examples
|
|
1687
1698
|
)
|
1688
1699
|
|
1689
1700
|
// numeric input, array or matrix of decimals
|
1690
|
-
const numFloored:
|
1701
|
+
const numFloored: MathArray = math.floor(math.tau, [2, 3])
|
1691
1702
|
assert.deepStrictEqual(numFloored, [6.28, 6.283])
|
1692
|
-
const bigFloored:
|
1703
|
+
const bigFloored: Matrix = math.floor(
|
1693
1704
|
math.bignumber(6.28318),
|
1694
1705
|
math.matrix([2, 3])
|
1695
1706
|
)
|
@@ -1763,9 +1774,9 @@ Function round examples
|
|
1763
1774
|
)
|
1764
1775
|
|
1765
1776
|
// numeric input, array or matrix of decimals
|
1766
|
-
const numRounded:
|
1777
|
+
const numRounded: MathArray = math.round(math.tau, [2, 3])
|
1767
1778
|
assert.deepStrictEqual(numRounded, [6.28, 6.283])
|
1768
|
-
const bigRounded:
|
1779
|
+
const bigRounded: Matrix = math.round(
|
1769
1780
|
math.bignumber(6.28318),
|
1770
1781
|
math.matrix([2, 3])
|
1771
1782
|
)
|
@@ -2065,6 +2076,7 @@ Factory Test
|
|
2065
2076
|
math.isOperatorNode,
|
2066
2077
|
math.isParenthesisNode,
|
2067
2078
|
math.isRangeNode,
|
2079
|
+
math.isRelationalNode,
|
2068
2080
|
math.isSymbolNode,
|
2069
2081
|
math.isChain,
|
2070
2082
|
]
|
@@ -2077,22 +2089,22 @@ Factory Test
|
|
2077
2089
|
|
2078
2090
|
// Check guards do type refinement
|
2079
2091
|
|
2080
|
-
|
2092
|
+
const x: unknown = undefined
|
2081
2093
|
|
2082
2094
|
if (math.isNumber(x)) {
|
2083
2095
|
expectTypeOf(x).toMatchTypeOf<number>()
|
2084
2096
|
}
|
2085
2097
|
if (math.isBigNumber(x)) {
|
2086
|
-
expectTypeOf(x).toMatchTypeOf<
|
2098
|
+
expectTypeOf(x).toMatchTypeOf<BigNumber>()
|
2087
2099
|
}
|
2088
2100
|
if (math.isComplex(x)) {
|
2089
|
-
expectTypeOf(x).toMatchTypeOf<
|
2101
|
+
expectTypeOf(x).toMatchTypeOf<Complex>()
|
2090
2102
|
}
|
2091
2103
|
if (math.isFraction(x)) {
|
2092
|
-
expectTypeOf(x).toMatchTypeOf<
|
2104
|
+
expectTypeOf(x).toMatchTypeOf<Fraction>()
|
2093
2105
|
}
|
2094
2106
|
if (math.isUnit(x)) {
|
2095
|
-
expectTypeOf(x).toMatchTypeOf<
|
2107
|
+
expectTypeOf(x).toMatchTypeOf<Unit>()
|
2096
2108
|
}
|
2097
2109
|
if (math.isString(x)) {
|
2098
2110
|
expectTypeOf(x).toMatchTypeOf<string>()
|
@@ -2101,22 +2113,22 @@ Factory Test
|
|
2101
2113
|
expectTypeOf(x).toMatchTypeOf<unknown[]>()
|
2102
2114
|
}
|
2103
2115
|
if (math.isMatrix(x)) {
|
2104
|
-
expectTypeOf(x).toMatchTypeOf<
|
2116
|
+
expectTypeOf(x).toMatchTypeOf<Matrix>()
|
2105
2117
|
}
|
2106
2118
|
if (math.isDenseMatrix(x)) {
|
2107
|
-
expectTypeOf(x).toMatchTypeOf<
|
2119
|
+
expectTypeOf(x).toMatchTypeOf<Matrix>()
|
2108
2120
|
}
|
2109
2121
|
if (math.isSparseMatrix(x)) {
|
2110
|
-
expectTypeOf(x).toMatchTypeOf<
|
2122
|
+
expectTypeOf(x).toMatchTypeOf<Matrix>()
|
2111
2123
|
}
|
2112
2124
|
if (math.isIndex(x)) {
|
2113
|
-
expectTypeOf(x).toMatchTypeOf<
|
2125
|
+
expectTypeOf(x).toMatchTypeOf<Index>()
|
2114
2126
|
}
|
2115
2127
|
if (math.isBoolean(x)) {
|
2116
2128
|
expectTypeOf(x).toMatchTypeOf<boolean>()
|
2117
2129
|
}
|
2118
2130
|
if (math.isHelp(x)) {
|
2119
|
-
expectTypeOf(x).toMatchTypeOf<
|
2131
|
+
expectTypeOf(x).toMatchTypeOf<Help>()
|
2120
2132
|
}
|
2121
2133
|
if (math.isDate(x)) {
|
2122
2134
|
expectTypeOf(x).toMatchTypeOf<Date>()
|
@@ -2132,40 +2144,40 @@ Factory Test
|
|
2132
2144
|
}
|
2133
2145
|
|
2134
2146
|
if (math.isAccessorNode(x)) {
|
2135
|
-
expectTypeOf(x).toMatchTypeOf<
|
2147
|
+
expectTypeOf(x).toMatchTypeOf<AccessorNode>()
|
2136
2148
|
}
|
2137
2149
|
if (math.isArrayNode(x)) {
|
2138
|
-
expectTypeOf(x).toMatchTypeOf<
|
2150
|
+
expectTypeOf(x).toMatchTypeOf<ArrayNode>()
|
2139
2151
|
}
|
2140
2152
|
if (math.isAssignmentNode(x)) {
|
2141
|
-
expectTypeOf(x).toMatchTypeOf<
|
2153
|
+
expectTypeOf(x).toMatchTypeOf<AssignmentNode>()
|
2142
2154
|
}
|
2143
2155
|
if (math.isBlockNode(x)) {
|
2144
|
-
expectTypeOf(x).toMatchTypeOf<
|
2156
|
+
expectTypeOf(x).toMatchTypeOf<BlockNode>()
|
2145
2157
|
}
|
2146
2158
|
if (math.isConditionalNode(x)) {
|
2147
|
-
expectTypeOf(x).toMatchTypeOf<
|
2159
|
+
expectTypeOf(x).toMatchTypeOf<ConditionalNode>()
|
2148
2160
|
}
|
2149
2161
|
if (math.isConstantNode(x)) {
|
2150
|
-
expectTypeOf(x).toMatchTypeOf<
|
2162
|
+
expectTypeOf(x).toMatchTypeOf<ConstantNode>()
|
2151
2163
|
}
|
2152
2164
|
if (math.isFunctionAssignmentNode(x)) {
|
2153
|
-
expectTypeOf(x).toMatchTypeOf<
|
2165
|
+
expectTypeOf(x).toMatchTypeOf<FunctionAssignmentNode>()
|
2154
2166
|
}
|
2155
2167
|
if (math.isFunctionNode(x)) {
|
2156
|
-
expectTypeOf(x).toMatchTypeOf<
|
2168
|
+
expectTypeOf(x).toMatchTypeOf<FunctionNode>()
|
2157
2169
|
}
|
2158
2170
|
if (math.isIndexNode(x)) {
|
2159
|
-
expectTypeOf(x).toMatchTypeOf<
|
2171
|
+
expectTypeOf(x).toMatchTypeOf<IndexNode>()
|
2160
2172
|
}
|
2161
2173
|
if (math.isNode(x)) {
|
2162
|
-
expectTypeOf(x).toMatchTypeOf<
|
2174
|
+
expectTypeOf(x).toMatchTypeOf<MathNodeCommon>()
|
2163
2175
|
}
|
2164
2176
|
if (math.isNode(x)) {
|
2165
|
-
expectTypeOf(x).toMatchTypeOf<
|
2177
|
+
expectTypeOf(x).toMatchTypeOf<MathNodeCommon>()
|
2166
2178
|
}
|
2167
2179
|
if (math.isObjectNode(x)) {
|
2168
|
-
expectTypeOf(x).toMatchTypeOf<
|
2180
|
+
expectTypeOf(x).toMatchTypeOf<ObjectNode>()
|
2169
2181
|
}
|
2170
2182
|
if (math.isOperatorNode(x)) {
|
2171
2183
|
expectTypeOf(x).toMatchTypeOf<
|
@@ -2173,17 +2185,20 @@ Factory Test
|
|
2173
2185
|
>()
|
2174
2186
|
}
|
2175
2187
|
if (math.isParenthesisNode(x)) {
|
2176
|
-
expectTypeOf(x).toMatchTypeOf<
|
2188
|
+
expectTypeOf(x).toMatchTypeOf<ParenthesisNode>()
|
2177
2189
|
}
|
2178
2190
|
if (math.isRangeNode(x)) {
|
2179
|
-
expectTypeOf(x).toMatchTypeOf<
|
2191
|
+
expectTypeOf(x).toMatchTypeOf<RangeNode>()
|
2192
|
+
}
|
2193
|
+
if (math.isRelationalNode(x)) {
|
2194
|
+
expectTypeOf(x).toMatchTypeOf<math.RelationalNode>()
|
2180
2195
|
}
|
2181
2196
|
if (math.isSymbolNode(x)) {
|
2182
|
-
expectTypeOf(x).toMatchTypeOf<
|
2197
|
+
expectTypeOf(x).toMatchTypeOf<SymbolNode>()
|
2183
2198
|
}
|
2184
2199
|
if (math.isChain(x)) {
|
2185
2200
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2186
|
-
expectTypeOf(x).toMatchTypeOf<
|
2201
|
+
expectTypeOf(x).toMatchTypeOf<MathJsChain<any>>()
|
2187
2202
|
}
|
2188
2203
|
}
|
2189
2204
|
|
@@ -2194,9 +2209,7 @@ Probability function examples
|
|
2194
2209
|
const math = create(all, {})
|
2195
2210
|
|
2196
2211
|
expectTypeOf(math.lgamma(1.5)).toMatchTypeOf<number>()
|
2197
|
-
expectTypeOf(
|
2198
|
-
math.lgamma(math.complex(1.5, -1.5))
|
2199
|
-
).toMatchTypeOf<math.Complex>()
|
2212
|
+
expectTypeOf(math.lgamma(math.complex(1.5, -1.5))).toMatchTypeOf<Complex>()
|
2200
2213
|
}
|
2201
2214
|
|
2202
2215
|
/*
|
@@ -2231,7 +2244,22 @@ Resolve examples
|
|
2231
2244
|
expectTypeOf(
|
2232
2245
|
math.resolve([math.parse('x + y'), 'x*x'], { x: 0 })
|
2233
2246
|
).toMatchTypeOf<MathNode[]>()
|
2234
|
-
expectTypeOf(
|
2235
|
-
|
2236
|
-
|
2247
|
+
expectTypeOf(math.resolve(math.matrix(['x', 'y']))).toMatchTypeOf<Matrix>()
|
2248
|
+
}
|
2249
|
+
|
2250
|
+
/*
|
2251
|
+
Random examples
|
2252
|
+
*/
|
2253
|
+
{
|
2254
|
+
const math = create(all, {})
|
2255
|
+
expectTypeOf(math.pickRandom([1, 2, 3])).toMatchTypeOf<number>()
|
2256
|
+
expectTypeOf(math.pickRandom(['a', { b: 10 }, 42])).toMatchTypeOf<
|
2257
|
+
string | number | { b: number }
|
2258
|
+
>()
|
2259
|
+
expectTypeOf(math.pickRandom([1, 2, 3])).toMatchTypeOf<number>()
|
2260
|
+
expectTypeOf(math.pickRandom([1, 2, 3], 2)).toMatchTypeOf<number[]>()
|
2261
|
+
|
2262
|
+
expectTypeOf(math.chain([1, 2, 3]).pickRandom(2)).toMatchTypeOf<
|
2263
|
+
MathJsChain<number[]>
|
2264
|
+
>()
|
2237
2265
|
}
|