mathjs 11.8.0 → 11.8.2

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 (63) hide show
  1. package/HISTORY.md +25 -1
  2. package/lib/browser/math.js +1 -1
  3. package/lib/browser/math.js.LICENSE.txt +2 -2
  4. package/lib/browser/math.js.map +1 -1
  5. package/lib/cjs/entry/dependenciesAny/dependenciesDistance.generated.js +2 -2
  6. package/lib/cjs/entry/pureFunctionsAny.generated.js +2 -2
  7. package/lib/cjs/expression/Parser.js +1 -1
  8. package/lib/cjs/expression/function/parser.js +1 -1
  9. package/lib/cjs/expression/transform/filter.transform.js +4 -13
  10. package/lib/cjs/expression/transform/forEach.transform.js +3 -12
  11. package/lib/cjs/expression/transform/map.transform.js +4 -13
  12. package/lib/cjs/function/algebra/derivative.js +2 -2
  13. package/lib/cjs/function/algebra/leafCount.js +1 -1
  14. package/lib/cjs/function/algebra/polynomialRoot.js +1 -1
  15. package/lib/cjs/function/algebra/rationalize.js +4 -4
  16. package/lib/cjs/function/algebra/resolve.js +1 -1
  17. package/lib/cjs/function/algebra/simplify.js +7 -7
  18. package/lib/cjs/function/algebra/simplifyConstant.js +2 -2
  19. package/lib/cjs/function/algebra/simplifyCore.js +2 -2
  20. package/lib/cjs/function/algebra/symbolicEqual.js +8 -8
  21. package/lib/cjs/function/geometry/distance.js +21 -17
  22. package/lib/cjs/function/matrix/filter.js +2 -11
  23. package/lib/cjs/function/matrix/forEach.js +3 -12
  24. package/lib/cjs/function/matrix/map.js +6 -38
  25. package/lib/cjs/function/matrix/sqrtm.js +1 -1
  26. package/lib/cjs/header.js +2 -2
  27. package/lib/cjs/type/bignumber/function/bignumber.js +7 -0
  28. package/lib/cjs/type/fraction/function/fraction.js +8 -1
  29. package/lib/cjs/type/number.js +7 -3
  30. package/lib/cjs/type/unit/function/splitUnit.js +1 -1
  31. package/lib/cjs/utils/applyCallback.js +73 -0
  32. package/lib/cjs/utils/bignumber/formatter.js +4 -3
  33. package/lib/cjs/version.js +1 -1
  34. package/lib/esm/entry/dependenciesAny/dependenciesDistance.generated.js +2 -2
  35. package/lib/esm/entry/pureFunctionsAny.generated.js +2 -2
  36. package/lib/esm/expression/Parser.js +1 -1
  37. package/lib/esm/expression/function/parser.js +1 -1
  38. package/lib/esm/expression/transform/filter.transform.js +4 -13
  39. package/lib/esm/expression/transform/forEach.transform.js +3 -12
  40. package/lib/esm/expression/transform/map.transform.js +4 -13
  41. package/lib/esm/function/algebra/derivative.js +2 -2
  42. package/lib/esm/function/algebra/leafCount.js +1 -1
  43. package/lib/esm/function/algebra/polynomialRoot.js +1 -1
  44. package/lib/esm/function/algebra/rationalize.js +4 -4
  45. package/lib/esm/function/algebra/resolve.js +1 -1
  46. package/lib/esm/function/algebra/simplify.js +7 -7
  47. package/lib/esm/function/algebra/simplifyConstant.js +2 -2
  48. package/lib/esm/function/algebra/simplifyCore.js +2 -2
  49. package/lib/esm/function/algebra/symbolicEqual.js +8 -8
  50. package/lib/esm/function/geometry/distance.js +21 -17
  51. package/lib/esm/function/matrix/filter.js +2 -11
  52. package/lib/esm/function/matrix/forEach.js +3 -12
  53. package/lib/esm/function/matrix/map.js +6 -38
  54. package/lib/esm/function/matrix/sqrtm.js +1 -1
  55. package/lib/esm/type/bignumber/function/bignumber.js +5 -0
  56. package/lib/esm/type/fraction/function/fraction.js +6 -1
  57. package/lib/esm/type/number.js +5 -3
  58. package/lib/esm/type/unit/function/splitUnit.js +1 -1
  59. package/lib/esm/utils/applyCallback.js +67 -0
  60. package/lib/esm/utils/bignumber/formatter.js +4 -3
  61. package/lib/esm/version.js +1 -1
  62. package/package.json +19 -19
  63. package/types/index.d.ts +149 -50
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.applyCallback = applyCallback;
8
+ var _typedFunction = _interopRequireDefault(require("typed-function"));
9
+ var _is = require("./is.js");
10
+ /**
11
+ * Invoke a callback for functions like map and filter with a matching number of arguments
12
+ * @param {function} callback
13
+ * @param {any} value
14
+ * @param {number | number[]} index
15
+ * @param {Array} array
16
+ * @param {string} mappingFnName The name of the function that is invoking these callbacks, for example "map" or "filter"
17
+ * @returns {*}
18
+ */
19
+ function applyCallback(callback, value, index, array, mappingFnName) {
20
+ if (_typedFunction["default"].isTypedFunction(callback)) {
21
+ // invoke the typed callback function with the matching number of arguments only
22
+
23
+ var args3 = [value, index, array];
24
+ var signature3 = _typedFunction["default"].resolve(callback, args3);
25
+ if (signature3) {
26
+ return tryWithArgs(signature3.implementation, args3);
27
+ }
28
+ var args2 = [value, index];
29
+ var signature2 = _typedFunction["default"].resolve(callback, args2);
30
+ if (signature2) {
31
+ return tryWithArgs(signature2.implementation, args2);
32
+ }
33
+ var args1 = [value];
34
+ var signature1 = _typedFunction["default"].resolve(callback, args1);
35
+ if (signature1) {
36
+ return tryWithArgs(signature1.implementation, args1);
37
+ }
38
+
39
+ // fallback (will throw an exception)
40
+ return tryWithArgs(callback, args3);
41
+ } else {
42
+ // A regular JavaScript function
43
+ return callback(value, index, array);
44
+ }
45
+
46
+ /**
47
+ * @param {function} signature The selected signature of the typed-function
48
+ * @param {Array} args List with arguments to apply to the selected signature
49
+ * @returns {*} Returns the return value of the invoked signature
50
+ * @throws {TypeError} Throws an error when no matching signature was found
51
+ */
52
+ function tryWithArgs(signature, args) {
53
+ try {
54
+ return signature.apply(signature, args);
55
+ } catch (err) {
56
+ var _err$data;
57
+ // Enrich the error message so the user understands that it took place inside the callback function
58
+ if (err instanceof TypeError && ((_err$data = err.data) === null || _err$data === void 0 ? void 0 : _err$data.category) === 'wrongType') {
59
+ var argsDesc = [];
60
+ argsDesc.push("value: ".concat((0, _is.typeOf)(value)));
61
+ if (args.length >= 2) {
62
+ argsDesc.push("index: ".concat((0, _is.typeOf)(index)));
63
+ }
64
+ if (args.length >= 3) {
65
+ argsDesc.push("array: ".concat((0, _is.typeOf)(array)));
66
+ }
67
+ throw new TypeError("Function ".concat(mappingFnName, " cannot apply callback arguments ") + "".concat(callback.name, "(").concat(argsDesc.join(', '), ") at index ").concat(JSON.stringify(index)));
68
+ } else {
69
+ throw new TypeError("Function ".concat(mappingFnName, " cannot apply callback arguments ") + "to function ".concat(callback.name, ": ").concat(err.message));
70
+ }
71
+ }
72
+ }
73
+ }
@@ -154,7 +154,7 @@ function format(value, options) {
154
154
  // determine precision from options
155
155
  if (typeof options === 'number') {
156
156
  precision = options;
157
- } else if (options.precision) {
157
+ } else if (options.precision !== undefined) {
158
158
  precision = options.precision;
159
159
  }
160
160
  if (options.wordSize) {
@@ -215,7 +215,7 @@ function format(value, options) {
215
215
 
216
216
  /**
217
217
  * Format a BigNumber in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3'
218
- * @param {BigNumber | string} value
218
+ * @param {BigNumber} value
219
219
  * @param {number} [precision] Optional number of significant figures to return.
220
220
  */
221
221
  function toEngineering(value, precision) {
@@ -227,7 +227,8 @@ function toEngineering(value, precision) {
227
227
  var valueWithoutExp = value.mul(Math.pow(10, -newExp));
228
228
  var valueStr = valueWithoutExp.toPrecision(precision);
229
229
  if (valueStr.indexOf('e') !== -1) {
230
- valueStr = valueWithoutExp.toString();
230
+ var BigNumber = value.constructor;
231
+ valueStr = new BigNumber(valueStr).toFixed();
231
232
  }
232
233
  return valueStr + 'e' + (e >= 0 ? '+' : '') + newExp.toString();
233
234
  }
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.version = void 0;
7
- var version = '11.8.0';
7
+ var version = '11.8.2';
8
8
  // Note: This file is automatically generated when building math.js.
9
9
  // Changes made in this file will be overwritten.
10
10
  exports.version = version;
@@ -4,21 +4,21 @@
4
4
  */
5
5
  import { absDependencies } from './dependenciesAbs.generated.js';
6
6
  import { addScalarDependencies } from './dependenciesAddScalar.generated.js';
7
+ import { deepEqualDependencies } from './dependenciesDeepEqual.generated.js';
7
8
  import { divideScalarDependencies } from './dependenciesDivideScalar.generated.js';
8
9
  import { multiplyScalarDependencies } from './dependenciesMultiplyScalar.generated.js';
9
10
  import { sqrtDependencies } from './dependenciesSqrt.generated.js';
10
11
  import { subtractDependencies } from './dependenciesSubtract.generated.js';
11
12
  import { typedDependencies } from './dependenciesTyped.generated.js';
12
- import { unaryMinusDependencies } from './dependenciesUnaryMinus.generated.js';
13
13
  import { createDistance } from '../../factoriesAny.js';
14
14
  export var distanceDependencies = {
15
15
  absDependencies,
16
16
  addScalarDependencies,
17
+ deepEqualDependencies,
17
18
  divideScalarDependencies,
18
19
  multiplyScalarDependencies,
19
20
  sqrtDependencies,
20
21
  subtractDependencies,
21
22
  typedDependencies,
22
- unaryMinusDependencies,
23
23
  createDistance
24
24
  };
@@ -875,12 +875,12 @@ export var diff = /* #__PURE__ */createDiff({
875
875
  export var distance = /* #__PURE__ */createDistance({
876
876
  abs,
877
877
  addScalar,
878
+ deepEqual,
878
879
  divideScalar,
879
880
  multiplyScalar,
880
881
  sqrt,
881
882
  subtract,
882
- typed,
883
- unaryMinus
883
+ typed
884
884
  });
885
885
  export var dotMultiply = /* #__PURE__ */createDotMultiply({
886
886
  concat,
@@ -40,7 +40,7 @@ export var createParserClass = /* #__PURE__ */factory(name, dependencies, _ref =
40
40
  * parser.evaluate('f(2, 3)') // 8
41
41
  *
42
42
  * // get and set variables and functions
43
- * const x = parser.get('x') // 7
43
+ * const x = parser.get('x') // 3.5
44
44
  * const f = parser.get('f') // function
45
45
  * const g = f(3, 2) // 9
46
46
  * parser.set('h', 500)
@@ -30,7 +30,7 @@ export var createParser = /* #__PURE__ */factory(name, dependencies, _ref => {
30
30
  * parser.evaluate('f(2, 3)') // 8
31
31
  *
32
32
  * // get and set variables and functions
33
- * const x = parser.get('x') // 7
33
+ * const x = parser.get('x') // 3.5
34
34
  * const f = parser.get('f') // function
35
35
  * const g = f(3, 2) // 9
36
36
  * parser.set('h', 500)
@@ -1,8 +1,8 @@
1
- import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
1
+ import { applyCallback } from '../../utils/applyCallback.js';
2
2
  import { filter, filterRegExp } from '../../utils/array.js';
3
- import { maxArgumentCount } from '../../utils/function.js';
4
- import { compileInlineExpression } from './utils/compileInlineExpression.js';
5
3
  import { factory } from '../../utils/factory.js';
4
+ import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
5
+ import { compileInlineExpression } from './utils/compileInlineExpression.js';
6
6
  var name = 'filter';
7
7
  var dependencies = ['typed'];
8
8
  export var createFilterTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
@@ -61,17 +61,8 @@ export var createFilterTransform = /* #__PURE__ */factory(name, dependencies, _r
61
61
  * @private
62
62
  */
63
63
  function _filter(x, callback) {
64
- // figure out what number of arguments the callback function expects
65
- var args = maxArgumentCount(callback);
66
64
  return filter(x, function (value, index, array) {
67
65
  // invoke the callback function with the right number of arguments
68
- if (args === 1) {
69
- return callback(value);
70
- } else if (args === 2) {
71
- return callback(value, [index + 1]);
72
- } else {
73
- // 3 or -1
74
- return callback(value, [index + 1], array);
75
- }
66
+ return applyCallback(callback, value, [index + 1], array, 'filter');
76
67
  });
77
68
  }
@@ -1,7 +1,7 @@
1
- import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
2
- import { maxArgumentCount } from '../../utils/function.js';
1
+ import { applyCallback } from '../../utils/applyCallback.js';
3
2
  import { forEach } from '../../utils/array.js';
4
3
  import { factory } from '../../utils/factory.js';
4
+ import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
5
5
  import { compileInlineExpression } from './utils/compileInlineExpression.js';
6
6
  var name = 'forEach';
7
7
  var dependencies = ['typed'];
@@ -36,8 +36,6 @@ export var createForEachTransform = /* #__PURE__ */factory(name, dependencies, _
36
36
  // one-based version of forEach
37
37
  var _forEach = typed('forEach', {
38
38
  'Array | Matrix, function': function ArrayMatrixFunction(array, callback) {
39
- // figure out what number of arguments the callback function expects
40
- var args = maxArgumentCount(callback);
41
39
  var recurse = function recurse(value, index) {
42
40
  if (Array.isArray(value)) {
43
41
  forEach(value, function (child, i) {
@@ -46,14 +44,7 @@ export var createForEachTransform = /* #__PURE__ */factory(name, dependencies, _
46
44
  });
47
45
  } else {
48
46
  // invoke the callback function with the right number of arguments
49
- if (args === 1) {
50
- callback(value);
51
- } else if (args === 2) {
52
- callback(value, index);
53
- } else {
54
- // 3 or -1
55
- callback(value, index, array);
56
- }
47
+ return applyCallback(callback, value, index, array, 'forEach');
57
48
  }
58
49
  };
59
50
  recurse(array.valueOf(), []); // pass Array
@@ -1,7 +1,7 @@
1
- import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
2
- import { maxArgumentCount } from '../../utils/function.js';
1
+ import { applyCallback } from '../../utils/applyCallback.js';
3
2
  import { map } from '../../utils/array.js';
4
3
  import { factory } from '../../utils/factory.js';
4
+ import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
5
5
  import { compileInlineExpression } from './utils/compileInlineExpression.js';
6
6
  var name = 'map';
7
7
  var dependencies = ['typed'];
@@ -48,7 +48,7 @@ export var createMapTransform = /* #__PURE__ */factory(name, dependencies, _ref
48
48
  });
49
49
 
50
50
  /**
51
- * Map for a multi dimensional array. One-based indexes
51
+ * Map for a multidimensional array. One-based indexes
52
52
  * @param {Array} array
53
53
  * @param {function} callback
54
54
  * @param {Array} orig
@@ -56,8 +56,6 @@ export var createMapTransform = /* #__PURE__ */factory(name, dependencies, _ref
56
56
  * @private
57
57
  */
58
58
  function _map(array, callback, orig) {
59
- // figure out what number of arguments the callback function expects
60
- var argsCount = maxArgumentCount(callback);
61
59
  function recurse(value, index) {
62
60
  if (Array.isArray(value)) {
63
61
  return map(value, function (child, i) {
@@ -66,14 +64,7 @@ function _map(array, callback, orig) {
66
64
  });
67
65
  } else {
68
66
  // invoke the (typed) callback function with the right number of arguments
69
- if (argsCount === 1) {
70
- return callback(value);
71
- } else if (argsCount === 2) {
72
- return callback(value, index);
73
- } else {
74
- // 3 or -1
75
- return callback(value, index, orig);
76
- }
67
+ return applyCallback(callback, value, index, orig, 'map');
77
68
  }
78
69
  }
79
70
  return recurse(array, []);
@@ -29,8 +29,8 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
29
29
  *
30
30
  * Syntax:
31
31
  *
32
- * derivative(expr, variable)
33
- * derivative(expr, variable, options)
32
+ * math.derivative(expr, variable)
33
+ * math.derivative(expr, variable, options)
34
34
  *
35
35
  * Examples:
36
36
  *
@@ -29,7 +29,7 @@ export var createLeafCount = /* #__PURE__ */factory(name, dependencies, _ref =>
29
29
  *
30
30
  * Syntax:
31
31
  *
32
- * leafCount(expr)
32
+ * math.leafCount(expr)
33
33
  *
34
34
  * Examples:
35
35
  *
@@ -24,7 +24,7 @@ export var createPolynomialRoot = /* #__PURE__ */factory(name, dependencies, _re
24
24
  *
25
25
  * Syntax:
26
26
  *
27
- * polynomialRoot(constant, linearCoeff, quadraticCoeff, cubicCoeff)
27
+ * math.polynomialRoot(constant, linearCoeff, quadraticCoeff, cubicCoeff)
28
28
  *
29
29
  * Examples:
30
30
  * // linear
@@ -39,10 +39,10 @@ export var createRationalize = /* #__PURE__ */factory(name, dependencies, _ref =
39
39
  *
40
40
  * Syntax:
41
41
  *
42
- * rationalize(expr)
43
- * rationalize(expr, detailed)
44
- * rationalize(expr, scope)
45
- * rationalize(expr, scope, detailed)
42
+ * math.rationalize(expr)
43
+ * math.rationalize(expr, detailed)
44
+ * math.rationalize(expr, scope)
45
+ * math.rationalize(expr, scope, detailed)
46
46
  *
47
47
  * Examples:
48
48
  *
@@ -17,7 +17,7 @@ export var createResolve = /* #__PURE__ */factory(name, dependencies, _ref => {
17
17
  *
18
18
  * Syntax:
19
19
  *
20
- * resolve(expr, scope)
20
+ * math.resolve(expr, scope)
21
21
  *
22
22
  * Examples:
23
23
  *
@@ -138,13 +138,13 @@ export var createSimplify = /* #__PURE__ */factory(name, dependencies, _ref => {
138
138
  *
139
139
  * Syntax:
140
140
  *
141
- * simplify(expr)
142
- * simplify(expr, rules)
143
- * simplify(expr, rules)
144
- * simplify(expr, rules, scope)
145
- * simplify(expr, rules, scope, options)
146
- * simplify(expr, scope)
147
- * simplify(expr, scope, options)
141
+ * math.simplify(expr)
142
+ * math.simplify(expr, rules)
143
+ * math.simplify(expr, rules)
144
+ * math.simplify(expr, rules, scope)
145
+ * math.simplify(expr, rules, scope, options)
146
+ * math.simplify(expr, scope)
147
+ * math.simplify(expr, scope, options)
148
148
  *
149
149
  * Examples:
150
150
  *
@@ -40,8 +40,8 @@ export var createSimplifyConstant = /* #__PURE__ */factory(name, dependencies, _
40
40
  *
41
41
  * Syntax:
42
42
  *
43
- * simplifyConstant(expr)
44
- * simplifyConstant(expr, options)
43
+ * math.simplifyConstant(expr)
44
+ * math.simplifyConstant(expr, options)
45
45
  *
46
46
  * Examples:
47
47
  *
@@ -67,8 +67,8 @@ export var createSimplifyCore = /* #__PURE__ */factory(name, dependencies, _ref
67
67
  *
68
68
  * Syntax:
69
69
  *
70
- * simplifyCore(expr)
71
- * simplifyCore(expr, options)
70
+ * math.simplifyCore(expr)
71
+ * math.simplifyCore(expr, options)
72
72
  *
73
73
  * Examples:
74
74
  *
@@ -25,20 +25,20 @@ export var createSymbolicEqual = /* #__PURE__ */factory(name, dependencies, _ref
25
25
  *
26
26
  * Syntax:
27
27
  *
28
- * symbolicEqual(expr1, expr2)
29
- * symbolicEqual(expr1, expr2, options)
28
+ * math.symbolicEqual(expr1, expr2)
29
+ * math.symbolicEqual(expr1, expr2, options)
30
30
  *
31
31
  * Examples:
32
32
  *
33
- * symbolicEqual('x*y', 'y*x') // Returns true
34
- * symbolicEqual('x*y', 'y*x', {context: {multiply: {commutative: false}}}) // Returns false
35
- * symbolicEqual('x/y', '(y*x^(-1))^(-1)') // Returns true
36
- * symbolicEqual('abs(x)','x') // Returns false
37
- * symbolicEqual('abs(x)','x', simplify.positiveContext) // Returns true
33
+ * math.symbolicEqual('x*y', 'y*x') // Returns true
34
+ * math.symbolicEqual('x*y', 'y*x', {context: {multiply: {commutative: false}}}) // Returns false
35
+ * math.symbolicEqual('x/y', '(y*x^(-1))^(-1)') // Returns true
36
+ * math.symbolicEqual('abs(x)','x') // Returns false
37
+ * math.symbolicEqual('abs(x)','x', simplify.positiveContext) // Returns true
38
38
  *
39
39
  * See also:
40
40
  *
41
- * simplify, evaluate
41
+ * simplify, evaluate
42
42
  *
43
43
  * @param {Node|string} expr1 The first expression to compare
44
44
  * @param {Node|string} expr2 The second expression to compare
@@ -1,7 +1,7 @@
1
1
  import { isBigNumber } from '../../utils/is.js';
2
2
  import { factory } from '../../utils/factory.js';
3
3
  var name = 'distance';
4
- var dependencies = ['typed', 'addScalar', 'subtract', 'divideScalar', 'multiplyScalar', 'unaryMinus', 'sqrt', 'abs'];
4
+ var dependencies = ['typed', 'addScalar', 'subtract', 'divideScalar', 'multiplyScalar', 'deepEqual', 'sqrt', 'abs'];
5
5
  export var createDistance = /* #__PURE__ */factory(name, dependencies, _ref => {
6
6
  var {
7
7
  typed,
@@ -9,7 +9,7 @@ export var createDistance = /* #__PURE__ */factory(name, dependencies, _ref => {
9
9
  subtract,
10
10
  multiplyScalar,
11
11
  divideScalar,
12
- unaryMinus,
12
+ deepEqual,
13
13
  sqrt,
14
14
  abs
15
15
  } = _ref;
@@ -31,10 +31,10 @@ export var createDistance = /* #__PURE__ */factory(name, dependencies, _ref => {
31
31
  * math.distance([x1,y1,z1,a1], [x2,y2,z2,a2])
32
32
  * math.distance([[x1,y1], [x2,y2], [x3,y3]])
33
33
  * math.distance([[x1,y1,z1], [x2,y2,z2], [x3,y3,z3]])
34
- * math.distance([x1,y1], [x2,y2,z2])
35
- * math.distance([x1,y1], [x2,y2], [x3,y3])
34
+ * math.distance([pointX,pointY], [a,b,c])
35
+ * math.distance([pointX,pointY], [lineOnePtX,lineOnePtY], [lineTwoPtX,lineTwoPtY])
36
36
  * math.distance({pointX, pointY}, {lineOnePtX, lineOnePtY}, {lineTwoPtX, lineTwoPtY})
37
- * math.distance([x1,y1,z1], [x0, y0, z0, a, b, c])
37
+ * math.distance([pointX,pointY,pointZ], [x0, y0, z0, a, b, c])
38
38
  * math.distance({pointX, pointY, pointZ}, {x0, y0, z0, a, b, c})
39
39
  *
40
40
  * Examples:
@@ -50,11 +50,11 @@ export var createDistance = /* #__PURE__ */factory(name, dependencies, _ref => {
50
50
  * math.distance([[1, 2], [1, 2], [1, 3]]) // Returns [0, 1, 1]
51
51
  * math.distance([[1,2,4], [1,2,6], [8,1,3]]) // Returns [2, 7.14142842854285, 7.681145747868608]
52
52
  * math.distance([10, 10], [8, 1, 3]) // Returns 11.535230316796387
53
- * math.distance([10, 10], [2, 3], [-8, 0]) // Returns 8.759953130362847
53
+ * math.distance([0, 0], [3, 0], [0, 4]) // Returns 2.4
54
54
  * math.distance(
55
- * {pointX: 1, pointY: 4},
56
- * {lineOnePtX: 6, lineOnePtY: 3},
57
- * {lineTwoPtX: 2, lineTwoPtY: 8}) // Returns 2.720549372624744
55
+ * {pointX: 0, pointY: 0},
56
+ * {lineOnePtX: 3, lineOnePtY: 0},
57
+ * {lineTwoPtX: 0, lineTwoPtY: 4}) // Returns 2.4
58
58
  * math.distance([2, 3, 1], [1, 1, 2, 5, 0, 1]) // Returns 2.3204774044612857
59
59
  * math.distance(
60
60
  * {pointX: 2, pointY: 3, pointZ: 1},
@@ -77,10 +77,12 @@ export var createDistance = /* #__PURE__ */factory(name, dependencies, _ref => {
77
77
  if (!_2d(z)) {
78
78
  throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument');
79
79
  }
80
- var m = divideScalar(subtract(z[1], z[0]), subtract(y[1], y[0]));
81
- var xCoeff = multiplyScalar(multiplyScalar(m, m), y[0]);
82
- var yCoeff = unaryMinus(multiplyScalar(m, y[0]));
83
- var constant = x[1];
80
+ if (deepEqual(y, z)) {
81
+ throw new TypeError('LinePoint1 should not be same with LinePoint2');
82
+ }
83
+ var xCoeff = subtract(z[1], y[1]);
84
+ var yCoeff = subtract(y[0], z[0]);
85
+ var constant = subtract(multiplyScalar(z[0], y[1]), multiplyScalar(y[0], z[1]));
84
86
  return _distancePointLine2D(x[0], x[1], xCoeff, yCoeff, constant);
85
87
  } else {
86
88
  throw new TypeError('Invalid Arguments: Try again');
@@ -97,11 +99,13 @@ export var createDistance = /* #__PURE__ */factory(name, dependencies, _ref => {
97
99
  if (!_2d(z)) {
98
100
  throw new TypeError('Values of lineTwoPtX and lineTwoPtY should be numbers or BigNumbers');
99
101
  }
102
+ if (deepEqual(_objectToArray(y), _objectToArray(z))) {
103
+ throw new TypeError('LinePoint1 should not be same with LinePoint2');
104
+ }
100
105
  if ('pointX' in x && 'pointY' in x && 'lineOnePtX' in y && 'lineOnePtY' in y && 'lineTwoPtX' in z && 'lineTwoPtY' in z) {
101
- var m = divideScalar(subtract(z.lineTwoPtY, z.lineTwoPtX), subtract(y.lineOnePtY, y.lineOnePtX));
102
- var xCoeff = multiplyScalar(multiplyScalar(m, m), y.lineOnePtX);
103
- var yCoeff = unaryMinus(multiplyScalar(m, y.lineOnePtX));
104
- var constant = x.pointX;
106
+ var xCoeff = subtract(z.lineTwoPtY, y.lineOnePtY);
107
+ var yCoeff = subtract(y.lineOnePtX, z.lineTwoPtX);
108
+ var constant = subtract(multiplyScalar(z.lineTwoPtX, y.lineOnePtY), multiplyScalar(y.lineOnePtX, z.lineTwoPtY));
105
109
  return _distancePointLine2D(x.pointX, x.pointY, xCoeff, yCoeff, constant);
106
110
  } else {
107
111
  throw new TypeError('Key names do not match');
@@ -1,5 +1,5 @@
1
+ import { applyCallback } from '../../utils/applyCallback.js';
1
2
  import { filter, filterRegExp } from '../../utils/array.js';
2
- import { maxArgumentCount } from '../../utils/function.js';
3
3
  import { factory } from '../../utils/factory.js';
4
4
  var name = 'filter';
5
5
  var dependencies = ['typed'];
@@ -56,17 +56,8 @@ export var createFilter = /* #__PURE__ */factory(name, dependencies, _ref => {
56
56
  * @private
57
57
  */
58
58
  function _filterCallback(x, callback) {
59
- // figure out what number of arguments the callback function expects
60
- var args = maxArgumentCount(callback);
61
59
  return filter(x, function (value, index, array) {
62
60
  // invoke the callback function with the right number of arguments
63
- if (args === 1) {
64
- return callback(value);
65
- } else if (args === 2) {
66
- return callback(value, [index]);
67
- } else {
68
- // 3 or -1
69
- return callback(value, [index], array);
70
- }
61
+ return applyCallback(callback, value, [index], array, 'filter');
71
62
  });
72
63
  }
@@ -1,4 +1,4 @@
1
- import { maxArgumentCount } from '../../utils/function.js';
1
+ import { applyCallback } from '../../utils/applyCallback.js';
2
2
  import { forEach as forEachArray } from '../../utils/array.js';
3
3
  import { factory } from '../../utils/factory.js';
4
4
  var name = 'forEach';
@@ -39,14 +39,12 @@ export var createForEach = /* #__PURE__ */factory(name, dependencies, _ref => {
39
39
  });
40
40
 
41
41
  /**
42
- * forEach for a multi dimensional array
42
+ * forEach for a multidimensional array
43
43
  * @param {Array} array
44
44
  * @param {Function} callback
45
45
  * @private
46
46
  */
47
47
  function _forEach(array, callback) {
48
- // figure out what number of arguments the callback function expects
49
- var args = maxArgumentCount(callback);
50
48
  var recurse = function recurse(value, index) {
51
49
  if (Array.isArray(value)) {
52
50
  forEachArray(value, function (child, i) {
@@ -55,14 +53,7 @@ function _forEach(array, callback) {
55
53
  });
56
54
  } else {
57
55
  // invoke the callback function with the right number of arguments
58
- if (args === 1) {
59
- callback(value);
60
- } else if (args === 2) {
61
- callback(value, index);
62
- } else {
63
- // 3 or -1
64
- callback(value, index, array);
65
- }
56
+ return applyCallback(callback, value, index, array, 'forEach');
66
57
  }
67
58
  };
68
59
  recurse(array, []);
@@ -1,4 +1,4 @@
1
- import { maxArgumentCount } from '../../utils/function.js';
1
+ import { applyCallback } from '../../utils/applyCallback.js';
2
2
  import { factory } from '../../utils/factory.js';
3
3
  var name = 'map';
4
4
  var dependencies = ['typed'];
@@ -27,14 +27,9 @@ export var createMap = /* #__PURE__ */factory(name, dependencies, _ref => {
27
27
  * return value * value
28
28
  * }) // returns [1, 4, 9]
29
29
  *
30
- * // The calling convention for the callback can cause subtleties:
31
- * math.map([1, 2, 3], math.format)
32
- * // throws TypeError: map attempted to call 'format(1,[0])' but argument 2 of type Array does not match expected type number or function or Object or string or boolean
33
- * // [This happens because `format` _can_ take a second argument,
34
- * // but its semantics don't match that of the 2nd argument `map` provides]
35
- *
36
- * // To avoid this error, use a function that takes exactly the
37
- * // desired arguments:
30
+ * // The callback is normally called with three arguments:
31
+ * // callback(value, index, Array)
32
+ * // If you want to call with only one argument, use:
38
33
  * math.map([1, 2, 3], x => math.format(x)) // returns ['1', '2', '3']
39
34
  *
40
35
  * See also:
@@ -63,8 +58,6 @@ export var createMap = /* #__PURE__ */factory(name, dependencies, _ref => {
63
58
  * @private
64
59
  */
65
60
  function _map(array, callback) {
66
- // figure out what number of arguments the callback function expects
67
- var args = maxArgumentCount(callback);
68
61
  var recurse = function recurse(value, index) {
69
62
  if (Array.isArray(value)) {
70
63
  return value.map(function (child, i) {
@@ -72,33 +65,8 @@ function _map(array, callback) {
72
65
  return recurse(child, index.concat(i));
73
66
  });
74
67
  } else {
75
- try {
76
- // invoke the callback function with the right number of arguments
77
- if (args === 1) {
78
- return callback(value);
79
- } else if (args === 2) {
80
- return callback(value, index);
81
- } else {
82
- // 3 or -1
83
- return callback(value, index, array);
84
- }
85
- } catch (err) {
86
- // But maybe the arguments still weren't right
87
- if (err instanceof TypeError && 'data' in err && err.data.category === 'wrongType') {
88
- var newmsg = "map attempted to call '".concat(err.data.fn, "(").concat(value);
89
- var indexString = JSON.stringify(index);
90
- if (args === 2) {
91
- newmsg += ',' + indexString;
92
- } else if (args !== 1) {
93
- newmsg += ",".concat(indexString, ",").concat(array);
94
- }
95
- newmsg += ")' but argument ".concat(err.data.index + 1, " of type ");
96
- newmsg += "".concat(err.data.actual, " does not match expected type ");
97
- newmsg += err.data.expected.join(' or ');
98
- throw new TypeError(newmsg);
99
- }
100
- throw err;
101
- }
68
+ // invoke the callback function with the right number of arguments
69
+ return applyCallback(callback, value, index, array, 'map');
102
70
  }
103
71
  };
104
72
  return recurse(array, []);
@@ -55,7 +55,7 @@ export var createSqrtm = /* #__PURE__ */factory(name, dependencies, _ref => {
55
55
  *
56
56
  * Syntax:
57
57
  *
58
- * X = math.sqrtm(A)
58
+ * math.sqrtm(A)
59
59
  *
60
60
  * Examples:
61
61
  *