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

Remove vecdot #245

Merged
merged 1 commit into from
Dec 4, 2018
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
37 changes: 17 additions & 20 deletions src/atoms/affine/dot.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import LinearAlgebra.dot
export vecdot, dot
export dot

ismatrix(x::AbstractExpr) = (s = size(x); length(s) == 2 && s[1] > 1 && s[2] > 1)
ismatrix(::AbstractMatrix) = true
ismatrix(::Any) = 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)))
# NOTE: Using asvec avoids broadcast-specific behaviors that we want to avoid, such
# as extending singleton dimensions. We need to ensure that the inputs have the same
# length, which broadcast will check for us if both inputs are vectors.
asvec(x) = convert(AbstractExpr, ismatrix(x) ? vec(x) : x)
_vecdot(x, y) = sum(broadcast(*, asvec(x), asvec(y)))

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) = _vecdot(x, y)
dot(x::Value, y::AbstractExpr) = _vecdot(x, y)
dot(x::AbstractExpr, y::Value) = _vecdot(x, 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