mathjs 15.0.0 → 15.1.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 (114) hide show
  1. package/HISTORY.md +22 -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/entry/dependenciesAny/dependenciesBernoulli.generated.js +25 -0
  7. package/lib/cjs/entry/dependenciesAny/dependenciesConstantNode.generated.js +2 -0
  8. package/lib/cjs/entry/dependenciesAny/dependenciesEqual.generated.js +0 -2
  9. package/lib/cjs/entry/dependenciesAny/dependenciesIsBounded.generated.js +17 -0
  10. package/lib/cjs/entry/dependenciesAny/dependenciesIsFinite.generated.js +21 -0
  11. package/lib/cjs/entry/dependenciesAny/dependenciesIsInteger.generated.js +2 -0
  12. package/lib/cjs/entry/dependenciesAny/dependenciesSimplifyConstant.generated.js +2 -0
  13. package/lib/cjs/entry/dependenciesAny/dependenciesUnitClass.generated.js +0 -2
  14. package/lib/cjs/entry/dependenciesAny/dependenciesZeta.generated.js +2 -0
  15. package/lib/cjs/entry/dependenciesAny.generated.js +21 -0
  16. package/lib/cjs/entry/dependenciesNumber/dependenciesBernoulli.generated.js +21 -0
  17. package/lib/cjs/entry/dependenciesNumber/dependenciesConstantNode.generated.js +2 -0
  18. package/lib/cjs/entry/dependenciesNumber/dependenciesIsBounded.generated.js +17 -0
  19. package/lib/cjs/entry/dependenciesNumber/dependenciesIsFinite.generated.js +21 -0
  20. package/lib/cjs/entry/dependenciesNumber/dependenciesSimplifyConstant.generated.js +2 -0
  21. package/lib/cjs/entry/dependenciesNumber/dependenciesZeta.generated.js +2 -0
  22. package/lib/cjs/entry/dependenciesNumber.generated.js +21 -0
  23. package/lib/cjs/entry/impureFunctionsAny.generated.js +217 -212
  24. package/lib/cjs/entry/impureFunctionsNumber.generated.js +82 -77
  25. package/lib/cjs/entry/pureFunctionsAny.generated.js +668 -652
  26. package/lib/cjs/entry/pureFunctionsNumber.generated.js +155 -140
  27. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +6 -0
  28. package/lib/cjs/expression/embeddedDocs/function/combinatorics/stirlingS2.js +2 -2
  29. package/lib/cjs/expression/embeddedDocs/function/probability/bernoulli.js +14 -0
  30. package/lib/cjs/expression/embeddedDocs/function/utils/isBounded.js +14 -0
  31. package/lib/cjs/expression/embeddedDocs/function/utils/isFinite.js +14 -0
  32. package/lib/cjs/expression/embeddedDocs/function/utils/isNaN.js +1 -1
  33. package/lib/cjs/expression/embeddedDocs/function/utils/isNumeric.js +1 -1
  34. package/lib/cjs/expression/node/AccessorNode.js +36 -7
  35. package/lib/cjs/expression/node/ConstantNode.js +4 -4
  36. package/lib/cjs/expression/node/FunctionNode.js +20 -5
  37. package/lib/cjs/expression/parse.js +36 -12
  38. package/lib/cjs/factoriesAny.js +21 -0
  39. package/lib/cjs/factoriesNumber.js +23 -2
  40. package/lib/cjs/function/algebra/simplifyConstant.js +3 -2
  41. package/lib/cjs/function/arithmetic/nthRoots.js +5 -1
  42. package/lib/cjs/function/probability/bernoulli.js +108 -0
  43. package/lib/cjs/function/relational/equal.js +2 -3
  44. package/lib/cjs/function/special/zeta.js +3 -2
  45. package/lib/cjs/function/utils/isBounded.js +54 -0
  46. package/lib/cjs/function/utils/isFinite.js +50 -0
  47. package/lib/cjs/function/utils/isInteger.js +7 -15
  48. package/lib/cjs/function/utils/isNaN.js +1 -1
  49. package/lib/cjs/function/utils/isNumeric.js +1 -1
  50. package/lib/cjs/header.js +2 -2
  51. package/lib/cjs/json/replacer.js +1 -1
  52. package/lib/cjs/plain/number/probability.js +2 -2
  53. package/lib/cjs/plain/number/trigonometry.js +1 -1
  54. package/lib/cjs/type/fraction/function/fraction.js +1 -1
  55. package/lib/cjs/type/matrix/SparseMatrix.js +24 -4
  56. package/lib/cjs/type/unit/Unit.js +12 -8
  57. package/lib/cjs/utils/number.js +7 -7
  58. package/lib/cjs/utils/optimizeCallback.js +13 -1
  59. package/lib/cjs/version.js +1 -1
  60. package/lib/esm/entry/dependenciesAny/dependenciesBernoulli.generated.js +18 -0
  61. package/lib/esm/entry/dependenciesAny/dependenciesConstantNode.generated.js +2 -0
  62. package/lib/esm/entry/dependenciesAny/dependenciesEqual.generated.js +0 -2
  63. package/lib/esm/entry/dependenciesAny/dependenciesIsBounded.generated.js +10 -0
  64. package/lib/esm/entry/dependenciesAny/dependenciesIsFinite.generated.js +14 -0
  65. package/lib/esm/entry/dependenciesAny/dependenciesIsInteger.generated.js +2 -0
  66. package/lib/esm/entry/dependenciesAny/dependenciesSimplifyConstant.generated.js +2 -0
  67. package/lib/esm/entry/dependenciesAny/dependenciesUnitClass.generated.js +0 -2
  68. package/lib/esm/entry/dependenciesAny/dependenciesZeta.generated.js +2 -0
  69. package/lib/esm/entry/dependenciesAny.generated.js +3 -0
  70. package/lib/esm/entry/dependenciesNumber/dependenciesBernoulli.generated.js +14 -0
  71. package/lib/esm/entry/dependenciesNumber/dependenciesConstantNode.generated.js +2 -0
  72. package/lib/esm/entry/dependenciesNumber/dependenciesIsBounded.generated.js +10 -0
  73. package/lib/esm/entry/dependenciesNumber/dependenciesIsFinite.generated.js +14 -0
  74. package/lib/esm/entry/dependenciesNumber/dependenciesSimplifyConstant.generated.js +2 -0
  75. package/lib/esm/entry/dependenciesNumber/dependenciesZeta.generated.js +2 -0
  76. package/lib/esm/entry/dependenciesNumber.generated.js +3 -0
  77. package/lib/esm/entry/impureFunctionsAny.generated.js +219 -214
  78. package/lib/esm/entry/impureFunctionsNumber.generated.js +84 -79
  79. package/lib/esm/entry/pureFunctionsAny.generated.js +665 -649
  80. package/lib/esm/entry/pureFunctionsNumber.generated.js +154 -139
  81. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +6 -0
  82. package/lib/esm/expression/embeddedDocs/function/combinatorics/stirlingS2.js +2 -2
  83. package/lib/esm/expression/embeddedDocs/function/probability/bernoulli.js +8 -0
  84. package/lib/esm/expression/embeddedDocs/function/utils/isBounded.js +8 -0
  85. package/lib/esm/expression/embeddedDocs/function/utils/isFinite.js +8 -0
  86. package/lib/esm/expression/embeddedDocs/function/utils/isNaN.js +1 -1
  87. package/lib/esm/expression/embeddedDocs/function/utils/isNumeric.js +1 -1
  88. package/lib/esm/expression/node/AccessorNode.js +36 -7
  89. package/lib/esm/expression/node/ConstantNode.js +4 -4
  90. package/lib/esm/expression/node/FunctionNode.js +20 -5
  91. package/lib/esm/expression/parse.js +36 -12
  92. package/lib/esm/factoriesAny.js +3 -0
  93. package/lib/esm/factoriesNumber.js +3 -0
  94. package/lib/esm/function/algebra/simplifyConstant.js +3 -2
  95. package/lib/esm/function/arithmetic/nthRoots.js +5 -1
  96. package/lib/esm/function/probability/bernoulli.js +102 -0
  97. package/lib/esm/function/relational/equal.js +2 -3
  98. package/lib/esm/function/special/zeta.js +3 -2
  99. package/lib/esm/function/utils/isBounded.js +48 -0
  100. package/lib/esm/function/utils/isFinite.js +44 -0
  101. package/lib/esm/function/utils/isInteger.js +7 -15
  102. package/lib/esm/function/utils/isNaN.js +1 -1
  103. package/lib/esm/function/utils/isNumeric.js +1 -1
  104. package/lib/esm/json/replacer.js +1 -1
  105. package/lib/esm/plain/number/probability.js +2 -2
  106. package/lib/esm/plain/number/trigonometry.js +1 -1
  107. package/lib/esm/type/fraction/function/fraction.js +1 -1
  108. package/lib/esm/type/matrix/SparseMatrix.js +24 -4
  109. package/lib/esm/type/unit/Unit.js +12 -8
  110. package/lib/esm/utils/number.js +7 -7
  111. package/lib/esm/utils/optimizeCallback.js +13 -1
  112. package/lib/esm/version.js +1 -1
  113. package/package.json +8 -8
  114. package/types/index.d.ts +535 -223
