Skip to content

Commit 9f5298c

Browse files
authored
Merge pull request #515 from JuliaSymbolics/s/fix-infinite-loop
apply arithmetic operations in basic_similarterm only if binary
2 parents 224e752 + a3ed38b commit 9f5298c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/types.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,9 @@ function basic_similarterm(t, f, args, stype; metadata=nothing)
532532
if T === nothing
533533
T = _promote_symtype(f, args)
534534
end
535-
if stype <: Number && (f in (+, *) || (f in (/, ^) && length(args) == 2)) && all(x->symtype(x) <: Number, args)
535+
if T <: LiteralReal
536+
Term{T}(f, args, metadata=metadata)
537+
elseif stype <: Number && (f in (+, *) || (f in (/, ^) && length(args) == 2)) && all(x->symtype(x) <: Number, args)
536538
res = f(args...)
537539
if res isa Symbolic
538540
@set! res.metadata = metadata

test/rulesets.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ end
5454
@test simplify(Term(zero, [x + 2])) == 0
5555
end
5656

57+
@testset "LiteralReal" begin
58+
@syms x1::LiteralReal x2::LiteralReal
59+
s = cos(x1 * 3.2) - x2 * 5.8 + x2 * 1.2
60+
@eqtest s == cos(x1 * 3.2) - x2 * 5.8 + x2 * 1.2
61+
62+
# Prevents automatic simplification:
63+
@eqtest s != cos(3.2(x1^1)) - 4.6x2
64+
65+
# However, manual simplification should still work:
66+
@eqtest simplify(s) == simplify(cos(3.2x1) - 4.6x2)
67+
end
68+
5769
@testset "boolean" begin
5870
@syms a::Real b c
5971

0 commit comments

Comments
 (0)