From 75c3df80d944c1a73f2a29bb15079da31b998a1b Mon Sep 17 00:00:00 2001 From: jverzani Date: Sun, 2 Mar 2025 17:45:21 -0500 Subject: [PATCH 1/2] more methods for real, imag --- src/numerics.jl | 21 ++++++++++++++++++--- test/runtests.jl | 9 +++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/numerics.jl b/src/numerics.jl index 421acbc..d32d223 100644 --- a/src/numerics.jl +++ b/src/numerics.jl @@ -169,16 +169,31 @@ as_numer_denom(x::BasicType) = as_numer_denom(Basic(x)) denominator(x::SymbolicType) = as_numer_denom(x)[2] numerator(x::SymbolicType) = as_numer_denom(x)[1] -## Complex +## Complex; real, imag for :Complex defined elsewhere via ccall +# MethodError if x not a number type. real(x::Basic) = Basic(real(SymEngine.BasicType(x))) -real(x::SymEngine.BasicType) = x +real(x::BasicType{Val{:Integer}}) = x +real(x::BasicType{Val{:RealDouble}}) = x +real(x::BasicType{Val{:RealMPFR}}) = x +real(x::BasicType{Val{:Rational}}) = x +function real(x::BasicType{Val{:Constant}}) + any(==(x), (PI, E, EulerGamma, Catalan, oo, NAN)) && return Basic(x) + x == IM && return zero(x) + x == zoo && return oo +end + imag(x::Basic) = Basic(imag(SymEngine.BasicType(x))) imag(x::BasicType{Val{:Integer}}) = Basic(0) imag(x::BasicType{Val{:RealDouble}}) = Basic(0) imag(x::BasicType{Val{:RealMPFR}}) = Basic(0) imag(x::BasicType{Val{:Rational}}) = Basic(0) -imag(x::SymEngine.BasicType) = throw(InexactError()) +function imag(x::BasicType{Val{:Constant}}) + any(==(x), (PI, E, EulerGamma, Catalan, oo, NAN)) && return zero(x) + x == IM && return one(x) + x == zoo && return oo +end + # Because of the definitions above, `real(x) == x` for `x::Basic` # such as `x = symbols("x")`. Thus, it is consistent to define the diff --git a/test/runtests.jl b/test/runtests.jl index 9855ce9..b2ca7f1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -285,6 +285,15 @@ expr = x^3 + 3*x^2*y + 3*x*y^2 + y^3 + 1 end end +@test real(Basic(1.5)) == 1.5 +@test isa(real(2 + 3IM), Basic) +@test real(2 + 3IM) == 2 + +@test imag(Basic(1.5)) == 0 +@test isa(imag(2 + 3IM), Basic) +@test imag(2 + 3IM) == Basic(3) + + @test round(Basic(3.14)) == 3.0 @test round(Basic(3.14); digits=1) == 3.1 From 17c2ee6abbec34b4b042acb5de256b397bec1ab8 Mon Sep 17 00:00:00 2001 From: jverzani Date: Mon, 3 Mar 2025 07:42:56 -0500 Subject: [PATCH 2/2] keep fallback --- src/numerics.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/numerics.jl b/src/numerics.jl index d32d223..9bb67fe 100644 --- a/src/numerics.jl +++ b/src/numerics.jl @@ -172,6 +172,7 @@ numerator(x::SymbolicType) = as_numer_denom(x)[1] ## Complex; real, imag for :Complex defined elsewhere via ccall # MethodError if x not a number type. real(x::Basic) = Basic(real(SymEngine.BasicType(x))) +real(x::BasicType{Val{:Symbol}}) = x # issue #273 has issue here real(x::BasicType{Val{:Integer}}) = x real(x::BasicType{Val{:RealDouble}}) = x real(x::BasicType{Val{:RealMPFR}}) = x