summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/tex/algebra/luacas-absexpression.lua
blob: 2bf0b6a7efc9c11937bd05d2b5f6b6856078d3cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
--- @class AbsExpression
--- The absolute value of an expression.
--- @field expression Expression
AbsExpression = {}
__AbsExpression = {}

----------------------------
-- Instance functionality --
----------------------------

--- Creates a new absolute value expression with the given expression.
--- @param expression Expression
--- @return AbsExpression
function AbsExpression:new(expression)
    local o = {}
    local __o = Copy(__ExpressionOperations)

    o.expression = expression

    __o.__index = AbsExpression
    __o.__tostring = function(a)
        return '|' .. tostring(a.expression) .. '|'
    end

    o = setmetatable(o, __o)
    return o
end

--- @return Expression
function AbsExpression:evaluate()
    if self.expression:isconstant() then
        if self.expression >= Integer.zero() then
            return self.expression
        end
        return -self.expression
    end
    return self
end

--- @return Expression
function AbsExpression:autosimplify()
    return AbsExpression(self.expression:autosimplify()):evaluate()
end

--- @return table<number, Expression>
function AbsExpression:subexpressions()
    return {self.expression}
end

--- @param subexpressions table<number, Expression>
--- @return AbsExpression
function AbsExpression:setsubexpressions(subexpressions)
    return AbsExpression(subexpressions[1])
end

--- @param other Expression
--- @return boolean
function AbsExpression:order(other)
    return FunctionExpression("abs", self.expression):order(other)
end

--- @return string
function AbsExpression:tolatex()
    return "\\left|" .. self.expression:tolatex() .. "\\right|"
end

-----------------
-- Inheritance --
-----------------

__AbsExpression.__index = CompoundExpression
__AbsExpression.__call = AbsExpression.new
AbsExpression = setmetatable(AbsExpression, __AbsExpression)

----------------------
-- Static constants --
----------------------
ABS = function (a)
    return AbsExpression(a)
end