Skip to content

Commit

Permalink
Merge pull request #106 from tkoolen/tk/fix-static-vecdot
Browse files Browse the repository at this point in the history
 Add zero for Variable, LinearTerm, QuadraticTerm
  • Loading branch information
tkoolen authored Jun 14, 2019
2 parents 68c49f7 + 4778a1c commit 4766fa1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,9 @@ LinearAlgebra.dot(x::ParametronFunctions, y::ParametronFunctions) = x * y
LinearAlgebra.dot(x::ParametronFunctions, y::Number) = x * y
LinearAlgebra.dot(x::Number, y::ParametronFunctions) = x * y
Base.to_power_type(x::ParametronFunctions) = x # TODO: remove once https://github.com/JuliaLang/julia/issues/24151 is fixed
Base.zero(x::Variable) = 0 * x
Base.zero(x::LinearTerm) = zero(x.coeff) * x.var
Base.zero(x::QuadraticTerm) = zero(x.coeff) * x.rowvar * x.colvar
Base.zero(::T) where {T<:ParametronFunctions} = zero(T)
Base.one(::T) where {T<:ParametronFunctions} = one(T)
Base.adjoint(x::ParametronFunctions) = x
Expand Down
13 changes: 13 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,17 @@ end
@test x^2 * SVector(1, 2) === SVector(1, 2) * x^2 === SVector(x^2, 2 * x^2)
end

@testset "zero" begin
x = Variable(1)
@test zero(x) === LinearTerm(0, x)
@test zero(3.0 * x) === LinearTerm(0.0, x)
@test zero(4 * x * x) === QuadraticTerm(0, x, x)
end

@testset "SVector dot" begin
x = Variable(1)
y = Variable(2)
@test canonicalize(dot(SVector(x, y), SVector(x, y))) == x^2 + y^2
end

end # module

0 comments on commit 4766fa1

Please sign in to comment.