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,69 @@
import { factory } from '../../utils/factory.js';
import { combinationsNumber } from '../../plain/number/combinations.js';
var name = 'combinations';
var dependencies = ['typed'];
export var createCombinations = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed
} = _ref;
/**
* Compute the number of ways of picking `k` unordered outcomes from `n`
* possibilities.
*
* Combinations only takes integer arguments.
* The following condition must be enforced: k <= n.
*
* Syntax:
*
* math.combinations(n, k)
*
* Examples:
*
* math.combinations(7, 5) // returns 21
*
* See also:
*
* combinationsWithRep, permutations, factorial
*
* @param {number | BigNumber} n Total number of objects in the set
* @param {number | BigNumber} k Number of objects in the subset
* @return {number | BigNumber} Number of possible combinations.
*/
return typed(name, {
'number, number': combinationsNumber,
'BigNumber, BigNumber': function BigNumber_BigNumber(n, k) {
var BigNumber = n.constructor;
var result, i;
var nMinusk = n.minus(k);
var one = new BigNumber(1);
if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
throw new TypeError('Positive integer value expected in function combinations');
}
if (k.gt(n)) {
throw new TypeError('k must be less than n in function combinations');
}
result = one;
if (k.lt(nMinusk)) {
for (i = one; i.lte(nMinusk); i = i.plus(one)) {
result = result.times(k.plus(i)).dividedBy(i);
}
} else {
for (i = one; i.lte(k); i = i.plus(one)) {
result = result.times(nMinusk.plus(i)).dividedBy(i);
}
}
return result;
}
// TODO: implement support for collection in combinations
});
});
/**
* Test whether BigNumber n is a positive integer
* @param {BigNumber} n
* @returns {boolean} isPositiveInteger
*/
function isPositiveInteger(n) {
return n.isInteger() && n.gte(0);
}

View File

@@ -0,0 +1,84 @@
import { factory } from '../../utils/factory.js';
import { isInteger } from '../../utils/number.js';
import { product } from '../../utils/product.js';
var name = 'combinationsWithRep';
var dependencies = ['typed'];
export var createCombinationsWithRep = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed
} = _ref;
/**
* Compute the number of ways of picking `k` unordered outcomes from `n`
* possibilities, allowing individual outcomes to be repeated more than once.
*
* CombinationsWithRep only takes integer arguments.
* The following condition must be enforced: k <= n + k -1.
*
* Syntax:
*
* math.combinationsWithRep(n, k)
*
* Examples:
*
* math.combinationsWithRep(7, 5) // returns 462
*
* See also:
*
* combinations, permutations, factorial
*
* @param {number | BigNumber} n Total number of objects in the set
* @param {number | BigNumber} k Number of objects in the subset
* @return {number | BigNumber} Number of possible combinations with replacement.
*/
return typed(name, {
'number, number': function number_number(n, k) {
if (!isInteger(n) || n < 0) {
throw new TypeError('Positive integer value expected in function combinationsWithRep');
}
if (!isInteger(k) || k < 0) {
throw new TypeError('Positive integer value expected in function combinationsWithRep');
}
if (n < 1) {
throw new TypeError('k must be less than or equal to n + k - 1');
}
if (k < n - 1) {
var _prodrange = product(n, n + k - 1);
return _prodrange / product(1, k);
}
var prodrange = product(k + 1, n + k - 1);
return prodrange / product(1, n - 1);
},
'BigNumber, BigNumber': function BigNumber_BigNumber(n, k) {
var BigNumber = n.constructor;
var result, i;
var one = new BigNumber(1);
var nMinusOne = n.minus(one);
if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
throw new TypeError('Positive integer value expected in function combinationsWithRep');
}
if (n.lt(one)) {
throw new TypeError('k must be less than or equal to n + k - 1 in function combinationsWithRep');
}
result = one;
if (k.lt(nMinusOne)) {
for (i = one; i.lte(nMinusOne); i = i.plus(one)) {
result = result.times(k.plus(i)).dividedBy(i);
}
} else {
for (i = one; i.lte(k); i = i.plus(one)) {
result = result.times(nMinusOne.plus(i)).dividedBy(i);
}
}
return result;
}
});
});
/**
* Test whether BigNumber n is a positive integer
* @param {BigNumber} n
* @returns {boolean} isPositiveInteger
*/
function isPositiveInteger(n) {
return n.isInteger() && n.gte(0);
}

View File

