mathjs 14.3.1 → 14.5.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.
Files changed (60) hide show
  1. package/HISTORY.md +28 -6
  2. package/README.md +1 -1
  3. package/lib/browser/math.js +1 -1
  4. package/lib/browser/math.js.LICENSE.txt +2 -2
  5. package/lib/browser/math.js.map +1 -1
  6. package/lib/cjs/constants.js +12 -12
  7. package/lib/cjs/core/create.js +1 -2
  8. package/lib/cjs/defaultInstance.js +1 -2
  9. package/lib/cjs/entry/allFactoriesAny.js +1 -2
  10. package/lib/cjs/entry/allFactoriesNumber.js +1 -2
  11. package/lib/cjs/expression/embeddedDocs/construction/index.js +1 -1
  12. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +6 -0
  13. package/lib/cjs/expression/embeddedDocs/function/expression/compile.js +14 -0
  14. package/lib/cjs/expression/embeddedDocs/function/expression/evaluate.js +1 -1
  15. package/lib/cjs/expression/embeddedDocs/function/expression/parse.js +14 -0
  16. package/lib/cjs/expression/embeddedDocs/function/expression/parser.js +14 -0
  17. package/lib/cjs/expression/embeddedDocs/function/statistics/sum.js +1 -1
  18. package/lib/cjs/expression/function/parser.js +1 -1
  19. package/lib/cjs/expression/parse.js +4 -3
  20. package/lib/cjs/function/arithmetic/add.js +33 -33
  21. package/lib/cjs/function/arithmetic/hypot.js +1 -1
  22. package/lib/cjs/function/matrix/filter.js +4 -1
  23. package/lib/cjs/function/matrix/flatten.js +1 -1
  24. package/lib/cjs/function/matrix/forEach.js +2 -1
  25. package/lib/cjs/function/matrix/map.js +2 -1
  26. package/lib/cjs/header.js +2 -2
  27. package/lib/cjs/type/matrix/DenseMatrix.js +144 -81
  28. package/lib/cjs/type/matrix/SparseMatrix.js +4 -3
  29. package/lib/cjs/type/unit/Unit.js +19 -4
  30. package/lib/cjs/utils/array.js +31 -7
  31. package/lib/cjs/utils/collection.js +3 -3
  32. package/lib/cjs/utils/latex.js +4 -1
  33. package/lib/cjs/utils/optimizeCallback.js +61 -13
  34. package/lib/cjs/utils/snapshot.js +1 -2
  35. package/lib/cjs/version.js +1 -1
  36. package/lib/esm/constants.js +12 -12
  37. package/lib/esm/expression/embeddedDocs/construction/index.js +1 -1
  38. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +6 -0
  39. package/lib/esm/expression/embeddedDocs/function/expression/compile.js +8 -0
  40. package/lib/esm/expression/embeddedDocs/function/expression/evaluate.js +1 -1
  41. package/lib/esm/expression/embeddedDocs/function/expression/parse.js +8 -0
  42. package/lib/esm/expression/embeddedDocs/function/expression/parser.js +8 -0
  43. package/lib/esm/expression/embeddedDocs/function/statistics/sum.js +1 -1
  44. package/lib/esm/expression/function/parser.js +1 -1
  45. package/lib/esm/expression/parse.js +4 -3
  46. package/lib/esm/function/arithmetic/add.js +33 -33
  47. package/lib/esm/function/arithmetic/hypot.js +1 -1
  48. package/lib/esm/function/matrix/filter.js +4 -1
  49. package/lib/esm/function/matrix/flatten.js +1 -1
  50. package/lib/esm/function/matrix/forEach.js +2 -1
  51. package/lib/esm/function/matrix/map.js +2 -1
  52. package/lib/esm/type/matrix/DenseMatrix.js +150 -87
  53. package/lib/esm/type/matrix/SparseMatrix.js +4 -3
  54. package/lib/esm/type/unit/Unit.js +19 -4
  55. package/lib/esm/utils/array.js +33 -9
  56. package/lib/esm/utils/collection.js +3 -3
  57. package/lib/esm/utils/latex.js +4 -1
  58. package/lib/esm/utils/optimizeCallback.js +61 -13
  59. package/lib/esm/version.js +1 -1
  60. package/package.json +20 -20
@@ -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.1';
1
+ export var version = '14.5.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.1",
3
+ "version": "14.5.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",
40
- "@babel/plugin-transform-object-assign": "7.25.9",
41
- "@babel/plugin-transform-optional-catch-binding": "7.25.9",
42
- "@babel/plugin-transform-runtime": "7.26.9",
43
- "@babel/preset-env": "7.26.9",
44
- "@babel/register": "7.25.9",
39
+ "@babel/core": "7.27.1",
40
+ "@babel/plugin-transform-object-assign": "7.27.1",
41
+ "@babel/plugin-transform-optional-catch-binding": "7.27.1",
42
+ "@babel/plugin-transform-runtime": "7.27.1",
43
+ "@babel/preset-env": "7.27.2",
44
+ "@babel/register": "7.27.1",
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.32.1",
48
+ "@typescript-eslint/parser": "8.32.1",
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.42.0",
54
54
  "del": "8.0.0",
55
55
  "dtslint": "4.2.1",
56
56
  "eigen": "0.2.2",
@@ -60,12 +60,12 @@
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.4.0",
64
64
  "eslint-plugin-promise": "6.6.0",
65
- "expect-type": "1.1.0",
65
+ "expect-type": "1.2.1",
66
66
  "expr-eval": "2.0.2",
67
67
  "fancy-log": "2.0.0",
68
- "glob": "11.0.1",
68
+ "glob": "11.0.2",
69
69
  "gulp": "5.0.0",
70
70
  "gulp-babel": "8.0.0",
71
71
  "handlebars": "4.7.8",
@@ -78,7 +78,7 @@
78
78
  "karma-webdriver-launcher": "1.0.8",
79
79
  "karma-webpack": "5.0.1",
80
80
  "mkdirp": "3.0.1",
81
- "mocha": "11.1.0",
81
+ "mocha": "11.4.0",
82
82
  "mocha-junit-reporter": "2.2.1",
83
83
  "ndarray": "1.0.19",
84
84
  "ndarray-determinant": "1.0.0",
@@ -86,14 +86,14 @@
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": "20.0.0",
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",
96
- "webpack": "5.98.0",
95
+ "typescript": "5.8.3",
96
+ "webpack": "5.99.9",
97
97
  "zeros": "1.0.0"
98
98
  },
99
99
  "type": "module",