Skip to content

Commit e06bafd

Browse files
committed
[Utilities] fix operate(vcat for VectorNonlinearFunction
1 parent 927c58b commit e06bafd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Utilities/operate.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,11 @@ function operate(
991991
append!(out, scalarize(a))
992992
end
993993
end
994-
return MOI.VectorNonlinearFunction(out)
994+
# We need to `copy` the rows hre, because if `a` are mutable
995+
# AbstractScalarFunction then mutating the return value will mutate the
996+
# inputs. This _is_ what Base.vcat does, but it doesn't fit with the general
997+
# assumption that `Utilities.operate(` returns a new object.
998+
return MOI.VectorNonlinearFunction(copy.(out))
995999
end
9961000

9971001
### 6a: operate(::typeof(imag), ::Type{T}, ::F)

test/Utilities/test_operate!.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,26 @@ function test_operate_5a()
399399
return
400400
end
401401

402+
function test_operate_5a_VectorNonlinearFunction()
403+
x = MOI.ScalarNonlinearFunction(:+, Any[])
404+
f = MOI.VectorNonlinearFunction([x])
405+
g = MOI.Utilities.operate(vcat, Float64, f, f, x)
406+
h = MOI.VectorNonlinearFunction(
407+
[MOI.ScalarNonlinearFunction(:+, Any[]) for _ in 1:3],
408+
)
409+
@test g h
410+
push!(x.args, 0.0)
411+
@test g h
412+
@test g.rows[1] !== g.rows[2]
413+
@test g.rows[1] !== g.rows[3]
414+
@test g.rows[2] !== g.rows[3]
415+
f_new = MOI.VectorNonlinearFunction(
416+
[MOI.ScalarNonlinearFunction(:+, Any[0.0])],
417+
)
418+
@test f f_new
419+
return
420+
end
421+
402422
function test_operate_6a()
403423
T = Float64
404424
@test MOI.Utilities.operate(imag, T, _test_function((0.0, 0.0, 0.0)))

0 commit comments

Comments
 (0)