Skip to content

Instantly share code, notes, and snippets.

@karanveerm
Created January 16, 2015 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karanveerm/efe5c3e7589a5611eb63 to your computer and use it in GitHub Desktop.
Save karanveerm/efe5c3e7589a5611eb63 to your computer and use it in GitHub Desktop.
- #############################################################################
- # dcp.jl
- # This file handles the basic rules on interactions of mathematical expressions
- # to create new expressions.
- #
- # For example: negative of a concaveVexity expression is convexVexity, or multiplication
- # of two positive expressions continue to be positive.
- #
- # See: http://dcp.stanford.edu/rules or the original paper at
- # http://web.stanford.edu/~boyd/papers/disc_cvx_prog.html
- #############################################################################
-
- export Vexity, ConstVexity, AffineVexity, ConvexVexity, ConcaveVexity, NotDcp
- export Monotonicity, Nonincreasing, Nondecreasing, NoMonotonicity
- export Sign, Positive, Negative, NoSign
- export -, +, *
-
- # Vexity subtypes
- abstract Vexity
- type ConstVexity <: Vexity end
- type AffineVexity <: Vexity end
- type ConvexVexity <: Vexity end
- type ConcaveVexity <: Vexity end
-
- type NotDcp <: Vexity
- function NotDcp()
- warn("Expression not DCP compliant")
- return new()
- end
- end
-
- # Monotonocity subtypes
- abstract Monotonicity
- type Nonincreasing <: Monotonicity end
- type Nondecreasing <: Monotonicity end
- type ConstMonotonicity <: Monotonicity end
- type NoMonotonicity <: Monotonicity end
-
- # Sign subtypes
- abstract Sign
- type Positive <: Sign end
- type Negative <: Sign end
- type NoSign <: Sign end
-
257 -(v::Vexity) = v
9 -(v::ConcaveVexity) = ConvexVexity()
- -(v::ConvexVexity) = ConcaveVexity()
-
- -(m::Monotonicity) = m
- -(m::Nonincreasing) = Nondecreasing()
- -(m::Nondecreasing) = Nonincreasing()
-
4 -(s::Sign) = s
- -(s::Positive) = Negative()
- -(s::Negative) = Positive()
-
- +(v::NotDcp, w::NotDcp) = v
- +(v::NotDcp, w::Vexity) = v
- +(v::Vexity, w::NotDcp) = w
-
69 +(v::ConstVexity, w::ConstVexity) = v
- +(v::ConstVexity, w::NotDcp) = w
- +(v::NotDcp, w::ConstVexity) = v
403 +(v::ConstVexity, w::Vexity) = w
186 +(v::Vexity, w::ConstVexity) = v
-
155 +(v::AffineVexity, w::AffineVexity) = v
3 +(v::AffineVexity, w::ConvexVexity) = w
57 +(v::ConvexVexity, w::AffineVexity) = v
2 +(v::AffineVexity, w::ConcaveVexity) = w
12 +(v::ConcaveVexity, w::AffineVexity) = v
-
8 +(v::ConvexVexity, w::ConvexVexity) = v
2 +(v::ConcaveVexity, w::ConcaveVexity) = v
- +(v::ConcaveVexity, w::ConvexVexity) = NotDcp()
- +(v::ConvexVexity, w::ConcaveVexity) = NotDcp()
-
- +(s::Positive, t::Positive) = s
- +(s::Negative, t::Negative) = s
- +(s::Positive, t::Negative) = NoSign()
- +(s::Negative, t::Positive) = NoSign()
1 +(s::NoSign, t::NoSign) = s
4 +(s::NoSign, t::Sign) = s
- +(s::Sign, t::NoSign) = t
-
1 *(s::NoSign, t::NoSign) = s
- *(s::NoSign, t::Sign) = s
18 *(s::Sign, t::NoSign) = t
- *(s::Positive, t::Positive) = s
- *(s::Positive, t::Negative) = t
- *(s::Negative, t::Positive) = s
- *(s::Negative, t::Negative) = Positive()
-
91 *(s::Positive, m::Monotonicity) = m
1 *(s::Negative, m::Monotonicity) = -m
89 *(s::NoSign, m::Monotonicity) = NoMonotonicity()
12 *(m::Monotonicity, s::Sign) = s * m
-
372 *(m::Nondecreasing, v::Vexity) = v
21 *(m::Nonincreasing, v::Vexity) = -v
101 *(m::NoMonotonicity, v::Vexity) = v
- *(m::NoMonotonicity, v::ConvexVexity) = NotDcp()
- *(m::NoMonotonicity, v::ConcaveVexity) = NotDcp()
-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment