mathjs 10.4.0 → 10.4.1

Sign up to get free protection for your applications and to get access to all the features.
package/types/index.ts CHANGED
@@ -8,6 +8,12 @@ import {
8
8
  divideDependencies,
9
9
  formatDependencies,
10
10
  } from 'mathjs';
11
+ import * as assert from 'assert';
12
+ import { expectTypeOf } from 'expect-type'
13
+
14
+ // This file serves a dual purpose:
15
+ // 1) examples of how to use math.js in TypeScript
16
+ // 2) tests for the TypeScript declarations provided by math.js
11
17
 
12
18
  /*
13
19
  Basic usage examples
@@ -25,17 +31,45 @@ Basic usage examples
25
31
  const angle = 0.2;
26
32
  math.add(math.pow(math.sin(angle), 2), math.pow(math.cos(angle), 2));
27
33
 
34
+ // std and variance check
35
+ math.std(1, 2, 3)
36
+ math.std([1, 2, 3])
37
+ math.std([1, 2, 3], "biased")
38
+ math.std([1,2, 3], 0, "biased")
39
+ math.std([[1,2,3], [4,5,6]], 1, "unbiased")
40
+ math.std([[1,2,3], [4,5,6]], 1, "uncorrected")
41
+ math.variance(1, 2, 3)
42
+ math.variance([1, 2, 3])
43
+ math.variance([1, 2, 3], "biased")
44
+ math.variance([1,2, 3], 0, "biased")
45
+ math.variance([[1,2,3], [4,5,6]], 1, "unbiased")
46
+ math.variance([[1,2,3], [4,5,6]], 1, "uncorrected")
47
+
48
+ // std and variance on chain
49
+ math.chain([1, 2, 3]).std("unbiased")
50
+ math.chain([[1, 2, 3], [4, 5, 6]]).std(0, "biased").std(0, "uncorrected")
51
+ math.chain([[1, 2, 3], [4, 5, 6]]).std(0, "biased").std(0, "uncorrected")
52
+ math.chain([1, 2, 3]).std("unbiased")
53
+ math.chain([[1, 2, 3], [4, 5, 6]]).variance(0, "biased")
54
+ math.chain([[1, 2, 3], [4, 5, 6]]).variance(1, "uncorrected").variance("unbiased")
55
+
56
+
28
57
  // expressions
29
58
  math.evaluate('1.2 * (2 + 4.5)');
30
59
 
31
60
  // chained operations
32
- const a = math.chain(3).add(4).multiply(2).done(); // 14
61
+ const a = math.chain(3).add(4).multiply(2).done();
62
+ assert.strictEqual(a, 14);
33
63
 
34
64
  // mixed use of different data types in functions
35
- math.add(4, [5, 6]); // number + Array, [9, 10]
36
- math.multiply(math.unit('5 mm'), 3); // Unit * number, 15 mm
37
- math.subtract([2, 3, 4], 5); // Array - number, [-3, -2, -1]
38
- math.add(math.matrix([2, 3]), [4, 5]); // Matrix + Array, [6, 8]
65
+ assert.deepStrictEqual(math.add(4, [5, 6]), [9, 10]); // number + Array
66
+ assert.deepStrictEqual(math.multiply(math.unit('5 mm'), 3), math.unit('15 mm')); // Unit * number
67
+ assert.deepStrictEqual(math.subtract([2, 3, 4], 5), [-3, -2, -1]); // Array - number
68
+ assert.deepStrictEqual(math.add(math.matrix([2, 3]), [4, 5]), math.matrix([6, 8])); // Matrix + Array
69
+
70
+ // narrowed type inference
71
+ const b: math.Matrix = math.add(math.matrix([2]), math.matrix([3]));
72
+ const c: math.Matrix = math.subtract(math.matrix([4]), math.matrix([5]));
39
73
  }
40
74
 
41
75
  /*
@@ -49,8 +83,8 @@ Bignumbers examples
49
83
  });
50
84
 
51
85
  {
52
- math.add(math.bignumber(0.1), math.bignumber(0.2)); // BigNumber, 0.3
53
- math.divide(math.bignumber(0.3), math.bignumber(0.2)); // BigNumber, 1.5
86
+ assert.deepStrictEqual(math.add(math.bignumber(0.1), math.bignumber(0.2)), math.bignumber(0.3));
87
+ assert.deepStrictEqual(math.divide(math.bignumber(0.3), math.bignumber(0.2)), math.bignumber(1.5));
54
88
  }
55
89
  }
56
90
 
@@ -59,14 +93,16 @@ Chaining examples
59
93
  */
