mathjs 11.12.0 → 12.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 (43) hide show
  1. package/HISTORY.md +54 -2
  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/dependenciesEigs.generated.js +4 -0
  7. package/lib/cjs/entry/pureFunctionsAny.generated.js +2 -0
  8. package/lib/cjs/expression/embeddedDocs/function/arithmetic/round.js +2 -2
  9. package/lib/cjs/expression/embeddedDocs/function/matrix/eigs.js +2 -2
  10. package/lib/cjs/expression/node/AssignmentNode.js +1 -1
  11. package/lib/cjs/expression/node/FunctionAssignmentNode.js +1 -1
  12. package/lib/cjs/function/algebra/derivative.js +8 -31
  13. package/lib/cjs/function/arithmetic/gcd.js +4 -5
  14. package/lib/cjs/function/arithmetic/mod.js +2 -9
  15. package/lib/cjs/function/arithmetic/round.js +59 -16
  16. package/lib/cjs/function/matrix/eigs/complexEigs.js +73 -68
  17. package/lib/cjs/function/matrix/eigs/{realSymetric.js → realSymmetric.js} +57 -51
  18. package/lib/cjs/function/matrix/eigs.js +118 -45
  19. package/lib/cjs/function/probability/pickRandom.js +2 -2
  20. package/lib/cjs/header.js +2 -2
  21. package/lib/cjs/type/number.js +2 -2
  22. package/lib/cjs/utils/number.js +1 -1
  23. package/lib/cjs/utils/snapshot.js +6 -6
  24. package/lib/cjs/version.js +1 -1
  25. package/lib/esm/entry/dependenciesAny/dependenciesEigs.generated.js +4 -0
  26. package/lib/esm/entry/pureFunctionsAny.generated.js +2 -0
  27. package/lib/esm/expression/embeddedDocs/function/arithmetic/round.js +2 -2
  28. package/lib/esm/expression/embeddedDocs/function/matrix/eigs.js +2 -2
  29. package/lib/esm/expression/node/AssignmentNode.js +1 -1
  30. package/lib/esm/expression/node/FunctionAssignmentNode.js +1 -1
  31. package/lib/esm/function/algebra/derivative.js +8 -31
  32. package/lib/esm/function/arithmetic/mod.js +2 -9
  33. package/lib/esm/function/arithmetic/round.js +40 -17
  34. package/lib/esm/function/matrix/eigs/complexEigs.js +73 -68
  35. package/lib/esm/function/matrix/eigs/{realSymetric.js → realSymmetric.js} +55 -51
  36. package/lib/esm/function/matrix/eigs.js +119 -47
  37. package/lib/esm/function/probability/pickRandom.js +2 -2
  38. package/lib/esm/type/number.js +2 -2
  39. package/lib/esm/utils/number.js +1 -1
  40. package/lib/esm/version.js +1 -1
  41. package/package.json +14 -14
  42. package/types/EXPLANATION.md +54 -0
  43. package/types/index.d.ts +6825 -6483
@@ -1,12 +1,13 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
1
2
  import { factory } from '../../utils/factory.js';
2
3
  import { format } from '../../utils/string.js';
3
4
  import { createComplexEigs } from './eigs/complexEigs.js';
4
- import { createRealSymmetric } from './eigs/realSymetric.js';
5
+ import { createRealSymmetric } from './eigs/realSymmetric.js';
5
6
  import { typeOf, isNumber, isBigNumber, isComplex, isFraction } from '../../utils/is.js';
6
7
  var name = 'eigs';
7
8
 
8
9
  // The absolute state of math.js's dependency system:
9
- var dependencies = ['config', 'typed', 'matrix', 'addScalar', 'equal', 'subtract', 'abs', 'atan', 'cos', 'sin', 'multiplyScalar', 'divideScalar', 'inv', 'bignumber', 'multiply', 'add', 'larger', 'column', 'flatten', 'number', 'complex', 'sqrt', 'diag', 'qr', 'usolve', 'usolveAll', 'im', 're', 'smaller', 'matrixFromColumns', 'dot'];
10
+ var dependencies = ['config', 'typed', 'matrix', 'addScalar', 'equal', 'subtract', 'abs', 'atan', 'cos', 'sin', 'multiplyScalar', 'divideScalar', 'inv', 'bignumber', 'multiply', 'add', 'larger', 'column', 'flatten', 'number', 'complex', 'sqrt', 'diag', 'size', 'reshape', 'qr', 'usolve', 'usolveAll', 'im', 're', 'smaller', 'matrixFromColumns', 'dot'];
10
11
  export var createEigs = /* #__PURE__ */factory(name, dependencies, _ref => {
11
12
  var {
12
13
  config,
@@ -32,6 +33,8 @@ export var createEigs = /* #__PURE__ */factory(name, dependencies, _ref => {
32
33
  complex,
33
34
  sqrt,
34
35
  diag,
36
+ size,
37
+ reshape,
35
38
  qr,
36
39
  usolve,
37
40
  usolveAll,
@@ -41,7 +44,7 @@ export var createEigs = /* #__PURE__ */factory(name, dependencies, _ref => {
41
44
  matrixFromColumns,
42
45
  dot
43
46
  } = _ref;
44
- var doRealSymetric = createRealSymmetric({
47
+ var doRealSymmetric = createRealSymmetric({
45
48
  config,
46
49
  addScalar,
47
50
  subtract,
@@ -71,6 +74,8 @@ export var createEigs = /* #__PURE__ */factory(name, dependencies, _ref => {
71
74
  abs,
72
75
  bignumber,
73
76
  diag,
77
+ size,
78
+ reshape,
74
79
  qr,
75
80
  inv,
76
81
  usolve,
@@ -84,26 +89,54 @@ export var createEigs = /* #__PURE__ */factory(name, dependencies, _ref => {
84
89
  });
85
90
 
86
91
  /**
87
- * Compute eigenvalues and eigenvectors of a matrix. The eigenvalues are sorted by their absolute value, ascending.
88
- * An eigenvalue with multiplicity k will be listed k times. The eigenvectors are returned as columns of a matrix
89
- * the eigenvector that belongs to the j-th eigenvalue in the list (eg. `values[j]`) is the j-th column (eg. `column(vectors, j)`).
90
- * If the algorithm fails to converge, it will throw an error – in that case, however, you may still find useful information
92
+ * Compute eigenvalues and optionally eigenvectors of a square matrix.
93
+ * The eigenvalues are sorted by their absolute value, ascending, and
94
+ * returned as a vector in the `values` property of the returned project.
95
+ * An eigenvalue with algebraic multiplicity k will be listed k times, so
96
+ * that the returned `values` vector always has length equal to the size
97
+ * of the input matrix.
98
+ *
99
+ * The `eigenvectors` property of the return value provides the eigenvectors.
100
+ * It is an array of plain objects: the `value` property of each gives the
101
+ * associated eigenvalue, and the `vector` property gives the eigenvector
102
+ * itself. Note that the same `value` property will occur as many times in
103
+ * the list provided by `eigenvectors` as the geometric multiplicity of
104
+ * that value.
105
+ *
106
+ * If the algorithm fails to converge, it will throw an error –
107
+ * in that case, however, you may still find useful information
91
108
  * in `err.values` and `err.vectors`.
92
109
  *
110
+ * Note that the 'precision' option does not directly specify the _accuracy_
111
+ * of the returned eigenvalues. Rather, it determines how small an entry
112
+ * of the iterative approximations to an upper triangular matrix must be
113
+ * in order to be considered zero. The actual accuracy of the returned
114
+ * eigenvalues may be greater or less than the precision, depending on the
115
+ * conditioning of the matrix and how far apart or close the actual
116
+ * eigenvalues are. Note that currently, relatively simple, "traditional"
117
+ * methods of eigenvalue computation are being used; this is not a modern,
118
+ * high-precision eigenvalue computation. That said, it should typically
119
+ * produce fairly reasonable results.
120
+ *
93
121
  * Syntax:
94
122
  *
95
123
  * math.eigs(x, [prec])
124
+ * math.eigs(x, {options})
96
125
  *
97
126
  * Examples:
98
127
  *
99
- * const { eigs, multiply, column, transpose } = math
128
+ * const { eigs, multiply, column, transpose, matrixFromColumns } = math
100
129
  * const H = [[5, 2.3], [2.3, 1]]
101
- * const ans = eigs(H) // returns {values: [E1,E2...sorted], vectors: [v1,v2.... corresponding vectors as columns]}
130
+ * const ans = eigs(H) // returns {values: [E1,E2...sorted], eigenvectors: [{value: E1, vector: v2}, {value: e, vector: v2}, ...]
102
131
  * const E = ans.values
103
- * const U = ans.vectors
104
- * multiply(H, column(U, 0)) // returns multiply(E[0], column(U, 0))
105
- * const UTxHxU = multiply(transpose(U), H, U) // diagonalizes H
106
- * E[0] == UTxHxU[0][0] // returns true
132
+ * const V = ans.eigenvectors
133
+ * multiply(H, V[0].vector)) // returns multiply(E[0], V[0].vector))
134
+ * const U = matrixFromColumns(...V.map(obj => obj.vector))
135
+ * const UTxHxU = multiply(transpose(U), H, U) // diagonalizes H if possible
136
+ * E[0] == UTxHxU[0][0] // returns true always
137
+ *
138
+ * // Compute only approximate eigenvalues:
139
+ * const {values} = eigs(H, {eigenvectors: false, precision: 1e-6})
107
140
  *
108
141
  * See also:
109
142
  *
@@ -111,59 +144,98 @@ export var createEigs = /* #__PURE__ */factory(name, dependencies, _ref => {
111
144
  *
112
145
  * @param {Array | Matrix} x Matrix to be diagonalized
113
146
  *
114
- * @param {number | BigNumber} [prec] Precision, default value: 1e-15
115
- * @return {{values: Array|Matrix, vectors: Array|Matrix}} Object containing an array of eigenvalues and a matrix with eigenvectors as columns.
147
+ * @param {number | BigNumber | OptsObject} [opts] Object with keys `precision`, defaulting to config.epsilon, and `eigenvectors`, defaulting to true and specifying whether to compute eigenvectors. If just a number, specifies precision.
148
+ * @return {{values: Array|Matrix, eigenvectors?: Array<EVobj>}} Object containing an array of eigenvalues and an array of {value: number|BigNumber, vector: Array|Matrix} objects. The eigenvectors property is undefined if eigenvectors were not requested.
116
149
  *
117
150
  */
118
151
  return typed('eigs', {
152
+ // The conversion to matrix in the first two implementations,
153
+ // just to convert back to an array right away in
154
+ // computeValuesAndVectors, is unfortunate, and should perhaps be
155
+ // streamlined. It is done because the Matrix object carries some
156
+ // type information about its entries, and so constructing the matrix
157
+ // is a roundabout way of doing type detection.
119
158
  Array: function Array(x) {
120
- var mat = matrix(x);
121
- return computeValuesAndVectors(mat);
159
+ return doEigs(matrix(x));
122
160
  },
123
161
  'Array, number|BigNumber': function ArrayNumberBigNumber(x, prec) {
124
- var mat = matrix(x);
125
- return computeValuesAndVectors(mat, prec);
162
+ return doEigs(matrix(x), {
163
+ precision: prec
164
+ });
165
+ },
166
+ 'Array, Object'(x, opts) {
167
+ return doEigs(matrix(x), opts);
126
168
  },
127
169
  Matrix: function Matrix(mat) {
128
- var {
129
- values,
130
- vectors
131
- } = computeValuesAndVectors(mat);
132
- return {
133
- values: matrix(values),
134
- vectors: matrix(vectors)
135
- };
170
+ return doEigs(mat, {
171
+ matricize: true
172
+ });
136
173
  },
137
174
  'Matrix, number|BigNumber': function MatrixNumberBigNumber(mat, prec) {
138
- var {
139
- values,
140
- vectors
141
- } = computeValuesAndVectors(mat, prec);
142
- return {
143
- values: matrix(values),
144
- vectors: matrix(vectors)
175
+ return doEigs(mat, {
176
+ precision: prec,
177
+ matricize: true
178
+ });
179
+ },
180
+ 'Matrix, Object': function MatrixObject(mat, opts) {
181
+ var useOpts = {
182
+ matricize: true
145
183
  };
184
+ _extends(useOpts, opts);
185
+ return doEigs(mat, useOpts);
146
186
  }
147
187
  });
148
- function computeValuesAndVectors(mat, prec) {
149
- if (prec === undefined) {
150
- prec = config.epsilon;
188
+ function doEigs(mat) {
189
+ var _opts$precision;
190
+ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
191
+ var computeVectors = 'eigenvectors' in opts ? opts.eigenvectors : true;
192
+ var prec = (_opts$precision = opts.precision) !== null && _opts$precision !== void 0 ? _opts$precision : config.epsilon;
193
+ var result = computeValuesAndVectors(mat, prec, computeVectors);
194
+ if (opts.matricize) {
195
+ result.values = matrix(result.values);
196
+ if (computeVectors) {
197
+ result.eigenvectors = result.eigenvectors.map(_ref2 => {
198
+ var {
199
+ value,
200
+ vector
201
+ } = _ref2;
202
+ return {
203
+ value,
204
+ vector: matrix(vector)
205
+ };
206
+ });
207
+ }
208
+ }
209
+ if (computeVectors) {
210
+ Object.defineProperty(result, 'vectors', {
211
+ enumerable: false,
212
+ // to make sure that the eigenvectors can still be
213
+ // converted to string.
214
+ get: () => {
215
+ throw new Error('eigs(M).vectors replaced with eigs(M).eigenvectors');
216
+ }
217
+ });
151
218
  }
152
- var size = mat.size();
153
- if (size.length !== 2 || size[0] !== size[1]) {
154
- throw new RangeError('Matrix must be square (size: ' + format(size) + ')');
219
+ return result;
220
+ }
221
+ function computeValuesAndVectors(mat, prec, computeVectors) {
222
+ var arr = mat.toArray(); // NOTE: arr is guaranteed to be unaliased
223
+ // and so safe to modify in place
224
+ var asize = mat.size();
225
+ if (asize.length !== 2 || asize[0] !== asize[1]) {
226
+ throw new RangeError("Matrix must be square (size: ".concat(format(asize), ")"));
155
227
  }
156
- var arr = mat.toArray();
157
- var N = size[0];
228
+ var N = asize[0];
158
229
  if (isReal(arr, N, prec)) {
159
- coerceReal(arr, N);
230
+ coerceReal(arr, N); // modifies arr by side effect
231
+
160
232
  if (isSymmetric(arr, N, prec)) {
161
- var _type = coerceTypes(mat, arr, N);
162
- return doRealSymetric(arr, N, prec, _type);
233
+ var _type = coerceTypes(mat, arr, N); // modifies arr by side effect
234
+ return doRealSymmetric(arr, N, prec, _type, computeVectors);
163
235
  }
164
236
  }
165
- var type = coerceTypes(mat, arr, N);
166
- return doComplexEigs(arr, N, prec, type);
237
+ var type = coerceTypes(mat, arr, N); // modifies arr by side effect
238
+ return doComplexEigs(arr, N, prec, type, computeVectors);
167
239
  }
168
240
 
169
241
  /** @return {boolean} */
@@ -52,8 +52,8 @@ export var createPickRandom = /* #__PURE__ */factory(name, dependencies, _ref =>
52
52
  * @param {Array | Matrix} array A one dimensional array
53
53
  * @param {Int} number An int or float
54
54
  * @param {Array | Matrix} weights An array of ints or floats
55
- * @return {number | Array} Returns a single random value from array when number is 1 or undefined.
56
- * Returns an array with the configured number of elements when number is > 1.
55
+ * @return {number | Array} Returns a single random value from array when number is undefined.
56
+ * Returns an array with the configured number of elements when number is defined.
57
57
  */
58
58
  return typed(name, {
59
59
  'Array | Matrix': function ArrayMatrix(possibles) {
@@ -43,7 +43,7 @@ function makeNumberFromNonDecimalParts(parts) {
43
43
  }
44
44
  var result = n + f;
45
45
  if (isNaN(result)) {
46
- throw new SyntaxError('String "' + parts.input + '" is no valid number');
46
+ throw new SyntaxError('String "' + parts.input + '" is not a valid number');
47
47
  }
48
48
  return result;
49
49
  }
@@ -99,7 +99,7 @@ export var createNumber = /* #__PURE__ */factory(name, dependencies, _ref => {
99
99
  }
100
100
  var num = Number(x);
101
101
  if (isNaN(num)) {
102
- throw new SyntaxError('String "' + x + '" is no valid number');
102
+ throw new SyntaxError('String "' + x + '" is not a valid number');
103
103
  }
104
104
  if (wordSizeSuffixMatch) {
105
105
  // x is a signed bin, oct, or hex literal
@@ -583,7 +583,7 @@ export function nearlyEqual(x, y, epsilon) {
583
583
  if (isFinite(x) && isFinite(y)) {
584
584
  // check numbers are very close, needed when comparing numbers near zero
585
585
  var diff = Math.abs(x - y);
586
- if (diff < DBL_EPSILON) {
586
+ if (diff <= DBL_EPSILON) {
587
587
  return true;
588
588
  } else {
589
589
  // use relative error
@@ -1,3 +1,3 @@
1
- export var version = '11.12.0';
1
+ export var version = '12.1.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": "11.12.0",
3
+ "version": "12.1.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",
@@ -36,29 +36,29 @@
36
36
  "typed-function": "^4.1.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@babel/core": "7.23.2",
40
- "@babel/plugin-transform-object-assign": "7.22.5",
41
- "@babel/plugin-transform-runtime": "7.23.2",
42
- "@babel/preset-env": "7.23.2",
39
+ "@babel/core": "7.23.3",
40
+ "@babel/plugin-transform-object-assign": "7.23.3",
41
+ "@babel/plugin-transform-runtime": "7.23.3",
42
+ "@babel/preset-env": "7.23.3",
43
43
  "@babel/register": "7.22.15",
44
- "@types/assert": "1.5.8",
45
- "@types/mocha": "10.0.3",
46
- "@typescript-eslint/eslint-plugin": "6.9.0",
47
- "@typescript-eslint/parser": "6.9.0",
44
+ "@types/assert": "1.5.9",
45
+ "@types/mocha": "10.0.4",
46
+ "@typescript-eslint/eslint-plugin": "6.11.0",
47
+ "@typescript-eslint/parser": "6.11.0",
48
48
  "assert": "2.1.0",
49
49
  "babel-loader": "9.1.3",
50
50
  "benchmark": "2.1.4",
51
51
  "c8": "8.0.1",
52
52
  "codecov": "3.8.3",
53
- "core-js": "3.33.1",
53
+ "core-js": "3.33.2",
54
54
  "del": "6.1.1",
55
55
  "dtslint": "4.2.1",
56
- "eslint": "8.52.0",
56
+ "eslint": "8.53.0",
57
57
  "eslint-config-prettier": "9.0.0",
58
58
  "eslint-config-standard": "17.1.0",
59
59
  "eslint-plugin-import": "2.29.0",
60
60
  "eslint-plugin-mocha": "10.2.0",
61
- "eslint-plugin-n": "16.2.0",
61
+ "eslint-plugin-n": "16.3.1",
62
62
  "eslint-plugin-prettier": "5.0.1",
63
63
  "eslint-plugin-promise": "6.1.1",
64
64
  "expect-type": "0.17.3",
@@ -85,7 +85,7 @@
85
85
  "ndarray-pack": "1.2.1",
86
86
  "numericjs": "1.2.6",
87
87
  "pad-right": "0.2.2",
88
- "prettier": "3.0.3",
88
+ "prettier": "3.1.0",
89
89
  "process": "0.11.10",
90
90
  "sylvester": "0.0.21",
91
91
  "ts-node": "10.9.1",
@@ -169,7 +169,7 @@
169
169
  "mathjs": "./bin/cli.js"
170
170
  },
171
171
  "engines": {
172
- "node": ">= 14"
172
+ "node": ">= 18"
173
173
  },
174
174
  "bugs": {
175
175
  "url": "https://github.com/josdejong/mathjs/issues"
@@ -0,0 +1,54 @@
1
+ # Mathjs TypeScript types
2
+
3
+ The code base of Mathjs is writting in JavaScript. The TypeScript definitions are maintained separately.
4
+
5
+ ## Library structure
6
+
7
+ The over all structure is:
8
+
9
+ - The library exports the core function `create` which creates a MathJS instance and returns `MathJsInstance`.
10
+ - Mathjs has a special function `chain`, which allows you to use the functions in a chained way, like `chain(2).add(3).done()`. The function `chain` returns an interface `MathJsChain`, which contains all mathjs functions and constants as a method. Unlike the static functions, these methods are defined with the chain instance `this` as first argument.
11
+ - The library exports collections with factory functions of all functions and their dependencies. To create an instance of the function `add`, one can do `create(addDependencies)` for example. To import all functions, one can do `create(all)`.
12
+ - The library also returns a static instance, which can directly be used like `import { add } from 'mathjs'`.
13
+
14
+ ## Defining the types of a new function
15
+
16
+ Maintaining the TypeScript types is done manually. When adding a function, one has to create the following TypeScript definitions:
17
+
18
+ 1. Add a normal definition inside `interface MathJsInstance {...}`
19
+ 2. Add a chained definition inside `interface MathJsChain {...}`
20
+ 3. Add a static definition inside `export const {...} : MathJsInstance`
21
+ 4. Add a dependencies definition inside `export const {...} : Record<string, FactoryFunctionMap>`
22
+
23
+ For exampe for the function `add`, we can have the following definitions:
24
+
25
+ ```ts
26
+ // instance
27
+ export interface MathJsInstance extends MathJsFactory {
28
+ //...
29
+ add<T extends MathType>(x: T, y: T): T
30
+ //...
31
+ }
32
+
33
+ // chain
34
+ export interface MathJsChain<TValue> {
35
+ //...
36
+ add<T extends MathType>(this: MathJsChain<T>, y: T): MathJsChain<T>
37
+ //...
38
+ }
39
+
40
+ // static
41
+ export const {
42
+ // ...
43
+ add,
44
+ // ...
45
+ } : MathJsInstance
46
+
47
+
48
+ // dependencies
49
+ export const {
50
+ // ...
51
+ addDependencies,
52
+ // ...
53
+ } : Record<string, FactoryFunctionMap>
54
+ ```