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,35 @@
import { createAnd } from '../../function/logical/and.js';
import { factory } from '../../utils/factory.js';
import { isCollection } from '../../utils/is.js';
var name = 'and';
var dependencies = ['typed', 'matrix', 'zeros', 'add', 'equalScalar', 'not', 'concat'];
export var createAndTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
equalScalar,
zeros,
not,
concat
} = _ref;
var and = createAnd({
typed,
matrix,
equalScalar,
zeros,
not,
concat
});
function andTransform(args, math, scope) {
var condition1 = args[0].compile().evaluate(scope);
if (!isCollection(condition1) && !and(condition1, true)) {
return false;
}
var condition2 = args[1].compile().evaluate(scope);
return and(condition1, condition2);
}
andTransform.rawArgs = true;
return andTransform;
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,44 @@
import { errorTransform } from './utils/errorTransform.js';
import { factory } from '../../utils/factory.js';
import { createApply } from '../../function/matrix/apply.js';
import { isBigNumber, isNumber } from '../../utils/is.js';
var name = 'apply';
var dependencies = ['typed', 'isInteger'];
/**
* Attach a transform function to math.apply
* Adds a property transform containing the transform function.
*
* This transform changed the last `dim` parameter of function apply
* from one-based to zero based
*/
export var createApplyTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
isInteger
} = _ref;
var apply = createApply({
typed,
isInteger
});
// @see: comment of concat itself
return typed('apply', {
'...any': function any(args) {
// change dim from one-based to zero-based
var dim = args[1];
if (isNumber(dim)) {
args[1] = dim - 1;
} else if (isBigNumber(dim)) {
args[1] = dim.minus(1);
}
try {
return apply.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,40 @@
import { createBitAnd } from '../../function/bitwise/bitAnd.js';
import { factory } from '../../utils/factory.js';
import { isCollection } from '../../utils/is.js';
var name = 'bitAnd';
var dependencies = ['typed', 'matrix', 'zeros', 'add', 'equalScalar', 'not', 'concat'];
export var createBitAndTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
equalScalar,
zeros,
not,
concat
} = _ref;
var bitAnd = createBitAnd({
typed,
matrix,
equalScalar,
zeros,
not,
concat
});
function bitAndTransform(args, math, scope) {
var condition1 = args[0].compile().evaluate(scope);
if (!isCollection(condition1)) {
if (isNaN(condition1)) {
return NaN;
}
if (condition1 === 0 || condition1 === false) {
return 0;
}
}
var condition2 = args[1].compile().evaluate(scope);
return bitAnd(condition1, condition2);
}
bitAndTransform.rawArgs = true;
return bitAndTransform;
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,41 @@
import { createBitOr } from '../../function/bitwise/bitOr.js';
import { factory } from '../../utils/factory.js';
import { isCollection } from '../../utils/is.js';
var name = 'bitOr';
var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'concat'];
export var createBitOrTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
equalScalar,
DenseMatrix,
concat
} = _ref;
var bitOr = createBitOr({
typed,
matrix,
equalScalar,
DenseMatrix,
concat
});
function bitOrTransform(args, math, scope) {
var condition1 = args[0].compile().evaluate(scope);
if (!isCollection(condition1)) {
if (isNaN(condition1)) {
return NaN;
}
if (condition1 === -1) {
return -1;
}
if (condition1 === true) {
return 1;
}
}
var condition2 = args[1].compile().evaluate(scope);
return bitOr(condition1, condition2);
}
bitOrTransform.rawArgs = true;
return bitOrTransform;
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,47 @@
import { errorTransform } from './utils/errorTransform.js';
import { factory } from '../../utils/factory.js';
import { createColumn } from '../../function/matrix/column.js';
import { isNumber } from '../../utils/is.js';
var name = 'column';
var dependencies = ['typed', 'Index', 'matrix', 'range'];
/**
* Attach a transform function to matrix.column
* Adds a property transform containing the transform function.
*
* This transform changed the last `index` parameter of function column
* from zero-based to one-based
*/
export var createColumnTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
Index,
matrix,
range
} = _ref;
var column = createColumn({
typed,
Index,
matrix,
range
});
// @see: comment of column itself
return typed('column', {
'...any': function any(args) {
// change last argument from zero-based to one-based
var lastIndex = args.length - 1;
var last = args[lastIndex];
if (isNumber(last)) {
args[lastIndex] = last - 1;
}
try {
return column.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,45 @@
import { isBigNumber, isNumber } from '../../utils/is.js';
import { errorTransform } from './utils/errorTransform.js';
import { factory } from '../../utils/factory.js';
import { createConcat } from '../../function/matrix/concat.js';
var name = 'concat';
var dependencies = ['typed', 'matrix', 'isInteger'];
export var createConcatTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
isInteger
} = _ref;
var concat = createConcat({
typed,
matrix,
isInteger
});
/**
* Attach a transform function to math.range
* Adds a property transform containing the transform function.
*
* This transform changed the last `dim` parameter of function concat
* from one-based to zero based
*/
return typed('concat', {
'...any': function any(args) {
// change last argument from one-based to zero-based
var lastIndex = args.length - 1;
var last = args[lastIndex];
if (isNumber(last)) {
args[lastIndex] = last - 1;
} else if (isBigNumber(last)) {
args[lastIndex] = last.minus(1);
}
try {
return concat.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,46 @@
import { isBigNumber, isCollection, isNumber } from '../../utils/is.js';
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createCumSum } from '../../function/statistics/cumsum.js';
/**
* Attach a transform function to math.sum
* Adds a property transform containing the transform function.
*
* This transform changed the last `dim` parameter of function sum
* from one-based to zero based
*/
var name = 'cumsum';
var dependencies = ['typed', 'add', 'unaryPlus'];
export var createCumSumTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
unaryPlus
} = _ref;
var cumsum = createCumSum({
typed,
add,
unaryPlus
});
return typed(name, {
'...any': function any(args) {
// change last argument dim from one-based to zero-based
if (args.length === 2 && isCollection(args[0])) {
var dim = args[1];
if (isNumber(dim)) {
args[1] = dim - 1;
} else if (isBigNumber(dim)) {
args[1] = dim.minus(1);
}
}
try {
return cumsum.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,41 @@
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createDiff } from '../../function/matrix/diff.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
var name = 'diff';
var dependencies = ['typed', 'matrix', 'subtract', 'number', 'bignumber'];
export var createDiffTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
subtract,
number,
bignumber
} = _ref;
var diff = createDiff({
typed,
matrix,
subtract,
number,
bignumber
});
/**
* Attach a transform function to math.diff
* Adds a property transform containing the transform function.
*
* This transform creates a range which includes the end value
*/
return typed(name, {
'...any': function any(args) {
args = lastDimToZeroBase(args);
try {
return diff.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,56 @@
import { createFilter } from '../../function/matrix/filter.js';
import { factory } from '../../utils/factory.js';
import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
import { compileInlineExpression } from './utils/compileInlineExpression.js';
import { createTransformCallback } from './utils/transformCallback.js';
var name = 'filter';
var dependencies = ['typed'];
export var createFilterTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed
} = _ref;
/**
* Attach a transform function to math.filter
* Adds a property transform containing the transform function.
*
* This transform adds support for equations as test function for math.filter,
* so you can do something like 'filter([3, -2, 5], x > 0)'.
*/
function filterTransform(args, math, scope) {
var filter = createFilter({
typed
});
var transformCallback = createTransformCallback({
typed
});
if (args.length === 0) {
return filter();
}
var x = args[0];
if (args.length === 1) {
return filter(x);
}
var N = args.length - 1;
var callback = args[N];
if (x) {
x = _compileAndEvaluate(x, scope);
}
if (callback) {
if (isSymbolNode(callback) || isFunctionAssignmentNode(callback)) {
// a function pointer, like filter([3, -2, 5], myTestFunction)
callback = _compileAndEvaluate(callback, scope);
} else {
// an expression like filter([3, -2, 5], x > 0)
callback = compileInlineExpression(callback, math, scope);
}
}
return filter(x, transformCallback(callback, N));
}
filterTransform.rawArgs = true;
function _compileAndEvaluate(arg, scope) {
return arg.compile().evaluate(scope);
}
return filterTransform;
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,55 @@
import { createForEach } from '../../function/matrix/forEach.js';
import { createTransformCallback } from './utils/transformCallback.js';
import { factory } from '../../utils/factory.js';
import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
import { compileInlineExpression } from './utils/compileInlineExpression.js';
var name = 'forEach';
var dependencies = ['typed'];
export var createForEachTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed
} = _ref;
/**
* Attach a transform function to math.forEach
* Adds a property transform containing the transform function.
*
* This transform creates a one-based index instead of a zero-based index
*/
var forEach = createForEach({
typed
});
var transformCallback = createTransformCallback({
typed
});
function forEachTransform(args, math, scope) {
if (args.length === 0) {
return forEach();
}
var x = args[0];
if (args.length === 1) {
return forEach(x);
}
var N = args.length - 1;
var callback = args[N];
if (x) {
x = _compileAndEvaluate(x, scope);
}
if (callback) {
if (isSymbolNode(callback) || isFunctionAssignmentNode(callback)) {
// a function pointer, like filter([3, -2, 5], myTestFunction)
callback = _compileAndEvaluate(callback, scope);
} else {
// an expression like filter([3, -2, 5], x > 0)
callback = compileInlineExpression(callback, math, scope);
}
}
return forEach(x, transformCallback(callback, N));
}
forEachTransform.rawArgs = true;
function _compileAndEvaluate(arg, scope) {
return arg.compile().evaluate(scope);
}
return forEachTransform;
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,52 @@
import { isArray, isBigNumber, isMatrix, isNumber, isRange } from '../../utils/is.js';
import { factory } from '../../utils/factory.js';
var name = 'index';
var dependencies = ['Index', 'getMatrixDataType'];
export var createIndexTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
Index,
getMatrixDataType
} = _ref;
/**
* Attach a transform function to math.index
* Adds a property transform containing the transform function.
*
* This transform creates a one-based index instead of a zero-based index
*/
return function indexTransform() {
var args = [];
for (var i = 0, ii = arguments.length; i < ii; i++) {
var arg = arguments[i];
// change from one-based to zero based, convert BigNumber to number and leave Array of Booleans as is
if (isRange(arg)) {
arg.start--;
arg.end -= arg.step > 0 ? 0 : 2;
} else if (arg && arg.isSet === true) {
arg = arg.map(function (v) {
return v - 1;
});
} else if (isArray(arg) || isMatrix(arg)) {
if (getMatrixDataType(arg) !== 'boolean') {
arg = arg.map(function (v) {
return v - 1;
});
}
} else if (isNumber(arg)) {
arg--;
} else if (isBigNumber(arg)) {
arg = arg.toNumber() - 1;
} else if (typeof arg === 'string') {
// leave as is
} else {
throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range');
}
args[i] = arg;
}
var res = new Index();
Index.apply(res, args);
return res;
};
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,53 @@
import { factory } from '../../utils/factory.js';
import { isFunctionAssignmentNode, isSymbolNode } from '../../utils/is.js';
import { createMap } from '../../function/matrix/map.js';
import { compileInlineExpression } from './utils/compileInlineExpression.js';
import { createTransformCallback } from './utils/transformCallback.js';
var name = 'map';
var dependencies = ['typed'];
export var createMapTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed
} = _ref;
/**
* Attach a transform function to math.map
* Adds a property transform containing the transform function.
*
* This transform creates a one-based index instead of a zero-based index
*/
var map = createMap({
typed
});
var transformCallback = createTransformCallback({
typed
});
function mapTransform(args, math, scope) {
if (args.length === 0) {
return map();
}
if (args.length === 1) {
return map(args[0]);
}
var N = args.length - 1;
var X = args.slice(0, N);
var callback = args[N];
X = X.map(arg => _compileAndEvaluate(arg, scope));
if (callback) {
if (isSymbolNode(callback) || isFunctionAssignmentNode(callback)) {
// a function pointer, like filter([3, -2, 5], myTestFunction)
callback = _compileAndEvaluate(callback, scope);
} else {
// an expression like filter([3, -2, 5], x > 0)
callback = compileInlineExpression(callback, math, scope);
}
}
return map(...X, transformCallback(callback, N));
function _compileAndEvaluate(arg, scope) {
return arg.compile().evaluate(scope);
}
}
mapTransform.rawArgs = true;
return mapTransform;
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,40 @@
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createMax } from '../../function/statistics/max.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
var name = 'max';
var dependencies = ['typed', 'config', 'numeric', 'larger'];
export var createMaxTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
numeric,
larger
} = _ref;
var max = createMax({
typed,
config,
numeric,
larger
});
/**
* Attach a transform function to math.max
* Adds a property transform containing the transform function.
*
* This transform changed the last `dim` parameter of function max
* from one-based to zero based
*/
return typed('max', {
'...any': function any(args) {
args = lastDimToZeroBase(args);
try {
return max.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,38 @@
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createMean } from '../../function/statistics/mean.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
var name = 'mean';
var dependencies = ['typed', 'add', 'divide'];
export var createMeanTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
divide
} = _ref;
var mean = createMean({
typed,
add,
divide
});
/**
* Attach a transform function to math.mean
* Adds a property transform containing the transform function.
*
* This transform changed the last `dim` parameter of function mean
* from one-based to zero based
*/
return typed('mean', {
'...any': function any(args) {
args = lastDimToZeroBase(args);
try {
return mean.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,40 @@
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createMin } from '../../function/statistics/min.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
var name = 'min';
var dependencies = ['typed', 'config', 'numeric', 'smaller'];
export var createMinTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
numeric,
smaller
} = _ref;
var min = createMin({
typed,
config,
numeric,
smaller
});
/**
* Attach a transform function to math.min
* Adds a property transform containing the transform function.
*
* This transform changed the last `dim` parameter of function min
* from one-based to zero based
*/
return typed('min', {
'...any': function any(args) {
args = lastDimToZeroBase(args);
try {
return min.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,33 @@
import { createOr } from '../../function/logical/or.js';
import { factory } from '../../utils/factory.js';
import { isCollection } from '../../utils/is.js';
var name = 'or';
var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'concat'];
export var createOrTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
equalScalar,
DenseMatrix,
concat
} = _ref;
var or = createOr({
typed,
matrix,
equalScalar,
DenseMatrix,
concat
});
function orTransform(args, math, scope) {
var condition1 = args[0].compile().evaluate(scope);
if (!isCollection(condition1) && or(condition1, false)) {
return true;
}
var condition2 = args[1].compile().evaluate(scope);
return or(condition1, condition2);
}
orTransform.rawArgs = true;
return orTransform;
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,42 @@
import { createPrint } from '../../function/string/print.js';
import { factory } from '../../utils/factory.js';
import { printTemplate } from '../../utils/print.js';
var name = 'print';
var dependencies = ['typed', 'matrix', 'zeros', 'add'];
export var createPrintTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
zeros,
add
} = _ref;
var print = createPrint({
typed,
matrix,
zeros,
add
});
return typed(name, {
'string, Object | Array': function string_Object__Array(template, values) {
return print(_convertTemplateToZeroBasedIndex(template), values);
},
'string, Object | Array, number | Object': function string_Object__Array_number__Object(template, values, options) {
return print(_convertTemplateToZeroBasedIndex(template), values, options);
}
});
function _convertTemplateToZeroBasedIndex(template) {
return template.replace(printTemplate, x => {
var parts = x.slice(1).split('.');
var result = parts.map(function (part) {
if (!isNaN(part) && part.length > 0) {
return parseInt(part) - 1;
} else {
return part;
}
});
return '$' + result.join('.');
});
}
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,59 @@
import { factory } from '../../utils/factory.js';
import { createQuantileSeq } from '../../function/statistics/quantileSeq.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
var name = 'quantileSeq';
var dependencies = ['typed', 'bignumber', 'add', 'subtract', 'divide', 'multiply', 'partitionSelect', 'compare', 'isInteger', 'smaller', 'smallerEq', 'larger'];
/**
* Attach a transform function to math.quantileSeq
* Adds a property transform containing the transform function.
*
* This transform changed the `dim` parameter of function std
* from one-based to zero based
*/
export var createQuantileSeqTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
bignumber,
add,
subtract,
divide,
multiply,
partitionSelect,
compare,
isInteger,
smaller,
smallerEq,
larger
} = _ref;
var quantileSeq = createQuantileSeq({
typed,
bignumber,
add,
subtract,
divide,
multiply,
partitionSelect,
compare,
isInteger,
smaller,
smallerEq,
larger
});
return typed('quantileSeq', {
'Array | Matrix, number | BigNumber': quantileSeq,
'Array | Matrix, number | BigNumber, number': (arr, prob, dim) => quantileSeq(arr, prob, dimToZeroBase(dim)),
'Array | Matrix, number | BigNumber, boolean': quantileSeq,
'Array | Matrix, number | BigNumber, boolean, number': (arr, prob, sorted, dim) => quantileSeq(arr, prob, sorted, dimToZeroBase(dim)),
'Array | Matrix, Array | Matrix': quantileSeq,
'Array | Matrix, Array | Matrix, number': (data, prob, dim) => quantileSeq(data, prob, dimToZeroBase(dim)),
'Array | Matrix, Array | Matrix, boolean': quantileSeq,
'Array | Matrix, Array | Matrix, boolean, number': (data, prob, sorted, dim) => quantileSeq(data, prob, sorted, dimToZeroBase(dim))
});
function dimToZeroBase(dim) {
// TODO: find a better way, maybe lastDimToZeroBase could apply to more cases.
return lastDimToZeroBase([[], dim])[1];
}
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,50 @@
import { factory } from '../../utils/factory.js';
import { createRange } from '../../function/matrix/range.js';
var name = 'range';
var dependencies = ['typed', 'config', '?matrix', '?bignumber', 'smaller', 'smallerEq', 'larger', 'largerEq', 'add', 'isPositive'];
export var createRangeTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
matrix,
bignumber,
smaller,
smallerEq,
larger,
largerEq,
add,
isPositive
} = _ref;
var range = createRange({
typed,
config,
matrix,
bignumber,
smaller,
smallerEq,
larger,
largerEq,
add,
isPositive
});
/**
* Attach a transform function to math.range
* Adds a property transform containing the transform function.
*
* This transform creates a range which includes the end value
*/
return typed('range', {
'...any': function any(args) {
var lastIndex = args.length - 1;
var last = args[lastIndex];
if (typeof last !== 'boolean') {
// append a parameter includeEnd=true
args.push(true);
}
return range.apply(null, args);
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,47 @@
import { factory } from '../../utils/factory.js';
import { createRow } from '../../function/matrix/row.js';
import { errorTransform } from './utils/errorTransform.js';
import { isNumber } from '../../utils/is.js';
var name = 'row';
var dependencies = ['typed', 'Index', 'matrix', 'range'];
/**
* Attach a transform function to matrix.column
* Adds a property transform containing the transform function.
*
* This transform changed the last `index` parameter of function column
* from zero-based to one-based
*/
export var createRowTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
Index,
matrix,
range
} = _ref;
var row = createRow({
typed,
Index,
matrix,
range
});
// @see: comment of row itself
return typed('row', {
'...any': function any(args) {
// change last argument from zero-based to one-based
var lastIndex = args.length - 1;
var last = args[lastIndex];
if (isNumber(last)) {
args[lastIndex] = last - 1;
}
try {
return row.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,40 @@
import { factory } from '../../utils/factory.js';
import { createStd } from '../../function/statistics/std.js';
import { errorTransform } from './utils/errorTransform.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
var name = 'std';
var dependencies = ['typed', 'map', 'sqrt', 'variance'];
/**
* Attach a transform function to math.std
* Adds a property transform containing the transform function.
*
* This transform changed the `dim` parameter of function std
* from one-based to zero based
*/
export var createStdTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
map,
sqrt,
variance
} = _ref;
var std = createStd({
typed,
map,
sqrt,
variance
});
return typed('std', {
'...any': function any(args) {
args = lastDimToZeroBase(args);
try {
return std.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,37 @@
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createSubset } from '../../function/matrix/subset.js';
var name = 'subset';
var dependencies = ['typed', 'matrix', 'zeros', 'add'];
export var createSubsetTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
matrix,
zeros,
add
} = _ref;
var subset = createSubset({
typed,
matrix,
zeros,
add
});
/**
* Attach a transform function to math.subset
* Adds a property transform containing the transform function.
*
* This transform creates a range which includes the end value
*/
return typed('subset', {
'...any': function any(args) {
try {
return subset.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,40 @@
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createSum } from '../../function/statistics/sum.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
/**
* Attach a transform function to math.sum
* Adds a property transform containing the transform function.
*
* This transform changed the last `dim` parameter of function sum
* from one-based to zero based
*/
var name = 'sum';
var dependencies = ['typed', 'config', 'add', 'numeric'];
export var createSumTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
config,
add,
numeric
} = _ref;
var sum = createSum({
typed,
config,
add,
numeric
});
return typed(name, {
'...any': function any(args) {
args = lastDimToZeroBase(args);
try {
return sum.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});

View File

@@ -0,0 +1,30 @@
import { isSymbolNode } from '../../../utils/is.js';
import { PartitionedMap } from '../../../utils/map.js';
/**
* Compile an inline expression like "x > 0"
* @param {Node} expression
* @param {Object} math
* @param {Map} scope
* @return {function} Returns a function with one argument which fills in the
* undefined variable (like "x") and evaluates the expression
*/
export function compileInlineExpression(expression, math, scope) {
// find an undefined symbol
var symbol = expression.filter(function (node) {
return isSymbolNode(node) && !(node.name in math) && !scope.has(node.name);
})[0];
if (!symbol) {
throw new Error('No undefined variable found in inline expression "' + expression + '"');
}
// create a test function for this equation
var name = symbol.name; // variable name
var argsScope = new Map();
var subScope = new PartitionedMap(scope, argsScope, new Set([name]));
var eq = expression.compile();
return function inlineExpression(x) {
argsScope.set(name, x);
return eq.evaluate(subScope);
};
}

View File

@@ -0,0 +1,16 @@
import { isNumber, isBigNumber } from '../../../utils/is.js';
/**
* Change last argument dim from one-based to zero-based.
*/
export function dimToZeroBase(dim) {
if (isNumber(dim)) {
return dim - 1;
} else if (isBigNumber(dim)) {
return dim.minus(1);
} else {
return dim;
}
}
export function isNumberOrBigNumber(n) {
return isNumber(n) || isBigNumber(n);
}

View File

@@ -0,0 +1,13 @@
import { IndexError } from '../../../error/IndexError.js';
/**
* Transform zero-based indices to one-based indices in errors
* @param {Error} err
* @returns {Error | IndexError} Returns the transformed error
*/
export function errorTransform(err) {
if (err && err.isIndexError) {
return new IndexError(err.index + 1, err.min + 1, err.max !== undefined ? err.max + 1 : undefined);
}
return err;
}

View File

@@ -0,0 +1,15 @@
import { isCollection } from '../../../utils/is.js';
import { dimToZeroBase, isNumberOrBigNumber } from './dimToZeroBase.js';
/**
* Change last argument dim from one-based to zero-based.
*/
export function lastDimToZeroBase(args) {
if (args.length === 2 && isCollection(args[0])) {
args = args.slice();
var dim = args[1];
if (isNumberOrBigNumber(dim)) {
args[1] = dimToZeroBase(dim);
}
}
return args;
}

View File

@@ -0,0 +1,95 @@
import { factory } from '../../../utils/factory.js';
var name = 'transformCallback';
var dependencies = ['typed'];
export var createTransformCallback = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed
} = _ref;
/**
* Transforms the given callback function based on its type and number of arrays.
*
* @param {Function} callback - The callback function to transform.
* @param {number} numberOfArrays - The number of arrays to pass to the callback function.
* @returns {*} - The transformed callback function.
*/
return function (callback, numberOfArrays) {
if (typed.isTypedFunction(callback)) {
return _transformTypedCallbackFunction(callback, numberOfArrays);
} else {
return _transformCallbackFunction(callback, callback.length, numberOfArrays);
}
};
/**
* Transforms the given typed callback function based on the number of arrays.
*
* @param {Function} typedFunction - The typed callback function to transform.
* @param {number} numberOfArrays - The number of arrays to pass to the callback function.
* @returns {*} - The transformed callback function.
*/
function _transformTypedCallbackFunction(typedFunction, numberOfArrays) {
var signatures = Object.fromEntries(Object.entries(typedFunction.signatures).map(_ref2 => {
var [signature, callbackFunction] = _ref2;
var numberOfCallbackInputs = signature.split(',').length;
if (typed.isTypedFunction(callbackFunction)) {
return [signature, _transformTypedCallbackFunction(callbackFunction, numberOfArrays)];
} else {
return [signature, _transformCallbackFunction(callbackFunction, numberOfCallbackInputs, numberOfArrays)];
}
}));
if (typeof typedFunction.name === 'string') {
return typed(typedFunction.name, signatures);
} else {
return typed(signatures);
}
}
});
/**
* Transforms the callback function based on the number of callback inputs and arrays.
* There are three cases:
* 1. The callback function has N arguments.
* 2. The callback function has N+1 arguments.
* 3. The callback function has 2N+1 arguments.
*
* @param {Function} callbackFunction - The callback function to transform.
* @param {number} numberOfCallbackInputs - The number of callback inputs.
* @param {number} numberOfArrays - The number of arrays.
* @returns {Function} The transformed callback function.
*/
function _transformCallbackFunction(callbackFunction, numberOfCallbackInputs, numberOfArrays) {
if (numberOfCallbackInputs === numberOfArrays) {
return callbackFunction;
} else if (numberOfCallbackInputs === numberOfArrays + 1) {
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var vals = args.slice(0, numberOfArrays);
var idx = _transformDims(args[numberOfArrays]);
return callbackFunction(...vals, idx);
};
} else if (numberOfCallbackInputs > numberOfArrays + 1) {
return function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var vals = args.slice(0, numberOfArrays);
var idx = _transformDims(args[numberOfArrays]);
var rest = args.slice(numberOfArrays + 1);
return callbackFunction(...vals, idx, ...rest);
};
} else {
return callbackFunction;
}
}
/**
* Transforms the dimensions by adding 1 to each dimension.
*
* @param {Array} dims - The dimensions to transform.
* @returns {Array} The transformed dimensions.
*/
function _transformDims(dims) {
return dims.map(dim => dim + 1);
}

View File

@@ -0,0 +1,46 @@
import { factory } from '../../utils/factory.js';
import { errorTransform } from './utils/errorTransform.js';
import { createVariance } from '../../function/statistics/variance.js';
import { lastDimToZeroBase } from './utils/lastDimToZeroBase.js';
var name = 'variance';
var dependencies = ['typed', 'add', 'subtract', 'multiply', 'divide', 'apply', 'isNaN'];
/**
* Attach a transform function to math.var
* Adds a property transform containing the transform function.
*
* This transform changed the `dim` parameter of function var
* from one-based to zero based
*/
export var createVarianceTransform = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
add,
subtract,
multiply,
divide,
apply,
isNaN
} = _ref;
var variance = createVariance({
typed,
add,
subtract,
multiply,
divide,
apply,
isNaN
});
return typed(name, {
'...any': function any(args) {
args = lastDimToZeroBase(args);
try {
return variance.apply(null, args);
} catch (err) {
throw errorTransform(err);
}
}
});
}, {
isTransformFunction: true
});