mathjs 13.0.3 → 13.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. package/HISTORY.md +30 -1
  2. package/bin/cli.js +24 -10
  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/create.js +12 -9
  7. package/lib/cjs/core/function/typed.js +3 -4
  8. package/lib/cjs/entry/dependenciesAny/dependenciesFlatten.generated.js +0 -2
  9. package/lib/cjs/entry/dependenciesAny/dependenciesSqueeze.generated.js +0 -2
  10. package/lib/cjs/entry/impureFunctionsAny.generated.js +2 -2
  11. package/lib/cjs/entry/pureFunctionsAny.generated.js +6 -8
  12. package/lib/cjs/entry/typeChecks.js +18 -0
  13. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +2 -2
  14. package/lib/cjs/expression/embeddedDocs/function/arithmetic/hypot.js +1 -1
  15. package/lib/cjs/expression/embeddedDocs/function/matrix/diff.js +1 -1
  16. package/lib/cjs/expression/embeddedDocs/function/matrix/fft.js +1 -1
  17. package/lib/cjs/expression/embeddedDocs/function/matrix/ifft.js +1 -1
  18. package/lib/cjs/expression/embeddedDocs/function/matrix/kron.js +1 -1
  19. package/lib/cjs/expression/embeddedDocs/function/matrix/map.js +3 -3
  20. package/lib/cjs/expression/embeddedDocs/function/special/zeta.js +1 -1
  21. package/lib/cjs/expression/embeddedDocs/function/statistics/quantileSeq.js +1 -1
  22. package/lib/cjs/expression/embeddedDocs/function/trigonometry/acoth.js +1 -1
  23. package/lib/cjs/expression/embeddedDocs/function/trigonometry/acsch.js +1 -1
  24. package/lib/cjs/expression/embeddedDocs/function/utils/clone.js +1 -1
  25. package/lib/cjs/expression/function/evaluate.js +5 -0
  26. package/lib/cjs/expression/node/FunctionNode.js +10 -2
  27. package/lib/cjs/expression/transform/filter.transform.js +2 -2
  28. package/lib/cjs/expression/transform/map.transform.js +104 -37
  29. package/lib/cjs/expression/transform/utils/dimToZeroBase.js +23 -0
  30. package/lib/cjs/expression/transform/utils/lastDimToZeroBase.js +3 -4
  31. package/lib/cjs/function/arithmetic/hypot.js +3 -3
  32. package/lib/cjs/function/matrix/apply.js +1 -1
  33. package/lib/cjs/function/matrix/fft.js +3 -3
  34. package/lib/cjs/function/matrix/filter.js +2 -2
  35. package/lib/cjs/function/matrix/flatten.js +5 -6
  36. package/lib/cjs/function/matrix/ifft.js +2 -2
  37. package/lib/cjs/function/matrix/kron.js +4 -4
  38. package/lib/cjs/function/matrix/map.js +109 -18
  39. package/lib/cjs/function/matrix/size.js +7 -7
  40. package/lib/cjs/function/matrix/squeeze.js +3 -4
  41. package/lib/cjs/function/probability/random.js +1 -1
  42. package/lib/cjs/function/probability/randomInt.js +1 -1
  43. package/lib/cjs/function/statistics/cumsum.js +2 -2
  44. package/lib/cjs/function/trigonometry/acoth.js +2 -2
  45. package/lib/cjs/function/trigonometry/acsch.js +2 -2
  46. package/lib/cjs/header.js +2 -2
  47. package/lib/cjs/type/matrix/DenseMatrix.js +3 -28
  48. package/lib/cjs/type/matrix/SparseMatrix.js +5 -8
  49. package/lib/cjs/utils/array.js +27 -0
  50. package/lib/cjs/utils/collection.js +1 -1
  51. package/lib/cjs/utils/customs.js +5 -12
  52. package/lib/cjs/utils/function.js +0 -14
  53. package/lib/cjs/utils/is.js +27 -0
  54. package/lib/cjs/utils/map.js +7 -23
  55. package/lib/cjs/version.js +1 -1
  56. package/lib/esm/core/create.js +9 -6
  57. package/lib/esm/core/function/typed.js +2 -3
  58. package/lib/esm/entry/dependenciesAny/dependenciesFlatten.generated.js +0 -2
  59. package/lib/esm/entry/dependenciesAny/dependenciesSqueeze.generated.js +0 -2
  60. package/lib/esm/entry/impureFunctionsAny.generated.js +3 -3
  61. package/lib/esm/entry/pureFunctionsAny.generated.js +7 -9
  62. package/lib/esm/entry/typeChecks.js +1 -1
  63. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +2 -2
  64. package/lib/esm/expression/embeddedDocs/function/arithmetic/hypot.js +1 -1
  65. package/lib/esm/expression/embeddedDocs/function/matrix/diff.js +1 -1
  66. package/lib/esm/expression/embeddedDocs/function/matrix/fft.js +1 -1
  67. package/lib/esm/expression/embeddedDocs/function/matrix/ifft.js +1 -1
  68. package/lib/esm/expression/embeddedDocs/function/matrix/kron.js +1 -1
  69. package/lib/esm/expression/embeddedDocs/function/matrix/map.js +3 -3
  70. package/lib/esm/expression/embeddedDocs/function/special/zeta.js +1 -1
  71. package/lib/esm/expression/embeddedDocs/function/statistics/quantileSeq.js +1 -1
  72. package/lib/esm/expression/embeddedDocs/function/trigonometry/acoth.js +1 -1
  73. package/lib/esm/expression/embeddedDocs/function/trigonometry/acsch.js +1 -1
  74. package/lib/esm/expression/embeddedDocs/function/utils/clone.js +1 -1
  75. package/lib/esm/expression/function/compile.js +1 -1
  76. package/lib/esm/expression/function/evaluate.js +8 -3
  77. package/lib/esm/expression/node/FunctionNode.js +10 -2
  78. package/lib/esm/expression/parse.js +2 -2
  79. package/lib/esm/expression/transform/filter.transform.js +4 -4
  80. package/lib/esm/expression/transform/forEach.transform.js +4 -4
  81. package/lib/esm/expression/transform/map.transform.js +104 -37
  82. package/lib/esm/expression/transform/print.transform.js +2 -2
  83. package/lib/esm/expression/transform/utils/dimToZeroBase.js +16 -0
  84. package/lib/esm/expression/transform/utils/lastDimToZeroBase.js +4 -6
  85. package/lib/esm/function/algebra/decomposition/slu.js +1 -1
  86. package/lib/esm/function/algebra/derivative.js +15 -15
  87. package/lib/esm/function/algebra/lyap.js +4 -4
  88. package/lib/esm/function/algebra/simplify/util.js +3 -3
  89. package/lib/esm/function/algebra/simplifyConstant.js +9 -9
  90. package/lib/esm/function/algebra/solver/lsolve.js +3 -3
  91. package/lib/esm/function/algebra/solver/lsolveAll.js +3 -3
  92. package/lib/esm/function/algebra/solver/lusolve.js +5 -5
  93. package/lib/esm/function/algebra/solver/usolve.js +3 -3
  94. package/lib/esm/function/algebra/solver/usolveAll.js +3 -3
  95. package/lib/esm/function/algebra/sylvester.js +7 -7
  96. package/lib/esm/function/arithmetic/addScalar.js +4 -4
  97. package/lib/esm/function/arithmetic/ceil.js +6 -6
  98. package/lib/esm/function/arithmetic/divide.js +5 -5
  99. package/lib/esm/function/arithmetic/divideScalar.js +5 -5
  100. package/lib/esm/function/arithmetic/fix.js +5 -5
  101. package/lib/esm/function/arithmetic/floor.js +6 -6
  102. package/lib/esm/function/arithmetic/hypot.js +3 -3
  103. package/lib/esm/function/arithmetic/mod.js +3 -3
  104. package/lib/esm/function/arithmetic/multiply.js +7 -7
  105. package/lib/esm/function/arithmetic/multiplyScalar.js +4 -4
  106. package/lib/esm/function/arithmetic/norm.js +2 -2
  107. package/lib/esm/function/arithmetic/pow.js +6 -6
  108. package/lib/esm/function/arithmetic/round.js +7 -7
  109. package/lib/esm/function/arithmetic/subtractScalar.js +4 -4
  110. package/lib/esm/function/arithmetic/xgcd.js +1 -1
  111. package/lib/esm/function/combinatorics/bellNumbers.js +1 -1
  112. package/lib/esm/function/combinatorics/catalan.js +1 -1
  113. package/lib/esm/function/combinatorics/composition.js +1 -1
  114. package/lib/esm/function/combinatorics/stirlingS2.js +1 -1
  115. package/lib/esm/function/geometry/distance.js +4 -4
  116. package/lib/esm/function/geometry/intersect.js +2 -2
  117. package/lib/esm/function/logical/and.js +2 -2
  118. package/lib/esm/function/logical/or.js +2 -2
  119. package/lib/esm/function/logical/xor.js +2 -2
  120. package/lib/esm/function/matrix/apply.js +2 -2
  121. package/lib/esm/function/matrix/column.js +1 -1
  122. package/lib/esm/function/matrix/concat.js +1 -1
  123. package/lib/esm/function/matrix/count.js +1 -1
  124. package/lib/esm/function/matrix/cross.js +3 -3
  125. package/lib/esm/function/matrix/diag.js +10 -10
  126. package/lib/esm/function/matrix/diff.js +2 -2
  127. package/lib/esm/function/matrix/eigs.js +3 -3
  128. package/lib/esm/function/matrix/fft.js +3 -3
  129. package/lib/esm/function/matrix/filter.js +4 -4
  130. package/lib/esm/function/matrix/flatten.js +5 -6
  131. package/lib/esm/function/matrix/forEach.js +4 -4
  132. package/lib/esm/function/matrix/identity.js +6 -6
  133. package/lib/esm/function/matrix/ifft.js +3 -3
  134. package/lib/esm/function/matrix/inv.js +1 -1
  135. package/lib/esm/function/matrix/kron.js +7 -7
  136. package/lib/esm/function/matrix/map.js +110 -19
  137. package/lib/esm/function/matrix/matrixFromFunction.js +6 -6
  138. package/lib/esm/function/matrix/ones.js +2 -2
  139. package/lib/esm/function/matrix/partitionSelect.js +2 -2
  140. package/lib/esm/function/matrix/pinv.js +1 -1
  141. package/lib/esm/function/matrix/range.js +10 -10
  142. package/lib/esm/function/matrix/reshape.js +2 -2
  143. package/lib/esm/function/matrix/rotate.js +4 -4
  144. package/lib/esm/function/matrix/rotationMatrix.js +6 -6
  145. package/lib/esm/function/matrix/row.js +1 -1
  146. package/lib/esm/function/matrix/size.js +8 -8
  147. package/lib/esm/function/matrix/sort.js +4 -4
  148. package/lib/esm/function/matrix/sqrtm.js +1 -1
  149. package/lib/esm/function/matrix/squeeze.js +3 -4
  150. package/lib/esm/function/matrix/subset.js +2 -2
  151. package/lib/esm/function/matrix/zeros.js +2 -2
  152. package/lib/esm/function/probability/combinations.js +1 -1
  153. package/lib/esm/function/probability/combinationsWithRep.js +2 -2
  154. package/lib/esm/function/probability/kldivergence.js +4 -4
  155. package/lib/esm/function/probability/multinomial.js +1 -1
  156. package/lib/esm/function/probability/permutations.js +2 -2
  157. package/lib/esm/function/probability/pickRandom.js +6 -6
  158. package/lib/esm/function/probability/random.js +1 -1
  159. package/lib/esm/function/probability/randomInt.js +1 -1
  160. package/lib/esm/function/relational/compare.js +6 -6
  161. package/lib/esm/function/relational/deepEqual.js +1 -1
  162. package/lib/esm/function/relational/equal.js +1 -1
  163. package/lib/esm/function/relational/equalScalar.js +7 -7
  164. package/lib/esm/function/relational/equalText.js +1 -1
  165. package/lib/esm/function/relational/larger.js +3 -3
  166. package/lib/esm/function/relational/largerEq.js +4 -4
  167. package/lib/esm/function/relational/smaller.js +3 -3
  168. package/lib/esm/function/relational/smallerEq.js +3 -3
  169. package/lib/esm/function/relational/unequal.js +1 -1
  170. package/lib/esm/function/set/setCartesian.js +1 -1
  171. package/lib/esm/function/set/setDifference.js +1 -1
  172. package/lib/esm/function/set/setDistinct.js +1 -1
  173. package/lib/esm/function/set/setIntersect.js +1 -1
  174. package/lib/esm/function/set/setIsSubset.js +1 -1
  175. package/lib/esm/function/set/setMultiplicity.js +1 -1
  176. package/lib/esm/function/set/setPowerset.js +1 -1
  177. package/lib/esm/function/set/setSize.js +2 -2
  178. package/lib/esm/function/set/setSymDifference.js +1 -1
  179. package/lib/esm/function/set/setUnion.js +1 -1
  180. package/lib/esm/function/signal/freqz.js +6 -6
  181. package/lib/esm/function/statistics/corr.js +2 -2
  182. package/lib/esm/function/statistics/cumsum.js +3 -3
  183. package/lib/esm/function/statistics/max.js +1 -1
  184. package/lib/esm/function/statistics/median.js +3 -3
  185. package/lib/esm/function/statistics/min.js +1 -1
  186. package/lib/esm/function/statistics/prod.js +1 -1
  187. package/lib/esm/function/statistics/variance.js +2 -2
  188. package/lib/esm/function/string/bin.js +2 -2
  189. package/lib/esm/function/string/hex.js +2 -2
  190. package/lib/esm/function/string/oct.js +2 -2
  191. package/lib/esm/function/trigonometry/acoth.js +2 -2
  192. package/lib/esm/function/trigonometry/acsch.js +2 -2
  193. package/lib/esm/type/bigint.js +1 -1
  194. package/lib/esm/type/complex/function/complex.js +2 -2
  195. package/lib/esm/type/fraction/function/fraction.js +1 -1
  196. package/lib/esm/type/matrix/DenseMatrix.js +13 -38
  197. package/lib/esm/type/matrix/SparseMatrix.js +5 -8
  198. package/lib/esm/type/matrix/function/index.js +1 -1
  199. package/lib/esm/type/matrix/function/matrix.js +1 -1
  200. package/lib/esm/type/matrix/function/sparse.js +2 -2
  201. package/lib/esm/type/number.js +1 -1
  202. package/lib/esm/type/unit/function/createUnit.js +3 -3
  203. package/lib/esm/type/unit/function/splitUnit.js +1 -1
  204. package/lib/esm/type/unit/function/unit.js +2 -2
  205. package/lib/esm/utils/array.js +26 -0
  206. package/lib/esm/utils/collection.js +1 -1
  207. package/lib/esm/utils/customs.js +5 -12
  208. package/lib/esm/utils/function.js +0 -13
  209. package/lib/esm/utils/is.js +24 -0
  210. package/lib/esm/utils/map.js +7 -22
  211. package/lib/esm/version.js +1 -1
  212. package/package.json +13 -13
  213. package/types/index.d.ts +57 -18