@@ -0,0 +1,47 @@
import { deepMap } from '../../utils/collection.js';
import { factory } from '../../utils/factory.js';
var name = 'factorial';
var dependencies = ['typed', 'gamma'];
export var createFactorial = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
gamma
} = _ref;
/**
* Compute the factorial of a value
*
* Factorial only supports an integer value as argument.
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.factorial(n)
*
* Examples:
*
* math.factorial(5) // returns 120
* math.factorial(3) // returns 6
*
* See also:
*
* combinations, combinationsWithRep, gamma, permutations
*
* @param {number | BigNumber | Array | Matrix} n An integer number
* @return {number | BigNumber | Array | Matrix} The factorial of `n`
*/
return typed(name, {
number: function number(n) {
if (n < 0) {
throw new Error('Value must be non-negative');
}
return gamma(n + 1);
},
BigNumber: function BigNumber(n) {
if (n.isNegative()) {
throw new Error('Value must be non-negative');
}
return gamma(n.plus(1));
},
'Array | Matrix': typed.referToSelf(self => n => deepMap(n, self))
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,77 @@
import { factory } from '../../utils/factory.js';
var name = 'kldivergence';
var dependencies = ['typed', 'matrix', 'divide', 'sum', 'multiply', 'map', 'dotDivide', 'log', 'isNumeric'];
export var createKldivergence = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
divide,
sum,
multiply,
map,
dotDivide,
log,
isNumeric
} = _ref;
/**
* Calculate the Kullback-Leibler (KL) divergence between two distributions
*
* Syntax:
*
* math.kldivergence(x, y)
*
* Examples:
*
* math.kldivergence([0.7,0.5,0.4], [0.2,0.9,0.5]) //returns 0.24376698773121153
*
*
* @param {Array | Matrix} q First vector
* @param {Array | Matrix} p Second vector
* @return {number} Returns distance between q and p
*/
return typed(name, {
'Array, Array': function Array_Array(q, p) {
return _kldiv(matrix(q), matrix(p));
},
'Matrix, Array': function Matrix_Array(q, p) {
return _kldiv(q, matrix(p));
},
'Array, Matrix': function Array_Matrix(q, p) {
return _kldiv(matrix(q), p);
},
'Matrix, Matrix': function Matrix_Matrix(q, p) {
return _kldiv(q, p);
}
});
function _kldiv(q, p) {
var plength = p.size().length;
var qlength = q.size().length;
if (plength > 1) {
throw new Error('first object must be one dimensional');
}
if (qlength > 1) {
throw new Error('second object must be one dimensional');
}
if (plength !== qlength) {
throw new Error('Length of two vectors must be equal');
}
// Before calculation, apply normalization
var sumq = sum(q);
if (sumq === 0) {
throw new Error('Sum of elements in first object must be non zero');
}
var sump = sum(p);
if (sump === 0) {
throw new Error('Sum of elements in second object must be non zero');
}
var qnorm = divide(q, sum(q));
var pnorm = divide(p, sum(p));
var result = sum(multiply(qnorm, map(dotDivide(qnorm, pnorm), x => log(x))));
if (isNumeric(result)) {
return result;
} else {
return Number.NaN;
}
}
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,50 @@
import { deepForEach } from '../../utils/collection.js';
import { factory } from '../../utils/factory.js';
var name = 'multinomial';
var dependencies = ['typed', 'add', 'divide', 'multiply', 'factorial', 'isInteger', 'isPositive'];
export var createMultinomial = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
divide,
multiply,
factorial,
isInteger,
isPositive
} = _ref;
/**
* Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities.
*
* multinomial takes one array of integers as an argument.
* The following condition must be enforced: every ai <= 0
*
* Syntax:
*
* math.multinomial(a) // a is an array type
*
* Examples:
*
* math.multinomial([1,2,1]) // returns 12
*
* See also:
*
* combinations, factorial
*
* @param {number[] | BigNumber[]} a Integer numbers of objects in the subset
* @return {Number | BigNumber} Multinomial coefficient.
*/
return typed(name, {
'Array | Matrix': function Array__Matrix(a) {
var sum = 0;
var denom = 1;
deepForEach(a, function (ai) {
if (!isInteger(ai) || !isPositive(ai)) {
throw new TypeError('Positive integer value expected in function multinomial');
}
sum = add(sum, ai);
denom = multiply(denom, factorial(ai));
});
return divide(factorial(sum), denom);
}
});
});

View File

