mathjs 11.9.1 → 11.10.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.
- package/HISTORY.md +10 -0
- package/lib/browser/math.js +1 -1
- package/lib/browser/math.js.LICENSE.txt +2 -2
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesIndexClass.generated.js +2 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesIndexTransform.generated.js +2 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesQuantileSeq.generated.js +2 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesQuantileSeqTransform.generated.js +28 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSubset.generated.js +4 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSubsetTransform.generated.js +4 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesZeta.generated.js +46 -0
- package/lib/cjs/entry/dependenciesAny.generated.js +14 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesQuantileSeq.generated.js +2 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesZeta.generated.js +42 -0
- package/lib/cjs/entry/dependenciesNumber.generated.js +7 -0
- package/lib/cjs/entry/impureFunctionsAny.generated.js +52 -40
- package/lib/cjs/entry/impureFunctionsNumber.generated.js +1 -0
- package/lib/cjs/entry/pureFunctionsAny.generated.js +159 -136
- package/lib/cjs/entry/pureFunctionsNumber.generated.js +19 -1
- package/lib/cjs/expression/embeddedDocs/construction/index.js +1 -1
- package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +2 -0
- package/lib/cjs/expression/embeddedDocs/function/special/zeta.js +15 -0
- package/lib/cjs/expression/node/utils/assign.js +7 -2
- package/lib/cjs/expression/transform/index.transform.js +9 -6
- package/lib/cjs/expression/transform/quantileSeq.transform.js +50 -0
- package/lib/cjs/expression/transform/subset.transform.js +7 -3
- package/lib/cjs/factoriesAny.js +14 -0
- package/lib/cjs/factoriesNumber.js +7 -0
- package/lib/cjs/function/matrix/concat.js +2 -29
- package/lib/cjs/function/matrix/subset.js +86 -24
- package/lib/cjs/function/special/zeta.js +169 -0
- package/lib/cjs/function/statistics/quantileSeq.js +39 -17
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/type/matrix/DenseMatrix.js +19 -9
- package/lib/cjs/type/matrix/MatrixIndex.js +38 -6
- package/lib/cjs/type/matrix/function/index.js +6 -4
- package/lib/cjs/type/matrix/utils/broadcast.js +8 -14
- package/lib/cjs/utils/array.js +253 -19
- package/lib/cjs/version.js +1 -1
- package/lib/esm/entry/dependenciesAny/dependenciesIndexClass.generated.js +2 -0
- package/lib/esm/entry/dependenciesAny/dependenciesIndexTransform.generated.js +2 -0
- package/lib/esm/entry/dependenciesAny/dependenciesQuantileSeq.generated.js +2 -0
- package/lib/esm/entry/dependenciesAny/dependenciesQuantileSeqTransform.generated.js +20 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSubset.generated.js +4 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSubsetTransform.generated.js +4 -0
- package/lib/esm/entry/dependenciesAny/dependenciesZeta.generated.js +38 -0
- package/lib/esm/entry/dependenciesAny.generated.js +2 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesQuantileSeq.generated.js +2 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesZeta.generated.js +34 -0
- package/lib/esm/entry/dependenciesNumber.generated.js +1 -0
- package/lib/esm/entry/impureFunctionsAny.generated.js +53 -41
- package/lib/esm/entry/impureFunctionsNumber.generated.js +2 -1
- package/lib/esm/entry/pureFunctionsAny.generated.js +140 -118
- package/lib/esm/entry/pureFunctionsNumber.generated.js +18 -1
- package/lib/esm/expression/embeddedDocs/construction/index.js +1 -1
- package/lib/esm/expression/embeddedDocs/embeddedDocs.js +2 -0
- package/lib/esm/expression/embeddedDocs/function/special/zeta.js +8 -0
- package/lib/esm/expression/node/utils/assign.js +7 -2
- package/lib/esm/expression/transform/index.transform.js +9 -6
- package/lib/esm/expression/transform/quantileSeq.transform.js +41 -0
- package/lib/esm/expression/transform/subset.transform.js +7 -3
- package/lib/esm/factoriesAny.js +2 -0
- package/lib/esm/factoriesNumber.js +1 -1
- package/lib/esm/function/matrix/concat.js +3 -30
- package/lib/esm/function/matrix/subset.js +85 -25
- package/lib/esm/function/special/zeta.js +146 -0
- package/lib/esm/function/statistics/quantileSeq.js +30 -16
- package/lib/esm/type/matrix/DenseMatrix.js +20 -10
- package/lib/esm/type/matrix/MatrixIndex.js +39 -7
- package/lib/esm/type/matrix/function/index.js +6 -4
- package/lib/esm/type/matrix/utils/broadcast.js +8 -14
- package/lib/esm/utils/array.js +241 -20
- package/lib/esm/version.js +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +16 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isBigNumber, isMatrix, isNumber } from '../../utils/is.js';
|
|
2
2
|
import { clone } from '../../utils/object.js';
|
|
3
|
-
import { arraySize } from '../../utils/array.js';
|
|
3
|
+
import { arraySize, concat as _concat } from '../../utils/array.js';
|
|
4
4
|
import { IndexError } from '../../error/IndexError.js';
|
|
5
5
|
import { DimensionError } from '../../error/DimensionError.js';
|
|
6
6
|
import { factory } from '../../utils/factory.js';
|
|
@@ -93,7 +93,7 @@ export var createConcat = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
|
93
93
|
}
|
|
94
94
|
var res = matrices.shift();
|
|
95
95
|
while (matrices.length) {
|
|
96
|
-
res = _concat(res, matrices.shift(), dim
|
|
96
|
+
res = _concat(res, matrices.shift(), dim);
|
|
97
97
|
}
|
|
98
98
|
return asMatrix ? matrix(res) : res;
|
|
99
99
|
},
|
|
@@ -101,31 +101,4 @@ export var createConcat = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
|
101
101
|
return args.join('');
|
|
102
102
|
}
|
|
103
103
|
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Recursively concatenate two matrices.
|
|
108
|
-
* The contents of the matrices is not cloned.
|
|
109
|
-
* @param {Array} a Multi dimensional array
|
|
110
|
-
* @param {Array} b Multi dimensional array
|
|
111
|
-
* @param {number} concatDim The dimension on which to concatenate (zero-based)
|
|
112
|
-
* @param {number} dim The current dim (zero-based)
|
|
113
|
-
* @return {Array} c The concatenated matrix
|
|
114
|
-
* @private
|
|
115
|
-
*/
|
|
116
|
-
function _concat(a, b, concatDim, dim) {
|
|
117
|
-
if (dim < concatDim) {
|
|
118
|
-
// recurse into next dimension
|
|
119
|
-
if (a.length !== b.length) {
|
|
120
|
-
throw new DimensionError(a.length, b.length);
|
|
121
|
-
}
|
|
122
|
-
var c = [];
|
|
123
|
-
for (var i = 0; i < a.length; i++) {
|
|
124
|
-
c[i] = _concat(a[i], b[i], concatDim, dim + 1);
|
|
125
|
-
}
|
|
126
|
-
return c;
|
|
127
|
-
} else {
|
|
128
|
-
// concatenate this dimension
|
|
129
|
-
return a.concat(b);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
104
|
+
});
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { isIndex } from '../../utils/is.js';
|
|
2
2
|
import { clone } from '../../utils/object.js';
|
|
3
|
-
import { validateIndex } from '../../utils/array.js';
|
|
3
|
+
import { isEmptyIndex, validateIndex, validateIndexSourceSize } from '../../utils/array.js';
|
|
4
4
|
import { getSafeProperty, setSafeProperty } from '../../utils/customs.js';
|
|
5
5
|
import { DimensionError } from '../../error/DimensionError.js';
|
|
6
6
|
import { factory } from '../../utils/factory.js';
|
|
7
7
|
var name = 'subset';
|
|
8
|
-
var dependencies = ['typed', 'matrix'];
|
|
8
|
+
var dependencies = ['typed', 'matrix', 'zeros', 'add'];
|
|
9
9
|
export var createSubset = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
10
10
|
var {
|
|
11
11
|
typed,
|
|
12
|
-
matrix
|
|
12
|
+
matrix,
|
|
13
|
+
zeros,
|
|
14
|
+
add
|
|
13
15
|
} = _ref;
|
|
14
16
|
/**
|
|
15
17
|
* Get or set a subset of a matrix or string.
|
|
@@ -22,13 +24,15 @@ export var createSubset = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
|
22
24
|
*
|
|
23
25
|
* // get a subset
|
|
24
26
|
* const d = [[1, 2], [3, 4]]
|
|
25
|
-
* math.subset(d, math.index(1, 0))
|
|
26
|
-
* math.subset(d, math.index([0, 1], 1))
|
|
27
|
+
* math.subset(d, math.index(1, 0)) // returns 3
|
|
28
|
+
* math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]]
|
|
29
|
+
* math.subset(d, math.index([false, true], 0)) // returns [[3]]
|
|
27
30
|
*
|
|
28
31
|
* // replace a subset
|
|
29
32
|
* const e = []
|
|
30
|
-
* const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]]
|
|
31
|
-
* const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]]
|
|
33
|
+
* const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 0, 6]]
|
|
34
|
+
* const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 0, 6], [0, 7, 0]]
|
|
35
|
+
* math.subset(g, math.index([false, true], 1), 8) // returns [[5, 0, 6], [0, 8, 0]]
|
|
32
36
|
*
|
|
33
37
|
* // get submatrix using ranges
|
|
34
38
|
* const M = [
|
|
@@ -36,7 +40,7 @@ export var createSubset = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
|
36
40
|
* [4,5,6],
|
|
37
41
|
* [7,8,9]
|
|
38
42
|
* ]
|
|
39
|
-
* math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1,2,3],[4,5,6]]
|
|
43
|
+
* math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1, 2, 3], [4, 5, 6]]
|
|
40
44
|
*
|
|
41
45
|
* See also:
|
|
42
46
|
*
|
|
@@ -55,36 +59,78 @@ export var createSubset = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
|
55
59
|
* math.matrix elements will be left undefined.
|
|
56
60
|
* @return {Array | Matrix | string} Either the retrieved subset or the updated matrix.
|
|
57
61
|
*/
|
|
62
|
+
|
|
58
63
|
return typed(name, {
|
|
59
64
|
// get subset
|
|
60
|
-
'Array, Index': function ArrayIndex(value, index) {
|
|
61
|
-
var m = matrix(value);
|
|
62
|
-
var subset = m.subset(index); // returns a Matrix
|
|
63
|
-
return index.isScalar() ? subset : subset.valueOf(); // return an Array (like the input)
|
|
64
|
-
},
|
|
65
|
-
|
|
66
65
|
'Matrix, Index': function MatrixIndex(value, index) {
|
|
66
|
+
if (isEmptyIndex(index)) {
|
|
67
|
+
return matrix();
|
|
68
|
+
}
|
|
69
|
+
validateIndexSourceSize(value, index);
|
|
67
70
|
return value.subset(index);
|
|
68
71
|
},
|
|
72
|
+
'Array, Index': typed.referTo('Matrix, Index', function (subsetRef) {
|
|
73
|
+
return function (value, index) {
|
|
74
|
+
var subsetResult = subsetRef(matrix(value), index);
|
|
75
|
+
return index.isScalar() ? subsetResult : subsetResult.valueOf();
|
|
76
|
+
};
|
|
77
|
+
}),
|
|
69
78
|
'Object, Index': _getObjectProperty,
|
|
70
79
|
'string, Index': _getSubstring,
|
|
71
80
|
// set subset
|
|
72
|
-
'Array, Index, any': function ArrayIndexAny(value, index, replacement) {
|
|
73
|
-
return matrix(clone(value)).subset(index, replacement, undefined).valueOf();
|
|
74
|
-
},
|
|
75
|
-
'Array, Index, any, any': function ArrayIndexAnyAny(value, index, replacement, defaultValue) {
|
|
76
|
-
return matrix(clone(value)).subset(index, replacement, defaultValue).valueOf();
|
|
77
|
-
},
|
|
78
|
-
'Matrix, Index, any': function MatrixIndexAny(value, index, replacement) {
|
|
79
|
-
return value.clone().subset(index, replacement);
|
|
80
|
-
},
|
|
81
81
|
'Matrix, Index, any, any': function MatrixIndexAnyAny(value, index, replacement, defaultValue) {
|
|
82
|
-
|
|
82
|
+
if (isEmptyIndex(index)) {
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
validateIndexSourceSize(value, index);
|
|
86
|
+
return value.clone().subset(index, _broadcastReplacement(replacement, index), defaultValue);
|
|
83
87
|
},
|
|
88
|
+
'Array, Index, any, any': typed.referTo('Matrix, Index, any, any', function (subsetRef) {
|
|
89
|
+
return function (value, index, replacement, defaultValue) {
|
|
90
|
+
var subsetResult = subsetRef(matrix(value), index, replacement, defaultValue);
|
|
91
|
+
return subsetResult.isMatrix ? subsetResult.valueOf() : subsetResult;
|
|
92
|
+
};
|
|
93
|
+
}),
|
|
94
|
+
'Array, Index, any': typed.referTo('Matrix, Index, any, any', function (subsetRef) {
|
|
95
|
+
return function (value, index, replacement) {
|
|
96
|
+
return subsetRef(matrix(value), index, replacement, undefined).valueOf();
|
|
97
|
+
};
|
|
98
|
+
}),
|
|
99
|
+
'Matrix, Index, any': typed.referTo('Matrix, Index, any, any', function (subsetRef) {
|
|
100
|
+
return function (value, index, replacement) {
|
|
101
|
+
return subsetRef(value, index, replacement, undefined);
|
|
102
|
+
};
|
|
103
|
+
}),
|
|
84
104
|
'string, Index, string': _setSubstring,
|
|
85
105
|
'string, Index, string, string': _setSubstring,
|
|
86
106
|
'Object, Index, any': _setObjectProperty
|
|
87
107
|
});
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Broadcasts a replacment value to be the same size as index
|
|
111
|
+
* @param {number | BigNumber | Array | Matrix} replacement Replacement value to try to broadcast
|
|
112
|
+
* @param {*} index Index value
|
|
113
|
+
* @returns broadcasted replacement that matches the size of index
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
function _broadcastReplacement(replacement, index) {
|
|
117
|
+
if (typeof replacement === 'string') {
|
|
118
|
+
throw new Error('can\'t boradcast a string');
|
|
119
|
+
}
|
|
120
|
+
if (index._isScalar) {
|
|
121
|
+
return replacement;
|
|
122
|
+
}
|
|
123
|
+
var indexSize = index.size();
|
|
124
|
+
if (indexSize.every(d => d > 0)) {
|
|
125
|
+
try {
|
|
126
|
+
return add(replacement, zeros(indexSize));
|
|
127
|
+
} catch (error) {
|
|
128
|
+
return replacement;
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
return replacement;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
88
134
|
});
|
|
89
135
|
|
|
90
136
|
/**
|
|
@@ -99,6 +145,10 @@ function _getSubstring(str, index) {
|
|
|
99
145
|
// TODO: better error message
|
|
100
146
|
throw new TypeError('Index expected');
|
|
101
147
|
}
|
|
148
|
+
if (isEmptyIndex(index)) {
|
|
149
|
+
return '';
|
|
150
|
+
}
|
|
151
|
+
validateIndexSourceSize(Array.from(str), index);
|
|
102
152
|
if (index.size().length !== 1) {
|
|
103
153
|
throw new DimensionError(index.size().length, 1);
|
|
104
154
|
}
|
|
@@ -120,7 +170,7 @@ function _getSubstring(str, index) {
|
|
|
120
170
|
* @param {string} str string to be replaced
|
|
121
171
|
* @param {Index} index An index or list of indices (character positions)
|
|
122
172
|
* @param {string} replacement Replacement string
|
|
123
|
-
* @param {string} [defaultValue] Default value to be
|
|
173
|
+
* @param {string} [defaultValue] Default value to be used when resizing
|
|
124
174
|
* the string. is ' ' by default
|
|
125
175
|
* @returns {string} result
|
|
126
176
|
* @private
|
|
@@ -130,6 +180,10 @@ function _setSubstring(str, index, replacement, defaultValue) {
|
|
|
130
180
|
// TODO: better error message
|
|
131
181
|
throw new TypeError('Index expected');
|
|
132
182
|
}
|
|
183
|
+
if (isEmptyIndex(index)) {
|
|
184
|
+
return str;
|
|
185
|
+
}
|
|
186
|
+
validateIndexSourceSize(Array.from(str), index);
|
|
133
187
|
if (index.size().length !== 1) {
|
|
134
188
|
throw new DimensionError(index.size().length, 1);
|
|
135
189
|
}
|
|
@@ -179,6 +233,9 @@ function _setSubstring(str, index, replacement, defaultValue) {
|
|
|
179
233
|
* @private
|
|
180
234
|
*/
|
|
181
235
|
function _getObjectProperty(object, index) {
|
|
236
|
+
if (isEmptyIndex(index)) {
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
182
239
|
if (index.size().length !== 1) {
|
|
183
240
|
throw new DimensionError(index.size(), 1);
|
|
184
241
|
}
|
|
@@ -198,6 +255,9 @@ function _getObjectProperty(object, index) {
|
|
|
198
255
|
* @private
|
|
199
256
|
*/
|
|
200
257
|
function _setObjectProperty(object, index, replacement) {
|
|
258
|
+
if (isEmptyIndex(index)) {
|
|
259
|
+
return object;
|
|
260
|
+
}
|
|
201
261
|
if (index.size().length !== 1) {
|
|
202
262
|
throw new DimensionError(index.size(), 1);
|
|
203
263
|
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { factory } from '../../utils/factory.js';
|
|
2
|
+
var name = 'zeta';
|
|
3
|
+
var dependencies = ['typed', 'config', 'multiply', 'pow', 'divide', 'factorial', 'equal', 'smallerEq', 'isNegative', 'gamma', 'sin', 'subtract', 'add', '?Complex', '?BigNumber', 'pi'];
|
|
4
|
+
export var createZeta = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
5
|
+
var {
|
|
6
|
+
typed,
|
|
7
|
+
config,
|
|
8
|
+
multiply,
|
|
9
|
+
pow,
|
|
10
|
+
divide,
|
|
11
|
+
factorial,
|
|
12
|
+
equal,
|
|
13
|
+
smallerEq,
|
|
14
|
+
isNegative,
|
|
15
|
+
gamma,
|
|
16
|
+
sin,
|
|
17
|
+
subtract,
|
|
18
|
+
add,
|
|
19
|
+
Complex,
|
|
20
|
+
BigNumber: _BigNumber,
|
|
21
|
+
pi
|
|
22
|
+
} = _ref;
|
|
23
|
+
/**
|
|
24
|
+
* Compute the Riemann Zeta function of a value using an infinite series for
|
|
25
|
+
* all of the complex plane using Riemann's Functional equation.
|
|
26
|
+
*
|
|
27
|
+
* Based off the paper by Xavier Gourdon and Pascal Sebah
|
|
28
|
+
* ( http://numbers.computation.free.fr/Constants/Miscellaneous/zetaevaluations.pdf )
|
|
29
|
+
*
|
|
30
|
+
* Implementation and slight modification by Anik Patel
|
|
31
|
+
*
|
|
32
|
+
* Note: the implementation is accurate up to about 6 digits.
|
|
33
|
+
*
|
|
34
|
+
* Syntax:
|
|
35
|
+
*
|
|
36
|
+
* math.zeta(n)
|
|
37
|
+
*
|
|
38
|
+
* Examples:
|
|
39
|
+
*
|
|
40
|
+
* math.zeta(5) // returns 1.0369277551433895
|
|
41
|
+
* math.zeta(-0.5) // returns -0.2078862249773449
|
|
42
|
+
* math.zeta(math.i) // returns 0.0033002236853253153 - 0.4181554491413212i
|
|
43
|
+
*
|
|
44
|
+
*
|
|
45
|
+
* @param {number | Complex | BigNumber} s A Real, Complex or BigNumber parameter to the Riemann Zeta Function
|
|
46
|
+
* @return {number | Complex | BigNumber} The Riemann Zeta of `s`
|
|
47
|
+
*/
|
|
48
|
+
return typed(name, {
|
|
49
|
+
number: s => zetaNumeric(s, value => value, () => 20),
|
|
50
|
+
BigNumber: s => zetaNumeric(s, value => new _BigNumber(value), () => {
|
|
51
|
+
// epsilon is for example 1e-12. Extract the positive exponent 12 from that
|
|
52
|
+
return Math.abs(Math.log10(config.epsilon));
|
|
53
|
+
}),
|
|
54
|
+
Complex: zetaComplex
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {number | BigNumber} s
|
|
59
|
+
* @param {(value: number) => number | BigNumber} createValue
|
|
60
|
+
* @param {(value: number | BigNumber | Complex) => number} determineDigits
|
|
61
|
+
* @returns {number | BigNumber}
|
|
62
|
+
*/
|
|
63
|
+
function zetaNumeric(s, createValue, determineDigits) {
|
|
64
|
+
if (equal(s, 0)) {
|
|
65
|
+
return createValue(-0.5);
|
|
66
|
+
}
|
|
67
|
+
if (equal(s, 1)) {
|
|
68
|
+
return createValue(NaN);
|
|
69
|
+
}
|
|
70
|
+
if (!isFinite(s)) {
|
|
71
|
+
return isNegative(s) ? createValue(NaN) : createValue(1);
|
|
72
|
+
}
|
|
73
|
+
return zeta(s, createValue, determineDigits, s => s);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {Complex} s
|
|
78
|
+
* @returns {Complex}
|
|
79
|
+
*/
|
|
80
|
+
function zetaComplex(s) {
|
|
81
|
+
if (s.re === 0 && s.im === 0) {
|
|
82
|
+
return new Complex(-0.5);
|
|
83
|
+
}
|
|
84
|
+
if (s.re === 1) {
|
|
85
|
+
return new Complex(NaN, NaN);
|
|
86
|
+
}
|
|
87
|
+
if (s.re === Infinity && s.im === 0) {
|
|
88
|
+
return new Complex(1);
|
|
89
|
+
}
|
|
90
|
+
if (s.im === Infinity || s.re === -Infinity) {
|
|
91
|
+
return new Complex(NaN, NaN);
|
|
92
|
+
}
|
|
93
|
+
return zeta(s, value => value, s => Math.round(1.3 * 15 + 0.9 * Math.abs(s.im)), s => s.re);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {number | BigNumber | Complex} s
|
|
98
|
+
* @param {(value: number) => number | BigNumber | Complex} createValue
|
|
99
|
+
* @param {(value: number | BigNumber | Complex) => number} determineDigits
|
|
100
|
+
* @param {(value: number | BigNumber | Complex) => number} getRe
|
|
101
|
+
* @returns {*|number}
|
|
102
|
+
*/
|
|
103
|
+
function zeta(s, createValue, determineDigits, getRe) {
|
|
104
|
+
var n = determineDigits(s);
|
|
105
|
+
if (getRe(s) > -(n - 1) / 2) {
|
|
106
|
+
return f(s, createValue(n), createValue);
|
|
107
|
+
} else {
|
|
108
|
+
// Function Equation for reflection to x < 1
|
|
109
|
+
var c = multiply(pow(2, s), pow(createValue(pi), subtract(s, 1)));
|
|
110
|
+
c = multiply(c, sin(multiply(divide(createValue(pi), 2), s)));
|
|
111
|
+
c = multiply(c, gamma(subtract(1, s)));
|
|
112
|
+
return multiply(c, zeta(subtract(1, s), createValue, determineDigits, getRe));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Calculate a portion of the sum
|
|
118
|
+
* @param {number | BigNumber} k a positive integer
|
|
119
|
+
* @param {number | BigNumber} n a positive integer
|
|
120
|
+
* @return {number} the portion of the sum
|
|
121
|
+
**/
|
|
122
|
+
function d(k, n) {
|
|
123
|
+
var S = k;
|
|
124
|
+
for (var j = k; smallerEq(j, n); j = add(j, 1)) {
|
|
125
|
+
var factor = divide(multiply(factorial(add(n, subtract(j, 1))), pow(4, j)), multiply(factorial(subtract(n, j)), factorial(multiply(2, j))));
|
|
126
|
+
S = add(S, factor);
|
|
127
|
+
}
|
|
128
|
+
return multiply(n, S);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Calculate the positive Riemann Zeta function
|
|
133
|
+
* @param {number} s a real or complex number with s.re > 1
|
|
134
|
+
* @param {number} n a positive integer
|
|
135
|
+
* @param {(number) => number | BigNumber | Complex} createValue
|
|
136
|
+
* @return {number} Riemann Zeta of s
|
|
137
|
+
**/
|
|
138
|
+
function f(s, n, createValue) {
|
|
139
|
+
var c = divide(1, multiply(d(createValue(0), n), subtract(1, pow(2, subtract(1, s)))));
|
|
140
|
+
var S = createValue(0);
|
|
141
|
+
for (var k = createValue(1); smallerEq(k, n); k = add(k, 1)) {
|
|
142
|
+
S = add(S, divide(multiply((-1) ** (k - 1), d(k, n)), pow(k, s)));
|
|
143
|
+
}
|
|
144
|
+
return multiply(c, S);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { isBigNumber, isCollection, isNumber } from '../../utils/is.js';
|
|
2
|
-
import { isInteger } from '../../utils/number.js';
|
|
3
2
|
import { flatten } from '../../utils/array.js';
|
|
4
3
|
import { factory } from '../../utils/factory.js';
|
|
4
|
+
import { createApply } from '../matrix/apply.js';
|
|
5
5
|
var name = 'quantileSeq';
|
|
6
|
-
var dependencies = ['typed', 'add', 'multiply', 'partitionSelect', 'compare'];
|
|
6
|
+
var dependencies = ['typed', 'add', 'multiply', 'partitionSelect', 'compare', 'isInteger'];
|
|
7
7
|
export var createQuantileSeq = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
8
8
|
var {
|
|
9
9
|
typed,
|
|
10
10
|
add,
|
|
11
11
|
multiply,
|
|
12
12
|
partitionSelect,
|
|
13
|
-
compare
|
|
13
|
+
compare,
|
|
14
|
+
isInteger
|
|
14
15
|
} = _ref;
|
|
15
16
|
/**
|
|
16
17
|
* Compute the prob order quantile of a matrix or a list with values.
|
|
@@ -46,6 +47,32 @@ export var createQuantileSeq = /* #__PURE__ */factory(name, dependencies, _ref =
|
|
|
46
47
|
* @param {Boolean} sorted=false is data sorted in ascending order
|
|
47
48
|
* @return {Number, BigNumber, Unit, Array} Quantile(s)
|
|
48
49
|
*/
|
|
50
|
+
|
|
51
|
+
var apply = createApply({
|
|
52
|
+
typed,
|
|
53
|
+
isInteger
|
|
54
|
+
});
|
|
55
|
+
/**
|
|
56
|
+
* Check if array value types are valid, throw error otherwise.
|
|
57
|
+
* @param {number | BigNumber | Unit} x
|
|
58
|
+
* @param {number | BigNumber | Unit} x
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
var validate = typed({
|
|
62
|
+
'number | BigNumber | Unit': function numberBigNumberUnit(x) {
|
|
63
|
+
return x;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return typed(name, {
|
|
67
|
+
'Array|Matrix, number|BigNumber|Array': (data, prob) => quantileSeq(data, prob, false),
|
|
68
|
+
'Array|Matrix, number|BigNumber|Array, boolean': quantileSeq,
|
|
69
|
+
'Array|Matrix, number|BigNumber|Array, number': (data, prob, dim) => _quantileSeqDim(data, prob, false, dim),
|
|
70
|
+
'Array|Matrix, number|BigNumber|Array, boolean, number': (data, prob, sorted, dim) => _quantileSeqDim(data, prob, sorted, dim)
|
|
71
|
+
});
|
|
72
|
+
function _quantileSeqDim(data, prob, sorted, dim) {
|
|
73
|
+
// return [1.3, 1.2]
|
|
74
|
+
return apply(data, dim, x => quantileSeq(x, prob, sorted));
|
|
75
|
+
}
|
|
49
76
|
function quantileSeq(data, probOrN, sorted) {
|
|
50
77
|
var probArr, dataArr, one;
|
|
51
78
|
if (arguments.length < 2 || arguments.length > 3) {
|
|
@@ -219,17 +246,4 @@ export var createQuantileSeq = /* #__PURE__ */factory(name, dependencies, _ref =
|
|
|
219
246
|
var one = new fracPart.constructor(1);
|
|
220
247
|
return add(multiply(left, one.minus(fracPart)), multiply(right, fracPart));
|
|
221
248
|
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Check if array value types are valid, throw error otherwise.
|
|
225
|
-
* @param {number | BigNumber | Unit} x
|
|
226
|
-
* @param {number | BigNumber | Unit} x
|
|
227
|
-
* @private
|
|
228
|
-
*/
|
|
229
|
-
var validate = typed({
|
|
230
|
-
'number | BigNumber | Unit': function numberBigNumberUnit(x) {
|
|
231
|
-
return x;
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
return quantileSeq;
|
|
235
249
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isArray, isBigNumber, isCollection, isIndex, isMatrix, isNumber, isString, typeOf } from '../../utils/is.js';
|
|
2
|
-
import { arraySize, getArrayDataType, processSizesWildcard, reshape, resize, unsqueeze, validate, validateIndex } from '../../utils/array.js';
|
|
2
|
+
import { arraySize, getArrayDataType, processSizesWildcard, reshape, resize, unsqueeze, validate, validateIndex, broadcastTo } from '../../utils/array.js';
|
|
3
3
|
import { format } from '../../utils/string.js';
|
|
4
4
|
import { isInteger } from '../../utils/number.js';
|
|
5
5
|
import { clone, deepStrictEqual } from '../../utils/object.js';
|
|
@@ -330,6 +330,18 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
|
|
|
330
330
|
} else {
|
|
331
331
|
// set a submatrix
|
|
332
332
|
|
|
333
|
+
// broadcast submatrix
|
|
334
|
+
if (!deepStrictEqual(sSize, iSize)) {
|
|
335
|
+
try {
|
|
336
|
+
if (sSize.length === 0) {
|
|
337
|
+
submatrix = broadcastTo([submatrix], iSize);
|
|
338
|
+
} else {
|
|
339
|
+
submatrix = broadcastTo(submatrix, iSize);
|
|
340
|
+
}
|
|
341
|
+
sSize = arraySize(submatrix);
|
|
342
|
+
} catch (_unused) {}
|
|
343
|
+
}
|
|
344
|
+
|
|
333
345
|
// validate dimensions
|
|
334
346
|
if (iSize.length < matrix._size.length) {
|
|
335
347
|
throw new DimensionError(iSize.length, matrix._size.length, '<');
|
|
@@ -921,19 +933,17 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
|
|
|
921
933
|
|
|
922
934
|
/**
|
|
923
935
|
* Preprocess data, which can be an Array or DenseMatrix with nested Arrays and
|
|
924
|
-
* Matrices.
|
|
936
|
+
* Matrices. Clones all (nested) Arrays, and replaces all nested Matrices with Arrays
|
|
925
937
|
* @memberof DenseMatrix
|
|
926
|
-
* @param {Array} data
|
|
938
|
+
* @param {Array | Matrix} data
|
|
927
939
|
* @return {Array} data
|
|
928
940
|
*/
|
|
929
941
|
function preprocess(data) {
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
data[i] = preprocess(elem.valueOf());
|
|
936
|
-
}
|
|
942
|
+
if (isMatrix(data)) {
|
|
943
|
+
return preprocess(data.valueOf());
|
|
944
|
+
}
|
|
945
|
+
if (isArray(data)) {
|
|
946
|
+
return data.map(preprocess);
|
|
937
947
|
}
|
|
938
948
|
return data;
|
|
939
949
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { isMatrix, isRange } from '../../utils/is.js';
|
|
1
|
+
import { isArray, isMatrix, isRange } from '../../utils/is.js';
|
|
2
2
|
import { clone } from '../../utils/object.js';
|
|
3
3
|
import { isInteger } from '../../utils/number.js';
|
|
4
4
|
import { factory } from '../../utils/factory.js';
|
|
5
5
|
var name = 'Index';
|
|
6
|
-
var dependencies = ['ImmutableDenseMatrix'];
|
|
6
|
+
var dependencies = ['ImmutableDenseMatrix', 'getMatrixDataType'];
|
|
7
7
|
export var createIndexClass = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
8
8
|
var {
|
|
9
|
-
ImmutableDenseMatrix
|
|
9
|
+
ImmutableDenseMatrix,
|
|
10
|
+
getMatrixDataType
|
|
10
11
|
} = _ref;
|
|
11
12
|
/**
|
|
12
13
|
* Create an index. An Index can store ranges and sets for multiple dimensions.
|
|
@@ -20,7 +21,9 @@ export var createIndexClass = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
|
20
21
|
* A string (containing a name of an object property)
|
|
21
22
|
* An instance of Range
|
|
22
23
|
* An Array with the Set values
|
|
24
|
+
* An Array with Booleans
|
|
23
25
|
* A Matrix with the Set values
|
|
26
|
+
* A Matrix with Booleans
|
|
24
27
|
*
|
|
25
28
|
* The parameters start, end, and step must be integer numbers.
|
|
26
29
|
*
|
|
@@ -33,20 +36,31 @@ export var createIndexClass = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
|
33
36
|
throw new SyntaxError('Constructor must be called with the new operator');
|
|
34
37
|
}
|
|
35
38
|
this._dimensions = [];
|
|
39
|
+
this._sourceSize = [];
|
|
36
40
|
this._isScalar = true;
|
|
37
41
|
for (var i = 0, ii = arguments.length; i < ii; i++) {
|
|
38
42
|
var arg = arguments[i];
|
|
43
|
+
var argIsArray = isArray(arg);
|
|
44
|
+
var argIsMatrix = isMatrix(arg);
|
|
45
|
+
var sourceSize = null;
|
|
39
46
|
if (isRange(arg)) {
|
|
40
47
|
this._dimensions.push(arg);
|
|
41
48
|
this._isScalar = false;
|
|
42
|
-
} else if (
|
|
49
|
+
} else if (argIsArray || argIsMatrix) {
|
|
43
50
|
// create matrix
|
|
44
|
-
var m =
|
|
51
|
+
var m = void 0;
|
|
52
|
+
if (getMatrixDataType(arg) === 'boolean') {
|
|
53
|
+
if (argIsArray) m = _createImmutableMatrix(_booleansArrayToNumbersForIndex(arg).valueOf());
|
|
54
|
+
if (argIsMatrix) m = _createImmutableMatrix(_booleansArrayToNumbersForIndex(arg._data).valueOf());
|
|
55
|
+
sourceSize = arg.valueOf().length;
|
|
56
|
+
} else {
|
|
57
|
+
m = _createImmutableMatrix(arg.valueOf());
|
|
58
|
+
}
|
|
45
59
|
this._dimensions.push(m);
|
|
46
60
|
// size
|
|
47
61
|
var size = m.size();
|
|
48
62
|
// scalar
|
|
49
|
-
if (size.length !== 1 || size[0] !== 1) {
|
|
63
|
+
if (size.length !== 1 || size[0] !== 1 || sourceSize !== null) {
|
|
50
64
|
this._isScalar = false;
|
|
51
65
|
}
|
|
52
66
|
} else if (typeof arg === 'number') {
|
|
@@ -57,6 +71,7 @@ export var createIndexClass = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
|
57
71
|
} else {
|
|
58
72
|
throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range');
|
|
59
73
|
}
|
|
74
|
+
this._sourceSize.push(sourceSize);
|
|
60
75
|
// TODO: implement support for wildcard '*'
|
|
61
76
|
}
|
|
62
77
|
}
|
|
@@ -86,6 +101,7 @@ export var createIndexClass = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
|
86
101
|
var index = new Index();
|
|
87
102
|
index._dimensions = clone(this._dimensions);
|
|
88
103
|
index._isScalar = this._isScalar;
|
|
104
|
+
index._sourceSize = this._sourceSize;
|
|
89
105
|
return index;
|
|
90
106
|
};
|
|
91
107
|
|
|
@@ -263,4 +279,20 @@ export var createIndexClass = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
|
263
279
|
return Index;
|
|
264
280
|
}, {
|
|
265
281
|
isClass: true
|
|
266
|
-
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Receives an array of booleans and returns an array of Numbers for Index
|
|
286
|
+
* @param {Array} booleanArrayIndex An array of booleans
|
|
287
|
+
* @return {Array} A set of numbers ready for index
|
|
288
|
+
*/
|
|
289
|
+
function _booleansArrayToNumbersForIndex(booleanArrayIndex) {
|
|
290
|
+
// gets an array of booleans and returns an array of numbers
|
|
291
|
+
var indexOfNumbers = [];
|
|
292
|
+
booleanArrayIndex.forEach((bool, idx) => {
|
|
293
|
+
if (bool) {
|
|
294
|
+
indexOfNumbers.push(idx);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
return indexOfNumbers;
|
|
298
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { isBigNumber, isMatrix, isArray } from '../../../utils/is.js';
|
|
1
2
|
import { factory } from '../../../utils/factory.js';
|
|
2
|
-
import { isBigNumber, isMatrix } from '../../../utils/is.js';
|
|
3
3
|
var name = 'index';
|
|
4
4
|
var dependencies = ['typed', 'Index'];
|
|
5
5
|
export var createIndex = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
@@ -21,17 +21,19 @@ export var createIndex = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
|
21
21
|
* - A number
|
|
22
22
|
* - A string for getting/setting an object property
|
|
23
23
|
* - An instance of `Range`
|
|
24
|
-
* - A one-dimensional Array or a Matrix with numbers
|
|
24
|
+
* - A one-dimensional Array or a Matrix with numbers or booleans
|
|
25
25
|
*
|
|
26
26
|
* Indexes must be zero-based, integer numbers.
|
|
27
27
|
*
|
|
28
28
|
* Examples:
|
|
29
29
|
*
|
|
30
30
|
* const b = [1, 2, 3, 4, 5]
|
|
31
|
-
* math.subset(b, math.index([1, 2, 3]))
|
|
31
|
+
* math.subset(b, math.index([1, 2, 3])) // returns [2, 3, 4]
|
|
32
|
+
* math.subset(b, math.index([false, true, true, true, false])) // returns [2, 3, 4]
|
|
32
33
|
*
|
|
33
34
|
* const a = math.matrix([[1, 2], [3, 4]])
|
|
34
35
|
* a.subset(math.index(0, 1)) // returns 2
|
|
36
|
+
* a.subset(math.index(0, [false, true])) // returns 2
|
|
35
37
|
*
|
|
36
38
|
* See also:
|
|
37
39
|
*
|
|
@@ -45,7 +47,7 @@ export var createIndex = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
|
45
47
|
var ranges = args.map(function (arg) {
|
|
46
48
|
if (isBigNumber(arg)) {
|
|
47
49
|
return arg.toNumber(); // convert BigNumber to Number
|
|
48
|
-
} else if (
|
|
50
|
+
} else if (isArray(arg) || isMatrix(arg)) {
|
|
49
51
|
return arg.map(function (elem) {
|
|
50
52
|
// convert BigNumber to Number
|
|
51
53
|
return isBigNumber(elem) ? elem.toNumber() : elem;
|