@@ -21,11 +21,11 @@ const createHypot = exports.createHypot = /* #__PURE__ */(0, _factory.factory)(n
21
21
  isPositive
22
22
  } = _ref;
23
23
  /**
24
- * Calculate the hypotenusa of a list with values. The hypotenusa is defined as:
24
+ * Calculate the hypotenuse of a list with values. The hypotenuse is defined as:
25
25
  *
26
26
  * hypot(a, b, c, ...) = sqrt(a^2 + b^2 + c^2 + ...)
27
27
  *
28
- * For matrix input, the hypotenusa is calculated for all values in the matrix.
28
+ * For matrix input, the hypotenuse is calculated for all values in the matrix.
29
29
  *
30
30
  * Syntax:
31
31
  *
@@ -55,7 +55,7 @@ const createHypot = exports.createHypot = /* #__PURE__ */(0, _factory.factory)(n
55
55
  });
56
56
 
57
57
  /**
58
- * Calculate the hypotenusa for an Array with values
58
+ * Calculate the hypotenuse for an Array with values
59
59
  * @param {Array.<number | BigNumber>} args
60
60
  * @return {number | BigNumber} Returns the result
61
61
  * @private
@@ -57,7 +57,7 @@ const createApply = exports.createApply = /* #__PURE__ */(0, _factory.factory)(n
57
57
  throw new _IndexError.IndexError(dim, size.length);
58
58
  }
