mathjs 14.9.1 → 15.1.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 (146) hide show
  1. package/HISTORY.md +35 -0
  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/core/config.js +5 -1
  7. package/lib/cjs/core/function/config.js +4 -0
  8. package/lib/cjs/entry/dependenciesAny/dependenciesBernoulli.generated.js +25 -0
  9. package/lib/cjs/entry/dependenciesAny/dependenciesConstantNode.generated.js +2 -0
  10. package/lib/cjs/entry/dependenciesAny/dependenciesEqual.generated.js +0 -2
  11. package/lib/cjs/entry/dependenciesAny/dependenciesIsBounded.generated.js +17 -0
  12. package/lib/cjs/entry/dependenciesAny/dependenciesIsFinite.generated.js +21 -0
  13. package/lib/cjs/entry/dependenciesAny/dependenciesIsInteger.generated.js +2 -0
  14. package/lib/cjs/entry/dependenciesAny/dependenciesSimplifyConstant.generated.js +2 -0
  15. package/lib/cjs/entry/dependenciesAny/dependenciesSize.generated.js +0 -2
  16. package/lib/cjs/entry/dependenciesAny/dependenciesUnitClass.generated.js +0 -2
  17. package/lib/cjs/entry/dependenciesAny/dependenciesZeta.generated.js +2 -0
  18. package/lib/cjs/entry/dependenciesAny.generated.js +21 -0
  19. package/lib/cjs/entry/dependenciesNumber/dependenciesBernoulli.generated.js +21 -0
  20. package/lib/cjs/entry/dependenciesNumber/dependenciesConstantNode.generated.js +2 -0
  21. package/lib/cjs/entry/dependenciesNumber/dependenciesIsBounded.generated.js +17 -0
  22. package/lib/cjs/entry/dependenciesNumber/dependenciesIsFinite.generated.js +21 -0
  23. package/lib/cjs/entry/dependenciesNumber/dependenciesSimplifyConstant.generated.js +2 -0
  24. package/lib/cjs/entry/dependenciesNumber/dependenciesSize.generated.js +0 -2
  25. package/lib/cjs/entry/dependenciesNumber/dependenciesZeta.generated.js +2 -0
  26. package/lib/cjs/entry/dependenciesNumber.generated.js +21 -0
  27. package/lib/cjs/entry/impureFunctionsAny.generated.js +223 -218
  28. package/lib/cjs/entry/impureFunctionsNumber.generated.js +82 -77
  29. package/lib/cjs/entry/pureFunctionsAny.generated.js +717 -702
  30. package/lib/cjs/entry/pureFunctionsNumber.generated.js +155 -142
  31. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +6 -0
  32. package/lib/cjs/expression/embeddedDocs/function/combinatorics/stirlingS2.js +2 -2
  33. package/lib/cjs/expression/embeddedDocs/function/probability/bernoulli.js +14 -0
  34. package/lib/cjs/expression/embeddedDocs/function/utils/isBounded.js +14 -0
  35. package/lib/cjs/expression/embeddedDocs/function/utils/isFinite.js +14 -0
  36. package/lib/cjs/expression/embeddedDocs/function/utils/isNaN.js +1 -1
  37. package/lib/cjs/expression/embeddedDocs/function/utils/isNumeric.js +1 -1
  38. package/lib/cjs/expression/node/AccessorNode.js +36 -7
  39. package/lib/cjs/expression/node/ConstantNode.js +4 -4
  40. package/lib/cjs/expression/node/FunctionNode.js +20 -5
  41. package/lib/cjs/expression/node/IndexNode.js +1 -1
  42. package/lib/cjs/expression/parse.js +74 -46
  43. package/lib/cjs/factoriesAny.js +21 -0
  44. package/lib/cjs/factoriesNumber.js +23 -2
  45. package/lib/cjs/function/algebra/simplifyConstant.js +3 -2
  46. package/lib/cjs/function/algebra/sylvester.js +6 -5
  47. package/lib/cjs/function/arithmetic/nthRoots.js +5 -1
  48. package/lib/cjs/function/logical/nullish.js +2 -2
  49. package/lib/cjs/function/matrix/column.js +2 -1
  50. package/lib/cjs/function/matrix/dot.js +4 -9
  51. package/lib/cjs/function/matrix/flatten.js +6 -3
  52. package/lib/cjs/function/matrix/kron.js +31 -30
  53. package/lib/cjs/function/matrix/row.js +2 -1
  54. package/lib/cjs/function/matrix/size.js +11 -17
  55. package/lib/cjs/function/matrix/subset.js +21 -11
  56. package/lib/cjs/function/probability/bernoulli.js +108 -0
  57. package/lib/cjs/function/relational/equal.js +2 -3
  58. package/lib/cjs/function/special/zeta.js +3 -2
  59. package/lib/cjs/function/utils/isBounded.js +54 -0
  60. package/lib/cjs/function/utils/isFinite.js +50 -0
  61. package/lib/cjs/function/utils/isInteger.js +7 -15
  62. package/lib/cjs/function/utils/isNaN.js +1 -1
  63. package/lib/cjs/function/utils/isNumeric.js +1 -1
  64. package/lib/cjs/header.js +2 -2
  65. package/lib/cjs/json/replacer.js +1 -1
  66. package/lib/cjs/plain/number/probability.js +2 -2
  67. package/lib/cjs/plain/number/trigonometry.js +1 -1
  68. package/lib/cjs/type/fraction/function/fraction.js +1 -1
  69. package/lib/cjs/type/matrix/DenseMatrix.js +52 -41
  70. package/lib/cjs/type/matrix/MatrixIndex.js +19 -20
  71. package/lib/cjs/type/matrix/SparseMatrix.js +37 -11
  72. package/lib/cjs/type/unit/Unit.js +12 -8
  73. package/lib/cjs/utils/number.js +7 -7
  74. package/lib/cjs/utils/optimizeCallback.js +13 -1
  75. package/lib/cjs/version.js +1 -1
  76. package/lib/esm/core/config.js +5 -1
  77. package/lib/esm/core/function/config.js +4 -0
  78. package/lib/esm/entry/dependenciesAny/dependenciesBernoulli.generated.js +18 -0
  79. package/lib/esm/entry/dependenciesAny/dependenciesConstantNode.generated.js +2 -0
  80. package/lib/esm/entry/dependenciesAny/dependenciesEqual.generated.js +0 -2
  81. package/lib/esm/entry/dependenciesAny/dependenciesIsBounded.generated.js +10 -0
  82. package/lib/esm/entry/dependenciesAny/dependenciesIsFinite.generated.js +14 -0
  83. package/lib/esm/entry/dependenciesAny/dependenciesIsInteger.generated.js +2 -0
  84. package/lib/esm/entry/dependenciesAny/dependenciesSimplifyConstant.generated.js +2 -0
  85. package/lib/esm/entry/dependenciesAny/dependenciesSize.generated.js +0 -2
  86. package/lib/esm/entry/dependenciesAny/dependenciesUnitClass.generated.js +0 -2
  87. package/lib/esm/entry/dependenciesAny/dependenciesZeta.generated.js +2 -0
  88. package/lib/esm/entry/dependenciesAny.generated.js +3 -0
  89. package/lib/esm/entry/dependenciesNumber/dependenciesBernoulli.generated.js +14 -0
  90. package/lib/esm/entry/dependenciesNumber/dependenciesConstantNode.generated.js +2 -0
  91. package/lib/esm/entry/dependenciesNumber/dependenciesIsBounded.generated.js +10 -0
  92. package/lib/esm/entry/dependenciesNumber/dependenciesIsFinite.generated.js +14 -0
  93. package/lib/esm/entry/dependenciesNumber/dependenciesSimplifyConstant.generated.js +2 -0
  94. package/lib/esm/entry/dependenciesNumber/dependenciesSize.generated.js +0 -2
  95. package/lib/esm/entry/dependenciesNumber/dependenciesZeta.generated.js +2 -0
  96. package/lib/esm/entry/dependenciesNumber.generated.js +3 -0
  97. package/lib/esm/entry/impureFunctionsAny.generated.js +225 -220
  98. package/lib/esm/entry/impureFunctionsNumber.generated.js +84 -79
  99. package/lib/esm/entry/pureFunctionsAny.generated.js +714 -699
  100. package/lib/esm/entry/pureFunctionsNumber.generated.js +154 -141
  101. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +6 -0
  102. package/lib/esm/expression/embeddedDocs/function/combinatorics/stirlingS2.js +2 -2
  103. package/lib/esm/expression/embeddedDocs/function/probability/bernoulli.js +8 -0
  104. package/lib/esm/expression/embeddedDocs/function/utils/isBounded.js +8 -0
  105. package/lib/esm/expression/embeddedDocs/function/utils/isFinite.js +8 -0
  106. package/lib/esm/expression/embeddedDocs/function/utils/isNaN.js +1 -1
  107. package/lib/esm/expression/embeddedDocs/function/utils/isNumeric.js +1 -1
  108. package/lib/esm/expression/node/AccessorNode.js +36 -7
  109. package/lib/esm/expression/node/ConstantNode.js +4 -4
  110. package/lib/esm/expression/node/FunctionNode.js +20 -5
  111. package/lib/esm/expression/node/IndexNode.js +1 -1
  112. package/lib/esm/expression/parse.js +74 -46
  113. package/lib/esm/factoriesAny.js +3 -0
  114. package/lib/esm/factoriesNumber.js +3 -0
  115. package/lib/esm/function/algebra/simplifyConstant.js +3 -2
  116. package/lib/esm/function/algebra/sylvester.js +6 -5
  117. package/lib/esm/function/arithmetic/nthRoots.js +5 -1
  118. package/lib/esm/function/logical/nullish.js +2 -2
  119. package/lib/esm/function/matrix/column.js +2 -1
  120. package/lib/esm/function/matrix/dot.js +4 -9
  121. package/lib/esm/function/matrix/flatten.js +6 -3
  122. package/lib/esm/function/matrix/kron.js +31 -30
  123. package/lib/esm/function/matrix/row.js +2 -1
  124. package/lib/esm/function/matrix/size.js +11 -17
  125. package/lib/esm/function/matrix/subset.js +21 -11
  126. package/lib/esm/function/probability/bernoulli.js +102 -0
  127. package/lib/esm/function/relational/equal.js +2 -3
  128. package/lib/esm/function/special/zeta.js +3 -2
  129. package/lib/esm/function/utils/isBounded.js +48 -0
  130. package/lib/esm/function/utils/isFinite.js +44 -0
  131. package/lib/esm/function/utils/isInteger.js +7 -15
  132. package/lib/esm/function/utils/isNaN.js +1 -1
  133. package/lib/esm/function/utils/isNumeric.js +1 -1
  134. package/lib/esm/json/replacer.js +1 -1
  135. package/lib/esm/plain/number/probability.js +2 -2
  136. package/lib/esm/plain/number/trigonometry.js +1 -1
  137. package/lib/esm/type/fraction/function/fraction.js +1 -1
  138. package/lib/esm/type/matrix/DenseMatrix.js +52 -41
  139. package/lib/esm/type/matrix/MatrixIndex.js +20 -21
  140. package/lib/esm/type/matrix/SparseMatrix.js +37 -11
  141. package/lib/esm/type/unit/Unit.js +12 -8
  142. package/lib/esm/utils/number.js +7 -7
  143. package/lib/esm/utils/optimizeCallback.js +13 -1
  144. package/lib/esm/version.js +1 -1
  145. package/package.json +8 -8
  146. package/types/index.d.ts +535 -223
