From 73b584f92ab37c7a5aabc0d49a9f93a83806f5c3 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Tue, 29 Oct 2024 14:41:04 +0100 Subject: [PATCH] Try to simplify numbers with integer values to integers --- src/simplify.jl | 2 +- src/simplify_rules.jl | 5 +++++ test/rulesets.jl | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/simplify.jl b/src/simplify.jl index 68fe78f83..12d766eeb 100644 --- a/src/simplify.jl +++ b/src/simplify.jl @@ -39,7 +39,7 @@ function simplify(x; end x = PassThrough(f)(x) - simplify_fractions && has_operation(x, /) ? + x = simplify_fractions && has_operation(x, /) ? SymbolicUtils.simplify_fractions(x) : x end diff --git a/src/simplify_rules.jl b/src/simplify_rules.jl index a612036cb..df1cc812b 100644 --- a/src/simplify_rules.jl +++ b/src/simplify_rules.jl @@ -101,6 +101,10 @@ let @rule(exp(~x)^(~y) => exp(~x * ~y)) ] + TYPE_RULES = [ + @rule(~x::_isinteger => Int(~x)) + ] + BOOLEAN_RULES = [ @rule((true | (~x)) => true) @rule(((~x) | true) => true) @@ -142,6 +146,7 @@ let If(x -> !ispow(x) && is_operation(^)(x), Chain(CANONICALIZE_POW)), If(is_operation(^), Chain(POW_RULES)), + Chain(TYPE_RULES) ] |> RestartedChain rule_tree diff --git a/test/rulesets.jl b/test/rulesets.jl index cfbc0143e..1f1153120 100644 --- a/test/rulesets.jl +++ b/test/rulesets.jl @@ -57,6 +57,14 @@ end @test simplify(Term(zero, [x + 2])) == 0 end +@testset "Types" begin + @syms x + + # should convert to int + @test simplify(7.0) === 7 + @test simplify(7.0*x) === 7*x +end + @testset "LiteralReal" begin @syms x1::LiteralReal x2::LiteralReal s = cos(x1 * 3.2) - x2 * 5.8 + x2 * 1.2