From 0a0ac35e410fd216e76a3305eeb190d10c099bb0 Mon Sep 17 00:00:00 2001 From: john verzani Date: Sat, 30 May 2020 10:13:55 -0400 Subject: [PATCH] close #351; issue with Bool type and arithmetic (#352) * close #351; issue with Bool type and arithmetic --- Project.toml | 2 +- src/mathops.jl | 13 +++++++++++++ test/tests.jl | 9 +++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 4a348d36..4fa315c0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "SymPy" uuid = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6" -version = "1.0.22" +version = "1.0.23" [deps] diff --git a/src/mathops.jl b/src/mathops.jl index 9ee18a17..5b8e3bd6 100644 --- a/src/mathops.jl +++ b/src/mathops.jl @@ -19,3 +19,16 @@ \(x::SymbolicObject, y::SymbolicObject) = (y'/x')' # ? inv(x::Sym) = x.__pow__(Sym(-1)) + +# special case Boolean; issue 351 +# promotion for Boolean here is to 0 or 1, not False, True ++(x::Bool, y::SymbolicObject) = Sym(Int(x)).__add__(y) ++(x::SymbolicObject, y::Bool) = x.__add__(Int(y)) +*(x::Bool, y::SymbolicObject) = Sym(Int(x)).__mul__(y) +*(x::SymbolicObject, y::Bool) = x.__mul__(Int(y)) +-(x::Bool, y::SymbolicObject) = Sym(Int(x)).__sub__(y) +-(x::SymbolicObject, y::Bool) = x.__sub__(Int(y)) +/(x::Bool, y::SymbolicObject) = Sym(Int(x)).__div__(y) +/(x::SymbolicObject, y::Bool) = x.__div__(Int(y)) +^(x::Bool, y::SymbolicObject) = Sym(Int(x)).__pow__(y) +^(x::SymbolicObject, y::Bool) = x.__pow__(Int(y)) diff --git a/test/tests.jl b/test/tests.jl index 8e8a0c6b..71649686 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -608,7 +608,7 @@ end @test eltype(A*A) == Sym @test eltype(A*ones(2,2)) == Sym @test eltype(A*Diagonal([1,1])) == Sym - @test_broken eltype(A * I(2)) == Sym + VERSION >= v"1.2.0" && @test eltype(A * I(2)) == Sym ## Issue 328 with E -> e @vars x @@ -654,6 +654,11 @@ end @test lambdify(PI^4*xreal)(256) == 256 * pi^4 - + ## Issue 351 booleans and arithmetic operations + @test 1 + true == 2 == true + 1 + @test 1 - true == 0 == true - 1 + @test 1 * true == 1 == true * 1 + @test 1/true == 1 == true/1 + @test true^1 == 1 == 1^true end