Skip to content

Commit f100b5e

Browse files
shashiYingboMa
andcommitted
make number methods defined only on Symbolic{<:Number}
Co-authored-by: "Yingbo Ma" <[email protected]>
1 parent ddef8dc commit f100b5e

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "0.9.4"
4+
version = "0.10.0"
55

66
[deps]
77
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"

src/methods.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ for f in [+, -, *, \, /, ^]
9494
end
9595

9696
promote_symtype(::typeof(rem2pi), T::Type{<:Number}, mode) = T
97-
Base.rem2pi(x::Symbolic, mode::Base.RoundingMode) = term(rem2pi, x, mode)
97+
Base.rem2pi(x::Symbolic{<:Number}, mode::Base.RoundingMode) = term(rem2pi, x, mode)
9898

9999
for f in monadic
100100
if f in [real]
101101
continue
102102
end
103103
@eval promote_symtype(::$(typeof(f)), T::Type{<:Number}) = promote_type(T, Real)
104-
@eval (::$(typeof(f)))(a::Symbolic) = term($f, a)
104+
@eval (::$(typeof(f)))(a::Symbolic{<:Number}) = term($f, a)
105105
end
106106

107107
Base.:*(a::AbstractArray, b::Symbolic{<:Number}) = map(x->x*b, a)
@@ -112,7 +112,7 @@ for f in [identity, one, zero, *, +, -]
112112
end
113113

114114
promote_symtype(::typeof(Base.real), T::Type{<:Number}) = Real
115-
Base.real(s::Symbolic) = islike(s, Real) ? s : term(real, s)
115+
Base.real(s::Symbolic{<:Number}) = islike(s, Real) ? s : term(real, s)
116116

117117
## Booleans
118118

@@ -148,5 +148,5 @@ promote_symtype(::typeof(ifelse), _, ::Type{T}, ::Type{S}) where {T,S} = Union{T
148148
Base.@deprecate cond(_if, _then, _else) ifelse(_if, _then, _else)
149149

150150
# Specially handle inv and literal pow
151-
Base.inv(x::Symbolic) = Base.:^(x, -1)
152-
Base.literal_pow(::typeof(^), x::Symbolic, ::Val{p}) where {p} = Base.:^(x, p)
151+
Base.inv(x::Symbolic{<:Number}) = Base.:^(x, -1)
152+
Base.literal_pow(::typeof(^), x::Symbolic{<:Number}, ::Val{p}) where {p} = Base.:^(x, p)

src/types.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,7 @@ showraw(t) = showraw(stdout, t)
543543

544544
sdict(kv...) = Dict{Any, Number}(kv...)
545545

546-
# this cannot be Symbolic{<:Number} to make MTK Parameters work. See #155
547-
const SN = Symbolic
546+
const SN = Symbolic{<:Number}
548547
"""
549548
Add(T, coeff, dict::Dict)
550549

test/basics.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,10 @@ end
174174
@test hash(a + b, UInt(0)) === hash(a + b) === hash(a + b, UInt(0)) # test caching
175175
@test hash(a + b, UInt(2)) !== hash(a + b)
176176
end
177+
178+
@testset "methoderror" begin
179+
@syms a::Any b::Any
180+
181+
@test_throws MethodError a * b
182+
@test_throws MethodError a + b
183+
end

0 commit comments

Comments
 (0)