@@ -79,7 +79,11 @@ const createNthRoots = exports.createNthRoots = /* #__PURE__ */(0, _factory.fact
79
79
  * Calculate the nth roots of a value.
80
80
  * An nth root of a positive real number A,
81
81
  * is a positive real solution of the equation "x^root = A".
82
- * This function returns an array of complex values.
82
+ * This function returns an array of Complex values.
83
+ * Note that currently the precision of Complex numbers are limited
84
+ * to the precision of a 64-bit IEEE floating point, so even if the input
85
+ * is a BigNumber with greater precision, rounding to 64 bits will occur
86
+ * in computing the nth roots.
83
87
  *
84
88
  * Syntax:
85
89
  *
@@ -68,8 +68,8 @@ const createNullish = exports.createNullish = /* #__PURE__ */(0, _factory.factor
68
68
  'undefined, any': (_x, y) => y,
69
69
  // SparseMatrix-first with collection RHS: enforce exact shape match
70
70
  'SparseMatrix, Array | Matrix': (x, y) => {
71
- const sx = flatten(size(x).valueOf()); // work around #3529/#3530
72
- const sy = flatten(size(y).valueOf());
71
+ const sx = size(x);
72
+ const sy = size(y);
73
73
  if (deepEqual(sx, sy)) return x;
74
74
  throw new _DimensionError.DimensionError(sx, sy);
75
75
  },
@@ -58,8 +58,9 @@ const createColumn = exports.createColumn = /* #__PURE__ */(0, _factory.factory)
58
58
  }
59
59
  (0, _array.validateIndex)(column, value.size()[1]);
60
60
  const rowRange = range(0, value.size()[0]);
61
- const index = new Index(rowRange, column);
61
+ const index = new Index(rowRange, [column]);
62
62
  const result = value.subset(index);
63
+ // once config.legacySubset just return result
63
64
  return (0, _is.isMatrix)(result) ? result : matrix([[result]]);
64
65
  }
