Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion src/Utilities/operate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,11 @@
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)
# We need to `copy` the rows hre, because if `a` are mutable
# AbstractScalarFunction then mutating the return value 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(copy.(out))

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

View check run for this annotation

Codecov / codecov/patch

src/Utilities/operate.jl#L998

Added line #L998 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