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
@@ -88,6 +88,8 @@ import { subtractDocs } from './function/arithmetic/subtract.js';
88
88
  import { unaryMinusDocs } from './function/arithmetic/unaryMinus.js';
89
89
  import { unaryPlusDocs } from './function/arithmetic/unaryPlus.js';
90
90
  import { xgcdDocs } from './function/arithmetic/xgcd.js';
91
+ import { numDocs } from './function/fraction/num.js';
92
+ import { denDocs } from './function/fraction/den.js';
91
93
  import { bitAndDocs } from './function/bitwise/bitAnd.js';
92
94
  import { bitNotDocs } from './function/bitwise/bitNot.js';
93
95
  import { bitOrDocs } from './function/bitwise/bitOr.js';
@@ -551,6 +553,8 @@ export var embeddedDocs = {
551
553
  unaryPlus: unaryPlusDocs,
552
554
  xgcd: xgcdDocs,
553
555
  invmod: invmodDocs,
556
+ num: numDocs,
557
+ den: denDocs,
554
558
  // functions - bitwise
555
559
  bitAnd: bitAndDocs,
556
560
  bitNot: bitNotDocs,
@@ -0,0 +1,8 @@
1
+ export var denDocs = {
2
+ name: 'den',
3
+ category: 'Fraction',
4
+ syntax: ['den(x)'],
5
+ description: 'Get the denominator of a fraction.',
6
+ examples: ['den(fraction(2, 3))', 'den(fraction(5, 8))'],
7
+ seealso: ['num', 'fraction']
8
+ };
@@ -0,0 +1,8 @@
1
+ export var numDocs = {
2
+ name: 'num',
3
+ category: 'Fraction',
4
+ syntax: ['num(x)'],
5
+ description: 'Get the numerator of a fraction.',
6
+ examples: ['num(fraction(2, 3))', 'num(fraction(5, 8))'],
7
+ seealso: ['den', 'fraction']
8
+ };
@@ -300,4 +300,6 @@ export { createAndTransform } from './expression/transform/and.transform.js';
300
300
  export { createOrTransform } from './expression/transform/or.transform.js';
301
301
  export { createNullishTransform } from './expression/transform/nullish.transform.js';
302
302
  export { createBitAndTransform } from './expression/transform/bitAnd.transform.js';
303
- export { createBitOrTransform } from './expression/transform/bitOr.transform.js';
303
+ export { createBitOrTransform } from './expression/transform/bitOr.transform.js';
304
+ export { createNum } from './function/fraction/num.js';
305
+ export { createDen } from './function/fraction/den.js';
@@ -0,0 +1,54 @@
1
+ import { factory } from '../../utils/factory.js';
2
+ import { deepMap } from '../../utils/collection.js';
3
+ var name = 'den';
4
+ var dependencies = ['typed', 'fraction'];
5
+ export var createDen = /* #__PURE__ */factory(name, dependencies, _ref => {
6
+ var {
7
+ typed,
8
+ fraction
9
+ } = _ref;
10
+ /**
11
+ * Get the denominator of a fraction.
12
+ * For a fraction `a/b`, the function returns `b`.
13
+ *
14
+ * The result is always in lowest terms. For example, `den(fraction(8, 6))`
15
+ * returns `3n` because 8/6 simplifies to 4/3.
16
+ *
17
+ * For negative fractions like `-a/b` or `a/-b`, the denominator is
18
+ * always returned as a positive bigint. The sign is stored in the
19
+ * numerator. So `den(fraction(-2, 3))` and `den(fraction(2, -3))`
20
+ * both return `3n`.
21
+ *
22
+ * For matrices, the function is evaluated element wise.
23
+ *
24
+ * Syntax:
25
+ *
26
+ * math.den(x)
27
+ *
28
+ * Examples:
29
+ *
30
+ * math.den(math.fraction(2, 3)) // returns 3n
31
+ * math.den(math.fraction(8, 6)) // returns 3n
32
+ * math.den(math.fraction('5/8')) // returns 8n
33
+ * math.den(math.fraction(-2, 3)) // returns 3n
34
+ * math.den(math.fraction(2, -3)) // returns 3n
35
+ * math.den(math.bignumber('0.5')) // returns 2n
36
+ *
37
+ * See also:
38
+ *
39
+ * num, fraction
40
+ *
41
+ * History:
42
+ *
43
+ * v15.2.0 Created
44
+ *
45
+ * @param {Fraction | BigNumber | Array | Matrix} x
46
+ * A fraction, BigNumber, or array with fractions
47
+ * @return {bigint | Array | Matrix} The denominator of x (in lowest terms)
48
+ */
49
+ return typed(name, {
50
+ Fraction: x => x.d,
51
+ BigNumber: x => fraction(x).d,
52
+ 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
53
+ });
54
+ });
@@ -0,0 +1,56 @@
1
+ import { factory } from '../../utils/factory.js';
2
+ import { deepMap } from '../../utils/collection.js';
3
+ var name = 'num';
4
+ var dependencies = ['typed', 'fraction'];
5
+ export var createNum = /* #__PURE__ */factory(name, dependencies, _ref => {
6
+ var {
7
+ typed,
8
+ fraction
9
+ } = _ref;
10
+ /**
11
+ * Get the numerator of a fraction.
12
+ * For a fraction `a/b`, the function returns `a`.
13
+ *
14
+ * The result is always in lowest terms. For example, `num(fraction(8, 6))`
15
+ * returns `4n` because 8/6 simplifies to 4/3.
16
+ *
17
+ * For negative fractions like `-a/b` or `a/-b`, the sign is always
18
+ * included in the numerator. Both forms are normalized internally, so
19
+ * `num(fraction(-2, 3))` and `num(fraction(2, -3))` both return `-2`.
20
+ *
21
+ * For matrices, the function is evaluated element wise.
22
+ *
23
+ * Syntax:
24
+ *
25
+ * math.num(x)
26
+ *
27
+ * Examples:
28
+ *
29
+ * math.num(math.fraction(2, 3)) // returns 2n
30
+ * math.num(math.fraction(8, 6)) // returns 4n
31
+ * math.num(math.fraction('5/8')) // returns 5n
32
+ * math.num(math.fraction(-2, 3)) // returns -2n
33
+ * math.num(math.fraction(2, -3)) // returns -2n
34
+ * math.num(math.bignumber('0.5')) // returns 1n
35
+ *
36
+ * See also:
37
+ *
38
+ * den, fraction
39
+ *
40
+ * History:
41
+ *
42
+ * v15.2.0 Created
43
+ *
44
+ * @param {Fraction | BigNumber | Array | Matrix} x
45
+ * A fraction, BigNumber, or array with fractions
46
+ * @return {bigint | Array | Matrix} The numerator of x (in lowest terms)
47
+ */
48
+ return typed(name, {
49
+ Fraction: x => x.s * x.n,
50
+ BigNumber: x => {
51
+ var f = fraction(x);
52
+ return f.s * f.n;
53
+ },
54
+ 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
55
+ });
56
+ });
@@ -3159,6 +3159,13 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
3159
3159
  value: 1,
3160
3160
  offset: 0
3161
3161
  },