65
66
  });
@@ -44,8 +44,8 @@ const createDot = exports.createDot = /* #__PURE__ */(0, _factory.factory)(name,
44
44
  'SparseMatrix, SparseMatrix': _sparseDot
45
45
  });
46
46
  function _validateDim(x, y) {
47
- const xSize = _size(x);
48
- const ySize = _size(y);
47
+ const xSize = size(x);
48
+ const ySize = size(y);
49
49
  let xLen, yLen;
50
50
  if (xSize.length === 1) {
51
51
  xLen = xSize[0];
@@ -73,8 +73,8 @@ const createDot = exports.createDot = /* #__PURE__ */(0, _factory.factory)(name,
73
73
  const bdt = (0, _is.isMatrix)(b) ? b._datatype || b.getDataType() : undefined;
74
74
 
75
75
  // are these 2-dimensional column vectors? (as opposed to 1-dimensional vectors)
76
- const aIsColumn = _size(a).length === 2;
77
- const bIsColumn = _size(b).length === 2;
76
+ const aIsColumn = size(a).length === 2;
77
+ const bIsColumn = size(b).length === 2;
78
78
  let add = addScalar;
79
79
  let mul = multiplyScalar;
80
80
 
@@ -154,9 +154,4 @@ const createDot = exports.createDot = /* #__PURE__ */(0, _factory.factory)(name,
154
154
  }
155
155
  return c;
156
156
  }
157
-
158
- // TODO remove this once #1771 is fixed
159
- function _size(x) {
160
- return (0, _is.isMatrix)(x) ? x.size() : size(x);
161
- }
162
157
  });
