feat:node-modules

This commit is contained in:
houjunxiang
2025-11-24 10:26:18 +08:00
parent 753766893b
commit 8a3e48d856
8825 changed files with 567399 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
import { factory } from '../../utils/factory.js';
var name = 'corr';
var dependencies = ['typed', 'matrix', 'mean', 'sqrt', 'sum', 'add', 'subtract', 'multiply', 'pow', 'divide'];
export var createCorr = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
sqrt,
sum,
add,
subtract,
multiply,
pow,
divide
} = _ref;
/**
* Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated.
*
* Syntax:
*
* math.corr(A, B)
*
* Examples:
*
* math.corr([1, 2, 3, 4, 5], [4, 5, 6, 7, 8]) // returns 1
* math.corr([1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]) //returns 0.9569941688503644
* math.corr([[1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]],[[1, 2.2, 3, 4.8, 5], [4, 5.3, 6.6, 7, 8]]) // returns [1,1]
*
* See also:
*
* median, mean, min, max, sum, prod, std, variance
*
* @param {Array | Matrix} A The first array or matrix to compute correlation coefficient
* @param {Array | Matrix} B The second array or matrix to compute correlation coefficient
* @return {*} The correlation coefficient
*/
return typed(name, {
'Array, Array': function Array_Array(A, B) {
return _corr(A, B);
},
'Matrix, Matrix': function Matrix_Matrix(A, B) {
var res = _corr(A.toArray(), B.toArray());
return Array.isArray(res) ? matrix(res) : res;
}
});
/**
* Calculate the correlation coefficient between two arrays or matrices.
* @param {Array | Matrix} A
* @param {Array | Matrix} B
* @return {*} correlation coefficient
* @private
*/
function _corr(A, B) {
var correlations = [];
if (Array.isArray(A[0]) && Array.isArray(B[0])) {
if (A.length !== B.length) {
throw new SyntaxError('Dimension mismatch. Array A and B must have the same length.');
}
for (var i = 0; i < A.length; i++) {
if (A[i].length !== B[i].length) {
throw new SyntaxError('Dimension mismatch. Array A and B must have the same number of elements.');
}
correlations.push(correlation(A[i], B[i]));
}
return correlations;
} else {
if (A.length !== B.length) {
throw new SyntaxError('Dimension mismatch. Array A and B must have the same number of elements.');
}
return correlation(A, B);
}
}
function correlation(A, B) {
var n = A.length;
var sumX = sum(A);
var sumY = sum(B);
var sumXY = A.reduce((acc, x, index) => add(acc, multiply(x, B[index])), 0);
var sumXSquare = sum(A.map(x => pow(x, 2)));
var sumYSquare = sum(B.map(y => pow(y, 2)));
var numerator = subtract(multiply(n, sumXY), multiply(sumX, sumY));
var denominator = sqrt(multiply(subtract(multiply(n, sumXSquare), pow(sumX, 2)), subtract(multiply(n, sumYSquare), pow(sumY, 2))));
return divide(numerator, denominator);
}
});

View File

@@ -0,0 +1,123 @@
import { containsCollections } from '../../utils/collection.js';
import { factory } from '../../utils/factory.js';
import { _switch } from '../../utils/switch.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
import { arraySize } from '../../utils/array.js';
import { IndexError } from '../../error/IndexError.js';
var name = 'cumsum';
var dependencies = ['typed', 'add', 'unaryPlus'];
export var createCumSum = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
unaryPlus
} = _ref;
/**
* Compute the cumulative sum of a matrix or a list with values.
* In case of a (multi dimensional) array or matrix, the cumulative sums
* along a specified dimension (defaulting to the first) will be calculated.
*
* Syntax:
*
* math.cumsum(a, b, c, ...)
* math.cumsum(A)
*
* Examples:
*
* math.cumsum(2, 1, 4, 3) // returns [2, 3, 7, 10]
* math.cumsum([2, 1, 4, 3]) // returns [2, 3, 7, 10]
* math.cumsum([[1, 2], [3, 4]]) // returns [[1, 2], [4, 6]]
* math.cumsum([[1, 2], [3, 4]], 0) // returns [[1, 2], [4, 6]]
* math.cumsum([[1, 2], [3, 4]], 1) // returns [[1, 3], [3, 7]]
* math.cumsum([[2, 5], [4, 3], [1, 7]]) // returns [[2, 5], [6, 8], [7, 15]]
*
* See also:
*
* mean, median, min, max, prod, std, variance, sum
*
* @param {... *} args A single matrix or or multiple scalar values
* @return {*} The cumulative sum of all values
*/
return typed(name, {
// sum([a, b, c, d, ...])
Array: _cumsum,
Matrix: function Matrix(matrix) {
return matrix.create(_cumsum(matrix.valueOf(), matrix.datatype()));
},
// sum([a, b, c, d, ...], dim)
'Array, number | BigNumber': _ncumSumDim,
'Matrix, number | BigNumber': function Matrix_number__BigNumber(matrix, dim) {
return matrix.create(_ncumSumDim(matrix.valueOf(), dim), matrix.datatype());
},
// cumsum(a, b, c, d, ...)
'...': function _(args) {
if (containsCollections(args)) {
throw new TypeError('All values expected to be scalar in function cumsum');
}
return _cumsum(args);
}
});
/**
* Recursively calculate the cumulative sum of an n-dimensional array
* @param {Array} array
* @return {number} cumsum
* @private
*/
function _cumsum(array) {
try {
return _cumsummap(array);
} catch (err) {
throw improveErrorMessage(err, name);
}
}
function _cumsummap(array) {
if (array.length === 0) {
return [];
}
var sums = [unaryPlus(array[0])]; // unaryPlus converts to number if need be
for (var i = 1; i < array.length; ++i) {
// Must use add below and not addScalar for the case of summing a
// 2+-dimensional array along the 0th dimension (the row vectors,
// or higher-d analogues, are literally added to each other).
sums.push(add(sums[i - 1], array[i]));
}
return sums;
}
function _ncumSumDim(array, dim) {
var size = arraySize(array);
if (dim < 0 || dim >= size.length) {
// TODO: would be more clear when throwing a DimensionError here
throw new IndexError(dim, size.length);
}
try {
return _cumsumDimensional(array, dim);
} catch (err) {
throw improveErrorMessage(err, name);
}
}
/* Possible TODO: Refactor _reduce in collection.js to be able to work here as well */
function _cumsumDimensional(mat, dim) {
var i, ret, tran;
if (dim <= 0) {
var initialValue = mat[0][0];
if (!Array.isArray(initialValue)) {
return _cumsummap(mat);
} else {
tran = _switch(mat);
ret = [];
for (i = 0; i < tran.length; i++) {
ret[i] = _cumsumDimensional(tran[i], dim - 1);
}
return ret;
}
} else {
ret = [];
for (i = 0; i < mat.length; i++) {
ret[i] = _cumsumDimensional(mat[i], dim - 1);
}
return ret;
}
}
});

64
node_modules/mathjs/lib/esm/function/statistics/mad.js generated vendored Normal file
View File

@@ -0,0 +1,64 @@
import { flatten } from '../../utils/array.js';
import { factory } from '../../utils/factory.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var name = 'mad';
var dependencies = ['typed', 'abs', 'map', 'median', 'subtract'];
export var createMad = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
abs,
map,
median,
subtract
} = _ref;
/**
* Compute the median absolute deviation of a matrix or a list with values.
* The median absolute deviation is defined as the median of the absolute
* deviations from the median.
*
* Syntax:
*
* math.mad(a, b, c, ...)
* math.mad(A)
*
* Examples:
*
* math.mad(10, 20, 30) // returns 10
* math.mad([1, 2, 3]) // returns 1
* math.mad([[1, 2, 3], [4, 5, 6]]) // returns 1.5
*
* See also:
*
* median, mean, std, abs
*
* @param {Array | Matrix} array
* A single matrix or multiple scalar values.
* @return {*} The median absolute deviation.
*/
return typed(name, {
// mad([a, b, c, d, ...])
'Array | Matrix': _mad,
// mad(a, b, c, d, ...)
'...': function _(args) {
return _mad(args);
}
});
function _mad(array) {
array = flatten(array.valueOf());
if (array.length === 0) {
throw new Error('Cannot calculate median absolute deviation (mad) of an empty array');
}
try {
var med = median(array);
return median(map(array, function (value) {
return abs(subtract(value, med));
}));
} catch (err) {
if (err instanceof TypeError && err.message.includes('median')) {
throw new TypeError(err.message.replace('median', 'mad'));
} else {
throw improveErrorMessage(err, 'mad');
}
}
}
});

105
node_modules/mathjs/lib/esm/function/statistics/max.js generated vendored Normal file
View File

@@ -0,0 +1,105 @@
import { deepForEach, reduce, containsCollections } from '../../utils/collection.js';
import { factory } from '../../utils/factory.js';
import { safeNumberType } from '../../utils/number.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var name = 'max';
var dependencies = ['typed', 'config', 'numeric', 'larger'];
export var createMax = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
numeric,
larger
} = _ref;
/**
* Compute the maximum value of a matrix or a list with values.
* In case of a multidimensional array, the maximum of the flattened array
* will be calculated. When `dim` is provided, the maximum over the selected
* dimension will be calculated. Parameter `dim` is zero-based.
*
* Syntax:
*
* math.max(a, b, c, ...)
* math.max(A)
* math.max(A, dimension)
*
* Examples:
*
* math.max(2, 1, 4, 3) // returns 4
* math.max([2, 1, 4, 3]) // returns 4
*
* // maximum over a specified dimension (zero-based)
* math.max([[2, 5], [4, 3], [1, 7]], 0) // returns [4, 7]
* math.max([[2, 5], [4, 3], [1, 7]], 1) // returns [5, 4, 7]
*
* math.max(2.7, 7.1, -4.5, 2.0, 4.1) // returns 7.1
* math.min(2.7, 7.1, -4.5, 2.0, 4.1) // returns -4.5
*
* See also:
*
* mean, median, min, prod, std, sum, variance
*
* @param {... *} args A single matrix or or multiple scalar values
* @return {*} The maximum value
*/
return typed(name, {
// max([a, b, c, d, ...])
'Array | Matrix': _max,
// max([a, b, c, d, ...], dim)
'Array | Matrix, number | BigNumber': function Array__Matrix_number__BigNumber(array, dim) {
return reduce(array, dim.valueOf(), _largest);
},
// max(a, b, c, d, ...)
'...': function _(args) {
if (containsCollections(args)) {
throw new TypeError('Scalar values expected in function max');
}
return _max(args);
}
});
/**
* Return the largest of two values
* @param {*} x
* @param {*} y
* @returns {*} Returns x when x is largest, or y when y is largest
* @private
*/
function _largest(x, y) {
try {
return larger(x, y) ? x : y;
} catch (err) {
throw improveErrorMessage(err, 'max', y);
}
}
/**
* Recursively calculate the maximum value in an n-dimensional array
* @param {Array} array
* @return {number} max
* @private
*/
function _max(array) {
var res;
deepForEach(array, function (value) {
try {
if (isNaN(value) && typeof value === 'number') {
res = NaN;
} else if (res === undefined || larger(value, res)) {
res = value;
}
} catch (err) {
throw improveErrorMessage(err, 'max', value);
}
});
if (res === undefined) {
throw new Error('Cannot calculate max of an empty array');
}
// make sure returning numeric value: parse a string into a numeric value
if (typeof res === 'string') {
res = numeric(res, safeNumberType(res, config));
}
return res;
}
});

