Skip to content

Commit 3ee7ae4

Browse files
committed
Remove vecdot
The `vecdot` function was deprecated in favor of `dot` in Julia PR 27401; `dot` on matrices now treats them like vectors. The change here updates our extension of `dot` to match this behavior and deprecates `vecnorm`.
1 parent ec26062 commit 3ee7ae4

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

src/atoms/affine/dot.jl

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import LinearAlgebra.dot
2-
export vecdot, dot
2+
export dot
33

4+
ismatrix(x::AbstractExpr) = (s = size(x); length(s) == 2 && s[1] > 1 && s[2] > 1)
5+
ismatrix(::AbstractMatrix) = true
6+
ismatrix(::Any) = false
47

5-
vecdot(x::AbstractExpr, y::AbstractExpr) = sum(broadcast(*, x, y))
6-
vecdot(x::Value, y::AbstractExpr) = sum(broadcast(*, Constant(x), y))
7-
vecdot(x::AbstractExpr, y::Value) = sum(broadcast(*, x, Constant(y)))
8+
# NOTE: Using asvec avoids broadcast-specific behaviors that we want to avoid, such
9+
# as extending singleton dimensions. We need to ensure that the inputs have the same
10+
# length, which broadcast will check for us if both inputs are vectors.
11+
asvec(x) = convert(AbstractExpr, ismatrix(x) ? vec(x) : x)
12+
_vecdot(x, y) = sum(broadcast(*, asvec(x), asvec(y)))
813

9-
dot(x::AbstractExpr, y::AbstractExpr) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y)
10-
dot(x::Value, y::AbstractExpr) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y)
11-
dot(x::AbstractExpr, y::Value) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y)
14+
dot(x::AbstractExpr, y::AbstractExpr) = _vecdot(x, y)
15+
dot(x::Value, y::AbstractExpr) = _vecdot(x, y)
16+
dot(x::AbstractExpr, y::Value) = _vecdot(x, y)
1217

13-
# tests if an array is a matrix (2D array) with both dimensions of size > 1
14-
function ismatrix(x)
15-
sz = size(x)
16-
if length(sz) != 2
17-
return false
18-
else
19-
for s in sz
20-
if s == 1
21-
return false
22-
end
23-
end
24-
end
25-
return true
18+
if isdefined(LinearAlgebra, :vecdot) # defined but deprecated
19+
import LinearAlgebra: vecdot
2620
end
21+
Base.@deprecate vecdot(x::AbstractExpr, y::AbstractExpr) dot(x, y)
22+
Base.@deprecate vecdot(x::Value, y::AbstractExpr) dot(x, y)
23+
Base.@deprecate vecdot(x::AbstractExpr, y::Value) dot(x, y)

test/test_affine.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ eye(n) = Matrix(1.0I, n, n)
6060
@test (evaluate(dot([2.0; 2.0], x)))[1] 4.4 atol=TOL
6161
end
6262

63-
@testset "vecdot atom" begin
63+
@testset "dot atom for matrix variables" begin
6464
x = Variable(2,2)
65-
p = minimize(vecdot(fill(2.0, (2,2)), x), x >= 1.1)
65+
p = minimize(dot(fill(2.0, (2,2)), x), x >= 1.1)
6666
@test vexity(p) == AffineVexity()
6767
solve!(p)
6868
@test p.optval 8.8 atol=TOL
69-
@test (evaluate(vecdot(fill(2.0, (2, 2)), x)))[1] 8.8 atol=TOL
69+
@test (evaluate(dot(fill(2.0, (2, 2)), x)))[1] 8.8 atol=TOL
7070
end
7171

7272
@testset "add atom" begin

0 commit comments

Comments
 (0)