mathjs 10.2.0 → 10.4.1
Sign up to get free protection for your applications and to get access to all the features.
- package/HISTORY.md +43 -0
- package/docs/expressions/syntax.md +31 -2
- package/docs/reference/functions/cumsum.md +57 -0
- package/docs/reference/functions/format.md +1 -1
- package/docs/reference/functions/map.md +22 -5
- package/docs/reference/functions/subset.md +10 -2
- package/docs/reference/functions/sum.md +2 -1
- package/docs/reference/functions/symbolicEqual.md +62 -0
- package/docs/reference/functions.md +3 -1
- package/lib/browser/math.js +6 -6
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesCumSum.generated.js +26 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesCumSumTransform.generated.js +26 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +29 -0
- package/lib/cjs/entry/dependenciesAny.generated.js +24 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesCumSum.generated.js +26 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesCumSumTransform.generated.js +26 -0
- package/lib/cjs/entry/dependenciesNumber.generated.js +16 -0
- package/lib/cjs/entry/impureFunctionsAny.generated.js +22 -8
- package/lib/cjs/entry/impureFunctionsNumber.generated.js +6 -0
- package/lib/cjs/entry/pureFunctionsAny.generated.js +18 -12
- package/lib/cjs/entry/pureFunctionsNumber.generated.js +8 -2
- package/lib/cjs/expression/embeddedDocs/construction/fraction.js +3 -3
- package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +240 -234
- package/lib/cjs/expression/embeddedDocs/function/algebra/symbolicEqual.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/subset.js +2 -2
- package/lib/cjs/expression/embeddedDocs/function/statistics/cumsum.js +15 -0
- package/lib/cjs/expression/node/FunctionNode.js +74 -55
- package/lib/cjs/expression/parse.js +12 -8
- package/lib/cjs/expression/transform/cumsum.transform.js +57 -0
- package/lib/cjs/expression/transform/sum.transform.js +1 -1
- package/lib/cjs/factoriesAny.js +24 -0
- package/lib/cjs/factoriesNumber.js +18 -2
- package/lib/cjs/function/algebra/simplify.js +8 -0
- package/lib/cjs/function/algebra/simplifyCore.js +2 -2
- package/lib/cjs/function/algebra/symbolicEqual.js +88 -0
- package/lib/cjs/function/matrix/eigs/complexEigs.js +39 -28
- package/lib/cjs/function/matrix/map.js +53 -15
- package/lib/cjs/function/matrix/subset.js +15 -5
- package/lib/cjs/function/statistics/cumsum.js +151 -0
- package/lib/cjs/function/statistics/sum.js +1 -1
- package/lib/cjs/function/string/format.js +1 -1
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/type/fraction/function/fraction.js +20 -8
- package/lib/cjs/utils/collection.js +3 -27
- package/lib/cjs/utils/switch.js +31 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/entry/dependenciesAny/dependenciesCumSum.generated.js +14 -0
- package/lib/esm/entry/dependenciesAny/dependenciesCumSumTransform.generated.js +14 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +16 -0
- package/lib/esm/entry/dependenciesAny.generated.js +3 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesCumSum.generated.js +14 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesCumSumTransform.generated.js +14 -0
- package/lib/esm/entry/dependenciesNumber.generated.js +2 -0
- package/lib/esm/entry/impureFunctionsAny.generated.js +22 -9
- package/lib/esm/entry/impureFunctionsNumber.generated.js +8 -2
- package/lib/esm/entry/pureFunctionsAny.generated.js +13 -8
- package/lib/esm/entry/pureFunctionsNumber.generated.js +6 -1
- package/lib/esm/expression/embeddedDocs/construction/fraction.js +3 -3
- package/lib/esm/expression/embeddedDocs/embeddedDocs.js +220 -216
- package/lib/esm/expression/embeddedDocs/function/algebra/symbolicEqual.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/subset.js +2 -2
- package/lib/esm/expression/embeddedDocs/function/statistics/cumsum.js +8 -0
- package/lib/esm/expression/node/FunctionNode.js +70 -53
- package/lib/esm/expression/parse.js +12 -8
- package/lib/esm/expression/transform/cumsum.transform.js +48 -0
- package/lib/esm/expression/transform/sum.transform.js +1 -1
- package/lib/esm/factoriesAny.js +3 -0
- package/lib/esm/factoriesNumber.js +2 -0
- package/lib/esm/function/algebra/simplify.js +8 -0
- package/lib/esm/function/algebra/simplifyCore.js +2 -2
- package/lib/esm/function/algebra/symbolicEqual.js +80 -0
- package/lib/esm/function/matrix/eigs/complexEigs.js +36 -25
- package/lib/esm/function/matrix/map.js +53 -15
- package/lib/esm/function/matrix/subset.js +15 -5
- package/lib/esm/function/statistics/cumsum.js +139 -0
- package/lib/esm/function/statistics/sum.js +1 -1
- package/lib/esm/function/string/format.js +1 -1
- package/lib/esm/type/fraction/function/fraction.js +20 -8
- package/lib/esm/utils/collection.js +1 -26
- package/lib/esm/utils/switch.js +24 -0
- package/lib/esm/version.js +1 -1
- package/package.json +15 -11
- package/types/index.d.ts +209 -23
- package/types/index.ts +274 -57
- package/types/tsconfig.json +2 -1
@@ -54,32 +54,36 @@ function createComplexEigs(_ref) {
|
|
54
54
|
} // TODO check if any row/col are zero except the diagonal
|
55
55
|
// make sure corresponding rows and columns have similar magnitude
|
56
56
|
// important because of numerical stability
|
57
|
+
// MODIFIES arr by side effect!
|
57
58
|
|
58
59
|
|
59
60
|
var R = balance(arr, N, prec, type, findVectors); // R is the row transformation matrix
|
60
|
-
// A' = R A R⁻¹, A is the original matrix
|
61
|
+
// arr = A' = R A R⁻¹, A is the original matrix
|
61
62
|
// (if findVectors is false, R is undefined)
|
63
|
+
// (And so to return to original matrix: A = R⁻¹ arr R)
|
62
64
|
// TODO if magnitudes of elements vary over many orders,
|
63
65
|
// move greatest elements to the top left corner
|
64
66
|
// using similarity transformations, reduce the matrix
|
65
67
|
// to Hessenberg form (upper triangular plus one subdiagonal row)
|
66
68
|
// updates the transformation matrix R with new row operationsq
|
69
|
+
// MODIFIES arr by side effect!
|
67
70
|
|
68
|
-
reduceToHessenberg(arr, N, prec, type, findVectors, R); //
|
71
|
+
reduceToHessenberg(arr, N, prec, type, findVectors, R); // still true that original A = R⁻¹ arr R)
|
72
|
+
// find eigenvalues
|
69
73
|
|
70
74
|
var _iterateUntilTriangul = iterateUntilTriangular(arr, N, prec, type, findVectors),
|
71
75
|
values = _iterateUntilTriangul.values,
|
72
76
|
C = _iterateUntilTriangul.C; // values is the list of eigenvalues, C is the column
|
73
|
-
// transformation matrix that transforms the hessenberg
|
74
|
-
// matrix to upper triangular
|
75
|
-
//
|
77
|
+
// transformation matrix that transforms arr, the hessenberg
|
78
|
+
// matrix, to upper triangular
|
79
|
+
// (So U = C⁻¹ arr C and the relationship between current arr
|
80
|
+
// and original A is unchanged.)
|
76
81
|
|
77
82
|
|
78
|
-
C = multiply(inv(R), C);
|
79
83
|
var vectors;
|
80
84
|
|
81
85
|
if (findVectors) {
|
82
|
-
vectors = findEigenvectors(arr, N, C, values, prec, type);
|
86
|
+
vectors = findEigenvectors(arr, N, C, R, values, prec, type);
|
83
87
|
vectors = matrixFromColumns.apply(void 0, (0, _toConsumableArray2.default)(vectors));
|
84
88
|
}
|
85
89
|
|
@@ -100,8 +104,9 @@ function createComplexEigs(_ref) {
|
|
100
104
|
function balance(arr, N, prec, type, findVectors) {
|
101
105
|
var big = type === 'BigNumber';
|
102
106
|
var cplx = type === 'Complex';
|
103
|
-
var
|
104
|
-
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
|
107
|
+
var realzero = big ? bignumber(0) : 0;
|
108
|
+
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
|
109
|
+
var realone = big ? bignumber(1) : 1; // base of the floating-point arithmetic
|
105
110
|
|
106
111
|
var radix = big ? bignumber(10) : 2;
|
107
112
|
var radixSq = multiplyScalar(radix, radix); // the diagonal transformation matrix R
|
@@ -122,12 +127,13 @@ function createComplexEigs(_ref) {
|
|
122
127
|
for (var i = 0; i < N; i++) {
|
123
128
|
// compute the taxicab norm of i-th column and row
|
124
129
|
// TODO optimize for complex numbers
|
125
|
-
var colNorm =
|
126
|
-
var rowNorm =
|
130
|
+
var colNorm = realzero;
|
131
|
+
var rowNorm = realzero;
|
127
132
|
|
128
133
|
for (var j = 0; j < N; j++) {
|
129
134
|
if (i === j) continue;
|
130
|
-
var c = abs(arr[i][j]);
|
135
|
+
var c = abs(arr[i][j]); // should be real
|
136
|
+
|
131
137
|
colNorm = addScalar(colNorm, c);
|
132
138
|
rowNorm = addScalar(rowNorm, c);
|
133
139
|
}
|
@@ -136,7 +142,7 @@ function createComplexEigs(_ref) {
|
|
136
142
|
// find integer power closest to balancing the matrix
|
137
143
|
// (we want to scale only by integer powers of radix,
|
138
144
|
// so that we don't lose any precision due to round-off)
|
139
|
-
var f =
|
145
|
+
var f = realone;
|
140
146
|
var _c = colNorm;
|
141
147
|
var rowDivRadix = divideScalar(rowNorm, radix);
|
142
148
|
var rowMulRadix = multiplyScalar(rowNorm, radix);
|
@@ -410,16 +416,17 @@ function createComplexEigs(_ref) {
|
|
410
416
|
};
|
411
417
|
}
|
412
418
|
/**
|
413
|
-
* @param {Matrix} A
|
419
|
+
* @param {Matrix} A hessenberg-form matrix
|
414
420
|
* @param {number} N size of A
|
415
421
|
* @param {Matrix} C column transformation matrix that turns A into upper triangular
|
422
|
+
* @param {Matrix} R similarity that turns original matrix into A
|
416
423
|
* @param {number[]} values array of eigenvalues of A
|
417
424
|
* @param {'number'|'BigNumber'|'Complex'} type
|
418
425
|
* @returns {number[][]} eigenvalues
|
419
426
|
*/
|
420
427
|
|
421
428
|
|
422
|
-
function findEigenvectors(A, N, C, values, prec, type) {
|
429
|
+
function findEigenvectors(A, N, C, R, values, prec, type) {
|
423
430
|
var Cinv = inv(C);
|
424
431
|
var U = multiply(Cinv, A, C);
|
425
432
|
var big = type === 'BigNumber';
|
@@ -436,12 +443,12 @@ function createComplexEigs(_ref) {
|
|
436
443
|
|
437
444
|
try {
|
438
445
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
439
|
-
var
|
446
|
+
var λ = _step.value;
|
440
447
|
|
441
|
-
var _i4 = indexOf(uniqueValues,
|
448
|
+
var _i4 = indexOf(uniqueValues, λ, equal);
|
442
449
|
|
443
450
|
if (_i4 === -1) {
|
444
|
-
uniqueValues.push(
|
451
|
+
uniqueValues.push(λ);
|
445
452
|
multiplicities.push(1);
|
446
453
|
} else {
|
447
454
|
multiplicities[_i4] += 1;
|
@@ -463,21 +470,16 @@ function createComplexEigs(_ref) {
|
|
463
470
|
|
464
471
|
var failedLambdas = [];
|
465
472
|
|
466
|
-
|
473
|
+
var _loop = function _loop(i) {
|
467
474
|
var λ = uniqueValues[i];
|
475
|
+
var S = subtract(U, multiply(λ, E)); // the characteristic matrix
|
468
476
|
|
469
|
-
var
|
470
|
-
|
471
|
-
|
472
|
-
var solutions = usolveAll(_A, b);
|
473
|
-
solutions = solutions.map(function (v) {
|
474
|
-
return multiply(C, v);
|
475
|
-
});
|
477
|
+
var solutions = usolveAll(S, b);
|
476
478
|
solutions.shift(); // ignore the null vector
|
477
479
|
// looks like we missed something, try inverse iteration
|
478
480
|
|
479
481
|
while (solutions.length < multiplicities[i]) {
|
480
|
-
var approxVec = inverseIterate(
|
482
|
+
var approxVec = inverseIterate(S, N, solutions, prec, type);
|
481
483
|
|
482
484
|
if (approxVec == null) {
|
483
485
|
// no more vectors were found
|
@@ -486,11 +488,20 @@ function createComplexEigs(_ref) {
|
|
486
488
|
}
|
487
489
|
|
488
490
|
solutions.push(approxVec);
|
489
|
-
}
|
491
|
+
} // Transform back into original array coordinates
|
492
|
+
|
490
493
|
|
494
|
+
var correction = multiply(inv(R), C);
|
495
|
+
solutions = solutions.map(function (v) {
|
496
|
+
return multiply(correction, v);
|
497
|
+
});
|
491
498
|
vectors.push.apply(vectors, (0, _toConsumableArray2.default)(solutions.map(function (v) {
|
492
499
|
return flatten(v);
|
493
500
|
})));
|
501
|
+
};
|
502
|
+
|
503
|
+
for (var i = 0; i < len; i++) {
|
504
|
+
_loop(i);
|
494
505
|
}
|
495
506
|
|
496
507
|
if (failedLambdas.length !== 0) {
|
@@ -15,8 +15,15 @@ var createMap = /* #__PURE__ */(0, _factory.factory)(name, dependencies, functio
|
|
15
15
|
var typed = _ref.typed;
|
16
16
|
|
17
17
|
/**
|
18
|
-
* Create a new matrix or array with the results of
|
19
|
-
* each entry of
|
18
|
+
* Create a new matrix or array with the results of a callback function executed on
|
19
|
+
* each entry of a given matrix/array.
|
20
|
+
*
|
21
|
+
* For each entry of the input, the callback is invoked with three arguments:
|
22
|
+
* the value of the entry, the index at which that entry occurs, and the full
|
23
|
+
* matrix/array being traversed. Note that because the matrix/array might be
|
24
|
+
* multidimensional, the "index" argument is always an array of numbers giving
|
25
|
+
* the index in each dimension. This is true even for vectors: the "index"
|
26
|
+
* argument is an array of length 1, rather than simply a number.
|
20
27
|
*
|
21
28
|
* Syntax:
|
22
29
|
*
|
@@ -28,15 +35,25 @@ var createMap = /* #__PURE__ */(0, _factory.factory)(name, dependencies, functio
|
|
28
35
|
* return value * value
|
29
36
|
* }) // returns [1, 4, 9]
|
30
37
|
*
|
38
|
+
* // The calling convention for the callback can cause subtleties:
|
39
|
+
* math.map([1, 2, 3], math.format)
|
40
|
+
* // throws TypeError: map attempted to call 'format(1,[0])' but argument 2 of type Array does not match expected type number or function or Object or string or boolean
|
41
|
+
* // [This happens because `format` _can_ take a second argument,
|
42
|
+
* // but its semantics don't match that of the 2nd argument `map` provides]
|
43
|
+
*
|
44
|
+
* // To avoid this error, use a function that takes exactly the
|
45
|
+
* // desired arguments:
|
46
|
+
* math.map([1, 2, 3], x => math.format(x)) // returns ['1', '2', '3']
|
47
|
+
*
|
31
48
|
* See also:
|
32
49
|
*
|
33
50
|
* filter, forEach, sort
|
34
51
|
*
|
35
|
-
* @param {Matrix | Array} x The
|
36
|
-
* @param {Function} callback
|
37
|
-
*
|
38
|
-
*
|
39
|
-
*
|
52
|
+
* @param {Matrix | Array} x The input to iterate on.
|
53
|
+
* @param {Function} callback
|
54
|
+
* The function to call (as described above) on each entry of the input
|
55
|
+
* @return {Matrix | array}
|
56
|
+
* Transformed map of x; always has the same type and shape as x
|
40
57
|
*/
|
41
58
|
return typed(name, {
|
42
59
|
'Array, function': _map,
|
@@ -66,14 +83,35 @@ function _map(array, callback) {
|
|
66
83
|
return recurse(child, index.concat(i));
|
67
84
|
});
|
68
85
|
} else {
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
86
|
+
try {
|
87
|
+
// invoke the callback function with the right number of arguments
|
88
|
+
if (args === 1) {
|
89
|
+
return callback(value);
|
90
|
+
} else if (args === 2) {
|
91
|
+
return callback(value, index);
|
92
|
+
} else {
|
93
|
+
// 3 or -1
|
94
|
+
return callback(value, index, array);
|
95
|
+
}
|
96
|
+
} catch (err) {
|
97
|
+
// But maybe the arguments still weren't right
|
98
|
+
if (err instanceof TypeError && 'data' in err && err.data.category === 'wrongType') {
|
99
|
+
var newmsg = "map attempted to call '".concat(err.data.fn, "(").concat(value);
|
100
|
+
var indexString = JSON.stringify(index);
|
101
|
+
|
102
|
+
if (args === 2) {
|
103
|
+
newmsg += ',' + indexString;
|
104
|
+
} else if (args !== 1) {
|
105
|
+
newmsg += ",".concat(indexString, ",").concat(array);
|
106
|
+
}
|
107
|
+
|
108
|
+
newmsg += ")' but argument ".concat(err.data.index + 1, " of type ");
|
109
|
+
newmsg += "".concat(err.data.actual, " does not match expected type ");
|
110
|
+
newmsg += err.data.expected.join(' or ');
|
111
|
+
throw new TypeError(newmsg);
|
112
|
+
}
|
113
|
+
|
114
|
+
throw err;
|
77
115
|
}
|
78
116
|
}
|
79
117
|
};
|
@@ -39,16 +39,26 @@ var createSubset = /* #__PURE__ */(0, _factory.factory)(name, dependencies, func
|
|
39
39
|
*
|
40
40
|
* // replace a subset
|
41
41
|
* const e = []
|
42
|
-
* const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]]
|
42
|
+
* const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]] and e = [[5, 0, 6]]
|
43
43
|
* const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]]
|
44
44
|
*
|
45
|
+
* // get submatrix using ranges
|
46
|
+
* const M = [
|
47
|
+
* [1,2,3],
|
48
|
+
* [4,5,6],
|
49
|
+
* [7,8,9]
|
50
|
+
* ]
|
51
|
+
* math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1,2,3],[4,5,6]]
|
52
|
+
*
|
45
53
|
* See also:
|
46
54
|
*
|
47
55
|
* size, resize, squeeze, index
|
48
56
|
*
|
49
57
|
* @param {Array | Matrix | string} matrix An array, matrix, or string
|
50
|
-
* @param {Index} index
|
51
|
-
*
|
58
|
+
* @param {Index} index
|
59
|
+
* For each dimension of the target, specifies an index or a list of
|
60
|
+
* indices to fetch or set. `subset` uses the cartesian product of
|
61
|
+
* the indices specified in each dimension.
|
52
62
|
* @param {*} [replacement] An array, matrix, or scalar.
|
53
63
|
* If provided, the subset is replaced with replacement.
|
54
64
|
* If not provided, the subset is returned
|
@@ -91,7 +101,7 @@ var createSubset = /* #__PURE__ */(0, _factory.factory)(name, dependencies, func
|
|
91
101
|
/**
|
92
102
|
* Retrieve a subset of a string
|
93
103
|
* @param {string} str string from which to get a substring
|
94
|
-
* @param {Index} index An index
|
104
|
+
* @param {Index} index An index or list of indices (character positions)
|
95
105
|
* @returns {string} substring
|
96
106
|
* @private
|
97
107
|
*/
|
@@ -122,7 +132,7 @@ function _getSubstring(str, index) {
|
|
122
132
|
/**
|
123
133
|
* Replace a substring in a string
|
124
134
|
* @param {string} str string to be replaced
|
125
|
-
* @param {Index} index An index
|
135
|
+
* @param {Index} index An index or list of indices (character positions)
|
126
136
|
* @param {string} replacement Replacement string
|
127
137
|
* @param {string} [defaultValue] Default value to be uses when resizing
|
128
138
|
* the string. is ' ' by default
|
@@ -0,0 +1,151 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.createCumSum = void 0;
|
7
|
+
|
8
|
+
var _collection = require("../../utils/collection.js");
|
9
|
+
|
10
|
+
var _factory = require("../../utils/factory.js");
|
11
|
+
|
12
|
+
var _switch2 = require("../../utils/switch.js");
|
13
|
+
|
14
|
+
var _improveErrorMessage = require("./utils/improveErrorMessage.js");
|
15
|
+
|
16
|
+
var _array = require("../../utils/array.js");
|
17
|
+
|
18
|
+
var _IndexError = require("../../error/IndexError.js");
|
19
|
+
|
20
|
+
var name = 'cumsum';
|
21
|
+
var dependencies = ['typed', 'add', 'unaryPlus'];
|
22
|
+
var createCumSum = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
|
23
|
+
var typed = _ref.typed,
|
24
|
+
add = _ref.add,
|
25
|
+
unaryPlus = _ref.unaryPlus;
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Compute the cumulative sum of a matrix or a list with values.
|
29
|
+
* In case of a (multi dimensional) array or matrix, the cumulative sums
|
30
|
+
* along a specified dimension (defaulting to the first) will be calculated.
|
31
|
+
*
|
32
|
+
* Syntax:
|
33
|
+
*
|
34
|
+
* math.cumsum(a, b, c, ...)
|
35
|
+
* math.cumsum(A)
|
36
|
+
*
|
37
|
+
* Examples:
|
38
|
+
*
|
39
|
+
* math.cumsum(2, 1, 4, 3) // returns [2, 3, 7, 10]
|
40
|
+
* math.cumsum([2, 1, 4, 3]) // returns [2, 3, 7, 10]
|
41
|
+
* math.cumsum([[1, 2], [3, 4]]) // returns [[1, 2], [4, 6]]
|
42
|
+
* math.cumsum([[1, 2], [3, 4]], 0) // returns [[1, 2], [4, 6]]
|
43
|
+
* math.cumsum([[1, 2], [3, 4]], 1) // returns [[1, 3], [3, 7]]
|
44
|
+
* math.cumsum([[2, 5], [4, 3], [1, 7]]) // returns [[2, 5], [6, 8], [7, 15]]
|
45
|
+
*
|
46
|
+
* See also:
|
47
|
+
*
|
48
|
+
* mean, median, min, max, prod, std, variance, sum
|
49
|
+
*
|
50
|
+
* @param {... *} args A single matrix or or multiple scalar values
|
51
|
+
* @return {*} The cumulative sum of all values
|
52
|
+
*/
|
53
|
+
return typed(name, {
|
54
|
+
// sum([a, b, c, d, ...])
|
55
|
+
Array: _cumsum,
|
56
|
+
Matrix: function Matrix(matrix) {
|
57
|
+
return matrix.create(_cumsum(matrix.valueOf()));
|
58
|
+
},
|
59
|
+
// sum([a, b, c, d, ...], dim)
|
60
|
+
'Array, number | BigNumber': _ncumSumDim,
|
61
|
+
'Matrix, number | BigNumber': function MatrixNumberBigNumber(matrix, dim) {
|
62
|
+
return matrix.create(_ncumSumDim(matrix.valueOf(), dim));
|
63
|
+
},
|
64
|
+
// cumsum(a, b, c, d, ...)
|
65
|
+
'...': function _(args) {
|
66
|
+
if ((0, _collection.containsCollections)(args)) {
|
67
|
+
throw new TypeError('All values expected to be scalar in function cumsum');
|
68
|
+
}
|
69
|
+
|
70
|
+
return _cumsum(args);
|
71
|
+
}
|
72
|
+
});
|
73
|
+
/**
|
74
|
+
* Recursively calculate the cumulative sum of an n-dimensional array
|
75
|
+
* @param {Array} array
|
76
|
+
* @return {number} cumsum
|
77
|
+
* @private
|
78
|
+
*/
|
79
|
+
|
80
|
+
function _cumsum(array) {
|
81
|
+
try {
|
82
|
+
return _cumsummap(array);
|
83
|
+
} catch (err) {
|
84
|
+
throw (0, _improveErrorMessage.improveErrorMessage)(err, name);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
function _cumsummap(array) {
|
89
|
+
if (array.length === 0) {
|
90
|
+
return [];
|
91
|
+
}
|
92
|
+
|
93
|
+
var sums = [unaryPlus(array[0])]; // unaryPlus converts to number if need be
|
94
|
+
|
95
|
+
for (var i = 1; i < array.length; ++i) {
|
96
|
+
// Must use add below and not addScalar for the case of summing a
|
97
|
+
// 2+-dimensional array along the 0th dimension (the row vectors,
|
98
|
+
// or higher-d analogues, are literally added to each other).
|
99
|
+
sums.push(add(sums[i - 1], array[i]));
|
100
|
+
}
|
101
|
+
|
102
|
+
return sums;
|
103
|
+
}
|
104
|
+
|
105
|
+
function _ncumSumDim(array, dim) {
|
106
|
+
var size = (0, _array.arraySize)(array);
|
107
|
+
|
108
|
+
if (dim < 0 || dim >= size.length) {
|
109
|
+
// TODO: would be more clear when throwing a DimensionError here
|
110
|
+
throw new _IndexError.IndexError(dim, size.length);
|
111
|
+
}
|
112
|
+
|
113
|
+
try {
|
114
|
+
return _cumsumDimensional(array, dim);
|
115
|
+
} catch (err) {
|
116
|
+
throw (0, _improveErrorMessage.improveErrorMessage)(err, name);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
/* Possible TODO: Refactor _reduce in collection.js to be able to work here as well */
|
120
|
+
|
121
|
+
|
122
|
+
function _cumsumDimensional(mat, dim) {
|
123
|
+
var i, ret, tran;
|
124
|
+
|
125
|
+
if (dim <= 0) {
|
126
|
+
var initialValue = mat[0][0];
|
127
|
+
|
128
|
+
if (!Array.isArray(initialValue)) {
|
129
|
+
return _cumsummap(mat);
|
130
|
+
} else {
|
131
|
+
tran = (0, _switch2._switch)(mat);
|
132
|
+
ret = [];
|
133
|
+
|
134
|
+
for (i = 0; i < tran.length; i++) {
|
135
|
+
ret[i] = _cumsumDimensional(tran[i], dim - 1);
|
136
|
+
}
|
137
|
+
|
138
|
+
return ret;
|
139
|
+
}
|
140
|
+
} else {
|
141
|
+
ret = [];
|
142
|
+
|
143
|
+
for (i = 0; i < mat.length; i++) {
|
144
|
+
ret[i] = _cumsumDimensional(mat[i], dim - 1);
|
145
|
+
}
|
146
|
+
|
147
|
+
return ret;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
});
|
151
|
+
exports.createCumSum = createCumSum;
|
@@ -37,7 +37,7 @@ var createSum = /* #__PURE__ */(0, _factory.factory)(name, dependencies, functio
|
|
37
37
|
*
|
38
38
|
* See also:
|
39
39
|
*
|
40
|
-
* mean, median, min, max, prod, std, variance
|
40
|
+
* mean, median, min, max, prod, std, variance, cumsum
|
41
41
|
*
|
42
42
|
* @param {... *} args A single matrix or or multiple scalar values
|
43
43
|
* @return {*} The sum of all values
|
@@ -119,7 +119,7 @@ var createFormat = /* #__PURE__ */(0, _factory.factory)(name, dependencies, func
|
|
119
119
|
* // you could also use math.format inside the callback:
|
120
120
|
* // return '$' + math.format(value, {notation: 'fixed', precision: 2})
|
121
121
|
* }
|
122
|
-
* math.format([2.1, 3, 0.016], formatCurrency
|
122
|
+
* math.format([2.1, 3, 0.016], formatCurrency) // returns '[$2.10, $3.00, $0.02]'
|
123
123
|
*
|
124
124
|
* See also:
|
125
125
|
*
|
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 10.
|
10
|
-
* @date 2022-03-
|
9
|
+
* @version 10.4.1
|
10
|
+
* @date 2022-03-23
|
11
11
|
*
|
12
12
|
* @license
|
13
13
|
* Copyright (C) 2013-2022 Jos de Jong <wjosdejong@gmail.com>
|
@@ -16,27 +16,39 @@ var createFraction = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
16
16
|
Fraction = _ref.Fraction;
|
17
17
|
|
18
18
|
/**
|
19
|
-
* Create a fraction convert a value to a fraction.
|
19
|
+
* Create a fraction or convert a value to a fraction.
|
20
|
+
*
|
21
|
+
* With one numeric argument, produces the closest rational approximation to the
|
22
|
+
* input.
|
23
|
+
* With two arguments, the first is the numerator and the second is the denominator,
|
24
|
+
* and creates the corresponding fraction. Both numerator and denominator must be
|
25
|
+
* integers.
|
26
|
+
* With one object argument, looks for the integer numerator as the value of property
|
27
|
+
* 'n' and the integer denominator as the value of property 'd'.
|
28
|
+
* With a matrix argument, creates a matrix of the same shape with entries
|
29
|
+
* converted into fractions.
|
20
30
|
*
|
21
31
|
* Syntax:
|
32
|
+
* math.fraction(value)
|
22
33
|
* math.fraction(numerator, denominator)
|
23
34
|
* math.fraction({n: numerator, d: denominator})
|
24
|
-
* math.fraction(matrix: Array | Matrix)
|
25
|
-
* into fractions
|
35
|
+
* math.fraction(matrix: Array | Matrix)
|
26
36
|
*
|
27
37
|
* Examples:
|
28
38
|
*
|
29
|
-
* math.fraction(
|
30
|
-
* math.fraction(
|
31
|
-
* math.fraction(
|
32
|
-
* math.fraction(
|
39
|
+
* math.fraction(6.283) // returns Fraction 6283/1000
|
40
|
+
* math.fraction(1, 3) // returns Fraction 1/3
|
41
|
+
* math.fraction('2/3') // returns Fraction 2/3
|
42
|
+
* math.fraction({n: 2, d: 3}) // returns Fraction 2/3
|
43
|
+
* math.fraction([0.2, 0.25, 1.25]) // returns Array [1/5, 1/4, 5/4]
|
44
|
+
* math.fraction(4, 5.1) // throws Error: Parameters must be integer
|
33
45
|
*
|
34
46
|
* See also:
|
35
47
|
*
|
36
48
|
* bignumber, number, string, unit
|
37
49
|
*
|
38
50
|
* @param {number | string | Fraction | BigNumber | Array | Matrix} [args]
|
39
|
-
* Arguments specifying the numerator and denominator of
|
51
|
+
* Arguments specifying the value, or numerator and denominator of
|
40
52
|
* the fraction
|
41
53
|
* @return {Fraction | Array | Matrix} Returns a fraction
|
42
54
|
*/
|
@@ -15,6 +15,8 @@ var _IndexError = require("../error/IndexError.js");
|
|
15
15
|
|
16
16
|
var _array = require("./array.js");
|
17
17
|
|
18
|
+
var _switch2 = require("./switch.js");
|
19
|
+
|
18
20
|
/**
|
19
21
|
* Test whether an array contains collections
|
20
22
|
* @param {Array} array
|
@@ -126,7 +128,7 @@ function _reduce(mat, dim, callback) {
|
|
126
128
|
|
127
129
|
return val;
|
128
130
|
} else {
|
129
|
-
tran = _switch(mat);
|
131
|
+
tran = (0, _switch2._switch)(mat);
|
130
132
|
ret = [];
|
131
133
|
|
132
134
|
for (i = 0; i < tran.length; i++) {
|
@@ -144,32 +146,6 @@ function _reduce(mat, dim, callback) {
|
|
144
146
|
|
145
147
|
return ret;
|
146
148
|
}
|
147
|
-
}
|
148
|
-
/**
|
149
|
-
* Transpose a matrix
|
150
|
-
* @param {Array} mat
|
151
|
-
* @returns {Array} ret
|
152
|
-
* @private
|
153
|
-
*/
|
154
|
-
|
155
|
-
|
156
|
-
function _switch(mat) {
|
157
|
-
var I = mat.length;
|
158
|
-
var J = mat[0].length;
|
159
|
-
var i, j;
|
160
|
-
var ret = [];
|
161
|
-
|
162
|
-
for (j = 0; j < J; j++) {
|
163
|
-
var tmp = [];
|
164
|
-
|
165
|
-
for (i = 0; i < I; i++) {
|
166
|
-
tmp.push(mat[i][j]);
|
167
|
-
}
|
168
|
-
|
169
|
-
ret.push(tmp);
|
170
|
-
}
|
171
|
-
|
172
|
-
return ret;
|
173
149
|
} // TODO: document function scatter
|
174
150
|
|
175
151
|
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports._switch = _switch;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Transpose a matrix
|
10
|
+
* @param {Array} mat
|
11
|
+
* @returns {Array} ret
|
12
|
+
* @private
|
13
|
+
*/
|
14
|
+
function _switch(mat) {
|
15
|
+
var I = mat.length;
|
16
|
+
var J = mat[0].length;
|
17
|
+
var i, j;
|
18
|
+
var ret = [];
|
19
|
+
|
20
|
+
for (j = 0; j < J; j++) {
|
21
|
+
var tmp = [];
|
22
|
+
|
23
|
+
for (i = 0; i < I; i++) {
|
24
|
+
tmp.push(mat[i][j]);
|
25
|
+
}
|
26
|
+
|
27
|
+
ret.push(tmp);
|
28
|
+
}
|
29
|
+
|
30
|
+
return ret;
|
31
|
+
}
|
package/lib/cjs/version.js
CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.version = void 0;
|
7
|
-
var version = '10.
|
7
|
+
var version = '10.4.1'; // Note: This file is automatically generated when building math.js.
|
8
8
|
// Changes made in this file will be overwritten.
|
9
9
|
|
10
10
|
exports.version = version;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
/**
|
2
|
+
* THIS FILE IS AUTO-GENERATED
|
3
|
+
* DON'T MAKE CHANGES HERE
|
4
|
+
*/
|
5
|
+
import { addDependencies } from './dependenciesAdd.generated.js';
|
6
|
+
import { typedDependencies } from './dependenciesTyped.generated.js';
|
7
|
+
import { unaryPlusDependencies } from './dependenciesUnaryPlus.generated.js';
|
8
|
+
import { createCumSum } from '../../factoriesAny.js';
|
9
|
+
export var cumsumDependencies = {
|
10
|
+
addDependencies,
|
11
|
+
typedDependencies,
|
12
|
+
unaryPlusDependencies,
|
13
|
+
createCumSum
|
14
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
/**
|
2
|
+
* THIS FILE IS AUTO-GENERATED
|
3
|
+
* DON'T MAKE CHANGES HERE
|
4
|
+
*/
|
5
|
+
import { addDependencies } from './dependenciesAdd.generated.js';
|
6
|
+
import { typedDependencies } from './dependenciesTyped.generated.js';
|
7
|
+
import { unaryPlusDependencies } from './dependenciesUnaryPlus.generated.js';
|
8
|
+
import { createCumSumTransform } from '../../factoriesAny.js';
|
9
|
+
export var cumsumTransformDependencies = {
|
10
|
+
addDependencies,
|
11
|
+
typedDependencies,
|
12
|
+
unaryPlusDependencies,
|
13
|
+
createCumSumTransform
|
14
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/**
|
2
|
+
* THIS FILE IS AUTO-GENERATED
|
3
|
+
* DON'T MAKE CHANGES HERE
|
4
|
+
*/
|
5
|
+
import { OperatorNodeDependencies } from './dependenciesOperatorNode.generated.js';
|
6
|
+
import { parseDependencies } from './dependenciesParse.generated.js';
|
7
|
+
import { simplifyDependencies } from './dependenciesSimplify.generated.js';
|
8
|
+
import { typedDependencies } from './dependenciesTyped.generated.js';
|
9
|
+
import { createSymbolicEqual } from '../../factoriesAny.js';
|
10
|
+
export var symbolicEqualDependencies = {
|
11
|
+
OperatorNodeDependencies,
|
12
|
+
parseDependencies,
|
13
|
+
simplifyDependencies,
|
14
|
+
typedDependencies,
|
15
|
+
createSymbolicEqual
|
16
|
+
};
|