diff --git a/ext/RootsSymPyExt.jl b/ext/RootsSymPyExt.jl index 3aaf762..919102c 100644 --- a/ext/RootsSymPyExt.jl +++ b/ext/RootsSymPyExt.jl @@ -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 diff --git a/ext/RootsSymPyPythonCallExt.jl b/ext/RootsSymPyPythonCallExt.jl index 5d2b686..ec2184e 100644 --- a/ext/RootsSymPyPythonCallExt.jl +++ b/ext/RootsSymPyPythonCallExt.jl @@ -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 diff --git a/test/test_extensions.jl b/test/test_extensions.jl index 67173df..d3573d7 100644 --- a/test/test_extensions.jl +++ b/test/test_extensions.jl @@ -2,9 +2,23 @@ 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 =# @@ -12,9 +26,23 @@ 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 =#