JuliaSymbolics / SymbolicUtils.jl

Symbolic expressions, rewriting and simplification
https://docs.sciml.ai/SymbolicUtils/stable/
Other
536 stars 107 forks source link

Fix printing of binary operators with 1 arg #375

Closed MasonProtter closed 1 year ago

MasonProtter commented 2 years ago

Before this fix:

julia> SymbolicUtils.show_call(stdout, (-), 1)
1

After this fix:

julia> SymbolicUtils.show_call(stdout, (-), 1)
-(1)
codecov-commenter commented 2 years ago

Codecov Report

Merging #375 (c88d3df) into master (48cc5ff) will decrease coverage by 0.33%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #375      +/-   ##
==========================================
- Coverage   83.70%   83.36%   -0.34%     
==========================================
  Files          12       12              
  Lines        1479     1479              
==========================================
- Hits         1238     1233       -5     
- Misses        241      246       +5     
Impacted Files Coverage Δ
src/types.jl 84.99% <100.00%> (ø)
src/polyform.jl 92.76% <0.00%> (-1.36%) :arrow_down:
src/ordering.jl 89.53% <0.00%> (-1.17%) :arrow_down:
src/code.jl 80.55% <0.00%> (-0.56%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 48cc5ff...c88d3df. Read the comment docs.

MasonProtter commented 2 years ago

Hm, this seems to have somehow broken the printing of more complicated expressions for me, etc.

julia> 1 + -1*σ1
1 - (-((-σ1)^1))

:(

Still not great, but way better than before (its at least mathematically correct). This is just happening because of the way the printing is assuming constructor level simplification.


using SymbolicUtils
using SymbolicUtils.Rewriters
using SymbolicUtils: Sym, Term, Symbolic, sym_isa
SymbolicUtils.show_simplified[] = false

struct Clifford end

@syms σ1::Clifford

for op ∈ (:*, :+, :-, :^, :/)
    @eval begin
        Base.$op(x::Symbolic{Clifford}, y::Symbolic{Clifford}) = Term{Clifford}($op, [x, y])
        Base.$op(x, y::Symbolic{Clifford}) = Term{Clifford}($op, [x, y])
        Base.$op(x::Symbolic{Clifford}, y) = Term{Clifford}($op, [x, y])

        Base.$op(x::Number, y::Symbolic{Clifford}) = Term{Clifford}($op, [x, y])
        Base.$op(x::Symbolic{Clifford}, y::Number) = Term{Clifford}($op, [x, y])
    end
end
shashi commented 1 year ago

Seems to have gotten fixed over time.