@@ -28,17 +28,20 @@ const createFlatten = exports.createFlatten = /* #__PURE__ */(0, _factory.factor
28
28
  *
29
29
  * concat, resize, size, squeeze
30
30
  *
31
- * @param {Matrix | Array} x Matrix to be flattened
32
- * @return {Matrix | Array} Returns the flattened matrix
31
+ * @param {DenseMatrix | Array} x Matrix to be flattened
32
+ * @return {DenseMatrix | Array} Returns the flattened matrix
33
33
  */
34
34
  return typed(name, {
35
35
  Array: function (x) {
36
36
  return (0, _array.flatten)(x);
37
37
  },
38
- Matrix: function (x) {
38
+ DenseMatrix: function (x) {
39
39
  // Return the same matrix type as x (Dense or Sparse Matrix)
40
40
  // Return the same data type as x
41
41
  return x.create((0, _array.flatten)(x.valueOf(), true), x.datatype());
42
+ },
43
+ SparseMatrix: function (_x) {
44
+ throw new TypeError('SparseMatrix is not supported by function flatten ' + 'because it does not support 1D vectors. ' + 'Convert to a DenseMatrix or Array first. Example: flatten(x.toArray())');
42
45
  }
43
46
  });
44
47
  });
@@ -31,7 +31,7 @@ const createKron = exports.createKron = /* #__PURE__ */(0, _factory.factory)(nam
31
31
  * // returns [ [ 1, 2, 0, 0 ], [ 3, 4, 0, 0 ], [ 0, 0, 1, 2 ], [ 0, 0, 3, 4 ] ]
32
32
  *
33
33
  * math.kron([1,1], [2,3,4])
34
- * // returns [ [ 2, 3, 4, 2, 3, 4 ] ]
34
+ * // returns [2, 3, 4, 2, 3, 4]
35
35
  *
36
36
  * See also:
37
37
  *
@@ -55,37 +55,38 @@ const createKron = exports.createKron = /* #__PURE__ */(0, _factory.factory)(nam
55
55
  });
56
56
 
57
57
  /**
58
- * Calculate the Kronecker product of two matrices / vectors
59
- * @param {Array} a First vector
60
- * @param {Array} b Second vector
61
- * @returns {Array} Returns the Kronecker product of x and y
62
- * @private
58
+ * Calculate the Kronecker product of two (1-dimensional) vectors,
59
+ * with no dimension checking
60
+ * @param {Array} a First vector
61
+ * @param {Array} b Second vector
62
+ * @returns {Array} the 1-dimensional Kronecker product of a and b
63
+ * @private
64
+ */
65
+ function _kron1d(a, b) {
66
+ // TODO in core overhaul: would be faster to see if we can choose a
67
+ // particular implementation of multiplyScalar at the beginning,
68
+ // rather than re-dispatch for _every_ ordered pair of entries.
69
+ return a.flatMap(x => b.map(y => multiplyScalar(x, y)));
70
+ }
71
+
72
+ /**
73
+ * Calculate the Kronecker product of two possibly multidimensional arrays
74
+ * @param {Array} a First array
75
+ * @param {Array} b Second array
76
+ * @param {number} [d] common dimension; if missing, compute and match args
77
+ * @returns {Array} Returns the Kronecker product of x and y
78
+ * @private
63
79
  */
64
80
  function _kron(a, b) {
65
- // Deal with the dimensions of the matricies.
66
- if ((0, _array.arraySize)(a).length === 1) {
67
- // Wrap it in a 2D Matrix
68
- a = [a];
69
- }
70
- if ((0, _array.arraySize)(b).length === 1) {
71
- // Wrap it in a 2D Matrix
72
- b = [b];
73
- }
74
- if ((0, _array.arraySize)(a).length > 2 || (0, _array.arraySize)(b).length > 2) {
75
- throw new RangeError('Vectors with dimensions greater then 2 are not supported expected ' + '(Size x = ' + JSON.stringify(a.length) + ', y = ' + JSON.stringify(b.length) + ')');
81
+ let d = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1;
82
+ if (d < 0) {
83
+ let adim = (0, _array.arraySize)(a).length;
84
+ let bdim = (0, _array.arraySize)(b).length;
85
+ d = Math.max(adim, bdim);
86
+ while (adim++ < d) a = [a];
87
+ while (bdim++ < d) b = [b];
76
88
  }
77
- const t = [];
78
- let r = [];
79
- return a.map(function (a) {
80
- return b.map(function (b) {
81
- r = [];
82
- t.push(r);
83
- return a.map(function (y) {
84
- return b.map(function (x) {
85
- return r.push(multiplyScalar(y, x));
86
- });
87
- });
88
- });
89
- }) && t;
89
+ if (d === 1) return _kron1d(a, b);
90
+ return a.flatMap(aSlice => b.map(bSlice => _kron(aSlice, bSlice, d - 1)));
90
91
  }
91
92
  });
@@ -58,8 +58,9 @@ const createRow = exports.createRow = /* #__PURE__ */(0, _factory.factory)(name,
58
58
  }
59
59
  (0, _array.validateIndex)(row, value.size()[0]);
60
60
  const columnRange = range(0, value.size()[1]);
61
- const index = new Index(row, columnRange);
61
+ const index = new Index([row], columnRange);
62
62
  const result = value.subset(index);
63
+ // once config.legacySubset just return result
63
64
  return (0, _is.isMatrix)(result) ? result : matrix([[result]]);
64
65
  }
65
66
  });
