diff --git a/Project.toml b/Project.toml index b320a97de..095763957 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SymbolicUtils" uuid = "d1185830-fcd6-423d-90d6-eec64667417b" authors = ["Shashi Gowda"] -version = "0.6.2" +version = "0.6.3" [deps] AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d" diff --git a/src/simplify_rules.jl b/src/simplify_rules.jl index ee6bac6d8..aaef7ac19 100644 --- a/src/simplify_rules.jl +++ b/src/simplify_rules.jl @@ -35,8 +35,8 @@ let POW_RULES = [ - @rule(^(*(~~x), ~y::isliteral(Integer)) => *(map(a->pow(a, ~y), ~~x)...)) - @rule((((~x)^(~p::isliteral(Integer)))^(~q::isliteral(Integer))) => (~x)^((~p)*(~q))) + @rule(^(*(~~x), ~y::_isinteger) => *(map(a->pow(a, ~y), ~~x)...)) + @rule((((~x)^(~p::_isinteger))^(~q::_isinteger)) => (~x)^((~p)*(~q))) @rule(^(~x, ~z::_iszero) => 1) @rule(^(~x, ~z::_isone) => ~x) @rule(inv(~x) => ~x ^ -1) diff --git a/src/utils.jl b/src/utils.jl index fe936feee..a67723f0b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -111,10 +111,10 @@ is_operation(f) = @nospecialize(x) -> istree(x) && (operation(x) == f) isliteral(::Type{T}) where {T} = x -> x isa T is_literal_number(x) = isliteral(Number)(x) -_iszero(t) = false -_iszero(x::Number) = iszero(x) -_isone(t) = false -_isone(x::Number) = isone(x) +# checking the type directly is faster than dynamic dispatch in type unstable code +_iszero(x) = x isa Number && iszero(x) +_isone(x) = x isa Number && isone(x) +_isinteger(x) = (x isa Number && isinteger(x)) || (x isa Symbolic && symtype(x) <: Integer) issortedâ‚‘(args) = issorted(args, lt=<â‚‘) needs_sorting(f) = x -> is_operation(f)(x) && !issortedâ‚‘(arguments(x)) diff --git a/test/rulesets.jl b/test/rulesets.jl index e4484d583..163dfcb45 100644 --- a/test/rulesets.jl +++ b/test/rulesets.jl @@ -38,6 +38,7 @@ end @eqtest simplify(a + b + 0*c + d) == simplify(a + b + d) @eqtest simplify(a * b * c^0 * d) == simplify(a * b * d) @eqtest simplify(a * b * 1*c * d) == simplify(a * b * c * d) + @eqtest simplify(x^2.0/(x*y)^2.0) == y ^ (-2.0) @test simplify(Term(one, [a])) == 1 @test simplify(Term(one, [b+1])) == 1