mathjs 15.1.1 → 15.2.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 (40) hide show
  1. package/HISTORY.md +10 -0
  2. package/README.md +2 -2
  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/dependenciesDen.generated.js +19 -0
  7. package/lib/cjs/entry/dependenciesAny/dependenciesNum.generated.js +19 -0
  8. package/lib/cjs/entry/dependenciesAny.generated.js +14 -0
  9. package/lib/cjs/entry/impureFunctionsAny.generated.js +123 -121
  10. package/lib/cjs/entry/pureFunctionsAny.generated.js +344 -336
  11. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +4 -0
  12. package/lib/cjs/expression/embeddedDocs/function/fraction/den.js +14 -0
  13. package/lib/cjs/expression/embeddedDocs/function/fraction/num.js +14 -0
  14. package/lib/cjs/factoriesAny.js +15 -1
  15. package/lib/cjs/function/fraction/den.js +60 -0
  16. package/lib/cjs/function/fraction/num.js +62 -0
  17. package/lib/cjs/header.js +2 -2
  18. package/lib/cjs/type/unit/Unit.js +7 -0
  19. package/lib/cjs/utils/array.js +3 -0
  20. package/lib/cjs/utils/customs.js +39 -46
  21. package/lib/cjs/utils/map.js +2 -2
  22. package/lib/cjs/version.js +1 -1
  23. package/lib/esm/entry/dependenciesAny/dependenciesDen.generated.js +12 -0
  24. package/lib/esm/entry/dependenciesAny/dependenciesNum.generated.js +12 -0
  25. package/lib/esm/entry/dependenciesAny.generated.js +2 -0
  26. package/lib/esm/entry/impureFunctionsAny.generated.js +125 -123
  27. package/lib/esm/entry/pureFunctionsAny.generated.js +341 -333
  28. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +4 -0
  29. package/lib/esm/expression/embeddedDocs/function/fraction/den.js +8 -0
  30. package/lib/esm/expression/embeddedDocs/function/fraction/num.js +8 -0
  31. package/lib/esm/factoriesAny.js +3 -1
  32. package/lib/esm/function/fraction/den.js +54 -0
  33. package/lib/esm/function/fraction/num.js +56 -0
  34. package/lib/esm/type/unit/Unit.js +7 -0
  35. package/lib/esm/utils/array.js +3 -0
  36. package/lib/esm/utils/customs.js +42 -56
  37. package/lib/esm/utils/map.js +3 -3
  38. package/lib/esm/version.js +1 -1
  39. package/package.json +1 -1
  40. package/types/index.d.ts +51 -1
@@ -94,6 +94,8 @@ var _subtract = require("./function/arithmetic/subtract.js");
94
94
  var _unaryMinus = require("./function/arithmetic/unaryMinus.js");
95
95
  var _unaryPlus = require("./function/arithmetic/unaryPlus.js");
96
96
  var _xgcd = require("./function/arithmetic/xgcd.js");
97
+ var _num = require("./function/fraction/num.js");
98
+ var _den = require("./function/fraction/den.js");
97
99
  var _bitAnd = require("./function/bitwise/bitAnd.js");
98
100
  var _bitNot = require("./function/bitwise/bitNot.js");
99
101
  var _bitOr = require("./function/bitwise/bitOr.js");
