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
@@ -248,36 +248,46 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
248
248
  }
249
249
 
250
250
  // retrieve submatrix
251
- // TODO: more efficient when creating an empty matrix and setting _data and _size manually
252
- return new DenseMatrix(_getSubmatrix(matrix._data, index, size.length, 0), matrix._datatype);
251
+ const returnMatrix = new DenseMatrix([]);
252
+ const submatrix = _getSubmatrix(matrix._data, index);
253
+ returnMatrix._size = submatrix.size;
254
+ returnMatrix._datatype = matrix._datatype;
255
+ returnMatrix._data = submatrix.data;
256
+ return returnMatrix;
253
257
  }
254
258
  }
255
259
 
256
260
  /**
257
- * Recursively get a submatrix of a multi dimensional matrix.
261
+ * Get a submatrix of a multi dimensional matrix.
258
262
  * Index is not checked for correct number or length of dimensions.
259
263
  * @memberof DenseMatrix
260
264
  * @param {Array} data
261
265
  * @param {Index} index
262
- * @param {number} dims Total number of dimensions
263
- * @param {number} dim Current dimension
264
266
  * @return {Array} submatrix
265
267
  * @private
266
268
  */
267
- function _getSubmatrix(data, index, dims, dim) {
268
- const last = dim === dims - 1;
269
- const range = index.dimension(dim);
270
- if (last) {
271
- return range.map(function (i) {
272
- (0, _array.validateIndex)(i, data.length);
273
- return data[i];
274
- }).valueOf();
275
- } else {
276
- return range.map(function (i) {
277
- (0, _array.validateIndex)(i, data.length);
278
- const child = data[i];
279
- return _getSubmatrix(child, index, dims, dim + 1);
280
- }).valueOf();
269
+ function _getSubmatrix(data, index) {
270
+ const maxDepth = index.size().length - 1;
271
+ const size = Array(maxDepth);
272
+ return {
273
+ data: getSubmatrixRecursive(data),
274
+ size
275
+ };
276
+ function getSubmatrixRecursive(data) {
277
+ let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
278
+ const ranges = index.dimension(depth);
279
+ size[depth] = ranges.size()[0];
280
+ if (depth < maxDepth) {
281
+ return ranges.map(rangeIndex => {
282
+ (0, _array.validateIndex)(rangeIndex, data.length);
283
+ return getSubmatrixRecursive(data[rangeIndex], depth + 1);
284
+ }).valueOf();
285
+ } else {
286
+ return ranges.map(rangeIndex => {
287
+ (0, _array.validateIndex)(rangeIndex, data.length);
288
+ return data[rangeIndex];
289
+ }).valueOf();
290
+ }
281
291
  }
282
292
  }
283
293
 
@@ -365,9 +375,7 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
365
375
  _fit(matrix, size, defaultValue);
366
376
 
367
377
  // insert the sub matrix
368
- const dims = iSize.length;
369
- const dim = 0;
370
- _setSubmatrix(matrix._data, index, submatrix, dims, dim);
378
+ _setSubmatrix(matrix._data, index, submatrix);
371
379
  }
372
380
  return matrix;
373
381
  }
@@ -378,23 +386,25 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
378
386
  * @param {Array} data
379
387
  * @param {Index} index
380
388
  * @param {Array} submatrix
381
- * @param {number} dims Total number of dimensions
382
- * @param {number} dim
383
389
  * @private
384
390
  */
