mathjs 10.5.0 → 10.5.1
Sign up to get free protection for your applications and to get access to all the features.
- package/HISTORY.md +12 -0
- package/docs/datatypes/matrices.md +17 -0
- package/docs/expressions/syntax.md +4 -1
- package/docs/reference/functions/fix.md +1 -0
- package/docs/reference/functions/floor.md +4 -0
- package/docs/reference/functions/resolve.md +2 -2
- package/docs/reference/functions/simplifyConstant.md +52 -0
- package/lib/browser/math.js +5 -5
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesCeil.generated.js +6 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesFix.generated.js +9 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesFloor.generated.js +6 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesIndexNode.generated.js +0 -3
- package/lib/cjs/entry/dependenciesNumber/dependenciesCeil.generated.js +3 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesFix.generated.js +6 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesFloor.generated.js +3 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesIndexNode.generated.js +0 -3
- package/lib/cjs/entry/impureFunctionsAny.generated.js +0 -1
- package/lib/cjs/entry/impureFunctionsNumber.generated.js +6 -7
- package/lib/cjs/entry/pureFunctionsAny.generated.js +10 -3
- package/lib/cjs/entry/pureFunctionsNumber.generated.js +21 -15
- package/lib/cjs/expression/node/IndexNode.js +26 -61
- package/lib/cjs/factoriesNumber.js +29 -24
- package/lib/cjs/function/algebra/resolve.js +3 -3
- package/lib/cjs/function/arithmetic/ceil.js +75 -39
- package/lib/cjs/function/arithmetic/fix.js +54 -16
- package/lib/cjs/function/arithmetic/floor.js +79 -37
- package/lib/cjs/function/arithmetic/nthRoot.js +1 -3
- package/lib/cjs/function/arithmetic/round.js +27 -47
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/plain/number/arithmetic.js +9 -27
- package/lib/cjs/type/matrix/function/sparse.js +6 -0
- package/lib/cjs/utils/lruQueue.js +1 -3
- package/lib/cjs/utils/object.js +3 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/entry/dependenciesAny/dependenciesCeil.generated.js +4 -0
- package/lib/esm/entry/dependenciesAny/dependenciesFix.generated.js +6 -0
- package/lib/esm/entry/dependenciesAny/dependenciesFloor.generated.js +4 -0
- package/lib/esm/entry/dependenciesAny/dependenciesIndexNode.generated.js +0 -2
- package/lib/esm/entry/dependenciesNumber/dependenciesCeil.generated.js +2 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesFix.generated.js +4 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesFloor.generated.js +2 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesIndexNode.generated.js +0 -2
- package/lib/esm/entry/impureFunctionsAny.generated.js +0 -1
- package/lib/esm/entry/impureFunctionsNumber.generated.js +7 -8
- package/lib/esm/entry/pureFunctionsAny.generated.js +10 -3
- package/lib/esm/entry/pureFunctionsNumber.generated.js +16 -10
- package/lib/esm/expression/node/BlockNode.js +3 -3
- package/lib/esm/expression/node/IndexNode.js +22 -59
- package/lib/esm/expression/parse.js +5 -5
- package/lib/esm/factoriesNumber.js +6 -6
- package/lib/esm/function/algebra/decomposition/qr.js +2 -2
- package/lib/esm/function/algebra/resolve.js +3 -3
- package/lib/esm/function/algebra/solver/utils/solveValidation.js +5 -5
- package/lib/esm/function/algebra/sparse/csChol.js +2 -2
- package/lib/esm/function/algebra/sparse/csLeaf.js +2 -2
- package/lib/esm/function/algebra/sparse/csLu.js +3 -3
- package/lib/esm/function/arithmetic/ceil.js +61 -24
- package/lib/esm/function/arithmetic/fix.js +51 -13
- package/lib/esm/function/arithmetic/floor.js +65 -23
- package/lib/esm/function/arithmetic/nthRoot.js +1 -3
- package/lib/esm/function/arithmetic/nthRoots.js +1 -1
- package/lib/esm/function/arithmetic/round.js +25 -43
- package/lib/esm/function/matrix/expm.js +2 -2
- package/lib/esm/function/probability/gamma.js +1 -1
- package/lib/esm/function/string/bin.js +1 -1
- package/lib/esm/function/string/hex.js +1 -1
- package/lib/esm/function/string/oct.js +1 -1
- package/lib/esm/plain/number/arithmetic.js +9 -17
- package/lib/esm/type/complex/Complex.js +2 -2
- package/lib/esm/type/matrix/DenseMatrix.js +2 -2
- package/lib/esm/type/matrix/FibonacciHeap.js +2 -2
- package/lib/esm/type/matrix/SparseMatrix.js +13 -13
- package/lib/esm/type/matrix/function/sparse.js +6 -0
- package/lib/esm/type/unit/Unit.js +7 -7
- package/lib/esm/utils/lruQueue.js +1 -2
- package/lib/esm/utils/number.js +3 -3
- package/lib/esm/utils/object.js +3 -1
- package/lib/esm/version.js +1 -1
- package/package.json +14 -3
- package/types/index.d.ts +1794 -1372
- package/types/index.ts +697 -266
package/types/index.ts
CHANGED
@@ -7,8 +7,9 @@ import {
|
|
7
7
|
addDependencies,
|
8
8
|
divideDependencies,
|
9
9
|
formatDependencies,
|
10
|
-
|
11
|
-
|
10
|
+
MathNode,
|
11
|
+
} from 'mathjs'
|
12
|
+
import * as assert from 'assert'
|
12
13
|
import { expectTypeOf } from 'expect-type'
|
13
14
|
|
14
15
|
// This file serves a dual purpose:
|
@@ -19,57 +20,73 @@ import { expectTypeOf } from 'expect-type'
|
|
19
20
|
Basic usage examples
|
20
21
|
*/
|
21
22
|
{
|
22
|
-
const math = create(all)
|
23
|
+
const math = create(all)
|
24
|
+
|
25
|
+
const m2by2 = [
|
26
|
+
[-1, 2],
|
27
|
+
[3, 1],
|
28
|
+
]
|
29
|
+
const m2by3 = [
|
30
|
+
[1, 2, 3],
|
31
|
+
[4, 5, 6],
|
32
|
+
]
|
23
33
|
|
24
34
|
// functions and constants
|
25
|
-
math.round(math.e, 3)
|
26
|
-
math.round(100.123, 3)
|
27
|
-
math.atan2(3, -3) / math.pi
|
28
|
-
math.log(10000, 10)
|
29
|
-
math.sqrt(-4)
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
math.round(math.e, 3)
|
36
|
+
math.round(100.123, 3)
|
37
|
+
math.atan2(3, -3) / math.pi
|
38
|
+
math.log(10000, 10)
|
39
|
+
math.sqrt(-4)
|
40
|
+
|
41
|
+
math.pow(m2by2, 2)
|
42
|
+
const angle = 0.2
|
43
|
+
math.add(math.pow(math.sin(angle), 2), math.pow(math.cos(angle), 2))
|
33
44
|
|
34
45
|
// std and variance check
|
46
|
+
|
35
47
|
math.std(1, 2, 3)
|
36
48
|
math.std([1, 2, 3])
|
37
|
-
math.std([1, 2, 3],
|
38
|
-
math.std([1,2, 3], 0,
|
39
|
-
math.std(
|
40
|
-
math.std(
|
49
|
+
math.std([1, 2, 3], 'biased')
|
50
|
+
math.std([1, 2, 3], 0, 'biased')
|
51
|
+
math.std(m2by3, 1, 'unbiased')
|
52
|
+
math.std(m2by3, 1, 'uncorrected')
|
41
53
|
math.variance(1, 2, 3)
|
42
54
|
math.variance([1, 2, 3])
|
43
|
-
math.variance([1, 2, 3],
|
44
|
-
math.variance([1,2, 3], 0,
|
45
|
-
math.variance(
|
46
|
-
math.variance(
|
55
|
+
math.variance([1, 2, 3], 'biased')
|
56
|
+
math.variance([1, 2, 3], 0, 'biased')
|
57
|
+
math.variance(m2by3, 1, 'unbiased')
|
58
|
+
math.variance(m2by3, 1, 'uncorrected')
|
47
59
|
|
48
60
|
// std and variance on chain
|
49
|
-
math.chain([1, 2, 3]).std(
|
50
|
-
math.chain(
|
51
|
-
math.chain(
|
52
|
-
math.chain([1, 2, 3]).std(
|
53
|
-
math.chain(
|
54
|
-
math.chain(
|
55
|
-
|
61
|
+
math.chain([1, 2, 3]).std('unbiased')
|
62
|
+
math.chain(m2by3).std(0, 'biased').std(0, 'uncorrected')
|
63
|
+
math.chain(m2by3).std(0, 'biased').std(0, 'uncorrected')
|
64
|
+
math.chain([1, 2, 3]).std('unbiased')
|
65
|
+
math.chain(m2by3).variance(0, 'biased')
|
66
|
+
math.chain(m2by3).variance(1, 'uncorrected').variance('unbiased')
|
56
67
|
|
57
68
|
// expressions
|
58
|
-
math.evaluate('1.2 * (2 + 4.5)')
|
69
|
+
math.evaluate('1.2 * (2 + 4.5)')
|
59
70
|
|
60
71
|
// chained operations
|
61
|
-
const a = math.chain(3).add(4).multiply(2).done()
|
62
|
-
assert.strictEqual(a, 14)
|
72
|
+
const a = math.chain(3).add(4).multiply(2).done()
|
73
|
+
assert.strictEqual(a, 14)
|
63
74
|
|
64
75
|
// mixed use of different data types in functions
|
65
|
-
assert.deepStrictEqual(math.add(4, [5, 6]), [9, 10])
|
66
|
-
assert.deepStrictEqual(
|
67
|
-
|
68
|
-
|
76
|
+
assert.deepStrictEqual(math.add(4, [5, 6]), [9, 10]) // number + Array
|
77
|
+
assert.deepStrictEqual(
|
78
|
+
math.multiply(math.unit('5 mm'), 3),
|
79
|
+
math.unit('15 mm')
|
80
|
+
) // Unit * number
|
81
|
+
assert.deepStrictEqual(math.subtract([2, 3, 4], 5), [-3, -2, -1]) // Array - number
|
82
|
+
assert.deepStrictEqual(
|
83
|
+
math.add(math.matrix([2, 3]), [4, 5]),
|
84
|
+
math.matrix([6, 8])
|
85
|
+
) // Matrix + Array
|
69
86
|
|
70
87
|
// narrowed type inference
|
71
|
-
const
|
72
|
-
const
|
88
|
+
const _b: math.Matrix = math.add(math.matrix([2]), math.matrix([3]))
|
89
|
+
const _c: math.Matrix = math.subtract(math.matrix([4]), math.matrix([5]))
|
73
90
|
}
|
74
91
|
|
75
92
|
/*
|
@@ -80,11 +97,17 @@ Bignumbers examples
|
|
80
97
|
const math = create(all, {
|
81
98
|
number: 'BigNumber',
|
82
99
|
precision: 20,
|
83
|
-
})
|
100
|
+
})
|
84
101
|
|
85
102
|
{
|
86
|
-
assert.deepStrictEqual(
|
87
|
-
|
103
|
+
assert.deepStrictEqual(
|
104
|
+
math.add(math.bignumber(0.1), math.bignumber(0.2)),
|
105
|
+
math.bignumber(0.3)
|
106
|
+
)
|
107
|
+
assert.deepStrictEqual(
|
108
|
+
math.divide(math.bignumber(0.3), math.bignumber(0.2)),
|
109
|
+
math.bignumber(1.5)
|
110
|
+
)
|
88
111
|
}
|
89
112
|
}
|
90
113
|
|
@@ -92,29 +115,29 @@ Bignumbers examples
|
|
92
115
|
Chaining examples
|
93
116
|
*/
|
94
117
|
{
|
95
|
-
const math = create(all, {})
|
96
|
-
const a = math.chain(3).add(4).multiply(2).done()
|
97
|
-
assert.strictEqual(a, 14)
|
118
|
+
const math = create(all, {})
|
119
|
+
const a = math.chain(3).add(4).multiply(2).done()
|
120
|
+
assert.strictEqual(a, 14)
|
98
121
|
|
99
122
|
// Another example, calculate square(sin(pi / 4))
|
100
|
-
const
|
123
|
+
const _b = math.chain(math.pi).divide(4).sin().square().done()
|
101
124
|
|
102
125
|
// toString will return a string representation of the chain's value
|
103
|
-
const chain = math.chain(2).divide(3)
|
104
|
-
const str: string = chain.toString()
|
105
|
-
assert.strictEqual(str,
|
126
|
+
const chain = math.chain(2).divide(3)
|
127
|
+
const str: string = chain.toString()
|
128
|
+
assert.strictEqual(str, '0.6666666666666666')
|
106
129
|
|
107
|
-
chain.valueOf()
|
130
|
+
chain.valueOf()
|
108
131
|
|
109
132
|
// the function subset can be used to get or replace sub matrices
|
110
133
|
const array = [
|
111
134
|
[1, 2],
|
112
135
|
[3, 4],
|
113
|
-
]
|
114
|
-
const v = math.chain(array).subset(math.index(1, 0)).done()
|
115
|
-
assert.strictEqual(v, 3)
|
136
|
+
]
|
137
|
+
const v = math.chain(array).subset(math.index(1, 0)).done()
|
138
|
+
assert.strictEqual(v, 3)
|
116
139
|
|
117
|
-
const
|
140
|
+
const _m = math.chain(array).subset(math.index(0, 0), 8).multiply(3).done()
|
118
141
|
|
119
142
|
// filtering
|
120
143
|
assert.deepStrictEqual(
|
@@ -125,64 +148,74 @@ Chaining examples
|
|
125
148
|
.filter((n) => n !== 1000)
|
126
149
|
.done(),
|
127
150
|
[2, 3]
|
128
|
-
)
|
151
|
+
)
|
152
|
+
|
153
|
+
const r = math.chain(-0.483).round([0, 1, 2]).floor().add(0.52).fix(1).done()
|
154
|
+
assert.deepStrictEqual(r, [0.5, -0.4, -0.4])
|
155
|
+
|
156
|
+
expectTypeOf(math.chain('x + y').parse().resolve({ x: 1 }).done())
|
157
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
158
|
+
.toMatchTypeOf<any>()
|
159
|
+
expectTypeOf(math.chain('x + y').parse().resolve().done())
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
161
|
+
.toMatchTypeOf<any>()
|
129
162
|
}
|
130
163
|
|
131
164
|
/*
|
132
165
|
Simplify examples
|
133
166
|
*/
|
134
167
|
{
|
135
|
-
const math = create(all)
|
168
|
+
const math = create(all)
|
136
169
|
|
137
|
-
math.simplify(
|
138
|
-
math.simplify(
|
170
|
+
math.simplify('2 * 1 * x ^ (2 - 1)')
|
171
|
+
math.simplify('2 * 3 * x', { x: 4 })
|
139
172
|
|
140
|
-
const f = math.parse(
|
141
|
-
math.simplify(f)
|
173
|
+
const f = math.parse('2 * 1 * x ^ (2 - 1)')
|
174
|
+
math.simplify(f)
|
142
175
|
|
143
|
-
math.simplify(
|
144
|
-
math.simplify(
|
176
|
+
math.simplify('0.4 * x', {}, { exactFractions: true })
|
177
|
+
math.simplify('0.4 * x', {}, { exactFractions: false })
|
145
178
|
}
|
146
179
|
|
147
180
|
/*
|
148
181
|
Complex numbers examples
|
149
182
|
*/
|
150
183
|
{
|
151
|
-
const math = create(all, {})
|
152
|
-
const a = math.complex(2, 3)
|
184
|
+
const math = create(all, {})
|
185
|
+
const a = math.complex(2, 3)
|
153
186
|
// create a complex number by providing a string with real and complex parts
|
154
|
-
const b = math.complex('3 - 7i')
|
187
|
+
const b = math.complex('3 - 7i')
|
155
188
|
|
156
189
|
// read the real and complex parts of the complex number
|
157
190
|
{
|
158
|
-
const
|
159
|
-
const
|
191
|
+
const _x: number = a.re
|
192
|
+
const _y: number = a.im
|
160
193
|
|
161
194
|
// adjust the complex value
|
162
|
-
a.re = 5
|
195
|
+
a.re = 5
|
163
196
|
}
|
164
197
|
|
165
198
|
// clone a complex value
|
166
199
|
{
|
167
|
-
const
|
200
|
+
const _clone = a.clone()
|
168
201
|
}
|
169
202
|
|
170
203
|
// perform operations with complex numbers
|
171
204
|
{
|
172
|
-
math.add(a, b)
|
173
|
-
math.multiply(a, b)
|
174
|
-
math.sin(a)
|
205
|
+
math.add(a, b)
|
206
|
+
math.multiply(a, b)
|
207
|
+
math.sin(a)
|
175
208
|
}
|
176
209
|
|
177
210
|
// create a complex number from polar coordinates
|
178
211
|
{
|
179
|
-
const p: math.PolarCoordinates = { r: math.sqrt(2), phi: math.pi / 4 }
|
180
|
-
const
|
212
|
+
const p: math.PolarCoordinates = { r: math.sqrt(2), phi: math.pi / 4 }
|
213
|
+
const _c: math.Complex = math.complex(p)
|
181
214
|
}
|
182
215
|
|
183
216
|
// get polar coordinates of a complex number
|
184
217
|
{
|
185
|
-
const
|
218
|
+
const _p: math.PolarCoordinates = math.complex(3, 4).toPolar()
|
186
219
|
}
|
187
220
|
}
|
188
221
|
|
@@ -190,78 +223,79 @@ Complex numbers examples
|
|
190
223
|
Expressions examples
|
191
224
|
*/
|
192
225
|
{
|
193
|
-
const math = create(all, {})
|
226
|
+
const math = create(all, {})
|
194
227
|
// evaluate expressions
|
195
228
|
{
|
196
|
-
math.evaluate('sqrt(3^2 + 4^2)')
|
229
|
+
math.evaluate('sqrt(3^2 + 4^2)')
|
197
230
|
}
|
198
231
|
|
199
232
|
// evaluate multiple expressions at once
|
200
233
|
{
|
201
|
-
math.evaluate(['f = 3', 'g = 4', 'f * g'])
|
234
|
+
math.evaluate(['f = 3', 'g = 4', 'f * g'])
|
202
235
|
}
|
203
236
|
|
204
237
|
// get content of a parenthesis node
|
205
238
|
{
|
206
|
-
const node = math.parse('(1)')
|
239
|
+
const node = math.parse('(1)')
|
207
240
|
if (node.type !== 'ParenthesisNode') {
|
208
|
-
throw Error(`expected ParenthesisNode, got ${node.type}`)
|
241
|
+
throw Error(`expected ParenthesisNode, got ${node.type}`)
|
209
242
|
}
|
210
|
-
const
|
243
|
+
const _innerNode = node.content
|
211
244
|
}
|
212
245
|
|
213
246
|
// scope can contain both variables and functions
|
214
247
|
{
|
215
|
-
const scope = { hello: (name: string) => `hello, ${name}!` }
|
216
|
-
assert.strictEqual(math.evaluate('hello("hero")', scope),
|
248
|
+
const scope = { hello: (name: string) => `hello, ${name}!` }
|
249
|
+
assert.strictEqual(math.evaluate('hello("hero")', scope), 'hello, hero!')
|
217
250
|
}
|
218
251
|
|
219
252
|
// define a function as an expression
|
220
253
|
{
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
221
255
|
const scope: any = {
|
222
256
|
a: 3,
|
223
257
|
b: 4,
|
224
|
-
}
|
225
|
-
const f = math.evaluate('f(x) = x ^ a', scope)
|
226
|
-
f(2)
|
227
|
-
scope.f(2)
|
258
|
+
}
|
259
|
+
const f = math.evaluate('f(x) = x ^ a', scope)
|
260
|
+
f(2)
|
261
|
+
scope.f(2)
|
228
262
|
}
|
229
263
|
|
230
264
|
{
|
231
|
-
const node2 = math.parse('x^a')
|
232
|
-
const
|
233
|
-
node2.toString()
|
265
|
+
const node2 = math.parse('x^a')
|
266
|
+
const _code2: math.EvalFunction = node2.compile()
|
267
|
+
node2.toString()
|
234
268
|
}
|
235
269
|
|
236
270
|
// 3. using function math.compile
|
237
271
|
// parse an expression
|
238
272
|
{
|
239
273
|
// provide a scope for the variable assignment
|
240
|
-
const code2 = math.compile('a = a + 3')
|
241
|
-
const scope = { a: 7 }
|
242
|
-
code2.evaluate(scope)
|
274
|
+
const code2 = math.compile('a = a + 3')
|
275
|
+
const scope = { a: 7 }
|
276
|
+
code2.evaluate(scope)
|
243
277
|
}
|
244
278
|
// 4. using a parser
|
245
|
-
const parser = math.parser()
|
279
|
+
const parser = math.parser()
|
246
280
|
|
247
281
|
// get and set variables and functions
|
248
282
|
{
|
249
|
-
assert.strictEqual(parser.evaluate('x = 7 / 2'), 3.5)
|
250
|
-
assert.strictEqual(parser.evaluate('x + 3'), 6.5)
|
251
|
-
parser.evaluate('f(x, y) = x^y')
|
252
|
-
assert.strictEqual(parser.evaluate('f(2, 3)'), 8)
|
253
|
-
|
254
|
-
const
|
255
|
-
const f = parser.get('f')
|
256
|
-
const
|
257
|
-
const
|
258
|
-
|
259
|
-
parser.set('h', 500)
|
260
|
-
parser.set('hello', (name: string) => `hello, ${name}!`)
|
283
|
+
assert.strictEqual(parser.evaluate('x = 7 / 2'), 3.5)
|
284
|
+
assert.strictEqual(parser.evaluate('x + 3'), 6.5)
|
285
|
+
parser.evaluate('f(x, y) = x^y') // f(x, y)
|
286
|
+
assert.strictEqual(parser.evaluate('f(2, 3)'), 8)
|
287
|
+
|
288
|
+
const _x = parser.get('x')
|
289
|
+
const f = parser.get('f')
|
290
|
+
const _y = parser.getAll()
|
291
|
+
const _g = f(3, 3)
|
292
|
+
|
293
|
+
parser.set('h', 500)
|
294
|
+
parser.set('hello', (name: string) => `hello, ${name}!`)
|
261
295
|
}
|
262
296
|
|
263
297
|
// clear defined functions and variables
|
264
|
-
parser.clear()
|
298
|
+
parser.clear()
|
265
299
|
}
|
266
300
|
|
267
301
|
/*
|
@@ -271,114 +305,122 @@ Fractions examples
|
|
271
305
|
// configure the default type of numbers as Fractions
|
272
306
|
const math = create(all, {
|
273
307
|
number: 'Fraction',
|
274
|
-
})
|
308
|
+
})
|
275
309
|
|
276
|
-
const x = math.fraction(0.125)
|
277
|
-
const y = math.fraction('1/3')
|
278
|
-
math.fraction(2, 3)
|
310
|
+
const x = math.fraction(0.125)
|
311
|
+
const y = math.fraction('1/3')
|
312
|
+
math.fraction(2, 3)
|
279
313
|
|
280
|
-
math.add(x, y)
|
281
|
-
math.divide(x, y)
|
314
|
+
math.add(x, y)
|
315
|
+
math.divide(x, y)
|
282
316
|
|
283
317
|
// output formatting
|
284
|
-
const
|
318
|
+
const _a = math.fraction('2/3')
|
285
319
|
}
|
286
320
|
|
287
321
|
/*
|
288
322
|
Matrices examples
|
289
323
|
*/
|
290
324
|
{
|
291
|
-
const math = create(all, {})
|
325
|
+
const math = create(all, {})
|
292
326
|
|
293
327
|
// create matrices and arrays. a matrix is just a wrapper around an Array,
|
294
328
|
// providing some handy utilities.
|
295
|
-
const a: math.Matrix = math.matrix([1, 4, 9, 16, 25])
|
296
|
-
const b: math.Matrix = math.matrix(math.ones([2, 3]))
|
297
|
-
b.size()
|
329
|
+
const a: math.Matrix = math.matrix([1, 4, 9, 16, 25])
|
330
|
+
const b: math.Matrix = math.matrix(math.ones([2, 3]))
|
331
|
+
b.size()
|
298
332
|
|
299
333
|
// the Array data of a Matrix can be retrieved using valueOf()
|
300
|
-
const
|
334
|
+
const _array = a.valueOf()
|
301
335
|
|
302
336
|
// Matrices can be cloned
|
303
|
-
const
|
337
|
+
const _clone: math.Matrix = a.clone()
|
304
338
|
|
305
339
|
// perform operations with matrices
|
306
|
-
math.sqrt(a)
|
307
|
-
math.factorial(a)
|
340
|
+
math.sqrt(a)
|
341
|
+
math.factorial(a)
|
308
342
|
|
309
343
|
// create and manipulate matrices. Arrays and Matrices can be used mixed.
|
310
344
|
{
|
311
345
|
const a = [
|
312
346
|
[1, 2],
|
313
347
|
[3, 4],
|
314
|
-
]
|
348
|
+
]
|
315
349
|
const b: math.Matrix = math.matrix([
|
316
350
|
[5, 6],
|
317
351
|
[1, 1],
|
318
|
-
])
|
352
|
+
])
|
319
353
|
|
320
|
-
b.subset(math.index(1, [0, 1]), [[7, 8]])
|
321
|
-
const
|
322
|
-
const f: math.Matrix = math.matrix([1, 0])
|
323
|
-
const
|
354
|
+
b.subset(math.index(1, [0, 1]), [[7, 8]])
|
355
|
+
const _c = math.multiply(a, b)
|
356
|
+
const f: math.Matrix = math.matrix([1, 0])
|
357
|
+
const _d: math.Matrix = f.subset(math.index(1))
|
324
358
|
}
|
325
359
|
|
326
360
|
// get a sub matrix
|
327
361
|
{
|
328
|
-
const a: math.Matrix = math.diag(math.range(1, 4))
|
329
|
-
a.subset(math.index([1, 2], [1, 2]))
|
330
|
-
const b: math.Matrix = math.range(1, 6)
|
331
|
-
b.subset(math.index(math.range(1, 4)))
|
362
|
+
const a: math.Matrix = math.diag(math.range(1, 4))
|
363
|
+
a.subset(math.index([1, 2], [1, 2]))
|
364
|
+
const b: math.Matrix = math.range(1, 6)
|
365
|
+
b.subset(math.index(math.range(1, 4)))
|
332
366
|
}
|
333
367
|
|
334
368
|
// resize a multi dimensional matrix
|
335
369
|
{
|
336
|
-
const a = math.matrix()
|
337
|
-
a.resize([2, 2, 2], 0)
|
338
|
-
a.size()
|
339
|
-
a.resize([2, 2])
|
340
|
-
a.size()
|
370
|
+
const a = math.matrix()
|
371
|
+
a.resize([2, 2, 2], 0)
|
372
|
+
a.size()
|
373
|
+
a.resize([2, 2])
|
374
|
+
a.size()
|
341
375
|
}
|
342
376
|
|
343
377
|
// can set a subset of a matrix to uninitialized
|
344
378
|
{
|
345
|
-
const m = math.matrix()
|
346
|
-
m.subset(math.index(2), 6, math.uninitialized)
|
379
|
+
const m = math.matrix()
|
380
|
+
m.subset(math.index(2), 6, math.uninitialized)
|
347
381
|
}
|
348
382
|
|
349
383
|
// create ranges
|
350
384
|
{
|
351
|
-
math.range(1, 6)
|
352
|
-
math.range(0, 18, 3)
|
353
|
-
math.range('2:-1:-3')
|
354
|
-
math.factorial(math.range('1:6'))
|
385
|
+
math.range(1, 6)
|
386
|
+
math.range(0, 18, 3)
|
387
|
+
math.range('2:-1:-3')
|
388
|
+
math.factorial(math.range('1:6'))
|
355
389
|
}
|
356
390
|
|
357
391
|
// map matrix
|
358
392
|
{
|
359
393
|
assert.deepStrictEqual(
|
360
394
|
math.map([1, 2, 3], function (value) {
|
361
|
-
return value * value
|
395
|
+
return value * value
|
362
396
|
}),
|
363
397
|
[1, 4, 9]
|
364
|
-
)
|
398
|
+
)
|
365
399
|
}
|
366
400
|
|
367
401
|
// filter matrix
|
368
402
|
{
|
369
403
|
assert.deepStrictEqual(
|
370
404
|
math.filter([6, -2, -1, 4, 3], function (x) {
|
371
|
-
return x > 0
|
405
|
+
return x > 0
|
372
406
|
}),
|
373
407
|
[6, 4, 3]
|
374
408
|
)
|
375
|
-
assert.deepStrictEqual(
|
409
|
+
assert.deepStrictEqual(
|
410
|
+
math.filter(['23', 'foo', '100', '55', 'bar'], /[0-9]+/),
|
411
|
+
['23', '100', '55']
|
412
|
+
)
|
376
413
|
}
|
377
414
|
|
378
415
|
// concat matrix
|
379
416
|
{
|
380
|
-
assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]]), [
|
381
|
-
|
417
|
+
assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]]), [
|
418
|
+
[0, 1, 2, 1, 2, 3],
|
419
|
+
])
|
420
|
+
assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]], 0), [
|
421
|
+
[0, 1, 2],
|
422
|
+
[1, 2, 3],
|
423
|
+
])
|
382
424
|
}
|
383
425
|
|
384
426
|
// Matrix is available as a constructor for instanceof checks
|
@@ -391,39 +433,50 @@ Matrices examples
|
|
391
433
|
Sparse matrices examples
|
392
434
|
*/
|
393
435
|
{
|
394
|
-
const math = create(all, {})
|
436
|
+
const math = create(all, {})
|
395
437
|
|
396
438
|
// create a sparse matrix
|
397
|
-
const a = math.identity(1000, 1000, 'sparse')
|
439
|
+
const a = math.identity(1000, 1000, 'sparse')
|
398
440
|
|
399
441
|
// do operations with a sparse matrix
|
400
|
-
const b = math.multiply(a, a)
|
401
|
-
const
|
402
|
-
const d = math.matrix([0, 1])
|
403
|
-
const e = math.transpose(d)
|
404
|
-
const
|
442
|
+
const b = math.multiply(a, a)
|
443
|
+
const _c = math.multiply(b, math.complex(2, 2))
|
444
|
+
const d = math.matrix([0, 1])
|
445
|
+
const e = math.transpose(d)
|
446
|
+
const _f = math.multiply(e, d)
|
405
447
|
}
|
406
448
|
|
407
449
|
/*
|
408
450
|
Units examples
|
409
451
|
*/
|
410
452
|
{
|
411
|
-
const math = create(all, {})
|
453
|
+
const math = create(all, {})
|
412
454
|
|
413
455
|
// units can be created by providing a value and unit name, or by providing
|
414
456
|
// a string with a valued unit.
|
415
|
-
const a = math.unit(45, 'cm')
|
416
|
-
const b = math.unit('0.1m')
|
417
|
-
const
|
457
|
+
const a = math.unit(45, 'cm') // 450 mm
|
458
|
+
const b = math.unit('0.1m') // 100 mm
|
459
|
+
const _c = math.unit(b)
|
418
460
|
|
419
461
|
// creating units
|
420
|
-
math.createUnit('foo')
|
421
|
-
math.createUnit('furlong', '220 yards')
|
422
|
-
math.createUnit('furlong', '220 yards', { override: true })
|
423
|
-
math.createUnit('testunit', { definition: '0.555556 kelvin', offset: 459.67 })
|
424
|
-
math.createUnit(
|
425
|
-
|
426
|
-
|
462
|
+
math.createUnit('foo')
|
463
|
+
math.createUnit('furlong', '220 yards')
|
464
|
+
math.createUnit('furlong', '220 yards', { override: true })
|
465
|
+
math.createUnit('testunit', { definition: '0.555556 kelvin', offset: 459.67 })
|
466
|
+
math.createUnit(
|
467
|
+
'testunit',
|
468
|
+
{ definition: '0.555556 kelvin', offset: 459.67 },
|
469
|
+
{ override: true }
|
470
|
+
)
|
471
|
+
math.createUnit('knot', {
|
472
|
+
definition: '0.514444 m/s',
|
473
|
+
aliases: ['knots', 'kt', 'kts'],
|
474
|
+
})
|
475
|
+
math.createUnit(
|
476
|
+
'knot',
|
477
|
+
{ definition: '0.514444 m/s', aliases: ['knots', 'kt', 'kts'] },
|
478
|
+
{ override: true }
|
479
|
+
)
|
427
480
|
math.createUnit(
|
428
481
|
'knot',
|
429
482
|
{
|
@@ -432,7 +485,7 @@ Units examples
|
|
432
485
|
prefixes: 'long',
|
433
486
|
},
|
434
487
|
{ override: true }
|
435
|
-
)
|
488
|
+
)
|
436
489
|
math.createUnit(
|
437
490
|
{
|
438
491
|
foo2: {
|
@@ -447,99 +500,381 @@ Units examples
|
|
447
500
|
{
|
448
501
|
override: true,
|
449
502
|
}
|
450
|
-
)
|
503
|
+
)
|
451
504
|
// use Unit as definition
|
452
|
-
math.createUnit('c', { definition: b })
|
453
|
-
math.createUnit('c', { definition: b }, { override: true })
|
505
|
+
math.createUnit('c', { definition: b })
|
506
|
+
math.createUnit('c', { definition: b }, { override: true })
|
454
507
|
|
455
508
|
// units can be added, subtracted, and multiplied or divided by numbers and by other units
|
456
|
-
math.add(a, b)
|
457
|
-
math.multiply(b, 2)
|
458
|
-
math.divide(math.unit('1 m'), math.unit('1 s'))
|
459
|
-
math.pow(math.unit('12 in'), 3)
|
509
|
+
math.add(a, b)
|
510
|
+
math.multiply(b, 2)
|
511
|
+
math.divide(math.unit('1 m'), math.unit('1 s'))
|
512
|
+
math.pow(math.unit('12 in'), 3)
|
460
513
|
|
461
514
|
// units can be converted to a specific type, or to a number
|
462
|
-
b.to('cm')
|
463
|
-
math.to(b, 'inch')
|
464
|
-
b.toNumber('cm')
|
465
|
-
math.number(b, 'cm')
|
515
|
+
b.to('cm')
|
516
|
+
math.to(b, 'inch')
|
517
|
+
b.toNumber('cm')
|
518
|
+
math.number(b, 'cm')
|
466
519
|
|
467
520
|
// the expression parser supports units too
|
468
|
-
math.evaluate('2 inch to cm')
|
521
|
+
math.evaluate('2 inch to cm')
|
469
522
|
|
470
523
|
// units can be converted to SI
|
471
|
-
math.unit('1 inch').toSI()
|
524
|
+
math.unit('1 inch').toSI()
|
472
525
|
|
473
526
|
// units can be split into other units
|
474
|
-
math.unit('1 m').splitUnit(['ft', 'in'])
|
527
|
+
math.unit('1 m').splitUnit(['ft', 'in'])
|
475
528
|
}
|
476
529
|
|
477
530
|
/*
|
478
531
|
Expression tree examples
|
479
532
|
*/
|
480
533
|
{
|
481
|
-
const math = create(all, {})
|
534
|
+
const math = create(all, {})
|
482
535
|
|
483
536
|
// Filter an expression tree
|
484
|
-
const node: math.MathNode = math.parse('x^2 + x/4 + 3*y')
|
485
|
-
const filtered: math.MathNode[] = node.filter(
|
537
|
+
const node: math.MathNode = math.parse('x^2 + x/4 + 3*y')
|
538
|
+
const filtered: math.MathNode[] = node.filter(
|
539
|
+
(node: math.MathNode) => node.type === 'SymbolNode' && node.name === 'x'
|
540
|
+
)
|
486
541
|
|
487
|
-
const
|
542
|
+
const _arr: string[] = filtered.map((node: math.MathNode) => node.toString())
|
488
543
|
|
489
544
|
// Traverse an expression tree
|
490
|
-
const node1: math.MathNode = math.parse('3 * x + 2')
|
491
|
-
node1.traverse(
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
545
|
+
const node1: math.MathNode = math.parse('3 * x + 2')
|
546
|
+
node1.traverse(
|
547
|
+
(node: math.MathNode, _path: string, _parent: math.MathNode) => {
|
548
|
+
switch (node.type) {
|
549
|
+
case 'OperatorNode':
|
550
|
+
return node.type === 'OperatorNode'
|
551
|
+
case 'ConstantNode':
|
552
|
+
return node.type === 'ConstantNode'
|
553
|
+
case 'SymbolNode':
|
554
|
+
return node.type === 'SymbolNode'
|
555
|
+
default:
|
556
|
+
return
|
557
|
+
}
|
501
558
|
}
|
502
|
-
|
559
|
+
)
|
560
|
+
}
|
561
|
+
|
562
|
+
/*
|
563
|
+
Function ceil examples
|
564
|
+
*/
|
565
|
+
{
|
566
|
+
const math = create(all, {})
|
567
|
+
|
568
|
+
// number input
|
569
|
+
assert.strictEqual(math.ceil(3.2), 4)
|
570
|
+
assert.strictEqual(math.ceil(-4.2), -4)
|
571
|
+
|
572
|
+
// number input
|
573
|
+
// roundoff result to 2 decimals
|
574
|
+
assert.strictEqual(math.ceil(3.212, 2), 3.22)
|
575
|
+
assert.deepStrictEqual(
|
576
|
+
math.ceil(3.212, math.bignumber(2)),
|
577
|
+
math.bignumber(3.22)
|
578
|
+
)
|
579
|
+
assert.strictEqual(math.ceil(-4.212, 2), -4.21)
|
580
|
+
|
581
|
+
// bignumber input
|
582
|
+
assert.deepStrictEqual(math.ceil(math.bignumber(3.212)), math.bignumber(4))
|
583
|
+
assert.deepStrictEqual(
|
584
|
+
math.ceil(math.bignumber(3.212), 2),
|
585
|
+
math.bignumber(3.22)
|
586
|
+
)
|
587
|
+
assert.deepStrictEqual(
|
588
|
+
math.ceil(math.bignumber(3.212), math.bignumber(2)),
|
589
|
+
math.bignumber(3.22)
|
590
|
+
)
|
591
|
+
|
592
|
+
// fraction input
|
593
|
+
assert.deepStrictEqual(math.ceil(math.fraction(44, 7)), math.fraction(7))
|
594
|
+
assert.deepStrictEqual(math.ceil(math.fraction(-44, 7)), math.fraction(-6))
|
595
|
+
assert.deepStrictEqual(
|
596
|
+
math.ceil(math.fraction(44, 7), 2),
|
597
|
+
math.fraction(629, 100)
|
598
|
+
)
|
599
|
+
assert.deepStrictEqual(
|
600
|
+
math.ceil(math.fraction(44, 7), math.bignumber(2)),
|
601
|
+
math.fraction(629, 100)
|
602
|
+
)
|
603
|
+
|
604
|
+
// Complex input
|
605
|
+
const c = math.complex(3.24, -2.71)
|
606
|
+
assert.deepStrictEqual(math.ceil(c), math.complex(4, -2))
|
607
|
+
assert.deepStrictEqual(math.ceil(c, 1), math.complex(3.3, -2.7))
|
608
|
+
assert.deepStrictEqual(
|
609
|
+
math.ceil(c, math.bignumber(1)),
|
610
|
+
math.complex(3.3, -2.7)
|
611
|
+
)
|
612
|
+
|
613
|
+
// array input
|
614
|
+
assert.deepStrictEqual(math.ceil([3.2, 3.8, -4.7]), [4, 4, -4])
|
615
|
+
assert.deepStrictEqual(math.ceil([3.21, 3.82, -4.71], 1), [3.3, 3.9, -4.7])
|
616
|
+
assert.deepStrictEqual(
|
617
|
+
math.ceil([3.21, 3.82, -4.71], math.bignumber(1)),
|
618
|
+
math.bignumber([3.3, 3.9, -4.7])
|
619
|
+
)
|
620
|
+
|
621
|
+
// numeric input, array or matrix of decimals
|
622
|
+
const numCeiled: math.MathArray = math.ceil(math.tau, [2, 3])
|
623
|
+
assert.deepStrictEqual(numCeiled, [6.29, 6.284])
|
624
|
+
const bigCeiled: math.Matrix = math.ceil(
|
625
|
+
math.bignumber(6.28318),
|
626
|
+
math.matrix([2, 3])
|
627
|
+
)
|
628
|
+
assert.deepStrictEqual(bigCeiled, math.matrix(math.bignumber([6.29, 6.284])))
|
629
|
+
assert.deepStrictEqual(math.ceil(math.fraction(44, 7), [2, 3]), [
|
630
|
+
math.fraction(629, 100),
|
631
|
+
math.fraction(6286, 1000),
|
632
|
+
])
|
633
|
+
|
634
|
+
// @ts-expect-error ... verify ceil(array, array) throws an error (for now)
|
635
|
+
assert.throws(() => math.ceil([3.21, 3.82], [1, 2]), TypeError)
|
636
|
+
}
|
637
|
+
|
638
|
+
/*
|
639
|
+
Function fix examples
|
640
|
+
*/
|
641
|
+
{
|
642
|
+
const math = create(all, {})
|
643
|
+
|
644
|
+
// number input
|
645
|
+
assert.strictEqual(math.fix(3.2), 3)
|
646
|
+
assert.strictEqual(math.fix(-4.2), -4)
|
647
|
+
|
648
|
+
// number input
|
649
|
+
// roundoff result to 2 decimals
|
650
|
+
assert.strictEqual(math.fix(3.212, 2), 3.21)
|
651
|
+
assert.deepStrictEqual(
|
652
|
+
math.fix(3.212, math.bignumber(2)),
|
653
|
+
math.bignumber(3.21)
|
654
|
+
)
|
655
|
+
assert.strictEqual(math.fix(-4.212, 2), -4.21)
|
656
|
+
|
657
|
+
// bignumber input
|
658
|
+
assert.deepStrictEqual(math.fix(math.bignumber(3.212)), math.bignumber(3))
|
659
|
+
assert.deepStrictEqual(
|
660
|
+
math.fix(math.bignumber(3.212), 2),
|
661
|
+
math.bignumber(3.21)
|
662
|
+
)
|
663
|
+
assert.deepStrictEqual(
|
664
|
+
math.fix(math.bignumber(3.212), math.bignumber(2)),
|
665
|
+
math.bignumber(3.21)
|
666
|
+
)
|
667
|
+
|
668
|
+
// fraction input
|
669
|
+
assert.deepStrictEqual(math.fix(math.fraction(44, 7)), math.fraction(6))
|
670
|
+
assert.deepStrictEqual(math.fix(math.fraction(-44, 7)), math.fraction(-6))
|
671
|
+
assert.deepStrictEqual(
|
672
|
+
math.fix(math.fraction(44, 7), 2),
|
673
|
+
math.fraction(628, 100)
|
674
|
+
)
|
675
|
+
assert.deepStrictEqual(
|
676
|
+
math.fix(math.fraction(44, 7), math.bignumber(2)),
|
677
|
+
math.fraction(628, 100)
|
678
|
+
)
|
679
|
+
|
680
|
+
// Complex input
|
681
|
+
const c = math.complex(3.24, -2.71)
|
682
|
+
assert.deepStrictEqual(math.fix(c), math.complex(3, -2))
|
683
|
+
assert.deepStrictEqual(math.fix(c, 1), math.complex(3.2, -2.7))
|
684
|
+
assert.deepStrictEqual(
|
685
|
+
math.fix(c, math.bignumber(1)),
|
686
|
+
math.complex(3.2, -2.7)
|
687
|
+
)
|
688
|
+
|
689
|
+
// array input
|
690
|
+
assert.deepStrictEqual(math.fix([3.2, 3.8, -4.7]), [3, 3, -4])
|
691
|
+
assert.deepStrictEqual(math.fix([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.7])
|
692
|
+
assert.deepStrictEqual(
|
693
|
+
math.fix([3.21, 3.82, -4.71], math.bignumber(1)),
|
694
|
+
math.bignumber([3.2, 3.8, -4.7])
|
695
|
+
)
|
696
|
+
|
697
|
+
// numeric input, array or matrix of decimals
|
698
|
+
const numFixed: math.MathArray = math.fix(math.tau, [2, 3])
|
699
|
+
assert.deepStrictEqual(numFixed, [6.28, 6.283])
|
700
|
+
const bigFixed: math.Matrix = math.fix(
|
701
|
+
math.bignumber(6.28318),
|
702
|
+
math.matrix([2, 3])
|
703
|
+
)
|
704
|
+
assert.deepStrictEqual(bigFixed, math.matrix(math.bignumber([6.28, 6.283])))
|
705
|
+
assert.deepStrictEqual(math.fix(math.fraction(44, 7), [2, 3]), [
|
706
|
+
math.fraction(628, 100),
|
707
|
+
math.fraction(6285, 1000),
|
708
|
+
])
|
709
|
+
|
710
|
+
// @ts-expect-error ... verify fix(array, array) throws an error (for now)
|
711
|
+
assert.throws(() => math.fix([3.21, 3.82], [1, 2]), TypeError)
|
503
712
|
}
|
504
713
|
|
505
714
|
/*
|
506
715
|
Function floor examples
|
507
716
|
*/
|
508
717
|
{
|
509
|
-
const math = create(all, {})
|
718
|
+
const math = create(all, {})
|
510
719
|
|
511
720
|
// number input
|
512
|
-
assert.strictEqual(math.floor(3.2), 3)
|
513
|
-
assert.strictEqual(math.floor(-4.2), -5)
|
721
|
+
assert.strictEqual(math.floor(3.2), 3)
|
722
|
+
assert.strictEqual(math.floor(-4.2), -5)
|
514
723
|
|
515
724
|
// number input
|
516
725
|
// roundoff result to 2 decimals
|
517
|
-
assert.strictEqual(math.floor(3.212, 2), 3.21)
|
518
|
-
assert.
|
726
|
+
assert.strictEqual(math.floor(3.212, 2), 3.21)
|
727
|
+
assert.deepStrictEqual(
|
728
|
+
math.floor(3.212, math.bignumber(2)),
|
729
|
+
math.bignumber(3.21)
|
730
|
+
)
|
731
|
+
assert.strictEqual(math.floor(-4.212, 2), -4.22)
|
732
|
+
|
733
|
+
// bignumber input
|
734
|
+
assert.deepStrictEqual(math.floor(math.bignumber(3.212)), math.bignumber(3))
|
735
|
+
assert.deepStrictEqual(
|
736
|
+
math.floor(math.bignumber(3.212), 2),
|
737
|
+
math.bignumber(3.21)
|
738
|
+
)
|
739
|
+
assert.deepStrictEqual(
|
740
|
+
math.floor(math.bignumber(3.212), math.bignumber(2)),
|
741
|
+
math.bignumber(3.21)
|
742
|
+
)
|
743
|
+
|
744
|
+
// fraction input
|
745
|
+
assert.deepStrictEqual(math.floor(math.fraction(44, 7)), math.fraction(6))
|
746
|
+
assert.deepStrictEqual(math.floor(math.fraction(-44, 7)), math.fraction(-7))
|
747
|
+
assert.deepStrictEqual(
|
748
|
+
math.floor(math.fraction(44, 7), 2),
|
749
|
+
math.fraction(628, 100)
|
750
|
+
)
|
751
|
+
assert.deepStrictEqual(
|
752
|
+
math.floor(math.fraction(44, 7), math.bignumber(2)),
|
753
|
+
math.fraction(628, 100)
|
754
|
+
)
|
519
755
|
|
520
756
|
// Complex input
|
521
|
-
const c = math.complex(3.24, -2.71)
|
522
|
-
assert.deepStrictEqual(math.floor(c), math.complex(3, -3))
|
523
|
-
assert.deepStrictEqual(math.floor(c, 1), math.complex(3.2, -2.8))
|
757
|
+
const c = math.complex(3.24, -2.71)
|
758
|
+
assert.deepStrictEqual(math.floor(c), math.complex(3, -3))
|
759
|
+
assert.deepStrictEqual(math.floor(c, 1), math.complex(3.2, -2.8))
|
760
|
+
assert.deepStrictEqual(
|
761
|
+
math.floor(c, math.bignumber(1)),
|
762
|
+
math.complex(3.2, -2.8)
|
763
|
+
)
|
524
764
|
|
525
|
-
//array input
|
526
|
-
assert.deepStrictEqual(math.floor([3.2, 3.8, -4.7]), [3, 3, -5])
|
527
|
-
assert.deepStrictEqual(math.floor([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.8])
|
765
|
+
// array input
|
766
|
+
assert.deepStrictEqual(math.floor([3.2, 3.8, -4.7]), [3, 3, -5])
|
767
|
+
assert.deepStrictEqual(math.floor([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.8])
|
768
|
+
assert.deepStrictEqual(
|
769
|
+
math.floor([3.21, 3.82, -4.71], math.bignumber(1)),
|
770
|
+
math.bignumber([3.2, 3.8, -4.8])
|
771
|
+
)
|
772
|
+
|
773
|
+
// numeric input, array or matrix of decimals
|
774
|
+
const numFloored: math.MathArray = math.floor(math.tau, [2, 3])
|
775
|
+
assert.deepStrictEqual(numFloored, [6.28, 6.283])
|
776
|
+
const bigFloored: math.Matrix = math.floor(
|
777
|
+
math.bignumber(6.28318),
|
778
|
+
math.matrix([2, 3])
|
779
|
+
)
|
780
|
+
assert.deepStrictEqual(bigFloored, math.matrix(math.bignumber([6.28, 6.283])))
|
781
|
+
assert.deepStrictEqual(math.floor(math.fraction(44, 7), [2, 3]), [
|
782
|
+
math.fraction(628, 100),
|
783
|
+
math.fraction(6285, 1000),
|
784
|
+
])
|
785
|
+
|
786
|
+
// @ts-expect-error ... verify floor(array, array) throws an error (for now)
|
787
|
+
assert.throws(() => math.floor([3.21, 3.82], [1, 2]), TypeError)
|
528
788
|
}
|
529
789
|
|
790
|
+
/*
|
791
|
+
Function round examples
|
792
|
+
*/
|
793
|
+
{
|
794
|
+
const math = create(all, {})
|
795
|
+
|
796
|
+
// number input
|
797
|
+
assert.strictEqual(math.round(3.2), 3)
|
798
|
+
assert.strictEqual(math.round(-4.2), -4)
|
799
|
+
|
800
|
+
// number input
|
801
|
+
// roundoff result to 2 decimals
|
802
|
+
assert.strictEqual(math.round(3.212, 2), 3.21)
|
803
|
+
assert.deepStrictEqual(
|
804
|
+
math.round(3.212, math.bignumber(2)),
|
805
|
+
math.bignumber(3.21)
|
806
|
+
)
|
807
|
+
assert.strictEqual(math.round(-4.212, 2), -4.21)
|
808
|
+
|
809
|
+
// bignumber input
|
810
|
+
assert.deepStrictEqual(math.round(math.bignumber(3.212)), math.bignumber(3))
|
811
|
+
assert.deepStrictEqual(
|
812
|
+
math.round(math.bignumber(3.212), 2),
|
813
|
+
math.bignumber(3.21)
|
814
|
+
)
|
815
|
+
assert.deepStrictEqual(
|
816
|
+
math.round(math.bignumber(3.212), math.bignumber(2)),
|
817
|
+
math.bignumber(3.21)
|
818
|
+
)
|
819
|
+
|
820
|
+
// fraction input
|
821
|
+
assert.deepStrictEqual(math.round(math.fraction(44, 7)), math.fraction(6))
|
822
|
+
assert.deepStrictEqual(math.round(math.fraction(-44, 7)), math.fraction(-6))
|
823
|
+
assert.deepStrictEqual(
|
824
|
+
math.round(math.fraction(44, 7), 2),
|
825
|
+
math.fraction(629, 100)
|
826
|
+
)
|
827
|
+
assert.deepStrictEqual(
|
828
|
+
math.round(math.fraction(44, 7), math.bignumber(2)),
|
829
|
+
math.fraction(629, 100)
|
830
|
+
)
|
831
|
+
|
832
|
+
// Complex input
|
833
|
+
const c = math.complex(3.24, -2.71)
|
834
|
+
assert.deepStrictEqual(math.round(c), math.complex(3, -3))
|
835
|
+
assert.deepStrictEqual(math.round(c, 1), math.complex(3.2, -2.7))
|
836
|
+
assert.deepStrictEqual(
|
837
|
+
math.round(c, math.bignumber(1)),
|
838
|
+
math.complex(3.2, -2.7)
|
839
|
+
)
|
840
|
+
|
841
|
+
// array input
|
842
|
+
assert.deepStrictEqual(math.round([3.2, 3.8, -4.7]), [3, 4, -5])
|
843
|
+
assert.deepStrictEqual(math.round([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.7])
|
844
|
+
assert.deepStrictEqual(
|
845
|
+
math.round([3.21, 3.82, -4.71], math.bignumber(1)),
|
846
|
+
math.bignumber([3.2, 3.8, -4.7])
|
847
|
+
)
|
848
|
+
|
849
|
+
// numeric input, array or matrix of decimals
|
850
|
+
const numRounded: math.MathArray = math.round(math.tau, [2, 3])
|
851
|
+
assert.deepStrictEqual(numRounded, [6.28, 6.283])
|
852
|
+
const bigRounded: math.Matrix = math.round(
|
853
|
+
math.bignumber(6.28318),
|
854
|
+
math.matrix([2, 3])
|
855
|
+
)
|
856
|
+
assert.deepStrictEqual(bigRounded, math.matrix(math.bignumber([6.28, 6.283])))
|
857
|
+
assert.deepStrictEqual(math.round(math.fraction(44, 7), [2, 3]), [
|
858
|
+
math.fraction(629, 100),
|
859
|
+
math.fraction(6286, 1000),
|
860
|
+
])
|
861
|
+
|
862
|
+
// @ts-expect-error ... verify round(array, array) throws an error (for now)
|
863
|
+
assert.throws(() => math.round([3.21, 3.82], [1, 2]), TypeError)
|
864
|
+
}
|
530
865
|
|
531
866
|
/*
|
532
867
|
JSON serialization/deserialization
|
533
868
|
*/
|
534
869
|
{
|
535
|
-
const math = create(all, {})
|
870
|
+
const math = create(all, {})
|
536
871
|
|
537
872
|
const data = {
|
538
873
|
bigNumber: math.bignumber('1.5'),
|
539
|
-
}
|
540
|
-
const stringified = JSON.stringify(data)
|
541
|
-
const parsed = JSON.parse(stringified, math.reviver)
|
542
|
-
assert.deepStrictEqual(parsed.bigNumber, math.bignumber('1.5'))
|
874
|
+
}
|
875
|
+
const stringified = JSON.stringify(data)
|
876
|
+
const parsed = JSON.parse(stringified, math.reviver)
|
877
|
+
assert.deepStrictEqual(parsed.bigNumber, math.bignumber('1.5'))
|
543
878
|
}
|
544
879
|
|
545
880
|
/*
|
@@ -548,14 +883,14 @@ Extend functionality with import
|
|
548
883
|
|
549
884
|
declare module 'mathjs' {
|
550
885
|
interface MathJsStatic {
|
551
|
-
testFun(): number
|
552
|
-
value: number
|
886
|
+
testFun(): number
|
887
|
+
value: number
|
553
888
|
}
|
554
889
|
}
|
555
890
|
|
556
891
|
{
|
557
|
-
const math = create(all, {})
|
558
|
-
const testFun = () => 5
|
892
|
+
const math = create(all, {})
|
893
|
+
const testFun = () => 5
|
559
894
|
|
560
895
|
math.import(
|
561
896
|
{
|
@@ -563,26 +898,84 @@ declare module 'mathjs' {
|
|
563
898
|
value: 10,
|
564
899
|
},
|
565
900
|
{}
|
566
|
-
)
|
901
|
+
)
|
902
|
+
|
903
|
+
math.testFun()
|
567
904
|
|
568
|
-
math.testFun()
|
905
|
+
expectTypeOf(math.testFun()).toMatchTypeOf<number>()
|
906
|
+
|
907
|
+
expectTypeOf(
|
908
|
+
math.import({
|
909
|
+
myvalue: 42,
|
910
|
+
myFunc: (name: string) => `myFunc ${name}`,
|
911
|
+
})
|
912
|
+
).toMatchTypeOf<void>()
|
913
|
+
|
914
|
+
expectTypeOf(
|
915
|
+
math.import(
|
916
|
+
{
|
917
|
+
myvalue: 42,
|
918
|
+
myFunc: (name: string) => `myFunc ${name}`,
|
919
|
+
},
|
920
|
+
{
|
921
|
+
override: true,
|
922
|
+
}
|
923
|
+
)
|
924
|
+
).toMatchTypeOf<void>()
|
569
925
|
|
570
|
-
|
926
|
+
expectTypeOf(
|
927
|
+
math.import(
|
928
|
+
{
|
929
|
+
myvalue2: 42,
|
930
|
+
},
|
931
|
+
{
|
932
|
+
silent: true,
|
933
|
+
}
|
934
|
+
)
|
935
|
+
).toMatchTypeOf<void>()
|
936
|
+
|
937
|
+
expectTypeOf(
|
938
|
+
math.import(
|
939
|
+
{
|
940
|
+
myvalue3: 42,
|
941
|
+
},
|
942
|
+
{
|
943
|
+
wrap: true,
|
944
|
+
}
|
945
|
+
)
|
946
|
+
).toMatchTypeOf<void>()
|
947
|
+
|
948
|
+
expectTypeOf(
|
949
|
+
math.import({
|
950
|
+
myvalue4: 42,
|
951
|
+
})
|
952
|
+
).toMatchTypeOf<void>()
|
953
|
+
|
954
|
+
expectTypeOf(
|
955
|
+
math.import([
|
956
|
+
{
|
957
|
+
myvalue5: 42,
|
958
|
+
},
|
959
|
+
{
|
960
|
+
myFunc2: (name: string) => `myFunc2 ${name}`,
|
961
|
+
},
|
962
|
+
])
|
963
|
+
).toMatchTypeOf<void>()
|
571
964
|
}
|
572
965
|
|
573
966
|
/*
|
574
967
|
Renamed functions from v5 => v6
|
575
968
|
*/
|
576
969
|
{
|
577
|
-
const math = create(all, {})
|
578
|
-
math.typeOf(1)
|
579
|
-
math.variance([1, 2, 3, 4])
|
580
|
-
math.evaluate('1 + 2')
|
970
|
+
const math = create(all, {})
|
971
|
+
math.typeOf(1)
|
972
|
+
math.variance([1, 2, 3, 4])
|
973
|
+
math.evaluate('1 + 2')
|
581
974
|
|
582
975
|
// chained operations
|
583
|
-
math.chain(3).typeOf().done()
|
584
|
-
math.chain([1, 2, 3]).variance().done()
|
585
|
-
math.chain('1 + 2').evaluate().done()
|
976
|
+
math.chain(3).typeOf().done()
|
977
|
+
math.chain([1, 2, 3]).variance().done()
|
978
|
+
math.chain('1 + 2').evaluate().done()
|
586
979
|
}
|
587
980
|
|
588
981
|
/*
|
@@ -590,20 +983,20 @@ Factory Test
|
|
590
983
|
*/
|
591
984
|
{
|
592
985
|
// create a factory function
|
593
|
-
const name = 'negativeSquare'
|
594
|
-
const dependencies: MathJsFunctionName[] = ['multiply', 'unaryMinus']
|
986
|
+
const name = 'negativeSquare'
|
987
|
+
const dependencies: MathJsFunctionName[] = ['multiply', 'unaryMinus']
|
595
988
|
const createNegativeSquare = factory(name, dependencies, (injected) => {
|
596
|
-
const { multiply, unaryMinus } = injected
|
989
|
+
const { multiply, unaryMinus } = injected
|
597
990
|
return function negativeSquare(x: number): number {
|
598
|
-
return unaryMinus(multiply(x, x))
|
599
|
-
}
|
600
|
-
})
|
991
|
+
return unaryMinus(multiply(x, x))
|
992
|
+
}
|
993
|
+
})
|
601
994
|
|
602
995
|
// create an instance of the function yourself:
|
603
|
-
const multiply = (a: number, b: number) => a * b
|
604
|
-
const unaryMinus = (a: number) => -a
|
605
|
-
const negativeSquare = createNegativeSquare({ multiply, unaryMinus })
|
606
|
-
negativeSquare(3)
|
996
|
+
const multiply = (a: number, b: number) => a * b
|
997
|
+
const unaryMinus = (a: number) => -a
|
998
|
+
const negativeSquare = createNegativeSquare({ multiply, unaryMinus })
|
999
|
+
negativeSquare(3)
|
607
1000
|
}
|
608
1001
|
|
609
1002
|
/**
|
@@ -613,7 +1006,7 @@ Factory Test
|
|
613
1006
|
{
|
614
1007
|
const config = {
|
615
1008
|
// optionally, you can specify configuration
|
616
|
-
}
|
1009
|
+
}
|
617
1010
|
|
618
1011
|
// Create just the functions we need
|
619
1012
|
const { fraction, add, divide, format } = create(
|
@@ -624,15 +1017,15 @@ Factory Test
|
|
624
1017
|
formatDependencies,
|
625
1018
|
},
|
626
1019
|
config
|
627
|
-
)
|
1020
|
+
)
|
628
1021
|
|
629
1022
|
// Use the created functions
|
630
|
-
const a = fraction(1, 3)
|
631
|
-
const b = fraction(3, 7)
|
632
|
-
const c = add(a, b)
|
633
|
-
const d = divide(a, b)
|
634
|
-
assert.strictEqual(format(c),
|
635
|
-
assert.strictEqual(format(d),
|
1023
|
+
const a = fraction(1, 3)
|
1024
|
+
const b = fraction(3, 7)
|
1025
|
+
const c = add(a, b)
|
1026
|
+
const d = divide(a, b)
|
1027
|
+
assert.strictEqual(format(c), '16/21')
|
1028
|
+
assert.strictEqual(format(d), '7/9')
|
636
1029
|
}
|
637
1030
|
|
638
1031
|
/**
|
@@ -640,15 +1033,15 @@ Factory Test
|
|
640
1033
|
* https://mathjs.org/docs/expressions/customization.html#customize-supported-characters
|
641
1034
|
*/
|
642
1035
|
{
|
643
|
-
const math = create(all, {})
|
644
|
-
const isAlphaOriginal = math.parse.isAlpha
|
1036
|
+
const math = create(all, {})
|
1037
|
+
const isAlphaOriginal = math.parse.isAlpha
|
645
1038
|
math.parse.isAlpha = (c, cPrev, cNext) => {
|
646
|
-
return isAlphaOriginal(c, cPrev, cNext) || c ===
|
647
|
-
}
|
1039
|
+
return isAlphaOriginal(c, cPrev, cNext) || c === '\u260E'
|
1040
|
+
}
|
648
1041
|
|
649
1042
|
// now we can use the \u260E (phone) character in expressions
|
650
|
-
const result = math.evaluate(
|
651
|
-
assert.strictEqual(result, 42)
|
1043
|
+
const result = math.evaluate('\u260Efoo', { '\u260Efoo': 42 })
|
1044
|
+
assert.strictEqual(result, 42)
|
652
1045
|
}
|
653
1046
|
|
654
1047
|
/**
|
@@ -656,26 +1049,30 @@ Factory Test
|
|
656
1049
|
* https://mathjs.org/docs/reference/functions.html#utils-functions
|
657
1050
|
*/
|
658
1051
|
{
|
659
|
-
const math = create(all, {})
|
1052
|
+
const math = create(all, {})
|
660
1053
|
|
661
1054
|
// hasNumericValue function
|
662
|
-
assert.
|
663
|
-
assert.
|
664
|
-
assert.
|
665
|
-
assert.
|
666
|
-
assert.
|
667
|
-
assert.deepStrictEqual(math.hasNumericValue([2.3, 'foo', false]),
|
668
|
-
|
669
|
-
|
1055
|
+
assert.strictEqual(math.hasNumericValue(2), true)
|
1056
|
+
assert.strictEqual(math.hasNumericValue('2'), true)
|
1057
|
+
assert.strictEqual(math.isNumeric('2'), false)
|
1058
|
+
assert.strictEqual(math.hasNumericValue(0), true)
|
1059
|
+
assert.strictEqual(math.hasNumericValue(math.bignumber(500)), true)
|
1060
|
+
assert.deepStrictEqual(math.hasNumericValue([2.3, 'foo', false]), [
|
1061
|
+
true,
|
1062
|
+
false,
|
1063
|
+
true,
|
1064
|
+
])
|
1065
|
+
assert.strictEqual(math.hasNumericValue(math.fraction(4)), true)
|
1066
|
+
assert.strictEqual(math.hasNumericValue(math.complex('2-4i')), false)
|
670
1067
|
}
|
671
1068
|
|
672
1069
|
/**
|
673
1070
|
* src/util/is functions
|
674
1071
|
*/
|
675
1072
|
{
|
676
|
-
const math = create(all, {})
|
1073
|
+
const math = create(all, {})
|
677
1074
|
|
678
|
-
type IsFunc = (x: unknown) => boolean
|
1075
|
+
type IsFunc = (x: unknown) => boolean
|
679
1076
|
const isFuncs: IsFunc[] = [
|
680
1077
|
math.isNumber,
|
681
1078
|
math.isBigNumber,
|
@@ -714,13 +1111,13 @@ Factory Test
|
|
714
1111
|
math.isParenthesisNode,
|
715
1112
|
math.isRangeNode,
|
716
1113
|
math.isSymbolNode,
|
717
|
-
math.isChain
|
1114
|
+
math.isChain,
|
718
1115
|
]
|
719
1116
|
|
720
|
-
isFuncs.forEach(f => {
|
721
|
-
const result = f(1)
|
722
|
-
const isResultBoolean = result === true || result === false
|
723
|
-
assert.ok(isResultBoolean)
|
1117
|
+
isFuncs.forEach((f) => {
|
1118
|
+
const result = f(1)
|
1119
|
+
const isResultBoolean = result === true || result === false
|
1120
|
+
assert.ok(isResultBoolean)
|
724
1121
|
})
|
725
1122
|
|
726
1123
|
// Check guards do type refinement
|
@@ -836,8 +1233,42 @@ Factory Test
|
|
836
1233
|
Probability function examples
|
837
1234
|
*/
|
838
1235
|
{
|
839
|
-
const math = create(all, {})
|
1236
|
+
const math = create(all, {})
|
840
1237
|
|
841
1238
|
expectTypeOf(math.lgamma(1.5)).toMatchTypeOf<number>()
|
842
|
-
expectTypeOf(
|
1239
|
+
expectTypeOf(
|
1240
|
+
math.lgamma(math.complex(1.5, -1.5))
|
1241
|
+
).toMatchTypeOf<math.Complex>()
|
1242
|
+
}
|
1243
|
+
|
1244
|
+
/*
|
1245
|
+
toTex examples
|
1246
|
+
*/
|
1247
|
+
|
1248
|
+
{
|
1249
|
+
const math = create(all, {})
|
1250
|
+
|
1251
|
+
expectTypeOf(math.parse('a/b').toTex()).toMatchTypeOf<string>()
|
1252
|
+
|
1253
|
+
// TODO add proper types for toTex options
|
1254
|
+
expectTypeOf(
|
1255
|
+
math.parse('a/b').toTex({
|
1256
|
+
a: '123',
|
1257
|
+
})
|
1258
|
+
).toMatchTypeOf<string>()
|
1259
|
+
}
|
1260
|
+
|
1261
|
+
/*
|
1262
|
+
Resolve examples
|
1263
|
+
*/
|
1264
|
+
{
|
1265
|
+
const math = create(all, {})
|
1266
|
+
|
1267
|
+
expectTypeOf(math.resolve(math.parse('x + y'))).toMatchTypeOf<MathNode>()
|
1268
|
+
expectTypeOf(
|
1269
|
+
math.resolve(math.parse('x + y'), { x: 0 })
|
1270
|
+
).toMatchTypeOf<MathNode>()
|
1271
|
+
expectTypeOf(math.resolve([math.parse('x + y')], { x: 0 })).toMatchTypeOf<
|
1272
|
+
MathNode[]
|
1273
|
+
>()
|
843
1274
|
}
|