mathjs 10.2.0 → 10.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. package/HISTORY.md +43 -0
  2. package/docs/expressions/syntax.md +31 -2
  3. package/docs/reference/functions/cumsum.md +57 -0
  4. package/docs/reference/functions/format.md +1 -1
  5. package/docs/reference/functions/map.md +22 -5
  6. package/docs/reference/functions/subset.md +10 -2
  7. package/docs/reference/functions/sum.md +2 -1
  8. package/docs/reference/functions/symbolicEqual.md +62 -0
  9. package/docs/reference/functions.md +3 -1
  10. package/lib/browser/math.js +6 -6
  11. package/lib/browser/math.js.map +1 -1
  12. package/lib/cjs/entry/dependenciesAny/dependenciesCumSum.generated.js +26 -0
  13. package/lib/cjs/entry/dependenciesAny/dependenciesCumSumTransform.generated.js +26 -0
  14. package/lib/cjs/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +29 -0
  15. package/lib/cjs/entry/dependenciesAny.generated.js +24 -0
  16. package/lib/cjs/entry/dependenciesNumber/dependenciesCumSum.generated.js +26 -0
  17. package/lib/cjs/entry/dependenciesNumber/dependenciesCumSumTransform.generated.js +26 -0
  18. package/lib/cjs/entry/dependenciesNumber.generated.js +16 -0
  19. package/lib/cjs/entry/impureFunctionsAny.generated.js +22 -8
  20. package/lib/cjs/entry/impureFunctionsNumber.generated.js +6 -0
  21. package/lib/cjs/entry/pureFunctionsAny.generated.js +18 -12
  22. package/lib/cjs/entry/pureFunctionsNumber.generated.js +8 -2
  23. package/lib/cjs/expression/embeddedDocs/construction/fraction.js +3 -3
  24. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +240 -234
  25. package/lib/cjs/expression/embeddedDocs/function/algebra/symbolicEqual.js +15 -0
  26. package/lib/cjs/expression/embeddedDocs/function/matrix/subset.js +2 -2
  27. package/lib/cjs/expression/embeddedDocs/function/statistics/cumsum.js +15 -0
  28. package/lib/cjs/expression/node/FunctionNode.js +74 -55
  29. package/lib/cjs/expression/parse.js +12 -8
  30. package/lib/cjs/expression/transform/cumsum.transform.js +57 -0
  31. package/lib/cjs/expression/transform/sum.transform.js +1 -1
  32. package/lib/cjs/factoriesAny.js +24 -0
  33. package/lib/cjs/factoriesNumber.js +18 -2
  34. package/lib/cjs/function/algebra/simplify.js +8 -0
  35. package/lib/cjs/function/algebra/simplifyCore.js +2 -2
  36. package/lib/cjs/function/algebra/symbolicEqual.js +88 -0
  37. package/lib/cjs/function/matrix/eigs/complexEigs.js +39 -28
  38. package/lib/cjs/function/matrix/map.js +53 -15
  39. package/lib/cjs/function/matrix/subset.js +15 -5
  40. package/lib/cjs/function/statistics/cumsum.js +151 -0
  41. package/lib/cjs/function/statistics/sum.js +1 -1
  42. package/lib/cjs/function/string/format.js +1 -1
  43. package/lib/cjs/header.js +2 -2
  44. package/lib/cjs/type/fraction/function/fraction.js +20 -8
  45. package/lib/cjs/utils/collection.js +3 -27
  46. package/lib/cjs/utils/switch.js +31 -0
  47. package/lib/cjs/version.js +1 -1
  48. package/lib/esm/entry/dependenciesAny/dependenciesCumSum.generated.js +14 -0
  49. package/lib/esm/entry/dependenciesAny/dependenciesCumSumTransform.generated.js +14 -0
  50. package/lib/esm/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +16 -0
  51. package/lib/esm/entry/dependenciesAny.generated.js +3 -0
  52. package/lib/esm/entry/dependenciesNumber/dependenciesCumSum.generated.js +14 -0
  53. package/lib/esm/entry/dependenciesNumber/dependenciesCumSumTransform.generated.js +14 -0
  54. package/lib/esm/entry/dependenciesNumber.generated.js +2 -0
  55. package/lib/esm/entry/impureFunctionsAny.generated.js +22 -9
  56. package/lib/esm/entry/impureFunctionsNumber.generated.js +8 -2
  57. package/lib/esm/entry/pureFunctionsAny.generated.js +13 -8
  58. package/lib/esm/entry/pureFunctionsNumber.generated.js +6 -1
  59. package/lib/esm/expression/embeddedDocs/construction/fraction.js +3 -3
  60. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +220 -216
  61. package/lib/esm/expression/embeddedDocs/function/algebra/symbolicEqual.js +8 -0
  62. package/lib/esm/expression/embeddedDocs/function/matrix/subset.js +2 -2
  63. package/lib/esm/expression/embeddedDocs/function/statistics/cumsum.js +8 -0
  64. package/lib/esm/expression/node/FunctionNode.js +70 -53
  65. package/lib/esm/expression/parse.js +12 -8
  66. package/lib/esm/expression/transform/cumsum.transform.js +48 -0
  67. package/lib/esm/expression/transform/sum.transform.js +1 -1
  68. package/lib/esm/factoriesAny.js +3 -0
  69. package/lib/esm/factoriesNumber.js +2 -0
  70. package/lib/esm/function/algebra/simplify.js +8 -0
  71. package/lib/esm/function/algebra/simplifyCore.js +2 -2
  72. package/lib/esm/function/algebra/symbolicEqual.js +80 -0
  73. package/lib/esm/function/matrix/eigs/complexEigs.js +36 -25
  74. package/lib/esm/function/matrix/map.js +53 -15
  75. package/lib/esm/function/matrix/subset.js +15 -5
  76. package/lib/esm/function/statistics/cumsum.js +139 -0
  77. package/lib/esm/function/statistics/sum.js +1 -1
  78. package/lib/esm/function/string/format.js +1 -1
  79. package/lib/esm/type/fraction/function/fraction.js +20 -8
  80. package/lib/esm/utils/collection.js +1 -26
  81. package/lib/esm/utils/switch.js +24 -0
  82. package/lib/esm/version.js +1 -1
  83. package/package.json +15 -11
  84. package/types/index.d.ts +209 -23
  85. package/types/index.ts +274 -57
  86. package/types/tsconfig.json +2 -1
