mathjs 11.11.2 → 12.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. package/HISTORY.md +48 -0
  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/core/create.js +2 -2
  7. package/lib/cjs/defaultInstance.js +2 -2
  8. package/lib/cjs/entry/allFactoriesAny.js +2 -2
  9. package/lib/cjs/entry/allFactoriesNumber.js +2 -2
  10. package/lib/cjs/entry/dependenciesAny/dependenciesDet.generated.js +2 -2
  11. package/lib/cjs/entry/dependenciesAny/dependenciesDistance.generated.js +2 -2
  12. package/lib/cjs/entry/dependenciesAny/dependenciesEigs.generated.js +4 -0
  13. package/lib/cjs/entry/dependenciesAny/dependenciesLsolve.generated.js +2 -2
  14. package/lib/cjs/entry/dependenciesAny/dependenciesLsolveAll.generated.js +2 -2
  15. package/lib/cjs/entry/dependenciesAny/dependenciesLup.generated.js +2 -2
  16. package/lib/cjs/entry/dependenciesAny/dependenciesQr.generated.js +2 -2
  17. package/lib/cjs/entry/dependenciesAny/dependenciesStirlingS2.generated.js +2 -2
  18. package/lib/cjs/entry/dependenciesAny/dependenciesSubtract.generated.js +2 -2
  19. package/lib/cjs/entry/dependenciesAny/dependenciesSubtractScalar.generated.js +17 -0
  20. package/lib/cjs/entry/dependenciesAny/dependenciesUnitClass.generated.js +2 -2
  21. package/lib/cjs/entry/dependenciesAny/dependenciesUsolve.generated.js +2 -2
  22. package/lib/cjs/entry/dependenciesAny/dependenciesUsolveAll.generated.js +2 -2
  23. package/lib/cjs/entry/dependenciesAny.generated.js +7 -0
  24. package/lib/cjs/entry/dependenciesNumber/dependenciesStirlingS2.generated.js +2 -2
  25. package/lib/cjs/entry/dependenciesNumber/dependenciesSubtractScalar.generated.js +17 -0
  26. package/lib/cjs/entry/dependenciesNumber.generated.js +7 -0
  27. package/lib/cjs/entry/impureFunctionsAny.generated.js +4 -3
  28. package/lib/cjs/entry/impureFunctionsNumber.generated.js +2 -1
  29. package/lib/cjs/entry/pureFunctionsAny.generated.js +43 -37
  30. package/lib/cjs/entry/pureFunctionsNumber.generated.js +16 -13
  31. package/lib/cjs/expression/embeddedDocs/function/matrix/eigs.js +2 -2
  32. package/lib/cjs/expression/node/AssignmentNode.js +1 -1
  33. package/lib/cjs/expression/node/FunctionAssignmentNode.js +1 -1
  34. package/lib/cjs/expression/parse.js +50 -69
  35. package/lib/cjs/factoriesAny.js +7 -0
  36. package/lib/cjs/factoriesNumber.js +2 -1
  37. package/lib/cjs/function/algebra/decomposition/lup.js +3 -3
  38. package/lib/cjs/function/algebra/decomposition/qr.js +5 -5
  39. package/lib/cjs/function/algebra/solver/lsolve.js +4 -4
  40. package/lib/cjs/function/algebra/solver/lsolveAll.js +6 -6
  41. package/lib/cjs/function/algebra/solver/usolve.js +4 -4
  42. package/lib/cjs/function/algebra/solver/usolveAll.js +6 -6
  43. package/lib/cjs/function/arithmetic/subtract.js +7 -37
  44. package/lib/cjs/function/arithmetic/subtractScalar.js +52 -0
  45. package/lib/cjs/function/combinatorics/stirlingS2.js +2 -2
  46. package/lib/cjs/function/geometry/distance.js +10 -10
  47. package/lib/cjs/function/matrix/det.js +4 -4
  48. package/lib/cjs/function/matrix/eigs/complexEigs.js +73 -68
  49. package/lib/cjs/function/matrix/eigs/{realSymetric.js → realSymmetric.js} +57 -51
  50. package/lib/cjs/function/matrix/eigs.js +118 -45
  51. package/lib/cjs/header.js +2 -2
  52. package/lib/cjs/type/unit/Unit.js +5 -5
  53. package/lib/cjs/utils/number.js +1 -1
  54. package/lib/cjs/utils/object.js +4 -2
  55. package/lib/cjs/utils/snapshot.js +3 -3
  56. package/lib/cjs/utils/string.js +12 -16
  57. package/lib/cjs/version.js +1 -1
  58. package/lib/esm/entry/dependenciesAny/dependenciesDet.generated.js +2 -2
  59. package/lib/esm/entry/dependenciesAny/dependenciesDistance.generated.js +2 -2
  60. package/lib/esm/entry/dependenciesAny/dependenciesEigs.generated.js +4 -0
  61. package/lib/esm/entry/dependenciesAny/dependenciesLsolve.generated.js +2 -2
  62. package/lib/esm/entry/dependenciesAny/dependenciesLsolveAll.generated.js +2 -2
  63. package/lib/esm/entry/dependenciesAny/dependenciesLup.generated.js +2 -2
  64. package/lib/esm/entry/dependenciesAny/dependenciesQr.generated.js +2 -2
  65. package/lib/esm/entry/dependenciesAny/dependenciesStirlingS2.generated.js +2 -2
  66. package/lib/esm/entry/dependenciesAny/dependenciesSubtract.generated.js +2 -2
  67. package/lib/esm/entry/dependenciesAny/dependenciesSubtractScalar.generated.js +10 -0
  68. package/lib/esm/entry/dependenciesAny/dependenciesUnitClass.generated.js +2 -2
  69. package/lib/esm/entry/dependenciesAny/dependenciesUsolve.generated.js +2 -2
  70. package/lib/esm/entry/dependenciesAny/dependenciesUsolveAll.generated.js +2 -2
  71. package/lib/esm/entry/dependenciesAny.generated.js +1 -0
  72. package/lib/esm/entry/dependenciesNumber/dependenciesStirlingS2.generated.js +2 -2
  73. package/lib/esm/entry/dependenciesNumber/dependenciesSubtractScalar.generated.js +10 -0
  74. package/lib/esm/entry/dependenciesNumber.generated.js +1 -0
  75. package/lib/esm/entry/impureFunctionsAny.generated.js +5 -4
  76. package/lib/esm/entry/impureFunctionsNumber.generated.js +3 -2
  77. package/lib/esm/entry/pureFunctionsAny.generated.js +42 -37
  78. package/lib/esm/entry/pureFunctionsNumber.generated.js +16 -13
  79. package/lib/esm/expression/embeddedDocs/function/matrix/eigs.js +2 -2
  80. package/lib/esm/expression/node/AssignmentNode.js +1 -1
  81. package/lib/esm/expression/node/FunctionAssignmentNode.js +1 -1
  82. package/lib/esm/expression/parse.js +50 -69
  83. package/lib/esm/factoriesAny.js +1 -0
  84. package/lib/esm/factoriesNumber.js +1 -0
  85. package/lib/esm/function/algebra/decomposition/lup.js +3 -3
  86. package/lib/esm/function/algebra/decomposition/qr.js +5 -5
  87. package/lib/esm/function/algebra/solver/lsolve.js +4 -4
  88. package/lib/esm/function/algebra/solver/lsolveAll.js +6 -6
  89. package/lib/esm/function/algebra/solver/usolve.js +4 -4
  90. package/lib/esm/function/algebra/solver/usolveAll.js +6 -6
  91. package/lib/esm/function/arithmetic/subtract.js +7 -27
  92. package/lib/esm/function/arithmetic/subtractScalar.js +46 -0
  93. package/lib/esm/function/combinatorics/stirlingS2.js +2 -2
  94. package/lib/esm/function/geometry/distance.js +10 -10
  95. package/lib/esm/function/matrix/det.js +4 -4
  96. package/lib/esm/function/matrix/eigs/complexEigs.js +73 -68
  97. package/lib/esm/function/matrix/eigs/{realSymetric.js → realSymmetric.js} +55 -51
  98. package/lib/esm/function/matrix/eigs.js +119 -47
  99. package/lib/esm/type/unit/Unit.js +5 -5
  100. package/lib/esm/utils/number.js +1 -1
  101. package/lib/esm/utils/object.js +5 -3
  102. package/lib/esm/utils/snapshot.js +1 -1
  103. package/lib/esm/utils/string.js +12 -16
  104. package/lib/esm/version.js +1 -1
  105. package/package.json +14 -14
  106. package/types/EXPLANATION.md +54 -0
  107. package/types/index.d.ts +6797 -6446
