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

Fix error for printing in IJuliaMode #2420

Merged
merged 11 commits into from
Jan 5, 2021
35 changes: 30 additions & 5 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,37 @@ end
#------------------------------------------------------------------------
## _NonlinearExprData
#------------------------------------------------------------------------
function nl_expr_string(model::Model, mode, c::_NonlinearExprData)
return string(_tape_to_expr(model, 1, c.nd, adjmat(c.nd), c.const_values,
[], [], model.nlp_data.user_operators, false,
false, mode))
function nl_expr_string(model::Model, print_mode, c::_NonlinearExprData)
ex = _tape_to_expr(model, 1, c.nd, adjmat(c.nd), c.const_values,
[], [], model.nlp_data.user_operators, false,
false, print_mode)
if print_mode == IJuliaMode
ex = _latexify_exponentials(ex)
end
return string(ex)
end

# Change x ^ -2.0 to x ^ {-2.0}
# x ^ (x ^ 2.0) to x ^ {x ^ {2.0}}
# and so on
function _latexify_exponentials(ex::Expr)
if ex.head != :call
return ex
end
for i = 1:length(ex.args)
if isa(ex.args[i], Expr)
ex.args[i] = _latexify_exponentials(ex.args[i])
end
if ex.args[1] == :^
if isa(ex.args[3], Expr)
ex.args[3] = _latexify_exponentials(ex.args[3])
end
ex.args[3] = Expr(:braces, ex.args[3])
return ex
end
end
return ex
end

#------------------------------------------------------------------------
## _NonlinearConstraint
#------------------------------------------------------------------------
Expand Down
24 changes: 24 additions & 0 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,42 @@ end
constr_ge = @NLconstraint(model, sin(x) >= 1)
constr_eq = @NLconstraint(model, sin(x) == 1)
constr_range = @NLconstraint(model, 0 <= sin(x) <= 1)
constr_exponent_1 = @NLconstraint(model, x^4 <= 1)
constr_exponent_2 = @NLconstraint(model, x^40.23 <= 1)
constr_exponent_3 = @NLconstraint(model, x^4 + x^3 + x^2 <= 1)
constr_exponent_4 = @NLconstraint(model, x^-4 + x^-3 + x^-2 <= 1)
constr_exponent_5 = @NLconstraint(model, x^(x * x) <= 1)
constr_exponent_6 = @NLconstraint(model, x^(2x) <= 1)
constr_exponent_7 = @NLconstraint(model, x^(2 * 5) <= 1)
constr_exponent_8 = @NLconstraint(model, x^(x^2) <= 1)

io_test(REPLMode, constr_le, "sin(x) - 1.0 $le 0")
io_test(REPLMode, constr_ge, "sin(x) - 1.0 $ge 0")
io_test(REPLMode, constr_eq, "sin(x) - 1.0 $eq 0")
# Note: This is inconsistent with the "x in [-1, 1]" printing for
# regular constraints.
io_test(REPLMode, constr_range, "0 $le sin(x) $le 1")
io_test(REPLMode, constr_exponent_1, "x ^ 4.0 - 1.0 $le 0")
io_test(REPLMode, constr_exponent_2, "x ^ 40.23 - 1.0 $le 0")
io_test(REPLMode, constr_exponent_3, "(x ^ 4.0 + x ^ 3.0 + x ^ 2.0) - 1.0 $le 0")
io_test(REPLMode, constr_exponent_4, "(x ^ -4.0 + x ^ -3.0 + x ^ -2.0) - 1.0 $le 0")
io_test(REPLMode, constr_exponent_5, "x ^ (x * x) - 1.0 $le 0")
io_test(REPLMode, constr_exponent_6, "x ^ (2.0 * x) - 1.0 $le 0")
io_test(REPLMode, constr_exponent_7, "x ^ (2.0 * 5.0) - 1.0 $le 0")
io_test(REPLMode, constr_exponent_8, "x ^ (x ^ 2.0) - 1.0 $le 0")

io_test(IJuliaMode, constr_le, "sin(x) - 1.0 \\leq 0")
io_test(IJuliaMode, constr_ge, "sin(x) - 1.0 \\geq 0")
io_test(IJuliaMode, constr_eq, "sin(x) - 1.0 = 0")
io_test(IJuliaMode, constr_range, "0 \\leq sin(x) \\leq 1")
io_test(IJuliaMode, constr_exponent_1, "x ^ {4.0} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_2, "x ^ {40.23} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_3, "(x ^ {4.0} + x ^ {3.0} + x ^ {2.0}) - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_4, "(x ^ {-4.0} + x ^ {-3.0} + x ^ {-2.0}) - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_5, "x ^ {x * x} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_6, "x ^ {2.0 * x} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_7, "x ^ {2.0 * 5.0} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_8, "x ^ {x ^ {2.0}} - 1.0 \\leq 0")
end

@testset "Nonlinear constraints with embedded parameters/expressions" begin
Expand Down