From f100b5ed7b307ad5b7f2608af0e43fd6d5e55b84 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Tue, 30 Mar 2021 01:05:42 +0530 Subject: [PATCH] make number methods defined only on Symbolic{<:Number} Co-authored-by: "Yingbo Ma" --- Project.toml | 2 +- src/methods.jl | 10 +++++----- src/types.jl | 3 +-- test/basics.jl | 7 +++++++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index ea9efbcfb..d98ba79d8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SymbolicUtils" uuid = "d1185830-fcd6-423d-90d6-eec64667417b" authors = ["Shashi Gowda"] -version = "0.9.4" +version = "0.10.0" [deps] AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d" diff --git a/src/methods.jl b/src/methods.jl index 6dc358513..b8dc91f95 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -94,14 +94,14 @@ for f in [+, -, *, \, /, ^] end promote_symtype(::typeof(rem2pi), T::Type{<:Number}, mode) = T -Base.rem2pi(x::Symbolic, mode::Base.RoundingMode) = term(rem2pi, x, mode) +Base.rem2pi(x::Symbolic{<:Number}, mode::Base.RoundingMode) = term(rem2pi, x, mode) for f in monadic if f in [real] continue end @eval promote_symtype(::$(typeof(f)), T::Type{<:Number}) = promote_type(T, Real) - @eval (::$(typeof(f)))(a::Symbolic) = term($f, a) + @eval (::$(typeof(f)))(a::Symbolic{<:Number}) = term($f, a) end Base.:*(a::AbstractArray, b::Symbolic{<:Number}) = map(x->x*b, a) @@ -112,7 +112,7 @@ for f in [identity, one, zero, *, +, -] end promote_symtype(::typeof(Base.real), T::Type{<:Number}) = Real -Base.real(s::Symbolic) = islike(s, Real) ? s : term(real, s) +Base.real(s::Symbolic{<:Number}) = islike(s, Real) ? s : term(real, s) ## Booleans @@ -148,5 +148,5 @@ promote_symtype(::typeof(ifelse), _, ::Type{T}, ::Type{S}) where {T,S} = Union{T Base.@deprecate cond(_if, _then, _else) ifelse(_if, _then, _else) # Specially handle inv and literal pow -Base.inv(x::Symbolic) = Base.:^(x, -1) -Base.literal_pow(::typeof(^), x::Symbolic, ::Val{p}) where {p} = Base.:^(x, p) +Base.inv(x::Symbolic{<:Number}) = Base.:^(x, -1) +Base.literal_pow(::typeof(^), x::Symbolic{<:Number}, ::Val{p}) where {p} = Base.:^(x, p) diff --git a/src/types.jl b/src/types.jl index c288cea16..98cc50eaa 100644 --- a/src/types.jl +++ b/src/types.jl @@ -543,8 +543,7 @@ showraw(t) = showraw(stdout, t) sdict(kv...) = Dict{Any, Number}(kv...) -# this cannot be Symbolic{<:Number} to make MTK Parameters work. See #155 -const SN = Symbolic +const SN = Symbolic{<:Number} """ Add(T, coeff, dict::Dict) diff --git a/test/basics.jl b/test/basics.jl index 50f3c1f4f..a1d396415 100644 --- a/test/basics.jl +++ b/test/basics.jl @@ -174,3 +174,10 @@ end @test hash(a + b, UInt(0)) === hash(a + b) === hash(a + b, UInt(0)) # test caching @test hash(a + b, UInt(2)) !== hash(a + b) end + +@testset "methoderror" begin + @syms a::Any b::Any + + @test_throws MethodError a * b + @test_throws MethodError a + b +end