Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do zero checking to avoid unnecessary work in addToExpression #536

Merged
merged 1 commit into from
Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions src/v0.3/parseExpr_0.3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,25 @@ function timesvar(x::Expr)
return x.args[end]
end

function addToExpression{C,V}(aff::GenericAffExpr{C,V},c::Number,x::V)
push!(aff, convert(C,c), x)
aff
end

function addToExpression(aff::GenericAffExpr,c::Number,x::Number)
aff.constant += c*x
aff
end

function addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::V)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it me or is this method identical to the one above on line 31?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they're the same

push!(aff.vars, x)
push!(aff.coeffs, c)
if c != 0
push!(aff.vars, x)
push!(aff.coeffs, c)
end
aff
end

function addToExpression{C,V}(aff::GenericAffExpr{C,V},c::Number,x::GenericAffExpr{C,V})
append!(aff.vars, x.vars)
append!(aff.coeffs, c*x.coeffs)
aff.constant += c*x.constant
if c != 0
append!(aff.vars, x.vars)
append!(aff.coeffs, c*x.coeffs)
aff.constant += c*x.constant
end
aff
end

Expand All @@ -68,14 +67,21 @@ addToExpression{C}(aff::GenericAffExpr{C,Variable},c::Variable,x::GenericAffExpr
x.coeffs,
addToExpression(aff,x.constant,c))

addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::GenericQuadExpr{C,V}) =
GenericQuadExpr{C,V}(copy(x.qvars1),
copy(x.qvars2),
c*x.qcoeffs,
addToExpression(aff,c,x.aff))
function addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::GenericQuadExpr{C,V})
if c == 0
convert(GenericQuadExpr{C,V}, aff)
else
GenericQuadExpr{C,V}(copy(x.qvars1),
copy(x.qvars2),
c*x.qcoeffs,
addToExpression(aff,c,x.aff))
end
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::V)
push!(quad.aff,convert(C,c),x)
if c != 0
push!(quad.aff,convert(C,c),x)
end
quad
end

Expand All @@ -92,16 +98,20 @@ function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::V,x::V)
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::GenericAffExpr{C,V})
append!(quad.aff.vars, x.vars)
append!(quad.aff.coeffs, c*x.coeffs)
quad.aff.constant += c*x.constant
if c != 0
append!(quad.aff.vars, x.vars)
append!(quad.aff.coeffs, c*x.coeffs)
quad.aff.constant += c*x.constant
end
quad
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::GenericAffExpr{C,V},x::Number)
append!(quad.aff.vars, c.vars)
append!(quad.aff.coeffs, c.coeffs*x)
quad.aff.constant += c.constant*x
if x != 0
append!(quad.aff.vars, c.vars)
append!(quad.aff.coeffs, c.coeffs*x)
quad.aff.constant += c.constant*x
end
quad
end

Expand All @@ -123,18 +133,22 @@ function addToExpression{C}(quad::GenericQuadExpr{C,Variable},c::Variable,x::Gen
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::GenericQuadExpr{C,V},x::Number)
append!(quad.qvars1,c.qvars1)
append!(quad.qvars2,c.qvars2)
append!(quad.qcoeffs,c.qcoeffs*x)
addToExpression(quad,c.aff,x)
if x != 0
append!(quad.qvars1,c.qvars1)
append!(quad.qvars2,c.qvars2)
append!(quad.qcoeffs,c.qcoeffs*x)
addToExpression(quad,c.aff,x)
end
quad
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::GenericQuadExpr{C,V})
append!(quad.qvars1,x.qvars1)
append!(quad.qvars2,x.qvars2)
append!(quad.qcoeffs,c*x.qcoeffs)
addToExpression(quad,c,x.aff)
if c != 0
append!(quad.qvars1,x.qvars1)
append!(quad.qvars2,x.qvars2)
append!(quad.qcoeffs,c*x.qcoeffs)
addToExpression(quad,c,x.aff)
end
quad
end

Expand Down
153 changes: 95 additions & 58 deletions src/v0.4/parseExpr_staged.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ addToExpression(ex::Number, c::Number, x::Number) = ex + c*x

addToExpression(ex::Number, c::Number, x::Variable) = AffExpr([x],[c],ex)

