UNPKG

1.71 kBJavaScriptView Raw
1import { factory } from '../../utils/factory';
2import { deepMap } from '../../utils/collection';
3import { signNumber } from '../../plain/number';
4var name = 'sign';
5var dependencies = ['typed', 'BigNumber', 'Fraction'];
6export var createSign =
7/* #__PURE__ */
8factory(name, dependencies, function (_ref) {
9 var typed = _ref.typed,
10 _BigNumber = _ref.BigNumber,
11 _Fraction = _ref.Fraction;
12
13 /**
14 * Compute the sign of a value. The sign of a value x is:
15 *
16 * - 1 when x > 0
17 * - -1 when x < 0
18 * - 0 when x == 0
19 *
20 * For matrices, the function is evaluated element wise.
21 *
22 * Syntax:
23 *
24 * math.sign(x)
25 *
26 * Examples:
27 *
28 * math.sign(3.5) // returns 1
29 * math.sign(-4.2) // returns -1
30 * math.sign(0) // returns 0
31 *
32 * math.sign([3, 5, -2, 0, 2]) // returns [1, 1, -1, 0, 1]
33 *
34 * See also:
35 *
36 * abs
37 *
38 * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x
39 * The number for which to determine the sign
40 * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}e
41 * The sign of `x`
42 */
43 var sign = typed(name, {
44 number: signNumber,
45 Complex: function Complex(x) {
46 return x.sign();
47 },
48 BigNumber: function BigNumber(x) {
49 return new _BigNumber(x.cmp(0));
50 },
51 Fraction: function Fraction(x) {
52 return new _Fraction(x.s, 1);
53 },
54 'Array | Matrix': function ArrayMatrix(x) {
55 // deep map collection, skip zeros since sign(0) = 0
56 return deepMap(x, sign, true);
57 },
58 Unit: function Unit(x) {
59 return sign(x.value);
60 }
61 });
62 return sign;
63});
\No newline at end of file