mathjs 12.0.0 → 12.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/HISTORY.md +18 -3
- package/lib/browser/math.js +1 -1
- package/lib/browser/math.js.LICENSE.txt +2 -2
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/expression/embeddedDocs/function/arithmetic/round.js +2 -2
- package/lib/cjs/function/algebra/derivative.js +8 -31
- package/lib/cjs/function/arithmetic/gcd.js +4 -5
- package/lib/cjs/function/arithmetic/mod.js +2 -9
- package/lib/cjs/function/arithmetic/round.js +59 -16
- package/lib/cjs/function/probability/pickRandom.js +2 -2
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/type/number.js +2 -2
- package/lib/cjs/utils/snapshot.js +6 -6
- package/lib/cjs/version.js +1 -1
- package/lib/esm/expression/embeddedDocs/function/arithmetic/round.js +2 -2
- package/lib/esm/function/algebra/derivative.js +8 -31
- package/lib/esm/function/arithmetic/mod.js +2 -9
- package/lib/esm/function/arithmetic/round.js +40 -17
- package/lib/esm/function/probability/pickRandom.js +2 -2
- package/lib/esm/type/number.js +2 -2
- package/lib/esm/version.js +1 -1
- package/package.json +13 -13
- package/types/index.d.ts +29 -7
@@ -7,8 +7,8 @@ exports.roundDocs = void 0;
|
|
7
7
|
var roundDocs = exports.roundDocs = {
|
8
8
|
name: 'round',
|
9
9
|
category: 'Arithmetic',
|
10
|
-
syntax: ['round(x)', 'round(x, n)'],
|
10
|
+
syntax: ['round(x)', 'round(x, n)', 'round(unit, valuelessUnit)', 'round(unit, n, valuelessUnit)'],
|
11
11
|
description: 'round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.',
|
12
|
-
examples: ['round(3.2)', 'round(3.8)', 'round(-4.2)', 'round(-4.8)', 'round(pi, 3)', 'round(123.45678, 2)'],
|
12
|
+
examples: ['round(3.2)', 'round(3.8)', 'round(-4.2)', 'round(-4.8)', 'round(pi, 3)', 'round(123.45678, 2)', 'round(3.241cm, 2, cm)', 'round([3.2, 3.8, -4.7])'],
|
13
13
|
seealso: ['ceil', 'floor', 'fix']
|
14
14
|
};
|
@@ -212,9 +212,6 @@ var createDerivative = exports.createDerivative = /* #__PURE__ */(0, _factory.fa
|
|
212
212
|
return _derivative(node.expr, constNodes);
|
213
213
|
},
|
214
214
|
'FunctionNode, Object': function FunctionNodeObject(node, constNodes) {
|
215
|
-
if (node.args.length !== 1) {
|
216
|
-
funcArgsCheck(node);
|
217
|
-
}
|
218
215
|
if (constNodes[node] !== undefined) {
|
219
216
|
return createConstantNode(0);
|
220
217
|
}
|
@@ -263,9 +260,12 @@ var createDerivative = exports.createDerivative = /* #__PURE__ */(0, _factory.fa
|
|
263
260
|
}
|
264
261
|
break;
|
265
262
|
case 'pow':
|
266
|
-
|
267
|
-
|
268
|
-
|
263
|
+
if (node.args.length === 2) {
|
264
|
+
constNodes[arg1] = constNodes[node.args[1]];
|
265
|
+
// Pass to pow operator node parser
|
266
|
+
return _derivative(new OperatorNode('^', 'pow', [arg0, node.args[1]]), constNodes);
|
267
|
+
}
|
268
|
+
break;
|
269
269
|
case 'exp':
|
270
270
|
// d/dx(e^x) = e^x
|
271
271
|
funcDerivative = new FunctionNode('exp', [arg0.clone()]);
|
@@ -395,7 +395,7 @@ var createDerivative = exports.createDerivative = /* #__PURE__ */(0, _factory.fa
|
|
395
395
|
break;
|
396
396
|
case 'gamma': // Needs digamma function, d/dx(gamma(x)) = gamma(x)digamma(x)
|
397
397
|
default:
|
398
|
-
throw new Error('
|
398
|
+
throw new Error('Cannot process function "' + node.name + '" in derivative: ' + 'the function is not supported, undefined, or the number of arguments passed to it are not supported');
|
399
399
|
}
|
400
400
|
var op, func;
|
401
401
|
if (div) {
|
@@ -506,33 +506,10 @@ var createDerivative = exports.createDerivative = /* #__PURE__ */(0, _factory.fa
|
|
506
506
|
// Functional Power Rule, d/dx(f^g) = f^g*[f'*(g/f) + g'ln(f)]
|
507
507
|
return new OperatorNode('*', 'multiply', [new OperatorNode('^', 'pow', [_arg.clone(), _arg2.clone()]), new OperatorNode('+', 'add', [new OperatorNode('*', 'multiply', [_derivative(_arg, constNodes), new OperatorNode('/', 'divide', [_arg2.clone(), _arg.clone()])]), new OperatorNode('*', 'multiply', [_derivative(_arg2, constNodes), new FunctionNode('log', [_arg.clone()])])])]);
|
508
508
|
}
|
509
|
-
throw new Error('
|
509
|
+
throw new Error('Cannot process operator "' + node.op + '" in derivative: ' + 'the operator is not supported, undefined, or the number of arguments passed to it are not supported');
|
510
510
|
}
|
511
511
|
});
|
512
512
|
|
513
|
-
/**
|
514
|
-
* Ensures the number of arguments for a function are correct,
|
515
|
-
* and will throw an error otherwise.
|
516
|
-
*
|
517
|
-
* @param {FunctionNode} node
|
518
|
-
*/
|
519
|
-
function funcArgsCheck(node) {
|
520
|
-
// TODO add min, max etc
|
521
|
-
if ((node.name === 'log' || node.name === 'nthRoot' || node.name === 'pow') && node.args.length === 2) {
|
522
|
-
return;
|
523
|
-
}
|
524
|
-
|
525
|
-
// There should be an incorrect number of arguments if we reach here
|
526
|
-
|
527
|
-
// Change all args to constants to avoid unidentified
|
528
|
-
// symbol error when compiling function
|
529
|
-
for (var i = 0; i < node.args.length; ++i) {
|
530
|
-
node.args[i] = createConstantNode(0);
|
531
|
-
}
|
532
|
-
node.compile().evaluate();
|
533
|
-
throw new Error('Function "' + node.name + '" is not supported by derivative, or a wrong number of arguments is passed');
|
534
|
-
}
|
535
|
-
|
536
513
|
/**
|
537
514
|
* Helper function to create a constant node with a specific type
|
538
515
|
* (number, BigNumber, Fraction)
|
@@ -25,7 +25,6 @@ function is1d(array) {
|
|
25
25
|
});
|
26
26
|
}
|
27
27
|
var createGcd = exports.createGcd = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
|
28
|
-
var _typed;
|
29
28
|
var typed = _ref.typed,
|
30
29
|
matrix = _ref.matrix,
|
31
30
|
config = _ref.config,
|
@@ -97,7 +96,7 @@ var createGcd = exports.createGcd = /* #__PURE__ */(0, _factory.factory)(name, d
|
|
97
96
|
SS: matAlgo04xSidSid,
|
98
97
|
DS: matAlgo01xDSid,
|
99
98
|
Ss: matAlgo10xSids
|
100
|
-
}), (
|
99
|
+
}), (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, gcdManyTypesSignature, typed.referToSelf(function (self) {
|
101
100
|
return function (a, b, args) {
|
102
101
|
var res = self(a, b);
|
103
102
|
for (var i = 0; i < args.length; i++) {
|
@@ -105,7 +104,7 @@ var createGcd = exports.createGcd = /* #__PURE__ */(0, _factory.factory)(name, d
|
|
105
104
|
}
|
106
105
|
return res;
|
107
106
|
};
|
108
|
-
})),
|
107
|
+
})), "Array", typed.referToSelf(function (self) {
|
109
108
|
return function (array) {
|
110
109
|
if (array.length === 1 && Array.isArray(array[0]) && is1d(array[0])) {
|
111
110
|
return self.apply(void 0, (0, _toConsumableArray2["default"])(array[0]));
|
@@ -115,11 +114,11 @@ var createGcd = exports.createGcd = /* #__PURE__ */(0, _factory.factory)(name, d
|
|
115
114
|
}
|
116
115
|
throw new _ArgumentsError.ArgumentsError('gcd() supports only 1d matrices!');
|
117
116
|
};
|
118
|
-
})),
|
117
|
+
})), "Matrix", typed.referToSelf(function (self) {
|
119
118
|
return function (matrix) {
|
120
119
|
return self(matrix.toArray());
|
121
120
|
};
|
122
|
-
}))
|
121
|
+
})));
|
123
122
|
|
124
123
|
/**
|
125
124
|
* Calculate gcd for numbers
|
@@ -95,17 +95,10 @@ var createMod = exports.createMod = /* #__PURE__ */(0, _factory.factory)(name, d
|
|
95
95
|
return typed(name, {
|
96
96
|
'number, number': _modNumber,
|
97
97
|
'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
|
98
|
-
|
99
|
-
throw new Error('Cannot calculate mod for a negative divisor');
|
100
|
-
}
|
101
|
-
return y.isZero() ? x : x.mod(y);
|
98
|
+
return y.isZero() ? x : x.sub(y.mul(floor(x.div(y))));
|
102
99
|
},
|
103
100
|
'Fraction, Fraction': function FractionFraction(x, y) {
|
104
|
-
|
105
|
-
throw new Error('Cannot calculate mod for a negative divisor');
|
106
|
-
}
|
107
|
-
// Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend
|
108
|
-
return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y);
|
101
|
+
return y.equals(0) ? x : x.sub(y.mul(floor(x.div(y))));
|
109
102
|
}
|
110
103
|
}, matrixAlgorithmSuite({
|
111
104
|
SS: matAlgo05xSfSf,
|
@@ -40,6 +40,8 @@ var createRound = exports.createRound = /* #__PURE__ */(0, _factory.factory)(nam
|
|
40
40
|
*
|
41
41
|
* math.round(x)
|
42
42
|
* math.round(x, n)
|
43
|
+
* math.round(unit, valuelessUnit)
|
44
|
+
* math.round(unit, n, valuelessUnit)
|
43
45
|
*
|
44
46
|
* Examples:
|
45
47
|
*
|
@@ -57,14 +59,21 @@ var createRound = exports.createRound = /* #__PURE__ */(0, _factory.factory)(nam
|
|
57
59
|
* const c = math.complex(3.2, -2.7)
|
58
60
|
* math.round(c) // returns Complex 3 - 3i
|
59
61
|
*
|
62
|
+
* const unit = math.unit('3.241 cm')
|
63
|
+
* const cm = math.unit('cm')
|
64
|
+
* const mm = math.unit('mm')
|
65
|
+
* math.round(unit, 1, cm) // returns Unit 3.2 cm
|
66
|
+
* math.round(unit, 1, mm) // returns Unit 32.4 mm
|
67
|
+
*
|
60
68
|
* math.round([3.2, 3.8, -4.7]) // returns Array [3, 4, -5]
|
61
69
|
*
|
62
70
|
* See also:
|
63
71
|
*
|
64
72
|
* ceil, fix, floor
|
65
73
|
*
|
66
|
-
* @param {number | BigNumber | Fraction | Complex | Array | Matrix} x
|
74
|
+
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
|
67
75
|
* @param {number | BigNumber | Array} [n=0] Number of decimals
|
76
|
+
* @param {Unit} [valuelessUnit] A valueless unit
|
68
77
|
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
|
69
78
|
*/
|
70
79
|
return typed(name, {
|
@@ -116,52 +125,86 @@ var createRound = exports.createRound = /* #__PURE__ */(0, _factory.factory)(nam
|
|
116
125
|
}
|
117
126
|
return x.round(n.toNumber());
|
118
127
|
},
|
119
|
-
|
128
|
+
'Unit, number, Unit': typed.referToSelf(function (self) {
|
129
|
+
return function (x, n, unit) {
|
130
|
+
var valueless = x.toNumeric(unit);
|
131
|
+
return unit.multiply(self(valueless, n));
|
132
|
+
};
|
133
|
+
}),
|
134
|
+
'Unit, BigNumber, Unit': typed.referToSelf(function (self) {
|
135
|
+
return function (x, n, unit) {
|
136
|
+
return self(x, n.toNumber(), unit);
|
137
|
+
};
|
138
|
+
}),
|
139
|
+
'Unit, Unit': typed.referToSelf(function (self) {
|
140
|
+
return function (x, unit) {
|
141
|
+
return self(x, 0, unit);
|
142
|
+
};
|
143
|
+
}),
|
144
|
+
'Array | Matrix, number, Unit': typed.referToSelf(function (self) {
|
145
|
+
return function (x, n, unit) {
|
146
|
+
// deep map collection, skip zeros since round(0) = 0
|
147
|
+
return (0, _collection.deepMap)(x, function (value) {
|
148
|
+
return self(value, n, unit);
|
149
|
+
}, true);
|
150
|
+
};
|
151
|
+
}),
|
152
|
+
'Array | Matrix, BigNumber, Unit': typed.referToSelf(function (self) {
|
153
|
+
return function (x, n, unit) {
|
154
|
+
return self(x, n.toNumber(), unit);
|
155
|
+
};
|
156
|
+
}),
|
157
|
+
'Array | Matrix, Unit': typed.referToSelf(function (self) {
|
158
|
+
return function (x, unit) {
|
159
|
+
return self(x, 0, unit);
|
160
|
+
};
|
161
|
+
}),
|
120
162
|
'Array | Matrix': typed.referToSelf(function (self) {
|
121
163
|
return function (x) {
|
164
|
+
// deep map collection, skip zeros since round(0) = 0
|
122
165
|
return (0, _collection.deepMap)(x, self, true);
|
123
166
|
};
|
124
167
|
}),
|
125
168
|
'SparseMatrix, number | BigNumber': typed.referToSelf(function (self) {
|
126
|
-
return function (x,
|
127
|
-
return matAlgo11xS0s(x,
|
169
|
+
return function (x, n) {
|
170
|
+
return matAlgo11xS0s(x, n, self, false);
|
128
171
|
};
|
129
172
|
}),
|
130
173
|
'DenseMatrix, number | BigNumber': typed.referToSelf(function (self) {
|
131
|
-
return function (x,
|
132
|
-
return matAlgo14xDs(x,
|
174
|
+
return function (x, n) {
|
175
|
+
return matAlgo14xDs(x, n, self, false);
|
133
176
|
};
|
134
177
|
}),
|
135
178
|
'Array, number | BigNumber': typed.referToSelf(function (self) {
|
136
|
-
return function (x,
|
179
|
+
return function (x, n) {
|
137
180
|
// use matrix implementation
|
138
|
-
return matAlgo14xDs(matrix(x),
|
181
|
+
return matAlgo14xDs(matrix(x), n, self, false).valueOf();
|
139
182
|
};
|
140
183
|
}),
|
141
184
|
'number | Complex | BigNumber | Fraction, SparseMatrix': typed.referToSelf(function (self) {
|
142
|
-
return function (x,
|
185
|
+
return function (x, n) {
|
143
186
|
// check scalar is zero
|
144
187
|
if (equalScalar(x, 0)) {
|
145
188
|
// do not execute algorithm, result will be a zero matrix
|
146
|
-
return zeros(
|
189
|
+
return zeros(n.size(), n.storage());
|
147
190
|
}
|
148
|
-
return matAlgo12xSfs(
|
191
|
+
return matAlgo12xSfs(n, x, self, true);
|
149
192
|
};
|
150
193
|
}),
|
151
194
|
'number | Complex | BigNumber | Fraction, DenseMatrix': typed.referToSelf(function (self) {
|
152
|
-
return function (x,
|
195
|
+
return function (x, n) {
|
153
196
|
// check scalar is zero
|
154
197
|
if (equalScalar(x, 0)) {
|
155
198
|
// do not execute algorithm, result will be a zero matrix
|
156
|
-
return zeros(
|
199
|
+
return zeros(n.size(), n.storage());
|
157
200
|
}
|
158
|
-
return matAlgo14xDs(
|
201
|
+
return matAlgo14xDs(n, x, self, true);
|
159
202
|
};
|
160
203
|
}),
|
161
204
|
'number | Complex | BigNumber | Fraction, Array': typed.referToSelf(function (self) {
|
162
|
-
return function (x,
|
205
|
+
return function (x, n) {
|
163
206
|
// use matrix implementation
|
164
|
-
return matAlgo14xDs(matrix(
|
207
|
+
return matAlgo14xDs(matrix(n), x, self, true).valueOf();
|
165
208
|
};
|
166
209
|
})
|
167
210
|
});
|
@@ -56,8 +56,8 @@ var createPickRandom = exports.createPickRandom = /* #__PURE__ */(0, _factory.fa
|
|
56
56
|
* @param {Array | Matrix} array A one dimensional array
|
57
57
|
* @param {Int} number An int or float
|
58
58
|
* @param {Array | Matrix} weights An array of ints or floats
|
59
|
-
* @return {number | Array} Returns a single random value from array when number is
|
60
|
-
* Returns an array with the configured number of elements when number is
|
59
|
+
* @return {number | Array} Returns a single random value from array when number is undefined.
|
60
|
+
* Returns an array with the configured number of elements when number is defined.
|
61
61
|
*/
|
62
62
|
return typed(name, {
|
63
63
|
'Array | Matrix': function ArrayMatrix(possibles) {
|
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 12.
|
10
|
-
* @date 2023-
|
9
|
+
* @version 12.1.0
|
10
|
+
* @date 2023-11-17
|
11
11
|
*
|
12
12
|
* @license
|
13
13
|
* Copyright (C) 2013-2023 Jos de Jong <wjosdejong@gmail.com>
|
package/lib/cjs/type/number.js
CHANGED
@@ -49,7 +49,7 @@ function makeNumberFromNonDecimalParts(parts) {
|
|
49
49
|
}
|
50
50
|
var result = n + f;
|
51
51
|
if (isNaN(result)) {
|
52
|
-
throw new SyntaxError('String "' + parts.input + '" is
|
52
|
+
throw new SyntaxError('String "' + parts.input + '" is not a valid number');
|
53
53
|
}
|
54
54
|
return result;
|
55
55
|
}
|
@@ -103,7 +103,7 @@ var createNumber = exports.createNumber = /* #__PURE__ */(0, _factory.factory)(n
|
|
103
103
|
}
|
104
104
|
var num = Number(x);
|
105
105
|
if (isNaN(num)) {
|
106
|
-
throw new SyntaxError('String "' + x + '" is
|
106
|
+
throw new SyntaxError('String "' + x + '" is not a valid number');
|
107
107
|
}
|
108
108
|
if (wordSizeSuffixMatch) {
|
109
109
|
// x is a signed bin, oct, or hex literal
|
@@ -18,12 +18,12 @@ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return
|
|
18
18
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
19
19
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
20
20
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /**
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
* This file contains helper methods to create expected snapshot structures
|
22
|
+
* of both instance and ES6 exports.
|
23
|
+
*
|
24
|
+
* The files are located here and not under /test or /tools so it's transpiled
|
25
|
+
* into ES5 code under /lib and can be used straight by node.js
|
26
|
+
*/
|
27
27
|
var validateTypeOf = exports.validateTypeOf = allIsFunctions.typeOf;
|
28
28
|
function validateBundle(expectedBundleStructure, bundle) {
|
29
29
|
var originalWarn = console.warn;
|
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
|
-
var version = exports.version = '12.
|
7
|
+
var version = exports.version = '12.1.0';
|
8
8
|
// Note: This file is automatically generated when building math.js.
|
9
9
|
// Changes made in this file will be overwritten.
|
@@ -1,8 +1,8 @@
|
|
1
1
|
export var roundDocs = {
|
2
2
|
name: 'round',
|
3
3
|
category: 'Arithmetic',
|
4
|
-
syntax: ['round(x)', 'round(x, n)'],
|
4
|
+
syntax: ['round(x)', 'round(x, n)', 'round(unit, valuelessUnit)', 'round(unit, n, valuelessUnit)'],
|
5
5
|
description: 'round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.',
|
6
|
-
examples: ['round(3.2)', 'round(3.8)', 'round(-4.2)', 'round(-4.8)', 'round(pi, 3)', 'round(123.45678, 2)'],
|
6
|
+
examples: ['round(3.2)', 'round(3.8)', 'round(-4.2)', 'round(-4.8)', 'round(pi, 3)', 'round(123.45678, 2)', 'round(3.241cm, 2, cm)', 'round([3.2, 3.8, -4.7])'],
|
7
7
|
seealso: ['ceil', 'floor', 'fix']
|
8
8
|
};
|
@@ -208,9 +208,6 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
208
208
|
return _derivative(node.expr, constNodes);
|
209
209
|
},
|
210
210
|
'FunctionNode, Object': function FunctionNodeObject(node, constNodes) {
|
211
|
-
if (node.args.length !== 1) {
|
212
|
-
funcArgsCheck(node);
|
213
|
-
}
|
214
211
|
if (constNodes[node] !== undefined) {
|
215
212
|
return createConstantNode(0);
|
216
213
|
}
|
@@ -259,9 +256,12 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
259
256
|
}
|
260
257
|
break;
|
261
258
|
case 'pow':
|
262
|
-
|
263
|
-
|
264
|
-
|
259
|
+
if (node.args.length === 2) {
|
260
|
+
constNodes[arg1] = constNodes[node.args[1]];
|
261
|
+
// Pass to pow operator node parser
|
262
|
+
return _derivative(new OperatorNode('^', 'pow', [arg0, node.args[1]]), constNodes);
|
263
|
+
}
|
264
|
+
break;
|
265
265
|
case 'exp':
|
266
266
|
// d/dx(e^x) = e^x
|
267
267
|
funcDerivative = new FunctionNode('exp', [arg0.clone()]);
|
@@ -391,7 +391,7 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
391
391
|
break;
|
392
392
|
case 'gamma': // Needs digamma function, d/dx(gamma(x)) = gamma(x)digamma(x)
|
393
393
|
default:
|
394
|
-
throw new Error('
|
394
|
+
throw new Error('Cannot process function "' + node.name + '" in derivative: ' + 'the function is not supported, undefined, or the number of arguments passed to it are not supported');
|
395
395
|
}
|
396
396
|
var op, func;
|
397
397
|
if (div) {
|
@@ -502,33 +502,10 @@ export var createDerivative = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
502
502
|
// Functional Power Rule, d/dx(f^g) = f^g*[f'*(g/f) + g'ln(f)]
|
503
503
|
return new OperatorNode('*', 'multiply', [new OperatorNode('^', 'pow', [_arg.clone(), _arg2.clone()]), new OperatorNode('+', 'add', [new OperatorNode('*', 'multiply', [_derivative(_arg, constNodes), new OperatorNode('/', 'divide', [_arg2.clone(), _arg.clone()])]), new OperatorNode('*', 'multiply', [_derivative(_arg2, constNodes), new FunctionNode('log', [_arg.clone()])])])]);
|
504
504
|
}
|
505
|
-
throw new Error('
|
505
|
+
throw new Error('Cannot process operator "' + node.op + '" in derivative: ' + 'the operator is not supported, undefined, or the number of arguments passed to it are not supported');
|
506
506
|
}
|
507
507
|
});
|
508
508
|
|
509
|
-
/**
|
510
|
-
* Ensures the number of arguments for a function are correct,
|
511
|
-
* and will throw an error otherwise.
|
512
|
-
*
|
513
|
-
* @param {FunctionNode} node
|
514
|
-
*/
|
515
|
-
function funcArgsCheck(node) {
|
516
|
-
// TODO add min, max etc
|
517
|
-
if ((node.name === 'log' || node.name === 'nthRoot' || node.name === 'pow') && node.args.length === 2) {
|
518
|
-
return;
|
519
|
-
}
|
520
|
-
|
521
|
-
// There should be an incorrect number of arguments if we reach here
|
522
|
-
|
523
|
-
// Change all args to constants to avoid unidentified
|
524
|
-
// symbol error when compiling function
|
525
|
-
for (var i = 0; i < node.args.length; ++i) {
|
526
|
-
node.args[i] = createConstantNode(0);
|
527
|
-
}
|
528
|
-
node.compile().evaluate();
|
529
|
-
throw new Error('Function "' + node.name + '" is not supported by derivative, or a wrong number of arguments is passed');
|
530
|
-
}
|
531
|
-
|
532
509
|
/**
|
533
510
|
* Helper function to create a constant node with a specific type
|
534
511
|
* (number, BigNumber, Fraction)
|
@@ -91,17 +91,10 @@ export var createMod = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
91
91
|
return typed(name, {
|
92
92
|
'number, number': _modNumber,
|
93
93
|
'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
|
94
|
-
|
95
|
-
throw new Error('Cannot calculate mod for a negative divisor');
|
96
|
-
}
|
97
|
-
return y.isZero() ? x : x.mod(y);
|
94
|
+
return y.isZero() ? x : x.sub(y.mul(floor(x.div(y))));
|
98
95
|
},
|
99
96
|
'Fraction, Fraction': function FractionFraction(x, y) {
|
100
|
-
|
101
|
-
throw new Error('Cannot calculate mod for a negative divisor');
|
102
|
-
}
|
103
|
-
// Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend
|
104
|
-
return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y);
|
97
|
+
return y.equals(0) ? x : x.sub(y.mul(floor(x.div(y))));
|
105
98
|
}
|
106
99
|
}, matrixAlgorithmSuite({
|
107
100
|
SS: matAlgo05xSfSf,
|
@@ -36,6 +36,8 @@ export var createRound = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
36
36
|
*
|
37
37
|
* math.round(x)
|
38
38
|
* math.round(x, n)
|
39
|
+
* math.round(unit, valuelessUnit)
|
40
|
+
* math.round(unit, n, valuelessUnit)
|
39
41
|
*
|
40
42
|
* Examples:
|
41
43
|
*
|
@@ -53,14 +55,21 @@ export var createRound = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
53
55
|
* const c = math.complex(3.2, -2.7)
|
54
56
|
* math.round(c) // returns Complex 3 - 3i
|
55
57
|
*
|
58
|
+
* const unit = math.unit('3.241 cm')
|
59
|
+
* const cm = math.unit('cm')
|
60
|
+
* const mm = math.unit('mm')
|
61
|
+
* math.round(unit, 1, cm) // returns Unit 3.2 cm
|
62
|
+
* math.round(unit, 1, mm) // returns Unit 32.4 mm
|
63
|
+
*
|
56
64
|
* math.round([3.2, 3.8, -4.7]) // returns Array [3, 4, -5]
|
57
65
|
*
|
58
66
|
* See also:
|
59
67
|
*
|
60
68
|
* ceil, fix, floor
|
61
69
|
*
|
62
|
-
* @param {number | BigNumber | Fraction | Complex | Array | Matrix} x
|
70
|
+
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
|
63
71
|
* @param {number | BigNumber | Array} [n=0] Number of decimals
|
72
|
+
* @param {Unit} [valuelessUnit] A valueless unit
|
64
73
|
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
|
65
74
|
*/
|
66
75
|
return typed(name, {
|
@@ -112,37 +121,51 @@ export var createRound = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
112
121
|
}
|
113
122
|
return x.round(n.toNumber());
|
114
123
|
},
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
124
|
+
'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) {
|
125
|
+
var valueless = x.toNumeric(unit);
|
126
|
+
return unit.multiply(self(valueless, n));
|
127
|
+
}),
|
128
|
+
'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),
|
129
|
+
'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),
|
130
|
+
'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => {
|
131
|
+
// deep map collection, skip zeros since round(0) = 0
|
132
|
+
return deepMap(x, value => self(value, n, unit), true);
|
133
|
+
}),
|
134
|
+
'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),
|
135
|
+
'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),
|
136
|
+
'Array | Matrix': typed.referToSelf(self => x => {
|
137
|
+
// deep map collection, skip zeros since round(0) = 0
|
138
|
+
return deepMap(x, self, true);
|
139
|
+
}),
|
140
|
+
'SparseMatrix, number | BigNumber': typed.referToSelf(self => (x, n) => {
|
141
|
+
return matAlgo11xS0s(x, n, self, false);
|
119
142
|
}),
|
120
|
-
'DenseMatrix, number | BigNumber': typed.referToSelf(self => (x,
|
121
|
-
return matAlgo14xDs(x,
|
143
|
+
'DenseMatrix, number | BigNumber': typed.referToSelf(self => (x, n) => {
|
144
|
+
return matAlgo14xDs(x, n, self, false);
|
122
145
|
}),
|
123
|
-
'Array, number | BigNumber': typed.referToSelf(self => (x,
|
146
|
+
'Array, number | BigNumber': typed.referToSelf(self => (x, n) => {
|
124
147
|
// use matrix implementation
|
125
|
-
return matAlgo14xDs(matrix(x),
|
148
|
+
return matAlgo14xDs(matrix(x), n, self, false).valueOf();
|
126
149
|
}),
|
127
|
-
'number | Complex | BigNumber | Fraction, SparseMatrix': typed.referToSelf(self => (x,
|
150
|
+
'number | Complex | BigNumber | Fraction, SparseMatrix': typed.referToSelf(self => (x, n) => {
|
128
151
|
// check scalar is zero
|
129
152
|
if (equalScalar(x, 0)) {
|
130
153
|
// do not execute algorithm, result will be a zero matrix
|
131
|
-
return zeros(
|
154
|
+
return zeros(n.size(), n.storage());
|
132
155
|
}
|
133
|
-
return matAlgo12xSfs(
|
156
|
+
return matAlgo12xSfs(n, x, self, true);
|
134
157
|
}),
|
135
|
-
'number | Complex | BigNumber | Fraction, DenseMatrix': typed.referToSelf(self => (x,
|
158
|
+
'number | Complex | BigNumber | Fraction, DenseMatrix': typed.referToSelf(self => (x, n) => {
|
136
159
|
// check scalar is zero
|
137
160
|
if (equalScalar(x, 0)) {
|
138
161
|
// do not execute algorithm, result will be a zero matrix
|
139
|
-
return zeros(
|
162
|
+
return zeros(n.size(), n.storage());
|
140
163
|
}
|
141
|
-
return matAlgo14xDs(
|
164
|
+
return matAlgo14xDs(n, x, self, true);
|
142
165
|
}),
|
143
|
-
'number | Complex | BigNumber | Fraction, Array': typed.referToSelf(self => (x,
|
166
|
+
'number | Complex | BigNumber | Fraction, Array': typed.referToSelf(self => (x, n) => {
|
144
167
|
// use matrix implementation
|
145
|
-
return matAlgo14xDs(matrix(
|
168
|
+
return matAlgo14xDs(matrix(n), x, self, true).valueOf();
|
146
169
|
})
|
147
170
|
});
|
148
171
|
});
|
@@ -52,8 +52,8 @@ export var createPickRandom = /* #__PURE__ */factory(name, dependencies, _ref =>
|
|
52
52
|
* @param {Array | Matrix} array A one dimensional array
|
53
53
|
* @param {Int} number An int or float
|
54
54
|
* @param {Array | Matrix} weights An array of ints or floats
|
55
|
-
* @return {number | Array} Returns a single random value from array when number is
|
56
|
-
* Returns an array with the configured number of elements when number is
|
55
|
+
* @return {number | Array} Returns a single random value from array when number is undefined.
|
56
|
+
* Returns an array with the configured number of elements when number is defined.
|
57
57
|
*/
|
58
58
|
return typed(name, {
|
59
59
|
'Array | Matrix': function ArrayMatrix(possibles) {
|
package/lib/esm/type/number.js
CHANGED
@@ -43,7 +43,7 @@ function makeNumberFromNonDecimalParts(parts) {
|
|
43
43
|
}
|
44
44
|
var result = n + f;
|
45
45
|
if (isNaN(result)) {
|
46
|
-
throw new SyntaxError('String "' + parts.input + '" is
|
46
|
+
throw new SyntaxError('String "' + parts.input + '" is not a valid number');
|
47
47
|
}
|
48
48
|
return result;
|
49
49
|
}
|
@@ -99,7 +99,7 @@ export var createNumber = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
99
99
|
}
|
100
100
|
var num = Number(x);
|
101
101
|
if (isNaN(num)) {
|
102
|
-
throw new SyntaxError('String "' + x + '" is
|
102
|
+
throw new SyntaxError('String "' + x + '" is not a valid number');
|
103
103
|
}
|
104
104
|
if (wordSizeSuffixMatch) {
|
105
105
|
// x is a signed bin, oct, or hex literal
|
package/lib/esm/version.js
CHANGED