Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change binomial registration #1029

Merged
merged 4 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading