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,101 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.createTransformCallback = void 0;
|
7
|
+
var _factory = require("../../../utils/factory.js");
|
8
|
+
const name = 'transformCallback';
|
9
|
+
const dependencies = ['typed'];
|
10
|
+
const createTransformCallback = exports.createTransformCallback = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
11
|
+
let {
|
12
|
+
typed
|
13
|
+
} = _ref;
|
14
|
+
/**
|
15
|
+
* Transforms the given callback function based on its type and number of arrays.
|
16
|
+
*
|
17
|
+
* @param {Function} callback - The callback function to transform.
|
18
|
+
* @param {number} numberOfArrays - The number of arrays to pass to the callback function.
|
19
|
+
* @returns {*} - The transformed callback function.
|
20
|
+
*/
|
21
|
+
return function (callback, numberOfArrays) {
|
22
|
+
if (typed.isTypedFunction(callback)) {
|
23
|
+
return _transformTypedCallbackFunction(callback, numberOfArrays);
|
24
|
+
} else {
|
25
|
+
return _transformCallbackFunction(callback, callback.length, numberOfArrays);
|
26
|
+
}
|
27
|
+
};
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Transforms the given typed callback function based on the number of arrays.
|
31
|
+
*
|
32
|
+
* @param {Function} typedFunction - The typed callback function to transform.
|
33
|
+
* @param {number} numberOfArrays - The number of arrays to pass to the callback function.
|
34
|
+
* @returns {*} - The transformed callback function.
|
35
|
+
*/
|
36
|
+
function _transformTypedCallbackFunction(typedFunction, numberOfArrays) {
|
37
|
+
const signatures = Object.fromEntries(Object.entries(typedFunction.signatures).map(_ref2 => {
|
38
|
+
let [signature, callbackFunction] = _ref2;
|
39
|
+
const numberOfCallbackInputs = signature.split(',').length;
|
40
|
+
if (typed.isTypedFunction(callbackFunction)) {
|
41
|
+
return [signature, _transformTypedCallbackFunction(callbackFunction, numberOfArrays)];
|
42
|
+
} else {
|
43
|
+
return [signature, _transformCallbackFunction(callbackFunction, numberOfCallbackInputs, numberOfArrays)];
|
44
|
+
}
|
45
|
+
}));
|
46
|
+
if (typeof typedFunction.name === 'string') {
|
47
|
+
return typed(typedFunction.name, signatures);
|
48
|
+
} else {
|
49
|
+
return typed(signatures);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
});
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Transforms the callback function based on the number of callback inputs and arrays.
|
56
|
+
* There are three cases:
|
57
|
+
* 1. The callback function has N arguments.
|
58
|
+
* 2. The callback function has N+1 arguments.
|
59
|
+
* 3. The callback function has 2N+1 arguments.
|
60
|
+
*
|
61
|
+
* @param {Function} callbackFunction - The callback function to transform.
|
62
|
+
* @param {number} numberOfCallbackInputs - The number of callback inputs.
|
63
|
+
* @param {number} numberOfArrays - The number of arrays.
|
64
|
+
* @returns {Function} The transformed callback function.
|
65
|
+
*/
|
66
|
+
function _transformCallbackFunction(callbackFunction, numberOfCallbackInputs, numberOfArrays) {
|
67
|
+
if (numberOfCallbackInputs === numberOfArrays) {
|
68
|
+
return callbackFunction;
|
69
|
+
} else if (numberOfCallbackInputs === numberOfArrays + 1) {
|
70
|
+
return function () {
|
71
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
72
|
+
args[_key] = arguments[_key];
|
73
|
+
}
|
74
|
+
const vals = args.slice(0, numberOfArrays);
|
75
|
+
const idx = _transformDims(args[numberOfArrays]);
|
76
|
+
return callbackFunction(...vals, idx);
|
77
|
+
};
|
78
|
+
} else if (numberOfCallbackInputs > numberOfArrays + 1) {
|
79
|
+
return function () {
|
80
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
81
|
+
args[_key2] = arguments[_key2];
|
82
|
+
}
|
83
|
+
const vals = args.slice(0, numberOfArrays);
|
84
|
+
const idx = _transformDims(args[numberOfArrays]);
|
85
|
+
const rest = args.slice(numberOfArrays + 1);
|
86
|
+
return callbackFunction(...vals, idx, ...rest);
|
87
|
+
};
|
88
|
+
} else {
|
89
|
+
return callbackFunction;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Transforms the dimensions by adding 1 to each dimension.
|
95
|
+
*
|
96
|
+
* @param {Array} dims - The dimensions to transform.
|
97
|
+
* @returns {Array} The transformed dimensions.
|
98
|
+
*/
|
99
|
+
function _transformDims(dims) {
|
100
|
+
return dims.map(dim => dim + 1);
|
101
|
+
}
|
@@ -71,14 +71,18 @@ const createDerivative = exports.createDerivative = /* #__PURE__ */(0, _factory.
|
|
71
71
|
const res = _derivative(expr, constNodes);
|
72
72
|
return options.simplify ? simplify(res) : res;
|
73
73
|
}
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
74
|
+
function parseIdentifier(string) {
|
75
|
+
const symbol = parse(string);
|
76
|
+
if (!symbol.isSymbolNode) {
|
77
|
+
throw new TypeError('Invalid variable. ' + `Cannot parse ${JSON.stringify(string)} into a variable in function derivative`);
|
78
|
+
}
|
79
|
+
return symbol;
|
80
|
+
}
|
79
81
|
const derivative = typed(name, {
|
80
82
|
'Node, SymbolNode': plainDerivative,
|
81
|
-
'Node, SymbolNode, Object': plainDerivative
|
83
|
+
'Node, SymbolNode, Object': plainDerivative,
|
84
|
+
'Node, string': (node, symbol) => plainDerivative(node, parseIdentifier(symbol)),
|
85
|
+
'Node, string, Object': (node, symbol, options) => plainDerivative(node, parseIdentifier(symbol), options)
|
82
86
|
|
83
87
|
/* TODO: implement and test syntax with order of derivatives -> implement as an option {order: number}
|
84
88
|
'Node, SymbolNode, ConstantNode': function (expr, variable, {order}) {
|
@@ -92,11 +96,6 @@ const createDerivative = exports.createDerivative = /* #__PURE__ */(0, _factory.
|
|
92
96
|
}
|
93
97
|
*/
|
94
98
|
});
|
95
|
-
typed.removeConversion({
|
96
|
-
from: 'identifier',
|
97
|
-
to: 'SymbolNode',
|
98
|
-
convert: parse
|
99
|
-
});
|
100
99
|
derivative._simplify = true;
|
101
100
|
derivative.toTex = function (deriv) {
|
102
101
|
return _derivTex.apply(null, deriv.args);
|
@@ -11,26 +11,15 @@ var _util = require("./simplify/util.js");
|
|
11
11
|
var _object = require("../../utils/object.js");
|
12
12
|
var _map = require("../../utils/map.js");
|
13
13
|
const name = 'simplify';
|
14
|
-
const dependencies = ['
|
14
|
+
const dependencies = ['typed', 'parse', 'equal', 'resolve', 'simplifyConstant', 'simplifyCore', 'AccessorNode', 'ArrayNode', 'ConstantNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'ParenthesisNode', 'SymbolNode'];
|
15
15
|
const createSimplify = exports.createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
16
16
|
let {
|
17
|
-
config,
|
18
17
|
typed,
|
19
18
|
parse,
|
20
|
-
add,
|
21
|
-
subtract,
|
22
|
-
multiply,
|
23
|
-
divide,
|
24
|
-
pow,
|
25
|
-
isZero,
|
26
19
|
equal,
|
27
20
|
resolve,
|
28
21
|
simplifyConstant,
|
29
22
|
simplifyCore,
|
30
|
-
fraction,
|
31
|
-
bignumber,
|
32
|
-
mathWithTransform,
|
33
|
-
matrix,
|
34
23
|
AccessorNode,
|
35
24
|
ArrayNode,
|
36
25
|
ConstantNode,
|
@@ -195,7 +184,7 @@ const createSimplify = exports.createSimplify = /* #__PURE__ */(0, _factory.fact
|
|
195
184
|
simplify.realContext = realContext;
|
196
185
|
simplify.positiveContext = positiveContext;
|
197
186
|
function removeParens(node) {
|
198
|
-
return node.transform(function (node
|
187
|
+
return node.transform(function (node) {
|
199
188
|
return (0, _is.isParenthesisNode)(node) ? removeParens(node.content) : node;
|
200
189
|
});
|
201
190
|
}
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.createFilter = void 0;
|
7
|
-
var
|
7
|
+
var _optimizeCallback = require("../../utils/optimizeCallback.js");
|
8
8
|
var _array = require("../../utils/array.js");
|
9
9
|
var _factory = require("../../utils/factory.js");
|
10
10
|
const name = 'filter';
|
@@ -62,8 +62,9 @@ const createFilter = exports.createFilter = /* #__PURE__ */(0, _factory.factory)
|
|
62
62
|
* @private
|
63
63
|
*/
|
64
64
|
function _filterCallback(x, callback) {
|
65
|
+
const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, x, 'filter');
|
65
66
|
return (0, _array.filter)(x, function (value, index, array) {
|
66
67
|
// invoke the callback function with the right number of arguments
|
67
|
-
return (
|
68
|
+
return fastCallback(value, [index], array);
|
68
69
|
});
|
69
70
|
}
|
@@ -4,9 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.createForEach = void 0;
|
7
|
-
var
|
8
|
-
var _array = require("../../utils/array.js");
|
7
|
+
var _optimizeCallback = require("../../utils/optimizeCallback.js");
|
9
8
|
var _factory = require("../../utils/factory.js");
|
9
|
+
var _array = require("../../utils/array.js");
|
10
10
|
const name = 'forEach';
|
11
11
|
const dependencies = ['typed'];
|
12
12
|
const createForEach = exports.createForEach = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
@@ -51,16 +51,5 @@ const createForEach = exports.createForEach = /* #__PURE__ */(0, _factory.factor
|
|
51
51
|
* @private
|
52
52
|
*/
|
53
53
|
function _forEach(array, callback) {
|
54
|
-
|
55
|
-
if (Array.isArray(value)) {
|
56
|
-
(0, _array.forEach)(value, function (child, i) {
|
57
|
-
// we create a copy of the index array and append the new index value
|
58
|
-
recurse(child, index.concat(i));
|
59
|
-
});
|
60
|
-
} else {
|
61
|
-
// invoke the callback function with the right number of arguments
|
62
|
-
return (0, _applyCallback.applyCallback)(callback, value, index, array, 'forEach');
|
63
|
-
}
|
64
|
-
};
|
65
|
-
recurse(array, []);
|
54
|
+
(0, _array.recurse)(array, [], array, (0, _optimizeCallback.optimizeCallback)(callback, array, name));
|
66
55
|
}
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.createMap = void 0;
|
7
|
-
var
|
7
|
+
var _optimizeCallback = require("../../utils/optimizeCallback.js");
|
8
8
|
var _array = require("../../utils/array.js");
|
9
9
|
var _factory = require("../../utils/factory.js");
|
10
10
|
const name = 'map';
|
@@ -135,36 +135,14 @@ const createMap = exports.createMap = /* #__PURE__ */(0, _factory.factory)(name,
|
|
135
135
|
return 0;
|
136
136
|
}
|
137
137
|
}
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
function _mapArray(array, callback) {
|
148
|
-
return _recurse(array, [], array, callback);
|
149
|
-
}
|
150
|
-
|
151
|
-
/**
|
152
|
-
* Recursive function to map a multi-dimensional array.
|
153
|
-
*
|
154
|
-
* @param {*} value - The current value being processed in the array.
|
155
|
-
* @param {Array} index - The index of the current value being processed in the array.
|
156
|
-
* @param {Array} array - The array being processed.
|
157
|
-
* @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.
|
158
|
-
* @returns {*} The new array with each element being the result of the callback function.
|
159
|
-
*/
|
160
|
-
function _recurse(value, index, array, callback) {
|
161
|
-
if (Array.isArray(value)) {
|
162
|
-
return value.map(function (child, i) {
|
163
|
-
// we create a copy of the index array and append the new index value
|
164
|
-
return _recurse(child, index.concat(i), array, callback);
|
165
|
-
});
|
166
|
-
} else {
|
167
|
-
// invoke the callback function with the right number of arguments
|
168
|
-
return (0, _applyCallback.applyCallback)(callback, value, index, array, 'map');
|
138
|
+
/**
|
139
|
+
* Map for a multi dimensional array
|
140
|
+
* @param {Array} array
|
141
|
+
* @param {Function} callback
|
142
|
+
* @return {Array}
|
143
|
+
* @private
|
144
|
+
*/
|
145
|
+
function _mapArray(array, callback) {
|
146
|
+
return (0, _array.recurse)(array, [], array, (0, _optimizeCallback.optimizeCallback)(callback, array, name));
|
169
147
|
}
|
170
|
-
}
|
148
|
+
});
|
package/lib/cjs/header.js
CHANGED
@@ -6,8 +6,8 @@
|
|
6
6
|
* It features real and complex numbers, units, matrices, a large set of
|
7
7
|
* mathematical functions, and a flexible expression parser.
|
8
8
|
*
|
9
|
-
* @version 13.
|
10
|
-
* @date 2024-
|
9
|
+
* @version 13.2.1
|
10
|
+
* @date 2024-11-06
|
11
11
|
*
|
12
12
|
* @license
|
13
13
|
* Copyright (C) 2013-2024 Jos de Jong <wjosdejong@gmail.com>
|
@@ -11,7 +11,9 @@ var _number = require("../../utils/number.js");
|
|
11
11
|
var _object = require("../../utils/object.js");
|
12
12
|
var _DimensionError = require("../../error/DimensionError.js");
|
13
13
|
var _factory = require("../../utils/factory.js");
|
14
|
-
var
|
14
|
+
var _optimizeCallback = require("../../utils/optimizeCallback.js");
|
15
|
+
// deno-lint-ignore-file no-this-alias
|
16
|
+
|
15
17
|
const name = 'DenseMatrix';
|
16
18
|
const dependencies = ['Matrix'];
|
17
19
|
const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
@@ -522,6 +524,66 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
|
|
522
524
|
return this._size.slice(0); // return a clone of _size
|
523
525
|
};
|
524
526
|
|
527
|
+
/**
|
528
|
+
* Applies a callback function to a reference to each element of the matrix
|
529
|
+
* @memberof DenseMatrix
|
530
|
+
* @param {Function} callback The callback function is invoked with three
|
531
|
+
* parameters: an array, an integer index to that
|
532
|
+
* array, and the Matrix being traversed.
|
533
|
+
*/
|
534
|
+
DenseMatrix.prototype._forEach = function (callback) {
|
535
|
+
// matrix instance
|
536
|
+
const me = this;
|
537
|
+
const s = me.size();
|
538
|
+
|
539
|
+
// if there is only one dimension, just loop through it
|
540
|
+
if (s.length === 1) {
|
541
|
+
for (let i = 0; i < s[0]; i++) {
|
542
|
+
callback(me._data, i, [i]);
|
543
|
+
}
|
544
|
+
return;
|
545
|
+
}
|
546
|
+
|
547
|
+
// keep track of the current index permutation
|
548
|
+
const index = Array(s.length).fill(0);
|
549
|
+
|
550
|
+
// store a reference of each dimension of the matrix for faster access
|
551
|
+
const data = Array(s.length - 1);
|
552
|
+
const last = data.length - 1;
|
553
|
+
data[0] = me._data[0];
|
554
|
+
for (let i = 0; i < last; i++) {
|
555
|
+
data[i + 1] = data[i][0];
|
556
|
+
}
|
557
|
+
index[last] = -1;
|
558
|
+
while (true) {
|
559
|
+
let i;
|
560
|
+
for (i = last; i >= 0; i--) {
|
561
|
+
// march index to the next permutation
|
562
|
+
index[i]++;
|
563
|
+
if (index[i] === s[i]) {
|
564
|
+
index[i] = 0;
|
565
|
+
continue;
|
566
|
+
}
|
567
|
+
|
568
|
+
// update references to matrix dimensions
|
569
|
+
data[i] = i === 0 ? me._data[index[i]] : data[i - 1][index[i]];
|
570
|
+
for (let j = i; j < last; j++) {
|
571
|
+
data[j + 1] = data[j][0];
|
572
|
+
}
|
573
|
+
|
574
|
+
// loop through the last dimension and map each value
|
575
|
+
for (let j = 0; j < s[data.length]; j++) {
|
576
|
+
index[data.length] = j;
|
577
|
+
callback(data[last], j, index.slice(0));
|
578
|
+
}
|
579
|
+
break;
|
580
|
+
}
|
581
|
+
if (i === -1) {
|
582
|
+
break;
|
583
|
+
}
|
584
|
+
}
|
585
|
+
};
|
586
|
+
|
525
587
|
/**
|
526
588
|
* Create a new matrix with the results of the callback function executed on
|
527
589
|
* each entry of the matrix.
|
@@ -533,24 +595,13 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
|
|
533
595
|
* @return {DenseMatrix} matrix
|
534
596
|
*/
|
535
597
|
DenseMatrix.prototype.map = function (callback) {
|
536
|
-
// matrix instance
|
537
598
|
const me = this;
|
538
|
-
const
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
// invoke the callback function with the right number of arguments
|
545
|
-
return (0, _applyCallback.applyCallback)(callback, value, index, me, 'map');
|
546
|
-
}
|
547
|
-
};
|
548
|
-
|
549
|
-
// determine the new datatype when the original matrix has datatype defined
|
550
|
-
// TODO: should be done in matrix constructor instead
|
551
|
-
const data = recurse(this._data, []);
|
552
|
-
const datatype = this._datatype !== undefined ? (0, _array.getArrayDataType)(data, _is.typeOf) : undefined;
|
553
|
-
return new DenseMatrix(data, datatype);
|
599
|
+
const result = new DenseMatrix(me);
|
600
|
+
const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me._data, 'map');
|
601
|
+
result._forEach(function (arr, i, index) {
|
602
|
+
arr[i] = fastCallback(arr[i], index, me);
|
603
|
+
});
|
604
|
+
return result;
|
554
605
|
};
|
555
606
|
|
556
607
|
/**
|
@@ -561,18 +612,11 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
|
|
561
612
|
* of the element, and the Matrix being traversed.
|
562
613
|
*/
|
563
614
|
DenseMatrix.prototype.forEach = function (callback) {
|
564
|
-
// matrix instance
|
565
615
|
const me = this;
|
566
|
-
const
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
});
|
571
|
-
} else {
|
572
|
-
callback(value, index, me);
|
573
|
-
}
|
574
|
-
};
|
575
|
-
recurse(this._data, []);
|
616
|
+
const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me._data, 'map');
|
617
|
+
me._forEach(function (arr, i, index) {
|
618
|
+
fastCallback(arr[i], index, me);
|
619
|
+
});
|
576
620
|
};
|
577
621
|
|
578
622
|
/**
|
@@ -186,6 +186,9 @@ const createIndexClass = exports.createIndexClass = /* #__PURE__ */(0, _factory.
|
|
186
186
|
* @returns {Range | null} range
|
187
187
|
*/
|
188
188
|
Index.prototype.dimension = function (dim) {
|
189
|
+
if (typeof dim !== 'number') {
|
190
|
+
return null;
|
191
|
+
}
|
189
192
|
return this._dimensions[dim] || null;
|
190
193
|
};
|
191
194
|
|
@@ -11,7 +11,7 @@ var _object = require("../../utils/object.js");
|
|
11
11
|
var _array = require("../../utils/array.js");
|
12
12
|
var _factory = require("../../utils/factory.js");
|
13
13
|
var _DimensionError = require("../../error/DimensionError.js");
|
14
|
-
var
|
14
|
+
var _optimizeCallback = require("../../utils/optimizeCallback.js");
|
15
15
|
const name = 'SparseMatrix';
|
16
16
|
const dependencies = ['typed', 'equalScalar', 'Matrix'];
|
17
17
|
const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
@@ -878,10 +878,11 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
|
|
878
878
|
// rows and columns
|
879
879
|
const rows = this._size[0];
|
880
880
|
const columns = this._size[1];
|
881
|
+
const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me, 'map');
|
881
882
|
// invoke callback
|
882
883
|
const invoke = function (v, i, j) {
|
883
884
|
// invoke callback
|
884
|
-
return (
|
885
|
+
return fastCallback(v, [i, j], me);
|
885
886
|
};
|
886
887
|
// invoke _map
|
887
888
|
return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros);
|
@@ -986,6 +987,7 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
|
|
986
987
|
// rows and columns
|
987
988
|
const rows = this._size[0];
|
988
989
|
const columns = this._size[1];
|
990
|
+
const fastCallback = (0, _optimizeCallback.optimizeCallback)(callback, me, 'forEach');
|
989
991
|
// loop columns
|
990
992
|
for (let j = 0; j < columns; j++) {
|
991
993
|
// k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1]
|
@@ -998,7 +1000,7 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
|
|
998
1000
|
const i = this._index[k];
|
999
1001
|
|
1000
1002
|
// value @ k
|
1001
|
-
|
1003
|
+
fastCallback(this._values[k], [i, j], me);
|
1002
1004
|
}
|
1003
1005
|
} else {
|
1004
1006
|
// create a cache holding all defined values
|
@@ -1012,7 +1014,7 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
|
|
1012
1014
|
// and either read the value or zero
|
1013
1015
|
for (let i = 0; i < rows; i++) {
|
1014
1016
|
const value = i in values ? values[i] : 0;
|
1015
|
-
|
1017
|
+
fastCallback(value, [i, j], me);
|
1016
1018
|
}
|
1017
1019
|
}
|
1018
1020
|
}
|
@@ -1411,6 +1413,7 @@ const createSparseMatrixClass = exports.createSparseMatrixClass = /* #__PURE__ *
|
|
1411
1413
|
// indeces for column j
|
1412
1414
|
const k0 = ptr[j];
|
1413
1415
|
const k1 = ptr[j + 1];
|
1416
|
+
|
1414
1417
|
// loop
|
1415
1418
|
for (let k = k0; k < k1; k++) {
|
1416
1419
|
// invoke callback
|
@@ -42,7 +42,7 @@ const createResultSet = exports.createResultSet = /* #__PURE__ */(0, _factory.fa
|
|
42
42
|
* @returns {string} string
|
43
43
|
*/
|
44
44
|
ResultSet.prototype.toString = function () {
|
45
|
-
return '[' + this.entries.join(', ') + ']';
|
45
|
+
return '[' + this.entries.map(String).join(', ') + ']';
|
46
46
|
};
|
47
47
|
|
48
48
|
/**
|
package/lib/cjs/utils/array.js
CHANGED
@@ -25,6 +25,7 @@ exports.join = join;
|
|
25
25
|
exports.last = last;
|
26
26
|
exports.map = map;
|
27
27
|
exports.processSizesWildcard = processSizesWildcard;
|
28
|
+
exports.recurse = recurse;
|
28
29
|
exports.reshape = reshape;
|
29
30
|
exports.resize = resize;
|
30
31
|
exports.squeeze = squeeze;
|
@@ -840,6 +841,27 @@ function get(array, index) {
|
|
840
841
|
return index.reduce((acc, curr) => acc[curr], array);
|
841
842
|
}
|
842
843
|
|
844
|
+
/**
|
845
|
+
* Recursive function to map a multi-dimensional array.
|
846
|
+
*
|
847
|
+
* @param {*} value - The current value being processed in the array.
|
848
|
+
* @param {Array} index - The index of the current value being processed in the array.
|
849
|
+
* @param {Array} array - The array being processed.
|
850
|
+
* @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.
|
851
|
+
* @returns {*} The new array with each element being the result of the callback function.
|
852
|
+
*/
|
853
|
+
function recurse(value, index, array, callback) {
|
854
|
+
if (Array.isArray(value)) {
|
855
|
+
return value.map(function (child, i) {
|
856
|
+
// we create a copy of the index array and append the new index value
|
857
|
+
return recurse(child, index.concat(i), array, callback);
|
858
|
+
});
|
859
|
+
} else {
|
860
|
+
// invoke the callback function with the right number of arguments
|
861
|
+
return callback(value, index, array);
|
862
|
+
}
|
863
|
+
}
|
864
|
+
|
843
865
|
/**
|
844
866
|
* Deep clones a multidimensional array
|
845
867
|
* @param {Array} array
|
@@ -0,0 +1,94 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
5
|
+
value: true
|
6
|
+
});
|
7
|
+
exports.optimizeCallback = optimizeCallback;
|
8
|
+
var _typedFunction = _interopRequireDefault(require("typed-function"));
|
9
|
+
var _array = require("./array.js");
|
10
|
+
var _is = require("./is.js");
|
11
|
+
/**
|
12
|
+
* Simplifies a callback function by reducing its complexity and potentially improving its performance.
|
13
|
+
*
|
14
|
+
* @param {Function} callback The original callback function to simplify.
|
15
|
+
* @param {Array|Matrix} array The array that will be used with the callback function.
|
16
|
+
* @param {string} name The name of the function that is using the callback.
|
17
|
+
* @returns {Function} Returns a simplified version of the callback function.
|
18
|
+
*/
|
19
|
+
function optimizeCallback(callback, array, name) {
|
20
|
+
if (_typedFunction.default.isTypedFunction(callback)) {
|
21
|
+
const firstIndex = (array.isMatrix ? array.size() : (0, _array.arraySize)(array)).map(() => 0);
|
22
|
+
const firstValue = array.isMatrix ? array.get(firstIndex) : (0, _array.get)(array, firstIndex);
|
23
|
+
const hasSingleSignature = Object.keys(callback.signatures).length === 1;
|
24
|
+
const numberOfArguments = _findNumberOfArguments(callback, firstValue, firstIndex, array);
|
25
|
+
const fastCallback = hasSingleSignature ? Object.values(callback.signatures)[0] : callback;
|
26
|
+
if (numberOfArguments >= 1 && numberOfArguments <= 3) {
|
27
|
+
return function () {
|
28
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
29
|
+
args[_key] = arguments[_key];
|
30
|
+
}
|
31
|
+
return _tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name);
|
32
|
+
};
|
33
|
+
}
|
34
|
+
return function () {
|
35
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
36
|
+
args[_key2] = arguments[_key2];
|
37
|
+
}
|
38
|
+
return _tryFunctionWithArgs(fastCallback, args, name, callback.name);
|
39
|
+
};
|
40
|
+
}
|
41
|
+
return callback;
|
42
|
+
}
|
43
|
+
function _findNumberOfArguments(callback, value, index, array) {
|
44
|
+
const testArgs = [value, index, array];
|
45
|
+
for (let i = 3; i > 0; i--) {
|
46
|
+
const args = testArgs.slice(0, i);
|
47
|
+
if (_typedFunction.default.resolve(callback, args) !== null) {
|
48
|
+
return i;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
* @param {function} func The selected function taken from one of the signatures of the callback function
|
55
|
+
* @param {Array} args List with arguments to apply to the selected signature
|
56
|
+
* @param {string} mappingFnName the name of the function that is using the callback
|
57
|
+
* @param {string} callbackName the name of the callback function
|
58
|
+
* @returns {*} Returns the return value of the invoked signature
|
59
|
+
* @throws {TypeError} Throws an error when no matching signature was found
|
60
|
+
*/
|
61
|
+
function _tryFunctionWithArgs(func, args, mappingFnName, callbackName) {
|
62
|
+
try {
|
63
|
+
return func(...args);
|
64
|
+
} catch (err) {
|
65
|
+
_createCallbackError(err, args, mappingFnName, callbackName);
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Creates and throws a detailed TypeError when a callback function fails.
|
71
|
+
*
|
72
|
+
* @param {Error} err The original error thrown by the callback function.
|
73
|
+
* @param {Array} args The arguments that were passed to the callback function.
|
74
|
+
* @param {string} mappingFnName The name of the function that is using the callback.
|
75
|
+
* @param {string} callbackName The name of the callback function.
|
76
|
+
* @throws {TypeError} Throws a detailed TypeError with enriched error message.
|
77
|
+
*/
|
78
|
+
function _createCallbackError(err, args, mappingFnName, callbackName) {
|
79
|
+
var _err$data;
|
80
|
+
// Enrich the error message so the user understands that it took place inside the callback function
|
81
|
+
if (err instanceof TypeError && ((_err$data = err.data) === null || _err$data === void 0 ? void 0 : _err$data.category) === 'wrongType') {
|
82
|
+
const argsDesc = [];
|
83
|
+
argsDesc.push(`value: ${(0, _is.typeOf)(args[0])}`);
|
84
|
+
if (args.length >= 2) {
|
85
|
+
argsDesc.push(`index: ${(0, _is.typeOf)(args[1])}`);
|
86
|
+
}
|
87
|
+
if (args.length >= 3) {
|
88
|
+
argsDesc.push(`array: ${(0, _is.typeOf)(args[2])}`);
|
89
|
+
}
|
90
|
+
throw new TypeError(`Function ${mappingFnName} cannot apply callback arguments ` + `${callbackName}(${argsDesc.join(', ')}) at index ${JSON.stringify(args[1])}`);
|
91
|
+
} else {
|
92
|
+
throw new TypeError(`Function ${mappingFnName} cannot apply callback arguments ` + `to function ${callbackName}: ${err.message}`);
|
93
|
+
}
|
94
|
+
}
|
package/lib/cjs/version.js
CHANGED
@@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.version = void 0;
|
7
|
-
const version = exports.version = '13.
|
7
|
+
const version = exports.version = '13.2.1';
|
8
8
|
// Note: This file is automatically generated when building math.js.
|
9
9
|
// Changes made in this file will be overwritten.
|