View File

@@ -0,0 +1,94 @@
import { containsCollections, deepForEach, reduce } from '../../utils/collection.js';
import { arraySize } from '../../utils/array.js';
import { factory } from '../../utils/factory.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var name = 'mean';
var dependencies = ['typed', 'add', 'divide'];
export var createMean = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
divide
} = _ref;
/**
* Compute the mean value of matrix or a list with values.
* In case of a multidimensional array, the mean of the flattened array
* will be calculated. When `dim` is provided, the maximum over the selected
* dimension will be calculated. Parameter `dim` is zero-based.
*
* Syntax:
*
* math.mean(a, b, c, ...)
* math.mean(A)
* math.mean(A, dimension)
*
* Examples:
*
* math.mean(2, 1, 4, 3) // returns 2.5
* math.mean([1, 2.7, 3.2, 4]) // returns 2.725
*
* math.mean([[2, 5], [6, 3], [1, 7]], 0) // returns [3, 5]
* math.mean([[2, 5], [6, 3], [1, 7]], 1) // returns [3.5, 4.5, 4]
*
* See also:
*
* median, min, max, sum, prod, std, variance
*
* @param {... *} args A single matrix or or multiple scalar values
* @return {*} The mean of all values
*/
return typed(name, {
// mean([a, b, c, d, ...])
'Array | Matrix': _mean,
// mean([a, b, c, d, ...], dim)
'Array | Matrix, number | BigNumber': _nmeanDim,
// mean(a, b, c, d, ...)
'...': function _(args) {
if (containsCollections(args)) {
throw new TypeError('Scalar values expected in function mean');
}
return _mean(args);
}
});
/**
* Calculate the mean value in an n-dimensional array, returning a
* n-1 dimensional array
* @param {Array} array
* @param {number} dim
* @return {number} mean
* @private
*/
function _nmeanDim(array, dim) {
try {
var sum = reduce(array, dim, add);
var s = Array.isArray(array) ? arraySize(array) : array.size();
return divide(sum, s[dim]);
} catch (err) {
throw improveErrorMessage(err, 'mean');
}
}
/**
* Recursively calculate the mean value in an n-dimensional array
* @param {Array} array
* @return {number} mean
* @private
*/
function _mean(array) {
var sum;
var num = 0;
deepForEach(array, function (value) {
try {
sum = sum === undefined ? value : add(sum, value);
num++;
} catch (err) {
throw improveErrorMessage(err, 'mean', value);
}
});
if (num === 0) {
throw new Error('Cannot calculate the mean of an empty array');
}
return divide(sum, num);
}
});

View File

@@ -0,0 +1,108 @@
import { containsCollections } from '../../utils/collection.js';
import { flatten } from '../../utils/array.js';
import { factory } from '../../utils/factory.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var name = 'median';
var dependencies = ['typed', 'add', 'divide', 'compare', 'partitionSelect'];
export var createMedian = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
divide,
compare,
partitionSelect
} = _ref;
/**
* Recursively calculate the median of an n-dimensional array
* @param {Array} array
* @return {Number} median
* @private
*/
function _median(array) {
try {
array = flatten(array.valueOf());
var num = array.length;
if (num === 0) {
throw new Error('Cannot calculate median of an empty array');
}
if (num % 2 === 0) {
// even: return the average of the two middle values
var mid = num / 2 - 1;
var right = partitionSelect(array, mid + 1);
// array now partitioned at mid + 1, take max of left part
var left = array[mid];
for (var i = 0; i < mid; ++i) {
if (compare(array[i], left) > 0) {
left = array[i];
}
}
return middle2(left, right);
} else {
// odd: return the middle value
var m = partitionSelect(array, (num - 1) / 2);
return middle(m);
}
} catch (err) {
throw improveErrorMessage(err, 'median');
}
}
// helper function to type check the middle value of the array
var middle = typed({
'number | BigNumber | Complex | Unit': function number__BigNumber__Complex__Unit(value) {
return value;
}
});
// helper function to type check the two middle value of the array
var middle2 = typed({
'number | BigNumber | Complex | Unit, number | BigNumber | Complex | Unit': function number__BigNumber__Complex__Unit_number__BigNumber__Complex__Unit(left, right) {
return divide(add(left, right), 2);
}
});
/**
* Compute the median of a matrix or a list with values. The values are
* sorted and the middle value is returned. In case of an even number of
* values, the average of the two middle values is returned.
* Supported types of values are: Number, BigNumber, Unit
*
* In case of a (multi dimensional) array or matrix, the median of all
* elements will be calculated.
*
* Syntax:
*
* math.median(a, b, c, ...)
* math.median(A)
*
* Examples:
*
* math.median(5, 2, 7) // returns 5
* math.median([3, -1, 5, 7]) // returns 4
*
* See also:
*
* mean, min, max, sum, prod, std, variance, quantileSeq
*
* @param {... *} args A single matrix or or multiple scalar values
* @return {*} The median
*/
return typed(name, {
// median([a, b, c, d, ...])
'Array | Matrix': _median,
// median([a, b, c, d, ...], dim)
'Array | Matrix, number | BigNumber': function Array__Matrix_number__BigNumber(array, dim) {
// TODO: implement median(A, dim)
throw new Error('median(A, dim) is not yet supported');
// return reduce(arguments[0], arguments[1], ...)
},
// median(a, b, c, d, ...)
'...': function _(args) {
if (containsCollections(args)) {
throw new TypeError('Scalar values expected in function median');
}
return _median(args);
}
});
});

