From b6316e4d6f711222e27a3a47c020327a96e8f9b6 Mon Sep 17 00:00:00 2001 From: john verzani Date: Tue, 16 Aug 2022 13:47:58 -0400 Subject: [PATCH] close issue #477 (#478) --- src/mathops.jl | 46 +++++++++++++++++++++++----------------------- test/tests.jl | 8 ++++++++ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/mathops.jl b/src/mathops.jl index e6c0f53..1a4172e 100644 --- a/src/mathops.jl +++ b/src/mathops.jl @@ -6,31 +6,31 @@ -+(x::SymbolicObject, y::SymbolicObject) = x.__add__(y) -*(x::SymbolicObject, y::SymbolicObject) = x.__mul__(y) --(x::SymbolicObject, y::SymbolicObject) = x.__sub__(y) --(x::SymbolicObject) = x.__neg__() -/(x::SymbolicObject, y::SymbolicObject) = x.__truediv__(y) -^(x::SymbolicObject, y::SymbolicObject) = x.__pow__(y) -^(x::SymbolicObject, y::Rational) = x^convert(Sym,y) -#^(x::SymbolicObject, y::Integer) = x^convert(Sym,y) # no Union{Integer, Rational}, as that has ambiguity -//(x::SymbolicObject, y::Int) = x / Sym(y) -//(x::SymbolicObject, y::Rational) = x / Sym(y) -//(x::SymbolicObject, y::SymbolicObject) = x / y - -\(x::SymbolicObject, y::SymbolicObject) = (y'/x')' # ? ++(x::Sym, y::Sym)::Sym = x.__add__(y) +*(x::Sym, y::Sym)::Sym = x.__mul__(y) +-(x::Sym, y::Sym)::Sym = x.__sub__(y) +(-)(x::Sym)::Sym = x.__neg__() +/(x::Sym, y::Sym)::Sym = x.__truediv__(y) +^(x::Sym, y::Sym)::Sym = x.__pow__(y) +^(x::Sym, y::Rational) = x^convert(Sym,y) +#^(x::Sym, y::Integer) = x^convert(Sym,y) # no Union{Integer, Rational}, as that has ambiguity +//(x::Sym, y::Int) = x / Sym(y) +//(x::Sym, y::Rational) = x / Sym(y) +//(x::Sym, y::Sym) = x / y + +\(x::Sym, y::Sym) = (y'/x')' # ? Base.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::Bool, y::SymbolicObject) = Sym(Int(x)).__mul__(y) --(x::Bool, y::SymbolicObject) = Sym(Int(x)).__sub__(y) -/(x::Bool, y::SymbolicObject) = Sym(Int(x)).__truediv__(y) -^(x::Bool, y::SymbolicObject) = Sym(Int(x)).__pow__(y) -+(x::SymbolicObject, y::Bool) = x.__add__(Int(y)) -*(x::SymbolicObject, y::Bool) = x.__mul__(Int(y)) --(x::SymbolicObject, y::Bool) = x.__sub__(Int(y)) -/(x::SymbolicObject, y::Bool) = x.__truediv__(Int(y)) -^(x::SymbolicObject, y::Bool) = x.__pow__(Int(y)) ++(x::Bool, y::Sym)::Sym = Sym(Int(x)).__add__(y) +*(x::Bool, y::Sym)::Sym = Sym(Int(x)).__mul__(y) +-(x::Bool, y::Sym)::Sym = Sym(Int(x)).__sub__(y) +/(x::Bool, y::Sym)::Sym = Sym(Int(x)).__truediv__(y) +^(x::Bool, y::Sym)::Sym = Sym(Int(x)).__pow__(y) ++(x::Sym, y::Bool)::Sym = x.__add__(Int(y)) +*(x::Sym, y::Bool)::Sym = x.__mul__(Int(y)) +-(x::Sym, y::Bool)::Sym = x.__sub__(Int(y)) +/(x::Sym, y::Bool)::Sym = x.__truediv__(Int(y)) +^(x::Sym, y::Bool)::Sym = x.__pow__(Int(y)) diff --git a/test/tests.jl b/test/tests.jl index 8424a9c..8f147b3 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -777,6 +777,14 @@ end @syms x ex = integrate(sqrt(1 + (1/x)^2), (x, 1/sympy.E, sympy.E)) @test N(ex) ≈ 3.196198513599507 + + ## Issue #477 non typestable ops + @test Base.Broadcast.combine_eltypes(+, (zero(Sym), zero(Sym))) == Sym + @test Base.Broadcast.combine_eltypes(-, (zero(Sym), zero(Sym))) == Sym + @test Base.Broadcast.combine_eltypes(*, (zero(Sym), zero(Sym))) == Sym + @test Base.Broadcast.combine_eltypes(/, (zero(Sym), zero(Sym))) == Sym + @test Base.Broadcast.combine_eltypes(^, (zero(Sym), zero(Sym))) == Sym + end @testset "generic programming, issue 223" begin