Skip to content

Commit 2e7c255

Browse files
authored
Symbolic extensions (#401)
* widen applicability of equation syntax
1 parent fbd0ad4 commit 2e7c255

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

ext/RootsSymPyExt.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,20 @@ function Roots.Callable_Function(M::Roots.AbstractUnivariateZeroMethod, f::SymPy
1111
Roots.Callable_Function(M, lambdify(f), p)
1212
end
1313

14+
function Roots.FnWrapper(f::SymPy.Sym)
15+
if f.is_Equality == true
16+
f = lhs(f) - rhs(f)
17+
end
18+
Roots.FnWrapper(lambdify(f))
19+
end
20+
21+
22+
## allow find_zeros to use symbolic equation
23+
function Roots.find_zeros(f::SymPy.Sym, a, b=nothing; kwargs...)
24+
if f.is_Equality == true
25+
f = lhs(f) - rhs(f)
26+
end
27+
find_zeros(lambdify(f), a, b; kwargs...)
28+
end
1429

1530
end

ext/RootsSymPyPythonCallExt.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,21 @@ function Roots.Callable_Function(M::Roots.AbstractUnivariateZeroMethod, f::SymPy
1111
Roots.Callable_Function(M, lambdify(f), p)
1212
end
1313

14+
function Roots.FnWrapper(f::SymPyPythonCall.Sym)
15+
if f.is_Equality == true
16+
f = lhs(f) - rhs(f)
17+
end
18+
Roots.FnWrapper(lambdify(f))
19+
end
20+
21+
22+
## allow find_zeros to use symbolic equation
23+
function Roots.find_zeros(f::SymPyPythonCall.Sym, a, b=nothing; kwargs...)
24+
if f.is_Equality == true
25+
f = lhs(f) - rhs(f)
26+
end
27+
find_zeros(lambdify(f), a, b; kwargs...)
28+
end
29+
1430

1531
end

test/test_extensions.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,47 @@
22
using SymPy
33
@testset "SymPy" begin
44
SymPy.@syms x
5+
56
@test find_zero(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - 1/2, (0, pi/2))
67
@test find_zero(1/2 ~ cos(x), (0, pi/2)) ≈ find_zero(x -> 1/2 - cos(x), (0, pi/2))
78
@test find_zero(cos(x) ~ x/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - x/2, (0, pi/2))
9+
10+
@test find_zeros(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - 1/2, (0, pi/2))
11+
@test find_zeros(1/2 ~ cos(x), (0, pi/2)) ≈ find_zeros(x -> 1/2 - cos(x), (0, pi/2))
12+
@test find_zeros(cos(x) ~ x/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - x/2, (0, pi/2))
13+
14+
@test fzero(cos(x) ~ 1/2, 0, pi/2) ≈ fzero(x -> cos(x) - 1/2, 0, pi/2)
15+
@test fzero(1/2 ~ cos(x), 0, pi/2) ≈ fzero(x -> 1/2 - cos(x), 0, pi/2)
16+
@test fzero(cos(x) ~ x/2, 0, pi/2) ≈ fzero(x -> cos(x) - x/2, 0, pi/2)
17+
18+
@test fzeros(cos(x) ~ 1/2, 0, pi/2) ≈ fzeros(x -> cos(x) - 1/2, 0, pi/2)
19+
@test fzeros(1/2 ~ cos(x), 0, pi/2) ≈ fzeros(x -> 1/2 - cos(x), 0, pi/2)
20+
@test fzeros(cos(x) ~ x/2, 0, pi/2) ≈ fzeros(x -> cos(x) - x/2, 0, pi/2)
21+
822
end
923
=#
1024

1125
#=
1226
using SymPyPythonCall
1327
@testset "SymPythonCall" begin
1428
SymPyPythonCall.@syms x
29+
1530
@test find_zero(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - 1/2, (0, pi/2))
1631
@test find_zero(1/2 ~ cos(x), (0, pi/2)) ≈ find_zero(x -> 1/2 - cos(x), (0, pi/2))
1732
@test find_zero(cos(x) ~ x/2, (0, pi/2)) ≈ find_zero(x -> cos(x) - x/2, (0, pi/2))
33+
34+
@test find_zeros(cos(x) ~ 1/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - 1/2, (0, pi/2))
35+
@test find_zeros(1/2 ~ cos(x), (0, pi/2)) ≈ find_zeros(x -> 1/2 - cos(x), (0, pi/2))
36+
@test find_zeros(cos(x) ~ x/2, (0, pi/2)) ≈ find_zeros(x -> cos(x) - x/2, (0, pi/2))
37+
38+
@test fzero(cos(x) ~ 1/2, 0, pi/2) ≈ fzero(x -> cos(x) - 1/2, 0, pi/2)
39+
@test fzero(1/2 ~ cos(x), 0, pi/2) ≈ fzero(x -> 1/2 - cos(x), 0, pi/2)
40+
@test fzero(cos(x) ~ x/2, 0, pi/2) ≈ fzero(x -> cos(x) - x/2, 0, pi/2)
41+
42+
@test fzeros(cos(x) ~ 1/2, 0, pi/2) ≈ fzeros(x -> cos(x) - 1/2, 0, pi/2)
43+
@test fzeros(1/2 ~ cos(x), 0, pi/2) ≈ fzeros(x -> 1/2 - cos(x), 0, pi/2)
44+
@test fzeros(cos(x) ~ x/2, 0, pi/2) ≈ fzeros(x -> cos(x) - x/2, 0, pi/2)
45+
1846
end
1947
=#
2048

0 commit comments

Comments
 (0)