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 printing for term operators that could be both unary and binary #513

Merged
merged 2 commits into from
Mar 24, 2023

Conversation

bowenszhu
Copy link
Member

Fix #512

julia> VERSION
v"1.8.5"

(@v1.8) pkg> st -m Symbolics
Status `~/.julia/environments/v1.8/Manifest.toml`
  [0c5d862f] Symbolics v5.0.2

(@v1.8) pkg> st -m SymbolicUtils
Status `~/.julia/environments/v1.8/Manifest.toml`
  [d1185830] SymbolicUtils v1.0.3

julia> using Symbolics

julia> @variables y::LiteralReal
1-element Vector{Num}:
 y

julia> -y
y

A minus sign is missing in the printing result above.

The internal structure of -y is

julia> SymbolicUtils.isterm(Symbolics.value(-y))
true

julia> operation(Symbolics.value(-y))
- (generic function with 507 methods)

julia> arguments(Symbolics.value(-y))
1-element Vector{Any}:
 y

The printing function dispatched for -y is the following one:

function show_call(io, f, args)
fname = istree(f) ? Symbol(repr(f)) : nameof(f)
binary = Base.isbinaryoperator(fname)
if binary
for (i, t) in enumerate(args)
i != 1 && print(io, " $fname ")
print_arg(io, t, paren=true)
end
else
if issym(f)
Base.show_unquoted(io, nameof(f))
else
Base.show(io, f)
end
print(io, "(")
for i=1:length(args)
print(io, args[i])
i != length(args) && print(io, ", ")
end
print(io, ")")
end
end

Here, the function show_call checks if the operation is binary.

However, the - operator is special which could be both unary and binary. show_call doesn't consider the unary case and thus doesn't work in our test case.

This PR resolves this issue.

With this PR, the printing result is corrected

julia> using Symbolics

julia> @variables y::LiteralReal;

julia> -y
-y

@shashi
Copy link
Member

shashi commented Mar 17, 2023

@bowenszhu Hey sorry, can you rebase this? 😅

@shashi shashi merged commit cda830f into master Mar 24, 2023
@shashi shashi deleted the b/fix-unary-binary-operator-printing branch March 24, 2023 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LiteralReal printing issue
2 participants