385
- function _setSubmatrix(data, index, submatrix, dims, dim) {
386
- const last = dim === dims - 1;
387
- const range = index.dimension(dim);
388
- if (last) {
389
- range.forEach(function (dataIndex, subIndex) {
390
- (0, _array.validateIndex)(dataIndex);
391
- data[dataIndex] = submatrix[subIndex[0]];
392
- });
393
- } else {
394
- range.forEach(function (dataIndex, subIndex) {
395
- (0, _array.validateIndex)(dataIndex);
396
- _setSubmatrix(data[dataIndex], index, submatrix[subIndex[0]], dims, dim + 1);
397
- });
391
+ function _setSubmatrix(data, index, submatrix) {
392
+ const maxDepth = index.size().length - 1;
393
+ setSubmatrixRecursive(data, submatrix);
394
+ function setSubmatrixRecursive(data, submatrix) {
395
+ let depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
396
+ const range = index.dimension(depth);
397
+ if (depth < maxDepth) {
398
+ range.forEach((rangeIndex, i) => {
399
+ (0, _array.validateIndex)(rangeIndex, data.length);
400
+ setSubmatrixRecursive(data[rangeIndex], submatrix[i[0]], depth + 1);
401
+ });
402
+ } else {
403
+ range.forEach((rangeIndex, i) => {
404
+ (0, _array.validateIndex)(rangeIndex, data.length);
405
+ data[rangeIndex] = submatrix[i[0]];
406
+ });
407
+ }
398
408
  }
399
409
  }
400
410
 
@@ -525,62 +535,73 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
525
535
  };
526
536
 
527
537
  /**
528
- * Applies a callback function to a reference to each element of the matrix
538
+ * Create a new matrix with the results of the callback function executed on
539
+ * each entry of the matrix.
529
540
  * @memberof DenseMatrix
530
541
  * @param {Function} callback The callback function is invoked with three
531
- * parameters: an array, an integer index to that
532
- * array, and the Matrix being traversed.
542
+ * parameters: the value of the element, the index
543
+ * of the element, and the Matrix being traversed.
544
+ * @param {boolean} skipZeros If true, the callback function is invoked only for non-zero entries
545
+ * @param {boolean} isUnary If true, the callback function is invoked with one parameter
546
+ *
547
+ * @return {DenseMatrix} matrix
533
548
  */
534
- DenseMatrix.prototype._forEach = function (callback) {
549
+ DenseMatrix.prototype.map = function (callback) {
550
+ let skipZeros = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
551
+ let isUnary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
535
552
  const me = this;
536
- const s = me.size();
537
- const maxDepth = s.length - 1;
538
- if (maxDepth < 0) {
539
- return;
553
+ const maxDepth = me._size.length - 1;
554
+ if (maxDepth < 0) return me.clone();
555
+ const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me, 'map', isUnary);
556
+ const fastCallbackFn = fastCallback.fn;
557
+ const result = me.create(undefined, me._datatype);
558
+ result._size = me._size;
559
+ if (isUnary || fastCallback.isUnary) {
560
+ result._data = iterateUnary(me._data);
561
+ return result;
540
562
  }
541
563
  if (maxDepth === 0) {
542
- const thisSize = s[0];
543
- for (let i = 0; i < thisSize; i++) {
544
- callback(me._data, i, [i]);
564
+ const inputData = me.valueOf();
565
+ const data = Array(inputData.length);
566
+ for (let i = 0; i < inputData.length; i++) {
567
+ data[i] = fastCallbackFn(inputData[i], [i], me);
545
568
  }
546
- return;
569
+ result._data = data;
570
+ return result;
547
571
  }
548
- const index = Array(s.length);
549
- function recurse(data, depth) {
550
- const thisSize = s[depth];
572
+ const index = [];
573
+ result._data = iterate(me._data);
574
+ return result;
575
+ function iterate(data) {
576
+ let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
577
+ const result = Array(data.length);
551
578
  if (depth < maxDepth) {
552
- for (let i = 0; i < thisSize; i++) {
579
+ for (let i = 0; i < data.length; i++) {
553
580
  index[depth] = i;
554
- recurse(data[i], depth + 1);
581
+ result[i] = iterate(data[i], depth + 1);
555
582
  }
556
583
  } else {
557
- for (let i = 0; i < thisSize; i++) {
584
+ for (let i = 0; i < data.length; i++) {
558
585
  index[depth] = i;
559
- callback(data, i, index.slice());
586
+ result[i] = fastCallbackFn(data[i], index.slice(), me);
560
587
  }
561
588
  }
589
+ return result;
590
+ }
591
+ function iterateUnary(data) {
592
+ let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
593
+ const result = Array(data.length);
594
+ if (depth < maxDepth) {
595
+ for (let i = 0; i < data.length; i++) {
596
+ result[i] = iterateUnary(data[i], depth + 1);
597
+ }
598
+ } else {
599
+ for (let i = 0; i < data.length; i++) {
600
+ result[i] = fastCallbackFn(data[i]);
601
+ }
602
+ }
603
+ return result;
562
604
  }
563
- recurse(me._data, 0);
564
- };
565
-
566
- /**
567
- * Create a new matrix with the results of the callback function executed on
568
- * each entry of the matrix.
569
- * @memberof DenseMatrix
570
- * @param {Function} callback The callback function is invoked with three
571
- * parameters: the value of the element, the index
572
- * of the element, and the Matrix being traversed.
573
- *
574
- * @return {DenseMatrix} matrix
575
- */
576
- DenseMatrix.prototype.map = function (callback) {
577
- const me = this;
578
- const result = new DenseMatrix(me);
579
- const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me._data, 'map');
580
- result._forEach(function (arr, i, index) {
581
- arr[i] = fastCallback(arr[i], index, me);
582
- });
583
- return result;
584
605
  };
585
606
 
586
607
  /**
@@ -589,13 +610,55 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
589
610
  * @param {Function} callback The callback function is invoked with three
590
611
  * parameters: the value of the element, the index
591
612
  * of the element, and the Matrix being traversed.
613
+ * @param {boolean} skipZeros If true, the callback function is invoked only for non-zero entries
614
+ * @param {boolean} isUnary If true, the callback function is invoked with one parameter
592
615
  */
593
616
  DenseMatrix.prototype.forEach = function (callback) {
617
+ let skipZeros = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
618
+ let isUnary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
594
619
  const me = this;
595
- const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me._data, 'map');
596
- me._forEach(function (arr, i, index) {
597
- fastCallback(arr[i], index, me);
598
- });
620
+ const maxDepth = me._size.length - 1;
621
+ if (maxDepth < 0) return;
622
+ const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me, 'map', isUnary);
623
+ const fastCallbackFn = fastCallback.fn;
624
+ if (isUnary || fastCallback.isUnary) {
625
+ iterateUnary(me._data);
626
+ return;
627
+ }
628
+ if (maxDepth === 0) {
629
+ for (let i = 0; i < me._data.length; i++) {
630
+ fastCallbackFn(me._data[i], [i], me);
631
+ }
632
+ return;
633
+ }
634
+ const index = [];
635
+ iterate(me._data);
636
+ function iterate(data) {
637
+ let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
638
+ if (depth < maxDepth) {
639
+ for (let i = 0; i < data.length; i++) {
640
+ index[depth] = i;
641
+ iterate(data[i], depth + 1);
642
+ }
643
+ } else {
644
+ for (let i = 0; i < data.length; i++) {
645
+ index[depth] = i;
646
+ fastCallbackFn(data[i], index.slice(), me);
647
+ }
648
+ }
649
+ }
650
+ function iterateUnary(data) {
651
+ let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
652
+ if (depth < maxDepth) {
653
+ for (let i = 0; i < data.length; i++) {
654
+ iterateUnary(data[i], depth + 1);
655
+ }
656
+ } else {
657
+ for (let i = 0; i < data.length; i++) {
658
+ fastCallbackFn(data[i]);
659
+ }
660
+ }
661
+ }
599
662
  };
600
663
 
601
664
  /**
@@ -882,7 +882,7 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
882
882
  // invoke callback
883
883
  const invoke = function (v, i, j) {
884
884
  // invoke callback
885
- return fastCallback(v, [i, j], me);
885
+ return fastCallback.fn(v, [i, j], me);
886
886
  };
887
887
  // invoke _map
888
888
  return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros);
@@ -1000,7 +1000,8 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
1000
1000
  const i = this._index[k];
1001
1001
 
1002
1002
  // value @ k
1003
- fastCallback(this._values[k], [i, j], me);
1003
+ // TODO apply a non indexed version of algorithm in case fastCallback is not optimized
1004
+ fastCallback.fn(this._values[k], [i, j], me);
1004
1005
  }
1005
1006
  } else {
1006
1007
  // create a cache holding all defined values
@@ -1014,7 +1015,7 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
1014
1015
  // and either read the value or zero
1015
1016
  for (let i = 0; i < rows; i++) {
1016
1017
  const value = i in values ? values[i] : 0;
1017
- fastCallback(value, [i, j], me);
1018
+ fastCallback.fn(value, [i, j], me);
1018
1019
  }
1019
1020
  }
1020
1021
  }
@@ -624,8 +624,8 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
624
624
 
625
625
  // If at least one operand has a value, then the result should also have a value
626
626
  if (this.value !== null || other.value !== null) {
627
- const valThis = this.value === null ? this._normalize(1) : this.value;
628
- const valOther = other.value === null ? other._normalize(1) : other.value;
627
+ const valThis = this.value === null ? this._normalize(one(other.value)) : this.value;
628
+ const valOther = other.value === null ? other._normalize(one(this.value)) : other.value;
629
629
  res.value = multiplyScalar(valThis, valOther);
630
630
  } else {
631
631
  res.value = null;
@@ -673,8 +673,8 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
673
673
 
674
674
  // If at least one operand has a value, the result should have a value
675
675
  if (this.value !== null || other.value !== null) {
676
- const valThis = this.value === null ? this._normalize(1) : this.value;
677
- const valOther = other.value === null ? other._normalize(1) : other.value;
676
+ const valThis = this.value === null ? this._normalize(one(other.value)) : this.value;
677
+ const valOther = other.value === null ? other._normalize(one(this.value)) : other.value;
678
678
  res.value = divideScalar(valThis, valOther);
679
679
  } else {
680
680
  res.value = null;
@@ -730,6 +730,21 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
730
730
  }
731
731
  }
732
732
 
733
+ /**
734
+ * Create a value one with the numeric type of `typeOfValue`.
735
+ * For example, `one(new BigNumber(3))` returns `BigNumber(1)`
736
+ * @param {number | Fraction | BigNumber} typeOfValue
737
+ * @returns {number | Fraction | BigNumber}
738
+ */
739
+ function one(typeOfValue) {
740
+ // TODO: this is a workaround to prevent the following BigNumber conversion error from throwing:
741
+ // "TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber"
742
+ // see https://github.com/josdejong/mathjs/issues/3450
743
+ // https://github.com/josdejong/mathjs/pull/3375
744
+ const convert = Unit._getNumberConverter((0, _is.typeOf)(typeOfValue));
745
+ return convert(1);
746
+ }
747
+
733
748
  /**
734
749
  * Calculate the absolute value of a unit
735
750
  * @memberof Unit
@@ -278,7 +278,7 @@ function _resize(array, size, dim, defaultValue) {
278
278
  * not equal that of the old ones
279
279
  */
280
280
  function reshape(array, sizes) {
281
- const flatArray = flatten(array);
281
+ const flatArray = flatten(array, true); // since it has rectangular
282
282
  const currentLength = flatArray.length;
283
283
  if (!Array.isArray(array) || !Array.isArray(sizes)) {
284
284
  throw new TypeError('Array expected');
@@ -475,22 +475,46 @@ function _unsqueeze(array, dims, dim) {
475
475
  * Flatten a multi dimensional array, put all elements in a one dimensional
476
476
  * array
477
477
  * @param {Array} array A multi dimensional array
478
+ * @param {boolean} isRectangular Optional. If the array is rectangular (not jagged)
478
479
  * @return {Array} The flattened array (1 dimensional)
479
480
  */
480
481
  function flatten(array) {
482
+ let isRectangular = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
481
483
  if (!Array.isArray(array)) {
482
484
  // if not an array, return as is
483
485
  return array;
484
486
  }
487
+ if (typeof isRectangular !== 'boolean') {
488
+ throw new TypeError('Boolean expected for second argument of flatten');
489
+ }
485
490
  const flat = [];
486
- array.forEach(function callback(value) {
487
- if (Array.isArray(value)) {
488
- value.forEach(callback); // traverse through sub-arrays recursively
491
+ if (isRectangular) {
492
+ _flattenRectangular(array);
493
+ } else {
494
+ _flatten(array);
495
+ }
496
+ return flat;
497
+ function _flatten(array) {
498
+ for (let i = 0; i < array.length; i++) {
499
+ const item = array[i];
500
+ if (Array.isArray(item)) {
501
+ _flatten(item);
502
+ } else {
503
+ flat.push(item);
504
+ }
505
+ }
506
+ }
507
+ function _flattenRectangular(array) {
508
+ if (Array.isArray(array[0])) {
509
+ for (let i = 0; i < array.length; i++) {
510
+ _flattenRectangular(array[i]);
511
+ }
489
512
  } else {
490
- flat.push(value);
513
+ for (let i = 0; i < array.length; i++) {
514
+ flat.push(array[i]);
515
+ }
491
516
  }
492
- });
493
- return flat;
517
+ }
494
518
  }
495
519
 
496
520
  /**
@@ -36,7 +36,7 @@ function containsCollections(array) {
36
36
  */
37
37
  function deepForEach(array, callback) {
38
38
  if ((0, _is.isMatrix)(array)) {
39
- array.forEach(x => callback(x));
39
+ array.forEach(x => callback(x), false, true);
40
40
  } else {
41
41
  (0, _array.deepForEach)(array, callback, true);
42
42
  }
@@ -57,14 +57,14 @@ function deepForEach(array, callback) {
57
57
  function deepMap(array, callback, skipZeros) {
58
58
  if (!skipZeros) {
59
59
  if ((0, _is.isMatrix)(array)) {
60
- return array.map(x => callback(x));
60
+ return array.map(x => callback(x), false, true);
61
61
  } else {
62
62
  return (0, _array.deepMap)(array, callback, true);
63
63
  }
64
64
  }
65
65
  const skipZerosCallback = x => x === 0 ? x : callback(x);
66
66
  if ((0, _is.isMatrix)(array)) {
67
- return array.map(x => skipZerosCallback(x));
67
+ return array.map(x => skipZerosCallback(x), false, true);
68
68
  } else {
69
69
  return (0, _array.deepMap)(array, skipZerosCallback, true);
70
70
  }
@@ -161,6 +161,9 @@ const latexFunctions = exports.latexFunctions = {
161
161
  floor: {
162
162
  1: '\\left\\lfloor${args[0]}\\right\\rfloor'
163
163
  },
164
+ fraction: {
165
+ 2: '\\frac{${args[0]}}{${args[1]}}'
166
+ },
164
167
  gcd: '\\gcd\\left(${args}\\right)',
165
168
  hypot: '\\hypot\\left(${args}\\right)',
166
169
  log: {
@@ -189,7 +192,7 @@ const latexFunctions = exports.latexFunctions = {
189
192
  2: '\\sqrt[${args[1]}]{${args[0]}}'
190
193
  },
191
194
  nthRoots: {
192
- 2: '\\{y : $y^{args[1]} = {${args[0]}}\\}'
195
+ 2: '\\{y : y^${args[1]} = {${args[0]}}\\}'
193
196
  },
194
197
  pow: {
195
198
  2: `\\left(\${args[0]}\\right)${latexOperators.pow}{\${args[1]}}`
@@ -14,13 +14,20 @@ var _is = require("./is.js");
14
14
  * @param {Function} callback The original callback function to simplify.
15
15
  * @param {Array|Matrix} array The array that will be used with the callback function.
16
16
  * @param {string} name The name of the function that is using the callback.
17
+ * @param {boolean} [isUnary=false] If true, the callback function is unary and will be optimized as such.
17
18
  * @returns {Function} Returns a simplified version of the callback function.
18
19
  */
19
20
  function optimizeCallback(callback, array, name) {
21
+ let isUnary = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
20
22
  if (_typedFunction.default.isTypedFunction(callback)) {
21
- const firstIndex = (array.isMatrix ? array.size() : (0, _array.arraySize)(array)).map(() => 0);
22
- const firstValue = array.isMatrix ? array.get(firstIndex) : (0, _array.get)(array, firstIndex);
23
- const numberOfArguments = _findNumberOfArguments(callback, firstValue, firstIndex, array);
23
+ let numberOfArguments;
24
+ if (isUnary) {
25
+ numberOfArguments = 1;
26
+ } else {
27
+ const firstIndex = (array.isMatrix ? array.size() : (0, _array.arraySize)(array)).map(() => 0);
28
+ const firstValue = array.isMatrix ? array.get(firstIndex) : (0, _array.get)(array, firstIndex);
29
+ numberOfArguments = _findNumberOfArgumentsTyped(callback, firstValue, firstIndex, array);
30
+ }
24
31
  let fastCallback;
25
32
  if (array.isMatrix && array.dataType !== 'mixed' && array.dataType !== undefined) {
26
33
  const singleSignature = _findSingleSignatureWithArity(callback, numberOfArguments);
@@ -29,21 +36,37 @@ function optimizeCallback(callback, array, name) {
29
36
  fastCallback = callback;
30
37
  }
31
38
  if (numberOfArguments >= 1 && numberOfArguments <= 3) {
32
- return function () {
33
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
34
- args[_key] = arguments[_key];
39
+ return {
40
+ isUnary: numberOfArguments === 1,
41
+ fn: function () {
42
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
43
+ args[_key] = arguments[_key];
44
+ }
45
+ return _tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name);
35
46
  }
36
- return _tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name);
37
47
  };
38
48
  }
39
- return function () {
40
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
41
- args[_key2] = arguments[_key2];
49
+ return {
50
+ isUnary: false,
51
+ fn: function () {
52
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
53
+ args[_key2] = arguments[_key2];
54
+ }
55
+ return _tryFunctionWithArgs(fastCallback, args, name, callback.name);
42
56
  }
43
- return _tryFunctionWithArgs(fastCallback, args, name, callback.name);
44
57
  };
45
58
  }
46
- return callback;
59
+ if (isUnary === undefined) {
60
+ return {
61
+ isUnary: _findIfCallbackIsUnary(callback),
62
+ fn: callback
63
+ };
64
+ } else {
65
+ return {
66
+ isUnary,
67
+ fn: callback
68
+ };
69
+ }
47
70
  }
48
71
  function _findSingleSignatureWithArity(callback, arity) {
49
72
  const matchingFunctions = [];
@@ -57,7 +80,32 @@ function _findSingleSignatureWithArity(callback, arity) {
57
80
  return matchingFunctions[0];
58
81
  }
59
82
  }
60
- function _findNumberOfArguments(callback, value, index, array) {
83
+
84
+ /**
85
+ * Determines if a given callback function is unary (i.e., takes exactly one argument).
86
+ *
87
+ * This function checks the following conditions to determine if the callback is unary:
88
+ * 1. The callback function should have exactly one parameter.
89
+ * 2. The callback function should not use the `arguments` object.
90
+ * 3. The callback function should not use rest parameters (`...`).
91
+ * If in doubt, this function shall return `false` to be safe
92
+ *
93
+ * @param {Function} callback - The callback function to be checked.
94
+ * @returns {boolean} - Returns `true` if the callback is unary, otherwise `false`.
95
+ */
96
+ function _findIfCallbackIsUnary(callback) {
97
+ if (callback.length !== 1) return false;
98
+ const callbackStr = callback.toString();
99
+ // Check if the callback function uses `arguments`
100
+ if (/arguments/.test(callbackStr)) return false;
101
+
102
+ // Extract the parameters of the callback function
103
+ const paramsStr = callbackStr.match(/\(.*?\)/);
104
+ // Check if the callback function uses rest parameters
105
+ if (/\.\.\./.test(paramsStr)) return false;
106
+ return true;
107
+ }
108
+ function _findNumberOfArgumentsTyped(callback, value, index, array) {
61
109
  const testArgs = [value, index, array];
62
110
  for (let i = 3; i > 0; i--) {
63
111
  const args = testArgs.slice(0, i);
@@ -12,8 +12,7 @@ var _assert = _interopRequireDefault(require("assert"));
12
12
  var allIsFunctions = _interopRequireWildcard(require("./is.js"));
13
13
  var _create = require("../core/create.js");
14
14
  var _string = require("./string.js");
15
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
16
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
15
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
17
16
  /**
18
17
  * This file contains helper methods to create expected snapshot structures
19
18
  * of both instance and ES6 exports.
@@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.version = void 0;
7
- const version = exports.version = '14.3.1';
7
+ const version = exports.version = '14.5.0';
8
8
  // Note: This file is automatically generated when building math.js.
9
9
  // Changes made in this file will be overwritten.
@@ -70,47 +70,47 @@ export var createLOG2E = /* #__PURE__ */recreateFactory('LOG2E', ['config', '?Bi
70
70
  } = _ref9;
71
71
  return config.number === 'BigNumber' ? new BigNumber(1).div(new BigNumber(2).ln()) : Math.LOG2E;
72
72
  });
73
- export var createLOG10E = /* #__PURE__ */recreateFactory('LOG10E', ['config', '?BigNumber'], _ref10 => {
73
+ export var createLOG10E = /* #__PURE__ */recreateFactory('LOG10E', ['config', '?BigNumber'], _ref0 => {
74
74
  var {
75
75
  config,
76
76
  BigNumber
77
- } = _ref10;
77
+ } = _ref0;
78
78
  return config.number === 'BigNumber' ? new BigNumber(1).div(new BigNumber(10).ln()) : Math.LOG10E;
79
79
  });
80
80
  export var createSQRT1_2 = /* #__PURE__ */recreateFactory(
81
81
  // eslint-disable-line camelcase
82
- 'SQRT1_2', ['config', '?BigNumber'], _ref11 => {
82
+ 'SQRT1_2', ['config', '?BigNumber'], _ref1 => {
83
83
  var {
84
84
  config,
85
85
  BigNumber
86
- } = _ref11;
86
+ } = _ref1;
87
87
  return config.number === 'BigNumber' ? new BigNumber('0.5').sqrt() : Math.SQRT1_2;
88
88
  });
89
- export var createSQRT2 = /* #__PURE__ */recreateFactory('SQRT2', ['config', '?BigNumber'], _ref12 => {
89
+ export var createSQRT2 = /* #__PURE__ */recreateFactory('SQRT2', ['config', '?BigNumber'], _ref10 => {
90
90
  var {
91
91
  config,
92
92
  BigNumber
93
- } = _ref12;
93
+ } = _ref10;
94
94
  return config.number === 'BigNumber' ? new BigNumber(2).sqrt() : Math.SQRT2;
95
95
  });
96
- export var createI = /* #__PURE__ */recreateFactory('i', ['Complex'], _ref13 => {
96
+ export var createI = /* #__PURE__ */recreateFactory('i', ['Complex'], _ref11 => {
97
97
  var {
98
98
  Complex
99
- } = _ref13;
99
+ } = _ref11;
100
100
  return Complex.I;
101
101
  });
102
102
 
103
103
  // for backward compatibility with v5
104
- export var createUppercasePi = /* #__PURE__ */factory('PI', ['pi'], _ref14 => {
104
+ export var createUppercasePi = /* #__PURE__ */factory('PI', ['pi'], _ref12 => {
105
105
  var {
106
106
  pi
107
- } = _ref14;
107
+ } = _ref12;
108
108
  return pi;
109
109
  });
110
- export var createUppercaseE = /* #__PURE__ */factory('E', ['e'], _ref15 => {
110
+ export var createUppercaseE = /* #__PURE__ */factory('E', ['e'], _ref13 => {
111
111
  var {
112
112
  e
113
- } = _ref15;
113
+ } = _ref13;
114
114
  return e;
115
115
  });
116
116
  export var createVersion = /* #__PURE__ */factory('version', [], () => version);