Skip to content

[Utilities] fix operate(vcat for VectorNonlinearFunction #2682

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

Merged
merged 4 commits into from
Mar 6, 2025
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
10 changes: 9 additions & 1 deletion src/Utilities/operate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,15 @@
append!(out, scalarize(a))
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to copy if it's the output of scalarize, I would rather copy in the two push! above

Copy link
Member Author

Choose a reason for hiding this comment

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

You don't need to copy if it's the output of scalarize

Ah but you do if you hit this method MOI.Utilities.scalarize(f::MOI.VectorNonlinearFunction) = f.rows.

The question of whether scalarize and vectorize are mutable hasn't really come up before, because they always produced unique objects.

Copy link
Member

Choose a reason for hiding this comment

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

😨

end
end
return MOI.VectorNonlinearFunction(out)
_to_new_snf(f::MOI.ScalarNonlinearFunction) = copy(f)
_to_new_snf(f) = convert(MOI.ScalarNonlinearFunction, f)

Check warning on line 995 in src/Utilities/operate.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/operate.jl#L994-L995

Added lines #L994 - L995 were not covered by tests
# We need to `copy` the ScalarNonlinearFunction rows here, everything else
# will be `convert(ScalarNonlinearFunction, row)` into a new object, but the
# ScalarNonlinearFunction rows won't be, so mutating the return value of
# this `operate` method will mutate the inputs. This _is_ what Base.vcat
# does, but it doesn't fit with the general assumption that
# `Utilities.operate(` returns a new object.
return MOI.VectorNonlinearFunction(_to_new_snf.(out))

Check warning on line 1002 in src/Utilities/operate.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/operate.jl#L1002

Added line #L1002 was not covered by tests
end

### 6a: operate(::typeof(imag), ::Type{T}, ::F)
Expand Down
18 changes: 18 additions & 0 deletions test/Utilities/test_operate!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,24 @@ function test_operate_5a()
return
end

function test_operate_5a_VectorNonlinearFunction()
x = MOI.ScalarNonlinearFunction(:+, Any[])
f = MOI.VectorNonlinearFunction([x])
g = MOI.Utilities.operate(vcat, Float64, f, f, x)
rows = [MOI.ScalarNonlinearFunction(:+, Any[]) for _ in 1:3]
h = MOI.VectorNonlinearFunction(rows)
@test g ≈ h
push!(x.args, 0.0)
@test g ≈ h
@test g.rows[1] !== g.rows[2]
@test g.rows[1] !== g.rows[3]
@test g.rows[2] !== g.rows[3]
rows = [MOI.ScalarNonlinearFunction(:+, Any[0.0])]
f_new = MOI.VectorNonlinearFunction(rows)
@test f ≈ f_new
return
end

function test_operate_6a()
T = Float64
@test MOI.Utilities.operate(imag, T, _test_function((0.0, 0.0, 0.0))) ≈
Expand Down
Loading