@@ -28,16 +28,26 @@ export var createSubset = /* #__PURE__ */factory(name, dependencies, _ref => {
28
28
  *
29
29
  * // replace a subset
30
30
  * const e = []
31
- * const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]]
31
+ * const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]] and e = [[5, 0, 6]]
32
32
  * const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]]
33
33
  *
34
+ * // get submatrix using ranges
35
+ * const M = [
36
+ * [1,2,3],
37
+ * [4,5,6],
38
+ * [7,8,9]
39
+ * ]
40
+ * math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1,2,3],[4,5,6]]
41
+ *
34
42
  * See also:
35
43
  *
36
44
  * size, resize, squeeze, index
37
45
  *
38
46
  * @param {Array | Matrix | string} matrix An array, matrix, or string
39
- * @param {Index} index An index containing ranges for each
40
- * dimension
47
+ * @param {Index} index
48
+ * For each dimension of the target, specifies an index or a list of
49
+ * indices to fetch or set. `subset` uses the cartesian product of
50
+ * the indices specified in each dimension.
41
51
  * @param {*} [replacement] An array, matrix, or scalar.
42
52
  * If provided, the subset is replaced with replacement.
43
53
  * If not provided, the subset is returned
@@ -80,7 +90,7 @@ export var createSubset = /* #__PURE__ */factory(name, dependencies, _ref => {
80
90
  /**
81
91
  * Retrieve a subset of a string
82
92
  * @param {string} str string from which to get a substring
83
- * @param {Index} index An index containing ranges for each dimension
93
+ * @param {Index} index An index or list of indices (character positions)
84
94
  * @returns {string} substring
85
95
  * @private
86
96
  */
@@ -109,7 +119,7 @@ function _getSubstring(str, index) {
109
119
  /**
110
120
  * Replace a substring in a string
111
121
  * @param {string} str string to be replaced
112
- * @param {Index} index An index containing ranges for each dimension
122
+ * @param {Index} index An index or list of indices (character positions)
113
123
  * @param {string} replacement Replacement string
114
124
  * @param {string} [defaultValue] Default value to be uses when resizing
115
125
  * the string. is ' ' by default
@@ -0,0 +1,139 @@
1
+ import { containsCollections } from '../../utils/collection.js';
2
+ import { factory } from '../../utils/factory.js';
3
+ import { _switch } from '../../utils/switch.js';
4
+ import { improveErrorMessage } from './utils/improveErrorMessage.js';
5
+ import { arraySize } from '../../utils/array.js';
6
+ import { IndexError } from '../../error/IndexError.js';
7
+ var name = 'cumsum';
8
+ var dependencies = ['typed', 'add', 'unaryPlus'];
9
+ export var createCumSum = /* #__PURE__ */factory(name, dependencies, _ref => {
10
+ var {
11
+ typed,
12
+ add,
13
+ unaryPlus
14
+ } = _ref;
15
+
16
+ /**
17
+ * Compute the cumulative sum of a matrix or a list with values.
18
+ * In case of a (multi dimensional) array or matrix, the cumulative sums
19
+ * along a specified dimension (defaulting to the first) will be calculated.
20
+ *
21
+ * Syntax:
22
+ *
23
+ * math.cumsum(a, b, c, ...)
24
+ * math.cumsum(A)
25
+ *
26
+ * Examples:
27
+ *
28
+ * math.cumsum(2, 1, 4, 3) // returns [2, 3, 7, 10]
29
+ * math.cumsum([2, 1, 4, 3]) // returns [2, 3, 7, 10]
30
+ * math.cumsum([[1, 2], [3, 4]]) // returns [[1, 2], [4, 6]]
31
+ * math.cumsum([[1, 2], [3, 4]], 0) // returns [[1, 2], [4, 6]]
32
+ * math.cumsum([[1, 2], [3, 4]], 1) // returns [[1, 3], [3, 7]]
33
+ * math.cumsum([[2, 5], [4, 3], [1, 7]]) // returns [[2, 5], [6, 8], [7, 15]]
34
+ *
35
+ * See also:
36
+ *
37
+ * mean, median, min, max, prod, std, variance, sum
38
+ *
39
+ * @param {... *} args A single matrix or or multiple scalar values
40
+ * @return {*} The cumulative sum of all values
41
+ */
42
+ return typed(name, {
43
+ // sum([a, b, c, d, ...])
44
+ Array: _cumsum,
45
+ Matrix: function Matrix(matrix) {
46
+ return matrix.create(_cumsum(matrix.valueOf()));
47
+ },
48
+ // sum([a, b, c, d, ...], dim)
49
+ 'Array, number | BigNumber': _ncumSumDim,
50
+ 'Matrix, number | BigNumber': function MatrixNumberBigNumber(matrix, dim) {
51
+ return matrix.create(_ncumSumDim(matrix.valueOf(), dim));
52
+ },
53
+ // cumsum(a, b, c, d, ...)
54
+ '...': function _(args) {
55
+ if (containsCollections(args)) {
56
+ throw new TypeError('All values expected to be scalar in function cumsum');
57
+ }
58
+
59
+ return _cumsum(args);
60
+ }
61
+ });
62
+ /**
63
+ * Recursively calculate the cumulative sum of an n-dimensional array
64
+ * @param {Array} array
65
+ * @return {number} cumsum
66
+ * @private
67
+ */
68
+
69
+ function _cumsum(array) {
70
+ try {
71
+ return _cumsummap(array);
72
+ } catch (err) {
73
+ throw improveErrorMessage(err, name);
74
+ }
75
+ }
76
+
77
+ function _cumsummap(array) {
78
+ if (array.length === 0) {
79
+ return [];
80
+ }
81
+
82
+ var sums = [unaryPlus(array[0])]; // unaryPlus converts to number if need be
83
+
84
+ for (var i = 1; i < array.length; ++i) {
85
+ // Must use add below and not addScalar for the case of summing a
86
+ // 2+-dimensional array along the 0th dimension (the row vectors,
87
+ // or higher-d analogues, are literally added to each other).
88
+ sums.push(add(sums[i - 1], array[i]));
89
+ }
90
+
91
+ return sums;
92
+ }
93
+
94
+ function _ncumSumDim(array, dim) {
95
+ var size = arraySize(array);
96
+
97
+ if (dim < 0 || dim >= size.length) {
98
+ // TODO: would be more clear when throwing a DimensionError here
99
+ throw new IndexError(dim, size.length);
100
+ }
101
+
102
+ try {
103
+ return _cumsumDimensional(array, dim);
104
+ } catch (err) {
105
+ throw improveErrorMessage(err, name);
106
+ }
107
+ }
108
+ /* Possible TODO: Refactor _reduce in collection.js to be able to work here as well */
109
+
110
+
111
+ function _cumsumDimensional(mat, dim) {
112
+ var i, ret, tran;
113
+
114
+ if (dim <= 0) {
115
+ var initialValue = mat[0][0];
116
+
117
+ if (!Array.isArray(initialValue)) {
118
+ return _cumsummap(mat);
119
+ } else {
120
+ tran = _switch(mat);
121
+ ret = [];
122
+
123
+ for (i = 0; i < tran.length; i++) {
124
+ ret[i] = _cumsumDimensional(tran[i], dim - 1);
125
+ }
126
+
127
+ return ret;
128
+ }
129
+ } else {
130
+ ret = [];
131
+
132
+ for (i = 0; i < mat.length; i++) {
133
+ ret[i] = _cumsumDimensional(mat[i], dim - 1);
134
+ }
135
+
136
+ return ret;
137
+ }
138
+ }
139
+ });
@@ -29,7 +29,7 @@ export var createSum = /* #__PURE__ */factory(name, dependencies, _ref => {
29
29
  *
30
30
  * See also:
31
31
  *
32
- * mean, median, min, max, prod, std, variance
32
+ * mean, median, min, max, prod, std, variance, cumsum
33
33
  *
34
34
  * @param {... *} args A single matrix or or multiple scalar values
35
35
  * @return {*} The sum of all values
@@ -112,7 +112,7 @@ export var createFormat = /* #__PURE__ */factory(name, dependencies, _ref => {
112
112
  * // you could also use math.format inside the callback:
113
113
  * // return '$' + math.format(value, {notation: 'fixed', precision: 2})
114
114
  * }
115
- * math.format([2.1, 3, 0.016], formatCurrency} // returns '[$2.10, $3.00, $0.02]'
115
+ * math.format([2.1, 3, 0.016], formatCurrency) // returns '[$2.10, $3.00, $0.02]'
116
116
  *
117
117
  * See also:
118
118
  *
@@ -9,27 +9,39 @@ export var createFraction = /* #__PURE__ */factory(name, dependencies, _ref => {
9
9
  } = _ref;
10
10
 
11
11
  /**
12
- * Create a fraction convert a value to a fraction.
12
+ * Create a fraction or convert a value to a fraction.
13
+ *
14
+ * With one numeric argument, produces the closest rational approximation to the
15
+ * input.
16
+ * With two arguments, the first is the numerator and the second is the denominator,
17
+ * and creates the corresponding fraction. Both numerator and denominator must be
18
+ * integers.
19
+ * With one object argument, looks for the integer numerator as the value of property
20
+ * 'n' and the integer denominator as the value of property 'd'.
21
+ * With a matrix argument, creates a matrix of the same shape with entries
22
+ * converted into fractions.
13
23
  *
14
24
  * Syntax:
25
+ * math.fraction(value)
15
26
  * math.fraction(numerator, denominator)
16
27
  * math.fraction({n: numerator, d: denominator})
17
- * math.fraction(matrix: Array | Matrix) Turn all matrix entries
18
- * into fractions
28
+ * math.fraction(matrix: Array | Matrix)
19
29
  *
20
30
  * Examples:
21
31
  *
22
- * math.fraction(1, 3)
23
- * math.fraction('2/3')
24
- * math.fraction({n: 2, d: 3})
25
- * math.fraction([0.2, 0.25, 1.25])
32
+ * math.fraction(6.283) // returns Fraction 6283/1000
33
+ * math.fraction(1, 3) // returns Fraction 1/3
34
+ * math.fraction('2/3') // returns Fraction 2/3
35
+ * math.fraction({n: 2, d: 3}) // returns Fraction 2/3
36
+ * math.fraction([0.2, 0.25, 1.25]) // returns Array [1/5, 1/4, 5/4]
37
+ * math.fraction(4, 5.1) // throws Error: Parameters must be integer
26
38
  *
27
39
  * See also:
28
40
  *
29
41
  * bignumber, number, string, unit
30
42
  *
31
43
  * @param {number | string | Fraction | BigNumber | Array | Matrix} [args]
32
- * Arguments specifying the numerator and denominator of
44
+ * Arguments specifying the value, or numerator and denominator of
33
45
  * the fraction
34
46
  * @return {Fraction | Array | Matrix} Returns a fraction
35
47
  */
@@ -1,6 +1,7 @@
1
1
  import { isCollection, isMatrix } from './is.js';
2
2
  import { IndexError } from '../error/IndexError.js';
3
3
  import { arraySize } from './array.js';
4
+ import { _switch } from './switch.js';
4
5
  /**
5
6
  * Test whether an array contains collections
6
7
  * @param {Array} array
@@ -127,32 +128,6 @@ function _reduce(mat, dim, callback) {
127
128
 
128
129
  return ret;
129
130
  }
130
- }
131
- /**
132
- * Transpose a matrix
133
- * @param {Array} mat
134
- * @returns {Array} ret
135
- * @private
136
- */
137
-
138
-
139
- function _switch(mat) {
140
- var I = mat.length;
141
- var J = mat[0].length;
142
- var i, j;
143
- var ret = [];
144
-
145
- for (j = 0; j < J; j++) {
146
- var tmp = [];
147
-
148
- for (i = 0; i < I; i++) {
149
- tmp.push(mat[i][j]);
150
- }
151
-
152
- ret.push(tmp);
153
- }
154
-
155
- return ret;
156
131
  } // TODO: document function scatter
157
132
 
158
133
 
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Transpose a matrix
3
+ * @param {Array} mat
4
+ * @returns {Array} ret
5
+ * @private
6
+ */
7
+ export function _switch(mat) {
8
+ var I = mat.length;
9
+ var J = mat[0].length;
10
+ var i, j;
11
+ var ret = [];
12
+
13
+ for (j = 0; j < J; j++) {
14
+ var tmp = [];
15
+
16
+ for (i = 0; i < I; i++) {
17
+ tmp.push(mat[i][j]);
18
+ }
19
+
20
+ ret.push(tmp);
21
+ }
22
+
23
+ return ret;
24
+ }
@@ -1,2 +1,2 @@
1
- export var version = '10.2.0'; // Note: This file is automatically generated when building math.js.
1
+ export var version = '10.4.1'; // Note: This file is automatically generated when building math.js.
2
2
  // Changes made in this file will be overwritten.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mathjs",
3
- "version": "10.2.0",
3
+ "version": "10.4.1",
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,29 +25,31 @@
25
25
  "unit"
26
26
  ],
27
27
  "dependencies": {
28
- "@babel/runtime": "^7.17.2",
28
+ "@babel/runtime": "^7.17.8",
29
29
  "complex.js": "^2.0.15",
30
30
  "decimal.js": "^10.3.1",
31
31
  "escape-latex": "^1.2.0",
32
- "fraction.js": "^4.1.3",
32
+ "fraction.js": "^4.2.0",
33
33
  "javascript-natural-sort": "^0.7.1",
34
34
  "seedrandom": "^3.0.5",
35
35
  "tiny-emitter": "^2.1.0",
36
- "typed-function": "^2.0.0"
36
+ "typed-function": "^2.1.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@babel/core": "7.17.5",
39
+ "@babel/core": "7.17.8",
40
40
  "@babel/plugin-transform-object-assign": "7.16.7",
41
41
  "@babel/plugin-transform-runtime": "7.17.0",
42
42
  "@babel/preset-env": "7.16.11",
43
- "@babel/register": "7.17.0",
43
+ "@babel/register": "7.17.7",
44
+ "@types/assert": "^1.5.6",
44
45
  "assert": "2.0.0",
45
- "babel-loader": "8.2.3",
46
+ "babel-loader": "8.2.4",
46
47
  "benchmark": "2.1.4",
47
48
  "codecov": "3.8.3",
48
49
  "core-js": "3.21.1",
49
50
  "del": "6.0.0",
50
51
  "dtslint": "4.2.1",
52
+ "expect-type": "^0.13.0",
51
53
  "expr-eval": "2.0.2",
52
54
  "fancy-log": "2.0.0",
53
55
  "glob": "7.2.0",
@@ -55,8 +57,8 @@
55
57
  "gulp-babel": "8.0.0",
56
58
  "handlebars": "4.7.7",
57
59
  "istanbul": "0.4.5",
58
- "jsep": "1.3.0",
59
- "karma": "6.3.16",
60
+ "jsep": "1.3.4",
61
+ "karma": "6.3.17",
60
62
  "karma-browserstack-launcher": "1.6.0",
61
63
  "karma-firefox-launcher": "2.1.2",
62
64
  "karma-mocha": "2.0.1",
@@ -76,7 +78,8 @@
76
78
  "process": "0.11.10",
77
79
  "standard": "16.0.4",
78
80
  "sylvester": "0.0.21",
79
- "typescript": "4.5.5",
81
+ "ts-node": "10.7.0",
82
+ "typescript": "4.6.2",
80
83
  "webpack": "4.46.0",
81
84
  "zeros": "1.0.0"
82
85
  },
@@ -139,9 +142,10 @@
139
142
  "test:src": "mocha test/unit-tests",
140
143
  "test:generated": "mocha test/generated-code-tests",
141
144
  "test:node": "mocha test/node-tests/*.test.js test/node-tests/**/*.test.js",
142
- "test:all": "npm run test:src && npm run test:generated && npm run test:node",
145
+ "test:all": "npm run test:src && npm run test:generated && npm run test:node && npm run test:types",
143
146
  "test:browser": "karma start test/browser-test-config/local-karma.js",
144
147
  "test:browserstack": "karma start test/browser-test-config/browserstack-karma.js",
148
+ "test:types": "cd types && node --loader ts-node/esm ./index.ts",
145
149
  "coverage": "nyc --reporter=lcov --reporter=text-summary mocha test/unit-tests && echo \"\nDetailed coverage report is available at ./coverage/lcov-report/index.html\"",
146
150
  "prepublishOnly": "npm run test:all && npm run lint",
147
151
  "prepare": "npm run build",