jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.17k stars 390 forks source link

function_string of Symmetric matrix includes the entire matrix #3764

Closed LebedevRI closed 3 weeks ago

LebedevRI commented 1 month ago
using JuMP
import LinearAlgebra
import MathOptInterface as MOI

function main()
    model = Model() 

    @variable(model, v[1:3,1:3], Symmetric)

    @constraint(model, c0, LinearAlgebra.Symmetric(v.-1)  == LinearAlgebra.Symmetric(fill(41, 3,3))) # Yay!
    @show(c0) # Uhh, why is it not symmetric?
    print(c0) # Uhh, why is it not symmetric?
    display(c0) # Oh, but it is symmetric here.
    print(model) # And it is symmetric here too.
end

main()
c0 = c0 : [v[1,1] - 42  v[1,2] - 42  v[1,3] - 42;
 v[1,2] - 42  v[2,2] - 42  v[2,3] - 42; ∈ Zeros()
 v[1,3] - 42  v[2,3] - 42  v[3,3] - 42]

image This is even worse for text-only output, looks like only pretty-printing does it right.

odow commented 1 month ago

text/latex has a special check for Symmetric. We don't have the check in text/plain. But that's also because we defer the printing to Base.show, so I'm not sure this is easy to fix. https://github.com/jump-dev/JuMP.jl/blob/56b186fc18d854be33a7f558bc2ba1918dd7c86d/src/print.jl#L884-L919

In the REPL, Julia prints the full symmetric matrix:

julia> using LinearAlgebra

julia> Symmetric([1 2; 2 3])
2×2 Symmetric{Int64, Matrix{Int64}}:
 1  2
 2  3
LebedevRI commented 1 month ago

In the REPL, Julia prints the full symmetric matrix:

Right, i just saw that too. I thought it didn't, but apparently it does.

odow commented 1 month ago

Arguably someone might interpret out current text/latex printing as the upper triangular, with zeros in the lower.

Here's what UpperTriangular prints.

julia> UpperTriangular([1 2; 2 3])
2×2 UpperTriangular{Int64, Matrix{Int64}}:
 1  2
 ⋅  3

I don't even know if the current text/latex approach is a good idea.

LebedevRI commented 1 month ago

IMHO, the text/latex approach is generally the right way to do it. Perhaps a different symbol should be used?

odow commented 3 weeks ago

Thoughts @blegat? I would err on the side of sticking to Base.

odow commented 3 weeks ago

See https://github.com/jump-dev/JuMP.jl/pull/3768

LebedevRI commented 3 weeks ago

@odow thank you!