@@ -557,6 +559,8 @@ const embeddedDocs = exports.embeddedDocs = {
557
559
  unaryPlus: _unaryPlus.unaryPlusDocs,
558
560
  xgcd: _xgcd.xgcdDocs,
559
561
  invmod: _invmod.invmodDocs,
562
+ num: _num.numDocs,
563
+ den: _den.denDocs,
560
564
  // functions - bitwise
561
565
  bitAnd: _bitAnd.bitAndDocs,
562
566
  bitNot: _bitNot.bitNotDocs,
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.denDocs = void 0;
7
+ const denDocs = exports.denDocs = {
8
+ name: 'den',
9
+ category: 'Fraction',
10
+ syntax: ['den(x)'],
11
+ description: 'Get the denominator of a fraction.',
12
+ examples: ['den(fraction(2, 3))', 'den(fraction(5, 8))'],
13
+ seealso: ['num', 'fraction']
14
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.numDocs = void 0;
7
+ const numDocs = exports.numDocs = {
8
+ name: 'num',
9
+ category: 'Fraction',
10
+ syntax: ['num(x)'],
11
+ description: 'Get the numerator of a fraction.',
12
+ examples: ['num(fraction(2, 3))', 'num(fraction(5, 8))'],
13
+ seealso: ['den', 'fraction']
14
+ };
@@ -495,6 +495,12 @@ Object.defineProperty(exports, "createDeepEqual", {
495
495
  return _deepEqual.createDeepEqual;
496
496
  }
497
497
  });
498
+ Object.defineProperty(exports, "createDen", {
499
+ enumerable: true,
500
+ get: function () {
501
+ return _den.createDen;
502
+ }
503
+ });
498
504
  Object.defineProperty(exports, "createDenseMatrixClass", {
499
505
  enumerable: true,
500
506
  get: function () {
@@ -1371,6 +1377,12 @@ Object.defineProperty(exports, "createNullishTransform", {
1371
1377
  return _nullishTransform.createNullishTransform;
1372
1378
  }
1373
1379
  });
1380
+ Object.defineProperty(exports, "createNum", {
1381
+ enumerable: true,
1382
+ get: function () {
1383
+ return _num.createNum;
1384
+ }
1385
+ });
1374
1386
  Object.defineProperty(exports, "createNumber", {
1375
1387
  enumerable: true,
1376
1388
  get: function () {
@@ -2517,4 +2529,6 @@ var _andTransform = require("./expression/transform/and.transform.js");
2517
2529
  var _orTransform = require("./expression/transform/or.transform.js");
2518
2530
  var _nullishTransform = require("./expression/transform/nullish.transform.js");
2519
2531
  var _bitAndTransform = require("./expression/transform/bitAnd.transform.js");
2520
- var _bitOrTransform = require("./expression/transform/bitOr.transform.js");
2532
+ var _bitOrTransform = require("./expression/transform/bitOr.transform.js");
2533
+ var _num = require("./function/fraction/num.js");
2534
+ var _den = require("./function/fraction/den.js");
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createDen = void 0;
7
+ var _factory = require("../../utils/factory.js");
8
+ var _collection = require("../../utils/collection.js");
9
+ const name = 'den';
10
+ const dependencies = ['typed', 'fraction'];
11
+ const createDen = exports.createDen = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
12
+ let {
13
+ typed,
14
+ fraction
15
+ } = _ref;
16
+ /**
17
+ * Get the denominator of a fraction.
18
+ * For a fraction `a/b`, the function returns `b`.
19
+ *
20
+ * The result is always in lowest terms. For example, `den(fraction(8, 6))`
21
+ * returns `3n` because 8/6 simplifies to 4/3.
22
+ *
23
+ * For negative fractions like `-a/b` or `a/-b`, the denominator is
24
+ * always returned as a positive bigint. The sign is stored in the
25
+ * numerator. So `den(fraction(-2, 3))` and `den(fraction(2, -3))`
26
+ * both return `3n`.
27
+ *
28
+ * For matrices, the function is evaluated element wise.
29
+ *
30
+ * Syntax:
31
+ *
32
+ * math.den(x)
33
+ *
34
+ * Examples:
35
+ *
36
+ * math.den(math.fraction(2, 3)) // returns 3n
37
+ * math.den(math.fraction(8, 6)) // returns 3n
38
+ * math.den(math.fraction('5/8')) // returns 8n
39
+ * math.den(math.fraction(-2, 3)) // returns 3n
40
+ * math.den(math.fraction(2, -3)) // returns 3n
41
+ * math.den(math.bignumber('0.5')) // returns 2n
42
+ *
43
+ * See also:
44
+ *
45
+ * num, fraction
46
+ *
47
+ * History:
48
+ *
49
+ * v15.2.0 Created
50
+ *
51
+ * @param {Fraction | BigNumber | Array | Matrix} x
52
+ * A fraction, BigNumber, or array with fractions
53
+ * @return {bigint | Array | Matrix} The denominator of x (in lowest terms)
54
+ */
55
+ return typed(name, {
56
+ Fraction: x => x.d,
57
+ BigNumber: x => fraction(x).d,
58
+ 'Array | Matrix': typed.referToSelf(self => x => (0, _collection.deepMap)(x, self))
59
+ });
60
+ });
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createNum = void 0;
7
+ var _factory = require("../../utils/factory.js");
8
+ var _collection = require("../../utils/collection.js");
9
+ const name = 'num';
10
+ const dependencies = ['typed', 'fraction'];
11
+ const createNum = exports.createNum = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
12
+ let {
13
+ typed,
14
+ fraction
15
+ } = _ref;
16
+ /**
17
+ * Get the numerator of a fraction.
18
+ * For a fraction `a/b`, the function returns `a`.
19
+ *
20
+ * The result is always in lowest terms. For example, `num(fraction(8, 6))`
21
+ * returns `4n` because 8/6 simplifies to 4/3.
22
+ *
23
+ * For negative fractions like `-a/b` or `a/-b`, the sign is always
24
+ * included in the numerator. Both forms are normalized internally, so
25
+ * `num(fraction(-2, 3))` and `num(fraction(2, -3))` both return `-2`.
26
+ *
27
+ * For matrices, the function is evaluated element wise.
28
+ *
29
+ * Syntax:
30
+ *
31
+ * math.num(x)
32
+ *
33
+ * Examples:
34
+ *
35
+ * math.num(math.fraction(2, 3)) // returns 2n
36
+ * math.num(math.fraction(8, 6)) // returns 4n
37
+ * math.num(math.fraction('5/8')) // returns 5n
38
+ * math.num(math.fraction(-2, 3)) // returns -2n
39
+ * math.num(math.fraction(2, -3)) // returns -2n
40
+ * math.num(math.bignumber('0.5')) // returns 1n
41
+ *
42
+ * See also:
43
+ *
44
+ * den, fraction
45
+ *
46
+ * History:
47
+ *
48
+ * v15.2.0 Created
49
+ *
50
+ * @param {Fraction | BigNumber | Array | Matrix} x
51
+ * A fraction, BigNumber, or array with fractions
52
+ * @return {bigint | Array | Matrix} The numerator of x (in lowest terms)
53
+ */
54
+ return typed(name, {
55
+ Fraction: x => x.s * x.n,
56
+ BigNumber: x => {
57
+ const f = fraction(x);
58
+ return f.s * f.n;
59
+ },
60
+ 'Array | Matrix': typed.referToSelf(self => x => (0, _collection.deepMap)(x, self))
61
+ });
62
+ });
package/lib/cjs/header.js CHANGED
@@ -6,8 +6,8 @@
6
6
  * It features real and complex numbers, units, matrices, a large set of
7
7
  * mathematical functions, and a flexible expression parser.
8
8
  *
9
- * @version 15.1.1
10
- * @date 2026-02-10
9
+ * @version 15.2.0
10
+ * @date 2026-04-07
11
11
  *
12
12
  * @license
13
13
  * Copyright (C) 2013-2026 Jos de Jong <wjosdejong@gmail.com>
@@ -3166,6 +3166,13 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
3166
3166
  value: 1,
3167
3167
  offset: 0
3168
3168
  },
3169
+ Ah: {
3170
+ name: 'Ah',
3171
+ base: BASE_UNITS.ELECTRIC_CHARGE,
3172
+ prefixes: PREFIXES.SHORT,
3173
+ value: 3600,
3174
+ offset: 0
3175
+ },
3169
3176
  // Electric capacitance
3170
3177
  farad: {
3171
3178
  name: 'farad',
@@ -856,6 +856,9 @@ function get(array, index) {
856
856
  if (!Array.isArray(array)) {
857
857
  throw new Error('Array expected');
858
858
  }
859
+ if (!Array.isArray(index)) {
860
+ throw new Error('Array expected for index');
861
+ }
859
862
  const size = arraySize(array);
860
863
  if (index.length !== size.length) {
861
864
  throw new _DimensionError.DimensionError(index.length, size.length);
@@ -6,12 +6,13 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.getSafeMethod = getSafeMethod;
7
7
  exports.getSafeProperty = getSafeProperty;
8
8
  exports.isPlainObject = isPlainObject;
9
+ exports.isSafeArrayProperty = isSafeArrayProperty;
9
10
  exports.isSafeMethod = isSafeMethod;
10
- exports.isSafeProperty = isSafeProperty;
11
+ exports.isSafeObjectProperty = isSafeObjectProperty;
11
12
  exports.setSafeProperty = setSafeProperty;
12
13
  var _object = require("./object.js");
13
14
  /**
14
- * Get a property of a plain object
15
+ * Get a property of a plain object or array
15
16
  * Throws an error in case the object is not a plain object or the
16
17
  * property is not defined on the object itself
17
18
  * @param {Object} object
@@ -19,18 +20,20 @@ var _object = require("./object.js");
19
20
  * @return {*} Returns the property value when safe
20
21
  */
21
22
  function getSafeProperty(object, prop) {
22
- // only allow getting safe properties of a plain object
23
- if (isSafeProperty(object, prop)) {
23
+ if (isSafeObjectProperty(object, prop) || isSafeArrayProperty(object, prop)) {
24
24
  return object[prop];
25
25
  }
26
- if (typeof object[prop] === 'function' && isSafeMethod(object, prop)) {
27
- throw new Error('Cannot access method "' + prop + '" as a property');
26
+ if (isSafeMethod(object, prop)) {
27
+ throw new Error(`Cannot access method "${prop}" as a property`);
28
+ }
29
+ if (object === null || object === undefined) {
30
+ throw new TypeError(`Cannot access property "${prop}": object is ${object}`);
28
31
  }
29
32
  throw new Error('No access to property "' + prop + '"');
30
33
  }
31
34
 
32
35
  /**
33
- * Set a property on a plain object.
36
+ * Set a property on a plain object or array.
34
37
  * Throws an error in case the object is not a plain object or the
35
38
  * property would override an inherited property like .constructor or .toString
36
39
  * @param {Object} object
@@ -38,49 +41,43 @@ function getSafeProperty(object, prop) {
38
41
  * @param {*} value
39
42
  * @return {*} Returns the value
40
43
  */
41
- // TODO: merge this function into access.js?
42
44
  function setSafeProperty(object, prop, value) {
43
- // only allow setting safe properties of a plain object
44
- if (isSafeProperty(object, prop)) {
45
+ if (isSafeObjectProperty(object, prop) || isSafeArrayProperty(object, prop)) {
45
46
  object[prop] = value;
46
47
  return value;
47
48
  }
48
- throw new Error('No access to property "' + prop + '"');
49
+ throw new Error(`No access to property "${prop}"`);
49
50
  }
50
51
 
51
52
  /**
52
- * Test whether a property is safe to use on an object or Array.
53
- * For example .toString and .constructor are not safe
54
- * @param {Object | Array} object
53
+ * Test whether a property is safe for reading and writing on an object
54
+ * For example .constructor and .__proto__ are not safe
55
+ * @param {Object} object
55
56
  * @param {string} prop
56
57
  * @return {boolean} Returns true when safe
57
58
  */
58
- function isSafeProperty(object, prop) {
59
- if (!isPlainObject(object) && !Array.isArray(object)) {
59
+ function isSafeObjectProperty(object, prop) {
60
+ if (!isPlainObject(object)) {
60
61
  return false;
61
62
  }
62
- // SAFE: whitelisted
63
- // e.g length
64
- if ((0, _object.hasOwnProperty)(safeNativeProperties, prop)) {
65
- return true;
66
- }
67
- // UNSAFE: inherited from Object prototype
68
- // e.g constructor
69
- if (prop in Object.prototype) {
70
- // 'in' is used instead of hasOwnProperty for nodejs v0.10
71
- // which is inconsistent on root prototypes. It is safe
72
- // here because Object.prototype is a root object
73
- return false;
74
- }
75
- // UNSAFE: inherited from Function prototype
76
- // e.g call, apply
77
- if (prop in Function.prototype) {
78
- // 'in' is used instead of hasOwnProperty for nodejs v0.10
79
- // which is inconsistent on root prototypes. It is safe
80
- // here because Function.prototype is a root object
63
+ return !(prop in Object.prototype);
64
+ }
65
+
66
+ /**
67
+ * Test whether a property is safe for reading and writing on an Array
68
+ * For example .__proto__ and .constructor are not safe
69
+ * @param {unknown} array
70
+ * @param {string | number} prop
71
+ * @return {boolean} Returns true when safe
72
+ */
73
+ function isSafeArrayProperty(array, prop) {
74
+ if (!Array.isArray(array)) {
81
75
  return false;
82
76
  }
83
- return true;
77
+ return typeof prop === 'number' || typeof prop === 'string' && isInteger(prop) || prop === 'length';
78
+ }
79
+ function isInteger(prop) {
80
+ return /^\d+$/.test(prop);
84
81
  }
85
82
 
86
83
  /**
@@ -108,17 +105,20 @@ function isSafeMethod(object, method) {
108
105
  if (object === null || object === undefined || typeof object[method] !== 'function') {
109
106
  return false;
110
107
  }
108
+
111
109
  // UNSAFE: ghosted
112
110
  // e.g overridden toString
113
111
  // Note that IE10 doesn't support __proto__ and we can't do this check there.
114
112
  if ((0, _object.hasOwnProperty)(object, method) && Object.getPrototypeOf && method in Object.getPrototypeOf(object)) {
115
113
  return false;
116
114
  }
115
+
117
116
  // SAFE: whitelisted
118
117
  // e.g toString
119
- if ((0, _object.hasOwnProperty)(safeNativeMethods, method)) {
118
+ if (safeNativeMethods.has(method)) {
120
119
  return true;
121
120
  }
121
+
122
122
  // UNSAFE: inherited from Object prototype
123
123
  // e.g constructor
124
124
  if (method in Object.prototype) {
@@ -127,6 +127,7 @@ function isSafeMethod(object, method) {
127
127
  // here because Object.prototype is a root object
128
128
  return false;
129
129
  }
130
+
130
131
  // UNSAFE: inherited from Function prototype
131
132
  // e.g call, apply
132
133
  if (method in Function.prototype) {
@@ -140,12 +141,4 @@ function isSafeMethod(object, method) {
140
141
  function isPlainObject(object) {
141
142
  return typeof object === 'object' && object && object.constructor === Object;
142
143
  }
143
- const safeNativeProperties = {
144
- length: true,
145
- name: true
146
- };
147
- const safeNativeMethods = {
148
- toString: true,
149
- valueOf: true,
150
- toLocaleString: true
151
- };
144
+ const safeNativeMethods = new Set(['toString', 'valueOf', 'toLocaleString']);
@@ -34,7 +34,7 @@ class ObjectWrappingMap {
34
34
  return this;
35
35
  }
36
36
  has(key) {
37
- return (0, _customs.isSafeProperty)(this.wrappedObject, key) && key in this.wrappedObject;
37
+ return (0, _customs.isSafeObjectProperty)(this.wrappedObject, key) && key in this.wrappedObject;
38
38
  }
39
39
  entries() {
40
40
  return mapIterator(this.keys(), key => [key, this.get(key)]);
@@ -45,7 +45,7 @@ class ObjectWrappingMap {
45
45
  }
46
46
  }
47
47
  delete(key) {
48
- if ((0, _customs.isSafeProperty)(this.wrappedObject, key)) {
48
+ if ((0, _customs.isSafeObjectProperty)(this.wrappedObject, key)) {
49
49
  delete this.wrappedObject[key];
50
50
  }
51
51
  }
@@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.version = void 0;
7
- const version = exports.version = '15.1.1';
7
+ const version = exports.version = '15.2.0';
8
8
  // Note: This file is automatically generated when building math.js.
9
9
  // Changes made in this file will be overwritten.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED
3
+ * DON'T MAKE CHANGES HERE
4
+ */
5
+ import { fractionDependencies } from './dependenciesFraction.generated.js';
6
+ import { typedDependencies } from './dependenciesTyped.generated.js';
7
+ import { createDen } from '../../factoriesAny.js';
8
+ export var denDependencies = {
9
+ fractionDependencies,
10
+ typedDependencies,
11
+ createDen
12
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED
3
+ * DON'T MAKE CHANGES HERE
4
+ */
5
+ import { fractionDependencies } from './dependenciesFraction.generated.js';
6
+ import { typedDependencies } from './dependenciesTyped.generated.js';
7
+ import { createNum } from '../../factoriesAny.js';
8
+ export var numDependencies = {
9
+ fractionDependencies,
10
+ typedDependencies,
11
+ createNum
12
+ };
@@ -84,6 +84,7 @@ export { cubeDependencies } from './dependenciesAny/dependenciesCube.generated.j
84
84
  export { cumsumDependencies } from './dependenciesAny/dependenciesCumSum.generated.js';
85
85
  export { cumsumTransformDependencies } from './dependenciesAny/dependenciesCumSumTransform.generated.js';
86
86
  export { deepEqualDependencies } from './dependenciesAny/dependenciesDeepEqual.generated.js';
87
+ export { denDependencies } from './dependenciesAny/dependenciesDen.generated.js';
87
88
  export { DenseMatrixDependencies } from './dependenciesAny/dependenciesDenseMatrixClass.generated.js';
88
89
  export { derivativeDependencies } from './dependenciesAny/dependenciesDerivative.generated.js';
89
90
  export { detDependencies } from './dependenciesAny/dependenciesDet.generated.js';
@@ -230,6 +231,7 @@ export { nuclearMagnetonDependencies } from './dependenciesAny/dependenciesNucle
230
231
  export { nullDependencies } from './dependenciesAny/dependenciesNull.generated.js';
231
232
  export { nullishDependencies } from './dependenciesAny/dependenciesNullish.generated.js';
232
233
  export { nullishTransformDependencies } from './dependenciesAny/dependenciesNullishTransform.generated.js';
234
+ export { numDependencies } from './dependenciesAny/dependenciesNum.generated.js';
233
235
  export { numberDependencies } from './dependenciesAny/dependenciesNumber.generated.js';
234
236
  export { numericDependencies } from './dependenciesAny/dependenciesNumeric.generated.js';
235
237
  export { ObjectNodeDependencies } from './dependenciesAny/dependenciesObjectNode.generated.js';