mathjs 3.1.0 → 3.1.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mathjs might be problematic. Click here for more details.
- package/HISTORY.md +34 -1
- package/dist/math.js +614 -500
- package/dist/math.map +1 -1
- package/dist/math.min.js +17 -17
- package/docs/expressions/customization.md +1 -0
- package/docs/reference/functions/format.md +12 -5
- package/docs/reference/functions/log10.md +1 -1
- package/docs/reference/functions.md +1 -1
- package/lib/expression/node/FunctionNode.js +7 -6
- package/lib/expression/node/OperatorNode.js +4 -4
- package/lib/expression/parse.js +42 -20
- package/lib/function/arithmetic/cbrt.js +32 -32
- package/lib/function/arithmetic/log10.js +12 -2
- package/lib/function/arithmetic/nthRoot.js +2 -2
- package/lib/function/statistics/max.js +7 -2
- package/lib/function/statistics/mean.js +7 -2
- package/lib/function/statistics/median.js +8 -2
- package/lib/function/statistics/min.js +7 -2
- package/lib/function/statistics/mode.js +3 -3
- package/lib/function/statistics/prod.js +2 -2
- package/lib/function/statistics/std.js +2 -2
- package/lib/function/statistics/sum.js +2 -2
- package/lib/function/statistics/var.js +2 -2
- package/lib/function/string/format.js +12 -5
- package/lib/function/trigonometry/acosh.js +11 -1
- package/lib/function/trigonometry/asec.js +1 -1
- package/lib/function/trigonometry/asinh.js +1 -1
- package/lib/function/trigonometry/atanh.js +11 -1
- package/lib/function/trigonometry/cosh.js +2 -2
- package/lib/function/trigonometry/sinh.js +3 -7
- package/lib/function/trigonometry/tanh.js +2 -2
- package/lib/type/unit/Unit.js +3 -0
- package/lib/utils/collection/containsCollections.js +18 -0
- package/lib/utils/number.js +1 -1
- package/lib/utils/types.js +10 -0
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/test/expression/node/FunctionNode.test.js +12 -0
- package/test/expression/parse.test.js +24 -3
- package/test/function/arithmetic/nthRoot.test.js +10 -0
- package/test/function/statistics/max.test.js +5 -0
- package/test/function/statistics/mean.test.js +5 -0
- package/test/function/statistics/median.test.js +5 -0
- package/test/function/statistics/min.test.js +5 -0
- package/test/function/trigonometry/sinh.test.js +41 -30
@@ -332,6 +332,7 @@ var latex = expression.toTex({handler: customLaTeX});
|
|
332
332
|
You can change the way that implicit multiplication is converted to a string or LaTeX. The two options are `hide`, to not show a multiplication operator for implicit multiplication and `show` to show it.
|
333
333
|
|
334
334
|
Example:
|
335
|
+
|
335
336
|
```js
|
336
337
|
var node = math.parse('2a');
|
337
338
|
|
@@ -11,7 +11,7 @@ Format a value of any type into a string.
|
|
11
11
|
math.format(value)
|
12
12
|
math.format(value, options)
|
13
13
|
math.format(value, precision)
|
14
|
-
math.format(value,
|
14
|
+
math.format(value, callback)
|
15
15
|
```
|
16
16
|
|
17
17
|
### Where
|
@@ -47,10 +47,12 @@ math.format(value, fn)
|
|
47
47
|
- `fraction: string`. Available values: 'ratio' (default) or 'decimal'.
|
48
48
|
For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is
|
49
49
|
configured, and will output `0.(3)` when 'decimal' is configured.
|
50
|
-
- `
|
51
|
-
A custom formatting function
|
52
|
-
|
53
|
-
|
50
|
+
- `callback: function`
|
51
|
+
A custom formatting function, invoked for all numeric elements in `value`,
|
52
|
+
for example all elements of a matrix, or the real and imaginary
|
53
|
+
parts of a complex number. This callback can be used to override the
|
54
|
+
built-in numeric notation with any type of formatting. Function `callback`
|
55
|
+
is called with `value` as parameter and must return a string.
|
54
56
|
|
55
57
|
### Parameters
|
56
58
|
|
@@ -77,6 +79,11 @@ math.format(21385, 2); // returns '21000'
|
|
77
79
|
math.format(12.071, {notation: 'fixed'}); // returns '12'
|
78
80
|
math.format(2.3, {notation: 'fixed', precision: 2}); // returns '2.30'
|
79
81
|
math.format(52.8, {notation: 'exponential'}); // returns '5.28e+1'
|
82
|
+
|
83
|
+
function formatCurrency(value) {
|
84
|
+
return '$' + value.toFixed(2);
|
85
|
+
}
|
86
|
+
math.format([2.1, 3, 0.016], formatCurrency}; // returns '[$2.10, $3.00, $0.02]'
|
80
87
|
```
|
81
88
|
|
82
89
|
|
@@ -64,7 +64,7 @@ Function | Description
|
|
64
64
|
[math.hypot(a, b, ...)](functions/hypot.md) | Calculate the hypotenusa of a list with values.
|
65
65
|
[math.lcm(a, b)](functions/lcm.md) | Calculate the least common multiple for two or more values or arrays.
|
66
66
|
[math.log(x [, base])](functions/log.md) | Calculate the logarithm of a value.
|
67
|
-
[math.log10(x)](functions/log10.md) | Calculate the 10-base of a value.
|
67
|
+
[math.log10(x)](functions/log10.md) | Calculate the 10-base logarithm of a value.
|
68
68
|
[math.mod(x, y)](functions/mod.md) | Calculates the modulus, the remainder of an integer division.
|
69
69
|
[math.multiply(x, y)](functions/multiply.md) | Multiply two values, `x * y`.
|
70
70
|
[math.norm(x [, p])](functions/norm.md) | Calculate the norm of a number, vector or matrix.
|
@@ -195,7 +195,7 @@ function factory (type, config, load, typed, math) {
|
|
195
195
|
*/
|
196
196
|
FunctionNode.prototype.toString = function (options) {
|
197
197
|
var customString;
|
198
|
-
var name = this.fn.toString();
|
198
|
+
var name = this.fn.toString(options);
|
199
199
|
if (options && (typeof options.handler === 'object') && options.handler.hasOwnProperty(name)) {
|
200
200
|
//callback is a map of callback functions
|
201
201
|
customString = options.handler[name](this, options);
|
@@ -215,8 +215,12 @@ function factory (type, config, load, typed, math) {
|
|
215
215
|
* @return {string} str
|
216
216
|
*/
|
217
217
|
FunctionNode.prototype._toString = function (options) {
|
218
|
-
|
219
|
-
|
218
|
+
var args = this.args.map(function (arg) {
|
219
|
+
return arg.toString(options);
|
220
|
+
});
|
221
|
+
|
222
|
+
// format the arguments like "add(2, 4.2)"
|
223
|
+
return this.fn.toString(options) + '(' + args.join(', ') + ')';
|
220
224
|
};
|
221
225
|
|
222
226
|
/*
|
@@ -328,9 +332,6 @@ function factory (type, config, load, typed, math) {
|
|
328
332
|
* @return {string} str
|
329
333
|
*/
|
330
334
|
FunctionNode.prototype._toTex = function (options) {
|
331
|
-
var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep';
|
332
|
-
|
333
|
-
|
334
335
|
var args = this.args.map(function (arg) { //get LaTeX of the arguments
|
335
336
|
return arg.toTex(options);
|
336
337
|
});
|
@@ -14,10 +14,10 @@ function factory (type, config, load, typed, math) {
|
|
14
14
|
* @extends {Node}
|
15
15
|
* An operator with two arguments, like 2+3
|
16
16
|
*
|
17
|
-
* @param {string} op
|
18
|
-
* @param {string} fn
|
19
|
-
* @param {Node[]} args
|
20
|
-
* @param {boolean} implicit
|
17
|
+
* @param {string} op Operator name, for example '+'
|
18
|
+
* @param {string} fn Function name, for example 'add'
|
19
|
+
* @param {Node[]} args Operator arguments
|
20
|
+
* @param {boolean} [implicit] Is this an implicit multiplication?
|
21
21
|
*/
|
22
22
|
function OperatorNode(op, fn, args, implicit) {
|
23
23
|
if (!(this instanceof OperatorNode)) {
|
package/lib/expression/parse.js
CHANGED
@@ -271,7 +271,7 @@ function factory (type, config, load, typed) {
|
|
271
271
|
next();
|
272
272
|
|
273
273
|
if (!isDigit(c)) {
|
274
|
-
// this is no legal number, it is just a dot
|
274
|
+
// this is no legal number, it is just a dot (can be dot notation)
|
275
275
|
token_type = TOKENTYPE.UNKNOWN;
|
276
276
|
}
|
277
277
|
}
|
@@ -292,24 +292,33 @@ function factory (type, config, load, typed) {
|
|
292
292
|
|
293
293
|
// check for exponential notation like "2.3e-4", "1.23e50" or "2e+4"
|
294
294
|
c2 = nextPreview();
|
295
|
-
if (
|
296
|
-
|
297
|
-
next();
|
298
|
-
|
299
|
-
if (c == '+' || c == '-') {
|
295
|
+
if (c == 'E' || c == 'e') {
|
296
|
+
if (isDigit(c2) || c2 == '-' || c2 == '+') {
|
300
297
|
token += c;
|
301
298
|
next();
|
302
|
-
}
|
303
299
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
}
|
300
|
+
if (c == '+' || c == '-') {
|
301
|
+
token += c;
|
302
|
+
next();
|
303
|
+
}
|
309
304
|
|
310
|
-
|
311
|
-
|
305
|
+
// Scientific notation MUST be followed by an exponent
|
306
|
+
if (!isDigit(c)) {
|
307
|
+
throw createSyntaxError('Digit expected, got "' + c + '"');
|
308
|
+
}
|
309
|
+
|
310
|
+
while (isDigit(c)) {
|
311
|
+
token += c;
|
312
|
+
next();
|
313
|
+
}
|
314
|
+
|
315
|
+
if (c == '.') {
|
316
|
+
throw createSyntaxError('Digit expected, got "' + c + '"');
|
317
|
+
}
|
318
|
+
}
|
319
|
+
else if (c2 == '.') {
|
312
320
|
next();
|
321
|
+
throw createSyntaxError('Digit expected, got "' + c + '"');
|
313
322
|
}
|
314
323
|
}
|
315
324
|
|
@@ -774,8 +783,16 @@ function factory (type, config, load, typed) {
|
|
774
783
|
fn = operators[name];
|
775
784
|
|
776
785
|
getTokenSkipNewline();
|
777
|
-
|
778
|
-
|
786
|
+
|
787
|
+
if (name === 'in' && token === '') {
|
788
|
+
// end of expression -> this is the unit 'in' ('inch')
|
789
|
+
node = new OperatorNode('*', 'multiply', [node, new SymbolNode('in')], true);
|
790
|
+
}
|
791
|
+
else {
|
792
|
+
// operator 'a to b' or 'a in b'
|
793
|
+
params = [node, parseRange()];
|
794
|
+
node = new OperatorNode(name, fn, params);
|
795
|
+
}
|
779
796
|
}
|
780
797
|
|
781
798
|
return node;
|
@@ -1121,7 +1138,8 @@ function factory (type, config, load, typed) {
|
|
1121
1138
|
}
|
1122
1139
|
else {
|
1123
1140
|
// implicit multiplication like (2+3)(4+5)
|
1124
|
-
|
1141
|
+
// next parse operation is parseUnary, the one after parseMultiplyDivide
|
1142
|
+
node = new OperatorNode('*', 'multiply', [node, parseUnary()], true /*implicit*/);
|
1125
1143
|
}
|
1126
1144
|
}
|
1127
1145
|
else if (token == '[') {
|
@@ -1195,11 +1213,15 @@ function factory (type, config, load, typed) {
|
|
1195
1213
|
*/
|
1196
1214
|
function parseStringToken () {
|
1197
1215
|
var str = '';
|
1198
|
-
var tPrev = '';
|
1199
1216
|
|
1200
|
-
while (c != '' &&
|
1217
|
+
while (c != '' && c != '\"') {
|
1218
|
+
if (c == '\\') {
|
1219
|
+
// escape character
|
1220
|
+
str += c;
|
1221
|
+
next();
|
1222
|
+
}
|
1223
|
+
|
1201
1224
|
str += c;
|
1202
|
-
tPrev = c;
|
1203
1225
|
next();
|
1204
1226
|
}
|
1205
1227
|
|
@@ -67,38 +67,6 @@ function factory (type, config, load, typed) {
|
|
67
67
|
}
|
68
68
|
});
|
69
69
|
|
70
|
-
/**
|
71
|
-
* Calculate cbrt for a number
|
72
|
-
*
|
73
|
-
* Code from es6-shim.js:
|
74
|
-
* https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1564-L1577
|
75
|
-
*
|
76
|
-
* @param {number} x
|
77
|
-
* @returns {number | Complex} Returns the cubic root of x
|
78
|
-
* @private
|
79
|
-
*/
|
80
|
-
function _cbrtNumber(x) {
|
81
|
-
if (x === 0) {
|
82
|
-
return x;
|
83
|
-
}
|
84
|
-
|
85
|
-
var negate = x < 0;
|
86
|
-
var result;
|
87
|
-
if (negate) {
|
88
|
-
x = -x;
|
89
|
-
}
|
90
|
-
|
91
|
-
if (isFinite(x)) {
|
92
|
-
result = Math.exp(Math.log(x) / 3);
|
93
|
-
// from http://en.wikipedia.org/wiki/Cube_root#Numerical_methods
|
94
|
-
result = (x / (result * result) + (2 * result)) / 3;
|
95
|
-
} else {
|
96
|
-
result = x;
|
97
|
-
}
|
98
|
-
|
99
|
-
return negate ? -result : result;
|
100
|
-
}
|
101
|
-
|
102
70
|
/**
|
103
71
|
* Calculate the cubic root for a complex number
|
104
72
|
* @param {Complex} x
|
@@ -181,5 +149,37 @@ function factory (type, config, load, typed) {
|
|
181
149
|
return cbrt;
|
182
150
|
}
|
183
151
|
|
152
|
+
/**
|
153
|
+
* Calculate cbrt for a number
|
154
|
+
*
|
155
|
+
* Code from es6-shim.js:
|
156
|
+
* https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1564-L1577
|
157
|
+
*
|
158
|
+
* @param {number} x
|
159
|
+
* @returns {number | Complex} Returns the cubic root of x
|
160
|
+
* @private
|
161
|
+
*/
|
162
|
+
var _cbrtNumber = Math.cbrt || function (x) {
|
163
|
+
if (x === 0) {
|
164
|
+
return x;
|
165
|
+
}
|
166
|
+
|
167
|
+
var negate = x < 0;
|
168
|
+
var result;
|
169
|
+
if (negate) {
|
170
|
+
x = -x;
|
171
|
+
}
|
172
|
+
|
173
|
+
if (isFinite(x)) {
|
174
|
+
result = Math.exp(Math.log(x) / 3);
|
175
|
+
// from http://en.wikipedia.org/wiki/Cube_root#Numerical_methods
|
176
|
+
result = (x / (result * result) + (2 * result)) / 3;
|
177
|
+
} else {
|
178
|
+
result = x;
|
179
|
+
}
|
180
|
+
|
181
|
+
return negate ? -result : result;
|
182
|
+
};
|
183
|
+
|
184
184
|
exports.name = 'cbrt';
|
185
185
|
exports.factory = factory;
|
@@ -4,7 +4,7 @@ var deepMap = require('../../utils/collection/deepMap');
|
|
4
4
|
|
5
5
|
function factory (type, config, load, typed) {
|
6
6
|
/**
|
7
|
-
* Calculate the 10-base of a value. This is the same as calculating `log(x, 10)`.
|
7
|
+
* Calculate the 10-base logarithm of a value. This is the same as calculating `log(x, 10)`.
|
8
8
|
*
|
9
9
|
* For matrices, the function is evaluated element wise.
|
10
10
|
*
|
@@ -31,7 +31,7 @@ function factory (type, config, load, typed) {
|
|
31
31
|
var log10 = typed('log10', {
|
32
32
|
'number': function (x) {
|
33
33
|
if (x >= 0 || config.predictable) {
|
34
|
-
return
|
34
|
+
return _log10(x);
|
35
35
|
}
|
36
36
|
else {
|
37
37
|
// negative value -> complex value computation
|
@@ -63,6 +63,16 @@ function factory (type, config, load, typed) {
|
|
63
63
|
return log10;
|
64
64
|
}
|
65
65
|
|
66
|
+
/**
|
67
|
+
* Calculate the 10-base logarithm of a number
|
68
|
+
* @param {number} x
|
69
|
+
* @return {number}
|
70
|
+
* @private
|
71
|
+
*/
|
72
|
+
var _log10 = Math.log10 || function (x) {
|
73
|
+
return Math.log(x) / Math.LN10;
|
74
|
+
};
|
75
|
+
|
66
76
|
exports.name = 'log10';
|
67
77
|
exports.factory = factory;
|
68
78
|
|
@@ -203,7 +203,7 @@ function factory (type, config, load, typed) {
|
|
203
203
|
|
204
204
|
// edge cases zero and infinity
|
205
205
|
if (a.isZero()) {
|
206
|
-
return
|
206
|
+
return inv ? new Big(Infinity) : 0;
|
207
207
|
}
|
208
208
|
if (!a.isFinite()) {
|
209
209
|
return inv ? zero : a;
|
@@ -239,7 +239,7 @@ function _nthRoot(a, root) {
|
|
239
239
|
|
240
240
|
// edge cases zero and infinity
|
241
241
|
if (a == 0) {
|
242
|
-
return 0;
|
242
|
+
return inv ? Infinity : 0;
|
243
243
|
}
|
244
244
|
if (!isFinite(a)) {
|
245
245
|
return inv ? 0 : a;
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
var deepForEach = require('../../utils/collection/deepForEach');
|
4
4
|
var reduce = require('../../utils/collection/reduce');
|
5
|
+
var containsCollections = require('../../utils/collection/containsCollections');
|
5
6
|
|
6
7
|
function factory (type, config, load, typed) {
|
7
8
|
var larger = load(require('../relational/larger'));
|
@@ -47,8 +48,12 @@ function factory (type, config, load, typed) {
|
|
47
48
|
},
|
48
49
|
|
49
50
|
// max(a, b, c, d, ...)
|
50
|
-
'...': function () {
|
51
|
-
|
51
|
+
'...': function (args) {
|
52
|
+
if (containsCollections(args)) {
|
53
|
+
throw new TypeError('Scalar values expected in function max');
|
54
|
+
}
|
55
|
+
|
56
|
+
return _max(args);
|
52
57
|
}
|
53
58
|
});
|
54
59
|
|
@@ -3,6 +3,7 @@
|
|
3
3
|
var size = require('../../utils/array').size;
|
4
4
|
var deepForEach = require('../../utils/collection/deepForEach');
|
5
5
|
var reduce = require('../../utils/collection/reduce');
|
6
|
+
var containsCollections = require('../../utils/collection/containsCollections');
|
6
7
|
|
7
8
|
function factory (type, config, load, typed) {
|
8
9
|
var add = load(require('../arithmetic/add'));
|
@@ -43,8 +44,12 @@ function factory (type, config, load, typed) {
|
|
43
44
|
'Array | Matrix, number | BigNumber': _nmean,
|
44
45
|
|
45
46
|
// mean(a, b, c, d, ...)
|
46
|
-
'...': function () {
|
47
|
-
|
47
|
+
'...': function (args) {
|
48
|
+
if (containsCollections(args)) {
|
49
|
+
throw new TypeError('Scalar values expected in function mean');
|
50
|
+
}
|
51
|
+
|
52
|
+
return _mean(args);
|
48
53
|
}
|
49
54
|
});
|
50
55
|
|
@@ -1,6 +1,8 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
3
|
var flatten = require('../../utils/array').flatten;
|
4
|
+
var reduce = require('../../utils/collection/reduce');
|
5
|
+
var containsCollections = require('../../utils/collection/containsCollections');
|
4
6
|
|
5
7
|
function factory (type, config, load, typed) {
|
6
8
|
var add = load(require('../arithmetic/addScalar'));
|
@@ -46,8 +48,12 @@ function factory (type, config, load, typed) {
|
|
46
48
|
},
|
47
49
|
|
48
50
|
// median(a, b, c, d, ...)
|
49
|
-
'...': function () {
|
50
|
-
|
51
|
+
'...': function (args) {
|
52
|
+
if (containsCollections(args)) {
|
53
|
+
throw new TypeError('Scalar values expected in function median');
|
54
|
+
}
|
55
|
+
|
56
|
+
return _median(args);
|
51
57
|
}
|
52
58
|
});
|
53
59
|
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
var deepForEach = require('../../utils/collection/deepForEach');
|
4
4
|
var reduce = require('../../utils/collection/reduce');
|
5
|
+
var containsCollections = require('../../utils/collection/containsCollections');
|
5
6
|
|
6
7
|
function factory (type, config, load, typed) {
|
7
8
|
var smaller = load(require('../relational/smaller'));
|
@@ -47,8 +48,12 @@ function factory (type, config, load, typed) {
|
|
47
48
|
},
|
48
49
|
|
49
50
|
// min(a, b, c, d, ...)
|
50
|
-
'...': function () {
|
51
|
-
|
51
|
+
'...': function (args) {
|
52
|
+
if (containsCollections(args)) {
|
53
|
+
throw new TypeError('Scalar values expected in function min');
|
54
|
+
}
|
55
|
+
|
56
|
+
return _min(args);
|
52
57
|
}
|
53
58
|
});
|
54
59
|
|
@@ -33,8 +33,8 @@ function factory (type, config, load, typed) {
|
|
33
33
|
var mode = typed('mode', {
|
34
34
|
'Array | Matrix' : _mode,
|
35
35
|
|
36
|
-
'...': function () {
|
37
|
-
return _mode(
|
36
|
+
'...': function (args) {
|
37
|
+
return _mode(args);
|
38
38
|
}
|
39
39
|
});
|
40
40
|
|
@@ -42,7 +42,7 @@ function factory (type, config, load, typed) {
|
|
42
42
|
|
43
43
|
/**
|
44
44
|
* Calculates the mode in an 1-dimensional array
|
45
|
-
* @param {Array}
|
45
|
+
* @param {Array} values
|
46
46
|
* @return {number} mode
|
47
47
|
* @private
|
48
48
|
*/
|
@@ -40,9 +40,9 @@ function factory (type, config, load, typed) {
|
|
40
40
|
throw new Error('sum(A, dim) is not yet supported');
|
41
41
|
},
|
42
42
|
|
43
|
-
'...': function () {
|
43
|
+
'...': function (args) {
|
44
44
|
// sum(a, b, c, d, ...)
|
45
|
-
return _sum(
|
45
|
+
return _sum(args);
|
46
46
|
}
|
47
47
|
});
|
48
48
|
|
@@ -62,8 +62,8 @@ function factory (type, config, load, typed) {
|
|
62
62
|
'Array | Matrix, string': _var,
|
63
63
|
|
64
64
|
// var(a, b, c, d, ...)
|
65
|
-
'...': function () {
|
66
|
-
return _var(
|
65
|
+
'...': function (args) {
|
66
|
+
return _var(args, DEFAULT_NORMALIZATION);
|
67
67
|
}
|
68
68
|
});
|
69
69
|
|
@@ -11,7 +11,7 @@ function factory (type, config, load, typed) {
|
|
11
11
|
* math.format(value)
|
12
12
|
* math.format(value, options)
|
13
13
|
* math.format(value, precision)
|
14
|
-
* math.format(value,
|
14
|
+
* math.format(value, callback)
|
15
15
|
*
|
16
16
|
* Where:
|
17
17
|
*
|
@@ -46,10 +46,12 @@ function factory (type, config, load, typed) {
|
|
46
46
|
* - `fraction: string`. Available values: 'ratio' (default) or 'decimal'.
|
47
47
|
* For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is
|
48
48
|
* configured, and will output `0.(3)` when 'decimal' is configured.
|
49
|
-
* - `
|
50
|
-
* A custom formatting function
|
51
|
-
*
|
52
|
-
*
|
49
|
+
* - `callback: function`
|
50
|
+
* A custom formatting function, invoked for all numeric elements in `value`,
|
51
|
+
* for example all elements of a matrix, or the real and imaginary
|
52
|
+
* parts of a complex number. This callback can be used to override the
|
53
|
+
* built-in numeric notation with any type of formatting. Function `callback`
|
54
|
+
* is called with `value` as parameter and must return a string.
|
53
55
|
*
|
54
56
|
* When `value` is an Object:
|
55
57
|
*
|
@@ -77,6 +79,11 @@ function factory (type, config, load, typed) {
|
|
77
79
|
* math.format(2.3, {notation: 'fixed', precision: 2}); // returns '2.30'
|
78
80
|
* math.format(52.8, {notation: 'exponential'}); // returns '5.28e+1'
|
79
81
|
*
|
82
|
+
* function formatCurrency(value) {
|
83
|
+
* return '$' + value.toFixed(2);
|
84
|
+
* }
|
85
|
+
* math.format([2.1, 3, 0.016], formatCurrency}; // returns '[$2.10, $3.00, $0.02]'
|
86
|
+
*
|
80
87
|
* See also:
|
81
88
|
*
|
82
89
|
* print
|
@@ -28,7 +28,7 @@ function factory (type, config, load, typed) {
|
|
28
28
|
var acosh = typed('acosh', {
|
29
29
|
'number': function (x) {
|
30
30
|
if (x >= 1 || config.predictable) {
|
31
|
-
return
|
31
|
+
return _acosh(x);
|
32
32
|
}
|
33
33
|
if (x <= -1) {
|
34
34
|
return new type.Complex(Math.log(Math.sqrt(x*x - 1) - x), Math.PI);
|
@@ -54,5 +54,15 @@ function factory (type, config, load, typed) {
|
|
54
54
|
return acosh;
|
55
55
|
}
|
56
56
|
|
57
|
+
/**
|
58
|
+
* Calculate the hyperbolic arccos of a number
|
59
|
+
* @param {number} x
|
60
|
+
* @return {number}
|
61
|
+
* @private
|
62
|
+
*/
|
63
|
+
var _acosh = Math.acosh || function (x) {
|
64
|
+
return Math.log(Math.sqrt(x*x - 1) + x)
|
65
|
+
};
|
66
|
+
|
57
67
|
exports.name = 'acosh';
|
58
68
|
exports.factory = factory;
|
@@ -26,7 +26,7 @@ function factory (type, config, load, typed) {
|
|
26
26
|
* @return {number | Complex | Array | Matrix} Hyperbolic arcsine of x
|
27
27
|
*/
|
28
28
|
var asinh = typed('asinh', {
|
29
|
-
'number': function (x) {
|
29
|
+
'number': Math.asinh || function (x) {
|
30
30
|
return Math.log(Math.sqrt(x*x + 1) + x);
|
31
31
|
},
|
32
32
|
|
@@ -27,7 +27,7 @@ function factory (type, config, load, typed) {
|
|
27
27
|
var atanh = typed('atanh', {
|
28
28
|
'number': function (x) {
|
29
29
|
if ((x <= 1 && x >= -1) || config.predictable) {
|
30
|
-
return
|
30
|
+
return _atanh(x);
|
31
31
|
}
|
32
32
|
return new type.Complex(x, 0).atanh();
|
33
33
|
},
|
@@ -51,5 +51,15 @@ function factory (type, config, load, typed) {
|
|
51
51
|
return atanh;
|
52
52
|
}
|
53
53
|
|
54
|
+
/**
|
55
|
+
* Calculate the hyperbolic arctangent of a number
|
56
|
+
* @param {number} x
|
57
|
+
* @return {number}
|
58
|
+
* @private
|
59
|
+
*/
|
60
|
+
var _atanh = Math.atanh || function (x) {
|
61
|
+
return Math.log((1 + x)/(1 - x)) / 2
|
62
|
+
};
|
63
|
+
|
54
64
|
exports.name = 'atanh';
|
55
65
|
exports.factory = factory;
|
@@ -58,9 +58,9 @@ function factory (type, config, load, typed) {
|
|
58
58
|
* @returns {number}
|
59
59
|
* @private
|
60
60
|
*/
|
61
|
-
function
|
61
|
+
var _cosh = Math.cosh || function (x) {
|
62
62
|
return (Math.exp(x) + Math.exp(-x)) / 2;
|
63
|
-
}
|
63
|
+
};
|
64
64
|
|
65
65
|
exports.name = 'cosh';
|
66
66
|
exports.factory = factory;
|
@@ -59,13 +59,9 @@ function factory (type, config, load, typed) {
|
|
59
59
|
* @returns {number}
|
60
60
|
* @private
|
61
61
|
*/
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
} else {
|
66
|
-
return (Math.exp(x) - Math.exp(-x)) / 2;
|
67
|
-
}
|
68
|
-
}
|
62
|
+
var _sinh = Math.sinh || function (x) {
|
63
|
+
return (Math.exp(x) - Math.exp(-x)) / 2;
|
64
|
+
};
|
69
65
|
|
70
66
|
exports.name = 'sinh';
|
71
67
|
exports.factory = factory;
|