105
node_modules/mathjs/lib/esm/function/statistics/min.js generated vendored Normal file
View File

@@ -0,0 +1,105 @@
import { containsCollections, deepForEach, reduce } from '../../utils/collection.js';
import { factory } from '../../utils/factory.js';
import { safeNumberType } from '../../utils/number.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var name = 'min';
var dependencies = ['typed', 'config', 'numeric', 'smaller'];
export var createMin = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
numeric,
smaller
} = _ref;
/**
* Compute the minimum value of a matrix or a list of values.
* In case of a multidimensional array, the minimum of the flattened array
* will be calculated. When `dim` is provided, the minimum over the selected
* dimension will be calculated. Parameter `dim` is zero-based.
*
* Syntax:
*
* math.min(a, b, c, ...)
* math.min(A)
* math.min(A, dimension)
*
* Examples:
*
* math.min(2, 1, 4, 3) // returns 1
* math.min([2, 1, 4, 3]) // returns 1
*
* // minimum over a specified dimension (zero-based)
* math.min([[2, 5], [4, 3], [1, 7]], 0) // returns [1, 3]
* math.min([[2, 5], [4, 3], [1, 7]], 1) // returns [2, 3, 1]
*
* math.max(2.7, 7.1, -4.5, 2.0, 4.1) // returns 7.1
* math.min(2.7, 7.1, -4.5, 2.0, 4.1) // returns -4.5
*
* See also:
*
* mean, median, max, prod, std, sum, variance
*
* @param {... *} args A single matrix or or multiple scalar values
* @return {*} The minimum value
*/
return typed(name, {
// min([a, b, c, d, ...])
'Array | Matrix': _min,
// min([a, b, c, d, ...], dim)
'Array | Matrix, number | BigNumber': function Array__Matrix_number__BigNumber(array, dim) {
return reduce(array, dim.valueOf(), _smallest);
},
// min(a, b, c, d, ...)
'...': function _(args) {
if (containsCollections(args)) {
throw new TypeError('Scalar values expected in function min');
}
return _min(args);
}
});
/**
* Return the smallest of two values
* @param {*} x
* @param {*} y
* @returns {*} Returns x when x is smallest, or y when y is smallest
* @private
*/
function _smallest(x, y) {
try {
return smaller(x, y) ? x : y;
} catch (err) {
throw improveErrorMessage(err, 'min', y);
}
}
/**
* Recursively calculate the minimum value in an n-dimensional array
* @param {Array} array
* @return {number} min
* @private
*/
function _min(array) {
var min;
deepForEach(array, function (value) {
try {
if (isNaN(value) && typeof value === 'number') {
min = NaN;
} else if (min === undefined || smaller(value, min)) {
min = value;
}
} catch (err) {
throw improveErrorMessage(err, 'min', value);
}
});
if (min === undefined) {
throw new Error('Cannot calculate min of an empty array');
}
// make sure returning numeric value: parse a string into a numeric value
if (typeof min === 'string') {
min = numeric(min, safeNumberType(min, config));
}
return min;
}
});

View File

@@ -0,0 +1,76 @@
import { flatten } from '../../utils/array.js';
import { factory } from '../../utils/factory.js';
var name = 'mode';
var dependencies = ['typed', 'isNaN', 'isNumeric'];
export var createMode = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
isNaN,
isNumeric
} = _ref;
/**
* Computes the mode of a set of numbers or a list with values(numbers or characters).
* If there are multiple modes, it returns a list of those values.
*
* Syntax:
*
* math.mode(a, b, c, ...)
* math.mode(A)
*
* Examples:
*
* math.mode(2, 1, 4, 3, 1) // returns [1]
* math.mode([1, 2.7, 3.2, 4, 2.7]) // returns [2.7]
* math.mode(1, 4, 6, 1, 6) // returns [1, 6]
* math.mode('a','a','b','c') // returns ["a"]
* math.mode(1, 1.5, 'abc') // returns [1, 1.5, "abc"]
*
* See also:
*
* median,
* mean
*
* @param {... *} args A single matrix
* @return {*} The mode of all values
*/
return typed(name, {
'Array | Matrix': _mode,
'...': function _(args) {
return _mode(args);
}
});
/**
* Calculates the mode in an 1-dimensional array
* @param {Array} values
* @return {Array} mode
* @private
*/
function _mode(values) {
values = flatten(values.valueOf());
var num = values.length;
if (num === 0) {
throw new Error('Cannot calculate mode of an empty array');
}
var count = {};
var mode = [];
var max = 0;
for (var i = 0; i < values.length; i++) {
var value = values[i];
if (isNumeric(value) && isNaN(value)) {
throw new Error('Cannot calculate mode of an array containing NaN values');
}
if (!(value in count)) {
count[value] = 0;
}
count[value]++;
if (count[value] === max) {
mode.push(value);
} else if (count[value] > max) {
max = count[value];
mode = [value];
}
}
return mode;
}
});