@@ -0,0 +1,78 @@
import { isInteger } from '../../utils/number.js';
import { product } from '../../utils/product.js';
import { factory } from '../../utils/factory.js';
var name = 'permutations';
var dependencies = ['typed', 'factorial'];
export var createPermutations = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
factorial
} = _ref;
/**
* Compute the number of ways of obtaining an ordered subset of `k` elements
* from a set of `n` elements.
*
* Permutations only takes integer arguments.
* The following condition must be enforced: k <= n.
*
* Syntax:
*
* math.permutations(n)
* math.permutations(n, k)
*
* Examples:
*
* math.permutations(5) // 120
* math.permutations(5, 3) // 60
*
* See also:
*
* combinations, combinationsWithRep, factorial
*
* @param {number | BigNumber} n The number of objects in total
* @param {number | BigNumber} [k] The number of objects in the subset
* @return {number | BigNumber} The number of permutations
*/
return typed(name, {
'number | BigNumber': factorial,
'number, number': function number_number(n, k) {
if (!isInteger(n) || n < 0) {
throw new TypeError('Positive integer value expected in function permutations');
}
if (!isInteger(k) || k < 0) {
throw new TypeError('Positive integer value expected in function permutations');
}
if (k > n) {
throw new TypeError('second argument k must be less than or equal to first argument n');
}
// Permute n objects, k at a time
return product(n - k + 1, n);
},
'BigNumber, BigNumber': function BigNumber_BigNumber(n, k) {
var result, i;
if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
throw new TypeError('Positive integer value expected in function permutations');
}
if (k.gt(n)) {
throw new TypeError('second argument k must be less than or equal to first argument n');
}
var one = n.mul(0).add(1);
result = one;
for (i = n.minus(k).plus(1); i.lte(n); i = i.plus(1)) {
result = result.times(i);
}
return result;
}
// TODO: implement support for collection in permutations
});
});
/**
* Test whether BigNumber n is a positive integer
* @param {BigNumber} n
* @returns {boolean} isPositiveInteger
*/
function isPositiveInteger(n) {
return n.isInteger() && n.gte(0);
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,96 @@
import { factory } from '../../utils/factory.js';
import { isMatrix } from '../../utils/is.js';
import { createRng } from './util/seededRNG.js';
import { randomMatrix } from './util/randomMatrix.js';
var name = 'random';
var dependencies = ['typed', 'config', '?on'];
export var createRandom = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
on
} = _ref;
// seeded pseudo random number generator
var rng = createRng(config.randomSeed);
if (on) {
on('config', function (curr, prev) {
if (curr.randomSeed !== prev.randomSeed) {
rng = createRng(curr.randomSeed);
}
});
}
/**
* Return a random number larger or equal to `min` and smaller than `max`
* using a uniform distribution.
*
* Syntax:
*
* math.random() // generate a random number between 0 and 1
* math.random(max) // generate a random number between 0 and max
* math.random(min, max) // generate a random number between min and max
* math.random(size) // generate a matrix with random numbers between 0 and 1
* math.random(size, max) // generate a matrix with random numbers between 0 and max
* math.random(size, min, max) // generate a matrix with random numbers between min and max
*
* Examples:
*
* math.random() // returns a random number between 0 and 1
* math.random(100) // returns a random number between 0 and 100
* math.random(30, 40) // returns a random number between 30 and 40
* math.random([2, 3]) // returns a 2x3 matrix with random numbers between 0 and 1
*
* See also:
*
* randomInt, pickRandom
*
* @param {Array | Matrix} [size] If provided, an array or matrix with given
* size and filled with random values is returned
* @param {number} [min] Minimum boundary for the random value, included
* @param {number} [max] Maximum boundary for the random value, excluded
* @return {number | Array | Matrix} A random number
*/
return typed(name, {
'': () => _random(0, 1),
number: max => _random(0, max),
'number, number': (min, max) => _random(min, max),
'Array | Matrix': size => _randomMatrix(size, 0, 1),
'Array | Matrix, number': (size, max) => _randomMatrix(size, 0, max),
'Array | Matrix, number, number': (size, min, max) => _randomMatrix(size, min, max)
});
function _randomMatrix(size, min, max) {
var res = randomMatrix(size.valueOf(), () => _random(min, max));
return isMatrix(size) ? size.create(res, 'number') : res;
}
function _random(min, max) {
return min + rng() * (max - min);
}
});
// number only implementation of random, no matrix support
// TODO: there is quite some duplicate code in both createRandom and createRandomNumber, can we improve that?
export var createRandomNumber = /* #__PURE__ */factory(name, ['typed', 'config', '?on'], _ref2 => {
var {
typed,
config,
on,
matrix
} = _ref2;
// seeded pseudo random number generator1
var rng = createRng(config.randomSeed);
if (on) {
on('config', function (curr, prev) {
if (curr.randomSeed !== prev.randomSeed) {
rng = createRng(curr.randomSeed);
}
});
}
return typed(name, {
'': () => _random(0, 1),
number: max => _random(0, max),
'number, number': (min, max) => _random(min, max)
});
function _random(min, max) {
return min + rng() * (max - min);
}
});

Some files were not shown because too many files have changed in this diff Show More