@@ -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} */
@@ -9,13 +9,13 @@ import { endsWith } from '../../utils/string.js';
9
9
  import { clone, hasOwnProperty } from '../../utils/object.js';
10
10
  import { createBigNumberPi as createPi } from '../../utils/bignumber/constants.js';
11
11
  var name = 'Unit';
12
- var dependencies = ['?on', 'config', 'addScalar', 'subtract', 'multiplyScalar', 'divideScalar', 'pow', 'abs', 'fix', 'round', 'equal', 'isNumeric', 'format', 'number', 'Complex', 'BigNumber', 'Fraction'];
12
+ var dependencies = ['?on', 'config', 'addScalar', 'subtractScalar', 'multiplyScalar', 'divideScalar', 'pow', 'abs', 'fix', 'round', 'equal', 'isNumeric', 'format', 'number', 'Complex', 'BigNumber', 'Fraction'];
13
13
  export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref => {
14
14
  var {
15
15
  on,
16
16
  config,
17
17
  addScalar,
18
- subtract,
18
+ subtractScalar,
19
19
  multiplyScalar,
20
20
  divideScalar,
21
21
  pow,
@@ -741,7 +741,7 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
741
741
  var unitValue = convert(ret.units[0].unit.value);
742
742
  var nominalOffset = convert(ret.units[0].unit.offset);
743
743
  var unitOffset = multiplyScalar(unitValue, nominalOffset);
744
- ret.value = subtract(abs(addScalar(ret.value, unitOffset)), unitOffset);
744
+ ret.value = subtractScalar(abs(addScalar(ret.value, unitOffset)), unitOffset);
745
745
  }
746
746
  }
