Skip to content

Commit

Permalink
Try to simplify numbers with integer values to integers
Browse files Browse the repository at this point in the history
  • Loading branch information
hersle committed Oct 29, 2024
1 parent f9b0ade commit 73b584f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/simplify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions src/simplify_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/rulesets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 73b584f

Please sign in to comment.