@@ -6,17 +6,17 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.createSize = void 0;
7
7
  var _array = require("../../utils/array.js");
8
8
  var _factory = require("../../utils/factory.js");
9
- var _noop = require("../../utils/noop.js");
10
9
  const name = 'size';
11
- const dependencies = ['typed', 'config', '?matrix'];
10
+ const dependencies = ['typed'];
12
11
  const createSize = exports.createSize = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
13
12
  let {
14
- typed,
15
- config,
16
- matrix
13
+ typed
17
14
  } = _ref;
18
15
  /**
19
- * Calculate the size of a matrix or scalar.
16
+ * Calculate the size of a matrix or scalar. Always returns an Array containing numbers.
17
+ *
18
+ * Note that in mathjs v14 and older, function size could return a Matrix depending on
19
+ * the input type and configuration.
20
20
  *
21
21
  * Syntax:
22
22
  *
@@ -36,19 +36,13 @@ const createSize = exports.createSize = /* #__PURE__ */(0, _factory.factory)(nam
36
36
  * count, resize, squeeze, subset
37
37
  *
38
38
  * @param {boolean | number | Complex | Unit | string | Array | Matrix} x A matrix
39
- * @return {Array | Matrix} A vector with size of `x`.
39
+ * @return {Array} A vector with size of `x`.
40
40
  */
41
41
  return typed(name, {
42
- Matrix: function (x) {
43
- return x.create(x.size(), 'number');
44
- },
42
+ Matrix: x => x.size(),
45
43
  Array: _array.arraySize,
46
- string: function (x) {
47
- return config.matrix === 'Array' ? [x.length] : matrix([x.length], 'dense', 'number');
48
- },
49
- 'number | Complex | BigNumber | Unit | boolean | null': function (x) {
50
- // scalar
51
- return config.matrix === 'Array' ? [] : matrix ? matrix([], 'dense', 'number') : (0, _noop.noMatrix)();
52
- }
44
+ string: x => [x.length],
45
+ // scalar
46
+ 'number | Complex | BigNumber | Unit | boolean | null': _x => []
53
47
  });
54
48
  });
@@ -31,8 +31,8 @@ const createSubset = exports.createSubset = /* #__PURE__ */(0, _factory.factory)
31
31
  * // get a subset
32
32
  * const d = [[1, 2], [3, 4]]
33
33
  * math.subset(d, math.index(1, 0)) // returns 3
34
- * math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]]
35
- * math.subset(d, math.index([false, true], 0)) // returns [[3]]
34
+ * math.subset(d, math.index([0, 1], [1])) // returns [[2], [4]]
35
+ * math.subset(d, math.index([false, true], [0])) // returns [[3]]
36
36
  *
37
37
  * // replace a subset
38
38
  * const e = []
@@ -42,9 +42,9 @@ const createSubset = exports.createSubset = /* #__PURE__ */(0, _factory.factory)
42
42
  *
43
43
  * // get submatrix using ranges
44
44
  * const M = [
45
- * [1,2,3],
46
- * [4,5,6],
47
- * [7,8,9]
45
+ * [1, 2, 3],
46
+ * [4, 5, 6],
47
+ * [7, 8, 9]
48
48
  * ]
49
49
  * math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1, 2, 3], [4, 5, 6]]
50
50
  *
@@ -123,7 +123,7 @@ const createSubset = exports.createSubset = /* #__PURE__ */(0, _factory.factory)
123
123
  if (typeof replacement === 'string') {
124
124
  throw new Error('can\'t boradcast a string');
125
125
  }