View File

@@ -0,0 +1,79 @@
import { deepForEach } from '../../utils/collection.js';
import { factory } from '../../utils/factory.js';
import { safeNumberType } from '../../utils/number.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var name = 'prod';
var dependencies = ['typed', 'config', 'multiplyScalar', 'numeric'];
export var createProd = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
multiplyScalar,
numeric
} = _ref;
/**
* Compute the product of a matrix or a list with values.
* In case of a multidimensional array or matrix, the sum of all
* elements will be calculated.
*
* Syntax:
*
* math.prod(a, b, c, ...)
* math.prod(A)
*
* Examples:
*
* math.multiply(2, 3) // returns 6
* math.prod(2, 3) // returns 6
* math.prod(2, 3, 4) // returns 24
* math.prod([2, 3, 4]) // returns 24
* math.prod([[2, 5], [4, 3]]) // returns 120
*
* See also:
*
* mean, median, min, max, sum, std, variance
*
* @param {... *} args A single matrix or or multiple scalar values
* @return {*} The product of all values
*/
return typed(name, {
// prod([a, b, c, d, ...])
'Array | Matrix': _prod,
// prod([a, b, c, d, ...], dim)
'Array | Matrix, number | BigNumber': function Array__Matrix_number__BigNumber(array, dim) {
// TODO: implement prod(A, dim)
throw new Error('prod(A, dim) is not yet supported');
// return reduce(arguments[0], arguments[1], math.prod)
},
// prod(a, b, c, d, ...)
'...': function _(args) {
return _prod(args);
}
});
/**
* Recursively calculate the product of an n-dimensional array
* @param {Array} array
* @return {number} prod
* @private
*/
function _prod(array) {
var prod;
deepForEach(array, function (value) {
try {
prod = prod === undefined ? value : multiplyScalar(prod, value);
} catch (err) {
throw improveErrorMessage(err, 'prod', value);
}
});
// make sure returning numeric value: parse a string into a numeric value
if (typeof prod === 'string') {
prod = numeric(prod, safeNumberType(prod, config));
}
if (prod === undefined) {
throw new Error('Cannot calculate prod of an empty array');
}
return prod;
}
});

View File

