Skip to content

Commit 0a0ac35

Browse files
authored
close #351; issue with Bool type and arithmetic (#352)
* close #351; issue with Bool type and arithmetic
1 parent 6f51a94 commit 0a0ac35

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "SymPy"
22
uuid = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
3-
version = "1.0.22"
3+
version = "1.0.23"
44

55

66
[deps]

src/mathops.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@
1919
\(x::SymbolicObject, y::SymbolicObject) = (y'/x')' # ?
2020

2121
inv(x::Sym) = x.__pow__(Sym(-1))
22+
23+
# special case Boolean; issue 351
24+
# promotion for Boolean here is to 0 or 1, not False, True
25+
+(x::Bool, y::SymbolicObject) = Sym(Int(x)).__add__(y)
26+
+(x::SymbolicObject, y::Bool) = x.__add__(Int(y))
27+
*(x::Bool, y::SymbolicObject) = Sym(Int(x)).__mul__(y)
28+
*(x::SymbolicObject, y::Bool) = x.__mul__(Int(y))
29+
-(x::Bool, y::SymbolicObject) = Sym(Int(x)).__sub__(y)
30+
-(x::SymbolicObject, y::Bool) = x.__sub__(Int(y))
31+
/(x::Bool, y::SymbolicObject) = Sym(Int(x)).__div__(y)
32+
/(x::SymbolicObject, y::Bool) = x.__div__(Int(y))
33+
^(x::Bool, y::SymbolicObject) = Sym(Int(x)).__pow__(y)
34+
^(x::SymbolicObject, y::Bool) = x.__pow__(Int(y))

test/tests.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ end
608608
@test eltype(A*A) == Sym
609609
@test eltype(A*ones(2,2)) == Sym
610610
@test eltype(A*Diagonal([1,1])) == Sym
611-
@test_broken eltype(A * I(2)) == Sym
611+
VERSION >= v"1.2.0" && @test eltype(A * I(2)) == Sym
612612

613613
## Issue 328 with E -> e
614614
@vars x
@@ -654,6 +654,11 @@ end
654654
@test lambdify(PI^4*xreal)(256) == 256 * pi^4
655655

656656

657-
657+
## Issue 351 booleans and arithmetic operations
658+
@test 1 + true == 2 == true + 1
659+
@test 1 - true == 0 == true - 1
660+
@test 1 * true == 1 == true * 1
661+
@test 1/true == 1 == true/1
662+
@test true^1 == 1 == 1^true
658663

659664
end

0 commit comments

Comments
 (0)