mathjs 13.2.3 → 14.0.1

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 (111) hide show
  1. package/HISTORY.md +24 -0
  2. package/lib/browser/math.js +1 -1
  3. package/lib/browser/math.js.LICENSE.txt +2 -10
  4. package/lib/browser/math.js.map +1 -1
  5. package/lib/cjs/core/function/import.js +4 -1
  6. package/lib/cjs/core/function/typed.js +1 -1
  7. package/lib/cjs/entry/dependenciesAny/dependenciesBitXor.generated.js +2 -0
  8. package/lib/cjs/entry/dependenciesAny/dependenciesDotDivide.generated.js +2 -0
  9. package/lib/cjs/entry/dependenciesAny/dependenciesDotPow.generated.js +2 -0
  10. package/lib/cjs/entry/dependenciesAny/dependenciesEqual.generated.js +2 -0
  11. package/lib/cjs/entry/dependenciesAny/dependenciesLarger.generated.js +2 -0
  12. package/lib/cjs/entry/dependenciesAny/dependenciesLargerEq.generated.js +2 -0
  13. package/lib/cjs/entry/dependenciesAny/dependenciesLog.generated.js +2 -0
  14. package/lib/cjs/entry/dependenciesAny/dependenciesSimplify.generated.js +2 -0
  15. package/lib/cjs/entry/dependenciesAny/dependenciesSmaller.generated.js +2 -0
  16. package/lib/cjs/entry/dependenciesAny/dependenciesSmallerEq.generated.js +2 -0
  17. package/lib/cjs/entry/dependenciesAny/dependenciesUnequal.generated.js +2 -0
  18. package/lib/cjs/entry/dependenciesAny/dependenciesXor.generated.js +2 -0
  19. package/lib/cjs/entry/dependenciesNumber/dependenciesSimplify.generated.js +2 -0
  20. package/lib/cjs/entry/impureFunctionsAny.generated.js +1 -0
  21. package/lib/cjs/entry/impureFunctionsNumber.generated.js +1 -0
  22. package/lib/cjs/entry/pureFunctionsAny.generated.js +11 -0
  23. package/lib/cjs/expression/embeddedDocs/function/arithmetic/ceil.js +2 -2
  24. package/lib/cjs/expression/embeddedDocs/function/arithmetic/fix.js +2 -2
  25. package/lib/cjs/expression/embeddedDocs/function/arithmetic/floor.js +2 -2
  26. package/lib/cjs/expression/parse.js +26 -36
  27. package/lib/cjs/function/algebra/derivative.js +64 -77
  28. package/lib/cjs/function/algebra/simplify.js +4 -3
  29. package/lib/cjs/function/algebra/simplifyConstant.js +5 -11
  30. package/lib/cjs/function/arithmetic/ceil.js +21 -2
  31. package/lib/cjs/function/arithmetic/dotDivide.js +4 -3
  32. package/lib/cjs/function/arithmetic/dotPow.js +4 -3
  33. package/lib/cjs/function/arithmetic/fix.js +24 -5
  34. package/lib/cjs/function/arithmetic/floor.js +21 -2
  35. package/lib/cjs/function/arithmetic/log.js +12 -4
  36. package/lib/cjs/function/arithmetic/pow.js +2 -2
  37. package/lib/cjs/function/arithmetic/round.js +3 -5
  38. package/lib/cjs/function/arithmetic/sign.js +1 -1
  39. package/lib/cjs/function/bitwise/bitXor.js +4 -3
  40. package/lib/cjs/function/logical/xor.js +4 -3
  41. package/lib/cjs/function/relational/equal.js +4 -3
  42. package/lib/cjs/function/relational/larger.js +4 -3
  43. package/lib/cjs/function/relational/largerEq.js +4 -3
  44. package/lib/cjs/function/relational/smaller.js +4 -3
  45. package/lib/cjs/function/relational/smallerEq.js +4 -3
  46. package/lib/cjs/function/relational/unequal.js +4 -3
  47. package/lib/cjs/function/utils/isInteger.js +1 -1
  48. package/lib/cjs/function/utils/isNegative.js +1 -1
  49. package/lib/cjs/function/utils/isPositive.js +1 -1
  50. package/lib/cjs/header.js +2 -2
  51. package/lib/cjs/type/bignumber/function/bignumber.js +2 -2
  52. package/lib/cjs/type/fraction/Fraction.js +4 -4
  53. package/lib/cjs/type/fraction/function/fraction.js +3 -0
  54. package/lib/cjs/type/matrix/utils/matAlgo07xSSf.js +39 -43
  55. package/lib/cjs/type/unit/Unit.js +1 -1
  56. package/lib/cjs/utils/string.js +2 -2
  57. package/lib/cjs/version.js +1 -1
  58. package/lib/esm/core/function/import.js +4 -1
  59. package/lib/esm/core/function/typed.js +1 -1
  60. package/lib/esm/entry/dependenciesAny/dependenciesBitXor.generated.js +2 -0
  61. package/lib/esm/entry/dependenciesAny/dependenciesDotDivide.generated.js +2 -0
  62. package/lib/esm/entry/dependenciesAny/dependenciesDotPow.generated.js +2 -0
  63. package/lib/esm/entry/dependenciesAny/dependenciesEqual.generated.js +2 -0
  64. package/lib/esm/entry/dependenciesAny/dependenciesLarger.generated.js +2 -0
  65. package/lib/esm/entry/dependenciesAny/dependenciesLargerEq.generated.js +2 -0
  66. package/lib/esm/entry/dependenciesAny/dependenciesLog.generated.js +2 -0
  67. package/lib/esm/entry/dependenciesAny/dependenciesSimplify.generated.js +2 -0
  68. package/lib/esm/entry/dependenciesAny/dependenciesSmaller.generated.js +2 -0
  69. package/lib/esm/entry/dependenciesAny/dependenciesSmallerEq.generated.js +2 -0
  70. package/lib/esm/entry/dependenciesAny/dependenciesUnequal.generated.js +2 -0
  71. package/lib/esm/entry/dependenciesAny/dependenciesXor.generated.js +2 -0
  72. package/lib/esm/entry/dependenciesNumber/dependenciesSimplify.generated.js +2 -0
  73. package/lib/esm/entry/impureFunctionsAny.generated.js +1 -0
  74. package/lib/esm/entry/impureFunctionsNumber.generated.js +1 -0
  75. package/lib/esm/entry/pureFunctionsAny.generated.js +11 -0
  76. package/lib/esm/expression/embeddedDocs/function/arithmetic/ceil.js +2 -2
  77. package/lib/esm/expression/embeddedDocs/function/arithmetic/fix.js +2 -2
  78. package/lib/esm/expression/embeddedDocs/function/arithmetic/floor.js +2 -2
  79. package/lib/esm/expression/parse.js +26 -36
  80. package/lib/esm/function/algebra/derivative.js +64 -77
  81. package/lib/esm/function/algebra/simplify.js +4 -3
  82. package/lib/esm/function/algebra/simplifyConstant.js +5 -11
  83. package/lib/esm/function/arithmetic/ceil.js +21 -2
  84. package/lib/esm/function/arithmetic/dotDivide.js +4 -3
  85. package/lib/esm/function/arithmetic/dotPow.js +4 -3
  86. package/lib/esm/function/arithmetic/fix.js +24 -5
  87. package/lib/esm/function/arithmetic/floor.js +21 -2
  88. package/lib/esm/function/arithmetic/log.js +12 -4
  89. package/lib/esm/function/arithmetic/pow.js +2 -2
  90. package/lib/esm/function/arithmetic/round.js +3 -5
  91. package/lib/esm/function/arithmetic/sign.js +1 -1
  92. package/lib/esm/function/bitwise/bitXor.js +4 -3
  93. package/lib/esm/function/logical/xor.js +4 -3
  94. package/lib/esm/function/relational/equal.js +4 -3
  95. package/lib/esm/function/relational/larger.js +4 -3
  96. package/lib/esm/function/relational/largerEq.js +4 -3
  97. package/lib/esm/function/relational/smaller.js +4 -3
  98. package/lib/esm/function/relational/smallerEq.js +4 -3
  99. package/lib/esm/function/relational/unequal.js +4 -3
  100. package/lib/esm/function/utils/isInteger.js +1 -1
  101. package/lib/esm/function/utils/isNegative.js +1 -1
  102. package/lib/esm/function/utils/isPositive.js +1 -1
  103. package/lib/esm/type/bignumber/function/bignumber.js +2 -2
  104. package/lib/esm/type/fraction/Fraction.js +4 -4
  105. package/lib/esm/type/fraction/function/fraction.js +3 -0
  106. package/lib/esm/type/matrix/utils/matAlgo07xSSf.js +39 -43
  107. package/lib/esm/type/unit/Unit.js +1 -1
  108. package/lib/esm/utils/string.js +2 -2
  109. package/lib/esm/version.js +1 -1
  110. package/package.json +13 -13
  111. package/types/index.d.ts +112 -23
