Skip to content

Commit

Permalink
Merge pull request #1029 from isaacsas/binomial-fix
Browse files Browse the repository at this point in the history
change binomial registration
  • Loading branch information
ChrisRackauckas authored Dec 31, 2023
2 parents dc7c7a9 + bdf30c6 commit 006543a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/extra_functions.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
@register_symbolic Base.binomial(n, k::Integer)::Int true
@register_symbolic Base.binomial(n, k)::Int true
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)
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

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

@register_symbolic Base.sign(x)::Int
derivative(::typeof(sign), args::NTuple{1,Any}, ::Val{1}) = 0
Expand Down
15 changes: 15 additions & 0 deletions test/overloads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 006543a

Please sign in to comment.