1 |
|
2 |
|
3 | const deepMap = require('../../utils/collection/deepMap')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | const latex = require('../../utils/latex')
|
7 |
|
8 | /**
|
9 | * Inverse the sign of a value, apply a unary minus operation.
|
10 | *
|
11 | * For matrices, the function is evaluated element wise. Boolean values and
|
12 | * strings will be converted to a number. For complex numbers, both real and
|
13 | * complex value are inverted.
|
14 | *
|
15 | * Syntax:
|
16 | *
|
17 | * math.unaryMinus(x)
|
18 | *
|
19 | * Examples:
|
20 | *
|
21 | * math.unaryMinus(3.5) // returns -3.5
|
22 | * math.unaryMinus(-4.2) // returns 4.2
|
23 | *
|
24 | * See also:
|
25 | *
|
26 | * add, subtract, unaryPlus
|
27 | *
|
28 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Number to be inverted.
|
29 | * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Returns the value with inverted sign.
|
30 | */
|
31 | const unaryMinus = typed('unaryMinus', {
|
32 | 'number': function (x) {
|
33 | return -x
|
34 | },
|
35 |
|
36 | 'Complex': function (x) {
|
37 | return x.neg()
|
38 | },
|
39 |
|
40 | 'BigNumber': function (x) {
|
41 | return x.neg()
|
42 | },
|
43 |
|
44 | 'Fraction': function (x) {
|
45 | return x.neg()
|
46 | },
|
47 |
|
48 | 'Unit': function (x) {
|
49 | const res = x.clone()
|
50 | res.value = unaryMinus(x.value)
|
51 | return res
|
52 | },
|
53 |
|
54 | 'Array | Matrix': function (x) {
|
55 | // deep map collection, skip zeros since unaryMinus(0) = 0
|
56 | return deepMap(x, unaryMinus, true)
|
57 | }
|
58 |
|
59 | // TODO: add support for string
|
60 | })
|
61 |
|
62 | unaryMinus.toTex = {
|
63 | 1: `${latex.operators['unaryMinus']}\\left(\${args[0]}\\right)`
|
64 | }
|
65 |
|
66 | return unaryMinus
|
67 | }
|
68 |
|
69 | exports.name = 'unaryMinus'
|
70 | exports.factory = factory
|