mathjs 9.4.5 → 10.0.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 (74) hide show
  1. package/HISTORY.md +35 -0
  2. package/bin/cli.js +0 -0
  3. package/docs/expressions/syntax.md +46 -43
  4. package/docs/reference/functions/format.md +5 -2
  5. package/docs/reference/functions/parser.md +4 -4
  6. package/docs/reference/functions/setCartesian.md +3 -1
  7. package/lib/browser/math.js +5 -5
  8. package/lib/browser/math.js.map +1 -1
  9. package/lib/cjs/constants.js +1 -1
  10. package/lib/cjs/core/function/config.js +1 -1
  11. package/lib/cjs/core/function/import.js +2 -1
  12. package/lib/cjs/entry/dependenciesAny/dependenciesParserClass.generated.js +2 -2
  13. package/lib/cjs/entry/dependenciesAny.generated.js +999 -999
  14. package/lib/cjs/entry/dependenciesNumber/dependenciesParserClass.generated.js +2 -2
  15. package/lib/cjs/entry/dependenciesNumber.generated.js +581 -581
  16. package/lib/cjs/entry/impureFunctionsAny.generated.js +314 -313
  17. package/lib/cjs/entry/impureFunctionsNumber.generated.js +223 -222
  18. package/lib/cjs/entry/mainAny.js +8 -8
  19. package/lib/cjs/entry/mainNumber.js +8 -8
  20. package/lib/cjs/entry/pureFunctionsAny.generated.js +1100 -1100
  21. package/lib/cjs/entry/pureFunctionsNumber.generated.js +375 -375
  22. package/lib/cjs/entry/typeChecks.js +12 -12
  23. package/lib/cjs/expression/Parser.js +6 -5
  24. package/lib/cjs/expression/embeddedDocs/function/set/setCartesian.js +1 -1
  25. package/lib/cjs/expression/function/parser.js +4 -4
  26. package/lib/cjs/expression/node/OperatorNode.js +9 -6
  27. package/lib/cjs/expression/operators.js +1 -1
  28. package/lib/cjs/expression/parse.js +42 -6
  29. package/lib/cjs/factoriesAny.js +660 -660
  30. package/lib/cjs/factoriesNumber.js +268 -237
  31. package/lib/cjs/function/set/setCartesian.js +3 -1
  32. package/lib/cjs/function/string/format.js +5 -2
  33. package/lib/cjs/header.js +2 -2
  34. package/lib/cjs/plain/bignumber/arithmetic.js +2 -2
  35. package/lib/cjs/plain/number/arithmetic.js +10 -10
  36. package/lib/cjs/plain/number/constants.js +1 -1
  37. package/lib/cjs/plain/number/logical.js +1 -1
  38. package/lib/cjs/plain/number/probability.js +2 -1
  39. package/lib/cjs/plain/number/trigonometry.js +1 -1
  40. package/lib/cjs/plain/number/utils.js +1 -1
  41. package/lib/cjs/type/unit/physicalConstants.js +1 -1
  42. package/lib/cjs/utils/array.js +14 -14
  43. package/lib/cjs/utils/bignumber/bitwise.js +1 -1
  44. package/lib/cjs/utils/customs.js +5 -5
  45. package/lib/cjs/utils/factory.js +3 -3
  46. package/lib/cjs/utils/function.js +1 -1
  47. package/lib/cjs/utils/is.js +23 -23
  48. package/lib/cjs/utils/latex.js +2 -1
  49. package/lib/cjs/utils/map.js +3 -3
  50. package/lib/cjs/utils/noop.js +1 -1
  51. package/lib/cjs/utils/number.js +10 -6
  52. package/lib/cjs/utils/object.js +8 -8
  53. package/lib/cjs/utils/snapshot.js +1 -1
  54. package/lib/cjs/utils/string.js +2 -2
  55. package/lib/cjs/version.js +1 -1
  56. package/lib/esm/core/function/import.js +2 -1
  57. package/lib/esm/entry/dependenciesAny/dependenciesParserClass.generated.js +2 -2
  58. package/lib/esm/entry/dependenciesAny.generated.js +270 -270
  59. package/lib/esm/entry/dependenciesNumber/dependenciesParserClass.generated.js +2 -2
  60. package/lib/esm/entry/dependenciesNumber.generated.js +163 -163
  61. package/lib/esm/entry/impureFunctionsAny.generated.js +301 -301
  62. package/lib/esm/entry/impureFunctionsNumber.generated.js +211 -211
  63. package/lib/esm/entry/pureFunctionsAny.generated.js +816 -816
  64. package/lib/esm/entry/pureFunctionsNumber.generated.js +234 -234
  65. package/lib/esm/expression/Parser.js +6 -5
  66. package/lib/esm/expression/embeddedDocs/function/set/setCartesian.js +1 -1
  67. package/lib/esm/expression/function/parser.js +4 -4
  68. package/lib/esm/expression/node/OperatorNode.js +9 -6
  69. package/lib/esm/expression/parse.js +42 -6
  70. package/lib/esm/function/set/setCartesian.js +3 -1
  71. package/lib/esm/function/string/format.js +5 -2
  72. package/lib/esm/version.js +1 -1
  73. package/package.json +16 -16
  74. package/types/index.d.ts +281 -77
