Skip to content

Commit 7c1139b

Browse files
authored
[FileFormats.NL] fix try_scalar_affine_function (#2766)
1 parent a53fedb commit 7c1139b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/FileFormats/NL/read.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,15 @@ _try_scalar_affine_function(x::MOI.VariableIndex) = x
271271
function _try_scalar_affine_function(expr::Expr)
272272
if expr.args[1] == :+
273273
args = _try_scalar_affine_function.(expr.args[2:end])
274-
if !any(isnothing, args)
275-
return MOI.Utilities.operate(+, Float64, args...)
274+
if any(isnothing, args)
275+
return nothing
276276
end
277+
return MOI.Utilities.operate(+, Float64, args...)
277278
elseif expr.args[1] == :*
278279
args = _try_scalar_affine_function.(expr.args[2:end])
280+
if any(isnothing, args)
281+
return nothing
282+
end
279283
n_affine_terms = 0
280284
for arg in args
281285
n_affine_terms += arg isa MOI.VariableIndex

test/FileFormats/NL/read.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,29 @@ function test_binary_parse_S_Float64()
10661066
return
10671067
end
10681068

1069+
function test_try_scalar_affine_function()
1070+
compare(::Nothing, ::Nothing) = true
1071+
compare(x::T, y::T) where {T} = isapprox(x, y)
1072+
compare(x, y) = (@show(x, y, typeof(x), typeof(y)); false)
1073+
x = MOI.VariableIndex(1)
1074+
for (expr, ret) in Any[
1075+
:(2.0)=>2.0,
1076+
:($x)=>x,
1077+
:(2.0*$x)=>2.0*x,
1078+
:($x*2.0)=>2.0*x,
1079+
:(($x+$x))=>2.0*x,
1080+
:(2.0*($x+$x))=>4.0*x,
1081+
:(($x+$x)*2.0)=>4.0*x,
1082+
:(($x+$x)+2.0)=>2.0*x+2.0,
1083+
:(sin($x)*($x+$x))=>nothing,
1084+
:(($x+$x)*sin($x))=>nothing,
1085+
:($x*$x)=>nothing,
1086+
]
1087+
@test compare(MOI.FileFormats.NL._try_scalar_affine_function(expr), ret)
1088+
end
1089+
return
1090+
end
1091+
10691092
end
10701093

10711094
TestNonlinearRead.runtests()

0 commit comments

Comments
 (0)