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

[breaking] scalar row indexing produces column vector #624

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ W = ComplexVariable(n, n);
objective = real(sum(diag(W)));
c1 = Constraint[];
for i in 2:n
push!(c1, sum(W[i, :] .* (Y[i, :]')) == inj[i])
push!(c1, sum(W[i, :] .* conj(Y[i, :])) == inj[i])
Copy link
Member

Choose a reason for hiding this comment

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

Ooooofff. This is pretty subtle...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch! Btw maybe this could be dot? It used to be bugged in the complex case so maybe the original author didn’t use it

Copy link
Member

Choose a reason for hiding this comment

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

Updated to use dot. Yeah, I assume the previous code was just trial and error to find what worked.

end
c2 = isposdef(W)
c3 = real(W[1, 1]) == 1.06^2;
Expand Down
28 changes: 16 additions & 12 deletions src/atoms/IndexAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ mutable struct IndexAtom <: AbstractExpr
rows::Union{AbstractArray,Nothing}
cols::Union{AbstractArray,Nothing}
inds::Union{AbstractArray,Nothing}
end

function IndexAtom(
x::AbstractExpr,
rows::AbstractArray,
cols::AbstractArray,
)
return new((x,), (length(rows), length(cols)), rows, cols, nothing)
end
function IndexAtom(x::AbstractExpr, rows::AbstractArray, cols::AbstractArray)
return IndexAtom((x,), (length(rows), length(cols)), rows, cols, nothing)
end

function IndexAtom(x::AbstractExpr, inds::AbstractArray)
return new((x,), (length(inds), 1), nothing, nothing, inds)
end
function IndexAtom(x::AbstractExpr, inds::AbstractArray)
return IndexAtom((x,), (length(inds), 1), nothing, nothing, inds)
end
odow marked this conversation as resolved.
Show resolved Hide resolved

head(io::IO, ::IndexAtom) = print(io, "index")
Expand All @@ -33,7 +29,11 @@ curvature(::IndexAtom) = ConstVexity()

function evaluate(x::IndexAtom)
result = if x.inds === nothing
getindex(evaluate(x.children[1]), x.rows, x.cols)
# reshape to ensure we are respecting that a scalar row index
# creates a column vector. We can't just check `length(x.rows)`
# since that doesn't distinguish between `i` and `i:i`. But
# we had that info when we set the size, so we will use it now.
reshape(getindex(evaluate(x.children[1]), x.rows, x.cols), x.size)
else
getindex(evaluate(x.children[1]), x.inds)
end
Expand Down Expand Up @@ -98,7 +98,11 @@ function Base.getindex(x::AbstractExpr, row::Real, col::Real)
end

function Base.getindex(x::AbstractExpr, row::Real, cols::AbstractVector{<:Real})
return getindex(x, row:row, cols)
# In this case, we must construct a column vector
# https://github.com/jump-dev/Convex.jl/issues/509
# Here we construct `getindex(x, row:row, cols)`
# except with the size set to be that of a column vector
return IndexAtom((x,), (length(cols), 1), row:row, cols, nothing)
end

function Base.getindex(x::AbstractExpr, rows::AbstractVector{<:Real}, col::Real)
Expand Down
18 changes: 18 additions & 0 deletions src/problem_depot/problems/affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,24 @@ end
@test p.optval ≈ 147 atol = atol rtol = rtol
end

# *Scalar* indexing creates a column vector
ericphanson marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/jump-dev/Convex.jl/issues/509
x = rand(2, 3)
ϕ = Variable(2, 3)
Convex.set_value!(ϕ, rand(2, 3))
i = j = 1
@test size(ϕ[i, :]) == (3, 1) # "column-vector" in Convex.jl is a 1 column matrix
@test size(ϕ[i, 1:2]) == (2, 1)
@test size(ϕ[i, [2, 1]]) == (2, 1)

@test size(ϕ[i:i, :]) == (1, 3) # non-scalar: row-vector
@test size(evaluate(ϕ[i, :])) == (3,) # evaluates to true column vector
@test size(evaluate(ϕ[i:i, :])) == (1, 3) # row vector

# "Column vector", not 3x3 matrix!
@test size(evaluate(broadcast(*, ϕ[i, :] - ϕ[j, :], x[i, :] - x[j, :]))) ==
(3, 1)

x = Variable(2)
p = minimize(x[1] + x[2], [x >= 1]; numeric_type = T)

Expand Down
Loading