From 5e3143c5e1d97f865916fc143f5fe7f250ab570c Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Fri, 29 Dec 2023 11:40:31 -0500 Subject: [PATCH 1/4] change binomial registration --- src/extra_functions.jl | 2 +- test/overloads.jl | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/extra_functions.jl b/src/extra_functions.jl index 4f934ae2f..277f9a984 100644 --- a/src/extra_functions.jl +++ b/src/extra_functions.jl @@ -1,4 +1,4 @@ -@register_symbolic Base.binomial(n, k::Integer)::Int true +@register_symbolic Base.binomial(n, k)::Int true [Integer, Real] @register_symbolic Base.sign(x)::Int derivative(::typeof(sign), args::NTuple{1,Any}, ::Val{1}) = 0 diff --git a/test/overloads.jl b/test/overloads.jl index a32b39c54..84fcc3ecd 100644 --- a/test/overloads.jl +++ b/test/overloads.jl @@ -237,3 +237,18 @@ for f in [<, <=, >, >=, isless] end @test_nowarn binomial(t, 1) + +# test for https://github.com/JuliaSymbolics/Symbolics.jl/issues/1028 +let + @variables t A(t) B + @test try binomial(A, 2*B^2) + true + catch + false + end + @test try binomial(Symbolics.value(A), Symbolics.value(2*B^2)) + true + catch + false + end +end From 9c4706e5d0137b930eaca627ad26a9033671c6e2 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Sat, 30 Dec 2023 16:42:23 -0500 Subject: [PATCH 2/4] add more dispatches --- src/extra_functions.jl | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/extra_functions.jl b/src/extra_functions.jl index 277f9a984..35ae54e17 100644 --- a/src/extra_functions.jl +++ b/src/extra_functions.jl @@ -1,4 +1,57 @@ -@register_symbolic Base.binomial(n, k)::Int true [Integer, Real] +@register_symbolic Base.binomial(n, k)::Int true +function var"Base.binomial_17156291908497421557"(nothing, n, k) + begin + args = [n, k] + unwrapped_args = map(Symbolics.unwrap, args) + res = if !(any((x->begin + (SymbolicUtils.issym)(x) || (SymbolicUtils.istree)(x) + end), unwrapped_args)) + Base.binomial(unwrapped_args...) + else + (SymbolicUtils.Term){Int}(Base.binomial, unwrapped_args) + end + if typeof.(args) == typeof.(unwrapped_args) + return res + else + return (Symbolics.wrap)(res) + end + end +end +function Base.binomial(n::Symbolics.SymbolicUtils.Symbolic{<:Real}, k::Int64) + if any(Symbolics.iswrapped, (n, k)) + (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) + else + var"Base.binomial_17156291908497421557"(nothing, n, k) + end +end +function Base.binomial(n::Num, k::Int64) + if any(Symbolics.iswrapped, (n, k)) + (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) + else + var"Base.binomial_17156291908497421557"(nothing, n, k) + end +end +function Base.binomial(n::Real, k::Symbolics.SymbolicUtils.Symbolic{<:Int64}) + if any(Symbolics.iswrapped, (n, k)) + (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) + else + var"Base.binomial_17156291908497421557"(nothing, n, k) + end +end +function Base.binomial(n::Symbolics.SymbolicUtils.Symbolic{<:Real}, k::Symbolics.SymbolicUtils.Symbolic{<:Int64}) + if any(Symbolics.iswrapped, (n, k)) + (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) + else + var"Base.binomial_17156291908497421557"(nothing, n, k) + end +end +function Base.binomial(n::Num, k::Symbolics.SymbolicUtils.Symbolic{<:Int64}) + if any(Symbolics.iswrapped, (n, k)) + (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) + else + var"Base.binomial_17156291908497421557"(nothing, n, k) + end +end @register_symbolic Base.sign(x)::Int derivative(::typeof(sign), args::NTuple{1,Any}, ::Val{1}) = 0 From cd8b290067c9c4902696b841c8994215cd2171a7 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 31 Dec 2023 08:55:03 -0500 Subject: [PATCH 3/4] Update extra_functions.jl --- src/extra_functions.jl | 54 +++++++++++++----------------------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/src/extra_functions.jl b/src/extra_functions.jl index 35ae54e17..0b2cb2cdd 100644 --- a/src/extra_functions.jl +++ b/src/extra_functions.jl @@ -1,55 +1,35 @@ @register_symbolic Base.binomial(n, k)::Int true -function var"Base.binomial_17156291908497421557"(nothing, n, k) +function _binomial(nothing, n, k) begin args = [n, k] unwrapped_args = map(Symbolics.unwrap, args) res = if !(any((x->begin - (SymbolicUtils.issym)(x) || (SymbolicUtils.istree)(x) + SymbolicUtils.issym(x) || SymbolicUtils.istree(x) end), unwrapped_args)) Base.binomial(unwrapped_args...) else - (SymbolicUtils.Term){Int}(Base.binomial, unwrapped_args) + SymbolicUtils.Term{Int}(Base.binomial, unwrapped_args) end if typeof.(args) == typeof.(unwrapped_args) return res else - return (Symbolics.wrap)(res) + return Symbolics.wrap(res) end end end -function Base.binomial(n::Symbolics.SymbolicUtils.Symbolic{<:Real}, k::Int64) - if any(Symbolics.iswrapped, (n, k)) - (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) - else - var"Base.binomial_17156291908497421557"(nothing, n, k) - end -end -function Base.binomial(n::Num, k::Int64) - if any(Symbolics.iswrapped, (n, k)) - (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) - else - var"Base.binomial_17156291908497421557"(nothing, n, k) - end -end -function Base.binomial(n::Real, k::Symbolics.SymbolicUtils.Symbolic{<:Int64}) - if any(Symbolics.iswrapped, (n, k)) - (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) - else - var"Base.binomial_17156291908497421557"(nothing, n, k) - end -end -function Base.binomial(n::Symbolics.SymbolicUtils.Symbolic{<:Real}, k::Symbolics.SymbolicUtils.Symbolic{<:Int64}) - if any(Symbolics.iswrapped, (n, k)) - (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) - else - var"Base.binomial_17156291908497421557"(nothing, n, k) - end -end -function Base.binomial(n::Num, k::Symbolics.SymbolicUtils.Symbolic{<:Int64}) - if any(Symbolics.iswrapped, (n, k)) - (Symbolics.wrap)(var"Base.binomial_17156291908497421557"(nothing, (Symbolics.unwrap)(n), (Symbolics.unwrap)(k))) - else - var"Base.binomial_17156291908497421557"(nothing, n, k) + +for (T1, T2) in ((Symbolics.SymbolicUtils.Symbolic{<:Real}, Int64), + (Num, Int64), + (Real, Symbolics.SymbolicUtils.Symbolic{<:Int64}), + (Symbolics.SymbolicUtils.Symbolic{<:Real}, Symbolics.SymbolicUtils.Symbolic{<:Int64}), + (Num, Symbolics.SymbolicUtils.Symbolic{<:Int64}) + + @eval function Base.binomial(n::$T1, k::$T2) + if any(Symbolics.iswrapped, (n, k)) + Symbolics.wrap(_binomial(nothing, Symbolics.unwrap(n), Symbolics.unwrap(k))) + else + _binomial(nothing, n, k) + end end end From bdf30c6473a185b86ea547ebe772ea115f2f8c68 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 31 Dec 2023 09:29:16 -0500 Subject: [PATCH 4/4] Update extra_functions.jl --- src/extra_functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extra_functions.jl b/src/extra_functions.jl index 0b2cb2cdd..040f14f72 100644 --- a/src/extra_functions.jl +++ b/src/extra_functions.jl @@ -22,7 +22,7 @@ for (T1, T2) in ((Symbolics.SymbolicUtils.Symbolic{<:Real}, Int64), (Num, Int64), (Real, Symbolics.SymbolicUtils.Symbolic{<:Int64}), (Symbolics.SymbolicUtils.Symbolic{<:Real}, Symbolics.SymbolicUtils.Symbolic{<:Int64}), - (Num, Symbolics.SymbolicUtils.Symbolic{<:Int64}) + (Num, Symbolics.SymbolicUtils.Symbolic{<:Int64})) @eval function Base.binomial(n::$T1, k::$T2) if any(Symbolics.iswrapped, (n, k))