3162
+ Ah: {
3163
+ name: 'Ah',
3164
+ base: BASE_UNITS.ELECTRIC_CHARGE,
3165
+ prefixes: PREFIXES.SHORT,
3166
+ value: 3600,
3167
+ offset: 0
3168
+ },
3162
3169
  // Electric capacitance
3163
3170
  farad: {
3164
3171
  name: 'farad',
@@ -820,6 +820,9 @@ export function get(array, index) {
820
820
  if (!Array.isArray(array)) {
821
821
  throw new Error('Array expected');
822
822
  }
823
+ if (!Array.isArray(index)) {
824
+ throw new Error('Array expected for index');
825
+ }
823
826
  var size = arraySize(array);
824
827
  if (index.length !== size.length) {
825
828
  throw new DimensionError(index.length, size.length);
@@ -1,26 +1,28 @@
1
1
  import { hasOwnProperty } from './object.js';
2
2
 
3
3
  /**
4
- * Get a property of a plain object
4
+ * Get a property of a plain object or array
5
5
  * Throws an error in case the object is not a plain object or the
6
6
  * property is not defined on the object itself
7
7
  * @param {Object} object
8
8
  * @param {string} prop
9
9
  * @return {*} Returns the property value when safe
10
10
  */
11
- function getSafeProperty(object, prop) {
12
- // only allow getting safe properties of a plain object
13
- if (isSafeProperty(object, prop)) {
11
+ export function getSafeProperty(object, prop) {
12
+ if (isSafeObjectProperty(object, prop) || isSafeArrayProperty(object, prop)) {
14
13
  return object[prop];
15
14
  }
16
- if (typeof object[prop] === 'function' && isSafeMethod(object, prop)) {
17
- throw new Error('Cannot access method "' + prop + '" as a property');
15
+ if (isSafeMethod(object, prop)) {
16
+ throw new Error("Cannot access method \"".concat(prop, "\" as a property"));
17
+ }
18
+ if (object === null || object === undefined) {
19
+ throw new TypeError("Cannot access property \"".concat(prop, "\": object is ").concat(object));
18
20
  }
19
21
  throw new Error('No access to property "' + prop + '"');
20
22
  }
21
23
 
22
24
  /**
23
- * Set a property on a plain object.
25
+ * Set a property on a plain object or array.
24
26
  * Throws an error in case the object is not a plain object or the
25
27
  * property would override an inherited property like .constructor or .toString
26
28
  * @param {Object} object
@@ -28,49 +30,43 @@ function getSafeProperty(object, prop) {
28
30
  * @param {*} value
29
31
  * @return {*} Returns the value
30
32
  */
31
- // TODO: merge this function into access.js?
32
- function setSafeProperty(object, prop, value) {
33
- // only allow setting safe properties of a plain object
34
- if (isSafeProperty(object, prop)) {
33
+ export function setSafeProperty(object, prop, value) {
34
+ if (isSafeObjectProperty(object, prop) || isSafeArrayProperty(object, prop)) {
35
35
  object[prop] = value;
36
36
  return value;
37
37
  }
38
- throw new Error('No access to property "' + prop + '"');
38
+ throw new Error("No access to property \"".concat(prop, "\""));
39
39
  }
40
40
 
41
41
  /**
42
- * Test whether a property is safe to use on an object or Array.
43
- * For example .toString and .constructor are not safe
44
- * @param {Object | Array} object
42
+ * Test whether a property is safe for reading and writing on an object
43
+ * For example .constructor and .__proto__ are not safe
44
+ * @param {Object} object
45
45
  * @param {string} prop
46
46
  * @return {boolean} Returns true when safe
47
47
  */
48
- function isSafeProperty(object, prop) {
49
- if (!isPlainObject(object) && !Array.isArray(object)) {
48
+ export function isSafeObjectProperty(object, prop) {
49
+ if (!isPlainObject(object)) {
50
50
  return false;
51
51
  }
52
- // SAFE: whitelisted
53
- // e.g length
54
- if (hasOwnProperty(safeNativeProperties, prop)) {
55
- return true;
56
- }
57
- // UNSAFE: inherited from Object prototype
58
- // e.g constructor
59
- if (prop in Object.prototype) {
60
- // 'in' is used instead of hasOwnProperty for nodejs v0.10
61
- // which is inconsistent on root prototypes. It is safe
62
- // here because Object.prototype is a root object
63
- return false;
64
- }
65
- // UNSAFE: inherited from Function prototype
66
- // e.g call, apply
67
- if (prop in Function.prototype) {
68
- // 'in' is used instead of hasOwnProperty for nodejs v0.10
69
- // which is inconsistent on root prototypes. It is safe
70
- // here because Function.prototype is a root object
52
+ return !(prop in Object.prototype);
53
+ }
54
+
55
+ /**
56
+ * Test whether a property is safe for reading and writing on an Array
57
+ * For example .__proto__ and .constructor are not safe
58
+ * @param {unknown} array
59
+ * @param {string | number} prop
60
+ * @return {boolean} Returns true when safe
61
+ */
62
+ export function isSafeArrayProperty(array, prop) {
63
+ if (!Array.isArray(array)) {
71
64
  return false;
72
65
  }
73
- return true;
66
+ return typeof prop === 'number' || typeof prop === 'string' && isInteger(prop) || prop === 'length';
67
+ }
68
+ function isInteger(prop) {
69
+ return /^\d+$/.test(prop);
74
70
  }
75
71
 
76
72
  /**
@@ -80,7 +76,7 @@ function isSafeProperty(object, prop) {
80
76
  * @param {string} method
81
77
  * @return {function} Returns the method when valid
82
78
  */
83
- function getSafeMethod(object, method) {
79
+ export function getSafeMethod(object, method) {
84
80
  if (!isSafeMethod(object, method)) {
85
81
  throw new Error('No access to method "' + method + '"');
86
82
  }
@@ -94,21 +90,24 @@ function getSafeMethod(object, method) {
94
90
  * @param {string} method
95
91
  * @return {boolean} Returns true when safe, false otherwise
96
92
  */
97
- function isSafeMethod(object, method) {
93
+ export function isSafeMethod(object, method) {
98
94
  if (object === null || object === undefined || typeof object[method] !== 'function') {
99
95
  return false;
100
96
  }
97
+
101
98
  // UNSAFE: ghosted
102
99
  // e.g overridden toString
103
100
  // Note that IE10 doesn't support __proto__ and we can't do this check there.
104
101
  if (hasOwnProperty(object, method) && Object.getPrototypeOf && method in Object.getPrototypeOf(object)) {
105
102
  return false;
106
103
  }
104
+
107
105
  // SAFE: whitelisted
108
106
  // e.g toString
109
- if (hasOwnProperty(safeNativeMethods, method)) {
107
+ if (safeNativeMethods.has(method)) {
110
108
  return true;
111
109
  }
110
+
112
111
  // UNSAFE: inherited from Object prototype
113
112
  // e.g constructor
114
113
  if (method in Object.prototype) {
@@ -117,6 +116,7 @@ function isSafeMethod(object, method) {
117
116
  // here because Object.prototype is a root object
118
117
  return false;
119
118
  }
119
+
120
120
  // UNSAFE: inherited from Function prototype
121
121
  // e.g call, apply
122
122
  if (method in Function.prototype) {
@@ -127,21 +127,7 @@ function isSafeMethod(object, method) {
127
127
  }
128
128
  return true;
129
129
  }
130
- function isPlainObject(object) {
130
+ export function isPlainObject(object) {
131
131
  return typeof object === 'object' && object && object.constructor === Object;
132
132
  }
133
- var safeNativeProperties = {
134
- length: true,
135
- name: true
136
- };
137
- var safeNativeMethods = {
138
- toString: true,
139
- valueOf: true,
140
- toLocaleString: true
141
- };
142
- export { getSafeProperty };
143
- export { setSafeProperty };
144
- export { isSafeProperty };
145
- export { getSafeMethod };
146
- export { isSafeMethod };
147
- export { isPlainObject };
133
+ var safeNativeMethods = new Set(['toString', 'valueOf', 'toLocaleString']);
@@ -1,4 +1,4 @@
1
- import { getSafeProperty, isSafeProperty, setSafeProperty } from './customs.js';
1
+ import { getSafeProperty, isSafeObjectProperty, setSafeProperty } from './customs.js';
2
2
  import { isMap, isObject } from './is.js';
3
3
 
4
4
  /**
@@ -25,7 +25,7 @@ export class ObjectWrappingMap {
25
25
  return this;
26
26
  }
27
27
  has(key) {
28
- return isSafeProperty(this.wrappedObject, key) && key in this.wrappedObject;
28
+ return isSafeObjectProperty(this.wrappedObject, key) && key in this.wrappedObject;
29
29
  }
30
30
  entries() {
31
31
  return mapIterator(this.keys(), key => [key, this.get(key)]);
@@ -36,7 +36,7 @@ export class ObjectWrappingMap {
36
36
  }
37
37
  }
38
38
  delete(key) {
39
- if (isSafeProperty(this.wrappedObject, key)) {
39
+ if (isSafeObjectProperty(this.wrappedObject, key)) {
40
40
  delete this.wrappedObject[key];
41
41
  }
42
42
  }
@@ -1,3 +1,3 @@
1
- export var version = '15.1.1';
1
+ export var version = '15.2.0';
2
2
  // Note: This file is automatically generated when building math.js.
3
3
  // Changes made in this file will be overwritten.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mathjs",
3
- "version": "15.1.1",
3
+ "version": "15.2.0",
4
4
  "description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.",
5
5
  "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
6
6
  "homepage": "https://mathjs.org",
package/types/index.d.ts CHANGED
@@ -1823,6 +1823,26 @@ export interface MathJsInstance extends MathJsFactory {
1823
1823
  re(x: MathJsChain<number | Complex>): MathJsChain<number>
1824
1824
  re<T extends BigNumber | MathCollection>(x: MathJsChain<T>): MathJsChain<T>
1825
1825
 
1826
+ /**
1827
+ * Get the numerator of a fraction. For a fraction a/b, the function
1828
+ * returns a. For matrices, the function is evaluated element wise.
1829
+ * @param x A fraction, BigNumber, or array with fractions
1830
+ * @returns The numerator of x
1831
+ */
1832
+ num(x: number | BigNumber | bigint | Fraction): bigint
1833
+ num(x: MathArray<MathScalarType>): MathArray<bigint>
1834
+ num(x: Matrix<MathScalarType>): Matrix<bigint>
1835
+
1836
+ /**
1837
+ * Get the denominator of a fraction. For a fraction a/b, the function
1838
+ * returns b. For matrices, the function is evaluated element wise.
1839
+ * @param x A fraction, BigNumber, or array with fractions
1840
+ * @returns The denominator of x
1841
+ */
1842
+ den(x: number | BigNumber | bigint | Fraction): bigint
1843
+ den(x: MathArray<MathScalarType>): MathArray<bigint>
1844
+ den(x: Matrix<MathScalarType>): Matrix<bigint>
1845
+
1826
1846
  /*************************************************************************
1827
1847
  * Geometry functions
1828
1848
  ************************************************************************/
@@ -4320,7 +4340,9 @@ export const {
4320
4340
  stdTransformDependencies,
4321
4341
  sumTransformDependencies,
4322
4342
  varianceTransformDependencies,
4323
- printTransformDependencies
4343
+ printTransformDependencies,
4344
+ andTransformDependencies,
4345
+ orTransformDependencies
4324
4346
  }: Record<string, FactoryFunctionMap>
4325
4347
 
4326
4348
  export interface Matrix<T = MathGeneric> {
@@ -5942,6 +5964,30 @@ export interface MathJsChain<TValue> {
5942
5964
  re(this: MathJsChain<BigNumber>): MathJsChain<BigNumber>
5943
5965
  re(this: MathJsChain<MathCollection>): MathJsChain<MathCollection>
5944
5966
 
5967
+ /**
5968
+ * Get the numerator of a fraction. For a fraction a/b, the function
5969
+ * returns a. For matrices, the function is evaluated element wise.
5970
+ */
5971
+ num(
5972
+ this: MathJsChain<number | BigNumber | bigint | Fraction>
5973
+ ): MathJsChain<bigint>
5974
+ num(
5975
+ this: MathJsChain<MathArray<MathScalarType>>
5976
+ ): MathJsChain<MathArray<bigint>>
5977
+ num(this: MathJsChain<Matrix<MathScalarType>>): MathJsChain<Matrix<bigint>>
5978
+
5979
+ /**
5980
+ * Get the denominator of a fraction. For a fraction a/b, the function
5981
+ * returns b. For matrices, the function is evaluated element wise.
5982
+ */
5983
+ den(
5984
+ this: MathJsChain<number | BigNumber | bigint | Fraction>
5985
+ ): MathJsChain<bigint>
5986
+ den(
5987
+ this: MathJsChain<MathArray<MathScalarType>>
5988
+ ): MathJsChain<MathArray<bigint>>
5989
+ den(this: MathJsChain<Matrix<MathScalarType>>): MathJsChain<Matrix<bigint>>
5990
+
5945
5991
  /*************************************************************************
5946
5992
  * Geometry functions
5947
5993
  ************************************************************************/
@@ -7633,6 +7679,10 @@ export const {
7633
7679
  im,
7634
7680
  re,
7635
7681
 
7682
+ // fraction
7683
+ num,
7684
+ den,
7685
+
7636
7686
  // geometry
7637
7687
  distance,
7638
7688
  intersect,