@@ -0,0 +1,166 @@
import { isNumber } from '../../utils/is.js';
import { flatten } from '../../utils/array.js';
import { factory } from '../../utils/factory.js';
import { createApply } from '../matrix/apply.js';
var name = 'quantileSeq';
var dependencies = ['typed', '?bignumber', 'add', 'subtract', 'divide', 'multiply', 'partitionSelect', 'compare', 'isInteger', 'smaller', 'smallerEq', 'larger'];
export var createQuantileSeq = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
bignumber,
add,
subtract,
divide,
multiply,
partitionSelect,
compare,
isInteger,
smaller,
smallerEq,
larger
} = _ref;
var apply = createApply({
typed,
isInteger
});
/**
* Compute the prob order quantile of a matrix or a list with values.
* The sequence is sorted and the middle value is returned.
* Supported types of sequence values are: Number, BigNumber, Unit
* Supported types of probability are: Number, BigNumber
*
* In case of a multidimensional array or matrix, the prob order quantile
* of all elements will be calculated.
*
* Syntax:
*
* math.quantileSeq(A, prob[, sorted])
* math.quantileSeq(A, [prob1, prob2, ...][, sorted])
* math.quantileSeq(A, N[, sorted])
*
* Examples:
*
* math.quantileSeq([3, -1, 5, 7], 0.5) // returns 4
* math.quantileSeq([3, -1, 5, 7], [1/3, 2/3]) // returns [3, 5]
* math.quantileSeq([3, -1, 5, 7], 2) // returns [3, 5]
* math.quantileSeq([-1, 3, 5, 7], 0.5, true) // returns 4
*
* See also:
*
* median, mean, min, max, sum, prod, std, variance
*
* @param {Array, Matrix} data A single matrix or Array
* @param {Number, BigNumber, Array} probOrN prob is the order of the quantile, while N is
* the amount of evenly distributed steps of
* probabilities; only one of these options can
* be provided
* @param {Boolean} sorted=false is data sorted in ascending order
* @return {Number, BigNumber, Unit, Array} Quantile(s)
*/
return typed(name, {
'Array | Matrix, number | BigNumber': (data, p) => _quantileSeqProbNumber(data, p, false),
'Array | Matrix, number | BigNumber, number': (data, prob, dim) => _quantileSeqDim(data, prob, false, dim, _quantileSeqProbNumber),
'Array | Matrix, number | BigNumber, boolean': _quantileSeqProbNumber,
'Array | Matrix, number | BigNumber, boolean, number': (data, prob, sorted, dim) => _quantileSeqDim(data, prob, sorted, dim, _quantileSeqProbNumber),
'Array | Matrix, Array | Matrix': (data, p) => _quantileSeqProbCollection(data, p, false),
'Array | Matrix, Array | Matrix, number': (data, prob, dim) => _quantileSeqDim(data, prob, false, dim, _quantileSeqProbCollection),
'Array | Matrix, Array | Matrix, boolean': _quantileSeqProbCollection,
'Array | Matrix, Array | Matrix, boolean, number': (data, prob, sorted, dim) => _quantileSeqDim(data, prob, sorted, dim, _quantileSeqProbCollection)
});
function _quantileSeqDim(data, prob, sorted, dim, fn) {
return apply(data, dim, x => fn(x, prob, sorted));
}
function _quantileSeqProbNumber(data, probOrN, sorted) {
var probArr;
var dataArr = data.valueOf();
if (smaller(probOrN, 0)) {
throw new Error('N/prob must be non-negative');
}
if (smallerEq(probOrN, 1)) {
// quantileSeq([a, b, c, d, ...], prob[,sorted])
return isNumber(probOrN) ? _quantileSeq(dataArr, probOrN, sorted) : bignumber(_quantileSeq(dataArr, probOrN, sorted));
}
if (larger(probOrN, 1)) {
// quantileSeq([a, b, c, d, ...], N[,sorted])
if (!isInteger(probOrN)) {
throw new Error('N must be a positive integer');
}
// largest possible Array length is 2^32-1
// 2^32 < 10^15, thus safe conversion guaranteed
if (larger(probOrN, 4294967295)) {
throw new Error('N must be less than or equal to 2^32-1, as that is the maximum length of an Array');
}
var nPlusOne = add(probOrN, 1);
probArr = [];
for (var i = 0; smaller(i, probOrN); i++) {
var prob = divide(i + 1, nPlusOne);
probArr.push(_quantileSeq(dataArr, prob, sorted));
}
return isNumber(probOrN) ? probArr : bignumber(probArr);
}
}
/**
* Calculate the prob order quantile of an n-dimensional array.
*
* @param {Array, Matrix} array
* @param {Array, Matrix} prob
* @param {Boolean} sorted
* @return {Number, BigNumber, Unit} prob order quantile
* @private
*/
function _quantileSeqProbCollection(data, probOrN, sorted) {
var dataArr = data.valueOf();
// quantileSeq([a, b, c, d, ...], [prob1, prob2, ...][,sorted])
var probOrNArr = probOrN.valueOf();
var probArr = [];
for (var i = 0; i < probOrNArr.length; ++i) {
probArr.push(_quantileSeq(dataArr, probOrNArr[i], sorted));
}
return probArr;
}
/**
* Calculate the prob order quantile of an n-dimensional array.
*
* @param {Array} array
* @param {Number, BigNumber} prob
* @param {Boolean} sorted
* @return {Number, BigNumber, Unit} prob order quantile
* @private
*/
function _quantileSeq(array, prob, sorted) {
var flat = flatten(array);
var len = flat.length;
if (len === 0) {
throw new Error('Cannot calculate quantile of an empty sequence');
}
var index = isNumber(prob) ? prob * (len - 1) : prob.times(len - 1);
var integerPart = isNumber(prob) ? Math.floor(index) : index.floor().toNumber();
var fracPart = isNumber(prob) ? index % 1 : index.minus(integerPart);
if (isInteger(index)) {
return sorted ? flat[index] : partitionSelect(flat, isNumber(prob) ? index : index.valueOf());
}
var left;
var right;
if (sorted) {
left = flat[integerPart];
right = flat[integerPart + 1];
} else {
right = partitionSelect(flat, integerPart + 1);
// max of partition is kth largest
left = flat[integerPart];
for (var i = 0; i < integerPart; ++i) {
if (compare(flat[i], left) > 0) {
left = flat[i];
}
}
}
// Q(prob) = (1-f)*A[floor(index)] + f*A[floor(index)+1]
return add(multiply(left, subtract(1, fracPart)), multiply(right, fracPart));
}
});

97
node_modules/mathjs/lib/esm/function/statistics/std.js generated vendored Normal file
View File

