Skip to content

Commit 2461789

Browse files
authored
Merge pull request #156 from JuliaSymbolics/myb/int
Use _isinteger check in power rules
2 parents e00284a + 19dcae1 commit 2461789

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "0.6.2"
4+
version = "0.6.3"
55

66
[deps]
77
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"

src/simplify_rules.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ let
3535

3636

3737
POW_RULES = [
38-
@rule(^(*(~~x), ~y::isliteral(Integer)) => *(map(a->pow(a, ~y), ~~x)...))
39-
@rule((((~x)^(~p::isliteral(Integer)))^(~q::isliteral(Integer))) => (~x)^((~p)*(~q)))
38+
@rule(^(*(~~x), ~y::_isinteger) => *(map(a->pow(a, ~y), ~~x)...))
39+
@rule((((~x)^(~p::_isinteger))^(~q::_isinteger)) => (~x)^((~p)*(~q)))
4040
@rule(^(~x, ~z::_iszero) => 1)
4141
@rule(^(~x, ~z::_isone) => ~x)
4242
@rule(inv(~x) => ~x ^ -1)

src/utils.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ is_operation(f) = @nospecialize(x) -> istree(x) && (operation(x) == f)
111111
isliteral(::Type{T}) where {T} = x -> x isa T
112112
is_literal_number(x) = isliteral(Number)(x)
113113

114-
_iszero(t) = false
115-
_iszero(x::Number) = iszero(x)
116-
_isone(t) = false
117-
_isone(x::Number) = isone(x)
114+
# checking the type directly is faster than dynamic dispatch in type unstable code
115+
_iszero(x) = x isa Number && iszero(x)
116+
_isone(x) = x isa Number && isone(x)
117+
_isinteger(x) = (x isa Number && isinteger(x)) || (x isa Symbolic && symtype(x) <: Integer)
118118

119119
issortedₑ(args) = issorted(args, lt=<ₑ)
120120
needs_sorting(f) = x -> is_operation(f)(x) && !issortedₑ(arguments(x))

test/rulesets.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ end
3838
@eqtest simplify(a + b + 0*c + d) == simplify(a + b + d)
3939
@eqtest simplify(a * b * c^0 * d) == simplify(a * b * d)
4040
@eqtest simplify(a * b * 1*c * d) == simplify(a * b * c * d)
41+
@eqtest simplify(x^2.0/(x*y)^2.0) == y ^ (-2.0)
4142

4243
@test simplify(Term(one, [a])) == 1
4344
@test simplify(Term(one, [b+1])) == 1

0 commit comments

Comments
 (0)