function addToExpression(ex::Number, c::Number, x::GenericAffExpr)
function addToExpression{T<:GenericAffExpr}(ex::Number, c::Number, x::T)
# It's only safe to mutate the first argument.
x = copy(x)
scale!(x.coeffs, c)
x.constant *= c
x.constant += ex
x
if c == 0
T(ex)
else
x = copy(x)
scale!(x.coeffs, c)
x.constant *= c
x.constant += ex
x
end
end

function addToExpression(ex::Number, c::Number, x::GenericQuadExpr)
function addToExpression{T<:GenericQuadExpr}(ex::Number, c::Number, x::T)
# It's only safe to mutate the first argument.
x = copy(x)
scale!(x.qcoeffs, c)
scale!(x.aff.coeffs, c)
x.aff.constant *= c
x.aff.constant += ex
x
if c == 0
T(ex)
else
x = copy(x)
scale!(x.qcoeffs, c)
scale!(x.aff.coeffs, c)
x.aff.constant *= c
x.aff.constant += ex
x
end
end

addToExpression(ex::Number, c::Variable, x::Variable) = QuadExpr([c],[x],[1.0],zero(AffExpr))
Expand All @@ -42,10 +50,14 @@ function addToExpression{C,V}(ex::Number, c::GenericAffExpr{C,V}, x::V)
q
end

function addToExpression(ex::Number, c::GenericQuadExpr, x::Number)
q = c*x
q.aff.constant += ex
q
function addToExpression{T<:GenericQuadExpr}(ex::Number, c::T, x::Number)
if x == 0
T(ex)
else
q = c*x
q.aff.constant += ex
q
end
end

function addToExpression(aff::GenericAffExpr, c::Number, x::Number)
Expand All @@ -54,24 +66,27 @@ function addToExpression(aff::GenericAffExpr, c::Number, x::Number)
end

function addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::V)
push!(aff.vars, x)
push!(aff.coeffs, c)
if c != 0
push!(aff.vars, x)
push!(aff.coeffs, c)
end
aff
end

function addToExpression{C,V}(aff::GenericAffExpr{C,V},c::Number,x::GenericAffExpr{C,V})
append!(aff.vars, x.vars)
sizehint!(aff.coeffs, length(aff.coeffs)+length(x.coeffs))
for i in 1:length(x.coeffs)
push!(aff.coeffs, c*x.coeffs[i])
if c != 0
append!(aff.vars, x.vars)
sizehint!(aff.coeffs, length(aff.coeffs)+length(x.coeffs))
for i in 1:length(x.coeffs)
push!(aff.coeffs, c*x.coeffs[i])
end
aff.constant += c*x.constant
end
aff.constant += c*x.constant
aff
end

function addToExpression{C,V}(aff::GenericAffExpr{C,V},c::V,x::V)
addToExpression{C,V}(aff::GenericAffExpr{C,V},c::V,x::V) =
GenericQuadExpr{C,V}([c],[x],[one(C)],aff)
end

# TODO: add generic versions of following two methods
addToExpression(aff::AffExpr,c::AffExpr,x::Variable) =
Expand All @@ -87,23 +102,35 @@ addToExpression(aff::AffExpr,c::Variable,x::AffExpr) =
addToExpression(aff,c,x.constant))

function addToExpression{C,V}(aff::GenericAffExpr{C,V},c::GenericAffExpr{C,V},x::Number)
append!(aff.vars, c.vars)
append!(aff.coeffs, c.coeffs * x)
aff.constant += c.constant * x
if x != 0
append!(aff.vars, c.vars)
append!(aff.coeffs, c.coeffs * x)
aff.constant += c.constant * x
end
aff
end

addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::GenericQuadExpr{C,V}, x::Number) =
GenericQuadExpr{C,V}(copy(c.qvars1),
copy(c.qvars2),
c*qcoeffs*x,
addToExpression(aff,c.aff,x))
function addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::GenericQuadExpr{C,V}, x::Number)
if x == 0
GenericQuadExpr{C,V}(aff)
else
GenericQuadExpr{C,V}(copy(c.qvars1),
copy(c.qvars2),
c*qcoeffs*x,
addToExpression(aff,c.aff,x))
end
end

addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::GenericQuadExpr{C,V}) =
GenericQuadExpr{C,V}(copy(x.qvars1),
copy(x.qvars2),
c*x.qcoeffs,
addToExpression(aff,c,x.aff))
function addToExpression{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::GenericQuadExpr{C,V})
if c == 0
GenericQuadExpr{C,V}(aff)
else
GenericQuadExpr{C,V}(copy(x.qvars1),
copy(x.qvars2),
c*x.qcoeffs,
addToExpression(aff,c,x.aff))
end
end

function addToExpression{C,V}(ex::GenericAffExpr{C,V}, c::GenericAffExpr{C,V}, x::GenericAffExpr{C,V})
q = convert(GenericQuadExpr{C,V}, ex)
Expand All @@ -112,7 +139,9 @@ function addToExpression{C,V}(ex::GenericAffExpr{C,V}, c::GenericAffExpr{C,V}, x
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::V)
push!(quad.aff, convert(C,c), x)
if c != 0
push!(quad.aff, convert(C,c), x)
end
quad
end

Expand All @@ -129,17 +158,21 @@ function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::V,x::V)
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::GenericAffExpr{C,V})
append!(quad.aff.vars, x.vars)
sizehint!(quad.aff.coeffs, length(quad.aff.coeffs)+length(x.coeffs))
for i in 1:length(x.coeffs)
push!(quad.aff.coeffs, c*x.coeffs[i])
if c != 0
append!(quad.aff.vars, x.vars)
sizehint!(quad.aff.coeffs, length(quad.aff.coeffs)+length(x.coeffs))
for i in 1:length(x.coeffs)
push!(quad.aff.coeffs, c*x.coeffs[i])
end
quad.aff.constant += c*x.constant
end
quad.aff.constant += c*x.constant
quad
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::GenericAffExpr{C,V},x::Number)
addToExpression(quad.aff,c,x)
if x != 0
addToExpression(quad.aff,c,x)
end
quad
end

Expand All @@ -160,24 +193,28 @@ function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::V,x::GenericAffExpr{
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::GenericQuadExpr{C,V},x::Number)
append!(quad.qvars1,c.qvars1)
append!(quad.qvars2,c.qvars2)
sizehint!(quad.qcoeffs, length(quad.qcoeffs)+length(c.qcoeffs))
for i in 1:length(c.qcoeffs)
push!(quad.qcoeffs, c.qcoeffs[i]*x)
if x != 0
append!(quad.qvars1,c.qvars1)
append!(quad.qvars2,c.qvars2)
sizehint!(quad.qcoeffs, length(quad.qcoeffs)+length(c.qcoeffs))
for i in 1:length(c.qcoeffs)
push!(quad.qcoeffs, c.qcoeffs[i]*x)
end
addToExpression(quad,c.aff,x)
end
addToExpression(quad,c.aff,x)
quad
end

function addToExpression{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::GenericQuadExpr{C,V})
append!(quad.qvars1,x.qvars1)
append!(quad.qvars2,x.qvars2)
sizehint!(quad.qcoeffs, length(quad.qcoeffs)+length(x.qcoeffs))
for i in 1:length(x.qcoeffs)
push!(quad.qcoeffs, c*x.qcoeffs[i])
if c != 0
append!(quad.qvars1,x.qvars1)
append!(quad.qvars2,x.qvars2)
sizehint!(quad.qcoeffs, length(quad.qcoeffs)+length(x.qcoeffs))
for i in 1:length(x.qcoeffs)
push!(quad.qcoeffs, c*x.qcoeffs[i])
end
addToExpression(quad,c,x.aff)
end
addToExpression(quad,c,x.aff)
quad
end

Expand Down
8 changes: 8 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,11 @@ facts("[macros] Norm parsing") do
@fact_throws @addConstraint(model, (x[1,1]+1)*norm2{x[i,j], i=1:2, j=1:2} + x[1,2] >= -1)
@fact_throws @addConstraint(model, norm2{x[i,j], i=1:2, j=1:2} + x[1,2] >= -1)
end

facts("[macros] Extraneous terms in QuadExpr (#535)") do
model = Model()
@defVar(model, x)
@defVar(model, y)
@addConstraint(model, x*x <= y*y)
@fact conToStr(model.quadconstr[1]) --> "x² - y² $leq 0"
end