59
59
  if ((0, _is.isMatrix)(mat)) {
60
- return mat.create(_apply(mat.valueOf(), dim, callback));
60
+ return mat.create(_apply(mat.valueOf(), dim, callback), mat.datatype());
61
61
  } else {
62
62
  return _apply(mat, dim, callback);
63
63
  }
@@ -25,7 +25,7 @@ const createFft = exports.createFft = /* #__PURE__ */(0, _factory.factory)(name,
25
25
  log2
26
26
  } = _ref;
27
27
  /**
28
- * Calculate N-dimensional fourier transform
28
+ * Calculate N-dimensional Fourier transform
29
29
  *
30
30
  * Syntax:
31
31
  *
@@ -41,12 +41,12 @@ const createFft = exports.createFft = /* #__PURE__ */(0, _factory.factory)(name,
41
41
  * ifft
42
42
  *
43
43
  * @param {Array | Matrix} arr An array or matrix
44
- * @return {Array | Matrix} N-dimensional fourier transformation of the array
44
+ * @return {Array | Matrix} N-dimensional Fourier transformation of the array
45
45
  */
46
46
  return typed(name, {
47
47
  Array: _ndFft,
48
48
  Matrix: function (matrix) {
49
- return matrix.create(_ndFft(matrix.toArray()));
49
+ return matrix.create(_ndFft(matrix.valueOf()), matrix.datatype());
50
50
  }
51
51
  });
52
52
 
@@ -45,11 +45,11 @@ const createFilter = exports.createFilter = /* #__PURE__ */(0, _factory.factory)
45
45
  return typed('filter', {
46
46
  'Array, function': _filterCallback,
47
47
  'Matrix, function': function (x, test) {
48
- return x.create(_filterCallback(x.toArray(), test));
48
+ return x.create(_filterCallback(x.valueOf(), test), x.datatype());
49
49
  },
50
50
  'Array, RegExp': _array.filterRegExp,
51
51
  'Matrix, RegExp': function (x, test) {
52
- return x.create((0, _array.filterRegExp)(x.toArray(), test));
52
+ return x.create((0, _array.filterRegExp)(x.valueOf(), test), x.datatype());
53
53
  }
54
54
  });
55
55
  });
@@ -7,11 +7,10 @@ exports.createFlatten = void 0;
7
7
  var _array = require("../../utils/array.js");
8
8
  var _factory = require("../../utils/factory.js");
9
9
  const name = 'flatten';
10
- const dependencies = ['typed', 'matrix'];
10
+ const dependencies = ['typed'];
11
11
  const createFlatten = exports.createFlatten = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
12
12
  let {
13
- typed,
14
- matrix
13
+ typed
15
14
  } = _ref;
16
15
  /**
17
16
  * Flatten a multidimensional matrix into a single dimensional matrix.
@@ -37,9 +36,9 @@ const createFlatten = exports.createFlatten = /* #__PURE__ */(0, _factory.factor
37
36
  return (0, _array.flatten)(x);
38
37
  },
39
38
  Matrix: function (x) {
40
- const flat = (0, _array.flatten)(x.toArray());
41
- // TODO: return the same matrix type as x (Dense or Sparse Matrix)
42
- return matrix(flat);
39
+ // Return the same matrix type as x (Dense or Sparse Matrix)
40
+ // Return the same data type as x
41
+ return x.create((0, _array.flatten)(x.toArray()), x.datatype());
43
42
  }
44
43
  });
45
44
  });
@@ -17,7 +17,7 @@ const createIfft = exports.createIfft = /* #__PURE__ */(0, _factory.factory)(nam
17
17
  conj
18
18
  } = _ref;