@@ -0,0 +1,97 @@
import { factory } from '../../utils/factory.js';
import { isCollection } from '../../utils/is.js';
var name = 'std';
var dependencies = ['typed', 'map', 'sqrt', 'variance'];
export var createStd = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
map,
sqrt,
variance
} = _ref;
/**
* Compute the standard deviation of a matrix or a list with values.
* The standard deviations is defined as the square root of the variance:
* `std(A) = sqrt(variance(A))`.
* In case of a (multi dimensional) array or matrix, the standard deviation
* over all elements will be calculated by default, unless an axis is specified
* in which case the standard deviation will be computed along that axis.
*
* Additionally, it is possible to compute the standard deviation along the rows
* or columns of a matrix by specifying the dimension as the second argument.
*
* Optionally, the type of normalization can be specified as the final
* parameter. The parameter `normalization` can be one of the following values:
*
* - 'unbiased' (default) The sum of squared errors is divided by (n - 1)
* - 'uncorrected' The sum of squared errors is divided by n
* - 'biased' The sum of squared errors is divided by (n + 1)
*
*
* Syntax:
*
* math.std(a, b, c, ...)
* math.std(A)
* math.std(A, normalization)
* math.std(A, dimension)
* math.std(A, dimension, normalization)
*
* Examples:
*
* math.std(2, 4, 6) // returns 2
* math.std([2, 4, 6, 8]) // returns 2.581988897471611
* math.std([2, 4, 6, 8], 'uncorrected') // returns 2.23606797749979
* math.std([2, 4, 6, 8], 'biased') // returns 2
*
* math.std([[1, 2, 3], [4, 5, 6]]) // returns 1.8708286933869707
* math.std([[1, 2, 3], [4, 6, 8]], 0) // returns [2.1213203435596424, 2.8284271247461903, 3.5355339059327378]
* math.std([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 2]
* math.std([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.7071067811865476, 1.4142135623730951]
*
* See also:
*
* mean, median, max, min, prod, sum, variance
*
* @param {Array | Matrix} array
* A single matrix or or multiple scalar values
* @param {string} [normalization='unbiased']
* Determines how to normalize the variance.
* Choose 'unbiased' (default), 'uncorrected', or 'biased'.
* @param dimension {number | BigNumber}
* Determines the axis to compute the standard deviation for a matrix
* @return {*} The standard deviation
*/
return typed(name, {
// std([a, b, c, d, ...])
'Array | Matrix': _std,
// std([a, b, c, d, ...], normalization)
'Array | Matrix, string': _std,
// std([a, b, c, c, ...], dim)
'Array | Matrix, number | BigNumber': _std,
// std([a, b, c, c, ...], dim, normalization)
'Array | Matrix, number | BigNumber, string': _std,
// std(a, b, c, d, ...)
'...': function _(args) {
return _std(args);
}
});
function _std(array, normalization) {
if (array.length === 0) {
throw new SyntaxError('Function std requires one or more parameters (0 provided)');
}
try {
var v = variance.apply(null, arguments);
if (isCollection(v)) {
return map(v, sqrt);
} else {
return sqrt(v);
}
} catch (err) {
if (err instanceof TypeError && err.message.includes(' variance')) {
throw new TypeError(err.message.replace(' variance', ' std'));
} else {
throw err;
}
}
}
});

85
node_modules/mathjs/lib/esm/function/statistics/sum.js generated vendored Normal file
View File

@@ -0,0 +1,85 @@
import { containsCollections, deepForEach, reduce } from '../../utils/collection.js';
import { factory } from '../../utils/factory.js';
import { safeNumberType } from '../../utils/number.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var name = 'sum';
var dependencies = ['typed', 'config', 'add', 'numeric'];
export var createSum = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
add,
numeric
} = _ref;
/**
* Compute the sum of a matrix or a list with values.
* In case of a multidimensional array or matrix, the sum of all
* elements will be calculated.
*
* Syntax:
*
* math.sum(a, b, c, ...)
* math.sum(A)
* math.sum(A, dimension)
*
* Examples:
*
* math.sum(2, 1, 4, 3) // returns 10
* math.sum([2, 1, 4, 3]) // returns 10
* math.sum([[2, 5], [4, 3], [1, 7]]) // returns 22
*
* See also:
*
* mean, median, min, max, prod, std, variance, cumsum
*
* @param {... *} args A single matrix or multiple scalar values
* @return {*} The sum of all values
*/
return typed(name, {
// sum([a, b, c, d, ...])
'Array | Matrix': _sum,
// sum([a, b, c, d, ...], dim)
'Array | Matrix, number | BigNumber': _nsumDim,
// sum(a, b, c, d, ...)
'...': function _(args) {
if (containsCollections(args)) {
throw new TypeError('Scalar values expected in function sum');
}
return _sum(args);
}
});
/**
* Recursively calculate the sum of an n-dimensional array
* @param {Array | Matrix} array
* @return {number} sum
* @private
*/
function _sum(array) {
var sum;
deepForEach(array, function (value) {
try {
sum = sum === undefined ? value : add(sum, value);
} catch (err) {
throw improveErrorMessage(err, 'sum', value);
}
});
// make sure returning numeric value: parse a string into a numeric value
if (sum === undefined) {
sum = numeric(0, config.number);
}
if (typeof sum === 'string') {
sum = numeric(sum, safeNumberType(sum, config));
}
return sum;
}
function _nsumDim(array, dim) {
try {
var sum = reduce(array, dim, add);
return sum;
} catch (err) {
throw improveErrorMessage(err, 'sum');
}
}
});

View File

@@ -0,0 +1,25 @@
import { typeOf } from '../../../utils/is.js';
/**
* Improve error messages for statistics functions. Errors are typically
* thrown in an internally used function like larger, causing the error
* not to mention the function (like max) which is actually used by the user.
*
* @param {Error} err
* @param {String} fnName
* @param {*} [value]
* @return {Error}
*/
export function improveErrorMessage(err, fnName, value) {
// TODO: add information with the index (also needs transform in expression parser)
var details;
if (String(err).includes('Unexpected type')) {
details = arguments.length > 2 ? ' (type: ' + typeOf(value) + ', value: ' + JSON.stringify(value) + ')' : ' (type: ' + err.data.actual + ')';
return new TypeError('Cannot calculate ' + fnName + ', unexpected type of argument' + details);
}
if (String(err).includes('complex numbers')) {
details = arguments.length > 2 ? ' (type: ' + typeOf(value) + ', value: ' + JSON.stringify(value) + ')' : '';
return new TypeError('Cannot calculate ' + fnName + ', no ordering relation is defined for complex numbers' + details);
}
return err;
}