@@ -100,7 +100,7 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
100
100
  * the arguments, typically a SymbolNode or AccessorNode
101
101
  * @param {./Node[]} args
102
102
  */
103
- constructor(fn, args) {
103
+ constructor(fn, args, optional) {
104
104
  super();
105
105
  if (typeof fn === 'string') {
106
106
  fn = new SymbolNode(fn);
@@ -111,8 +111,13 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
111
111
  if (!Array.isArray(args) || !args.every(isNode)) {
112
112
  throw new TypeError('Array containing Nodes expected for parameter "args"');
113
113
  }
114
+ var optionalType = typeof optional;
115
+ if (!(optionalType === 'undefined' || optionalType === 'boolean')) {
116
+ throw new TypeError('optional flag, if specified, must be boolean');
117
+ }
114
118
  this.fn = fn;
115
119
  this.args = args || [];
120
+ this.optional = !!optional;
116
121
  }
117
122
 
118
123
  // readonly property name
@@ -142,6 +147,7 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
142
147
  _compile(math, argNames) {
143
148
  // compile arguments
144
149
  var evalArgs = this.args.map(arg => arg._compile(math, argNames));
150
+ var fromOptionalChaining = this.optional || isAccessorNode(this.fn) && this.fn.optionalChaining;
145
151
  if (isSymbolNode(this.fn)) {
146
152
  var _name = this.fn.name;
147
153
  if (!argNames[_name]) {
@@ -155,10 +161,8 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
155
161
  value = scope.get(_name);
156
162
  } else if (_name in math) {
157
163
  value = getSafeProperty(math, _name);
158
- } else {
159
- return FunctionNode.onUndefinedFunction(_name);
160
- }
161
- if (typeof value === 'function') {
164
+ } else if (fromOptionalChaining) value = undefined;else return FunctionNode.onUndefinedFunction(_name);
165
+ if (typeof value === 'function' || fromOptionalChaining && value === undefined) {
162
166
  return value;
163
167
  }
164
168
  throw new TypeError("'".concat(_name, "' is not a function; its value is:\n ").concat(strin(value)));
@@ -185,17 +189,20 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
185
189
  case 0:
186
190
  return function evalFunctionNode(scope, args, context) {
187
191
  var fn = resolveFn(scope);
192
+ if (fromOptionalChaining && fn === undefined) return undefined;
188
193
  return fn();
189
194
  };
190
195
  case 1:
191
196
  return function evalFunctionNode(scope, args, context) {
192
197
  var fn = resolveFn(scope);
198
+ if (fromOptionalChaining && fn === undefined) return undefined;
193
199
  var evalArg0 = evalArgs[0];
194
200
  return fn(evalArg0(scope, args, context));
195
201
  };
196
202
  case 2:
197
203
  return function evalFunctionNode(scope, args, context) {
198
204
  var fn = resolveFn(scope);
205
+ if (fromOptionalChaining && fn === undefined) return undefined;
199
206
  var evalArg0 = evalArgs[0];
200
207
  var evalArg1 = evalArgs[1];
201
208
  return fn(evalArg0(scope, args, context), evalArg1(scope, args, context));
@@ -203,6 +210,7 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
203
210
  default:
204
211
  return function evalFunctionNode(scope, args, context) {
205
212
  var fn = resolveFn(scope);
213
+ if (fromOptionalChaining && fn === undefined) return undefined;
206
214
  var values = evalArgs.map(evalArg => evalArg(scope, args, context));
207
215
  return fn(...values);
208
216
  };
@@ -213,6 +221,7 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
213
221
  var _rawArgs = this.args;
214
222
  return function evalFunctionNode(scope, args, context) {
215
223
  var fn = getSafeProperty(args, _name);
224
+ if (fromOptionalChaining && fn === undefined) return undefined;
216
225
  if (typeof fn !== 'function') {
217
226
  throw new TypeError("Argument '".concat(_name, "' was not a function; received: ").concat(strin(fn)));
218
227
  }
@@ -234,6 +243,11 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
234
243
  var _rawArgs2 = this.args;
235
244
  return function evalFunctionNode(scope, args, context) {
236
245
  var object = evalObject(scope, args, context);
246
+
247
+ // Optional chaining: if the base object is nullish, short-circuit to undefined
248
+ if (fromOptionalChaining && (object == null || object[prop] === undefined)) {
249
+ return undefined;
250
+ }
237
251
  var fn = getSafeMethod(object, prop);
238
252
  if (fn !== null && fn !== void 0 && fn.rawArgs) {
239
253
  // "Raw" evaluation
@@ -253,6 +267,7 @@ export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref
253
267
  var _rawArgs3 = this.args;
254
268
  return function evalFunctionNode(scope, args, context) {
255
269
  var fn = evalFn(scope, args, context);
270
+ if (fromOptionalChaining && fn === undefined) return undefined;
256
271
  if (typeof fn !== 'function') {
257
272
  throw new TypeError("Expression '".concat(fnExpr, "' did not evaluate to a function; value is:") + "\n ".concat(strin(fn)));
258
273
  }
@@ -129,6 +129,7 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
129
129
  '=': true,
130
130
  ':': true,
131
131
  '?': true,
132
+ '?.': true,
132
133
  '??': true,
133
134
  '==': true,
134
135
  '!=': true,
@@ -295,7 +296,9 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
295
296
  }
296
297
 
297
298
  // check for delimiters consisting of 2 characters
298
- if (c2.length === 2 && DELIMITERS[c2]) {
299
+ // Special case: the check for '?.' is to prevent a case like 'a?.3:.7' from being interpreted as optional chaining
300
+ // TODO: refactor the tokenization into some better way to deal with cases like 'a?.3:.7', see https://github.com/josdejong/mathjs/pull/3584
301
+ if (c2.length === 2 && DELIMITERS[c2] && (c2 !== '?.' || !parse.isDigit(state.expression.charAt(state.index + 2)))) {
299
302
  state.tokenType = TOKENTYPE.DELIMITER;
300
303
  state.token = c2;
301
304
  next(state);
@@ -640,6 +643,9 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
640
643
  return new AssignmentNode(new SymbolNode(name), value);
641
644
  } else if (isAccessorNode(node)) {
642
645
  // parse a matrix subset assignment like 'A[1,2] = 4'
646
+ if (node.optionalChaining) {
647
+ throw createSyntaxError(state, 'Cannot assign to optional chain');
648
+ }
643
649
  getTokenSkipNewline(state);
644
650
  value = parseAssignment(state);
645
651
  return new AssignmentNode(node.object, node.index, value);
@@ -1254,9 +1260,9 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
1254
1260
 
1255
1261
  /**
1256
1262
  * parse accessors:
1257
- * - function invocation in round brackets (...), for example sqrt(2)
1258
- * - index enclosed in square brackets [...], for example A[2,3]
1259
- * - dot notation for properties, like foo.bar
1263
+ * - function invocation in round brackets (...), for example sqrt(2) or sqrt?.(2) with optional chaining
1264
+ * - index enclosed in square brackets [...], for example A[2,3] or A?.[2,3] with optional chaining
1265
+ * - dot notation for properties, like foo.bar or foo?.bar with optional chaining
1260
1266
  * @param {Object} state
1261
1267
  * @param {Node} node Node on which to apply the parameters. If there
1262
1268
  * are no parameters in the expression, the node
@@ -1268,12 +1274,27 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
1268
1274
  */
1269
1275
  function parseAccessors(state, node, types) {
1270
1276
  var params;
1271
- while ((state.token === '(' || state.token === '[' || state.token === '.') && (!types || types.includes(state.token))) {
1277
+
1278
+ // Iterate and handle chained accessors, including repeated optional chaining
1279
+ while (true) {
1272
1280
  // eslint-disable-line no-unmodified-loop-condition
1281
+ // Track whether an optional chaining operator precedes the next accessor
1282
+ var optional = false;
1283
+
1284
+ // Consume an optional chaining operator if present
1285
+ if (state.token === '?.') {
1286
+ optional = true;
1287
+ // consume the '?.' token
1288
+ getToken(state);
1289
+ }
1290
+ var hasNextAccessor = (state.token === '(' || state.token === '[' || state.token === '.') && (!types || types.includes(state.token));
1291
+ if (!(optional || hasNextAccessor)) {
1292
+ break;
1293
+ }
1273
1294
  params = [];
1274
1295
  if (state.token === '(') {
1275
- if (isSymbolNode(node) || isAccessorNode(node)) {
1276
- // function invocation like fn(2, 3) or obj.fn(2, 3)
1296
+ if (optional || isSymbolNode(node) || isAccessorNode(node)) {
1297
+ // function invocation: fn(2, 3) or obj.fn(2, 3) or (anything)?.(2, 3)
1277
1298
  openParams(state);
1278
1299
  getToken(state);
1279
1300
  if (state.token !== ')') {
@@ -1291,7 +1312,7 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
1291
1312
  }
1292
1313
  closeParams(state);
1293
1314
  getToken(state);
1294
- node = new FunctionNode(node, params);
1315
+ node = new FunctionNode(node, params, optional);
1295
1316
  } else {
1296
1317
  // implicit multiplication like (2+3)(4+5) or sqrt(2)(1+2)
1297
1318
  // don't parse it here but let it be handled by parseImplicitMultiplication
@@ -1317,18 +1338,21 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
1317
1338
  }
1318
1339
  closeParams(state);
1319
1340
  getToken(state);
1320
- node = new AccessorNode(node, new IndexNode(params));
1341
+ node = new AccessorNode(node, new IndexNode(params), optional);
1321
1342
  } else {
1322
1343
  // dot notation like variable.prop
1323
- getToken(state);
1344
+ // consume the `.` (if it was ?., already consumed):
1345
+ if (!optional) getToken(state);
1324
1346
  var isPropertyName = state.tokenType === TOKENTYPE.SYMBOL || state.tokenType === TOKENTYPE.DELIMITER && state.token in NAMED_DELIMITERS;
1325
1347
  if (!isPropertyName) {
1326
- throw createSyntaxError(state, 'Property name expected after dot');
1348
+ var message = 'Property name expected after ';
1349
+ message += optional ? 'optional chain' : 'dot';
1350
+ throw createSyntaxError(state, message);
1327
1351
  }
1328
1352
  params.push(new ConstantNode(state.token));
1329
1353
  getToken(state);
1330
1354
  var dotNotation = true;
1331
- node = new AccessorNode(node, new IndexNode(params, dotNotation));
1355
+ node = new AccessorNode(node, new IndexNode(params, dotNotation), optional);
1332
1356
  }
1333
1357
  }
1334
1358
  return node;
@@ -14,6 +14,8 @@ export { createHasNumericValue } from './function/utils/hasNumericValue.js';
14
14
  export { createIsPositive } from './function/utils/isPositive.js';
15
15
  export { createIsZero } from './function/utils/isZero.js';
16
16
  export { createIsNaN } from './function/utils/isNaN.js';
17
+ export { createIsBounded } from './function/utils/isBounded.js';
18
+ export { createIsFinite } from './function/utils/isFinite.js';
17
19
  export { createTypeOf } from './function/utils/typeOf.js';
18
20
  export { createEqualScalar } from './function/relational/equalScalar.js';
19
21
  export { createSparseMatrixClass } from './type/matrix/SparseMatrix.js';
@@ -242,6 +244,7 @@ export { createVariance } from './function/statistics/variance.js';
242
244
  export { createQuantileSeq } from './function/statistics/quantileSeq.js';
243
245
  export { createStd } from './function/statistics/std.js';
244
246
  export { createCorr } from './function/statistics/corr.js';
247
+ export { createBernoulli } from './function/probability/bernoulli.js';
245
248
  export { createCombinations } from './function/probability/combinations.js';
246
249
  export { createCombinationsWithRep } from './function/probability/combinationsWithRep.js';
247
250
  export { createGamma } from './function/probability/gamma.js';
@@ -136,6 +136,7 @@ export var createSubset = /* #__PURE__ */factory('subset', [], () => noSubset);
136
136
  export { createPartitionSelect } from './function/matrix/partitionSelect.js';
137
137
 
138
138
  // probability
139
+ export { createBernoulli } from './function/probability/bernoulli.js';
139
140
  export var createCombinations = createNumberFactory('combinations', combinationsNumber);
140
141
  export var createGamma = createNumberFactory('gamma', gammaNumber);
141
142
  export var createLgamma = createNumberFactory('lgamma', lgammaNumber);
@@ -236,6 +237,8 @@ export { createHasNumericValue } from './function/utils/hasNumericValue.js';
236
237
  export var createIsPositive = /* #__PURE__ */createNumberFactory('isPositive', isPositiveNumber);
237
238
  export var createIsZero = /* #__PURE__ */createNumberFactory('isZero', isZeroNumber);
238
239
  export var createIsNaN = /* #__PURE__ */createNumberFactory('isNaN', isNaNNumber);
240
+ export { createIsBounded } from './function/utils/isBounded.js';
241
+ export { createIsFinite } from './function/utils/isFinite.js';
239
242
  export { createTypeOf } from './function/utils/typeOf.js';
240
243
  export { createIsPrime } from './function/utils/isPrime.js';
241
244
  export { createNumeric } from './function/utils/numeric.js';
@@ -4,13 +4,14 @@ import { safeNumberType } from '../../utils/number.js';
4
4
  import { createUtil } from './simplify/util.js';
5
5
  import { noBignumber, noFraction } from '../../utils/noop.js';
6
6
  var name = 'simplifyConstant';
7
- var dependencies = ['typed', 'config', 'mathWithTransform', 'matrix', '?fraction', '?bignumber', 'AccessorNode', 'ArrayNode', 'ConstantNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'SymbolNode'];
7
+ var dependencies = ['typed', 'config', 'mathWithTransform', 'matrix', 'isBounded', '?fraction', '?bignumber', 'AccessorNode', 'ArrayNode', 'ConstantNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'SymbolNode'];
8
8
  export var createSimplifyConstant = /* #__PURE__ */factory(name, dependencies, _ref => {
9
9
  var {
10
10
  typed,
11
11
  config,
12
12
  mathWithTransform,
13
13
  matrix,
14
+ isBounded,
14
15
  fraction,
15
16
  bignumber,
16
17
  AccessorNode,
@@ -128,7 +129,7 @@ export var createSimplifyConstant = /* #__PURE__ */factory(name, dependencies, _
128
129
  // and when both numerator and denominator are small enough
129
130
  function _exactFraction(n, options) {
130
131
  var exactFractions = options && options.exactFractions !== false;
131
- if (exactFractions && isFinite(n) && fraction) {
132
+ if (exactFractions && isBounded(n) && fraction) {
132
133
  var f = fraction(n);
133
134
  var fractionsLimit = options && typeof options.fractionsLimit === 'number' ? options.fractionsLimit : Infinity; // no limit by default
134
135
 
@@ -73,7 +73,11 @@ export var createNthRoots = /* #__PURE__ */factory(name, dependencies, _ref => {
73
73
  * Calculate the nth roots of a value.
74
74
  * An nth root of a positive real number A,
75
75
  * is a positive real solution of the equation "x^root = A".
76
- * This function returns an array of complex values.
76
+ * This function returns an array of Complex values.
77
+ * Note that currently the precision of Complex numbers are limited
78
+ * to the precision of a 64-bit IEEE floating point, so even if the input
79
+ * is a BigNumber with greater precision, rounding to 64 bits will occur
80
+ * in computing the nth roots.
77
81
  *
78
82
  * Syntax:
79
83
  *
@@ -0,0 +1,102 @@
1
+ import { factory } from '../../utils/factory.js';
2
+ import { isInteger } from '../../utils/number.js';
3
+ var name = 'bernoulli';
4
+ var dependencies = ['typed', 'config', 'isInteger', 'number', '?BigNumber', '?Fraction'];
5
+ export var createBernoulli = /* #__PURE__ */factory(name, dependencies, _ref => {
6
+ var {
7
+ typed,
8
+ config,
9
+ number,
10
+ BigNumber: _BigNumber,
11
+ Fraction
12
+ } = _ref;
13
+ /**
14
+ * Return the `n`th Bernoulli number, for positive integers `n`
15
+ *
16
+ * Syntax:
17
+ *
18
+ * math.bernoulli(n)
19
+ *
20
+ * Examples:
21
+ *
22
+ * math.bernoulli(1) // returns -0.5
23
+ * // All other odd Bernoulli numbers are 0:
24
+ * math.bernoulli(7) // returns 0
25
+ * math.bernoulli(math.bignumber(6)) // value bignumber(1).div(42)
26
+ * // Produces exact rationals for bigint or fraction input:
27
+ * math.bernoulli(8n) // Fraction -1,30
28
+ * math.bernoulli(math.fraction(10)) // Fraction 5,66
29
+ *
30
+ * See also:
31
+ *
32
+ * combinations, gamma, stirlingS2
33
+ *
34
+ * @param {number | BigNumber | bigint | Fraction} n
35
+ * Index of the Bernoulli number
36
+ * @return {number | BigNumber | Fraction}
37
+ * nth Bernoulli number, of a type corresponding to the argument n
38
+ */
39
+
40
+ var numberCache = [undefined];
41
+ var fractionCache = [undefined];
42
+ var bigCache = [undefined];
43
+ var cachedPrecision = 50;
44
+ return typed(name, {
45
+ number: index => _bernoulli(index, n => n, numberCache, (a, b) => a + b, (a, b) => a * b, (a, b) => a / b),
46
+ 'bigint | Fraction': index => _bernoulli(number(index), n => new Fraction(n), fractionCache, (a, b) => a.add(b), (a, b) => a.mul(b), (a, b) => a.div(b)),
47
+ BigNumber: index => {
48
+ if (config.precision !== cachedPrecision) {
49
+ bigCache = [undefined];
50
+ cachedPrecision = config.precision;
51
+ }
52
+ return _bernoulli(number(index), n => new _BigNumber(n), bigCache, (a, b) => a.add(b), (a, b) => a.mul(b), (a, b) => a.div(b));
53
+ }
54
+ });
55
+ });
56
+
57
+ /**
58
+ * Underlying implementation, with all operations passed in.
59
+ * Parameters:
60
+ * 1. index: a (positive integer) number specifying which Bernoulli number
61
+ * to compute.
62
+ * 2. promote: a function that takes an integer number and returns
63
+ * the desired type for the Bernoulli number values.
64
+ * 3. A: a cache array of partial computation data that _bernoulli should use.
65
+ * Different cache arrays should be provided for different types.
66
+ * 4. plus: a function that adds two values of the desired type.
67
+ * 5. times: a function that multiplies two values of the desired type.
68
+ * 6. divide: a function that divides one value of the desired type by another.
69
+ */
70
+ function _bernoulli(index, promote, A, plus, times, divide) {
71
+ if (index < 0 || !isInteger(index)) {
72
+ throw new RangeError('Bernoulli index must be nonnegative integer');
73
+ }
74
+ if (index === 0) return promote(1);
75
+ if (index === 1) return divide(promote(-1), promote(2));
76
+ if (index % 2 === 1) return promote(0);
77
+ // We proceed as in https://math.stackexchange.com/a/2844337
78
+ // (by no means the most efficient, but very simple to implement)
79
+ // A cache entry consists of a triple
80
+ // [cotangent coefficient a_n, prefactor, Bernouilli number B_2n]
81
+ var one = promote(1);
82
+ if (A.length === 1) {
83
+ A.push([divide(one, promote(-3)), divide(one, promote(-2)), divide(one, promote(6))]);
84
+ }
85
+ var half = index / 2;
86
+ var zero = promote(0);
87
+ var two = promote(2);
88
+ while (A.length <= half) {
89
+ var i = A.length; // next cotangent coefficient to compute
90
+ var lim = Math.floor((i + 1) / 2);
91
+ var a = zero;
92
+ for (var m = 1; m < lim; ++m) {
93
+ a = plus(a, times(A[m][0], A[i - m][0]));
94
+ }
95
+ a = times(a, two);
96
+ if (i % 2 === 0) a = plus(a, times(A[lim][0], A[lim][0]));
97
+ a = divide(a, promote(-(2 * i + 1)));
98
+ var prefactor = divide(times(A[i - 1][1], promote(-i * (2 * i - 1))), two);
99
+ A.push([a, prefactor, times(prefactor, a)]);
100
+ }
101
+ return A[half][2];
102
+ }
@@ -4,7 +4,7 @@ import { createMatAlgo07xSSf } from '../../type/matrix/utils/matAlgo07xSSf.js';
4
4
  import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
5
5
  import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
6
6
  var name = 'equal';
7
- var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'concat', 'SparseMatrix'];
7
+ var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'SparseMatrix'];
8
8
  export var createEqual = /* #__PURE__ */factory(name, dependencies, _ref => {
9
9
  var {
10
10
  typed,
@@ -27,8 +27,7 @@ export var createEqual = /* #__PURE__ */factory(name, dependencies, _ref => {
27
27
  });
28
28
  var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
29
29
  typed,
30
- matrix,
31
- concat
30
+ matrix
32
31
  });
33
32
 
34
33
  /**
@@ -1,6 +1,6 @@
1
1
  import { factory } from '../../utils/factory.js';
2
2
  var name = 'zeta';
3
- var dependencies = ['typed', 'config', 'multiply', 'pow', 'divide', 'factorial', 'equal', 'smallerEq', 'isNegative', 'gamma', 'sin', 'subtract', 'add', '?Complex', '?BigNumber', 'pi'];
3
+ var dependencies = ['typed', 'config', 'multiply', 'pow', 'divide', 'factorial', 'equal', 'smallerEq', 'isBounded', 'isNegative', 'gamma', 'sin', 'subtract', 'add', '?Complex', '?BigNumber', 'pi'];
4
4
  export var createZeta = /* #__PURE__ */factory(name, dependencies, _ref => {
5
5
  var {
6
6
  typed,
@@ -11,6 +11,7 @@ export var createZeta = /* #__PURE__ */factory(name, dependencies, _ref => {
11
11
  factorial,
12
12
  equal,
13
13
  smallerEq,
14
+ isBounded,
14
15
  isNegative,
15
16
  gamma,
16
17
  sin,
@@ -69,7 +70,7 @@ export var createZeta = /* #__PURE__ */factory(name, dependencies, _ref => {
69
70
  if (equal(s, 1)) {
70
71
  return createValue(NaN);
71
72
  }
72
- if (!isFinite(s)) {
73
+ if (!isBounded(s)) {
73
74
  return isNegative(s) ? createValue(NaN) : createValue(1);
74
75
  }
75
76
  return zeta(s, createValue, determineDigits, s => s);
@@ -0,0 +1,48 @@
1
+ import { factory } from '../../utils/factory.js';
2
+ var name = 'isBounded';
3
+ var dependencies = ['typed'];
4
+ export var createIsBounded = /* #__PURE__ */factory(name, dependencies, _ref => {
5
+ var {
6
+ typed
7
+ } = _ref;
8
+ /**
9
+ * Test whether a value is bounded. For scalars, this test is equivalent
10
+ * to the isFinite finiteness test. On the other hand, a Matrix or Array
11
+ * is defined to be bounded if every entry is finite.
12
+ *
13
+ * Syntax:
14
+ *
15
+ * math.isBounded(x)
16
+ *
17
+ * Examples:
18
+ *
19
+ * math.isBounded(0) // returns true
20
+ * math.isBounded(NaN) // returns false
21
+ * math.isBounded(math.bignumber(Infinity)) // returns false
22
+ * math.isBounded(math.fraction(1,3)) // returns true
23
+ * math.isBounded(math.complex('2 - 4i')) // returns true
24
+ * math.isBounded(-10000000000000000n) // returns true
25
+ * math.isBounded(undefined) // returns false
26
+ * math.isBounded(null) // returns false
27
+ * math.isBounded([0.001, -3n, 0]) // returns true
28
+ * math.isBounded([2, -Infinity, -3]) // returns false
29
+ *
30
+ * See also:
31
+ *
32
+ * isFinite, isNumeric, isPositive, isNegative, isNaN
33
+ *
34
+ * @param {number | BigNumber | bigint | Complex | Fraction | Unit | Array | Matrix} x Value to be tested
35
+ * @return {boolean} Returns true when `x` is bounded.
36
+ */
37
+ return typed(name, {
38
+ number: n => Number.isFinite(n),
39
+ 'BigNumber | Complex': x => x.isFinite(),
40
+ 'bigint | Fraction': () => true,
41
+ 'null | undefined': () => false,
42
+ Unit: typed.referToSelf(self => x => self(x.value)),
43
+ 'Array | Matrix': typed.referToSelf(self => A => {
44
+ if (!Array.isArray(A)) A = A.valueOf();
45
+ return A.every(entry => self(entry));
46
+ })
47
+ });
48
+ });
@@ -0,0 +1,44 @@
1
+ import { factory } from '../../utils/factory.js';
2
+ var name = 'isFinite';
3
+ var dependencies = ['typed', 'isBounded', 'map'];
4
+ export var createIsFinite = /* #__PURE__ */factory(name, dependencies, _ref => {
5
+ var {
6
+ typed,
7
+ isBounded,
8
+ map
9
+ } = _ref;
10
+ /**
11
+ * Test whether a value is finite.
12
+ *
13
+ * Operates elementwise on Array and Matrix values. To test if all entries
14
+ * of an Array or Matrix are finite, use isBounded.
15
+ *
16
+ * Syntax:
17
+ *
18
+ * math.isFinite(x)
19
+ *
20
+ * Examples:
21
+ *
22
+ * math.isFinite(0) // returns true
23
+ * math.isFinite(NaN) // returns false
24
+ * math.isFinite(math.bignumber(Infinity)) // returns false
25
+ * math.isFinite(math.fraction(1,3)) // returns true
26
+ * math.isFinite(math.complex('2 - 4i')) // returns true
27
+ * math.isFinite(-10000000000000000n) // returns true
28
+ * math.isFinite(undefined) // returns false
29
+ * math.isFinite(null) // returns false
30
+ * math.isFinite([0.001, -3n, 0]) // Array [true, true, true]
31
+ * math.isFinite([2, -Infinity, -3]) // Array [true, false, true]
32
+ *
33
+ * See also:
34
+ *
35
+ * isBounded isNumeric, isPositive, isNegative, isNaN
36
+ *
37
+ * @param {number | BigNumber | bigint | Complex | Fraction | Unit | Array | Matrix} x Value to be tested
38
+ * @return {boolean | Array | Matrix}
39
+ */
40
+ return typed(name, {
41
+ 'Array | Matrix': A => map(A, isBounded),
42
+ any: x => isBounded(x)
43
+ });
44
+ });
@@ -1,11 +1,11 @@
1
1
  import { deepMap } from '../../utils/collection.js';
2
- import { isInteger as isIntegerNumber } from '../../utils/number.js';
3
2
  import { factory } from '../../utils/factory.js';
4
3
  var name = 'isInteger';
5
- var dependencies = ['typed'];
4
+ var dependencies = ['typed', 'equal'];
6
5
  export var createIsInteger = /* #__PURE__ */factory(name, dependencies, _ref => {
7
6
  var {
8
- typed
7
+ typed,
8
+ equal
9
9
  } = _ref;
10
10
  /**
11
11
  * Test whether a value is an integer number.
@@ -37,18 +37,10 @@ export var createIsInteger = /* #__PURE__ */factory(name, dependencies, _ref =>
37
37
  * Throws an error in case of an unknown data type.
38
38
  */
39
39
  return typed(name, {
40
- number: isIntegerNumber,
41
- // TODO: what to do with isInteger(add(0.1, 0.2)) ?
42
-
43
- BigNumber: function BigNumber(x) {
44
- return x.isInt();
45
- },
46
- bigint: function bigint(x) {
47
- return true;
48
- },
49
- Fraction: function Fraction(x) {
50
- return x.d === 1n;
51
- },
40
+ number: n => Number.isFinite(n) ? equal(n, Math.round(n)) : false,
41
+ BigNumber: b => b.isFinite() ? equal(b.round(), b) : false,
42
+ bigint: b => true,
43
+ Fraction: r => r.d === 1n,
52
44
  'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
53
45
  });
54
46
  });
@@ -30,7 +30,7 @@ export var createIsNaN = /* #__PURE__ */factory(name, dependencies, _ref => {
30
30
  *
31
31
  * See also:
32
32
  *
33
- * isNumeric, isNegative, isPositive, isZero, isInteger
33
+ * isNumeric, isNegative, isPositive, isZero, isInteger, isFinite, isBounded
34
34
  *
35
35
  * @param {number | BigNumber | bigint | Fraction | Unit | Array | Matrix} x Value to be tested
36
36
  * @return {boolean} Returns true when `x` is NaN.
@@ -29,7 +29,7 @@ export var createIsNumeric = /* #__PURE__ */factory(name, dependencies, _ref =>
29
29
  *
30
30
  * See also:
31
31
  *
32
- * isZero, isPositive, isNegative, isInteger, hasNumericValue
32
+ * isZero, isPositive, isNegative, isInteger, hasNumericValue, isFinite, isBounded
33
33
  *
34
34
  * @param {*} x Value to be tested
35
35
  * @return {boolean} Returns true when `x` is a `number`, `BigNumber`,
@@ -16,7 +16,7 @@ export var createReplacer = /* #__PURE__ */factory(name, dependencies, () => {
16
16
  */
17
17
  return function replacer(key, value) {
18
18
  // the numeric values Infinitiy, -Infinity, and NaN cannot be serialized to JSON
19
- if (typeof value === 'number' && (!isFinite(value) || isNaN(value))) {
19
+ if (typeof value === 'number' && (!Number.isFinite(value) || isNaN(value))) {
20
20
  return {
21
21
  mathjs: 'number',
22
22
  value: String(value)
@@ -6,7 +6,7 @@ export function gammaNumber(n) {
6
6
  var x;
7
7
  if (isInteger(n)) {
8
8
  if (n <= 0) {
9
- return isFinite(n) ? Infinity : NaN;
9
+ return Number.isFinite(n) ? Infinity : NaN;
10
10
  }
11
11
  if (n > 171) {
12
12
  return Infinity; // Will overflow
@@ -53,7 +53,7 @@ export var lgammaSeries = [1.000000000190015, 76.18009172947146, -86.50532032941
53
53
  export function lgammaNumber(n) {
54
54
  if (n < 0) return NaN;
55
55
  if (n === 0) return Infinity;
56
- if (!isFinite(n)) return n;
56
+ if (!Number.isFinite(n)) return n;
57
57
  if (n < 0.5) {
58
58
  // Use Euler's reflection formula:
59
59
  // gamma(z) = PI / (sin(PI * z) * gamma(1 - z))
@@ -14,7 +14,7 @@ export function acotNumber(x) {
14
14
  }
15
15
  acotNumber.signature = n1;
16
16
  export function acothNumber(x) {
17
- return isFinite(x) ? (Math.log((x + 1) / x) + Math.log(x / (x - 1))) / 2 : 0;
17
+ return Number.isFinite(x) ? (Math.log((x + 1) / x) + Math.log(x / (x - 1))) / 2 : 0;
18
18
  }
19
19
  acothNumber.signature = n1;
20
20
  export function acscNumber(x) {
@@ -46,7 +46,7 @@ export var createFraction = /* #__PURE__ */factory(name, dependencies, _ref => {
46
46
  */
47
47
  return typed('fraction', {
48
48
  number: function number(x) {
49
- if (!isFinite(x) || isNaN(x)) {
49
+ if (!Number.isFinite(x) || isNaN(x)) {
50
50
  throw new Error(x + ' cannot be represented as a fraction');
51
51
  }
52
52
  return new Fraction(x);