19
19
  /**
20
- * Calculate N-dimensional inverse fourier transform
20
+ * Calculate N-dimensional inverse Fourier transform
21
21
  *
22
22
  * Syntax:
23
23
  *
@@ -32,7 +32,7 @@ const createIfft = exports.createIfft = /* #__PURE__ */(0, _factory.factory)(nam
32
32
  * fft
33
33
  *
34
34
  * @param {Array | Matrix} arr An array or matrix
35
- * @return {Array | Matrix} N-dimensional fourier transformation of the array
35
+ * @return {Array | Matrix} N-dimensional Fourier transformation of the array
36
36
  */
37
37
  return typed(name, {
38
38
  'Array | Matrix': function (arr) {
@@ -15,7 +15,7 @@ const createKron = exports.createKron = /* #__PURE__ */(0, _factory.factory)(nam
15
15
  multiplyScalar
16
16
  } = _ref;
17
17
  /**
18
- * Calculates the kronecker product of 2 matrices or vectors.
18
+ * Calculates the Kronecker product of 2 matrices or vectors.
19
19
  *
20
20
  * NOTE: If a one dimensional vector / matrix is given, it will be
21
21
  * wrapped so its two dimensions.
@@ -39,7 +39,7 @@ const createKron = exports.createKron = /* #__PURE__ */(0, _factory.factory)(nam
39
39
  *
40
40
  * @param {Array | Matrix} x First vector
41
41
  * @param {Array | Matrix} y Second vector
42
- * @return {Array | Matrix} Returns the kronecker product of `x` and `y`
42
+ * @return {Array | Matrix} Returns the Kronecker product of `x` and `y`
43
43
  */
44
44
  return typed(name, {
45
45
  'Matrix, Matrix': function (x, y) {
@@ -55,10 +55,10 @@ const createKron = exports.createKron = /* #__PURE__ */(0, _factory.factory)(nam
55
55
  });
56
56
 
57
57
  /**
58
- * Calculate the kronecker product of two matrices / vectors
58
+ * Calculate the Kronecker product of two matrices / vectors
59
59
  * @param {Array} a First vector
60
60
  * @param {Array} b Second vector
61
- * @returns {Array} Returns the kronecker product of x and y
61
+ * @returns {Array} Returns the Kronecker product of x and y
62
62
  * @private
63
63
  */
64
64
  function _kron(a, b) {
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createMap = void 0;
7
7
  var _applyCallback = require("../../utils/applyCallback.js");
8
+ var _array = require("../../utils/array.js");
8
9
  var _factory = require("../../utils/factory.js");
9
10
  const name = 'map';
10
11
  const dependencies = ['typed'];
@@ -16,9 +17,12 @@ const createMap = exports.createMap = /* #__PURE__ */(0, _factory.factory)(name,
16
17
  * Create a new matrix or array with the results of a callback function executed on
17
18
  * each entry of a given matrix/array.
18
19
  *
19
- * For each entry of the input, the callback is invoked with three arguments:
20
- * the value of the entry, the index at which that entry occurs, and the full
21
- * matrix/array being traversed. Note that because the matrix/array might be
20
+ * For each entry of the input,
21
+ *
22
+ * the callback is invoked with 2N + 1 arguments:
23
+ * the N values of the entry, the index at which that entry occurs, and the N full
24
+ * broadcasted matrix/array being traversed where N is the number of matrices being traversed.
25
+ * Note that because the matrix/array might be
22
26
  * multidimensional, the "index" argument is always an array of numbers giving
23
27
  * the index in each dimension. This is true even for vectors: the "index"
24
28
  * argument is an array of length 1, rather than simply a number.
@@ -26,17 +30,23 @@ const createMap = exports.createMap = /* #__PURE__ */(0, _factory.factory)(name,
26
30
  * Syntax:
27
31
  *
28
32
  * math.map(x, callback)
33
+ * math.map(x, y, ..., callback)
29
34
  *
30
35
  * Examples:
31
36
  *
32
37
  * math.map([1, 2, 3], function(value) {
33
38
  * return value * value
34
39
  * }) // returns [1, 4, 9]
40
+ * math.map([1, 2], [3, 4], function(a, b) {
41
+ * return a + b
42
+ * }) // returns [4, 6]
35
43
  *
36
44
  * // The callback is normally called with three arguments:
37
45
  * // callback(value, index, Array)
38
46
  * // If you want to call with only one argument, use:
39
47
  * math.map([1, 2, 3], x => math.format(x)) // returns ['1', '2', '3']
48
+ * // It can also be called with 2N + 1 arguments: for N arrays
49
+ * // callback(value1, value2, index, BroadcastedArray1, BroadcastedArray2)
40
50
  *
41
51
  * See also:
42
52
  *
@@ -49,11 +59,82 @@ const createMap = exports.createMap = /* #__PURE__ */(0, _factory.factory)(name,
49
59
  * Transformed map of x; always has the same type and shape as x
50
60
  */
51
61
  return typed(name, {
52
- 'Array, function': _map,
62
+ 'Array, function': _mapArray,
53
63
  'Matrix, function': function (x, callback) {
54
64
  return x.map(callback);
55
- }
65
+ },
66
+ 'Array|Matrix, Array|Matrix, ...Array|Matrix|function': (A, B, rest) => _mapMultiple([A, B, ...rest.slice(0, rest.length - 1)], rest[rest.length - 1])
56
67
  });
68
+
69
+ /**
70
+ * Maps over multiple arrays or matrices.
71
+ *
72
+ * @param {Array<Array|Matrix>} Arrays - An array of arrays or matrices to map over.
73
+ * @param {function} multiCallback - The callback function to apply to each element.
74
+ * @throws {Error} If the last argument is not a callback function.
75
+ * @returns {Array|Matrix} A new array or matrix with each element being the result of the callback function.
76
+ *
77
+ * @example
78
+ * _mapMultiple([[1, 2, 3], [4, 5, 6]], (a, b) => a + b); // Returns [5, 7, 9]
79
+ */
80
+ function _mapMultiple(Arrays, multiCallback) {
81
+ if (typeof multiCallback !== 'function') {
82
+ throw new Error('Last argument must be a callback function');
83
+ }
84
+ const firstArrayIsMatrix = Arrays[0].isMatrix;
85
+ const newSize = (0, _array.broadcastSizes)(...Arrays.map(M => M.isMatrix ? M.size() : (0, _array.arraySize)(M)));
86
+ const _get = firstArrayIsMatrix ? (matrix, idx) => matrix.get(idx) : _array.get;
87
+ const broadcastedArrays = firstArrayIsMatrix ? Arrays.map(M => M.isMatrix ? M.create((0, _array.broadcastTo)(M.toArray(), newSize), M.datatype()) : Arrays[0].create((0, _array.broadcastTo)(M.valueOf(), newSize))) : Arrays.map(M => M.isMatrix ? (0, _array.broadcastTo)(M.toArray(), newSize) : (0, _array.broadcastTo)(M, newSize));
88
+ let callback;
89
+ if (typed.isTypedFunction(multiCallback)) {
90
+ const firstIndex = newSize.map(() => 0);
91
+ const firstValues = broadcastedArrays.map(array => _get(array, firstIndex));
92
+ const callbackCase = _getTypedCallbackCase(multiCallback, firstValues, firstIndex, broadcastedArrays);
93
+ callback = _getLimitedCallback(callbackCase);
94
+ } else {
95
+ const numberOfArrays = Arrays.length;
96
+ const callbackCase = _getCallbackCase(multiCallback, numberOfArrays);
97
+ callback = _getLimitedCallback(callbackCase);
98
+ }
99
+ const broadcastedArraysCallback = (x, idx) => callback([x, ...broadcastedArrays.slice(1).map(Array => _get(Array, idx))], idx);
100
+ if (firstArrayIsMatrix) {
101
+ return broadcastedArrays[0].map(broadcastedArraysCallback);
102
+ } else {
103
+ return _mapArray(broadcastedArrays[0], broadcastedArraysCallback);
104
+ }
105
+ function _getLimitedCallback(callbackCase) {
106
+ switch (callbackCase) {
107
+ case 0:
108
+ return x => multiCallback(...x);
109
+ case 1:
110
+ return (x, idx) => multiCallback(...x, idx);
111
+ case 2:
112
+ return (x, idx) => multiCallback(...x, idx, ...broadcastedArrays);
113
+ }
114
+ }
115
+ function _getCallbackCase(callback, numberOfArrays) {
116
+ if (callback.length > numberOfArrays + 1) {
117
+ return 2;
118
+ }
119
+ if (callback.length === numberOfArrays + 1) {
120
+ return 1;
121
+ }
122
+ return 0;
123
+ }
124
+ function _getTypedCallbackCase(callback, values, idx, arrays) {
125
+ if (typed.resolve(callback, [...values, idx, ...arrays]) !== null) {
126
+ return 2;
127
+ }
128
+ if (typed.resolve(callback, [...values, idx]) !== null) {
129
+ return 1;
130
+ }
131
+ if (typed.resolve(callback, values) !== null) {
132
+ return 0;
133
+ }
134
+ // this should never happen
135
+ return 0;
136
+ }
137
+ }
57
138
  });
58
139
 
59
140
  /**
@@ -63,17 +144,27 @@ const createMap = exports.createMap = /* #__PURE__ */(0, _factory.factory)(name,
63
144
  * @return {Array}
64
145
  * @private
65
146
  */
66
- function _map(array, callback) {
67
- const recurse = function (value, index) {
68
- if (Array.isArray(value)) {
69
- return value.map(function (child, i) {
70
- // we create a copy of the index array and append the new index value
71
- return recurse(child, index.concat(i));
72
- });
73
- } else {
74
- // invoke the callback function with the right number of arguments
75
- return (0, _applyCallback.applyCallback)(callback, value, index, array, 'map');
76
- }
77
- };
78
- return recurse(array, []);
147
+ function _mapArray(array, callback) {
148
+ return _recurse(array, [], array, callback);
149
+ }
150
+
151
+ /**
152
+ * Recursive function to map a multi-dimensional array.
153
+ *
154
+ * @param {*} value - The current value being processed in the array.
155
+ * @param {Array} index - The index of the current value being processed in the array.
156
+ * @param {Array} array - The array being processed.
157
+ * @param {Function} callback - Function that produces the element of the new Array, taking three arguments: the value of the element, the index of the element, and the Array being processed.
158
+ * @returns {*} The new array with each element being the result of the callback function.
159
+ */
160
+ function _recurse(value, index, array, callback) {
161
+ if (Array.isArray(value)) {
162
+ return value.map(function (child, i) {
163
+ // we create a copy of the index array and append the new index value
164
+ return _recurse(child, index.concat(i), array, callback);
165
+ });
166
+ } else {
167
+ // invoke the callback function with the right number of arguments
168
+ return (0, _applyCallback.applyCallback)(callback, value, index, array, 'map');
169
+ }
79
170
  }
@@ -24,12 +24,12 @@ const createSize = exports.createSize = /* #__PURE__ */(0, _factory.factory)(nam
24
24
  *
25
25
  * Examples:
26
26
  *
27
- * math.size(2.3) // returns []
28
- * math.size('hello world') // returns [11]
27
+ * math.size(2.3) // returns []
28
+ * math.size('hello world') // returns [11]
29
29
  *
30
30
  * const A = [[1, 2, 3], [4, 5, 6]]
31
- * math.size(A) // returns [2, 3]
32
- * math.size(math.range(1,6)) // returns [5]
31
+ * math.size(A) // returns [2, 3]
32
+ * math.size(math.range(1,6).toArray()) // returns [5]
33
33
  *
34
34
  * See also:
35
35
  *
@@ -40,15 +40,15 @@ const createSize = exports.createSize = /* #__PURE__ */(0, _factory.factory)(nam
40
40
  */
41
41
  return typed(name, {
42
42
  Matrix: function (x) {
43
- return x.create(x.size());
43
+ return x.create(x.size(), 'number');
44
44
  },
45
45
  Array: _array.arraySize,
46
46
  string: function (x) {
47
- return config.matrix === 'Array' ? [x.length] : matrix([x.length]);
47
+ return config.matrix === 'Array' ? [x.length] : matrix([x.length], 'dense', 'number');
48
48
  },
49
49
  'number | Complex | BigNumber | Unit | boolean | null': function (x) {
50
50
  // scalar
51
- return config.matrix === 'Array' ? [] : matrix ? matrix([]) : (0, _noop.noMatrix)();
51
+ return config.matrix === 'Array' ? [] : matrix ? matrix([], 'dense', 'number') : (0, _noop.noMatrix)();
52
52
  }
53
53
  });
54
54
  });
@@ -8,11 +8,10 @@ var _object = require("../../utils/object.js");
8
8
  var _array = require("../../utils/array.js");
9
9
  var _factory = require("../../utils/factory.js");
10
10
  const name = 'squeeze';
11
- const dependencies = ['typed', 'matrix'];
11
+ const dependencies = ['typed'];
12
12
  const createSqueeze = exports.createSqueeze = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
13
13
  let {
14
- typed,
15
- matrix
14
+ typed
16
15
  } = _ref;
17
16
  /**
18
17
  * Squeeze a matrix, remove inner and outer singleton dimensions from a matrix.
@@ -50,7 +49,7 @@ const createSqueeze = exports.createSqueeze = /* #__PURE__ */(0, _factory.factor
50
49
  Matrix: function (x) {
51
50
  const res = (0, _array.squeeze)(x.toArray());
52
51
  // FIXME: return the same type of matrix as the input
53
- return Array.isArray(res) ? matrix(res) : res;
52
+ return Array.isArray(res) ? x.create(res, x.datatype()) : res;
54
53
  },
55
54
  any: function (x) {
56
55
  // scalar
@@ -66,7 +66,7 @@ const createRandom = exports.createRandom = /* #__PURE__ */(0, _factory.factory)
66
66
  });
67
67
  function _randomMatrix(size, min, max) {
68
68
  const res = (0, _randomMatrix2.randomMatrix)(size.valueOf(), () => _random(min, max));
69
- return (0, _is.isMatrix)(size) ? size.create(res) : res;
69
+ return (0, _is.isMatrix)(size) ? size.create(res, 'number') : res;
70
70
  }
71
71
  function _random(min, max) {
72
72
  return min + rng() * (max - min);
@@ -65,7 +65,7 @@ const createRandomInt = exports.createRandomInt = /* #__PURE__ */(0, _factory.fa
65
65
  });
66
66
  function _randomIntMatrix(size, min, max) {
67
67
  const res = (0, _randomMatrix.randomMatrix)(size.valueOf(), () => _randomInt(min, max));
68
- return (0, _is.isMatrix)(size) ? size.create(res) : res;
68
+ return (0, _is.isMatrix)(size) ? size.create(res, 'number') : res;
69
69
  }
70
70
  function _randomInt(min, max) {
71
71
  return Math.floor(min + rng() * (max - min));
@@ -48,12 +48,12 @@ const createCumSum = exports.createCumSum = /* #__PURE__ */(0, _factory.factory)
48
48
  // sum([a, b, c, d, ...])
49
49
  Array: _cumsum,
50
50
  Matrix: function (matrix) {
51
- return matrix.create(_cumsum(matrix.valueOf()));
51
+ return matrix.create(_cumsum(matrix.valueOf(), matrix.datatype()));
52
52
  },
53
53
  // sum([a, b, c, d, ...], dim)
54
54
  'Array, number | BigNumber': _ncumSumDim,
55
55
  'Matrix, number | BigNumber': function (matrix, dim) {
56
- return matrix.create(_ncumSumDim(matrix.valueOf(), dim));
56
+ return matrix.create(_ncumSumDim(matrix.valueOf(), dim), matrix.datatype());
57
57
  },
58
58
  // cumsum(a, b, c, d, ...)
59
59
  '...': function (args) {
@@ -16,10 +16,10 @@ const createAcoth = exports.createAcoth = /* #__PURE__ */(0, _factory.factory)(n
16
16
  BigNumber
17
17
  } = _ref;
18
18
  /**
19
- * Calculate the hyperbolic arccotangent of a value,
19
+ * Calculate the inverse hyperbolic tangent of a value,
20
20
  * defined as `acoth(x) = atanh(1/x) = (ln((x+1)/x) + ln(x/(x-1))) / 2`.
21
21
  *
22
- * To avoid confusion with the matrix hyperbolic arccotangent, this
22
+ * To avoid confusion with the matrix inverse hyperbolic tangent, this
23
23
  * function does not apply to matrices.
24
24
  *
25
25
  * Syntax:
@@ -14,10 +14,10 @@ const createAcsch = exports.createAcsch = /* #__PURE__ */(0, _factory.factory)(n
14
14
  BigNumber
15
15
  } = _ref;
16
16
  /**
17
- * Calculate the hyperbolic arccosecant of a value,
17
+ * Calculate the inverse hyperbolic cosecant of a value,
18
18
  * defined as `acsch(x) = asinh(1/x) = ln(1/x + sqrt(1/x^2 + 1))`.
19
19
  *
20
- * To avoid confusion with the matrix hyperbolic arccosecant, this function
20
+ * To avoid confusion with the matrix inverse hyperbolic cosecant, this function
21
21
  * does not apply to matrices.
22
22
  *
23
23
  * Syntax:
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 13.0.3
10
- * @date 2024-07-19
9
+ * @version 13.1.1
10
+ * @date 2024-08-27
11
11
  *
12
12
  * @license
13
13
  * Copyright (C) 2013-2024 Jos de Jong <wjosdejong@gmail.com>
@@ -11,7 +11,7 @@ var _number = require("../../utils/number.js");
11
11
  var _object = require("../../utils/object.js");
12
12
  var _DimensionError = require("../../error/DimensionError.js");
13
13
  var _factory = require("../../utils/factory.js");
14
- var _function = require("../../utils/function.js");
14
+ var _applyCallback = require("../../utils/applyCallback.js");
15
15
  const name = 'DenseMatrix';
16
16
  const dependencies = ['Matrix'];
17
17
  const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
@@ -172,24 +172,7 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
172
172
  * @return {*} value
173
173
  */
174
174
  DenseMatrix.prototype.get = function (index) {
175
- if (!(0, _is.isArray)(index)) {
176
- throw new TypeError('Array expected');
177
- }
178
- if (index.length !== this._size.length) {
179
- throw new _DimensionError.DimensionError(index.length, this._size.length);
180
- }
181
-
182
- // check index
183
- for (let x = 0; x < index.length; x++) {
184
- (0, _array.validateIndex)(index[x], this._size[x]);
185
- }
186
- let data = this._data;
187
- for (let i = 0, ii = index.length; i < ii; i++) {
188
- const indexI = index[i];
189
- (0, _array.validateIndex)(indexI, data.length);
190
- data = data[indexI];
191
- }
192
- return data;
175
+ return (0, _array.get)(this._data, index);
193
176
  };
194
177
 
195
178
  /**
@@ -552,7 +535,6 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
552
535
  DenseMatrix.prototype.map = function (callback) {
553
536
  // matrix instance
554
537
  const me = this;
555
- const args = (0, _function.maxArgumentCount)(callback);
556
538
  const recurse = function (value, index) {
557
539
  if ((0, _is.isArray)(value)) {
558
540
  return value.map(function (child, i) {
@@ -560,14 +542,7 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
560
542
  });
561
543
  } else {
562
544
  // invoke the callback function with the right number of arguments
563
- if (args === 1) {
564
- return callback(value);
565
- } else if (args === 2) {
566
- return callback(value, index);
567
- } else {
568
- // 3 or -1
569
- return callback(value, index, me);
570
- }
545
+ return (0, _applyCallback.applyCallback)(callback, value, index, me, 'map');
571
546
  }
572
547
  };
573
548
 
@@ -11,7 +11,7 @@ var _object = require("../../utils/object.js");
11
11
  var _array = require("../../utils/array.js");
12
12
  var _factory = require("../../utils/factory.js");
13
13
  var _DimensionError = require("../../error/DimensionError.js");
14
- var _function = require("../../utils/function.js");
14
+ var _applyCallback = require("../../utils/applyCallback.js");
15
15
  const name = 'SparseMatrix';
16
16
  const dependencies = ['typed', 'equalScalar', 'Matrix'];
17
17
  const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
@@ -879,12 +879,9 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
879
879
  const rows = this._size[0];
880
880
  const columns = this._size[1];
881
881
  // invoke callback
882
- const args = (0, _function.maxArgumentCount)(callback);
883
882
  const invoke = function (v, i, j) {
884
883
  // invoke callback
885
- if (args === 1) return callback(v);
886
- if (args === 2) return callback(v, [i, j]);
887
- return callback(v, [i, j], me);
884
+ return (0, _applyCallback.applyCallback)(callback, v, [i, j], me, 'map');
888
885
  };
889
886
  // invoke _map
890
887
  return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros);
@@ -914,11 +911,11 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
914
911
  // invoke callback
915
912
  const invoke = function (v, x, y) {
916
913
  // invoke callback
917
- v = callback(v, x, y);
914
+ const value = callback(v, x, y);
918
915
  // check value != 0
919
- if (!eq(v, zero)) {
916
+ if (!eq(value, zero)) {
920
917
  // store value
921
- values.push(v);
918
+ values.push(value);
922
919
  // index
923
920
  index.push(x);
924
921
  }
@@ -16,6 +16,7 @@ exports.filterRegExp = filterRegExp;
16
16
  exports.flatten = flatten;
17
17
  exports.forEach = forEach;
18
18
  exports.generalize = generalize;
19
+ exports.get = get;
19
20
  exports.getArrayDataType = getArrayDataType;
20
21
  exports.identify = identify;
21
22
  exports.initial = initial;
@@ -813,6 +814,32 @@ function stretch(arrayToStretch, sizeToStretch, dimToStretch) {
813
814
  return concat(...Array(sizeToStretch).fill(arrayToStretch), dimToStretch);
814
815
  }
815
816
 
817
+ /**
818
+ * Retrieves a single element from an array given an index.
819
+ *
820
+ * @param {Array} array - The array from which to retrieve the value.
821
+ * @param {Array<number>} idx - An array of indices specifying the position of the desired element in each dimension.
822
+ * @returns {*} - The value at the specified position in the array.
823
+ *
824
+ * @example
825
+ * const arr = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
826
+ * const index = [1, 0, 1];
827
+ * console.log(getValue(arr, index)); // 6
828
+ */
829
+ function get(array, index) {
830
+ if (!Array.isArray(array)) {
831
+ throw new Error('Array expected');
832
+ }
833
+ const size = arraySize(array);
834
+ if (index.length !== size.length) {
835
+ throw new _DimensionError.DimensionError(index.length, size.length);
836
+ }
837
+ for (let x = 0; x < index.length; x++) {
838
+ validateIndex(index[x], size[x]);
839
+ }
840
+ return index.reduce((acc, curr) => acc[curr], array);
841
+ }
842
+
816
843
  /**
817
844
  * Deep clones a multidimensional array
818
845
  * @param {Array} array
@@ -87,7 +87,7 @@ function reduce(mat, dim, callback) {
87
87
  throw new _IndexError.IndexError(dim, size.length);
88
88
  }
89
89
  if ((0, _is.isMatrix)(mat)) {
90
- return mat.create(_reduce(mat.valueOf(), dim, callback));
90
+ return mat.create(_reduce(mat.valueOf(), dim, callback), mat.datatype());
91
91
  } else {
92
92
  return _reduce(mat, dim, callback);
93
93
  }
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getSafeMethod = getSafeMethod;
7
- exports.getSafeProperties = getSafeProperties;
8
7
  exports.getSafeProperty = getSafeProperty;
9
- exports.hasSafeProperty = hasSafeProperty;
10
8
  exports.isPlainObject = isPlainObject;
11
9
  exports.isSafeMethod = isSafeMethod;
12
10
  exports.isSafeProperty = isSafeProperty;
@@ -22,7 +20,7 @@ var _object = require("./object.js");
22
20
  */
23
21
  function getSafeProperty(object, prop) {
24
22
  // only allow getting safe properties of a plain object
25
- if (isPlainObject(object) && isSafeProperty(object, prop)) {
23
+ if (isSafeProperty(object, prop)) {
26
24
  return object[prop];
27
25
  }
28
26
  if (typeof object[prop] === 'function' && isSafeMethod(object, prop)) {
@@ -43,27 +41,22 @@ function getSafeProperty(object, prop) {
43
41
  // TODO: merge this function into access.js?
44
42
  function setSafeProperty(object, prop, value) {
45
43
  // only allow setting safe properties of a plain object
46
- if (isPlainObject(object) && isSafeProperty(object, prop)) {
44
+ if (isSafeProperty(object, prop)) {
47
45
  object[prop] = value;
48
46
  return value;
49
47
  }
50
48
  throw new Error('No access to property "' + prop + '"');
51
49
  }
52
- function getSafeProperties(object) {
53
- return Object.keys(object).filter(prop => (0, _object.hasOwnProperty)(object, prop));
54
- }
55
- function hasSafeProperty(object, prop) {
56
- return prop in object;
57
- }
58
50
 
59
51
  /**
60
- * Test whether a property is safe to use for an object.
52
+ * Test whether a property is safe to use on an object or Array.
61
53
  * For example .toString and .constructor are not safe
54
+ * @param {Object | Array} object
62
55
  * @param {string} prop
63
56
  * @return {boolean} Returns true when safe
64
57
  */
65
58
  function isSafeProperty(object, prop) {
66
- if (!object || typeof object !== 'object') {
59
+ if (!isPlainObject(object) && !Array.isArray(object)) {
67
60
  return false;
68
61
  }
69
62
  // SAFE: whitelisted