Skip to content

Commit

Permalink
make number methods defined only on Symbolic{<:Number}
Browse files Browse the repository at this point in the history
Co-authored-by: "Yingbo Ma" <[email protected]>
  • Loading branch information
shashi and YingboMa committed Mar 29, 2021
1 parent ddef8dc commit f100b5e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 5 additions & 5 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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)
3 changes: 1 addition & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f100b5e

Please sign in to comment.