126
- if (index._isScalar) {
126
+ if (index.isScalar()) {
127
127
  return replacement;
128
128
  }
129
129
  const indexSize = index.size();
@@ -165,9 +165,14 @@ function _getSubstring(str, index) {
165
165
  (0, _array.validateIndex)(index.max()[0], strLen);
166
166
  const range = index.dimension(0);
167
167
  let substr = '';
168
- range.forEach(function (v) {
168
+ function callback(v) {
169
169
  substr += str.charAt(v);
170
- });
170
+ }
171
+ if (Number.isInteger(range)) {
172
+ callback(range);
173
+ } else {
174
+ range.forEach(callback);
175
+ }
171
176
  return substr;
172
177
  }
173
178
 
@@ -201,7 +206,7 @@ function _setSubstring(str, index, replacement, defaultValue) {
201
206
  defaultValue = ' ';
202
207
  }
203
208
  const range = index.dimension(0);
204
- const len = range.size()[0];
209
+ const len = Number.isInteger(range) ? 1 : range.size()[0];
205
210
  if (len !== replacement.length) {
206
211
  throw new _DimensionError.DimensionError(range.size()[0], replacement.length);
207
212
  }
@@ -216,9 +221,14 @@ function _setSubstring(str, index, replacement, defaultValue) {
216
221
  for (let i = 0; i < strLen; i++) {
217
222
  chars[i] = str.charAt(i);
218
223
  }
219
- range.forEach(function (v, i) {
224
+ function callback(v, i) {
220
225
  chars[v] = replacement.charAt(i[0]);
221
- });
226
+ }
227
+ if (Number.isInteger(range)) {
228
+ callback(range, [0]);
229
+ } else {
230
+ range.forEach(callback);
231
+ }
222
232
 
223
233
  // initialize undefined characters with a space
224
234
  if (chars.length > strLen) {
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBernoulli = void 0;
7
+ var _factory = require("../../utils/factory.js");
8
+ var _number = require("../../utils/number.js");
9
+ const name = 'bernoulli';
10
+ const dependencies = ['typed', 'config', 'isInteger', 'number', '?BigNumber', '?Fraction'];
11
+ const createBernoulli = exports.createBernoulli = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
12
+ let {
13
+ typed,
14
+ config,
15
+ number,
16
+ BigNumber,
17
+ Fraction
18
+ } = _ref;
19
+ /**
20
+ * Return the `n`th Bernoulli number, for positive integers `n`
21
+ *
22
+ * Syntax:
23
+ *
24
+ * math.bernoulli(n)
25
+ *
26
+ * Examples:
27
+ *
28
+ * math.bernoulli(1) // returns -0.5
29
+ * // All other odd Bernoulli numbers are 0:
30
+ * math.bernoulli(7) // returns 0
31
+ * math.bernoulli(math.bignumber(6)) // value bignumber(1).div(42)
32
+ * // Produces exact rationals for bigint or fraction input:
33
+ * math.bernoulli(8n) // Fraction -1,30
34
+ * math.bernoulli(math.fraction(10)) // Fraction 5,66
35
+ *
36
+ * See also:
37
+ *
38
+ * combinations, gamma, stirlingS2
39
+ *
40
+ * @param {number | BigNumber | bigint | Fraction} n
41
+ * Index of the Bernoulli number
42
+ * @return {number | BigNumber | Fraction}
43
+ * nth Bernoulli number, of a type corresponding to the argument n
44
+ */
45
+
46
+ const numberCache = [undefined];
47
+ const fractionCache = [undefined];
48
+ let bigCache = [undefined];
49
+ let cachedPrecision = 50;
50
+ return typed(name, {
51
+ number: index => _bernoulli(index, n => n, numberCache, (a, b) => a + b, (a, b) => a * b, (a, b) => a / b),
52
+ 'bigint | Fraction': index => _bernoulli(number(index), n => new Fraction(n), fractionCache, (a, b) => a.add(b), (a, b) => a.mul(b), (a, b) => a.div(b)),
53
+ BigNumber: index => {
54
+ if (config.precision !== cachedPrecision) {
55
+ bigCache = [undefined];
56
+ cachedPrecision = config.precision;
57
+ }
58
+ return _bernoulli(number(index), n => new BigNumber(n), bigCache, (a, b) => a.add(b), (a, b) => a.mul(b), (a, b) => a.div(b));
59
+ }
60
+ });
61
+ });
62
+
63
+ /**
64
+ * Underlying implementation, with all operations passed in.
65
+ * Parameters:
66
+ * 1. index: a (positive integer) number specifying which Bernoulli number
67
+ * to compute.
68
+ * 2. promote: a function that takes an integer number and returns
69
+ * the desired type for the Bernoulli number values.
70
+ * 3. A: a cache array of partial computation data that _bernoulli should use.
71
+ * Different cache arrays should be provided for different types.
72
+ * 4. plus: a function that adds two values of the desired type.
73
+ * 5. times: a function that multiplies two values of the desired type.
74
+ * 6. divide: a function that divides one value of the desired type by another.
75
+ */
76
+ function _bernoulli(index, promote, A, plus, times, divide) {
77
+ if (index < 0 || !(0, _number.isInteger)(index)) {
78
+ throw new RangeError('Bernoulli index must be nonnegative integer');
79
+ }
80
+ if (index === 0) return promote(1);
81
+ if (index === 1) return divide(promote(-1), promote(2));
82
+ if (index % 2 === 1) return promote(0);
83
+ // We proceed as in https://math.stackexchange.com/a/2844337
84
+ // (by no means the most efficient, but very simple to implement)
85
+ // A cache entry consists of a triple
86
+ // [cotangent coefficient a_n, prefactor, Bernouilli number B_2n]
87
+ const one = promote(1);
88
+ if (A.length === 1) {
89
+ A.push([divide(one, promote(-3)), divide(one, promote(-2)), divide(one, promote(6))]);
90
+ }
91
+ const half = index / 2;
92
+ const zero = promote(0);
93
+ const two = promote(2);
94
+ while (A.length <= half) {
95
+ const i = A.length; // next cotangent coefficient to compute
96
+ const lim = Math.floor((i + 1) / 2);
97
+ let a = zero;
98
+ for (let m = 1; m < lim; ++m) {
99
+ a = plus(a, times(A[m][0], A[i - m][0]));
100
+ }
101
+ a = times(a, two);
102
+ if (i % 2 === 0) a = plus(a, times(A[lim][0], A[lim][0]));
103
+ a = divide(a, promote(-(2 * i + 1)));
104
+ const prefactor = divide(times(A[i - 1][1], promote(-i * (2 * i - 1))), two);
105
+ A.push([a, prefactor, times(prefactor, a)]);
106
+ }
107
+ return A[half][2];
108
+ }
@@ -10,7 +10,7 @@ var _matAlgo07xSSf = require("../../type/matrix/utils/matAlgo07xSSf.js");
10
10
  var _matAlgo12xSfs = require("../../type/matrix/utils/matAlgo12xSfs.js");
11
11
  var _matrixAlgorithmSuite = require("../../type/matrix/utils/matrixAlgorithmSuite.js");
12
12
  const name = 'equal';
13
- const dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'concat', 'SparseMatrix'];
13
+ const dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'SparseMatrix'];
14
14
  const createEqual = exports.createEqual = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
