mathjs 14.3.0 → 14.4.0

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.
@@ -521,39 +521,52 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
521
521
  * Applies a callback function to a reference to each element of the matrix
522
522
  * @memberof DenseMatrix
523
523
  * @param {Function} callback The callback function is invoked with three
524
- * parameters: an array, an integer index to that
525
- * array, and the Matrix being traversed.
524
+ * parameters: the array containing the element,
525
+ * the index of the element within that array (as an integer),
526
+ * and for non unarry callbacks copy of the current index (as an array of integers).
526
527
  */
527
528
  DenseMatrix.prototype._forEach = function (callback) {
528
- var me = this;
529
- var s = me.size();
530
- var maxDepth = s.length - 1;
531
- if (maxDepth < 0) {
529
+ var isUnary = callback.length === 2; // callback has 2 parameters: value, index
530
+ var maxDepth = this._size.length - 1;
531
+ if (maxDepth < 0) return;
532
+ if (isUnary) {
533
+ iterateUnary(this._data);
532
534
  return;
533
535
  }
534
536
  if (maxDepth === 0) {
535
- var thisSize = s[0];
536
- for (var i = 0; i < thisSize; i++) {
537
- callback(me._data, i, [i]);
537
+ for (var i = 0; i < this._data.length; i++) {
538
+ callback(this._data, i, [i]);
538
539
  }
539
540
  return;
540
541
  }
541
- var index = Array(s.length);
542
- function recurse(data, depth) {
543
- var thisSize = s[depth];
542
+ var index = new Array(maxDepth + 1);
543
+ iterate(this._data);
544
+ function iterate(data) {
545
+ var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
544
546
  if (depth < maxDepth) {
545
- for (var _i = 0; _i < thisSize; _i++) {
547
+ for (var _i = 0; _i < data.length; _i++) {
546
548
  index[depth] = _i;
547
- recurse(data[_i], depth + 1);
549
+ iterate(data[_i], depth + 1);
548
550
  }
549
551
  } else {
550
- for (var _i2 = 0; _i2 < thisSize; _i2++) {
552
+ for (var _i2 = 0; _i2 < data.length; _i2++) {
551
553
  index[depth] = _i2;
552
554
  callback(data, _i2, index.slice());
553
555
  }
554
556
  }
555
557
  }
556
- recurse(me._data, 0);
558
+ function iterateUnary(data) {
559
+ var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
560
+ if (depth < maxDepth) {
561
+ for (var _i3 = 0; _i3 < data.length; _i3++) {
562
+ iterateUnary(data[_i3], depth + 1);
563
+ }
564
+ } else {
565
+ for (var _i4 = 0; _i4 < data.length; _i4++) {
566
+ callback(data, _i4);
567
+ }
568
+ }
569
+ }
557
570
  };
558
571
 
559
572
  /**
@@ -563,16 +576,23 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
563
576
  * @param {Function} callback The callback function is invoked with three
564
577
  * parameters: the value of the element, the index
565
578
  * of the element, and the Matrix being traversed.
579
+ * @param {boolean} skipZeros If true, the callback function is invoked only for non-zero entries
580
+ * @param {boolean} isUnary If true, the callback function is invoked with one parameter
566
581
  *
567
582
  * @return {DenseMatrix} matrix
568
583
  */
569
584
  DenseMatrix.prototype.map = function (callback) {
585
+ var skipZeros = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
586
+ var isUnary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
570
587
  var me = this;
571
588
  var result = new DenseMatrix(me);
572
- var fastCallback = optimizeCallback(callback, me._data, 'map');
573
- result._forEach(function (arr, i, index) {
574
- arr[i] = fastCallback(arr[i], index, me);
575
- });
589
+ var fastCallback = optimizeCallback(callback, me._data, 'map', isUnary);
590
+ var applyCallback = isUnary || fastCallback.isUnary ? (arr, i) => {
591
+ arr[i] = fastCallback.fn(arr[i]);
592
+ } : (arr, i, index) => {
593
+ arr[i] = fastCallback.fn(arr[i], index, me);
594
+ };
595
+ result._forEach(applyCallback);
576
596
  return result;
577
597
  };
578
598
 
@@ -582,13 +602,20 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
582
602
  * @param {Function} callback The callback function is invoked with three
583
603
  * parameters: the value of the element, the index
584
604
  * of the element, and the Matrix being traversed.
605
+ * @param {boolean} skipZeros If true, the callback function is invoked only for non-zero entries
606
+ * @param {boolean} isUnary If true, the callback function is invoked with one parameter
585
607
  */
586
608
  DenseMatrix.prototype.forEach = function (callback) {
609
+ var skipZeros = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
610
+ var isUnary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
587
611
  var me = this;
588
- var fastCallback = optimizeCallback(callback, me._data, 'map');
589
- me._forEach(function (arr, i, index) {
590
- fastCallback(arr[i], index, me);
591
- });
612
+ var fastCallback = optimizeCallback(callback, me._data, 'map', isUnary);
613
+ var applyCallback = isUnary || fastCallback.isUnary ? (arr, i) => {
614
+ fastCallback.fn(arr[i]);
615
+ } : (arr, i, index) => {
616
+ fastCallback.fn(arr[i], index, me);
617
+ };
618
+ me._forEach(applyCallback);
592
619
  };
593
620
 
594
621
  /**
@@ -612,15 +639,15 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
612
639
  var index = [];
613
640
  var _recurse = function* recurse(value, depth) {
614
641
  if (depth < maxDepth) {
615
- for (var _i3 = 0; _i3 < value.length; _i3++) {
616
- index[depth] = _i3;
617
- yield* _recurse(value[_i3], depth + 1);
642
+ for (var _i5 = 0; _i5 < value.length; _i5++) {
643
+ index[depth] = _i5;
644
+ yield* _recurse(value[_i5], depth + 1);
618
645
  }
619
646
  } else {
620
- for (var _i4 = 0; _i4 < value.length; _i4++) {
621
- index[depth] = _i4;
647
+ for (var _i6 = 0; _i6 < value.length; _i6++) {
648
+ index[depth] = _i6;
622
649
  yield {
623
- value: value[_i4],
650
+ value: value[_i6],
624
651
  index: index.slice()
625
652
  };
626
653
  }
@@ -876,7 +876,7 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
876
876
  // invoke callback
877
877
  var invoke = function invoke(v, i, j) {
878
878
  // invoke callback
879
- return fastCallback(v, [i, j], me);
879
+ return fastCallback.fn(v, [i, j], me);
880
880
  };
881
881
  // invoke _map
882
882
  return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros);
@@ -994,7 +994,8 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
994
994
  var i = this._index[k];
995
995
 
996
996
  // value @ k
997
- fastCallback(this._values[k], [i, j], me);
997
+ // TODO apply a non indexed version of algorithm in case fastCallback is not optimized
998
+ fastCallback.fn(this._values[k], [i, j], me);
998
999
  }
999
1000
  } else {
1000
1001
  // create a cache holding all defined values
@@ -1008,7 +1009,7 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
1008
1009
  // and either read the value or zero
1009
1010
  for (var _i7 = 0; _i7 < rows; _i7++) {
1010
1011
  var value = _i7 in values ? values[_i7] : 0;
1011
- fastCallback(value, [_i7, j], me);
1012
+ fastCallback.fn(value, [_i7, j], me);
1012
1013
  }
1013
1014
  }
1014
1015
  }
@@ -242,7 +242,7 @@ function _resize(array, size, dim, defaultValue) {
242
242
  * not equal that of the old ones
243
243
  */
244
244
  export function reshape(array, sizes) {
245
- var flatArray = flatten(array);
245
+ var flatArray = flatten(array, true); // since it has rectangular
246
246
  var currentLength = flatArray.length;
247
247
  if (!Array.isArray(array) || !Array.isArray(sizes)) {
248
248
  throw new TypeError('Array expected');
@@ -439,22 +439,46 @@ function _unsqueeze(array, dims, dim) {
439
439
  * Flatten a multi dimensional array, put all elements in a one dimensional
440
440
  * array
441
441
  * @param {Array} array A multi dimensional array
442
+ * @param {boolean} isRectangular Optional. If the array is rectangular (not jagged)
442
443
  * @return {Array} The flattened array (1 dimensional)
443
444
  */
444
445
  export function flatten(array) {
446
+ var isRectangular = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
445
447
  if (!Array.isArray(array)) {
446
448
  // if not an array, return as is
447
449
  return array;
448
450
  }
451
+ if (typeof isRectangular !== 'boolean') {
452
+ throw new TypeError('Boolean expected for second argument of flatten');
453
+ }
449
454
  var flat = [];
450
- array.forEach(function callback(value) {
451
- if (Array.isArray(value)) {
452
- value.forEach(callback); // traverse through sub-arrays recursively
455
+ if (isRectangular) {
456
+ _flattenRectangular(array);
457
+ } else {
458
+ _flatten(array);
459
+ }
460
+ return flat;
461
+ function _flatten(array) {
462
+ for (var i = 0; i < array.length; i++) {
463
+ var item = array[i];
464
+ if (Array.isArray(item)) {
465
+ _flatten(item);
466
+ } else {
467
+ flat.push(item);
468
+ }
469
+ }
470
+ }
471
+ function _flattenRectangular(array) {
472
+ if (Array.isArray(array[0])) {
473
+ for (var i = 0; i < array.length; i++) {
474
+ _flattenRectangular(array[i]);
475
+ }
453
476
  } else {
454
- flat.push(value);
477
+ for (var _i = 0; _i < array.length; _i++) {
478
+ flat.push(array[_i]);
479
+ }
455
480
  }
456
- });
457
- return flat;
481
+ }
458
482
  }
459
483
 
460
484
  /**
@@ -689,8 +713,8 @@ export function broadcastSizes() {
689
713
  }
690
714
  }
691
715
  }
692
- for (var _i = 0; _i < sizes.length; _i++) {
693
- checkBroadcastingRules(sizes[_i], sizeMax);
716
+ for (var _i2 = 0; _i2 < sizes.length; _i2++) {
717
+ checkBroadcastingRules(sizes[_i2], sizeMax);
694
718
  }
695
719
  return sizeMax;
696
720
  }
@@ -27,7 +27,7 @@ export function containsCollections(array) {
27
27
  */
28
28
  export function deepForEach(array, callback) {
29
29
  if (isMatrix(array)) {
30
- array.forEach(x => callback(x));
30
+ array.forEach(x => callback(x), false, true);
31
31
  } else {
32
32
  arrayDeepForEach(array, callback, true);
33
33
  }
@@ -48,14 +48,14 @@ export function deepForEach(array, callback) {
48
48
  export function deepMap(array, callback, skipZeros) {
49
49
  if (!skipZeros) {
50
50
  if (isMatrix(array)) {
51
- return array.map(x => callback(x));
51
+ return array.map(x => callback(x), false, true);
52
52
  } else {
53
53
  return arrayDeepMap(array, callback, true);
54
54
  }
55
55
  }
56
56
  var skipZerosCallback = x => x === 0 ? x : callback(x);
57
57
  if (isMatrix(array)) {
58
- return array.map(x => skipZerosCallback(x));
58
+ return array.map(x => skipZerosCallback(x), false, true);
59
59
  } else {
60
60
  return arrayDeepMap(array, skipZerosCallback, true);
61
61
  }
@@ -151,6 +151,9 @@ export var latexFunctions = {
151
151
  floor: {
152
152
  1: '\\left\\lfloor${args[0]}\\right\\rfloor'
153
153
  },
154
+ fraction: {
155
+ 2: '\\frac{${args[0]}}{${args[1]}}'
156
+ },
154
157
  gcd: '\\gcd\\left(${args}\\right)',
155
158
  hypot: '\\hypot\\left(${args}\\right)',
156
159
  log: {
@@ -179,7 +182,7 @@ export var latexFunctions = {
179
182
  2: '\\sqrt[${args[1]}]{${args[0]}}'
180
183
  },
181
184
  nthRoots: {
182
- 2: '\\{y : $y^{args[1]} = {${args[0]}}\\}'
185
+ 2: '\\{y : y^${args[1]} = {${args[0]}}\\}'
183
186
  },
184
187
  pow: {
185
188
  2: "\\left(${args[0]}\\right)".concat(latexOperators.pow, "{${args[1]}}")
@@ -8,13 +8,20 @@ import { typeOf as _typeOf } from './is.js';
8
8
  * @param {Function} callback The original callback function to simplify.
9
9
  * @param {Array|Matrix} array The array that will be used with the callback function.
10
10
  * @param {string} name The name of the function that is using the callback.
11
+ * @param {boolean} [isUnary=false] If true, the callback function is unary and will be optimized as such.
11
12
  * @returns {Function} Returns a simplified version of the callback function.
12
13
  */
13
14
  export function optimizeCallback(callback, array, name) {
15
+ var isUnary = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
14
16
  if (typed.isTypedFunction(callback)) {
15
- var firstIndex = (array.isMatrix ? array.size() : arraySize(array)).map(() => 0);
16
- var firstValue = array.isMatrix ? array.get(firstIndex) : get(array, firstIndex);
17
- var numberOfArguments = _findNumberOfArguments(callback, firstValue, firstIndex, array);
17
+ var numberOfArguments;
18
+ if (isUnary) {
19
+ numberOfArguments = 1;
20
+ } else {
21
+ var firstIndex = (array.isMatrix ? array.size() : arraySize(array)).map(() => 0);
22
+ var firstValue = array.isMatrix ? array.get(firstIndex) : get(array, firstIndex);
23
+ numberOfArguments = _findNumberOfArgumentsTyped(callback, firstValue, firstIndex, array);
24
+ }
18
25
  var fastCallback;
19
26
  if (array.isMatrix && array.dataType !== 'mixed' && array.dataType !== undefined) {
20
27
  var singleSignature = _findSingleSignatureWithArity(callback, numberOfArguments);
@@ -23,21 +30,37 @@ export function optimizeCallback(callback, array, name) {
23
30
  fastCallback = callback;
24
31
  }
25
32
  if (numberOfArguments >= 1 && numberOfArguments <= 3) {
26
- return function () {
27
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
28
- args[_key] = arguments[_key];
33
+ return {
34
+ isUnary: numberOfArguments === 1,
35
+ fn: function fn() {
36
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
37
+ args[_key] = arguments[_key];
38
+ }
39
+ return _tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name);
29
40
  }
30
- return _tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name);
31
41
  };
32
42
  }
33
- return function () {
34
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
35
- args[_key2] = arguments[_key2];
43
+ return {
44
+ isUnary: false,
45
+ fn: function fn() {
46
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
47
+ args[_key2] = arguments[_key2];
48
+ }
49
+ return _tryFunctionWithArgs(fastCallback, args, name, callback.name);
36
50
  }
37
- return _tryFunctionWithArgs(fastCallback, args, name, callback.name);
38
51
  };
39
52
  }
40
- return callback;
53
+ if (isUnary === undefined) {
54
+ return {
55
+ isUnary: _findIfCallbackIsUnary(callback),
56
+ fn: callback
57
+ };
58
+ } else {
59
+ return {
60
+ isUnary,
61
+ fn: callback
62
+ };
63
+ }
41
64
  }
42
65
  function _findSingleSignatureWithArity(callback, arity) {
43
66
  var matchingFunctions = [];
@@ -51,7 +74,32 @@ function _findSingleSignatureWithArity(callback, arity) {
51
74
  return matchingFunctions[0];
52
75
  }
53
76
  }
54
- function _findNumberOfArguments(callback, value, index, array) {
77
+
78
+ /**
79
+ * Determines if a given callback function is unary (i.e., takes exactly one argument).
80
+ *
81
+ * This function checks the following conditions to determine if the callback is unary:
82
+ * 1. The callback function should have exactly one parameter.
83
+ * 2. The callback function should not use the `arguments` object.
84
+ * 3. The callback function should not use rest parameters (`...`).
85
+ * If in doubt, this function shall return `false` to be safe
86
+ *
87
+ * @param {Function} callback - The callback function to be checked.
88
+ * @returns {boolean} - Returns `true` if the callback is unary, otherwise `false`.
89
+ */
90
+ function _findIfCallbackIsUnary(callback) {
91
+ if (callback.length !== 1) return false;
92
+ var callbackStr = callback.toString();
93
+ // Check if the callback function uses `arguments`
94
+ if (/arguments/.test(callbackStr)) return false;
95
+
96
+ // Extract the parameters of the callback function
97
+ var paramsStr = callbackStr.match(/\(.*?\)/);
98
+ // Check if the callback function uses rest parameters
99
+ if (/\.\.\./.test(paramsStr)) return false;
100
+ return true;
101
+ }
102
+ function _findNumberOfArgumentsTyped(callback, value, index, array) {
55
103
  var testArgs = [value, index, array];
56
104
  for (var i = 3; i > 0; i--) {
57
105
  var args = testArgs.slice(0, i);
@@ -1,3 +1,3 @@
1
- export var version = '14.3.0';
1
+ export var version = '14.4.0';
2
2
  // Note: This file is automatically generated when building math.js.
3
3
  // Changes made in this file will be overwritten.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mathjs",
3
- "version": "14.3.0",
3
+ "version": "14.4.0",
4
4
  "description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.",
5
5
  "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
6
6
  "homepage": "https://mathjs.org",
@@ -25,7 +25,7 @@
25
25
  "unit"
26
26
  ],
27
27
  "dependencies": {
28
- "@babel/runtime": "^7.25.7",
28
+ "@babel/runtime": "^7.26.10",
29
29
  "complex.js": "^2.2.5",
30
30
  "decimal.js": "^10.4.3",
31
31
  "escape-latex": "^1.2.0",
@@ -36,21 +36,21 @@
36
36
  "typed-function": "^4.2.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@babel/core": "7.26.9",
39
+ "@babel/core": "7.26.10",
40
40
  "@babel/plugin-transform-object-assign": "7.25.9",
41
41
  "@babel/plugin-transform-optional-catch-binding": "7.25.9",
42
- "@babel/plugin-transform-runtime": "7.26.9",
42
+ "@babel/plugin-transform-runtime": "7.26.10",
43
43
  "@babel/preset-env": "7.26.9",
44
44
  "@babel/register": "7.25.9",
45
45
  "@types/assert": "1.5.11",
46
46
  "@types/mocha": "10.0.10",
47
- "@typescript-eslint/eslint-plugin": "8.25.0",
48
- "@typescript-eslint/parser": "8.25.0",
47
+ "@typescript-eslint/eslint-plugin": "8.27.0",
48
+ "@typescript-eslint/parser": "8.27.0",
49
49
  "assert": "2.1.0",
50
50
  "babel-loader": "10.0.0",
51
51
  "c8": "10.1.3",
52
52
  "codecov": "3.8.3",
53
- "core-js": "3.40.0",
53
+ "core-js": "3.41.0",
54
54
  "del": "8.0.0",
55
55
  "dtslint": "4.2.1",
56
56
  "eigen": "0.2.2",
@@ -60,9 +60,9 @@
60
60
  "eslint-plugin-import": "2.31.0",
61
61
  "eslint-plugin-mocha": "10.5.0",
62
62
  "eslint-plugin-n": "16.6.2",
63
- "eslint-plugin-prettier": "5.2.3",
63
+ "eslint-plugin-prettier": "5.2.4",
64
64
  "eslint-plugin-promise": "6.6.0",
65
- "expect-type": "1.1.0",
65
+ "expect-type": "1.2.0",
66
66
  "expr-eval": "2.0.2",
67
67
  "fancy-log": "2.0.0",
68
68
  "glob": "11.0.1",
@@ -86,13 +86,13 @@
86
86
  "ndarray-ops": "1.2.2",
87
87
  "ndarray-pack": "1.2.1",
88
88
  "numericjs": "1.2.6",
89
- "prettier": "3.5.2",
89
+ "prettier": "3.5.3",
90
90
  "process": "0.11.10",
91
- "sinon": "19.0.2",
91
+ "sinon": "19.0.4",
92
92
  "sylvester": "0.0.21",
93
- "tinybench": "3.1.1",
93
+ "tinybench": "4.0.1",
94
94
  "ts-node": "10.9.2",
95
- "typescript": "5.7.3",
95
+ "typescript": "5.8.2",
96
96
  "webpack": "5.98.0",
97
97
  "zeros": "1.0.0"
98
98
  },