mathjs 11.3.0 → 11.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/types/index.ts DELETED
@@ -1,2330 +0,0 @@
1
- import {
2
- AccessorNode,
3
- addDependencies,
4
- all,
5
- ArrayNode,
6
- AssignmentNode,
7
- BigNumber,
8
- BlockNode,
9
- Complex,
10
- ConditionalNode,
11
- ConstantNode,
12
- create,
13
- divideDependencies,
14
- EvalFunction,
15
- factory,
16
- formatDependencies,
17
- Fraction,
18
- fractionDependencies,
19
- FunctionAssignmentNode,
20
- FunctionNode,
21
- Help,
22
- Index,
23
- IndexNode,
24
- LUDecomposition,
25
- MathArray,
26
- MathCollection,
27
- MathJsChain,
28
- MathJsFunctionName,
29
- MathNode,
30
- MathNumericType,
31
- MathType,
32
- Matrix,
33
- ObjectNode,
34
- OperatorNode,
35
- OperatorNodeFn,
36
- OperatorNodeOp,
37
- ParenthesisNode,
38
- PolarCoordinates,
39
- QRDecomposition,
40
- RangeNode,
41
- SimplifyRule,
42
- SLUDecomposition,
43
- SymbolNode,
44
- MathNodeCommon,
45
- Unit,
46
- Node,
47
- isSymbolNode,
48
- } from 'mathjs'
49
- import * as assert from 'assert'
50
- import { expectTypeOf } from 'expect-type'
51
-
52
- // This file serves a dual purpose:
53
- // 1) examples of how to use math.js in TypeScript
54
- // 2) tests for the TypeScript declarations provided by math.js
55
-
56
- /*
57
- Basic usage examples
58
- */
59
- {
60
- const math = create(all)
61
-
62
- const m2by2 = [
63
- [-1, 2],
64
- [3, 1],
65
- ]
66
- const m2by3 = [
67
- [1, 2, 3],
68
- [4, 5, 6],
69
- ]
70
-
71
- // functions and constants
72
- math.round(math.e, 3)
73
- math.round(100.123, 3)
74
- math.atan2(3, -3) / math.pi
75
- math.log(10000, 10)
76
- math.sqrt(-4)
77
-
78
- math.pow(m2by2, 2)
79
- const angle = 0.2
80
- math.add(math.pow(math.sin(angle), 2), math.pow(math.cos(angle), 2))
81
-
82
- // std and variance check
83
-
84
- math.std(1, 2, 3)
85
- math.std([1, 2, 3])
86
- math.std([1, 2, 3], 'biased')
87
- math.std([1, 2, 3], 0, 'biased')
88
- math.std(m2by3, 1, 'unbiased')
89
- math.std(m2by3, 1, 'uncorrected')
90
- math.variance(1, 2, 3)
91
- math.variance([1, 2, 3])
92
- math.variance([1, 2, 3], 'biased')
93
- math.variance([1, 2, 3], 0, 'biased')
94
- math.variance(m2by3, 1, 'unbiased')
95
- math.variance(m2by3, 1, 'uncorrected')
96
-
97
- // std and variance on chain
98
- math.chain([1, 2, 3]).std('unbiased')
99
- math.chain(m2by3).std(0, 'biased').std(0, 'uncorrected')
100
- math.chain(m2by3).std(0, 'biased').std(0, 'uncorrected')
101
- math.chain([1, 2, 3]).std('unbiased')
102
- math.chain(m2by3).variance(0, 'biased')
103
- math.chain(m2by3).variance(1, 'uncorrected').variance('unbiased')
104
-
105
- math.variance(math.variance(m2by3, 'uncorrected'))
106
-
107
- // count and sum check
108
- math.count([10, 10, 10])
109
- math.count([
110
- [1, 2, 3],
111
- [4, 5, 6],
112
- ])
113
- math.count('mathjs')
114
-
115
- math.sum(1, 2, 3, 4)
116
- math.sum([1, 2, 3, 4])
117
- math.sum([
118
- [1, 2],
119
- [3, 4],
120
- [5, 6],
121
- ])
122
-
123
- // expressions
124
- math.evaluate('1.2 * (2 + 4.5)')
125
-
126
- // chained operations
127
- const a = math.chain(3).add(4).multiply(2).done()
128
- assert.strictEqual(a, 14)
129
-
130
- // mixed use of different data types in functions
131
- assert.deepStrictEqual(math.add(4, [5, 6]), [9, 10]) // number + Array
132
- assert.deepStrictEqual(
133
- math.multiply(math.unit('5 mm'), 3),
134
- math.unit('15 mm')
135
- ) // Unit * number
136
- assert.deepStrictEqual(math.subtract([2, 3, 4], 5), [-3, -2, -1]) // Array - number
137
- assert.deepStrictEqual(
138
- math.add(math.matrix([2, 3]), [4, 5]),
139
- math.matrix([6, 8])
140
- ) // Matrix + Array
141
-
142
- // narrowed type inference
143
- const _b: Matrix = math.add(math.matrix([2]), math.matrix([3]))
144
- const _c: Matrix = math.subtract(math.matrix([4]), math.matrix([5]))
145
- }
146
-
147
- /*
148
- Bignumbers examples
149
- */
150
- {
151
- // configure the default type of numbers as BigNumbers
152
- const math = create(all, {
153
- number: 'BigNumber',
154
- precision: 20,
155
- })
156
-
157
- {
158
- assert.deepStrictEqual(
159
- math.add(math.bignumber(0.1), math.bignumber(0.2)),
160
- math.bignumber(0.3)
161
- )
162
- assert.deepStrictEqual(
163
- math.divide(math.bignumber(0.3), math.bignumber(0.2)),
164
- math.bignumber(1.5)
165
- )
166
- }
167
- }
168
-
169
- /*
170
- Arithmetic function examples
171
- */
172
- {
173
- const math = create(all, {})
174
-
175
- const _e: number = math.exp(1)
176
- const _bige: BigNumber = math.exp(math.bignumber(1))
177
- const _compe: Complex = math.exp(math.complex(1, 0))
178
- // TODO: Typescript type declarations are not understanding typed-function's
179
- // automatic conversions, so the following do not work:
180
-
181
- // const _compeagain: Complex = math.exp(math.fraction(1, 1))
182
- // const _eagain: number = math.exp('1')
183
- // const _eanother: number = math.exp(true)
184
- }
185
-
186
- /*
187
- Chaining examples
188
- */
189
- {
190
- const math = create(all, {})
191
- const a = math.chain(3).add(4).multiply(2).done()
192
- assert.strictEqual(a, 14)
193
-
194
- // Another example, calculate square(sin(pi / 4))
195
- const _b = math.chain(math.pi).divide(4).sin().square().done()
196
-
197
- // toString will return a string representation of the chain's value
198
- const chain = math.chain(2).divide(3)
199
- const str: string = chain.toString()
200
- assert.strictEqual(str, '0.6666666666666666')
201
-
202
- chain.valueOf()
203
-
204
- // the function subset can be used to get or replace sub matrices
205
- const array = [
206
- [1, 2],
207
- [3, 4],
208
- ]
209
- const v = math.chain(array).subset(math.index(1, 0)).done()
210
- assert.strictEqual(v, 3)
211
-
212
- const _m = math.chain(array).subset(math.index(0, 0), 8).multiply(3).done()
213
-
214
- // filtering
215
- assert.deepStrictEqual(
216
- math
217
- .chain([-1, 0, 1.1, 2, 3, 1000])
218
- .filter(math.isPositive)
219
- .filter(math.isInteger)
220
- .filter((n) => n !== 1000)
221
- .done(),
222
- [2, 3]
223
- )
224
-
225
- const r = math.chain(-0.483).round([0, 1, 2]).floor().add(0.52).fix(1).done()
226
-
227
- assert.deepStrictEqual(r, [0.5, -0.4, -0.4])
228
-
229
- expectTypeOf(
230
- math.chain('x + y').parse().resolve({ x: 1 }).done()
231
- ).toMatchTypeOf<MathNode>()
232
- expectTypeOf(
233
- math.chain('x + y').parse().resolve().done()
234
- ).toMatchTypeOf<MathNode>()
235
-
236
- // bignum
237
- expectTypeOf(math.chain(math.bignumber(12))).toMatchTypeOf<
238
- MathJsChain<BigNumber>
239
- >()
240
- expectTypeOf(math.chain(12).bignumber()).toMatchTypeOf<
241
- MathJsChain<BigNumber>
242
- >()
243
- expectTypeOf(math.chain([12, 13, 14]).bignumber()).toMatchTypeOf<
244
- MathJsChain<MathCollection>
245
- >()
246
-
247
- // chain
248
- expectTypeOf(math.chain(12).bignumber().clone()).toMatchTypeOf<
249
- MathJsChain<BigNumber>
250
- >()
251
-
252
- // boolean
253
- expectTypeOf(math.chain(math.boolean(true))).toMatchTypeOf<
254
- MathJsChain<boolean>
255
- >()
256
- expectTypeOf(math.chain(true).boolean()).toMatchTypeOf<MathJsChain<boolean>>()
257
- expectTypeOf(math.chain([12, 13, 14]).boolean()).toMatchTypeOf<
258
- MathJsChain<MathCollection>
259
- >()
260
-
261
- // complex
262
- expectTypeOf(math.chain(math.complex('123'))).toMatchTypeOf<
263
- MathJsChain<Complex>
264
- >()
265
- expectTypeOf(math.chain('123').complex()).toMatchTypeOf<
266
- MathJsChain<Complex>
267
- >()
268
- expectTypeOf(math.chain('123').complex(1)).toMatchTypeOf<
269
- MathJsChain<Complex>
270
- >()
271
- expectTypeOf(math.chain([12, 13, 14]).complex()).toMatchTypeOf<
272
- MathJsChain<MathCollection>
273
- >()
274
-
275
- // createUnit
276
- expectTypeOf(math.chain(math.createUnit('furlong'))).toMatchTypeOf<
277
- MathJsChain<Unit>
278
- >()
279
- expectTypeOf(
280
- math.chain(
281
- math.createUnit({
282
- fresnel: '1234',
283
- })
284
- )
285
- ).toMatchTypeOf<MathJsChain<Unit>>()
286
-
287
- // fraction
288
- expectTypeOf(math.chain(math.fraction('123'))).toMatchTypeOf<
289
- MathJsChain<Fraction>
290
- >()
291
- expectTypeOf(math.chain('123').fraction()).toMatchTypeOf<
292
- MathJsChain<Fraction>
293
- >()
294
- expectTypeOf(math.chain('123').fraction(2)).toMatchTypeOf<
295
- MathJsChain<Fraction>
296
- >()
297
- expectTypeOf(math.chain([12, 13, 14]).fraction()).toMatchTypeOf<
298
- MathJsChain<MathCollection>
299
- >()
300
- expectTypeOf(math.chain([12, 13, 14]).fraction()).toMatchTypeOf<
301
- MathJsChain<MathCollection>
302
- >()
303
-
304
- // index
305
- expectTypeOf(math.chain([12, 13, 14]).index()).toMatchTypeOf<
306
- MathJsChain<Index>
307
- >()
308
-
309
- // matrix
310
- expectTypeOf(math.chain([12, 13, 14, 15]).matrix()).toMatchTypeOf<
311
- MathJsChain<Matrix>
312
- >()
313
-
314
- // number
315
- expectTypeOf(math.chain('12').number()).toMatchTypeOf<MathJsChain<number>>()
316
- expectTypeOf(math.chain([12, 13, 14]).number()).toMatchTypeOf<
317
- MathJsChain<MathCollection>
318
- >()
319
-
320
- // sparse
321
- expectTypeOf(math.chain([12, 13, 14, 15]).sparse()).toMatchTypeOf<
322
- MathJsChain<Matrix>
323
- >()
324
-
325
- // split unit
326
- expectTypeOf(math.chain(math.unit('furlong')).splitUnit([])).toMatchTypeOf<
327
- MathJsChain<Unit[]>
328
- >()
329
-
330
- // string
331
- expectTypeOf(math.chain('test').string()).toMatchTypeOf<MathJsChain<string>>()
332
- expectTypeOf(math.chain([1, 2, 3]).string()).toMatchTypeOf<
333
- MathJsChain<MathCollection>
334
- >()
335
-
336
- // unit
337
- expectTypeOf(math.chain(12).unit()).toMatchTypeOf<MathJsChain<Unit>>()
338
- expectTypeOf(math.chain([1, 2, 3]).unit()).toMatchTypeOf<
339
- MathJsChain<Unit[]>
340
- >()
341
-
342
- // compile
343
- expectTypeOf(math.chain('a + b').compile()).toMatchTypeOf<
344
- MathJsChain<EvalFunction>
345
- >()
346
-
347
- // evaluate
348
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
349
- expectTypeOf(math.chain('1 + 1').evaluate()).toMatchTypeOf<MathJsChain<any>>()
350
- expectTypeOf(math.chain(['1 + 1', '2 + 2']).evaluate()).toMatchTypeOf<
351
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
352
- MathJsChain<any[]>
353
- >()
354
-
355
- // parse
356
- expectTypeOf(math.chain('1 + 1').parse()).toMatchTypeOf<
357
- MathJsChain<MathNode>
358
- >()
359
- expectTypeOf(math.chain(['1 + 1', '2 + 2']).parse()).toMatchTypeOf<
360
- MathJsChain<MathNode[]>
361
- >()
362
-
363
- // resolve
364
- expectTypeOf(math.chain(math.parse('1 + 1')).resolve({})).toMatchTypeOf<
365
- MathJsChain<MathNode>
366
- >()
367
- expectTypeOf(
368
- math.chain([math.parse('1 + 1'), math.parse('1 + 1')]).resolve({})
369
- ).toMatchTypeOf<MathJsChain<MathNode[]>>()
370
-
371
- // derivative
372
- expectTypeOf(math.chain(math.parse('x^2')).derivative('x')).toMatchTypeOf<
373
- MathJsChain<MathNode>
374
- >()
375
-
376
- // lsolve
377
- expectTypeOf(
378
- math
379
- .chain([
380
- [1, 2],
381
- [3, 4],
382
- ])
383
- .lsolve([1, 2])
384
- ).toMatchTypeOf<MathJsChain<MathArray>>()
385
- expectTypeOf(
386
- math
387
- .chain(
388
- math.matrix([
389
- [1, 2],
390
- [3, 4],
391
- ])
392
- )
393
- .lsolve([1, 2])
394
- ).toMatchTypeOf<MathJsChain<Matrix>>()
395
-
396
- // lup
397
- expectTypeOf(
398
- math
399
- .chain([
400
- [1, 2],
401
- [3, 4],
402
- ])
403
- .lup()
404
- ).toMatchTypeOf<MathJsChain<LUDecomposition>>()
405
- expectTypeOf(
406
- math
407
- .chain(
408
- math.matrix([
409
- [1, 2],
410
- [3, 4],
411
- ])
412
- )
413
- .lup()
414
- ).toMatchTypeOf<MathJsChain<LUDecomposition>>()
415
-
416
- // lusolve
417
- expectTypeOf(
418
- math
419
- .chain(
420
- math.matrix([
421
- [1, 2],
422
- [3, 4],
423
- ])
424
- )
425
- .lusolve(math.matrix([1, 2]))
426
- ).toMatchTypeOf<MathJsChain<Matrix>>()
427
-
428
- expectTypeOf(
429
- math
430
- .chain(
431
- math.matrix([
432
- [1, 2],
433
- [3, 4],
434
- ])
435
- )
436
- .lusolve([1, 2])
437
- ).toMatchTypeOf<MathJsChain<Matrix>>()
438
-
439
- expectTypeOf(
440
- math
441
- .chain([
442
- [1, 2],
443
- [3, 4],
444
- ])
445
- .lusolve(math.matrix([1, 2]))
446
- ).toMatchTypeOf<MathJsChain<MathArray>>()
447
-
448
- expectTypeOf(
449
- math
450
- .chain([
451
- [1, 2],
452
- [3, 4],
453
- ])
454
- .lusolve([1, 2])
455
- ).toMatchTypeOf<MathJsChain<MathArray>>()
456
-
457
- // qr
458
- expectTypeOf(
459
- math
460
- .chain([
461
- [1, 2],
462
- [3, 4],
463
- ])
464
- .qr()
465
- ).toMatchTypeOf<MathJsChain<QRDecomposition>>()
466
- expectTypeOf(
467
- math
468
- .chain(
469
- math.matrix([
470
- [1, 2],
471
- [3, 4],
472
- ])
473
- )
474
- .qr()
475
- ).toMatchTypeOf<MathJsChain<QRDecomposition>>()
476
-
477
- // rationalize
478
- expectTypeOf(math.chain('1.23').rationalize()).toMatchTypeOf<
479
- MathJsChain<MathNode>
480
- >()
481
- expectTypeOf(math.chain(math.parse('1.23')).rationalize()).toMatchTypeOf<
482
- MathJsChain<MathNode>
483
- >()
484
-
485
- // simplify
486
- expectTypeOf(math.chain('a + a + b').simplify()).toMatchTypeOf<
487
- MathJsChain<MathNode>
488
- >()
489
- expectTypeOf(math.chain(math.parse('a + a + b')).simplify()).toMatchTypeOf<
490
- MathJsChain<MathNode>
491
- >()
492
-
493
- // slu
494
- expectTypeOf(
495
- math
496
- .chain(
497
- math.sparse([
498
- [1, 2],
499
- [3, 4],
500
- ])
501
- )
502
- .slu(2, 0.5)
503
- ).toMatchTypeOf<MathJsChain<SLUDecomposition>>()
504
-
505
- // usolve
506
- expectTypeOf(
507
- math
508
- .chain([
509
- [1, 2],
510
- [3, 4],
511
- ])
512
- .usolve([1, 2])
513
- ).toMatchTypeOf<MathJsChain<MathArray>>()
514
- expectTypeOf(
515
- math
516
- .chain(
517
- math.matrix([
518
- [1, 2],
519
- [3, 4],
520
- ])
521
- )
522
- .usolve([1, 2])
523
- ).toMatchTypeOf<MathJsChain<Matrix>>()
524
-
525
- // abs
526
- expectTypeOf(math.chain(1).abs()).toMatchTypeOf<MathJsChain<number>>()
527
- expectTypeOf(math.chain(math.bignumber(1)).abs()).toMatchTypeOf<
528
- MathJsChain<BigNumber>
529
- >()
530
- expectTypeOf(math.chain(math.fraction(1, 2)).abs()).toMatchTypeOf<
531
- MathJsChain<Fraction>
532
- >()
533
- expectTypeOf(math.chain(math.complex(1, 2)).abs()).toMatchTypeOf<
534
- MathJsChain<Complex>
535
- >()
536
- expectTypeOf(math.chain([1, 2]).abs()).toMatchTypeOf<MathJsChain<MathArray>>()
537
- expectTypeOf(
538
- math
539
- .chain(
540
- math.matrix([
541
- [1, 2],
542
- [3, 4],
543
- ])
544
- )
545
- .abs()
546
- ).toMatchTypeOf<MathJsChain<Matrix>>()
547
- expectTypeOf(math.chain(math.unit('furlong')).abs()).toMatchTypeOf<
548
- MathJsChain<Unit>
549
- >()
550
-
551
- // add
552
- expectTypeOf(math.chain(1).add(2)).toMatchTypeOf<MathJsChain<MathType>>()
553
- expectTypeOf(math.chain([1]).add(2)).toMatchTypeOf<MathJsChain<MathType>>()
554
- expectTypeOf(
555
- math.chain(
556
- math.matrix([
557
- [1, 2],
558
- [3, 4],
559
- ])
560
- )
561
- ).toMatchTypeOf<MathJsChain<Matrix>>()
562
-
563
- // apply
564
- expectTypeOf(math.chain([1, 2, 3]).apply(0, () => 1)).toMatchTypeOf<
565
- MathJsChain<number[]>
566
- >()
567
-
568
- // cbrt
569
- expectTypeOf(math.chain(1).cbrt()).toMatchTypeOf<MathJsChain<number>>()
570
- expectTypeOf(math.chain(math.bignumber(1)).cbrt()).toMatchTypeOf<
571
- MathJsChain<BigNumber>
572
- >()
573
- expectTypeOf(math.chain(math.complex(1, 2)).cbrt()).toMatchTypeOf<
574
- MathJsChain<Complex>
575
- >()
576
- // @ts-expect-error ... verify cbrt does not run on arrays.
577
- assert.throws(() => math.chain([1, 2]).cbrt(), TypeError)
578
- assert.throws(
579
- () =>
580
- // @ts-expect-error ... verify cbrt does not run on matrices.
581
- math
582
- .chain(
583
- math.matrix([
584
- [1, 2],
585
- [3, 4],
586
- ])
587
- )
588
- .cbrt(),
589
- TypeError
590
- )
591
-
592
- // ceil
593
- expectTypeOf(math.chain(1).ceil()).toMatchTypeOf<
594
- MathJsChain<MathNumericType>
595
- >()
596
- expectTypeOf(math.chain([1]).ceil()).toMatchTypeOf<
597
- MathJsChain<MathCollection>
598
- >()
599
-
600
- // fix
601
- expectTypeOf(math.chain(1).fix()).toMatchTypeOf<
602
- MathJsChain<MathNumericType>
603
- >()
604
- expectTypeOf(math.chain([1]).fix()).toMatchTypeOf<
605
- MathJsChain<MathCollection>
606
- >()
607
-
608
- // floor
609
- expectTypeOf(math.chain(1).floor()).toMatchTypeOf<
610
- MathJsChain<MathNumericType>
611
- >()
612
- expectTypeOf(math.chain([1]).floor()).toMatchTypeOf<
613
- MathJsChain<MathCollection>
614
- >()
615
-
616
- // round
617
- expectTypeOf(math.chain(1).round()).toMatchTypeOf<
618
- MathJsChain<MathNumericType>
619
- >()
620
- expectTypeOf(math.chain([1]).round()).toMatchTypeOf<
621
- MathJsChain<MathCollection>
622
- >()
623
-
624
- // cube
625
- expectTypeOf(math.chain(1).cube()).toMatchTypeOf<MathJsChain<number>>()
626
- expectTypeOf(math.chain(math.bignumber(1)).cube()).toMatchTypeOf<
627
- MathJsChain<BigNumber>
628
- >()
629
- expectTypeOf(math.chain(math.fraction(1, 2)).cube()).toMatchTypeOf<
630
- MathJsChain<Fraction>
631
- >()
632
- expectTypeOf(math.chain(math.complex(1, 2)).cube()).toMatchTypeOf<
633
- MathJsChain<Complex>
634
- >()
635
-
636
- // @ts-expect-error ... verify cube does not run on arrays.
637
- assert.throws(() => math.chain([1, 2]).cube(), TypeError)
638
- assert.throws(
639
- () =>
640
- // @ts-expect-error ... verify cube does not run on matrices.
641
- math
642
- .chain(
643
- math.matrix([
644
- [1, 2],
645
- [3, 4],
646
- ])
647
- )
648
- .cube(),
649
- TypeError
650
- )
651
-
652
- expectTypeOf(math.chain(math.unit('furlong')).cube()).toMatchTypeOf<
653
- MathJsChain<Unit>
654
- >()
655
-
656
- // divide
657
- expectTypeOf(
658
- math.chain(math.unit('furlong')).divide(math.unit('femtosecond'))
659
- ).toMatchTypeOf<MathJsChain<number | Unit>>()
660
- expectTypeOf(math.chain(math.unit('furlong')).divide(6)).toMatchTypeOf<
661
- MathJsChain<Unit>
662
- >()
663
- expectTypeOf(math.chain(2).divide(6)).toMatchTypeOf<MathJsChain<number>>()
664
- expectTypeOf(math.chain([1, 2, 3]).divide(6)).toMatchTypeOf<
665
- MathJsChain<MathType>
666
- >()
667
-
668
- // dotDivide
669
- expectTypeOf(math.chain(1).dotDivide(2)).toMatchTypeOf<
670
- MathJsChain<MathType>
671
- >()
672
- expectTypeOf(
673
- math
674
- .chain(
675
- math.matrix([
676
- [1, 2],
677
- [3, 4],
678
- ])
679
- )
680
- .dotDivide(2)
681
- ).toMatchTypeOf<MathJsChain<MathType>>()
682
-
683
- // dotMultiply
684
- expectTypeOf(math.chain(1).dotMultiply(2)).toMatchTypeOf<
685
- MathJsChain<MathType>
686
- >()
687
- expectTypeOf(
688
- math
689
- .chain(
690
- math.matrix([
691
- [1, 2],
692
- [3, 4],
693
- ])
694
- )
695
- .dotMultiply(2)
696
- ).toMatchTypeOf<MathJsChain<MathType>>()
697
-
698
- // dotPow
699
- expectTypeOf(math.chain(1).dotPow(2)).toMatchTypeOf<MathJsChain<MathType>>()
700
- expectTypeOf(
701
- math
702
- .chain(
703
- math.matrix([
704
- [1, 2],
705
- [3, 4],
706
- ])
707
- )
708
- .dotPow(2)
709
- ).toMatchTypeOf<MathJsChain<MathType>>()
710
-
711
- // exp
712
- expectTypeOf(math.chain(1).exp()).toMatchTypeOf<MathJsChain<MathType>>()
713
- // @ts-expect-error ... verify exp does not run on arrays.
714
- assert.throws(() => math.chain([1, 2]).exp(), TypeError)
715
- assert.throws(
716
- () =>
717
- // @ts-expect-error ... verify exp does not run on matrices.
718
- math
719
- .chain(
720
- math.matrix([
721
- [1, 2],
722
- [3, 4],
723
- ])
724
- )
725
- .exp(),
726
- TypeError
727
- )
728
-
729
- // expm1
730
- expectTypeOf(math.chain(1).expm1()).toMatchTypeOf<MathJsChain<MathType>>()
731
-
732
- // @ts-expect-error ... verify expm1 does not run on arrays
733
- assert.throws(() => math.chain([1, 2]).expm1(), TypeError)
734
- assert.throws(
735
- () =>
736
- // @ts-expect-error ... verify expm1 does not run on arrays
737
- math
738
- .chain(
739
- math.matrix([
740
- [1, 2],
741
- [3, 4],
742
- ])
743
- )
744
- .expm1(),
745
- TypeError
746
- )
747
-
748
- // gcd
749
- expectTypeOf(math.chain([1, 2]).gcd(3)).toMatchTypeOf<MathJsChain<number>>()
750
- expectTypeOf(math.chain([1, 2]).gcd(3, 4)).toMatchTypeOf<
751
- MathJsChain<number>
752
- >()
753
- // TODO make gcd() work in the following cases
754
- // expectTypeOf(math.chain([1, 2]).gcd()).toMatchTypeOf<MathJsChain<number>>()
755
- // expectTypeOf(math.chain([[1], [2]]).gcd()).toMatchTypeOf<
756
- // MathJsChain<MathArray>
757
- // >()
758
- // expectTypeOf(
759
- // math.chain([math.bignumber(1), math.bignumber(1)]).gcd()
760
- // ).toMatchTypeOf<MathJsChain<BigNumber>>()
761
- // expectTypeOf(
762
- // math.chain([math.complex(1, 2), math.complex(1, 2)]).gcd()
763
- // ).toMatchTypeOf<MathJsChain<Complex>>()
764
- // expectTypeOf(
765
- // math
766
- // .chain(
767
- // math.matrix([
768
- // [1, 2],
769
- // [3, 4],
770
- // ])
771
- // )
772
- // .expm1()
773
- // ).toMatchTypeOf<MathJsChain<Matrix>>()
774
-
775
- // hypot
776
- expectTypeOf(math.chain([1, 2]).hypot()).toMatchTypeOf<MathJsChain<number>>()
777
- expectTypeOf(
778
- math.chain([math.bignumber(1), math.bignumber(1)]).hypot()
779
- ).toMatchTypeOf<MathJsChain<BigNumber>>()
780
-
781
- // lcm
782
- expectTypeOf(math.chain(1).lcm(2)).toMatchTypeOf<MathJsChain<number>>()
783
- expectTypeOf(
784
- math.chain(math.bignumber(1)).lcm(math.bignumber(2))
785
- ).toMatchTypeOf<MathJsChain<BigNumber>>()
786
- expectTypeOf(math.chain([1, 2]).lcm([3, 4])).toMatchTypeOf<
787
- MathJsChain<MathArray>
788
- >()
789
- expectTypeOf(
790
- math
791
- .chain(
792
- math.matrix([
793
- [1, 2],
794
- [3, 4],
795
- ])
796
- )
797
- .lcm(
798
- math.matrix([
799
- [1, 2],
800
- [3, 4],
801
- ])
802
- )
803
- ).toMatchTypeOf<MathJsChain<Matrix>>()
804
-
805
- // log
806
- expectTypeOf(math.chain(1).log(2)).toMatchTypeOf<MathJsChain<number>>()
807
- expectTypeOf(
808
- math.chain(math.bignumber(1)).log(math.bignumber(2))
809
- ).toMatchTypeOf<MathJsChain<BigNumber>>()
810
-
811
- // log10
812
- expectTypeOf(math.chain(1).log10()).toMatchTypeOf<MathJsChain<number>>()
813
- expectTypeOf(math.chain(math.bignumber(1)).log10()).toMatchTypeOf<
814
- MathJsChain<BigNumber>
815
- >()
816
- expectTypeOf(math.chain([1, 2]).log10()).toMatchTypeOf<
817
- MathJsChain<MathArray>
818
- >()
819
- expectTypeOf(
820
- math
821
- .chain(
822
- math.matrix([
823
- [1, 2],
824
- [3, 4],
825
- ])
826
- )
827
- .log10()
828
- ).toMatchTypeOf<MathJsChain<Matrix>>()
829
-
830
- expectTypeOf(math.chain([1, 2]).count()).toMatchTypeOf<MathJsChain<number>>()
831
- expectTypeOf(math.chain('mathjs').count()).toMatchTypeOf<
832
- MathJsChain<number>
833
- >()
834
- expectTypeOf(math.chain([1, 2]).sum()).toMatchTypeOf<MathJsChain<number>>()
835
- // TODO complete the rest of these...
836
- }
837
-
838
- /*
839
- Simplify examples
840
- */
841
- {
842
- const math = create(all)
843
-
844
- math.simplify('2 * 1 * x ^ (2 - 1)')
845
- math.simplifyConstant('2 * 1 * x ^ (2 - 1)')
846
- math.simplifyCore('2 * 1 * x ^ (2 - 1)')
847
- math.simplify('2 * 3 * x', { x: 4 })
848
-
849
- expectTypeOf(math.simplify.rules).toMatchTypeOf<Array<SimplifyRule>>()
850
-
851
- const f = math.parse('2 * 1 * x ^ (2 - 1)')
852
- math.simplify(f)
853
-
854
- math.simplify('0.4 * x', {}, { exactFractions: true })
855
- math.simplifyConstant('0.4 * x', { exactFractions: true })
856
- math.simplify('0.4 * x', {}, { exactFractions: false })
857
- math.simplify('0.4 * x', {}, { fractionsLimit: 2 })
858
- math.simplify('0.4 * x', {}, { consoleDebug: false })
859
-
860
- math.simplify(
861
- '0.4 * x',
862
- {},
863
- {
864
- context: {
865
- xor: {
866
- trivial: true,
867
- total: true,
868
- commutative: true,
869
- associative: true,
870
- },
871
- },
872
- }
873
- )
874
-
875
- math.simplify('0.4 * x', [])
876
- math.simplify('0.4 * x', [
877
- 'n * n -> 2n',
878
- {
879
- s: 'n * n -> 2n',
880
- repeat: true,
881
- assuming: {
882
- multiply: {
883
- trivial: true,
884
- total: true,
885
- commutative: true,
886
- associative: true,
887
- },
888
- },
889
- imposeContext: {
890
- multiply: {
891
- trivial: true,
892
- total: true,
893
- commutative: true,
894
- associative: true,
895
- },
896
- },
897
- },
898
- {
899
- l: 'n * n',
900
- r: '2n',
901
- repeat: true,
902
- assuming: {
903
- multiply: {
904
- trivial: true,
905
- total: true,
906
- commutative: true,
907
- associative: true,
908
- },
909
- },
910
- imposeContext: {
911
- multiply: {
912
- trivial: true,
913
- total: true,
914
- commutative: true,
915
- associative: true,
916
- },
917
- },
918
- },
919
- (node: MathNode) => node,
920
- ])
921
- math.simplifyCore('0.4 * x + 0', { exactFractions: false })
922
-
923
- math
924
- .chain('0.4 * x + 0')
925
- .parse()
926
- .simplifyCore({ exactFractions: false })
927
- .simplifyConstant()
928
- }
929
-
930
- /*
931
- Complex numbers examples
932
- */
933
- {
934
- const math = create(all, {})
935
- const a = math.complex(2, 3)
936
- // create a complex number by providing a string with real and complex parts
937
- const b = math.complex('3 - 7i')
938
-
939
- // read the real and complex parts of the complex number
940
- {
941
- const _x: number = a.re
942
- const _y: number = a.im
943
-
944
- // adjust the complex value
945
- a.re = 5
946
- }
947
-
948
- // clone a complex value
949
- {
950
- const _clone = a.clone()
951
- }
952
-
953
- // perform operations with complex numbers
954
- {
955
- math.add(a, b)
956
- math.multiply(a, b)
957
- math.sin(a)
958
- }
959
-
960
- // create a complex number from polar coordinates
961
- {
962
- const p: PolarCoordinates = {
963
- r: math.sqrt(2) as number, // must be real but a sqrt could be Complex
964
- phi: math.pi / 4,
965
- }
966
- const c: Complex = math.complex(p)
967
- assert.strictEqual(c.im, 1)
968
- assert.ok(Math.abs(c.re - 1) < 1e-12)
969
- }
970
-
971
- // get polar coordinates of a complex number
972
- {
973
- const _p: PolarCoordinates = math.complex(3, 4).toPolar()
974
- }
975
- }
976
-
977
- /*
978
- Parenthesis examples
979
- */
980
- {
981
- const math = create(all, {})
982
-
983
- expectTypeOf(
984
- new math.ParenthesisNode(new math.ConstantNode(3))
985
- ).toMatchTypeOf<ParenthesisNode<ConstantNode>>()
986
- }
987
-
988
- /*
989
- Expressions examples
990
- */
991
- {
992
- const math = create(all, {})
993
- // evaluate expressions
994
- {
995
- math.evaluate('sqrt(3^2 + 4^2)')
996
- }
997
-
998
- // evaluate multiple expressions at once
999
- {
1000
- math.evaluate(['f = 3', 'g = 4', 'f * g'])
1001
- }
1002
-
1003
- // get content of a parenthesis node
1004
- {
1005
- const node = math.parse('(1)')
1006
- if (node.type !== 'ParenthesisNode') {
1007
- throw Error(`expected ParenthesisNode, got ${node.type}`)
1008
- }
1009
- }
1010
-
1011
- // scope can contain both variables and functions
1012
- {
1013
- const scope = { hello: (name: string) => `hello, ${name}!` }
1014
- assert.strictEqual(math.evaluate('hello("hero")', scope), 'hello, hero!')
1015
- }
1016
-
1017
- // define a function as an expression
1018
- {
1019
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1020
- const scope: any = {
1021
- a: 3,
1022
- b: 4,
1023
- }
1024
- const f = math.evaluate('f(x) = x ^ a', scope)
1025
- f(2)
1026
- scope.f(2)
1027
- }
1028
-
1029
- {
1030
- const node2 = math.parse('x^a')
1031
- const _code2: EvalFunction = node2.compile()
1032
- node2.toString()
1033
- }
1034
-
1035
- // 3. using function math.compile
1036
- // parse an expression
1037
- {
1038
- // provide a scope for the variable assignment
1039
- const code2 = math.compile('a = a + 3')
1040
- const scope = { a: 7 }
1041
- code2.evaluate(scope)
1042
- }
1043
- // 4. using a parser
1044
- const parser = math.parser()
1045
-
1046
- // get and set variables and functions
1047
- {
1048
- assert.strictEqual(parser.evaluate('x = 7 / 2'), 3.5)
1049
- assert.strictEqual(parser.evaluate('x + 3'), 6.5)
1050
- parser.evaluate('f(x, y) = x^y') // f(x, y)
1051
- assert.strictEqual(parser.evaluate('f(2, 3)'), 8)
1052
-
1053
- const _x = parser.get('x')
1054
- const f = parser.get('f')
1055
- const _y = parser.getAll()
1056
- const _g = f(3, 3)
1057
-
1058
- parser.set('h', 500)
1059
- parser.set('hello', (name: string) => `hello, ${name}!`)
1060
- }
1061
-
1062
- // clear defined functions and variables
1063
- parser.clear()
1064
- }
1065
-
1066
- /*
1067
- Fractions examples
1068
- */
1069
- {
1070
- // configure the default type of numbers as Fractions
1071
- const math = create(all, {
1072
- number: 'Fraction',
1073
- })
1074
-
1075
- const x = math.fraction(0.125)
1076
- const y = math.fraction('1/3')
1077
- math.fraction(2, 3)
1078
-
1079
- math.add(x, y)
1080
- math.divide(x, y)
1081
-
1082
- // output formatting
1083
- const _a = math.fraction('2/3')
1084
- }
1085
-
1086
- /*
1087
- Transform examples
1088
- */
1089
- {
1090
- const math = create(all, {})
1091
- {
1092
- const myTransform1 = (node: MathNode): OperatorNode<'+', 'add'> =>
1093
- new OperatorNode('+', 'add', [node, new ConstantNode(1)])
1094
- const myTransform2 = (
1095
- node: OperatorNode<'+', 'add'>
1096
- ): OperatorNode<'-', 'subtract'> =>
1097
- new OperatorNode('-', 'subtract', [node, new ConstantNode(5)])
1098
-
1099
- expectTypeOf(
1100
- math.parse('sqrt(3^2 + 4^2)').transform(myTransform1)
1101
- ).toMatchTypeOf<OperatorNode<'+', 'add', MathNode[]>>()
1102
-
1103
- assert.deepStrictEqual(
1104
- math.parse('sqrt(3^2 + 4^2)').transform(myTransform1).toString(),
1105
- 'sqrt(3 ^ 2 + 4 ^ 2) + 1'
1106
- )
1107
-
1108
- expectTypeOf(
1109
- math
1110
- .parse('sqrt(3^2 + 4^2)')
1111
- .transform(myTransform1)
1112
- .transform(myTransform2)
1113
- ).toMatchTypeOf<OperatorNode<'-', 'subtract', MathNode[]>>()
1114
-
1115
- assert.deepStrictEqual(
1116
- math
1117
- .parse('sqrt(3^2 + 4^2)')
1118
- .transform(myTransform1)
1119
- .transform(myTransform2)
1120
- .toString(),
1121
- 'sqrt(3 ^ 2 + 4 ^ 2) + 1 - 5'
1122
- )
1123
- }
1124
- }
1125
-
1126
- /*
1127
- Matrices examples
1128
- */
1129
- {
1130
- const math = create(all, {})
1131
-
1132
- // create matrices and arrays. a matrix is just a wrapper around an Array,
1133
- // providing some handy utilities.
1134
- const a: Matrix = math.matrix([1, 4, 9, 16, 25])
1135
- const b: Matrix = math.matrix(math.ones([2, 3]))
1136
- b.size()
1137
-
1138
- // @ts-expect-error ... ones() in a chain cannot take more dimensions
1139
- assert.throws(() => math.chain(3).ones(2, 'dense').done(), /Error:.*ones/)
1140
- // @ts-expect-error ... and neither can zeros()
1141
- assert.throws(() => math.chain(3).zeros(2, 'sparse').done(), /Error:.*zeros/)
1142
-
1143
- // the Array data of a Matrix can be retrieved using valueOf()
1144
- const _array = a.valueOf()
1145
-
1146
- // Matrices can be cloned
1147
- const _clone: Matrix = a.clone()
1148
-
1149
- // perform operations with matrices
1150
- math.map(a, math.sqrt)
1151
- math.factorial(a)
1152
-
1153
- // create and manipulate matrices. Arrays and Matrices can be used mixed.
1154
- {
1155
- const a = [
1156
- [1, 2],
1157
- [3, 4],
1158
- ]
1159
- const b: Matrix = math.matrix([
1160
- [5, 6],
1161
- [1, 1],
1162
- ])
1163
-
1164
- b.subset(math.index(1, [0, 1]), [[7, 8]])
1165
- const _c = math.multiply(a, b)
1166
- const f: Matrix = math.matrix([1, 0])
1167
- const _d: Matrix = f.subset(math.index(1))
1168
- }
1169
-
1170
- // get a sub matrix
1171
- {
1172
- const a: Matrix = math.diag(math.range(1, 4))
1173
- a.subset(math.index([1, 2], [1, 2]))
1174
- const b: Matrix = math.range(1, 6)
1175
- b.subset(math.index(math.range(1, 4)))
1176
- }
1177
-
1178
- // resize a multi dimensional matrix
1179
- {
1180
- const a = math.matrix()
1181
- a.resize([2, 2, 2], 0)
1182
- a.size()
1183
- a.resize([2, 2])
1184
- a.size()
1185
- }
1186
-
1187
- // can set a subset of a matrix to uninitialized
1188
- {
1189
- const m = math.matrix()
1190
- m.subset(math.index(2), 6, math.uninitialized)
1191
- }
1192
-
1193
- // create ranges
1194
- {
1195
- math.range(1, 6)
1196
- math.range(0, 18, 3)
1197
- math.range('2:-1:-3')
1198
- math.factorial(math.range('1:6'))
1199
- }
1200
-
1201
- // map matrix
1202
- {
1203
- assert.deepStrictEqual(
1204
- math.map([1, 2, 3], function (value) {
1205
- return value * value
1206
- }),
1207
- [1, 4, 9]
1208
- )
1209
- }
1210
-
1211
- // filter matrix
1212
- {
1213
- assert.deepStrictEqual(
1214
- math.filter([6, -2, -1, 4, 3], function (x) {
1215
- return x > 0
1216
- }),
1217
- [6, 4, 3]
1218
- )
1219
- assert.deepStrictEqual(
1220
- math.filter(['23', 'foo', '100', '55', 'bar'], /\d+/),
1221
- ['23', '100', '55']
1222
- )
1223
- }
1224
-
1225
- // concat matrix
1226
- {
1227
- assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]]), [
1228
- [0, 1, 2, 1, 2, 3],
1229
- ])
1230
- assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]], 0), [
1231
- [0, 1, 2],
1232
- [1, 2, 3],
1233
- ])
1234
- }
1235
-
1236
- // Matrix is available as a constructor for instanceof checks
1237
- {
1238
- assert.strictEqual(math.matrix([1, 2, 3]) instanceof math.Matrix, true)
1239
- }
1240
-
1241
- // Fourier transform and inverse
1242
- {
1243
- assert.ok(
1244
- math.deepEqual(
1245
- math.fft([
1246
- [1, 0],
1247
- [1, 0],
1248
- ]),
1249
- [
1250
- [math.complex(2, 0), math.complex(2, 0)],
1251
- [math.complex(0, 0), math.complex(0, 0)],
1252
- ]
1253
- )
1254
- )
1255
- assert.ok(
1256
- math.deepEqual(
1257
- math.fft(
1258
- math.matrix([
1259
- [1, 0],
1260
- [1, 0],
1261
- ])
1262
- ),
1263
- math.matrix([
1264
- [math.complex(2, 0), math.complex(2, 0)],
1265
- [math.complex(0, 0), math.complex(0, 0)],
1266
- ])
1267
- )
1268
- )
1269
- assert.ok(
1270
- math.deepEqual(
1271
- math.ifft([
1272
- [2, 2],
1273
- [0, 0],
1274
- ]),
1275
- [
1276
- [math.complex(1, 0), math.complex(0, 0)],
1277
- [math.complex(1, 0), math.complex(0, 0)],
1278
- ]
1279
- )
1280
- )
1281
- assert.ok(
1282
- math.deepEqual(
1283
- math.ifft(
1284
- math.matrix([
1285
- [2, 2],
1286
- [0, 0],
1287
- ])
1288
- ),
1289
- math.matrix([
1290
- [math.complex(1, 0), math.complex(0, 0)],
1291
- [math.complex(1, 0), math.complex(0, 0)],
1292
- ])
1293
- )
1294
- )
1295
- }
1296
-
1297
- // Moore–Penrose inverse
1298
- {
1299
- assert.ok(
1300
- math.deepEqual(
1301
- math.pinv([
1302
- [1, 2],
1303
- [3, 4],
1304
- ]),
1305
- [
1306
- [-2, 1],
1307
- [1.5, -0.5],
1308
- ]
1309
- )
1310
- )
1311
- assert.ok(
1312
- math.deepEqual(
1313
- math.pinv(
1314
- math.matrix([
1315
- [1, 2],
1316
- [3, 4],
1317
- ])
1318
- ),
1319
- math.matrix([
1320
- [-2, 1],
1321
- [1.5, -0.5],
1322
- ])
1323
- )
1324
- )
1325
- assert.ok(math.deepEqual(math.pinv(4), 0.25))
1326
- }
1327
- }
1328
-
1329
- /*
1330
- Math types examples: Type results after multiplying 'MathTypes' with matrices
1331
- */
1332
- {
1333
- const math = create(all, {})
1334
-
1335
- const abc: MathArray = [1, 2, 3, 4]
1336
- const bcd: MathArray = [
1337
- [1, 2, 3, 4],
1338
- [2, 3, 4, 5],
1339
- [4, 5, 6, 7],
1340
- [5, 6, 7, 8],
1341
- ]
1342
-
1343
- const Mbcd = math.matrix(bcd)
1344
- const Mabc = math.matrix(abc)
1345
-
1346
- // Number
1347
- const _r1 = math.multiply(1, 2)
1348
-
1349
- // Unit
1350
- const a = math.unit(45, 'cm') // 450 mm
1351
- const b = math.unit(45, 'cm') // 450 mm
1352
- const _r2 = math.multiply(a, b)
1353
-
1354
- // 1D JS Array
1355
- const r3 = math.multiply(abc, bcd)
1356
- const _r31 = r3[1] // By default least promised valid syntax
1357
-
1358
- // 2D JS Array
1359
- const r12 = math.multiply(bcd, bcd)
1360
- // Example to sort ambiguity between multidimensional & singledimensional arrays
1361
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1362
- const multiDimensional = (x: any): x is any[][] => x.length && x[0].length
1363
- if (multiDimensional(r12)) {
1364
- const _r1211 = r12[1][1]
1365
- }
1366
- const _r121 = r12[1] // Valid syntax
1367
-
1368
- // Matrix: matrix * vector
1369
- const r7 = math.multiply(Mabc, bcd)
1370
- r7.toArray() // Matrix-es have toArray function
1371
-
1372
- // Matrix
1373
- const _r8 = math.multiply(Mabc, 4)
1374
-
1375
- // Matrix
1376
- const _r11 = math.multiply(4, Mabc)
1377
-
1378
- // Matrix of units
1379
- const _r9 = math.multiply(Mabc, a)
1380
- const _r10 = math.multiply(a, Mabc)
1381
-
1382
- // Matrix
1383
- const _r6 = math.multiply(abc, Mbcd)
1384
-
1385
- // 2D JS Array
1386
- const _r5 = math.multiply(bcd, abc)
1387
-
1388
- // Number
1389
- const _r4 = math.multiply(abc, math.transpose(abc))
1390
- }
1391
-
1392
- /*
1393
- Sparse matrices examples
1394
- */
1395
- {
1396
- const math = create(all, {})
1397
-
1398
- // create a sparse matrix
1399
- const a = math.identity(1000, 1000, 'sparse')
1400
-
1401
- // do operations with a sparse matrix
1402
- const b = math.multiply(a, a)
1403
- const _c = math.multiply(b, math.complex(2, 2))
1404
- const d = math.matrix([0, 1])
1405
- const e = math.transpose(d)
1406
- const _f = math.multiply(e, d)
1407
- }
1408
-
1409
- /*
1410
- Units examples
1411
- */
1412
- {
1413
- const math = create(all, {})
1414
-
1415
- // units can be created by providing a value and unit name, or by providing
1416
- // a string with a valued unit.
1417
- const a = math.unit(45, 'cm') // 450 mm
1418
- const b = math.unit('0.1m') // 100 mm
1419
- const _c = math.unit(b)
1420
-
1421
- // creating units
1422
- math.createUnit('foo')
1423
- math.createUnit('furlong', '220 yards')
1424
- math.createUnit('furlong', '220 yards', { override: true })
1425
- math.createUnit('testunit', { definition: '0.555556 kelvin', offset: 459.67 })
1426
- math.createUnit(
1427
- 'testunit',
1428
- { definition: '0.555556 kelvin', offset: 459.67 },
1429
- { override: true }
1430
- )
1431
- math.createUnit('knot', {
1432
- definition: '0.514444 m/s',
1433
- aliases: ['knots', 'kt', 'kts'],
1434
- })
1435
- math.createUnit(
1436
- 'knot',
1437
- { definition: '0.514444 m/s', aliases: ['knots', 'kt', 'kts'] },
1438
- { override: true }
1439
- )
1440
- math.createUnit(
1441
- 'knot',
1442
- {
1443
- definition: '0.514444 m/s',
1444
- aliases: ['knots', 'kt', 'kts'],
1445
- prefixes: 'long',
1446
- },
1447
- { override: true }
1448
- )
1449
- math.createUnit(
1450
- {
1451
- foo2: {
1452
- prefixes: 'long',
1453
- },
1454
- bar: '40 foo',
1455
- baz: {
1456
- definition: '1 bar/hour',
1457
- prefixes: 'long',
1458
- },
1459
- },
1460
- {
1461
- override: true,
1462
- }
1463
- )
1464
- // use Unit as definition
1465
- math.createUnit('c', { definition: b })
1466
- math.createUnit('c', { definition: b }, { override: true })
1467
- math.createUnit('customUnit', math.unit(0.5, 'm'))
1468
-
1469
- // units can be added, subtracted, and multiplied or divided by numbers and by other units
1470
- math.add(a, b)
1471
- math.multiply(b, 2)
1472
- math.divide(math.unit('1 m'), math.unit('1 s'))
1473
- math.pow(math.unit('12 in'), 3)
1474
-
1475
- // units can be converted to a specific type, or to a number
1476
- b.to('cm')
1477
- math.to(b, 'inch')
1478
- b.toNumber('cm')
1479
- math.number(b, 'cm')
1480
-
1481
- // the expression parser supports units too
1482
- math.evaluate('2 inch to cm')
1483
-
1484
- // units can be converted to SI
1485
- math.unit('1 inch').toSI()
1486
-
1487
- // units can be split into other units
1488
- math.unit('1 m').splitUnit(['ft', 'in'])
1489
- }
1490
-
1491
- /*
1492
- Expression tree examples
1493
- */
1494
- {
1495
- const math = create(all, {})
1496
-
1497
- // Filter an expression tree
1498
- const node = math.parse('x^2 + x/4 + 3*y')
1499
- const filtered = node.filter(
1500
- (node) => isSymbolNode(node) && node.name === 'x'
1501
- )
1502
-
1503
- const _arr: string[] = filtered.map((node: MathNode) => node.toString())
1504
-
1505
- // Traverse an expression tree
1506
- const node1: MathNode = math.parse('3 * x + 2')
1507
- node1.traverse((node: MathNode, _path: string, _parent: MathNode) => {
1508
- switch (node.type) {
1509
- case 'OperatorNode':
1510
- return node.type === 'OperatorNode'
1511
- case 'ConstantNode':
1512
- return node.type === 'ConstantNode'
1513
- case 'SymbolNode':
1514
- return node.type === 'SymbolNode'
1515
- default:
1516
- return
1517
- }
1518
- })
1519
- }
1520
-
1521
- /*
1522
- Function ceil examples
1523
- */
1524
- {
1525
- const math = create(all, {})
1526
-
1527
- // number input
1528
- assert.strictEqual(math.ceil(3.2), 4)
1529
- assert.strictEqual(math.ceil(-4.2), -4)
1530
-
1531
- // number input
1532
- // roundoff result to 2 decimals
1533
- assert.strictEqual(math.ceil(3.212, 2), 3.22)
1534
- assert.deepStrictEqual(
1535
- math.ceil(3.212, math.bignumber(2)),
1536
- math.bignumber(3.22)
1537
- )
1538
- assert.strictEqual(math.ceil(-4.212, 2), -4.21)
1539
-
1540
- // bignumber input
1541
- assert.deepStrictEqual(math.ceil(math.bignumber(3.212)), math.bignumber(4))
1542
- assert.deepStrictEqual(
1543
- math.ceil(math.bignumber(3.212), 2),
1544
- math.bignumber(3.22)
1545
- )
1546
- assert.deepStrictEqual(
1547
- math.ceil(math.bignumber(3.212), math.bignumber(2)),
1548
- math.bignumber(3.22)
1549
- )
1550
-
1551
- // fraction input
1552
- assert.deepStrictEqual(math.ceil(math.fraction(44, 7)), math.fraction(7))
1553
- assert.deepStrictEqual(math.ceil(math.fraction(-44, 7)), math.fraction(-6))
1554
- assert.deepStrictEqual(
1555
- math.ceil(math.fraction(44, 7), 2),
1556
- math.fraction(629, 100)
1557
- )
1558
- assert.deepStrictEqual(
1559
- math.ceil(math.fraction(44, 7), math.bignumber(2)),
1560
- math.fraction(629, 100)
1561
- )
1562
-
1563
- // Complex input
1564
- const c = math.complex(3.24, -2.71)
1565
- assert.deepStrictEqual(math.ceil(c), math.complex(4, -2))
1566
- assert.deepStrictEqual(math.ceil(c, 1), math.complex(3.3, -2.7))
1567
- assert.deepStrictEqual(
1568
- math.ceil(c, math.bignumber(1)),
1569
- math.complex(3.3, -2.7)
1570
- )
1571
-
1572
- // array input
1573
- assert.deepStrictEqual(math.ceil([3.2, 3.8, -4.7]), [4, 4, -4])
1574
- assert.deepStrictEqual(math.ceil([3.21, 3.82, -4.71], 1), [3.3, 3.9, -4.7])
1575
- assert.deepStrictEqual(
1576
- math.ceil([3.21, 3.82, -4.71], math.bignumber(1)),
1577
- math.bignumber([3.3, 3.9, -4.7])
1578
- )
1579
-
1580
- // numeric input, array or matrix of decimals
1581
- const numCeiled: MathArray = math.ceil(math.tau, [2, 3])
1582
- assert.deepStrictEqual(numCeiled, [6.29, 6.284])
1583
- const bigCeiled: Matrix = math.ceil(
1584
- math.bignumber(6.28318),
1585
- math.matrix([2, 3])
1586
- )
1587
- assert.deepStrictEqual(bigCeiled, math.matrix(math.bignumber([6.29, 6.284])))
1588
- assert.deepStrictEqual(math.ceil(math.fraction(44, 7), [2, 3]), [
1589
- math.fraction(629, 100),
1590
- math.fraction(6286, 1000),
1591
- ])
1592
-
1593
- // @ts-expect-error ... verify ceil(array, array) throws an error (for now)
1594
- assert.throws(() => math.ceil([3.21, 3.82], [1, 2]), TypeError)
1595
- }
1596
-
1597
- /*
1598
- Function fix examples
1599
- */
1600
- {
1601
- const math = create(all, {})
1602
-
1603
- // number input
1604
- assert.strictEqual(math.fix(3.2), 3)
1605
- assert.strictEqual(math.fix(-4.2), -4)
1606
-
1607
- // number input
1608
- // roundoff result to 2 decimals
1609
- assert.strictEqual(math.fix(3.212, 2), 3.21)
1610
- assert.deepStrictEqual(
1611
- math.fix(3.212, math.bignumber(2)),
1612
- math.bignumber(3.21)
1613
- )
1614
- assert.strictEqual(math.fix(-4.212, 2), -4.21)
1615
-
1616
- // bignumber input
1617
- assert.deepStrictEqual(math.fix(math.bignumber(3.212)), math.bignumber(3))
1618
- assert.deepStrictEqual(
1619
- math.fix(math.bignumber(3.212), 2),
1620
- math.bignumber(3.21)
1621
- )
1622
- assert.deepStrictEqual(
1623
- math.fix(math.bignumber(3.212), math.bignumber(2)),
1624
- math.bignumber(3.21)
1625
- )
1626
-
1627
- // fraction input
1628
- assert.deepStrictEqual(math.fix(math.fraction(44, 7)), math.fraction(6))
1629
- assert.deepStrictEqual(math.fix(math.fraction(-44, 7)), math.fraction(-6))
1630
- assert.deepStrictEqual(
1631
- math.fix(math.fraction(44, 7), 2),
1632
- math.fraction(628, 100)
1633
- )
1634
- assert.deepStrictEqual(
1635
- math.fix(math.fraction(44, 7), math.bignumber(2)),
1636
- math.fraction(628, 100)
1637
- )
1638
-
1639
- // Complex input
1640
- const c = math.complex(3.24, -2.71)
1641
- assert.deepStrictEqual(math.fix(c), math.complex(3, -2))
1642
- assert.deepStrictEqual(math.fix(c, 1), math.complex(3.2, -2.7))
1643
- assert.deepStrictEqual(
1644
- math.fix(c, math.bignumber(1)),
1645
- math.complex(3.2, -2.7)
1646
- )
1647
-
1648
- // array input
1649
- assert.deepStrictEqual(math.fix([3.2, 3.8, -4.7]), [3, 3, -4])
1650
- assert.deepStrictEqual(math.fix([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.7])
1651
- assert.deepStrictEqual(
1652
- math.fix([3.21, 3.82, -4.71], math.bignumber(1)),
1653
- math.bignumber([3.2, 3.8, -4.7])
1654
- )
1655
-
1656
- // numeric input, array or matrix of decimals
1657
- const numFixed: MathArray = math.fix(math.tau, [2, 3])
1658
- assert.deepStrictEqual(numFixed, [6.28, 6.283])
1659
- const bigFixed: Matrix = math.fix(
1660
- math.bignumber(6.28318),
1661
- math.matrix([2, 3])
1662
- )
1663
- assert.deepStrictEqual(bigFixed, math.matrix(math.bignumber([6.28, 6.283])))
1664
- assert.deepStrictEqual(math.fix(math.fraction(44, 7), [2, 3]), [
1665
- math.fraction(628, 100),
1666
- math.fraction(6285, 1000),
1667
- ])
1668
-
1669
- // @ts-expect-error ... verify fix(array, array) throws an error (for now)
1670
- assert.throws(() => math.fix([3.21, 3.82], [1, 2]), TypeError)
1671
- }
1672
-
1673
- /*
1674
- Function floor examples
1675
- */
1676
- {
1677
- const math = create(all, {})
1678
-
1679
- // number input
1680
- assert.strictEqual(math.floor(3.2), 3)
1681
- assert.strictEqual(math.floor(-4.2), -5)
1682
-
1683
- // number input
1684
- // roundoff result to 2 decimals
1685
- assert.strictEqual(math.floor(3.212, 2), 3.21)
1686
- assert.deepStrictEqual(
1687
- math.floor(3.212, math.bignumber(2)),
1688
- math.bignumber(3.21)
1689
- )
1690
- assert.strictEqual(math.floor(-4.212, 2), -4.22)
1691
-
1692
- // bignumber input
1693
- assert.deepStrictEqual(math.floor(math.bignumber(3.212)), math.bignumber(3))
1694
- assert.deepStrictEqual(
1695
- math.floor(math.bignumber(3.212), 2),
1696
- math.bignumber(3.21)
1697
- )
1698
- assert.deepStrictEqual(
1699
- math.floor(math.bignumber(3.212), math.bignumber(2)),
1700
- math.bignumber(3.21)
1701
- )
1702
-
1703
- // fraction input
1704
- assert.deepStrictEqual(math.floor(math.fraction(44, 7)), math.fraction(6))
1705
- assert.deepStrictEqual(math.floor(math.fraction(-44, 7)), math.fraction(-7))
1706
- assert.deepStrictEqual(
1707
- math.floor(math.fraction(44, 7), 2),
1708
- math.fraction(628, 100)
1709
- )
1710
- assert.deepStrictEqual(
1711
- math.floor(math.fraction(44, 7), math.bignumber(2)),
1712
- math.fraction(628, 100)
1713
- )
1714
-
1715
- // Complex input
1716
- const c = math.complex(3.24, -2.71)
1717
- assert.deepStrictEqual(math.floor(c), math.complex(3, -3))
1718
- assert.deepStrictEqual(math.floor(c, 1), math.complex(3.2, -2.8))
1719
- assert.deepStrictEqual(
1720
- math.floor(c, math.bignumber(1)),
1721
- math.complex(3.2, -2.8)
1722
- )
1723
-
1724
- // array input
1725
- assert.deepStrictEqual(math.floor([3.2, 3.8, -4.7]), [3, 3, -5])
1726
- assert.deepStrictEqual(math.floor([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.8])
1727
- assert.deepStrictEqual(
1728
- math.floor([3.21, 3.82, -4.71], math.bignumber(1)),
1729
- math.bignumber([3.2, 3.8, -4.8])
1730
- )
1731
-
1732
- // numeric input, array or matrix of decimals
1733
- const numFloored: MathArray = math.floor(math.tau, [2, 3])
1734
- assert.deepStrictEqual(numFloored, [6.28, 6.283])
1735
- const bigFloored: Matrix = math.floor(
1736
- math.bignumber(6.28318),
1737
- math.matrix([2, 3])
1738
- )
1739
- assert.deepStrictEqual(bigFloored, math.matrix(math.bignumber([6.28, 6.283])))
1740
- assert.deepStrictEqual(math.floor(math.fraction(44, 7), [2, 3]), [
1741
- math.fraction(628, 100),
1742
- math.fraction(6285, 1000),
1743
- ])
1744
-
1745
- // @ts-expect-error ... verify floor(array, array) throws an error (for now)
1746
- assert.throws(() => math.floor([3.21, 3.82], [1, 2]), TypeError)
1747
- }
1748
-
1749
- /*
1750
- Function round examples
1751
- */
1752
- {
1753
- const math = create(all, {})
1754
-
1755
- // number input
1756
- assert.strictEqual(math.round(3.2), 3)
1757
- assert.strictEqual(math.round(-4.2), -4)
1758
-
1759
- // number input
1760
- // roundoff result to 2 decimals
1761
- assert.strictEqual(math.round(3.212, 2), 3.21)
1762
- assert.deepStrictEqual(
1763
- math.round(3.212, math.bignumber(2)),
1764
- math.bignumber(3.21)
1765
- )
1766
- assert.strictEqual(math.round(-4.212, 2), -4.21)
1767
-
1768
- // bignumber input
1769
- assert.deepStrictEqual(math.round(math.bignumber(3.212)), math.bignumber(3))
1770
- assert.deepStrictEqual(
1771
- math.round(math.bignumber(3.212), 2),
1772
- math.bignumber(3.21)
1773
- )
1774
- assert.deepStrictEqual(
1775
- math.round(math.bignumber(3.212), math.bignumber(2)),
1776
- math.bignumber(3.21)
1777
- )
1778
-
1779
- // fraction input
1780
- assert.deepStrictEqual(math.round(math.fraction(44, 7)), math.fraction(6))
1781
- assert.deepStrictEqual(math.round(math.fraction(-44, 7)), math.fraction(-6))
1782
- assert.deepStrictEqual(
1783
- math.round(math.fraction(44, 7), 2),
1784
- math.fraction(629, 100)
1785
- )
1786
- assert.deepStrictEqual(
1787
- math.round(math.fraction(44, 7), math.bignumber(2)),
1788
- math.fraction(629, 100)
1789
- )
1790
-
1791
- // Complex input
1792
- const c = math.complex(3.24, -2.71)
1793
- assert.deepStrictEqual(math.round(c), math.complex(3, -3))
1794
- assert.deepStrictEqual(math.round(c, 1), math.complex(3.2, -2.7))
1795
- assert.deepStrictEqual(
1796
- math.round(c, math.bignumber(1)),
1797
- math.complex(3.2, -2.7)
1798
- )
1799
-
1800
- // array input
1801
- assert.deepStrictEqual(math.round([3.2, 3.8, -4.7]), [3, 4, -5])
1802
- assert.deepStrictEqual(math.round([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.7])
1803
- assert.deepStrictEqual(
1804
- math.round([3.21, 3.82, -4.71], math.bignumber(1)),
1805
- math.bignumber([3.2, 3.8, -4.7])
1806
- )
1807
-
1808
- // numeric input, array or matrix of decimals
1809
- const numRounded: MathArray = math.round(math.tau, [2, 3])
1810
- assert.deepStrictEqual(numRounded, [6.28, 6.283])
1811
- const bigRounded: Matrix = math.round(
1812
- math.bignumber(6.28318),
1813
- math.matrix([2, 3])
1814
- )
1815
- assert.deepStrictEqual(bigRounded, math.matrix(math.bignumber([6.28, 6.283])))
1816
- assert.deepStrictEqual(math.round(math.fraction(44, 7), [2, 3]), [
1817
- math.fraction(629, 100),
1818
- math.fraction(6286, 1000),
1819
- ])
1820
-
1821
- // @ts-expect-error ... verify round(array, array) throws an error (for now)
1822
- assert.throws(() => math.round([3.21, 3.82], [1, 2]), TypeError)
1823
- }
1824
-
1825
- /*
1826
- Clone examples
1827
- */
1828
- {
1829
- const math = create(all, {})
1830
- expectTypeOf(
1831
- new math.OperatorNode('/', 'divide', [
1832
- new math.ConstantNode(3),
1833
- new math.SymbolNode('x'),
1834
- ])
1835
- ).toMatchTypeOf<OperatorNode<'/', 'divide', (ConstantNode | SymbolNode)[]>>()
1836
-
1837
- expectTypeOf(new math.ConstantNode(1).clone()).toMatchTypeOf<ConstantNode>()
1838
- expectTypeOf(
1839
- new math.OperatorNode('*', 'multiply', [
1840
- new math.ConstantNode(3),
1841
- new math.SymbolNode('x'),
1842
- ]).clone()
1843
- ).toMatchTypeOf<
1844
- OperatorNode<'*', 'multiply', (ConstantNode | SymbolNode)[]>
1845
- >()
1846
-
1847
- expectTypeOf(
1848
- new math.ConstantNode(1).cloneDeep()
1849
- ).toMatchTypeOf<ConstantNode>()
1850
- expectTypeOf(
1851
- new math.OperatorNode('+', 'unaryPlus', [
1852
- new math.ConstantNode(3),
1853
- new math.SymbolNode('x'),
1854
- ]).cloneDeep()
1855
- ).toMatchTypeOf<
1856
- OperatorNode<'+', 'unaryPlus', (ConstantNode | SymbolNode)[]>
1857
- >()
1858
-
1859
- expectTypeOf(
1860
- math.clone(new math.ConstantNode(1))
1861
- ).toMatchTypeOf<ConstantNode>()
1862
- }
1863
-
1864
- /*
1865
- JSON serialization/deserialization
1866
- */
1867
- {
1868
- const math = create(all, {})
1869
-
1870
- const data = {
1871
- bigNumber: math.bignumber('1.5'),
1872
- }
1873
- const stringified = JSON.stringify(data)
1874
- const parsed = JSON.parse(stringified, math.reviver)
1875
- assert.deepStrictEqual(parsed.bigNumber, math.bignumber('1.5'))
1876
- }
1877
-
1878
- /*
1879
- Extend functionality with import
1880
- */
1881
-
1882
- declare module 'mathjs' {
1883
- interface MathJsStatic {
1884
- testFun(): number
1885
- value: number
1886
- }
1887
- }
1888
-
1889
- {
1890
- const math = create(all, {})
1891
- const testFun = () => 5
1892
-
1893
- math.import(
1894
- {
1895
- testFun,
1896
- value: 10,
1897
- },
1898
- {}
1899
- )
1900
-
1901
- math.testFun()
1902
-
1903
- expectTypeOf(math.testFun()).toMatchTypeOf<number>()
1904
-
1905
- expectTypeOf(
1906
- math.import({
1907
- myvalue: 42,
1908
- myFunc: (name: string) => `myFunc ${name}`,
1909
- })
1910
- ).toMatchTypeOf<void>()
1911
-
1912
- expectTypeOf(
1913
- math.import(
1914
- {
1915
- myvalue: 42,
1916
- myFunc: (name: string) => `myFunc ${name}`,
1917
- },
1918
- {
1919
- override: true,
1920
- }
1921
- )
1922
- ).toMatchTypeOf<void>()
1923
-
1924
- expectTypeOf(
1925
- math.import(
1926
- {
1927
- myvalue2: 42,
1928
- },
1929
- {
1930
- silent: true,
1931
- }
1932
- )
1933
- ).toMatchTypeOf<void>()
1934
-
1935
- expectTypeOf(
1936
- math.import(
1937
- {
1938
- myvalue3: 42,
1939
- },
1940
- {
1941
- wrap: true,
1942
- }
1943
- )
1944
- ).toMatchTypeOf<void>()
1945
-
1946
- expectTypeOf(
1947
- math.import({
1948
- myvalue4: 42,
1949
- })
1950
- ).toMatchTypeOf<void>()
1951
-
1952
- expectTypeOf(
1953
- math.import([
1954
- {
1955
- myvalue5: 42,
1956
- },
1957
- {
1958
- myFunc2: (name: string) => `myFunc2 ${name}`,
1959
- },
1960
- ])
1961
- ).toMatchTypeOf<void>()
1962
- }
1963
-
1964
- /*
1965
- Renamed functions from v5 => v6
1966
- */
1967
- {
1968
- const math = create(all, {})
1969
- math.typeOf(1)
1970
- math.variance([1, 2, 3, 4])
1971
- math.evaluate('1 + 2')
1972
-
1973
- // chained operations
1974
- math.chain(3).typeOf().done()
1975
- math.chain([1, 2, 3]).variance().done()
1976
- math.chain('1 + 2').evaluate().done()
1977
- }
1978
-
1979
- /*
1980
- Factory Test
1981
- */
1982
- {
1983
- // create a factory function
1984
- const name = 'negativeSquare'
1985
- const dependencies: MathJsFunctionName[] = ['multiply', 'unaryMinus']
1986
- const createNegativeSquare = factory(name, dependencies, (injected) => {
1987
- const { multiply, unaryMinus } = injected
1988
- return function negativeSquare(x: number): number {
1989
- return unaryMinus(multiply(x, x))
1990
- }
1991
- })
1992
-
1993
- // create an instance of the function yourself:
1994
- const multiply = (a: number, b: number) => a * b
1995
- const unaryMinus = (a: number) => -a
1996
- const negativeSquare = createNegativeSquare({ multiply, unaryMinus })
1997
- negativeSquare(3)
1998
- }
1999
-
2000
- /**
2001
- * Dependency map typing test from mathjs official document:
2002
- * https://mathjs.org/docs/custom_bundling.html#using-just-a-few-functions
2003
- */
2004
- {
2005
- const config = {
2006
- // optionally, you can specify configuration
2007
- }
2008
-
2009
- // Create just the functions we need
2010
- const { fraction, add, divide, format } = create(
2011
- {
2012
- fractionDependencies,
2013
- addDependencies,
2014
- divideDependencies,
2015
- formatDependencies,
2016
- },
2017
- config
2018
- )
2019
-
2020
- // Use the created functions
2021
- const a = fraction(1, 3)
2022
- const b = fraction(3, 7)
2023
- const c = add(a, b)
2024
- const d = divide(a, b)
2025
- assert.strictEqual(format(c), '16/21')
2026
- assert.strictEqual(format(d), '7/9')
2027
- }
2028
-
2029
- /**
2030
- * Custom parsing functions
2031
- * https://mathjs.org/docs/expressions/customization.html#customize-supported-characters
2032
- */
2033
- {
2034
- const math = create(all, {})
2035
- const isAlphaOriginal = math.parse.isAlpha
2036
- math.parse.isAlpha = (c, cPrev, cNext) => {
2037
- return isAlphaOriginal(c, cPrev, cNext) || c === '\u260E'
2038
- }
2039
-
2040
- // now we can use the \u260E (phone) character in expressions
2041
- const result = math.evaluate('\u260Efoo', { '\u260Efoo': 42 })
2042
- assert.strictEqual(result, 42)
2043
- }
2044
-
2045
- /**
2046
- * Util functions
2047
- * https://mathjs.org/docs/reference/functions.html#utils-functions
2048
- */
2049
- {
2050
- const math = create(all, {})
2051
-
2052
- // hasNumericValue function
2053
- assert.strictEqual(math.hasNumericValue(2), true)
2054
- assert.strictEqual(math.hasNumericValue('2'), true)
2055
- assert.strictEqual(math.isNumeric('2'), false)
2056
- assert.strictEqual(math.hasNumericValue(0), true)
2057
- assert.strictEqual(math.hasNumericValue(math.bignumber(500)), true)
2058
- assert.deepStrictEqual(math.hasNumericValue([2.3, 'foo', false]), [
2059
- true,
2060
- false,
2061
- true,
2062
- ])
2063
- assert.strictEqual(math.hasNumericValue(math.fraction(4)), true)
2064
- assert.strictEqual(math.hasNumericValue(math.complex('2-4i')), false)
2065
- }
2066
-
2067
- /**
2068
- * src/util/is functions
2069
- */
2070
- {
2071
- const math = create(all, {})
2072
-
2073
- type IsFunc = (x: unknown) => boolean
2074
- const isFuncs: IsFunc[] = [
2075
- math.isNumber,
2076
- math.isBigNumber,
2077
- math.isComplex,
2078
- math.isFraction,
2079
- math.isUnit,
2080
- math.isString,
2081
- math.isArray,
2082
- math.isMatrix,
2083
- math.isCollection,
2084
- math.isDenseMatrix,
2085
- math.isSparseMatrix,
2086
- math.isRange,
2087
- math.isIndex,
2088
- math.isBoolean,
2089
- math.isResultSet,
2090
- math.isHelp,
2091
- math.isFunction,
2092
- math.isDate,
2093
- math.isRegExp,
2094
- math.isObject,
2095
- math.isNull,
2096
- math.isUndefined,
2097
- math.isAccessorNode,
2098
- math.isArrayNode,
2099
- math.isAssignmentNode,
2100
- math.isBlockNode,
2101
- math.isConditionalNode,
2102
- math.isConstantNode,
2103
- math.isFunctionAssignmentNode,
2104
- math.isFunctionNode,
2105
- math.isIndexNode,
2106
- math.isNode,
2107
- math.isObjectNode,
2108
- math.isOperatorNode,
2109
- math.isParenthesisNode,
2110
- math.isRangeNode,
2111
- math.isRelationalNode,
2112
- math.isSymbolNode,
2113
- math.isChain,
2114
- ]
2115
-
2116
- isFuncs.forEach((f) => {
2117
- const result = f(1)
2118
- const isResultBoolean = result === true || result === false
2119
- assert.ok(isResultBoolean)
2120
- })
2121
-
2122
- // Check guards do type refinement
2123
-
2124
- const x: unknown = undefined
2125
-
2126
- if (math.isNumber(x)) {
2127
- expectTypeOf(x).toMatchTypeOf<number>()
2128
- }
2129
- if (math.isBigNumber(x)) {
2130
- expectTypeOf(x).toMatchTypeOf<BigNumber>()
2131
- }
2132
- if (math.isComplex(x)) {
2133
- expectTypeOf(x).toMatchTypeOf<Complex>()
2134
- }
2135
- if (math.isFraction(x)) {
2136
- expectTypeOf(x).toMatchTypeOf<Fraction>()
2137
- }
2138
- if (math.isUnit(x)) {
2139
- expectTypeOf(x).toMatchTypeOf<Unit>()
2140
- }
2141
- if (math.isString(x)) {
2142
- expectTypeOf(x).toMatchTypeOf<string>()
2143
- }
2144
- if (math.isArray(x)) {
2145
- expectTypeOf(x).toMatchTypeOf<unknown[]>()
2146
- }
2147
- if (math.isMatrix(x)) {
2148
- expectTypeOf(x).toMatchTypeOf<Matrix>()
2149
- }
2150
- if (math.isDenseMatrix(x)) {
2151
- expectTypeOf(x).toMatchTypeOf<Matrix>()
2152
- }
2153
- if (math.isSparseMatrix(x)) {
2154
- expectTypeOf(x).toMatchTypeOf<Matrix>()
2155
- }
2156
- if (math.isIndex(x)) {
2157
- expectTypeOf(x).toMatchTypeOf<Index>()
2158
- }
2159
- if (math.isBoolean(x)) {
2160
- expectTypeOf(x).toMatchTypeOf<boolean>()
2161
- }
2162
- if (math.isHelp(x)) {
2163
- expectTypeOf(x).toMatchTypeOf<Help>()
2164
- }
2165
- if (math.isDate(x)) {
2166
- expectTypeOf(x).toMatchTypeOf<Date>()
2167
- }
2168
- if (math.isRegExp(x)) {
2169
- expectTypeOf(x).toMatchTypeOf<RegExp>()
2170
- }
2171
- if (math.isNull(x)) {
2172
- expectTypeOf(x).toMatchTypeOf<null>()
2173
- }
2174
- if (math.isUndefined(x)) {
2175
- expectTypeOf(x).toMatchTypeOf<undefined>()
2176
- }
2177
-
2178
- if (math.isAccessorNode(x)) {
2179
- expectTypeOf(x).toMatchTypeOf<AccessorNode>()
2180
- }
2181
- if (math.isArrayNode(x)) {
2182
- expectTypeOf(x).toMatchTypeOf<ArrayNode>()
2183
- }
2184
- if (math.isAssignmentNode(x)) {
2185
- expectTypeOf(x).toMatchTypeOf<AssignmentNode>()
2186
- }
2187
- if (math.isBlockNode(x)) {
2188
- expectTypeOf(x).toMatchTypeOf<BlockNode>()
2189
- }
2190
- if (math.isConditionalNode(x)) {
2191
- expectTypeOf(x).toMatchTypeOf<ConditionalNode>()
2192
- }
2193
- if (math.isConstantNode(x)) {
2194
- expectTypeOf(x).toMatchTypeOf<ConstantNode>()
2195
- }
2196
- if (math.isFunctionAssignmentNode(x)) {
2197
- expectTypeOf(x).toMatchTypeOf<FunctionAssignmentNode>()
2198
- }
2199
- if (math.isFunctionNode(x)) {
2200
- expectTypeOf(x).toMatchTypeOf<FunctionNode>()
2201
- }
2202
- if (math.isIndexNode(x)) {
2203
- expectTypeOf(x).toMatchTypeOf<IndexNode>()
2204
- }
2205
- if (math.isNode(x)) {
2206
- expectTypeOf(x).toMatchTypeOf<MathNode>()
2207
- }
2208
- if (math.isNode(x)) {
2209
- expectTypeOf(x).toMatchTypeOf<MathNode>()
2210
- }
2211
- if (math.isObjectNode(x)) {
2212
- expectTypeOf(x).toMatchTypeOf<ObjectNode>()
2213
- }
2214
- if (math.isOperatorNode(x)) {
2215
- expectTypeOf(x).toMatchTypeOf<
2216
- OperatorNode<OperatorNodeOp, OperatorNodeFn, MathNode[]>
2217
- >()
2218
- }
2219
- if (math.isParenthesisNode(x)) {
2220
- expectTypeOf(x).toMatchTypeOf<ParenthesisNode>()
2221
- }
2222
- if (math.isRangeNode(x)) {
2223
- expectTypeOf(x).toMatchTypeOf<RangeNode>()
2224
- }
2225
- if (math.isRelationalNode(x)) {
2226
- expectTypeOf(x).toMatchTypeOf<math.RelationalNode>()
2227
- }
2228
- if (math.isSymbolNode(x)) {
2229
- expectTypeOf(x).toMatchTypeOf<SymbolNode>()
2230
- }
2231
- if (math.isChain(x)) {
2232
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
2233
- expectTypeOf(x).toMatchTypeOf<MathJsChain<any>>()
2234
- }
2235
- }
2236
-
2237
- /*
2238
- Probability function examples
2239
- */
2240
- {
2241
- const math = create(all, {})
2242
-
2243
- expectTypeOf(math.lgamma(1.5)).toMatchTypeOf<number>()
2244
- expectTypeOf(math.lgamma(math.complex(1.5, -1.5))).toMatchTypeOf<Complex>()
2245
- }
2246
-
2247
- /*
2248
- toTex examples
2249
- */
2250
-
2251
- {
2252
- const math = create(all, {})
2253
-
2254
- expectTypeOf(math.parse('a/b').toTex()).toMatchTypeOf<string>()
2255
-
2256
- // TODO add proper types for toTex options
2257
- expectTypeOf(
2258
- math.parse('a/b').toTex({
2259
- a: '123',
2260
- })
2261
- ).toMatchTypeOf<string>()
2262
- }
2263
-
2264
- /*
2265
- Resolve examples
2266
- */
2267
- {
2268
- const math = create(all, {})
2269
-
2270
- expectTypeOf(math.resolve('x + y')).toMatchTypeOf<MathNode>()
2271
- expectTypeOf(math.resolve(math.parse('x + y'))).toMatchTypeOf<MathNode>()
2272
- expectTypeOf(
2273
- math.resolve(math.parse('x + y'), { x: 0 })
2274
- ).toMatchTypeOf<MathNode>()
2275
- expectTypeOf(math.resolve('x + y', { x: 0 })).toMatchTypeOf<MathNode>()
2276
- expectTypeOf(
2277
- math.resolve([math.parse('x + y'), 'x*x'], { x: 0 })
2278
- ).toMatchTypeOf<MathNode[]>()
2279
- expectTypeOf(math.resolve(math.matrix(['x', 'y']))).toMatchTypeOf<Matrix>()
2280
- }
2281
-
2282
- /*
2283
- Random examples
2284
- */
2285
- {
2286
- const math = create(all, {})
2287
- expectTypeOf(math.pickRandom([1, 2, 3])).toMatchTypeOf<number>()
2288
- expectTypeOf(math.pickRandom(['a', { b: 10 }, 42])).toMatchTypeOf<
2289
- string | number | { b: number }
2290
- >()
2291
- expectTypeOf(math.pickRandom([1, 2, 3])).toMatchTypeOf<number>()
2292
- expectTypeOf(math.pickRandom([1, 2, 3], 2)).toMatchTypeOf<number[]>()
2293
-
2294
- expectTypeOf(math.chain([1, 2, 3]).pickRandom(2)).toMatchTypeOf<
2295
- MathJsChain<number[]>
2296
- >()
2297
- }
2298
-
2299
- /*
2300
- MathNode examples
2301
- */
2302
- {
2303
- class CustomNode extends Node {
2304
- a: MathNode
2305
- constructor(a: MathNode) {
2306
- super()
2307
- this.a = a
2308
- }
2309
- }
2310
-
2311
- // Basic node
2312
- const instance1 = new Node()
2313
-
2314
- // Built-in subclass of Node
2315
- const instance2 = new ConstantNode(2)
2316
-
2317
- // Custom subclass of node
2318
- const instance3 = new CustomNode(new ConstantNode(2))
2319
-
2320
- expectTypeOf(instance1).toMatchTypeOf<MathNode>()
2321
- expectTypeOf(instance1).toMatchTypeOf<MathNodeCommon>()
2322
-
2323
- expectTypeOf(instance2).toMatchTypeOf<MathNode>()
2324
- expectTypeOf(instance2).toMatchTypeOf<MathNodeCommon>()
2325
- expectTypeOf(instance2).toMatchTypeOf<ConstantNode>()
2326
-
2327
- expectTypeOf(instance3).toMatchTypeOf<MathNode>()
2328
- expectTypeOf(instance3).toMatchTypeOf<MathNodeCommon>()
2329
- expectTypeOf(instance3).toMatchTypeOf<CustomNode>()
2330
- }