15
15
  let {
16
16
  typed,
@@ -33,8 +33,7 @@ const createEqual = exports.createEqual = /* #__PURE__ */(0, _factory.factory)(n
33
33
  });
34
34
  const matrixAlgorithmSuite = (0, _matrixAlgorithmSuite.createMatrixAlgorithmSuite)({
35
35
  typed,
36
- matrix,
37
- concat
36
+ matrix
38
37
  });
39
38
 
40
39
  /**
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.createZeta = void 0;
7
7
  var _factory = require("../../utils/factory.js");
8
8
  const name = 'zeta';
9
- const dependencies = ['typed', 'config', 'multiply', 'pow', 'divide', 'factorial', 'equal', 'smallerEq', 'isNegative', 'gamma', 'sin', 'subtract', 'add', '?Complex', '?BigNumber', 'pi'];
9
+ const dependencies = ['typed', 'config', 'multiply', 'pow', 'divide', 'factorial', 'equal', 'smallerEq', 'isBounded', 'isNegative', 'gamma', 'sin', 'subtract', 'add', '?Complex', '?BigNumber', 'pi'];
10
10
  const createZeta = exports.createZeta = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
11
11
  let {
12
12
  typed,
@@ -17,6 +17,7 @@ const createZeta = exports.createZeta = /* #__PURE__ */(0, _factory.factory)(nam
17
17
  factorial,
18
18
  equal,
19
19
  smallerEq,
20
+ isBounded,
20
21
  isNegative,
21
22
  gamma,
22
23
  sin,
@@ -75,7 +76,7 @@ const createZeta = exports.createZeta = /* #__PURE__ */(0, _factory.factory)(nam
75
76
  if (equal(s, 1)) {
76
77
  return createValue(NaN);
77
78
  }
78
- if (!isFinite(s)) {
79
+ if (!isBounded(s)) {
79
80
  return isNegative(s) ? createValue(NaN) : createValue(1);
80
81
  }
81
82
  return zeta(s, createValue, determineDigits, s => s);
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createIsBounded = void 0;
7
+ var _factory = require("../../utils/factory.js");
8
+ const name = 'isBounded';
9
+ const dependencies = ['typed'];
10
+ const createIsBounded = exports.createIsBounded = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
11
+ let {
12
+ typed
13
+ } = _ref;
14
+ /**
15
+ * Test whether a value is bounded. For scalars, this test is equivalent
16
+ * to the isFinite finiteness test. On the other hand, a Matrix or Array
17
+ * is defined to be bounded if every entry is finite.
18
+ *
19
+ * Syntax:
20
+ *
21
+ * math.isBounded(x)
22
+ *
23
+ * Examples:
24
+ *
25
+ * math.isBounded(0) // returns true
26
+ * math.isBounded(NaN) // returns false
27
+ * math.isBounded(math.bignumber(Infinity)) // returns false
28
+ * math.isBounded(math.fraction(1,3)) // returns true
29
+ * math.isBounded(math.complex('2 - 4i')) // returns true
30
+ * math.isBounded(-10000000000000000n) // returns true
31
+ * math.isBounded(undefined) // returns false
32
+ * math.isBounded(null) // returns false
33
+ * math.isBounded([0.001, -3n, 0]) // returns true
34
+ * math.isBounded([2, -Infinity, -3]) // returns false
35
+ *
36
+ * See also:
37
+ *
38
+ * isFinite, isNumeric, isPositive, isNegative, isNaN
39
+ *
40
+ * @param {number | BigNumber | bigint | Complex | Fraction | Unit | Array | Matrix} x Value to be tested
41
+ * @return {boolean} Returns true when `x` is bounded.
42
+ */
43
+ return typed(name, {
44
+ number: n => Number.isFinite(n),
45
+ 'BigNumber | Complex': x => x.isFinite(),
46
+ 'bigint | Fraction': () => true,
47
+ 'null | undefined': () => false,
48
+ Unit: typed.referToSelf(self => x => self(x.value)),
49
+ 'Array | Matrix': typed.referToSelf(self => A => {
50
+ if (!Array.isArray(A)) A = A.valueOf();
51
+ return A.every(entry => self(entry));
52
+ })
53
+ });
54
+ });
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createIsFinite = void 0;
7
+ var _factory = require("../../utils/factory.js");
8
+ const name = 'isFinite';
9
+ const dependencies = ['typed', 'isBounded', 'map'];
10
+ const createIsFinite = exports.createIsFinite = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
11
+ let {
12
+ typed,
13
+ isBounded,
14
+ map
15
+ } = _ref;
16
+ /**
17
+ * Test whether a value is finite.
18
+ *
19
+ * Operates elementwise on Array and Matrix values. To test if all entries
20
+ * of an Array or Matrix are finite, use isBounded.
21
+ *
22
+ * Syntax:
23
+ *
24
+ * math.isFinite(x)
25
+ *
26
+ * Examples:
27
+ *
28
+ * math.isFinite(0) // returns true
29
+ * math.isFinite(NaN) // returns false
30
+ * math.isFinite(math.bignumber(Infinity)) // returns false
31
+ * math.isFinite(math.fraction(1,3)) // returns true
32
+ * math.isFinite(math.complex('2 - 4i')) // returns true
33
+ * math.isFinite(-10000000000000000n) // returns true
34
+ * math.isFinite(undefined) // returns false
35
+ * math.isFinite(null) // returns false
36
+ * math.isFinite([0.001, -3n, 0]) // Array [true, true, true]
37
+ * math.isFinite([2, -Infinity, -3]) // Array [true, false, true]
38
+ *
39
+ * See also:
40
+ *
41
+ * isBounded isNumeric, isPositive, isNegative, isNaN
42
+ *
43
+ * @param {number | BigNumber | bigint | Complex | Fraction | Unit | Array | Matrix} x Value to be tested
44
+ * @return {boolean | Array | Matrix}
45
+ */
46
+ return typed(name, {
47
+ 'Array | Matrix': A => map(A, isBounded),
48
+ any: x => isBounded(x)
49
+ });
50
+ });
@@ -5,13 +5,13 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createIsInteger = void 0;
7
7
  var _collection = require("../../utils/collection.js");