60
94
  {
61
95
  const math = create(all, {});
62
- const a = math.chain(3).add(4).multiply(2).done(); // 14
96
+ const a = math.chain(3).add(4).multiply(2).done();
97
+ assert.strictEqual(a, 14);
63
98
 
64
99
  // Another example, calculate square(sin(pi / 4))
65
100
  const b = math.chain(math.pi).divide(4).sin().square().done();
66
101
 
67
102
  // toString will return a string representation of the chain's value
68
103
  const chain = math.chain(2).divide(3);
69
- const str: string = chain.toString(); // "0.6666666666666666"
104
+ const str: string = chain.toString();
105
+ assert.strictEqual(str, "0.6666666666666666");
70
106
 
71
107
  chain.valueOf();
72
108
 
@@ -75,17 +111,21 @@ Chaining examples
75
111
  [1, 2],
76
112
  [3, 4],
77
113
  ];
78
- const v = math.chain(array).subset(math.index(1, 0)).done(); // 3
114
+ const v = math.chain(array).subset(math.index(1, 0)).done();
115
+ assert.strictEqual(v, 3);
79
116
 
80
117
  const m = math.chain(array).subset(math.index(0, 0), 8).multiply(3).done();
81
118
 
82
119
  // filtering
83
- math
84
- .chain([-1, 0, 1.1, 2, 3, 1000])
85
- .filter(math.isPositive)
86
- .filter(math.isInteger)
87
- .filter((n) => n !== 1000)
88
- .done(); // [2, 3]
120
+ assert.deepStrictEqual(
121
+ math
122
+ .chain([-1, 0, 1.1, 2, 3, 1000])
123
+ .filter(math.isPositive)
124
+ .filter(math.isInteger)
125
+ .filter((n) => n !== 1000)
126
+ .done(),
127
+ [2, 3]
128
+ );
89
129
  }
90
130
 
