-
Notifications
You must be signed in to change notification settings - Fork 119
/
abs.jl
54 lines (44 loc) · 1.32 KB
/
abs.jl
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
#############################################################################
# abs.jl
# Absolute value of an expression
# All expressions and atoms are subtpyes of AbstractExpr.
# Please read expressions.jl first.
#############################################################################
import Base.abs
export abs
export sign, curvature, monotonicity, evaluate
### Absolute Value
type AbsAtom <: AbstractExpr
head::Symbol
id_hash::UInt64
children::Tuple{AbstractExpr}
size::Tuple{Int, Int}
function AbsAtom(x::AbstractExpr)
children = (x,)
return new(:abs, hash(children), children, x.size)
end
end
function sign(x::AbsAtom)
return Positive()
end
function monotonicity(x::AbsAtom)
return (Nondecreasing() * sign(x.children[1]),)
end
function curvature(x::AbsAtom)
return ConvexVexity()
end
function evaluate(x::AbsAtom)
return abs(evaluate(x.children[1]))
end
abs(x::AbstractExpr) = AbsAtom(x)
function conic_form!(x::AbsAtom, unique_conic_forms::UniqueConicForms)
if !has_conic_form(unique_conic_forms, x)
c = x.children[1]
t = Variable(size(c))
objective = conic_form!(t, unique_conic_forms)
conic_form!(c<=t, unique_conic_forms)
conic_form!(c>=-t, unique_conic_forms)
cache_conic_form!(unique_conic_forms, x, objective)
end
return get_conic_form(unique_conic_forms, x)
end