8
- var _number = require("../../utils/number.js");
9
8
  var _factory = require("../../utils/factory.js");
10
9
  const name = 'isInteger';
11
- const dependencies = ['typed'];
10
+ const dependencies = ['typed', 'equal'];
12
11
  const createIsInteger = exports.createIsInteger = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
13
12
  let {
14
- typed
13
+ typed,
14
+ equal
15
15
  } = _ref;
16
16
  /**
17
17
  * Test whether a value is an integer number.
@@ -43,18 +43,10 @@ const createIsInteger = exports.createIsInteger = /* #__PURE__ */(0, _factory.fa
43
43
  * Throws an error in case of an unknown data type.
44
44
  */
45
45
  return typed(name, {
46
- number: _number.isInteger,
47
- // TODO: what to do with isInteger(add(0.1, 0.2)) ?
48
-
49
- BigNumber: function (x) {
50
- return x.isInt();
51
- },
52
- bigint: function (x) {
53
- return true;
54
- },
55
- Fraction: function (x) {
56
- return x.d === 1n;
57
- },
46
+ number: n => Number.isFinite(n) ? equal(n, Math.round(n)) : false,
47
+ BigNumber: b => b.isFinite() ? equal(b.round(), b) : false,
48
+ bigint: b => true,
49
+ Fraction: r => r.d === 1n,
58
50
  'Array | Matrix': typed.referToSelf(self => x => (0, _collection.deepMap)(x, self))
59
51
  });
60
52
  });
@@ -36,7 +36,7 @@ const createIsNaN = exports.createIsNaN = /* #__PURE__ */(0, _factory.factory)(n
36
36
  *
37
37
  * See also:
38
38
  *
39
- * isNumeric, isNegative, isPositive, isZero, isInteger
39
+ * isNumeric, isNegative, isPositive, isZero, isInteger, isFinite, isBounded
40
40
  *
41
41
  * @param {number | BigNumber | bigint | Fraction | Unit | Array | Matrix} x Value to be tested
42
42
  * @return {boolean} Returns true when `x` is NaN.
@@ -35,7 +35,7 @@ const createIsNumeric = exports.createIsNumeric = /* #__PURE__ */(0, _factory.fa
35
35
  *
36
36
  * See also:
37
37
  *
38
- * isZero, isPositive, isNegative, isInteger, hasNumericValue
38
+ * isZero, isPositive, isNegative, isInteger, hasNumericValue, isFinite, isBounded
39
39
  *
40
40
  * @param {*} x Value to be tested
41
41
  * @return {boolean} Returns true when `x` is a `number`, `BigNumber`,
package/lib/cjs/header.js CHANGED
@@ -6,8 +6,8 @@
6
6
  * It features real and complex numbers, units, matrices, a large set of
7
7
  * mathematical functions, and a flexible expression parser.
8
8
  *
9
- * @version 14.9.1
10
- * @date 2025-10-10
9
+ * @version 15.1.0
10
+ * @date 2025-11-05
11
11
  *
12
12
  * @license
13
13
  * Copyright (C) 2013-2025 Jos de Jong <wjosdejong@gmail.com>
@@ -22,7 +22,7 @@ const createReplacer = exports.createReplacer = /* #__PURE__ */(0, _factory.fact
22
22
  */
23
23
  return function replacer(key, value) {
24
24
  // the numeric values Infinitiy, -Infinity, and NaN cannot be serialized to JSON
25
- if (typeof value === 'number' && (!isFinite(value) || isNaN(value))) {
25
+ if (typeof value === 'number' && (!Number.isFinite(value) || isNaN(value))) {
26
26
  return {
27
27
  mathjs: 'number',
28
28
  value: String(value)