@@ -60,9 +60,18 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
60
60
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
61
61
  simplify: true
62
62
  };
63
- var constNodes = {};
64
- constTag(constNodes, expr, variable.name);
65
- var res = _derivative(expr, constNodes);
63
+ var cache = new Map();
64
+ var variableName = variable.name;
65
+ function isConstCached(node) {
66
+ var cached = cache.get(node);
67
+ if (cached !== undefined) {
68
+ return cached;
69
+ }
70
+ var res = _isConst(isConstCached, node, variableName);
71
+ cache.set(node, res);
72
+ return res;
73
+ }
74
+ var res = _derivative(expr, isConstCached);
66
75
  return options.simplify ? simplify(res) : res;
67
76
  }
68
77
  function parseIdentifier(string) {
@@ -82,9 +91,8 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
82
91
  'Node, SymbolNode, ConstantNode': function (expr, variable, {order}) {
83
92
  let res = expr
84
93
  for (let i = 0; i < order; i++) {
85
- let constNodes = {}
86
- constTag(constNodes, expr, variable.name)
87
- res = _derivative(res, constNodes)
94
+ <create caching isConst>
95
+ res = _derivative(res, isConst)
88
96
  }
89
97
  return res
90
98
  }
@@ -127,56 +135,39 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
127
135
  });
128
136
 
129
137
  /**
130
- * Does a depth-first search on the expression tree to identify what Nodes
131
- * are constants (e.g. 2 + 2), and stores the ones that are constants in
132
- * constNodes. Classification is done as follows:
138
+ * Checks if a node is constants (e.g. 2 + 2).
139
+ * Accepts (usually memoized) version of self as the first parameter for recursive calls.
140
+ * Classification is done as follows:
133
141
  *
134
142
  * 1. ConstantNodes are constants.
135
143
  * 2. If there exists a SymbolNode, of which we are differentiating over,
136
144
  * in the subtree it is not constant.
137
145
  *
138
- * @param {Object} constNodes Holds the nodes that are constant
146
+ * @param {function} isConst Function that tells whether sub-expression is a constant
139
147
  * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node
140
148
  * @param {string} varName Variable that we are differentiating
141
149
  * @return {boolean} if node is constant
142
150
  */
143
- // TODO: can we rewrite constTag into a pure function?
144
- var constTag = typed('constTag', {
145
- 'Object, ConstantNode, string': function Object_ConstantNode_string(constNodes, node) {
146
- constNodes[node] = true;
151
+ var _isConst = typed('_isConst', {
152
+ 'function, ConstantNode, string': function function_ConstantNode_string() {
147
153
  return true;
148
154
  },
149
- 'Object, SymbolNode, string': function Object_SymbolNode_string(constNodes, node, varName) {
155
+ 'function, SymbolNode, string': function function_SymbolNode_string(isConst, node, varName) {
150
156
  // Treat other variables like constants. For reasoning, see:
151
157
  // https://en.wikipedia.org/wiki/Partial_derivative
152
- if (node.name !== varName) {
153
- constNodes[node] = true;
154
- return true;
155
- }
156
- return false;
158
+ return node.name !== varName;
157
159
  },
158
- 'Object, ParenthesisNode, string': function Object_ParenthesisNode_string(constNodes, node, varName) {
159
- return constTag(constNodes, node.content, varName);
160
+ 'function, ParenthesisNode, string': function function_ParenthesisNode_string(isConst, node, varName) {
161
+ return isConst(node.content, varName);
160
162
  },
161
- 'Object, FunctionAssignmentNode, string': function Object_FunctionAssignmentNode_string(constNodes, node, varName) {
163
+ 'function, FunctionAssignmentNode, string': function function_FunctionAssignmentNode_string(isConst, node, varName) {
162
164
  if (!node.params.includes(varName)) {
163
- constNodes[node] = true;
164
165
  return true;
165
166
  }
166
- return constTag(constNodes, node.expr, varName);
167
+ return isConst(node.expr, varName);
167
168
  },
168
- 'Object, FunctionNode | OperatorNode, string': function Object_FunctionNode__OperatorNode_string(constNodes, node, varName) {
169
- if (node.args.length > 0) {
170
- var isConst = constTag(constNodes, node.args[0], varName);
171
- for (var i = 1; i < node.args.length; ++i) {
172
- isConst = constTag(constNodes, node.args[i], varName) && isConst;
173
- }
174
- if (isConst) {
175
- constNodes[node] = true;
176
- return true;
177
- }
178
- }
179
- return false;
169
+ 'function, FunctionNode | OperatorNode, string': function function_FunctionNode__OperatorNode_string(isConst, node, varName) {
170
+ return node.args.every(arg => isConst(arg, varName));
180
171
  }
181
172
  });
182
173
 
@@ -184,30 +175,30 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
184
175
  * Applies differentiation rules.
185
176
  *
186
177
  * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node
187
- * @param {Object} constNodes Holds the nodes that are constant
178
+ * @param {function} isConst Function that tells if a node is constant
188
179
  * @return {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} The derivative of `expr`
189
180
  */
190
181
  var _derivative = typed('_derivative', {
191
- 'ConstantNode, Object': function ConstantNode_Object(node) {
182
+ 'ConstantNode, function': function ConstantNode_function() {
192
183
  return createConstantNode(0);
193
184
  },
194
- 'SymbolNode, Object': function SymbolNode_Object(node, constNodes) {
195
- if (constNodes[node] !== undefined) {
185
+ 'SymbolNode, function': function SymbolNode_function(node, isConst) {
186
+ if (isConst(node)) {
196
187
  return createConstantNode(0);
197
188
  }
198
189
  return createConstantNode(1);
199
190
  },
200
- 'ParenthesisNode, Object': function ParenthesisNode_Object(node, constNodes) {
201
- return new ParenthesisNode(_derivative(node.content, constNodes));
191
+ 'ParenthesisNode, function': function ParenthesisNode_function(node, isConst) {
192
+ return new ParenthesisNode(_derivative(node.content, isConst));
202
193
  },
203
- 'FunctionAssignmentNode, Object': function FunctionAssignmentNode_Object(node, constNodes) {
204
- if (constNodes[node] !== undefined) {
194
+ 'FunctionAssignmentNode, function': function FunctionAssignmentNode_function(node, isConst) {
195
+ if (isConst(node)) {
205
196
  return createConstantNode(0);
206
197
  }
207
- return _derivative(node.expr, constNodes);
198
+ return _derivative(node.expr, isConst);
208
199
  },
209
- 'FunctionNode, Object': function FunctionNode_Object(node, constNodes) {
210
- if (constNodes[node] !== undefined) {
200
+ 'FunctionNode, function': function FunctionNode_function(node, isConst) {
201
+ if (isConst(node)) {
211
202
  return createConstantNode(0);
212
203
  }
213
204
  var arg0 = node.args[0];
@@ -231,10 +222,7 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
231
222
  } else if (node.args.length === 2) {
232
223
  // Rearrange from nthRoot(x, a) -> x^(1/a)
233
224
  arg1 = new OperatorNode('/', 'divide', [createConstantNode(1), node.args[1]]);
234
-
235
- // Is a variable?
236
- constNodes[arg1] = constNodes[node.args[1]];
237
- return _derivative(new OperatorNode('^', 'pow', [arg0, arg1]), constNodes);
225
+ return _derivative(new OperatorNode('^', 'pow', [arg0, arg1]), isConst);
238
226
  }
239
227
  break;
240
228
  case 'log10':
@@ -245,20 +233,19 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
245
233
  // d/dx(log(x)) = 1 / x
246
234
  funcDerivative = arg0.clone();
247
235
  div = true;
248
- } else if (node.args.length === 1 && arg1 || node.args.length === 2 && constNodes[node.args[1]] !== undefined) {
236
+ } else if (node.args.length === 1 && arg1 || node.args.length === 2 && isConst(node.args[1])) {
249
237
  // d/dx(log(x, c)) = 1 / (x*ln(c))
250
238
  funcDerivative = new OperatorNode('*', 'multiply', [arg0.clone(), new FunctionNode('log', [arg1 || node.args[1]])]);
251
239
  div = true;
252
240
  } else if (node.args.length === 2) {
253
241
  // d/dx(log(f(x), g(x))) = d/dx(log(f(x)) / log(g(x)))
254
- return _derivative(new OperatorNode('/', 'divide', [new FunctionNode('log', [arg0]), new FunctionNode('log', [node.args[1]])]), constNodes);
242
+ return _derivative(new OperatorNode('/', 'divide', [new FunctionNode('log', [arg0]), new FunctionNode('log', [node.args[1]])]), isConst);
255
243
  }
256
244
  break;
257
245
  case 'pow':
258
246
  if (node.args.length === 2) {
259
- constNodes[arg1] = constNodes[node.args[1]];
260
247
  // Pass to pow operator node parser
261
- return _derivative(new OperatorNode('^', 'pow', [arg0, node.args[1]]), constNodes);
248
+ return _derivative(new OperatorNode('^', 'pow', [arg0, node.args[1]]), isConst);
262
249
  }
263
250
  break;
264
251
  case 'exp':
@@ -404,51 +391,51 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
404
391
  /* Apply chain rule to all functions:
405
392
  F(x) = f(g(x))
406
393
  F'(x) = g'(x)*f'(g(x)) */
407
- var chainDerivative = _derivative(arg0, constNodes);
394
+ var chainDerivative = _derivative(arg0, isConst);
408
395
  if (negative) {
409
396
  chainDerivative = new OperatorNode('-', 'unaryMinus', [chainDerivative]);
410
397
  }
411
398
  return new OperatorNode(op, func, [chainDerivative, funcDerivative]);
412
399
  },
413
- 'OperatorNode, Object': function OperatorNode_Object(node, constNodes) {
414
- if (constNodes[node] !== undefined) {
400
+ 'OperatorNode, function': function OperatorNode_function(node, isConst) {
401
+ if (isConst(node)) {
415
402
  return createConstantNode(0);
416
403
  }
417
404
  if (node.op === '+') {
418
405
  // d/dx(sum(f(x)) = sum(f'(x))
419
406
  return new OperatorNode(node.op, node.fn, node.args.map(function (arg) {
420
- return _derivative(arg, constNodes);
407
+ return _derivative(arg, isConst);
421
408
  }));
422
409
  }
423
410
  if (node.op === '-') {
424
411
  // d/dx(+/-f(x)) = +/-f'(x)
425
412
  if (node.isUnary()) {
426
- return new OperatorNode(node.op, node.fn, [_derivative(node.args[0], constNodes)]);
413
+ return new OperatorNode(node.op, node.fn, [_derivative(node.args[0], isConst)]);
427
414
  }
428
415
 
429
416
  // Linearity of differentiation, d/dx(f(x) +/- g(x)) = f'(x) +/- g'(x)
430
417
  if (node.isBinary()) {
431
- return new OperatorNode(node.op, node.fn, [_derivative(node.args[0], constNodes), _derivative(node.args[1], constNodes)]);
418
+ return new OperatorNode(node.op, node.fn, [_derivative(node.args[0], isConst), _derivative(node.args[1], isConst)]);
432
419
  }
433
420
  }
434
421
  if (node.op === '*') {
435
422
  // d/dx(c*f(x)) = c*f'(x)
436
423
  var constantTerms = node.args.filter(function (arg) {
437
- return constNodes[arg] !== undefined;
424
+ return isConst(arg);
438
425
  });
439
426
  if (constantTerms.length > 0) {
440
427
  var nonConstantTerms = node.args.filter(function (arg) {
441
- return constNodes[arg] === undefined;
428
+ return !isConst(arg);
442
429
  });
443
430
  var nonConstantNode = nonConstantTerms.length === 1 ? nonConstantTerms[0] : new OperatorNode('*', 'multiply', nonConstantTerms);
444
- var newArgs = constantTerms.concat(_derivative(nonConstantNode, constNodes));
431
+ var newArgs = constantTerms.concat(_derivative(nonConstantNode, isConst));
445
432
  return new OperatorNode('*', 'multiply', newArgs);
446
433
  }
447
434
 
448
435
  // Product Rule, d/dx(f(x)*g(x)) = f'(x)*g(x) + f(x)*g'(x)
449
436
  return new OperatorNode('+', 'add', node.args.map(function (argOuter) {
450
437
  return new OperatorNode('*', 'multiply', node.args.map(function (argInner) {
451
- return argInner === argOuter ? _derivative(argInner, constNodes) : argInner.clone();
438
+ return argInner === argOuter ? _derivative(argInner, isConst) : argInner.clone();
452
439
  }));
453
440
  }));
454
441
  }
@@ -457,31 +444,31 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
457
444
  var arg1 = node.args[1];
458
445
 
459
446
  // d/dx(f(x) / c) = f'(x) / c
460
- if (constNodes[arg1] !== undefined) {
461
- return new OperatorNode('/', 'divide', [_derivative(arg0, constNodes), arg1]);
447
+ if (isConst(arg1)) {
448
+ return new OperatorNode('/', 'divide', [_derivative(arg0, isConst), arg1]);
462
449
  }
463
450
 
464
451
  // Reciprocal Rule, d/dx(c / f(x)) = -c(f'(x)/f(x)^2)
465
- if (constNodes[arg0] !== undefined) {
466
- return new OperatorNode('*', 'multiply', [new OperatorNode('-', 'unaryMinus', [arg0]), new OperatorNode('/', 'divide', [_derivative(arg1, constNodes), new OperatorNode('^', 'pow', [arg1.clone(), createConstantNode(2)])])]);
452
+ if (isConst(arg0)) {
453
+ return new OperatorNode('*', 'multiply', [new OperatorNode('-', 'unaryMinus', [arg0]), new OperatorNode('/', 'divide', [_derivative(arg1, isConst), new OperatorNode('^', 'pow', [arg1.clone(), createConstantNode(2)])])]);
467
454
  }
468
455
 
469
456
  // Quotient rule, d/dx(f(x) / g(x)) = (f'(x)g(x) - f(x)g'(x)) / g(x)^2
470
- return new OperatorNode('/', 'divide', [new OperatorNode('-', 'subtract', [new OperatorNode('*', 'multiply', [_derivative(arg0, constNodes), arg1.clone()]), new OperatorNode('*', 'multiply', [arg0.clone(), _derivative(arg1, constNodes)])]), new OperatorNode('^', 'pow', [arg1.clone(), createConstantNode(2)])]);
457
+ return new OperatorNode('/', 'divide', [new OperatorNode('-', 'subtract', [new OperatorNode('*', 'multiply', [_derivative(arg0, isConst), arg1.clone()]), new OperatorNode('*', 'multiply', [arg0.clone(), _derivative(arg1, isConst)])]), new OperatorNode('^', 'pow', [arg1.clone(), createConstantNode(2)])]);
471
458
  }
472
459
  if (node.op === '^' && node.isBinary()) {
473
460
  var _arg = node.args[0];
474
461
  var _arg2 = node.args[1];
475
- if (constNodes[_arg] !== undefined) {
462
+ if (isConst(_arg)) {
476
463
  // If is secretly constant; 0^f(x) = 1 (in JS), 1^f(x) = 1
477
464
  if (isConstantNode(_arg) && (isZero(_arg.value) || equal(_arg.value, 1))) {
478
465
  return createConstantNode(0);
479
466
  }
480
467
 
481
468
  // d/dx(c^f(x)) = c^f(x)*ln(c)*f'(x)
482
- return new OperatorNode('*', 'multiply', [node, new OperatorNode('*', 'multiply', [new FunctionNode('log', [_arg.clone()]), _derivative(_arg2.clone(), constNodes)])]);
469
+ return new OperatorNode('*', 'multiply', [node, new OperatorNode('*', 'multiply', [new FunctionNode('log', [_arg.clone()]), _derivative(_arg2.clone(), isConst)])]);
483
470
  }
484
- if (constNodes[_arg2] !== undefined) {
471
+ if (isConst(_arg2)) {
485
472
  if (isConstantNode(_arg2)) {
486
473
  // If is secretly constant; f(x)^0 = 1 -> d/dx(1) = 0
487
474
  if (isZero(_arg2.value)) {
@@ -489,17 +476,17 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
489
476
  }
490
477
  // Ignore exponent; f(x)^1 = f(x)
491
478
  if (equal(_arg2.value, 1)) {
492
- return _derivative(_arg, constNodes);
479
+ return _derivative(_arg, isConst);
493
480
  }
494
481
  }
495
482
 
496
483
  // Elementary Power Rule, d/dx(f(x)^c) = c*f'(x)*f(x)^(c-1)
497
484
  var powMinusOne = new OperatorNode('^', 'pow', [_arg.clone(), new OperatorNode('-', 'subtract', [_arg2, createConstantNode(1)])]);
498
- return new OperatorNode('*', 'multiply', [_arg2.clone(), new OperatorNode('*', 'multiply', [_derivative(_arg, constNodes), powMinusOne])]);
485
+ return new OperatorNode('*', 'multiply', [_arg2.clone(), new OperatorNode('*', 'multiply', [_derivative(_arg, isConst), powMinusOne])]);
499
486
  }
500
487
 
501
488
  // Functional Power Rule, d/dx(f^g) = f^g*[f'*(g/f) + g'ln(f)]
502
- return new OperatorNode('*', 'multiply', [new OperatorNode('^', 'pow', [_arg.clone(), _arg2.clone()]), new OperatorNode('+', 'add', [new OperatorNode('*', 'multiply', [_derivative(_arg, constNodes), new OperatorNode('/', 'divide', [_arg2.clone(), _arg.clone()])]), new OperatorNode('*', 'multiply', [_derivative(_arg2, constNodes), new FunctionNode('log', [_arg.clone()])])])]);
489
+ return new OperatorNode('*', 'multiply', [new OperatorNode('^', 'pow', [_arg.clone(), _arg2.clone()]), new OperatorNode('+', 'add', [new OperatorNode('*', 'multiply', [_derivative(_arg, isConst), new OperatorNode('/', 'divide', [_arg2.clone(), _arg.clone()])]), new OperatorNode('*', 'multiply', [_derivative(_arg2, isConst), new FunctionNode('log', [_arg.clone()])])])]);
503
490
  }
504
491
  throw new Error('Cannot process operator "' + node.op + '" in derivative: ' + 'the operator is not supported, undefined, or the number of arguments passed to it are not supported');
505
492
  }
@@ -5,7 +5,7 @@ import { createUtil } from './simplify/util.js';
5
5
  import { hasOwnProperty } from '../../utils/object.js';
6
6
  import { createEmptyMap, createMap } from '../../utils/map.js';
7
7
  var name = 'simplify';
8
- var dependencies = ['typed', 'parse', 'equal', 'resolve', 'simplifyConstant', 'simplifyCore', 'AccessorNode', 'ArrayNode', 'ConstantNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'ParenthesisNode', 'SymbolNode'];
8
+ var dependencies = ['typed', 'parse', 'equal', 'resolve', 'simplifyConstant', 'simplifyCore', 'AccessorNode', 'ArrayNode', 'ConstantNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'ParenthesisNode', 'SymbolNode', 'replacer'];
9
9
  export var createSimplify = /* #__PURE__ */factory(name, dependencies, _ref => {
10
10
  var {
11
11
  typed,
@@ -22,7 +22,8 @@ export var createSimplify = /* #__PURE__ */factory(name, dependencies, _ref => {
22
22
  ObjectNode,
23
23
  OperatorNode,
24
24
  ParenthesisNode,
25
- SymbolNode
25
+ SymbolNode,
26
+ replacer
26
27
  } = _ref;
27
28
  var {
28
29
  hasProperty,
@@ -998,7 +999,7 @@ export var createSimplify = /* #__PURE__ */factory(name, dependencies, _ref => {
998
999
  var uniqueSets = [];
999
1000
  var unique = {};
1000
1001
  for (var i = 0; i < sets.length; i++) {
1001
- var s = JSON.stringify(sets[i]);
1002
+ var s = JSON.stringify(sets[i], replacer);
1002
1003
  if (!unique[s]) {
1003
1004
  unique[s] = true;
1004
1005
  uniqueSets.push(sets[i]);
@@ -194,17 +194,11 @@ export var createSimplifyConstant = /* #__PURE__ */factory(name, dependencies, _
194
194
  return new OperatorNode('-', 'unaryMinus', [n]);
195
195
  }
196
196
  function _fractionToNode(f) {
197
- var n;
198
- var vn = f.s * f.n;
199
- if (vn < 0) {
200
- n = new OperatorNode('-', 'unaryMinus', [new ConstantNode(-vn)]);
201
- } else {
202
- n = new ConstantNode(vn);
203
- }
204
- if (f.d === 1) {
205
- return n;
206
- }
207
- return new OperatorNode('/', 'divide', [n, new ConstantNode(f.d)]);
197
+ // note: we convert await from bigint values, because bigint values gives issues with divisions: 1n/2n=0n and not 0.5
198
+ var fromBigInt = value => config.number === 'BigNumber' && bignumber ? bignumber(value) : Number(value);
199
+ var numeratorValue = f.s * f.n;
200
+ var numeratorNode = numeratorValue < 0n ? new OperatorNode('-', 'unaryMinus', [new ConstantNode(-fromBigInt(numeratorValue))]) : new ConstantNode(fromBigInt(numeratorValue));
201
+ return f.d === 1n ? numeratorNode : new OperatorNode('/', 'divide', [numeratorNode, new ConstantNode(fromBigInt(f.d))]);
208
202
  }
209
203
 
210
204
  /* Handles constant indexing of ArrayNodes, matrices, and ObjectNodes */
@@ -69,6 +69,8 @@ export var createCeil = /* #__PURE__ */factory(name, dependencies, _ref2 => {
69
69
  *
70
70
  * math.ceil(x)
71
71
  * math.ceil(x, n)
72
+ * math.ceil(unit, valuelessUnit)
73
+ * math.ceil(unit, n, valuelessUnit)
72
74
  *
73
75
  * Examples:
74
76
  *
@@ -86,6 +88,12 @@ export var createCeil = /* #__PURE__ */factory(name, dependencies, _ref2 => {
86
88
  * math.ceil(c) // returns Complex 4 - 2i
87
89
  * math.ceil(c, 1) // returns Complex 3.3 - 2.7i
88
90
  *
91
+ * const unit = math.unit('3.241 cm')
92
+ * const cm = math.unit('cm')
93
+ * const mm = math.unit('mm')
94
+ * math.ceil(unit, 1, cm) // returns Unit 3.3 cm
95
+ * math.ceil(unit, 1, mm) // returns Unit 32.5 mm
96
+ *
89
97
  * math.ceil([3.2, 3.8, -4.7]) // returns Array [4, 4, -4]
90
98
  * math.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.3, 3.9, -4.7]
91
99
  *
@@ -93,9 +101,10 @@ export var createCeil = /* #__PURE__ */factory(name, dependencies, _ref2 => {
93
101
  *
94
102
  * floor, fix, round
95
103
  *
96
- * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
104
+ * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
97
105
  * @param {number | BigNumber | Array} [n=0] Number of decimals
98
- * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
106
+ * @param {Unit} [valuelessUnit] A valueless unit
107
+ * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value
99
108
  */
100
109
  return typed('ceil', {
101
110
  number: ceilNumber.signatures.number,
@@ -132,6 +141,16 @@ export var createCeil = /* #__PURE__ */factory(name, dependencies, _ref2 => {
132
141
  'Fraction, BigNumber': function Fraction_BigNumber(x, n) {
133
142
  return x.ceil(n.toNumber());
134
143
  },
144
+ 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) {
145
+ var valueless = x.toNumeric(unit);
146
+ return unit.multiply(self(valueless, n));
147
+ }),
148
+ 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),
149
+ 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => {
150
+ // deep map collection, skip zeros since ceil(0) = 0
151
+ return deepMap(x, value => self(value, n, unit), true);
152
+ }),
153
+ 'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),
135
154
  'Array | Matrix': typed.referToSelf(self => x => {
136
155
  // deep map collection, skip zeros since ceil(0) = 0
137
156
  return deepMap(x, self, true);
@@ -6,7 +6,7 @@ import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js';
6
6
  import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
7
7
  import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
8
8
  var name = 'dotDivide';
9
- var dependencies = ['typed', 'matrix', 'equalScalar', 'divideScalar', 'DenseMatrix', 'concat'];
9
+ var dependencies = ['typed', 'matrix', 'equalScalar', 'divideScalar', 'DenseMatrix', 'concat', 'SparseMatrix'];
10
10
  export var createDotDivide = /* #__PURE__ */factory(name, dependencies, _ref => {
11
11
  var {
12
12
  typed,
@@ -14,7 +14,8 @@ export var createDotDivide = /* #__PURE__ */factory(name, dependencies, _ref =>
14
14
  equalScalar,
15
15
  divideScalar,
16
16
  DenseMatrix,
17
- concat
17
+ concat,
18
+ SparseMatrix
18
19
  } = _ref;
19
20
  var matAlgo02xDS0 = createMatAlgo02xDS0({
20
21
  typed,
@@ -25,7 +26,7 @@ export var createDotDivide = /* #__PURE__ */factory(name, dependencies, _ref =>
25
26
  });
26
27
  var matAlgo07xSSf = createMatAlgo07xSSf({
27
28
  typed,
28
- DenseMatrix
29
+ SparseMatrix
29
30
  });
30
31
  var matAlgo11xS0s = createMatAlgo11xS0s({
31
32
  typed,
@@ -5,7 +5,7 @@ import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js';
5
5
  import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
6
6
  import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
7
7
  var name = 'dotPow';
8
- var dependencies = ['typed', 'equalScalar', 'matrix', 'pow', 'DenseMatrix', 'concat'];
8
+ var dependencies = ['typed', 'equalScalar', 'matrix', 'pow', 'DenseMatrix', 'concat', 'SparseMatrix'];
9
9
  export var createDotPow = /* #__PURE__ */factory(name, dependencies, _ref => {
10
10
  var {
11
11
  typed,
@@ -13,14 +13,15 @@ export var createDotPow = /* #__PURE__ */factory(name, dependencies, _ref => {
13
13
  matrix,
14
14
  pow,
15
15
  DenseMatrix,
16
- concat
16
+ concat,
17
+ SparseMatrix
17
18
  } = _ref;
18
19
  var matAlgo03xDSf = createMatAlgo03xDSf({
19
20
  typed
20
21
  });
21
22
  var matAlgo07xSSf = createMatAlgo07xSSf({
22
23
  typed,
23
- DenseMatrix
24
+ SparseMatrix
24
25
  });
25
26
  var matAlgo11xS0s = createMatAlgo11xS0s({
26
27
  typed,
@@ -50,6 +50,8 @@ export var createFix = /* #__PURE__ */factory(name, dependencies, _ref2 => {
50
50
  *
51
51
  * math.fix(x)
52
52
  * math.fix(x,n)
53
+ * math.fix(unit, valuelessUnit)
54
+ * math.fix(unit, n, valuelessUnit)
53
55
  *
54
56
  * Examples:
55
57
  *
@@ -67,6 +69,12 @@ export var createFix = /* #__PURE__ */factory(name, dependencies, _ref2 => {
67
69
  * math.fix(c) // returns Complex 3 - 2i
68
70
  * math.fix(c, 1) // returns Complex 3.2 -2.7i
69
71
  *
72
+ * const unit = math.unit('3.241 cm')
73
+ * const cm = math.unit('cm')
74
+ * const mm = math.unit('mm')
75
+ * math.fix(unit, 1, cm) // returns Unit 3.2 cm
76
+ * math.fix(unit, 1, mm) // returns Unit 32.4 mm
77
+ *
70
78
  * math.fix([3.2, 3.8, -4.7]) // returns Array [3, 3, -4]
71
79
  * math.fix([3.2, 3.8, -4.7], 1) // returns Array [3.2, 3.8, -4.7]
72
80
  *
@@ -74,9 +82,10 @@ export var createFix = /* #__PURE__ */factory(name, dependencies, _ref2 => {
74
82
  *
75
83
  * ceil, floor, round
76
84
  *
77
- * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
78
- * @param {number | BigNumber | Array} [n=0] Number of decimals
79
- * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
85
+ * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
86
+ * @param {number | BigNumber | Array} [n=0] Number of decimals
87
+ * @param {Unit} [valuelessUnit] A valueless unit
88
+ * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value
80
89
  */
81
90
  return typed('fix', {
82
91
  number: fixNumber.signatures.number,
@@ -98,11 +107,21 @@ export var createFix = /* #__PURE__ */factory(name, dependencies, _ref2 => {
98
107
  return x.isNegative() ? ceil(x, n) : floor(x, n);
99
108
  },
100
109
  Fraction: function Fraction(x) {
101
- return x.s < 0 ? x.ceil() : x.floor();
110
+ return x.s < 0n ? x.ceil() : x.floor();
102
111
  },
103
112
  'Fraction, number | BigNumber': function Fraction_number__BigNumber(x, n) {
104
- return x.s < 0 ? ceil(x, n) : floor(x, n);
113
+ return x.s < 0n ? ceil(x, n) : floor(x, n);
105
114
  },
115
+ 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) {
116
+ var valueless = x.toNumeric(unit);
117
+ return unit.multiply(self(valueless, n));
118
+ }),
119
+ 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),
120
+ 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => {
121
+ // deep map collection, skip zeros since fix(0) = 0
122
+ return deepMap(x, value => self(value, n, unit), true);
123
+ }),
124
+ 'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),
106
125
  'Array | Matrix': typed.referToSelf(self => x => {
107
126
  // deep map collection, skip zeros since fix(0) = 0
108
127
  return deepMap(x, self, true);
@@ -68,6 +68,8 @@ export var createFloor = /* #__PURE__ */factory(name, dependencies, _ref2 => {
68
68
  *
69
69
  * math.floor(x)
70
70
  * math.floor(x, n)
71
+ * math.floor(unit, valuelessUnit)
72
+ * math.floor(unit, n, valuelessUnit)
71
73
  *
72
74
  * Examples:
73
75
  *
@@ -85,6 +87,12 @@ export var createFloor = /* #__PURE__ */factory(name, dependencies, _ref2 => {
85
87
  * math.floor(c) // returns Complex 3 - 3i
86
88
  * math.floor(c, 1) // returns Complex 3.2 -2.8i
87
89
  *
90
+ * const unit = math.unit('3.241 cm')
91
+ * const cm = math.unit('cm')
92
+ * const mm = math.unit('mm')
93
+ * math.floor(unit, 1, cm) // returns Unit 3.2 cm
94
+ * math.floor(unit, 1, mm) // returns Unit 32.4 mm
95
+ *
88
96
  * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5]
89
97
  * math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8]
90
98
  *
@@ -96,9 +104,10 @@ export var createFloor = /* #__PURE__ */factory(name, dependencies, _ref2 => {
96
104
  *
97
105
  * ceil, fix, round
98
106
  *
99
- * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
107
+ * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
100
108
  * @param {number | BigNumber | Array} [n=0] Number of decimals
101
- * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
109
+ * @param {Unit} [valuelessUnit] A valueless unit
110
+ * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value
102
111
  */
103
112
  return typed('floor', {
104
113
  number: floorNumber.signatures.number,
@@ -135,6 +144,16 @@ export var createFloor = /* #__PURE__ */factory(name, dependencies, _ref2 => {
135
144
  'Fraction, BigNumber': function Fraction_BigNumber(x, n) {
136
145
  return x.floor(n.toNumber());
137
146
  },
147
+ 'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) {
148
+ var valueless = x.toNumeric(unit);
149
+ return unit.multiply(self(valueless, n));
150
+ }),
151
+ 'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),
152
+ 'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => {
153
+ // deep map collection, skip zeros since floor(0) = 0
154
+ return deepMap(x, value => self(value, n, unit), true);
155
+ }),
156
+ 'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),
138
157
  'Array | Matrix': typed.referToSelf(self => x => {
139
158
  // deep map collection, skip zeros since floor(0) = 0
140
159
  return deepMap(x, self, true);
@@ -1,10 +1,11 @@
1
1
  import { factory } from '../../utils/factory.js';
2
2
  import { logNumber } from '../../plain/number/index.js';
3
3
  var name = 'log';
4
- var dependencies = ['config', 'typed', 'divideScalar', 'Complex'];
4
+ var dependencies = ['config', 'typed', 'typeOf', 'divideScalar', 'Complex'];
5
5
  export var createLog = /* #__PURE__ */factory(name, dependencies, _ref => {
6
6
  var {
7
7
  typed,
8
+ typeOf,
8
9
  config,
9
10
  divideScalar,
10
11
  Complex
@@ -36,12 +37,12 @@ export var createLog = /* #__PURE__ */factory(name, dependencies, _ref => {
36
37
  *
37
38
  * exp, log2, log10, log1p
38
39
  *
39
- * @param {number | BigNumber | Complex} x
40
+ * @param {number | BigNumber | Fraction | Complex} x
40
41
  * Value for which to calculate the logarithm.
41
- * @param {number | BigNumber | Complex} [base=e]
42
+ * @param {number | BigNumber | Fraction | Complex} [base=e]
42
43
  * Optional base for the logarithm. If not provided, the natural
43
44
  * logarithm of `x` is calculated.
44
- * @return {number | BigNumber | Complex}
45
+ * @return {number | BigNumber | Fraction | Complex}
45
46
  * Returns the logarithm of `x`
46
47
  */
47
48
  return typed(name, {
@@ -66,6 +67,13 @@ export var createLog = /* #__PURE__ */factory(name, dependencies, _ref => {
66
67
  },
67
68
  'any, any': typed.referToSelf(self => (x, base) => {
68
69
  // calculate logarithm for a specified base, log(x, base)
70
+
71
+ if (typeOf(x) === 'Fraction' && typeOf(base) === 'Fraction') {
72
+ var result = x.log(base);
73
+ if (result !== null) {
74
+ return result;
75
+ }
76
+ }
69
77
  return divideScalar(self(x), self(base));
70
78
  })
71
79
  });
@@ -107,8 +107,8 @@ export var createPow = /* #__PURE__ */factory(name, dependencies, _ref => {
107
107
  var yFrac = fraction(y);
108
108
  var yNum = number(yFrac);
109
109
  if (y === yNum || Math.abs((y - yNum) / y) < 1e-14) {
110
- if (yFrac.d % 2 === 1) {
111
- return (yFrac.n % 2 === 0 ? 1 : -1) * Math.pow(-x, y);
110
+ if (yFrac.d % 2n === 1n) {
111
+ return (yFrac.n % 2n === 0n ? 1 : -1) * Math.pow(-x, y);
112
112
  }
113
113
  }
114
114
  } catch (ex) {