mathjs 13.1.1 → 13.2.1
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 +24 -0
- package/lib/browser/math.js +1 -1
- package/lib/browser/math.js.LICENSE.txt +2 -9
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/core/function/typed.js +2 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesParserClass.generated.js +2 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSimplify.generated.js +0 -18
- package/lib/cjs/entry/dependenciesNumber/dependenciesParserClass.generated.js +2 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesSimplify.generated.js +0 -14
- package/lib/cjs/entry/impureFunctionsAny.generated.js +2 -12
- package/lib/cjs/entry/impureFunctionsNumber.generated.js +2 -10
- package/lib/cjs/expression/Parser.js +21 -2
- package/lib/cjs/expression/parse.js +1 -1
- package/lib/cjs/expression/transform/filter.transform.js +28 -40
- package/lib/cjs/expression/transform/forEach.transform.js +29 -30
- package/lib/cjs/expression/transform/map.transform.js +8 -93
- package/lib/cjs/expression/transform/utils/transformCallback.js +101 -0
- package/lib/cjs/function/algebra/derivative.js +10 -11
- package/lib/cjs/function/algebra/simplify.js +2 -13
- package/lib/cjs/function/matrix/filter.js +3 -2
- package/lib/cjs/function/matrix/forEach.js +3 -14
- package/lib/cjs/function/matrix/map.js +11 -33
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/type/matrix/DenseMatrix.js +73 -29
- package/lib/cjs/type/matrix/MatrixIndex.js +3 -0
- package/lib/cjs/type/matrix/SparseMatrix.js +7 -4
- package/lib/cjs/type/resultset/ResultSet.js +1 -1
- package/lib/cjs/utils/array.js +22 -0
- package/lib/cjs/utils/optimizeCallback.js +94 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/core/function/typed.js +2 -1
- package/lib/esm/entry/dependenciesAny/dependenciesParserClass.generated.js +2 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSimplify.generated.js +0 -18
- package/lib/esm/entry/dependenciesNumber/dependenciesParserClass.generated.js +2 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesSimplify.generated.js +0 -14
- package/lib/esm/entry/impureFunctionsAny.generated.js +2 -12
- package/lib/esm/entry/impureFunctionsNumber.generated.js +2 -10
- package/lib/esm/expression/Parser.js +21 -2
- package/lib/esm/expression/parse.js +1 -1
- package/lib/esm/expression/transform/filter.transform.js +28 -40
- package/lib/esm/expression/transform/forEach.transform.js +29 -30
- package/lib/esm/expression/transform/map.transform.js +8 -93
- package/lib/esm/expression/transform/utils/transformCallback.js +95 -0
- package/lib/esm/function/algebra/derivative.js +10 -11
- package/lib/esm/function/algebra/simplify.js +2 -13
- package/lib/esm/function/matrix/filter.js +3 -2
- package/lib/esm/function/matrix/forEach.js +3 -14
- package/lib/esm/function/matrix/map.js +12 -34
- package/lib/esm/type/matrix/DenseMatrix.js +75 -32
- package/lib/esm/type/matrix/MatrixIndex.js +3 -0
- package/lib/esm/type/matrix/SparseMatrix.js +7 -4
- package/lib/esm/type/resultset/ResultSet.js +1 -1
- package/lib/esm/utils/array.js +21 -0
- package/lib/esm/utils/optimizeCallback.js +88 -0
- package/lib/esm/version.js +1 -1
- package/package.json +19 -19
- package/types/index.d.ts +50 -12
- package/lib/cjs/utils/applyCallback.js +0 -73
- package/lib/esm/utils/applyCallback.js +0 -67
@@ -0,0 +1,95 @@
|
|
1
|
+
import { factory } from '../../../utils/factory.js';
|
2
|
+
var name = 'transformCallback';
|
3
|
+
var dependencies = ['typed'];
|
4
|
+
export var createTransformCallback = /* #__PURE__ */factory(name, dependencies, _ref => {
|
5
|
+
var {
|
6
|
+
typed
|
7
|
+
} = _ref;
|
8
|
+
/**
|
9
|
+
* Transforms the given callback function based on its type and number of arrays.
|
10
|
+
*
|
11
|
+
* @param {Function} callback - The callback function to transform.
|
12
|
+
* @param {number} numberOfArrays - The number of arrays to pass to the callback function.
|
13
|
+
* @returns {*} - The transformed callback function.
|
14
|
+
*/
|
15
|
+
return function (callback, numberOfArrays) {
|
16
|
+
if (typed.isTypedFunction(callback)) {
|
17
|
+
return _transformTypedCallbackFunction(callback, numberOfArrays);
|
18
|
+
} else {
|
19
|
+
return _transformCallbackFunction(callback, callback.length, numberOfArrays);
|
20
|
+
}
|
21
|
+
};
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Transforms the given typed callback function based on the number of arrays.
|
25
|
+
*
|
26
|
+
* @param {Function} typedFunction - The typed callback function to transform.
|
27
|
+
* @param {number} numberOfArrays - The number of arrays to pass to the callback function.
|
28
|
+
* @returns {*} - The transformed callback function.
|
29
|
+
*/
|
30
|
+
function _transformTypedCallbackFunction(typedFunction, numberOfArrays) {
|
31
|
+
var signatures = Object.fromEntries(Object.entries(typedFunction.signatures).map(_ref2 => {
|
32
|
+
var [signature, callbackFunction] = _ref2;
|
33
|
+
var numberOfCallbackInputs = signature.split(',').length;
|
34
|
+
if (typed.isTypedFunction(callbackFunction)) {
|
35
|
+
return [signature, _transformTypedCallbackFunction(callbackFunction, numberOfArrays)];
|
36
|
+
} else {
|
37
|
+
return [signature, _transformCallbackFunction(callbackFunction, numberOfCallbackInputs, numberOfArrays)];
|
38
|
+
}
|
39
|
+
}));
|
40
|
+
if (typeof typedFunction.name === 'string') {
|
41
|
+
return typed(typedFunction.name, signatures);
|
42
|
+
} else {
|
43
|
+
return typed(signatures);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
});
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Transforms the callback function based on the number of callback inputs and arrays.
|
50
|
+
* There are three cases:
|
51
|
+
* 1. The callback function has N arguments.
|
52
|
+
* 2. The callback function has N+1 arguments.
|
53
|
+
* 3. The callback function has 2N+1 arguments.
|
54
|
+
*
|
55
|
+
* @param {Function} callbackFunction - The callback function to transform.
|
56
|
+
* @param {number} numberOfCallbackInputs - The number of callback inputs.
|
57
|
+
* @param {number} numberOfArrays - The number of arrays.
|
58
|
+
* @returns {Function} The transformed callback function.
|
59
|
+
*/
|
60
|
+
function _transformCallbackFunction(callbackFunction, numberOfCallbackInputs, numberOfArrays) {
|
61
|
+
if (numberOfCallbackInputs === numberOfArrays) {
|
62
|
+
return callbackFunction;
|
63
|
+
} else if (numberOfCallbackInputs === numberOfArrays + 1) {
|
64
|
+
return function () {
|
65
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
66
|
+
args[_key] = arguments[_key];
|
67
|
+
}
|
68
|
+
var vals = args.slice(0, numberOfArrays);
|
69
|
+
var idx = _transformDims(args[numberOfArrays]);
|
70
|
+
return callbackFunction(...vals, idx);
|
71
|
+
};
|
72
|
+
} else if (numberOfCallbackInputs > numberOfArrays + 1) {
|
73
|
+
return function () {
|
74
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
75
|
+
args[_key2] = arguments[_key2];
|
76
|
+
}
|
77
|
+
var vals = args.slice(0, numberOfArrays);
|
78
|
+
var idx = _transformDims(args[numberOfArrays]);
|
79
|
+
var rest = args.slice(numberOfArrays + 1);
|
80
|
+
return callbackFunction(...vals, idx, ...rest);
|
81
|
+
};
|
82
|
+
} else {
|
83
|
+
return callbackFunction;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
/**
|
88
|
+
* Transforms the dimensions by adding 1 to each dimension.
|
89
|
+
*
|
90
|
+
* @param {Array} dims - The dimensions to transform.
|
91
|
+
* @returns {Array} The transformed dimensions.
|
92
|
+
*/
|
93
|
+
function _transformDims(dims) {
|
94
|
+
return dims.map(dim => dim + 1);
|
95
|
+
}
|
@@ -65,14 +65,18 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
65
65
|
var res = _derivative(expr, constNodes);
|
66
66
|
return options.simplify ? simplify(res) : res;
|
67
67
|
}
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
68
|
+
function parseIdentifier(string) {
|
69
|
+
var symbol = parse(string);
|
70
|
+
if (!symbol.isSymbolNode) {
|
71
|
+
throw new TypeError('Invalid variable. ' + "Cannot parse ".concat(JSON.stringify(string), " into a variable in function derivative"));
|
72
|
+
}
|
73
|
+
return symbol;
|
74
|
+
}
|
73
75
|
var derivative = typed(name, {
|
74
76
|
'Node, SymbolNode': plainDerivative,
|
75
|
-
'Node, SymbolNode, Object': plainDerivative
|
77
|
+
'Node, SymbolNode, Object': plainDerivative,
|
78
|
+
'Node, string': (node, symbol) => plainDerivative(node, parseIdentifier(symbol)),
|
79
|
+
'Node, string, Object': (node, symbol, options) => plainDerivative(node, parseIdentifier(symbol), options)
|
76
80
|
|
77
81
|
/* TODO: implement and test syntax with order of derivatives -> implement as an option {order: number}
|
78
82
|
'Node, SymbolNode, ConstantNode': function (expr, variable, {order}) {
|
@@ -86,11 +90,6 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
86
90
|
}
|
87
91
|
*/
|
88
92
|
});
|
89
|
-
typed.removeConversion({
|
90
|
-
from: 'identifier',
|
91
|
-
to: 'SymbolNode',
|
92
|
-
convert: parse
|
93
|
-
});
|
94
93
|
derivative._simplify = true;
|
95
94
|
derivative.toTex = function (deriv) {
|
96
95
|
return _derivTex.apply(null, deriv.args);
|
@@ -5,26 +5,15 @@ import { createUtil } from './simplify/util.js';
|
|
5
5
|
import { hasOwnProperty } from '../../utils/object.js';
|
6
6
|
import { createEmptyMap, createMap } from '../../utils/map.js';
|
7
7
|
var name = 'simplify';
|
8
|
-
var dependencies = ['
|
8
|
+
var dependencies = ['typed', 'parse', 'equal', 'resolve', 'simplifyConstant', 'simplifyCore', 'AccessorNode', 'ArrayNode', 'ConstantNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'ParenthesisNode', 'SymbolNode'];
|
9
9
|
export var createSimplify = /* #__PURE__ */factory(name, dependencies, _ref => {
|
10
10
|
var {
|
11
|
-
config,
|
12
11
|
typed,
|
13
12
|
parse,
|
14
|
-
add,
|
15
|
-
subtract,
|
16
|
-
multiply,
|
17
|
-
divide,
|
18
|
-
pow,
|
19
|
-
isZero,
|
20
13
|
equal,
|
21
14
|
resolve,
|
22
15
|
simplifyConstant,
|
23
16
|
simplifyCore,
|
24
|
-
fraction,
|
25
|
-
bignumber,
|
26
|
-
mathWithTransform,
|
27
|
-
matrix,
|
28
17
|
AccessorNode,
|
29
18
|
ArrayNode,
|
30
19
|
ConstantNode,
|
@@ -189,7 +178,7 @@ export var createSimplify = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
189
178
|
simplify.realContext = realContext;
|
190
179
|
simplify.positiveContext = positiveContext;
|
191
180
|
function removeParens(node) {
|
192
|
-
return node.transform(function (node
|
181
|
+
return node.transform(function (node) {
|
193
182
|
return isParenthesisNode(node) ? removeParens(node.content) : node;
|
194
183
|
});
|
195
184
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { optimizeCallback } from '../../utils/optimizeCallback.js';
|
2
2
|
import { filter, filterRegExp } from '../../utils/array.js';
|
3
3
|
import { factory } from '../../utils/factory.js';
|
4
4
|
var name = 'filter';
|
@@ -56,8 +56,9 @@ export var createFilter = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
56
56
|
* @private
|
57
57
|
*/
|
58
58
|
function _filterCallback(x, callback) {
|
59
|
+
var fastCallback = optimizeCallback(callback, x, 'filter');
|
59
60
|
return filter(x, function (value, index, array) {
|
60
61
|
// invoke the callback function with the right number of arguments
|
61
|
-
return
|
62
|
+
return fastCallback(value, [index], array);
|
62
63
|
});
|
63
64
|
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import {
|
2
|
-
import { forEach as forEachArray } from '../../utils/array.js';
|
1
|
+
import { optimizeCallback } from '../../utils/optimizeCallback.js';
|
3
2
|
import { factory } from '../../utils/factory.js';
|
3
|
+
import { recurse } from '../../utils/array.js';
|
4
4
|
var name = 'forEach';
|
5
5
|
var dependencies = ['typed'];
|
6
6
|
export var createForEach = /* #__PURE__ */factory(name, dependencies, _ref => {
|
@@ -45,16 +45,5 @@ export var createForEach = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
45
45
|
* @private
|
46
46
|
*/
|
47
47
|
function _forEach(array, callback) {
|
48
|
-
|
49
|
-
if (Array.isArray(value)) {
|
50
|
-
forEachArray(value, function (child, i) {
|
51
|
-
// we create a copy of the index array and append the new index value
|
52
|
-
_recurse(child, index.concat(i));
|
53
|
-
});
|
54
|
-
} else {
|
55
|
-
// invoke the callback function with the right number of arguments
|
56
|
-
return applyCallback(callback, value, index, array, 'forEach');
|
57
|
-
}
|
58
|
-
};
|
59
|
-
_recurse(array, []);
|
48
|
+
recurse(array, [], array, optimizeCallback(callback, array, name));
|
60
49
|
}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
import { arraySize, broadcastSizes, broadcastTo, get } from '../../utils/array.js';
|
1
|
+
import { optimizeCallback } from '../../utils/optimizeCallback.js';
|
2
|
+
import { arraySize, broadcastSizes, broadcastTo, get, recurse } from '../../utils/array.js';
|
3
3
|
import { factory } from '../../utils/factory.js';
|
4
4
|
var name = 'map';
|
5
5
|
var dependencies = ['typed'];
|
@@ -129,36 +129,14 @@ export var createMap = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
129
129
|
return 0;
|
130
130
|
}
|
131
131
|
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
function _mapArray(array, callback) {
|
142
|
-
return _recurse(array, [], array, callback);
|
143
|
-
}
|
144
|
-
|
145
|
-
/**
|
146
|
-
* Recursive function to map a multi-dimensional array.
|
147
|
-
*
|
148
|
-
* @param {*} value - The current value being processed in the array.
|
149
|
-
* @param {Array} index - The index of the current value being processed in the array.
|
150
|
-
* @param {Array} array - The array being processed.
|
151
|
-
* @param {Function} callback - Function that produces the element of the new Array, taking three arguments: the value of the element, the index of the element, and the Array being processed.
|
152
|
-
* @returns {*} The new array with each element being the result of the callback function.
|
153
|
-
*/
|
154
|
-
function _recurse(value, index, array, callback) {
|
155
|
-
if (Array.isArray(value)) {
|
156
|
-
return value.map(function (child, i) {
|
157
|
-
// we create a copy of the index array and append the new index value
|
158
|
-
return _recurse(child, index.concat(i), array, callback);
|
159
|
-
});
|
160
|
-
} else {
|
161
|
-
// invoke the callback function with the right number of arguments
|
162
|
-
return applyCallback(callback, value, index, array, 'map');
|
132
|
+
/**
|
133
|
+
* Map for a multi dimensional array
|
134
|
+
* @param {Array} array
|
135
|
+
* @param {Function} callback
|
136
|
+
* @return {Array}
|
137
|
+
* @private
|
138
|
+
*/
|
139
|
+
function _mapArray(array, callback) {
|
140
|
+
return recurse(array, [], array, optimizeCallback(callback, array, name));
|
163
141
|
}
|
164
|
-
}
|
142
|
+
});
|
@@ -1,3 +1,4 @@
|
|
1
|
+
// deno-lint-ignore-file no-this-alias
|
1
2
|
import { isArray, isBigNumber, isCollection, isIndex, isMatrix, isNumber, isString, typeOf } from '../../utils/is.js';
|
2
3
|
import { arraySize, getArrayDataType, processSizesWildcard, reshape, resize, unsqueeze, validate, validateIndex, broadcastTo, get } from '../../utils/array.js';
|
3
4
|
import { format } from '../../utils/string.js';
|
@@ -5,7 +6,7 @@ import { isInteger } from '../../utils/number.js';
|
|
5
6
|
import { clone, deepStrictEqual } from '../../utils/object.js';
|
6
7
|
import { DimensionError } from '../../error/DimensionError.js';
|
7
8
|
import { factory } from '../../utils/factory.js';
|
8
|
-
import {
|
9
|
+
import { optimizeCallback } from '../../utils/optimizeCallback.js';
|
9
10
|
var name = 'DenseMatrix';
|
10
11
|
var dependencies = ['Matrix'];
|
11
12
|
export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _ref => {
|
@@ -516,6 +517,66 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
|
|
516
517
|
return this._size.slice(0); // return a clone of _size
|
517
518
|
};
|
518
519
|
|
520
|
+
/**
|
521
|
+
* Applies a callback function to a reference to each element of the matrix
|
522
|
+
* @memberof DenseMatrix
|
523
|
+
* @param {Function} callback The callback function is invoked with three
|
524
|
+
* parameters: an array, an integer index to that
|
525
|
+
* array, and the Matrix being traversed.
|
526
|
+
*/
|
527
|
+
DenseMatrix.prototype._forEach = function (callback) {
|
528
|
+
// matrix instance
|
529
|
+
var me = this;
|
530
|
+
var s = me.size();
|
531
|
+
|
532
|
+
// if there is only one dimension, just loop through it
|
533
|
+
if (s.length === 1) {
|
534
|
+
for (var i = 0; i < s[0]; i++) {
|
535
|
+
callback(me._data, i, [i]);
|
536
|
+
}
|
537
|
+
return;
|
538
|
+
}
|
539
|
+
|
540
|
+
// keep track of the current index permutation
|
541
|
+
var index = Array(s.length).fill(0);
|
542
|
+
|
543
|
+
// store a reference of each dimension of the matrix for faster access
|
544
|
+
var data = Array(s.length - 1);
|
545
|
+
var last = data.length - 1;
|
546
|
+
data[0] = me._data[0];
|
547
|
+
for (var _i = 0; _i < last; _i++) {
|
548
|
+
data[_i + 1] = data[_i][0];
|
549
|
+
}
|
550
|
+
index[last] = -1;
|
551
|
+
while (true) {
|
552
|
+
var _i2 = void 0;
|
553
|
+
for (_i2 = last; _i2 >= 0; _i2--) {
|
554
|
+
// march index to the next permutation
|
555
|
+
index[_i2]++;
|
556
|
+
if (index[_i2] === s[_i2]) {
|
557
|
+
index[_i2] = 0;
|
558
|
+
continue;
|
559
|
+
}
|
560
|
+
|
561
|
+
// update references to matrix dimensions
|
562
|
+
data[_i2] = _i2 === 0 ? me._data[index[_i2]] : data[_i2 - 1][index[_i2]];
|
563
|
+
for (var j = _i2; j < last; j++) {
|
564
|
+
data[j + 1] = data[j][0];
|
565
|
+
}
|
566
|
+
|
567
|
+
// loop through the last dimension and map each value
|
568
|
+
for (var _j = 0; _j < s[data.length]; _j++) {
|
569
|
+
index[data.length] = _j;
|
570
|
+
callback(data[last], _j, index.slice(0));
|
571
|
+
}
|
572
|
+
break;
|
573
|
+
}
|
574
|
+
if (_i2 === -1) {
|
575
|
+
break;
|
576
|
+
}
|
577
|
+
}
|
578
|
+
};
|
579
|
+
|
519
580
|
/**
|
520
581
|
* Create a new matrix with the results of the callback function executed on
|
521
582
|
* each entry of the matrix.
|
@@ -527,24 +588,13 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
|
|
527
588
|
* @return {DenseMatrix} matrix
|
528
589
|
*/
|
529
590
|
DenseMatrix.prototype.map = function (callback) {
|
530
|
-
// matrix instance
|
531
591
|
var me = this;
|
532
|
-
var
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
// invoke the callback function with the right number of arguments
|
539
|
-
return applyCallback(callback, value, index, me, 'map');
|
540
|
-
}
|
541
|
-
};
|
542
|
-
|
543
|
-
// determine the new datatype when the original matrix has datatype defined
|
544
|
-
// TODO: should be done in matrix constructor instead
|
545
|
-
var data = _recurse(this._data, []);
|
546
|
-
var datatype = this._datatype !== undefined ? getArrayDataType(data, typeOf) : undefined;
|
547
|
-
return new DenseMatrix(data, datatype);
|
592
|
+
var result = new DenseMatrix(me);
|
593
|
+
var fastCallback = optimizeCallback(callback, me._data, 'map');
|
594
|
+
result._forEach(function (arr, i, index) {
|
595
|
+
arr[i] = fastCallback(arr[i], index, me);
|
596
|
+
});
|
597
|
+
return result;
|
548
598
|
};
|
549
599
|
|
550
600
|
/**
|
@@ -555,18 +605,11 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
|
|
555
605
|
* of the element, and the Matrix being traversed.
|
556
606
|
*/
|
557
607
|
DenseMatrix.prototype.forEach = function (callback) {
|
558
|
-
// matrix instance
|
559
608
|
var me = this;
|
560
|
-
var
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
});
|
565
|
-
} else {
|
566
|
-
callback(value, index, me);
|
567
|
-
}
|
568
|
-
};
|
569
|
-
_recurse2(this._data, []);
|
609
|
+
var fastCallback = optimizeCallback(callback, me._data, 'map');
|
610
|
+
me._forEach(function (arr, i, index) {
|
611
|
+
fastCallback(arr[i], index, me);
|
612
|
+
});
|
570
613
|
};
|
571
614
|
|
572
615
|
/**
|
@@ -574,10 +617,10 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
|
|
574
617
|
* @return {Iterable<{ value, index: number[] }>}
|
575
618
|
*/
|
576
619
|
DenseMatrix.prototype[Symbol.iterator] = function* () {
|
577
|
-
var
|
620
|
+
var _recurse = function* recurse(value, index) {
|
578
621
|
if (isArray(value)) {
|
579
622
|
for (var i = 0; i < value.length; i++) {
|
580
|
-
yield*
|
623
|
+
yield* _recurse(value[i], index.concat(i));
|
581
624
|
}
|
582
625
|
} else {
|
583
626
|
yield {
|
@@ -586,7 +629,7 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
|
|
586
629
|
};
|
587
630
|
}
|
588
631
|
};
|
589
|
-
yield*
|
632
|
+
yield* _recurse(this._data, []);
|
590
633
|
};
|
591
634
|
|
592
635
|
/**
|
@@ -180,6 +180,9 @@ export var createIndexClass = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
180
180
|
* @returns {Range | null} range
|
181
181
|
*/
|
182
182
|
Index.prototype.dimension = function (dim) {
|
183
|
+
if (typeof dim !== 'number') {
|
184
|
+
return null;
|
185
|
+
}
|
183
186
|
return this._dimensions[dim] || null;
|
184
187
|
};
|
185
188
|
|
@@ -5,7 +5,7 @@ import { clone, deepStrictEqual } from '../../utils/object.js';
|
|
5
5
|
import { arraySize, getArrayDataType, processSizesWildcard, unsqueeze, validateIndex } from '../../utils/array.js';
|
6
6
|
import { factory } from '../../utils/factory.js';
|
7
7
|
import { DimensionError } from '../../error/DimensionError.js';
|
8
|
-
import {
|
8
|
+
import { optimizeCallback } from '../../utils/optimizeCallback.js';
|
9
9
|
var name = 'SparseMatrix';
|
10
10
|
var dependencies = ['typed', 'equalScalar', 'Matrix'];
|
11
11
|
export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies, _ref => {
|
@@ -872,10 +872,11 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
|
|
872
872
|
// rows and columns
|
873
873
|
var rows = this._size[0];
|
874
874
|
var columns = this._size[1];
|
875
|
+
var fastCallback = optimizeCallback(callback, me, 'map');
|
875
876
|
// invoke callback
|
876
877
|
var invoke = function invoke(v, i, j) {
|
877
878
|
// invoke callback
|
878
|
-
return
|
879
|
+
return fastCallback(v, [i, j], me);
|
879
880
|
};
|
880
881
|
// invoke _map
|
881
882
|
return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros);
|
@@ -980,6 +981,7 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
|
|
980
981
|
// rows and columns
|
981
982
|
var rows = this._size[0];
|
982
983
|
var columns = this._size[1];
|
984
|
+
var fastCallback = optimizeCallback(callback, me, 'forEach');
|
983
985
|
// loop columns
|
984
986
|
for (var j = 0; j < columns; j++) {
|
985
987
|
// k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1]
|
@@ -992,7 +994,7 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
|
|
992
994
|
var i = this._index[k];
|
993
995
|
|
994
996
|
// value @ k
|
995
|
-
|
997
|
+
fastCallback(this._values[k], [i, j], me);
|
996
998
|
}
|
997
999
|
} else {
|
998
1000
|
// create a cache holding all defined values
|
@@ -1006,7 +1008,7 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
|
|
1006
1008
|
// and either read the value or zero
|
1007
1009
|
for (var _i7 = 0; _i7 < rows; _i7++) {
|
1008
1010
|
var value = _i7 in values ? values[_i7] : 0;
|
1009
|
-
|
1011
|
+
fastCallback(value, [_i7, j], me);
|
1010
1012
|
}
|
1011
1013
|
}
|
1012
1014
|
}
|
@@ -1405,6 +1407,7 @@ export var createSparseMatrixClass = /* #__PURE__ */factory(name, dependencies,
|
|
1405
1407
|
// indeces for column j
|
1406
1408
|
var k0 = ptr[j];
|
1407
1409
|
var k1 = ptr[j + 1];
|
1410
|
+
|
1408
1411
|
// loop
|
1409
1412
|
for (var k = k0; k < k1; k++) {
|
1410
1413
|
// invoke callback
|
@@ -36,7 +36,7 @@ export var createResultSet = /* #__PURE__ */factory(name, dependencies, () => {
|
|
36
36
|
* @returns {string} string
|
37
37
|
*/
|
38
38
|
ResultSet.prototype.toString = function () {
|
39
|
-
return '[' + this.entries.join(', ') + ']';
|
39
|
+
return '[' + this.entries.map(String).join(', ') + ']';
|
40
40
|
};
|
41
41
|
|
42
42
|
/**
|
package/lib/esm/utils/array.js
CHANGED
@@ -806,6 +806,27 @@ export function get(array, index) {
|
|
806
806
|
return index.reduce((acc, curr) => acc[curr], array);
|
807
807
|
}
|
808
808
|
|
809
|
+
/**
|
810
|
+
* Recursive function to map a multi-dimensional array.
|
811
|
+
*
|
812
|
+
* @param {*} value - The current value being processed in the array.
|
813
|
+
* @param {Array} index - The index of the current value being processed in the array.
|
814
|
+
* @param {Array} array - The array being processed.
|
815
|
+
* @param {Function} callback - Function that produces the element of the new Array, taking three arguments: the value of the element, the index of the element, and the Array being processed.
|
816
|
+
* @returns {*} The new array with each element being the result of the callback function.
|
817
|
+
*/
|
818
|
+
export function recurse(value, index, array, callback) {
|
819
|
+
if (Array.isArray(value)) {
|
820
|
+
return value.map(function (child, i) {
|
821
|
+
// we create a copy of the index array and append the new index value
|
822
|
+
return recurse(child, index.concat(i), array, callback);
|
823
|
+
});
|
824
|
+
} else {
|
825
|
+
// invoke the callback function with the right number of arguments
|
826
|
+
return callback(value, index, array);
|
827
|
+
}
|
828
|
+
}
|
829
|
+
|
809
830
|
/**
|
810
831
|
* Deep clones a multidimensional array
|
811
832
|
* @param {Array} array
|
@@ -0,0 +1,88 @@
|
|
1
|
+
import typed from 'typed-function';
|
2
|
+
import { get, arraySize } from './array.js';
|
3
|
+
import { typeOf as _typeOf } from './is.js';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Simplifies a callback function by reducing its complexity and potentially improving its performance.
|
7
|
+
*
|
8
|
+
* @param {Function} callback The original callback function to simplify.
|
9
|
+
* @param {Array|Matrix} array The array that will be used with the callback function.
|
10
|
+
* @param {string} name The name of the function that is using the callback.
|
11
|
+
* @returns {Function} Returns a simplified version of the callback function.
|
12
|
+
*/
|
13
|
+
export function optimizeCallback(callback, array, name) {
|
14
|
+
if (typed.isTypedFunction(callback)) {
|
15
|
+
var firstIndex = (array.isMatrix ? array.size() : arraySize(array)).map(() => 0);
|
16
|
+
var firstValue = array.isMatrix ? array.get(firstIndex) : get(array, firstIndex);
|
17
|
+
var hasSingleSignature = Object.keys(callback.signatures).length === 1;
|
18
|
+
var numberOfArguments = _findNumberOfArguments(callback, firstValue, firstIndex, array);
|
19
|
+
var fastCallback = hasSingleSignature ? Object.values(callback.signatures)[0] : callback;
|
20
|
+
if (numberOfArguments >= 1 && numberOfArguments <= 3) {
|
21
|
+
return function () {
|
22
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
23
|
+
args[_key] = arguments[_key];
|
24
|
+
}
|
25
|
+
return _tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name);
|
26
|
+
};
|
27
|
+
}
|
28
|
+
return function () {
|
29
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
30
|
+
args[_key2] = arguments[_key2];
|
31
|
+
}
|
32
|
+
return _tryFunctionWithArgs(fastCallback, args, name, callback.name);
|
33
|
+
};
|
34
|
+
}
|
35
|
+
return callback;
|
36
|
+
}
|
37
|
+
function _findNumberOfArguments(callback, value, index, array) {
|
38
|
+
var testArgs = [value, index, array];
|
39
|
+
for (var i = 3; i > 0; i--) {
|
40
|
+
var args = testArgs.slice(0, i);
|
41
|
+
if (typed.resolve(callback, args) !== null) {
|
42
|
+
return i;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* @param {function} func The selected function taken from one of the signatures of the callback function
|
49
|
+
* @param {Array} args List with arguments to apply to the selected signature
|
50
|
+
* @param {string} mappingFnName the name of the function that is using the callback
|
51
|
+
* @param {string} callbackName the name of the callback function
|
52
|
+
* @returns {*} Returns the return value of the invoked signature
|
53
|
+
* @throws {TypeError} Throws an error when no matching signature was found
|
54
|
+
*/
|
55
|
+
function _tryFunctionWithArgs(func, args, mappingFnName, callbackName) {
|
56
|
+
try {
|
57
|
+
return func(...args);
|
58
|
+
} catch (err) {
|
59
|
+
_createCallbackError(err, args, mappingFnName, callbackName);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
/**
|
64
|
+
* Creates and throws a detailed TypeError when a callback function fails.
|
65
|
+
*
|
66
|
+
* @param {Error} err The original error thrown by the callback function.
|
67
|
+
* @param {Array} args The arguments that were passed to the callback function.
|
68
|
+
* @param {string} mappingFnName The name of the function that is using the callback.
|
69
|
+
* @param {string} callbackName The name of the callback function.
|
70
|
+
* @throws {TypeError} Throws a detailed TypeError with enriched error message.
|
71
|
+
*/
|
72
|
+
function _createCallbackError(err, args, mappingFnName, callbackName) {
|
73
|
+
var _err$data;
|
74
|
+
// Enrich the error message so the user understands that it took place inside the callback function
|
75
|
+
if (err instanceof TypeError && ((_err$data = err.data) === null || _err$data === void 0 ? void 0 : _err$data.category) === 'wrongType') {
|
76
|
+
var argsDesc = [];
|
77
|
+
argsDesc.push("value: ".concat(_typeOf(args[0])));
|
78
|
+
if (args.length >= 2) {
|
79
|
+
argsDesc.push("index: ".concat(_typeOf(args[1])));
|
80
|
+
}
|
81
|
+
if (args.length >= 3) {
|
82
|
+
argsDesc.push("array: ".concat(_typeOf(args[2])));
|
83
|
+
}
|
84
|
+
throw new TypeError("Function ".concat(mappingFnName, " cannot apply callback arguments ") + "".concat(callbackName, "(").concat(argsDesc.join(', '), ") at index ").concat(JSON.stringify(args[1])));
|
85
|
+
} else {
|
86
|
+
throw new TypeError("Function ".concat(mappingFnName, " cannot apply callback arguments ") + "to function ".concat(callbackName, ": ").concat(err.message));
|
87
|
+
}
|
88
|
+
}
|
package/lib/esm/version.js
CHANGED