Skip to content

Commit

Permalink
Symbolic extensions (#401)
Browse files Browse the repository at this point in the history
* widen applicability of equation syntax
  • Loading branch information
jverzani authored Sep 29, 2023
1 parent fbd0ad4 commit 2e7c255
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ext/RootsSymPyExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,20 @@ function Roots.Callable_Function(M::Roots.AbstractUnivariateZeroMethod, f::SymPy
Roots.Callable_Function(M, lambdify(f), p)
end

function Roots.FnWrapper(f::SymPy.Sym)
if f.is_Equality == true
f = lhs(f) - rhs(f)
end
Roots.FnWrapper(lambdify(f))
end


## allow find_zeros to use symbolic equation
function Roots.find_zeros(f::SymPy.Sym, a, b=nothing; kwargs...)
if f.is_Equality == true
f = lhs(f) - rhs(f)
end
find_zeros(lambdify(f), a, b; kwargs...)
end

end
16 changes: 16 additions & 0 deletions ext/RootsSymPyPythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@ function Roots.Callable_Function(M::Roots.AbstractUnivariateZeroMethod, f::SymPy
Roots.Callable_Function(M, lambdify(f), p)
end

function Roots.FnWrapper(f::SymPyPythonCall.Sym)
if f.is_Equality == true
f = lhs(f) - rhs(f)
end
Roots.FnWrapper(lambdify(f))
end


## allow find_zeros to use symbolic equation
function Roots.find_zeros(f::SymPyPythonCall.Sym, a, b=nothing; kwargs...)
if f.is_Equality == true
f = lhs(f) - rhs(f)
end
find_zeros(lambdify(f), a, b; kwargs...)
end


end
28 changes: 28 additions & 0 deletions test/test_extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@
using SymPy
@testset "SymPy" begin
SymPy.@syms x
@test find_zero(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - 1/2, (0, pi/2))
@test find_zero(1/2 ~ cos(x), (0, pi/2)) ≈ find_zero(x -> 1/2 - cos(x), (0, pi/2))
@test find_zero(cos(x) ~ x/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - x/2, (0, pi/2))
@test find_zeros(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - 1/2, (0, pi/2))
@test find_zeros(1/2 ~ cos(x), (0, pi/2)) ≈ find_zeros(x -> 1/2 - cos(x), (0, pi/2))
@test find_zeros(cos(x) ~ x/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - x/2, (0, pi/2))
@test fzero(cos(x) ~ 1/2, 0, pi/2) ≈ fzero(x -> cos(x) - 1/2, 0, pi/2)
@test fzero(1/2 ~ cos(x), 0, pi/2) ≈ fzero(x -> 1/2 - cos(x), 0, pi/2)
@test fzero(cos(x) ~ x/2, 0, pi/2) ≈ fzero(x -> cos(x) - x/2, 0, pi/2)
@test fzeros(cos(x) ~ 1/2, 0, pi/2) ≈ fzeros(x -> cos(x) - 1/2, 0, pi/2)
@test fzeros(1/2 ~ cos(x), 0, pi/2) ≈ fzeros(x -> 1/2 - cos(x), 0, pi/2)
@test fzeros(cos(x) ~ x/2, 0, pi/2) ≈ fzeros(x -> cos(x) - x/2, 0, pi/2)
end
=#

#=
using SymPyPythonCall
@testset "SymPythonCall" begin
SymPyPythonCall.@syms x
@test find_zero(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - 1/2, (0, pi/2))
@test find_zero(1/2 ~ cos(x), (0, pi/2)) ≈ find_zero(x -> 1/2 - cos(x), (0, pi/2))
@test find_zero(cos(x) ~ x/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - x/2, (0, pi/2))
@test find_zeros(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - 1/2, (0, pi/2))
@test find_zeros(1/2 ~ cos(x), (0, pi/2)) ≈ find_zeros(x -> 1/2 - cos(x), (0, pi/2))
@test find_zeros(cos(x) ~ x/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - x/2, (0, pi/2))
@test fzero(cos(x) ~ 1/2, 0, pi/2) ≈ fzero(x -> cos(x) - 1/2, 0, pi/2)
@test fzero(1/2 ~ cos(x), 0, pi/2) ≈ fzero(x -> 1/2 - cos(x), 0, pi/2)
@test fzero(cos(x) ~ x/2, 0, pi/2) ≈ fzero(x -> cos(x) - x/2, 0, pi/2)
@test fzeros(cos(x) ~ 1/2, 0, pi/2) ≈ fzeros(x -> cos(x) - 1/2, 0, pi/2)
@test fzeros(1/2 ~ cos(x), 0, pi/2) ≈ fzeros(x -> 1/2 - cos(x), 0, pi/2)
@test fzeros(cos(x) ~ x/2, 0, pi/2) ≈ fzeros(x -> cos(x) - x/2, 0, pi/2)
end
=#

Expand Down

2 comments on commit 2e7c255

@jverzani
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.

Registration pull request created: JuliaRegistries/General/92486

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.0.20 -m "<description of version>" 2e7c255e859a88238e5f858f180c8623babe2921
git push origin v2.0.20

Please sign in to comment.