Skip to content

Commit

Permalink
Merge pull request #244 from JuliaSymbolics/myb/sign
Browse files Browse the repository at this point in the history
Eagerly evaluate registered function if arguments are not symbolic
  • Loading branch information
YingboMa authored May 26, 2021
2 parents 5d8dfeb + 40b9447 commit 070d8f8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/extra_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
@register Base.getindex(x,i) # define one and only one promotion rule
@register Base.binomial(n,k)

Base.sign(x::Symbolic) = Term{Int}(sign, [x])
Base.sign(x::Num) = Num(sign(value(x)))
@register Base.sign(x)::Int
derivative(::typeof(sign), args::NTuple{1,Any}, ::Val{1}) = 0

@register Base.signbit(x)::Bool
derivative(::typeof(signbit), args::NTuple{1,Any}, ::Val{1}) = 0
derivative(::typeof(abs), args::NTuple{1,Any}, ::Val{1}) = IfElse.ifelse(signbit(args[1]),-one(args[1]),one(args[1]))
Expand Down
8 changes: 7 additions & 1 deletion src/register.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SymbolicUtils: Symbolic
"""
@register(expr, define_promotion = true, Ts = [Num, Symbolic, Real])
Expand Down Expand Up @@ -50,7 +51,12 @@ macro register(expr, define_promotion = true, Ts = [Num, Symbolic, Real])
push!(ex.args, quote
function $f($(setinds(args, symbolic_args, ts)...))
wrap = any(x->typeof(x) <: $Num, tuple($(setinds(args, symbolic_args, ts)...),)) ? $Num : $identity
wrap($Term{$ret_type}($f, [$(map(name, args)...)]))
args = ($(map(name, args)...),)
if all(arg -> !(arg isa $Symbolic), args)
wrap($f(args...,))
else
wrap($Term{$ret_type}($f, collect(args)))
end
end
end)
end
Expand Down
3 changes: 2 additions & 1 deletion test/overloads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ eqs = [
@test [2 1 -1; -3 1 -1; 0 1 -5] * Symbolics.solve_for(eqs, [x, y, z]) == [2; -2; -2]
@test isequal(Symbolics.solve_for(2//1*x + y - 2//1*z ~ 9//1*x, 1//1*x), 1//7*y - 2//7*z)

@test isequal(sign(x), Num(SymbolicUtils.Term{Int}(sign, [x])))
@test isequal(sign(x), Num(SymbolicUtils.Term{Int}(sign, [Symbolics.value(x)])))
@test sign(Num(1)) isa Num
@test isequal(sign(Num(1)), Num(1))
@test isequal(sign(Num(-1)), Num(-1))

Expand Down

2 comments on commit 070d8f8

@YingboMa
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: "Tag with name v0.1.25 already exists and points to a different commit"

Please sign in to comment.