Skip to content

Commit 3306eb0

Browse files
authored
Merge pull request #81 from JuliaSymbolics/s/one-zero
Simplify one and zero terms
2 parents e3a7b5d + a38142d commit 3306eb0

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/methods.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ for f in [+, *]
5454
end
5555
end
5656

57-
for f in [identity, one, *, +]
57+
for f in [identity, one, zero, *, +]
5858
@eval promote_symtype(::$(typeof(f)), T::Type{<:Number}) = T
5959
end
6060

src/rulesets.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const ASSORTED_RULES = RuleSet([
5656
@rule(-(~x) => -1*~x)
5757
@rule(-(~x, ~y) => ~x + -1(~y))
5858
@rule(~x / ~y => ~x * pow(~y, -1))
59+
@rule(one(~x) => one(symtype(~x)))
60+
@rule(zero(~x) => zero(symtype(~x)))
5961
@rule(cond(~x::isnumber, ~y, ~z) => ~x ? ~y : ~z)
6062
])
6163

test/fuzzlib.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const num_spec = let
3737
binops = SymbolicUtils.diadic
3838
nopow = filter(x->x!==(^), binops)
3939
twoargfns = vcat(nopow, (x,y)->x isa Union{Int, Rational, Complex{<:Rational}} ? x * y : x^y)
40-
fns = vcat(1 .=> SymbolicUtils.monadic,
40+
fns = vcat(1 .=> vcat(SymbolicUtils.monadic, [one, zero]),
4141
2 .=> vcat(twoargfns, fill(+, 5), [-,-], fill(*, 5)),
4242
3 .=> [+, *])
4343

@@ -141,9 +141,16 @@ function fuzz_test(ntrials, spec)
141141
@test typeof(simplified.err) == typeof(unsimplified.err)
142142
else
143143
try
144-
@test unsimplified simplified
145-
if !(unsimplified simplified)
146-
error("Failed")
144+
if isnan(unsimplified)
145+
@test isnan(simplified)
146+
if !isnan(simplified)
147+
error("Failed")
148+
end
149+
else
150+
@test unsimplified simplified
151+
if !(unsimplified simplified)
152+
error("Failed")
153+
end
147154
end
148155
catch err
149156
println("""Test failed for expression

test/rulesets.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ end
3939
@eqtest simplify(a + b + 0*c + d) == a + b + d
4040
@eqtest simplify(a * b * c^0 * d) == a * b * d
4141
@eqtest simplify(a * b * 1*c * d) == a * b * c * d
42+
43+
@test simplify(Term(one, [a])) == 1
44+
@test simplify(Term(one, [b+1])) == 1
45+
@test simplify(Term(one, [x+2])) == 1
46+
47+
48+
@test simplify(Term(zero, [a])) == 0
49+
@test simplify(Term(zero, [b+1])) == 0
50+
@test simplify(Term(zero, [x+2])) == 0
4251
end
4352

4453
@testset "boolean" begin

0 commit comments

Comments
 (0)