View File

@@ -0,0 +1,153 @@
import { deepForEach } from '../../utils/collection.js';
import { isBigNumber } from '../../utils/is.js';
import { factory } from '../../utils/factory.js';
import { improveErrorMessage } from './utils/improveErrorMessage.js';
var DEFAULT_NORMALIZATION = 'unbiased';
var name = 'variance';
var dependencies = ['typed', 'add', 'subtract', 'multiply', 'divide', 'apply', 'isNaN'];
export var createVariance = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
subtract,
multiply,
divide,
apply,
isNaN
} = _ref;
/**
* Compute the variance of a matrix or a list with values.
* In case of a multidimensional array or matrix, the variance over all
* elements will be calculated.
*
* Additionally, it is possible to compute the variance along the rows
* or columns of a matrix by specifying the dimension as the second argument.
*
* Optionally, the type of normalization can be specified as the final
* parameter. The parameter `normalization` can be one of the following values:
*
* - 'unbiased' (default) The sum of squared errors is divided by (n - 1)
* - 'uncorrected' The sum of squared errors is divided by n
* - 'biased' The sum of squared errors is divided by (n + 1)
*
*
* Note that older browser may not like the variable name `var`. In that
* case, the function can be called as `math['var'](...)` instead of
* `math.var(...)`.
*
* Syntax:
*
* math.variance(a, b, c, ...)
* math.variance(A)
* math.variance(A, normalization)
* math.variance(A, dimension)
* math.variance(A, dimension, normalization)
*
* Examples:
*
* math.variance(2, 4, 6) // returns 4
* math.variance([2, 4, 6, 8]) // returns 6.666666666666667
* math.variance([2, 4, 6, 8], 'uncorrected') // returns 5
* math.variance([2, 4, 6, 8], 'biased') // returns 4
*
* math.variance([[1, 2, 3], [4, 5, 6]]) // returns 3.5
* math.variance([[1, 2, 3], [4, 6, 8]], 0) // returns [4.5, 8, 12.5]
* math.variance([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 4]
* math.variance([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.5, 2]
*
* See also:
*
* mean, median, max, min, prod, std, sum
*
* @param {Array | Matrix} array
* A single matrix or or multiple scalar values
* @param {string} [normalization='unbiased']
* Determines how to normalize the variance.
* Choose 'unbiased' (default), 'uncorrected', or 'biased'.
* @param dimension {number | BigNumber}
* Determines the axis to compute the variance for a matrix
* @return {*} The variance
*/
return typed(name, {
// variance([a, b, c, d, ...])
'Array | Matrix': function Array__Matrix(array) {
return _var(array, DEFAULT_NORMALIZATION);
},
// variance([a, b, c, d, ...], normalization)
'Array | Matrix, string': _var,
// variance([a, b, c, c, ...], dim)
'Array | Matrix, number | BigNumber': function Array__Matrix_number__BigNumber(array, dim) {
return _varDim(array, dim, DEFAULT_NORMALIZATION);
},
// variance([a, b, c, c, ...], dim, normalization)
'Array | Matrix, number | BigNumber, string': _varDim,
// variance(a, b, c, d, ...)
'...': function _(args) {
return _var(args, DEFAULT_NORMALIZATION);
}
});
/**
* Recursively calculate the variance of an n-dimensional array
* @param {Array} array
* @param {string} normalization
* Determines how to normalize the variance:
* - 'unbiased' The sum of squared errors is divided by (n - 1)
* - 'uncorrected' The sum of squared errors is divided by n
* - 'biased' The sum of squared errors is divided by (n + 1)
* @return {number | BigNumber} variance
* @private
*/
function _var(array, normalization) {
var sum;
var num = 0;
if (array.length === 0) {
throw new SyntaxError('Function variance requires one or more parameters (0 provided)');
}
// calculate the mean and number of elements
deepForEach(array, function (value) {
try {
sum = sum === undefined ? value : add(sum, value);
num++;
} catch (err) {
throw improveErrorMessage(err, 'variance', value);
}
});
if (num === 0) throw new Error('Cannot calculate variance of an empty array');
var mean = divide(sum, num);
// calculate the variance
sum = undefined;
deepForEach(array, function (value) {
var diff = subtract(value, mean);
sum = sum === undefined ? multiply(diff, diff) : add(sum, multiply(diff, diff));
});
if (isNaN(sum)) {
return sum;
}
switch (normalization) {
case 'uncorrected':
return divide(sum, num);
case 'biased':
return divide(sum, num + 1);
case 'unbiased':
{
var zero = isBigNumber(sum) ? sum.mul(0) : 0;
return num === 1 ? zero : divide(sum, num - 1);
}
default:
throw new Error('Unknown normalization "' + normalization + '". ' + 'Choose "unbiased" (default), "uncorrected", or "biased".');
}
}
function _varDim(array, dim, normalization) {
try {
if (array.length === 0) {
throw new SyntaxError('Function variance requires one or more parameters (0 provided)');
}
return apply(array, dim, x => _var(x, normalization));
} catch (err) {
throw improveErrorMessage(err, 'variance');
}
}
});