Skip to content

Commit

Permalink
Merge bd5cca1 into 942a771
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Nov 7, 2018
2 parents 942a771 + bd5cca1 commit 044c8b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
33 changes: 13 additions & 20 deletions src/atoms/affine/dot.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import LinearAlgebra.dot
export vecdot, dot
export dot

ismatrix(x::AbstractExpr) = (s = size(x); length(s) == 2 && !any(==(1), s))
ismatrix(x::AbstractMatrix) = true
ismatrix(x) = false

vecdot(x::AbstractExpr, y::AbstractExpr) = sum(broadcast(*, x, y))
vecdot(x::Value, y::AbstractExpr) = sum(broadcast(*, Constant(x), y))
vecdot(x::AbstractExpr, y::Value) = sum(broadcast(*, x, Constant(y)))
asvec(x) = ismatrix(x) ? convert(AbstractExpr, vec(x)) : convert(AbstractExpr, x)

dot(x::AbstractExpr, y::AbstractExpr) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y)
dot(x::Value, y::AbstractExpr) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y)
dot(x::AbstractExpr, y::Value) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y)
dot(x::AbstractExpr, y::AbstractExpr) = sum(broadcast(*, asvec(x), asvec(y)))
dot(x::Value, y::AbstractExpr) = sum(broadcast(*, asvec(x), asvec(y)))
dot(x::AbstractExpr, y::Value) = sum(broadcast(*, asvec(x), asvec(y)))

# tests if an array is a matrix (2D array) with both dimensions of size > 1
function ismatrix(x)
sz = size(x)
if length(sz) != 2
return false
else
for s in sz
if s == 1
return false
end
end
end
return true
if isdefined(LinearAlgebra, :vecdot) # defined but deprecated
import LinearAlgebra: vecdot
end
Base.@deprecate vecdot(x::AbstractExpr, y::AbstractExpr) dot(x, y)
Base.@deprecate vecdot(x::Value, y::AbstractExpr) dot(x, y)
Base.@deprecate vecdot(x::AbstractExpr, y::Value) dot(x, y)
6 changes: 3 additions & 3 deletions test/test_affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ eye(n) = Matrix(1.0I, n, n)
@test (evaluate(dot([2.0; 2.0], x)))[1] 4.4 atol=TOL
end

@testset "vecdot atom" begin
@testset "dot atom for matrix variables" begin
x = Variable(2,2)
p = minimize(vecdot(fill(2.0, (2,2)), x), x >= 1.1)
p = minimize(dot(fill(2.0, (2,2)), x), x >= 1.1)
@test vexity(p) == AffineVexity()
solve!(p)
@test p.optval 8.8 atol=TOL
@test (evaluate(vecdot(fill(2.0, (2, 2)), x)))[1] 8.8 atol=TOL
@test (evaluate(dot(fill(2.0, (2, 2)), x)))[1] 8.8 atol=TOL
end

@testset "add atom" begin
Expand Down

0 comments on commit 044c8b5

Please sign in to comment.