@@ -153,18 +153,6 @@ Object.defineProperty(exports, "isNumber", {
153
153
  return _is.isNumber;
154
154
  }
155
155
  });
156
- Object.defineProperty(exports, "isString", {
157
- enumerable: true,
158
- get: function get() {
159
- return _is.isString;
160
- }
161
- });
162
- Object.defineProperty(exports, "isUndefined", {
163
- enumerable: true,
164
- get: function get() {
165
- return _is.isUndefined;
166
- }
167
- });
168
156
  Object.defineProperty(exports, "isObject", {
169
157
  enumerable: true,
170
158
  get: function get() {
@@ -219,12 +207,24 @@ Object.defineProperty(exports, "isSparseMatrix", {
219
207
  return _is.isSparseMatrix;
220
208
  }
221
209
  });
210
+ Object.defineProperty(exports, "isString", {
211
+ enumerable: true,
212
+ get: function get() {
213
+ return _is.isString;
214
+ }
215
+ });
222
216
  Object.defineProperty(exports, "isSymbolNode", {
223
217
  enumerable: true,
224
218
  get: function get() {
225
219
  return _is.isSymbolNode;
226
220
  }
227
221
  });
222
+ Object.defineProperty(exports, "isUndefined", {
223
+ enumerable: true,
224
+ get: function get() {
225
+ return _is.isUndefined;
226
+ }
227
+ });
228
228
  Object.defineProperty(exports, "isUnit", {
229
229
  enumerable: true,
230
230
  get: function get() {
@@ -10,9 +10,9 @@ var _factory = require("../utils/factory.js");
10
10
  var _map = require("../utils/map.js");
11
11
 
12
12
  var name = 'Parser';
13
- var dependencies = ['parse'];
13
+ var dependencies = ['evaluate'];
14
14
  var createParserClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
15
- var parse = _ref.parse;
15
+ var evaluate = _ref.evaluate;
16
16
 
17
17
  /**
18
18
  * @constructor Parser
@@ -44,7 +44,7 @@ var createParserClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies,
44
44
  * // define variables and functions
45
45
  * parser.evaluate('x = 7 / 2') // 3.5
46
46
  * parser.evaluate('x + 3') // 6.5
47
- * parser.evaluate('function f(x, y) = x^y') // f(x, y)
47
+ * parser.evaluate('f(x, y) = x^y') // f(x, y)
48
48
  * parser.evaluate('f(2, 3)') // 8
49
49
  *
50
50
  * // get and set variables and functions
@@ -81,14 +81,15 @@ var createParserClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies,
81
81
  Parser.prototype.isParser = true;
82
82
  /**
83
83
  * Parse and evaluate the given expression
84
- * @param {string} expr A string containing an expression, for example "2+3"
84
+ * @param {string | string[]} expr A string containing an expression,
85
+ * for example "2+3", or a list with expressions
85
86
  * @return {*} result The result, or undefined when the expression was empty
86
87
  * @throws {Error}
87
88
  */
88
89
 
89
90
  Parser.prototype.evaluate = function (expr) {
90
91
  // TODO: validate arguments
91
- return parse(expr).compile().evaluate(this.scope);
92
+ return evaluate(expr, this.scope);
92
93
  };
93
94
  /**
94
95
  * Get a variable (a function or variable) by name from the parsers scope.
@@ -8,7 +8,7 @@ var setCartesianDocs = {
8
8
  name: 'setCartesian',
9
9
  category: 'Set',
10
10
  syntax: ['setCartesian(set1, set2)'],
11
- description: 'Create the cartesian product of two (multi)sets. Multi-dimension arrays will be converted to single-dimension arrays before the operation.',
11
+ description: 'Create the cartesian product of two (multi)sets. Multi-dimension arrays will be converted to single-dimension arrays and the values will be sorted in ascending order before the operation.',
12
12
  examples: ['setCartesian([1, 2], [3, 4])'],
13
13
  seealso: ['setUnion', 'setIntersect', 'setDifference', 'setPowerset']
14
14
  };
@@ -31,10 +31,10 @@ var createParser = /* #__PURE__ */(0, _factory.factory)(name, dependencies, func
31
31
  * const d = parser.evaluate('cos(45 deg)') // 0.7071067811865476
32
32
  *
33
33
  * // define variables and functions
34
- * parser.evaluate('x = 7 / 2') // 3.5
35
- * parser.evaluate('x + 3') // 6.5
36
- * parser.evaluate('function f(x, y) = x^y') // f(x, y)
37
- * parser.evaluate('f(2, 3)') // 8
34
+ * parser.evaluate('x = 7 / 2') // 3.5
35
+ * parser.evaluate('x + 3') // 6.5
36
+ * parser.evaluate('f(x, y) = x^y') // f(x, y)
37
+ * parser.evaluate('f(2, 3)') // 8
38
38
  *
39
39
  * // get and set variables and functions
40
40
  * const x = parser.get('x') // 7
@@ -33,8 +33,9 @@ var createOperatorNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies
33
33
  * @param {string} fn Function name, for example 'add'
34
34
  * @param {Node[]} args Operator arguments
35
35
  * @param {boolean} [implicit] Is this an implicit multiplication?
36
+ * @param {boolean} [isPercentage] Is this an percentage Operation?
36
37
  */
37
- function OperatorNode(op, fn, args, implicit) {
38
+ function OperatorNode(op, fn, args, implicit, isPercentage) {
38
39
  if (!(this instanceof OperatorNode)) {
39
40
  throw new SyntaxError('Constructor must be called with the new operator');
40
41
  } // validate input
@@ -53,6 +54,7 @@ var createOperatorNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies
53
54
  }
54
55
 
55
56
  this.implicit = implicit === true;
57
+ this.isPercentage = isPercentage === true;
56
58
  this.op = op;
57
59
  this.fn = fn;
58
60
  this.args = args || [];
@@ -135,7 +137,7 @@ var createOperatorNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies
135
137
  args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this));
136
138
  }
137
139
 
138
- return new OperatorNode(this.op, this.fn, args, this.implicit);
140
+ return new OperatorNode(this.op, this.fn, args, this.implicit, this.isPercentage);
139
141
  };
140
142
  /**
141
143
  * Create a clone of this node, a shallow copy
@@ -144,7 +146,7 @@ var createOperatorNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies
144
146
 
145
147
 
146
148
  OperatorNode.prototype.clone = function () {
147
- return new OperatorNode(this.op, this.fn, this.args.slice(0), this.implicit);
149
+ return new OperatorNode(this.op, this.fn, this.args.slice(0), this.implicit, this.isPercentage);
148
150
  };
149
151
  /**
150
152
  * Check whether this is an unary OperatorNode:
@@ -478,20 +480,21 @@ var createOperatorNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies
478
480
  op: this.op,
479
481
  fn: this.fn,
480
482
  args: this.args,
481
- implicit: this.implicit
483
+ implicit: this.implicit,
484
+ isPercentage: this.isPercentage
482
485
  };
483
486
  };
484
487
  /**
485
488
  * Instantiate an OperatorNode from its JSON representation
486
489
  * @param {Object} json An object structured like
487
- * `{"mathjs": "OperatorNode", "op": "+", "fn": "add", "args": [...], "implicit": false}`,
490
+ * `{"mathjs": "OperatorNode", "op": "+", "fn": "add", "args": [...], "implicit": false, "isPercentage":false}`,
488
491
  * where mathjs is optional
489
492
  * @returns {OperatorNode}
490
493
  */
491
494
 
492
495
 
493
496
  OperatorNode.fromJSON = function (json) {
494
- return new OperatorNode(json.op, json.fn, json.args, json.implicit);
497
+ return new OperatorNode(json.op, json.fn, json.args, json.implicit, json.isPercentage);
495
498
  };
496
499
  /**
497
500
  * Get HTML representation.
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getPrecedence = getPrecedence;
7
6
  exports.getAssociativity = getAssociativity;
7
+ exports.getPrecedence = getPrecedence;
8
8
  exports.isAssociativeWith = isAssociativeWith;
9
9
  exports.properties = void 0;
10
10
 
@@ -1018,7 +1018,14 @@ var createParse = /* #__PURE__ */(0, _factory.factory)(name, dependencies, funct
1018
1018
  name = state.token;
1019
1019
  fn = operators[name];
1020
1020
  getTokenSkipNewline(state);
1021
- params = [node, parseMultiplyDivide(state)];
1021
+ var rightNode = parseMultiplyDivide(state);
1022
+
1023
+ if (rightNode.isPercentage) {
1024
+ params = [node, new OperatorNode('*', 'multiply', [node, rightNode])];
1025
+ } else {
1026
+ params = [node, rightNode];
1027
+ }
1028
+
1022
1029
  node = new OperatorNode(name, fn, params);
1023
1030
  }
1024
1031
 
@@ -1039,9 +1046,7 @@ var createParse = /* #__PURE__ */(0, _factory.factory)(name, dependencies, funct
1039
1046
  '*': 'multiply',
1040
1047
  '.*': 'dotMultiply',
1041
1048
  '/': 'divide',
1042
- './': 'dotDivide',
1043
- '%': 'mod',
1044
- mod: 'mod'
1049
+ './': 'dotDivide'
1045
1050
  };
1046
1051
 
1047
1052
  while (true) {
@@ -1099,7 +1104,7 @@ var createParse = /* #__PURE__ */(0, _factory.factory)(name, dependencies, funct
1099
1104
 
1100
1105
 
1101
1106
  function parseRule2(state) {
1102
- var node = parseUnary(state);
1107
+ var node = parsePercentage(state);
1103
1108
  var last = node;
1104
1109
  var tokenStates = [];
1105
1110
 
@@ -1120,7 +1125,7 @@ var createParse = /* #__PURE__ */(0, _factory.factory)(name, dependencies, funct
1120
1125
  // Rewind once and build the "number / number" node; the symbol will be consumed later
1121
1126
  (0, _extends2["default"])(state, tokenStates.pop());
1122
1127
  tokenStates.pop();
1123
- last = parseUnary(state);
1128
+ last = parsePercentage(state);
1124
1129
  node = new OperatorNode('/', 'divide', [node, last]);
1125
1130
  } else {
1126
1131
  // Not a match, so rewind
@@ -1140,6 +1145,37 @@ var createParse = /* #__PURE__ */(0, _factory.factory)(name, dependencies, funct
1140
1145
 
1141
1146
  return node;
1142
1147
  }
1148
+ /**
1149
+ * percentage or mod
1150
+ * @return {Node} node
1151
+ * @private
1152
+ */
1153
+
1154
+
1155
+ function parsePercentage(state) {
1156
+ var node, name, fn, params;
1157
+ node = parseUnary(state);
1158
+ var operators = {
1159
+ '%': 'mod',
1160
+ mod: 'mod'
1161
+ };
1162
+
1163
+ while ((0, _object.hasOwnProperty)(operators, state.token)) {
1164
+ name = state.token;
1165
+ fn = operators[name];
1166
+ getTokenSkipNewline(state);
1167
+
1168
+ if (name === '%' && state.tokenType === TOKENTYPE.DELIMITER && state.token !== '(') {
1169
+ // If the expression contains only %, then treat that as /100
1170
+ node = new OperatorNode('/', 'divide', [node, new ConstantNode(100)], false, true);
1171
+ } else {
1172
+ params = [node, parseUnary(state)];
1173
+ node = new OperatorNode(name, fn, params);
1174
+ }
1175
+ }
1176
+
1177
+ return node;
1178
+ }
1143
1179
  /**
1144
1180
  * Unary plus and minus, and logical and bitwise not
1145
1181
  * @return {Node} node