747
747
  for (var i in ret.units) {
@@ -786,7 +786,7 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
786
786
  var otherUnitValue = other.units[0].unit.value;
787
787
  var otherNominalOffset = other.units[0].unit.offset;
788
788
  var otherUnitOffset = multiplyScalar(otherUnitValue, otherNominalOffset);
789
- other.value = addScalar(value, convert(subtract(thisUnitOffset, otherUnitOffset)));
789
+ other.value = addScalar(value, convert(subtractScalar(thisUnitOffset, otherUnitOffset)));
790
790
  }
791
791
  other.fixPrefix = true;
792
792
  other.skipAutomaticSimplification = true;
@@ -1162,7 +1162,7 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
1162
1162
  }
1163
1163
  var y = new Unit(xFixed, parts[i].toString());
1164
1164
  ret.push(y);
1165
- x = subtract(x, y);
1165
+ x = subtractScalar(x, y);
1166
1166
  }
1167
1167
 
1168
1168
  // This little bit fixes a bug where the remainder should be 0 but is a little bit off.
@@ -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,4 +1,4 @@
1
- import { isBigNumber } from './is.js';
1
+ import { isBigNumber, isObject } from './is.js';
2
2
 
3
3
  /**
4
4
  * Clone an object
@@ -32,10 +32,12 @@ export function clone(x) {
32
32
  }
33
33
  if (x instanceof Date) return new Date(x.valueOf());
34
34
  if (isBigNumber(x)) return x; // bignumbers are immutable
35
- if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x); // TODO: clone a RegExp
36
35
 
37
36
  // object
38
- return mapObject(x, clone);
37
+ if (isObject(x)) {
38
+ return mapObject(x, clone);
39
+ }
40
+ throw new TypeError("Cannot clone: unknown type of value (value: ".concat(x, ")"));
39
41
  }
40
42
 
41
43
  /**
@@ -131,7 +131,7 @@ export function createSnapshotFromFactories(factories) {
131
131
  embeddedDocs[name] = 'Object';
132
132
  }
133
133
  });
134
- embeddedDocs = exclude(embeddedDocs, ['equalScalar', 'apply', 'addScalar', 'multiplyScalar', 'print', 'divideScalar', 'parse', 'compile', 'parser', 'chain', 'reviver', 'replacer']);
134
+ embeddedDocs = exclude(embeddedDocs, ['equalScalar', 'apply', 'addScalar', 'subtractScalar', 'multiplyScalar', 'print', 'divideScalar', 'parse', 'compile', 'parser', 'chain', 'reviver', 'replacer']);
135
135
  var allTypeChecks = {};
136
136
  Object.keys(allIsFunctions).forEach(name => {
137
137
  if (name.indexOf('is') === 0) {
@@ -81,7 +81,7 @@ function _format(value, options) {
81
81
  return formatArray(value, options);
82
82
  }
83
83
  if (isString(value)) {
84
- return '"' + value + '"';
84
+ return stringify(value);
85
85
  }
86
86
  if (typeof value === 'function') {
87
87
  return value.syntax ? String(value.syntax) : 'function';
@@ -94,7 +94,7 @@ function _format(value, options) {
94
94
  return value.toString(options);
95
95
  } else {
96
96
  var entries = Object.keys(value).map(key => {
97
- return '"' + key + '": ' + format(value[key], options);
97
+ return stringify(key) + ': ' + format(value[key], options);
98
98
  });
99
99
  return '{' + entries.join(', ') + '}';
100
100
  }
@@ -114,24 +114,20 @@ export function stringify(value) {
114
114
  var i = 0;
115
115
  while (i < text.length) {
116
116
  var c = text.charAt(i);
117
- if (c === '\\') {
118
- escaped += c;
119
- i++;
120
- c = text.charAt(i);
121
- if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) {
122
- escaped += '\\'; // no valid escape character -> escape it
123
- }
124
-
125
- escaped += c;
126
- } else if (c === '"') {
127
- escaped += '\\"';
128
- } else {
129
- escaped += c;
130
- }
117
+ escaped += c in controlCharacters ? controlCharacters[c] : c;
131
118
  i++;
132
119
  }
133
120
  return '"' + escaped + '"';
134
121
  }
122
+ var controlCharacters = {
123
+ '"': '\\"',
124
+ '\\': '\\\\',
125
+ '\b': '\\b',
126
+ '\f': '\\f',
127
+ '\n': '\\n',
128
+ '\r': '\\r',
129
+ '\t': '\\t'
130
+ };
135
131
 
136
132
  /**
137
133
  * Escape special HTML characters
@@ -1,3 +1,3 @@
1
- export var version = '11.11.2';
1
+ export var version = '12.0.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.11.2",
3
+ "version": "12.0.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",
@@ -25,7 +25,7 @@
25
25
  "unit"
26
26
  ],
27
27
  "dependencies": {
28
- "@babel/runtime": "^7.23.1",
28
+ "@babel/runtime": "^7.23.2",
29
29
  "complex.js": "^2.1.1",
30
30
  "decimal.js": "^10.4.3",
31
31
  "escape-latex": "^1.2.0",
@@ -36,27 +36,27 @@
36
36
  "typed-function": "^4.1.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@babel/core": "7.23.0",
39
+ "@babel/core": "7.23.2",
40
40
  "@babel/plugin-transform-object-assign": "7.22.5",
41
- "@babel/plugin-transform-runtime": "7.22.15",
42
- "@babel/preset-env": "7.22.20",
41
+ "@babel/plugin-transform-runtime": "7.23.2",
42
+ "@babel/preset-env": "7.23.2",
43
43
  "@babel/register": "7.22.15",
44
- "@types/assert": "1.5.7",
45
- "@types/mocha": "10.0.2",
46
- "@typescript-eslint/eslint-plugin": "6.7.5",
47
- "@typescript-eslint/parser": "6.7.5",
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",
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.0",
53
+ "core-js": "3.33.1",
54
54
  "del": "6.1.1",
55
55
  "dtslint": "4.2.1",
56
- "eslint": "8.51.0",
56
+ "eslint": "8.52.0",
57
57
  "eslint-config-prettier": "9.0.0",
58
58
  "eslint-config-standard": "17.1.0",
59
- "eslint-plugin-import": "2.28.1",
59
+ "eslint-plugin-import": "2.29.0",
60
60
  "eslint-plugin-mocha": "10.2.0",
61
61
  "eslint-plugin-n": "16.2.0",
62
62
  "eslint-plugin-prettier": "5.0.1",
@@ -90,7 +90,7 @@
90
90
  "sylvester": "0.0.21",
91
91
  "ts-node": "10.9.1",
92
92
  "typescript": "5.2.2",
93
- "webpack": "5.88.2",
93
+ "webpack": "5.89.0",
94
94
  "zeros": "1.0.0"
95
95
  },
96
96
  "type": "module",
@@ -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
+ ```