Skip to content

Commit c91806f

Browse files
authored
Merge pull request #306 from JuliaSymbolics/myb/div
Preserve rational if possible
2 parents 0112c2c + 1b56117 commit c91806f

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/types.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ struct FnType{X<:Tuple,Y} end
200200

201201
function (f::Symbolic)(args...)
202202
error("Sym $f is not callable. " *
203-
"Use @syms $f(var1, var2,...) to create it as a callable. " *
204-
"See ?@fun for more options")
203+
"Use @syms $f(var1, var2,...) to create it as a callable.")
205204
end
206205

207206
"""
@@ -494,7 +493,7 @@ end
494493
function show_mul(io, args)
495494
length(args) == 1 && return print_arg(io, *, args[1])
496495

497-
paren_scalar = args[1] isa Complex || args[1] isa Rational || (args[1] isa Number && !isfinite(args[1]))
496+
paren_scalar = (args[1] isa Complex && !_iszero(imag(args[1]))) || args[1] isa Rational || (args[1] isa Number && !isfinite(args[1]))
498497
minus = args[1] isa Number && args[1] == -1
499498
unit = args[1] isa Number && args[1] == 1
500499
nostar = !paren_scalar && args[1] isa Number && !(args[2] isa Number)
@@ -831,7 +830,7 @@ end
831830

832831
\(a::Number, b::SN) = b / a
833832

834-
/(a::SN, b::Number) = inv(b) * a
833+
/(a::SN, b::Number) = (b isa Integer ? 1//b : inv(b)) * a
835834

836835
//(a::Union{SN, Number}, b::SN) = a / b
837836

test/basics.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ end
123123
@test isequal(w == 0, Term{Bool}(==, [w, 0]))
124124

125125
@eqtest x // 5 == (1 // 5) * x
126+
@eqtest (1//2 * x) / 5 == (1 // 10) * x
126127
@eqtest x // Int16(5) == Rational{Int16}(1, 5) * x
127128
@eqtest 5 // x == 5 / x
128129
@eqtest x // a == x / a

test/fuzz.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ seed!(6174)
66
@testset "Fuzz test" begin
77
@time @testset "expand fuzz" begin
88
for i=1:500
9+
i % 100 == 0 && @info "expand fuzz" iter=i
910
fuzz_test(5, num_spec, SymbolicUtils.expand; min_depth=3)
1011
end
1112
end
1213
@time @testset "num fuzz" begin
1314
for i=1:1500
15+
i % 100 == 0 && @info "num fuzz" iter=i
1416
fuzz_test(5, num_spec)
1517
end
1618
end
1719
@time @testset "bool fuzz" begin
1820
for i=1:500
1921
seed!(i)
22+
i % 100 == 0 && @info "bool fuzz" iter=i
2023
fuzz_test(5, bool_spec)
2124
end
2225
end

0 commit comments

Comments
 (0)