91
131
  /*
@@ -164,13 +204,16 @@ Expressions examples
164
204
  // get content of a parenthesis node
165
205
  {
166
206
  const node = math.parse('(1)');
207
+ if (node.type !== 'ParenthesisNode') {
208
+ throw Error(`expected ParenthesisNode, got ${node.type}`);
209
+ }
167
210
  const innerNode = node.content;
168
211
  }
169
212
 
170
213
  // scope can contain both variables and functions
171
214
  {
172
215
  const scope = { hello: (name: string) => `hello, ${name}!` };
173
- math.evaluate('hello("hero")', scope); // "hello, hero!"
216
+ assert.strictEqual(math.evaluate('hello("hero")', scope), "hello, hero!");
174
217
  }
175
218
 
176
219
  // define a function as an expression
@@ -203,10 +246,10 @@ Expressions examples
203
246
 
204
247
  // get and set variables and functions
205
248
  {
206
- parser.evaluate('x = 7 / 2'); // 3.5
207
- parser.evaluate('x + 3'); // 6.5
249
+ assert.strictEqual(parser.evaluate('x = 7 / 2'), 3.5);
250
+ assert.strictEqual(parser.evaluate('x + 3'), 6.5);
208
251
  parser.evaluate('f(x, y) = x^y'); // f(x, y)
209
- parser.evaluate('f(2, 3)'); // 8
252
+ assert.strictEqual(parser.evaluate('f(2, 3)'), 8);
210
253
 
211
254
  const x = parser.get('x');
212
255
  const f = parser.get('f');
@@ -277,7 +320,7 @@ Matrices examples
277
320
  b.subset(math.index(1, [0, 1]), [[7, 8]]);
278
321
  const c = math.multiply(a, b);
279
322
  const f: math.Matrix = math.matrix([1, 0]);
280
- const d: math.Matrix = f.subset(math.index(1, 0));
323
+ const d: math.Matrix = f.subset(math.index(1));
281
324
  }
282
325
 
283
326
  // get a sub matrix
@@ -313,23 +356,34 @@ Matrices examples
313
356
 
314
357
  // map matrix
315
358
  {
316
- math.map([1, 2, 3], function (value) {
317
- return value * value;
318
- }); // returns [1, 4, 9]
359
+ assert.deepStrictEqual(
360
+ math.map([1, 2, 3], function (value) {
361
+ return value * value;
362
+ }),
363
+ [1, 4, 9]
364
+ );
319
365
  }
320
366
 
321
367
  // filter matrix
322
368
  {
323
- math.filter([6, -2, -1, 4, 3], function (x) {
324
- return x > 0;
325
- }); // returns [6, 4, 3]
326
- math.filter(['23', 'foo', '100', '55', 'bar'], /[0-9]+/); // returns ["23", "100", "55"]
369
+ assert.deepStrictEqual(
370
+ math.filter([6, -2, -1, 4, 3], function (x) {
371
+ return x > 0;
372
+ }),
373
+ [6, 4, 3]
374
+ )
375
+ assert.deepStrictEqual(math.filter(['23', 'foo', '100', '55', 'bar'], /[0-9]+/), ["23", "100", "55"]);
327
376
  }
328
377
 
329
378
  // concat matrix
330
379
  {
331
- math.concat([[0, 1, 2]], [[1, 2, 3]]); // returns [[ 0, 1, 2, 1, 2, 3 ]]
332
- math.concat([[0, 1, 2]], [[1, 2, 3]], 0); // returns [[ 0, 1, 2 ], [ 1, 2, 3 ]]
380
+ assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]]), [[ 0, 1, 2, 1, 2, 3 ]]);
381
+ assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]], 0), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
382
+ }
383
+
384
+ // Matrix is available as a constructor for instanceof checks
385
+ {
386
+ assert.strictEqual(math.matrix([1, 2, 3]) instanceof math.Matrix, true)
333
387
  }
334
388
  }
335
389
 
@@ -347,7 +401,7 @@ Sparse matrices examples
347
401
  const c = math.multiply(b, math.complex(2, 2));
348
402
  const d = math.matrix([0, 1]);
349
403
  const e = math.transpose(d);
350
- const f = math.multiply(e, a);
404
+ const f = math.multiply(e, d);
351
405
  }
352
406
 
353
407
  /*
@@ -360,6 +414,7 @@ Units examples
360
414
  // a string with a valued unit.
361
415
  const a = math.unit(45, 'cm'); // 450 mm
362
416
  const b = math.unit('0.1m'); // 100 mm
417
+ const c = math.unit(b)
363
418
 
364
419
  // creating units
365
420
  math.createUnit('foo');
@@ -380,7 +435,7 @@ Units examples
380
435
  );
381
436
  math.createUnit(
382
437
  {
383
- foo_2: {
438
+ foo2: {
384
439
  prefixes: 'long',
385
440
  },
386
441
  bar: '40 foo',
@@ -427,7 +482,7 @@ Expression tree examples
427
482
 
428
483
  // Filter an expression tree
429
484
  const node: math.MathNode = math.parse('x^2 + x/4 + 3*y');
430
- const filtered: math.MathNode[] = node.filter((node: math.MathNode) => node.isSymbolNode && node.name === 'x');
485
+ const filtered: math.MathNode[] = node.filter((node: math.MathNode) => node.type === 'SymbolNode' && node.name === 'x');
431
486
 
432
487
  const arr: string[] = filtered.map((node: math.MathNode) => node.toString());
433
488
 
@@ -442,7 +497,7 @@ Expression tree examples
442
497
  case 'SymbolNode':
443
498
  return node.type === 'SymbolNode';
444
499
  default:
445
- return node.type === 'any string at all';
500
+ return;
446
501
  }
447
502
  });
448
503
  }
@@ -454,22 +509,22 @@ Function floor examples
454
509
  const math = create(all, {});
455
510
 
456
511
  // number input
457
- math.floor(3.2); // returns number 3
458
- math.floor(-4.2); // returns number -5
512
+ assert.strictEqual(math.floor(3.2), 3);
513
+ assert.strictEqual(math.floor(-4.2), -5);
459
514
 
460
515
  // number input
461
516
  // roundoff result to 2 decimals
462
- math.floor(3.212, 2); // returns number 3.21
463
- math.floor(-4.212, 2); // returns number -4.22
517
+ assert.strictEqual(math.floor(3.212, 2), 3.21);
518
+ assert.strictEqual(math.floor(-4.212, 2), -4.22);
464
519
 
465
520
  // Complex input
466
- const c = math.complex(3.24, -2.71); // returns Complex 3 - 3i
467
- math.floor(c); // returns Complex 3 - 3i
468
- math.floor(c, 1); // returns Complex 3.2 - 2.8i
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));
469
524
 
470
525
  //array input
471
- math.floor([3.2, 3.8, -4.7]); // returns Array [3, 3, -5]
472
- math.floor([3.21, 3.82, -4.71], 1); // returns Array [3.2, 3.8, -4.8]
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]);
473
528
  }
474
529
 
475
530
 
@@ -483,8 +538,8 @@ JSON serialization/deserialization
483
538
  bigNumber: math.bignumber('1.5'),
484
539
  };
485
540
  const stringified = JSON.stringify(data);
486
- const parsed = JSON.parse(stringified, math.json.reviver);
487
- parsed.bigNumber === math.bignumber('1.5'); // true
541
+ const parsed = JSON.parse(stringified, math.reviver);
542
+ assert.deepStrictEqual(parsed.bigNumber, math.bignumber('1.5'));
488
543
  }
489
544
 
490
545
  /*
@@ -576,8 +631,8 @@ Factory Test
576
631
  const b = fraction(3, 7);
577
632
  const c = add(a, b);
578
633
  const d = divide(a, b);
579
- console.log('c =', format(c)); // outputs "c = 16/21"
580
- console.log('d =', format(d)); // outputs "d = 7/9"
634
+ assert.strictEqual(format(c), "16/21");
635
+ assert.strictEqual(format(d), "7/9");
581
636
  }
582
637
 
583
638
  /**
@@ -592,8 +647,8 @@ Factory Test
592
647
  };
593
648
 
594
649
  // now we can use the \u260E (phone) character in expressions
595
- const result = math.evaluate("\u260Efoo", { "\u260Efoo": 42 }); // returns 42
596
- console.log(result);
650
+ const result = math.evaluate("\u260Efoo", { "\u260Efoo": 42 });
651
+ assert.strictEqual(result, 42);
597
652
  }
598
653
 
599
654
  /**
@@ -604,13 +659,175 @@ Factory Test
604
659
  const math = create(all, {});
605
660
 
606
661
  // hasNumericValue function
607
- math.hasNumericValue(2); // returns true
608
- math.hasNumericValue('2'); // returns true
609
- math.isNumeric('2'); // returns false
610
- math.hasNumericValue(0); // returns true
611
- math.hasNumericValue(math.bignumber(500)); // returns true
612
- math.hasNumericValue([2.3, 'foo', false]); // returns [true, false, true]
613
- math.hasNumericValue(math.fraction(4)); // returns true
614
- math.hasNumericValue(math.complex('2-4i')); // returns false
662
+ assert. strictEqual(math.hasNumericValue(2), true);
663
+ assert. strictEqual(math.hasNumericValue('2'), true);
664
+ assert. strictEqual(math.isNumeric('2'), false);
665
+ assert. strictEqual(math.hasNumericValue(0), true);
666
+ assert. strictEqual(math.hasNumericValue(math.bignumber(500)), true);
667
+ assert.deepStrictEqual(math.hasNumericValue([2.3, 'foo', false]), [true, false, true]);
668
+ assert. strictEqual(math.hasNumericValue(math.fraction(4)), true);
669
+ assert. strictEqual(math.hasNumericValue(math.complex('2-4i')), false);
615
670
  }
616
671
 
672
+ /**
673
+ * src/util/is functions
674
+ */
675
+ {
676
+ const math = create(all, {});
677
+
678
+ type IsFunc = (x: unknown) => boolean;
679
+ const isFuncs: IsFunc[] = [
680
+ math.isNumber,
681
+ math.isBigNumber,
682
+ math.isComplex,
683
+ math.isFraction,
684
+ math.isUnit,
685
+ math.isString,
686
+ math.isArray,
687
+ math.isMatrix,
688
+ math.isCollection,
689
+ math.isDenseMatrix,
690
+ math.isSparseMatrix,
691
+ math.isRange,
692
+ math.isIndex,
693
+ math.isBoolean,
694
+ math.isResultSet,
695
+ math.isHelp,
696
+ math.isFunction,
697
+ math.isDate,
698
+ math.isRegExp,
699
+ math.isObject,
700
+ math.isNull,
701
+ math.isUndefined,
702
+ math.isAccessorNode,
703
+ math.isArrayNode,
704
+ math.isAssignmentNode,
705
+ math.isBlockNode,
706
+ math.isConditionalNode,
707
+ math.isConstantNode,
708
+ math.isFunctionAssignmentNode,
709
+ math.isFunctionNode,
710
+ math.isIndexNode,
711
+ math.isNode,
712
+ math.isObjectNode,
713
+ math.isOperatorNode,
714
+ math.isParenthesisNode,
715
+ math.isRangeNode,
716
+ math.isSymbolNode,
717
+ math.isChain
718
+ ]
719
+
720
+ isFuncs.forEach(f => {
721
+ const result = f(1);
722
+ const isResultBoolean = result === true || result === false;
723
+ assert.ok(isResultBoolean);
724
+ })
725
+
726
+ // Check guards do type refinement
727
+
728
+ let x: unknown
729
+
730
+ if (math.isNumber(x)) {
731
+ expectTypeOf(x).toMatchTypeOf<number>()
732
+ }
733
+ if (math.isBigNumber(x)) {
734
+ expectTypeOf(x).toMatchTypeOf<math.BigNumber>()
735
+ }
736
+ if (math.isComplex(x)) {
737
+ expectTypeOf(x).toMatchTypeOf<math.Complex>()
738
+ }
739
+ if (math.isFraction(x)) {
740
+ expectTypeOf(x).toMatchTypeOf<math.Fraction>()
741
+ }
742
+ if (math.isUnit(x)) {
743
+ expectTypeOf(x).toMatchTypeOf<math.Unit>()
744
+ }
745
+ if (math.isString(x)) {
746
+ expectTypeOf(x).toMatchTypeOf<string>()
747
+ }
748
+ if (math.isArray(x)) {
749
+ expectTypeOf(x).toMatchTypeOf<unknown[]>()
750
+ }
751
+ if (math.isMatrix(x)) {
752
+ expectTypeOf(x).toMatchTypeOf<math.Matrix>()
753
+ }
754
+ if (math.isDenseMatrix(x)) {
755
+ expectTypeOf(x).toMatchTypeOf<math.Matrix>()
756
+ }
757
+ if (math.isSparseMatrix(x)) {
758
+ expectTypeOf(x).toMatchTypeOf<math.Matrix>()
759
+ }
760
+ if (math.isIndex(x)) {
761
+ expectTypeOf(x).toMatchTypeOf<math.Index>()
762
+ }
763
+ if (math.isBoolean(x)) {
764
+ expectTypeOf(x).toMatchTypeOf<boolean>()
765
+ }
766
+ if (math.isHelp(x)) {
767
+ expectTypeOf(x).toMatchTypeOf<math.Help>()
768
+ }
769
+ if (math.isDate(x)) {
770
+ expectTypeOf(x).toMatchTypeOf<Date>()
771
+ }
772
+ if (math.isRegExp(x)) {
773
+ expectTypeOf(x).toMatchTypeOf<RegExp>()
774
+ }
775
+ if (math.isNull(x)) {
776
+ expectTypeOf(x).toMatchTypeOf<null>()
777
+ }
778
+ if (math.isUndefined(x)) {
779
+ expectTypeOf(x).toMatchTypeOf<undefined>()
780
+ }
781
+
782
+ if (math.isAccessorNode(x)) {
783
+ expectTypeOf(x).toMatchTypeOf<math.AccessorNode>()
784
+ }
785
+ if (math.isArrayNode(x)) {
786
+ expectTypeOf(x).toMatchTypeOf<math.ArrayNode>()
787
+ }
788
+ if (math.isAssignmentNode(x)) {
789
+ expectTypeOf(x).toMatchTypeOf<math.AssignmentNode>()
790
+ }
791
+ if (math.isBlockNode(x)) {
792
+ expectTypeOf(x).toMatchTypeOf<math.BlockNode>()
793
+ }
794
+ if (math.isConditionalNode(x)) {
795
+ expectTypeOf(x).toMatchTypeOf<math.ConditionalNode>()
796
+ }
797
+ if (math.isConstantNode(x)) {
798
+ expectTypeOf(x).toMatchTypeOf<math.ConstantNode>()
799
+ }
800
+ if (math.isFunctionAssignmentNode(x)) {
801
+ expectTypeOf(x).toMatchTypeOf<math.FunctionAssignmentNode>()
802
+ }
803
+ if (math.isFunctionNode(x)) {
804
+ expectTypeOf(x).toMatchTypeOf<math.FunctionNode>()
805
+ }
806
+ if (math.isIndexNode(x)) {
807
+ expectTypeOf(x).toMatchTypeOf<math.IndexNode>()
808
+ }
809
+ if (math.isNode(x)) {
810
+ expectTypeOf(x).toMatchTypeOf<math.MathNodeCommon>()
811
+ }
812
+ if (math.isNode(x)) {
813
+ expectTypeOf(x).toMatchTypeOf<math.MathNodeCommon>()
814
+ }
815
+ if (math.isObjectNode(x)) {
816
+ expectTypeOf(x).toMatchTypeOf<math.ObjectNode>()
817
+ }
818
+ if (math.isOperatorNode(x)) {
819
+ expectTypeOf(x).toMatchTypeOf<math.OperatorNode>()
820
+ }
821
+ if (math.isParenthesisNode(x)) {
822
+ expectTypeOf(x).toMatchTypeOf<math.ParenthesisNode>()
823
+ }
824
+ if (math.isRangeNode(x)) {
825
+ expectTypeOf(x).toMatchTypeOf<math.RangeNode>()
826
+ }
827
+ if (math.isSymbolNode(x)) {
828
+ expectTypeOf(x).toMatchTypeOf<math.SymbolNode>()
829
+ }
830
+ if (math.isChain(x)) {
831
+ expectTypeOf(x).toMatchTypeOf<math.MathJsChain>()
832
+ }
833
+ }
@@ -4,10 +4,11 @@
4
4
  "paths": {
5
5
  "mathjs": ["./index.d.ts"]
6
6
  },
7
+ "moduleResolution": "node",
7
8
  "typeRoots": [],
8
9
  "types": [],
9
10
  "lib": ["ES6", "DOM"],
10
- "module": "commonjs",
11
+ "module": "ESNEXT",
11
12
  "noEmit": true,
12
13
  "noImplicitAny": true,
13
14
  "noImplicitThis": true,