Skip to content

Commit

Permalink
RootFinding GSOC project
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rbed committed Jul 25, 2024
1 parent ad852d5 commit 0d6395e
Show file tree
Hide file tree
Showing 16 changed files with 2,622 additions and 0 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Expand Down
90 changes: 90 additions & 0 deletions ext/SymbolicsGroebnerExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,94 @@ function Symbolics.is_groebner_basis(polynomials::Vector{Num}; kwargs...)
Groebner.isgroebner(polynoms; kwargs...)
end

function Symbolics.solve_multivar(eqs::Vector{Num}, vars::Vector{Num}, mult=false)
# do the trick
@variables HAT
old_len = length(vars)
push!(vars, HAT)
new_eqs = []
generating = true
while generating
new_eqs = deepcopy(eqs)
eq = HAT
for i = 1:(old_len)
eq -= rand(1:10)*vars[i]
end
push!(new_eqs, eq)
new_eqs = convert(Vector{Any}, Symbolics.groebner_basis(new_eqs, ordering=Lex(vars)))

if length(new_eqs) <= length(vars)
generating &= false
end

for i in eachindex(new_eqs)[2:end]
generating |= all(Symbolics.degree(var) > 1 for var in Symbolics.get_variables(new_eqs[i]))
end
end

solutions = []

# handle "unsolvable" cases
if isequal(1, new_eqs[1])
return solutions
end
if length(new_eqs) < length(vars)
throw("Infinite number of solutions")
end


# first, solve any single variable equations
i = 1
while !(i > length(new_eqs))
present_vars = Symbolics.get_variables(new_eqs[i])
for var in vars
if size(present_vars, 1) == 1 && isequal(var, present_vars[1])
new_sols = Symbolics.solve(Symbolics.wrap(new_eqs[i]), var, mult)

if length(solutions) == 0
append!(solutions, [Dict{Num, Any}(var => sol) for sol in new_sols])
else
solutions = Symbolics.add_sol_to_all(solutions, new_sols, var)
end

deleteat!(new_eqs, i)
i = i - 1
break
end
end
i = i + 1
end


# second, iterate over eqs and sub each found solution
# then add the roots of the remaining unknown variables
for eq in new_eqs
solved = false
present_vars = Symbolics.get_variables(eq)
size_of_sub = length(solutions[1])

if size(present_vars, 1) <= (size_of_sub + 1)
while !solved
subbed_eq = eq
for (var, root) in solutions[1]
subbed_eq = Symbolics.substitute(subbed_eq, Dict([var => root]), fold=false)
end

var_tosolve = Symbolics.get_variables(subbed_eq)[1]
new_var_sols = Symbolics.solve_univar(subbed_eq, var_tosolve, mult)
Symbolics.add_sol!(solutions, new_var_sols, var_tosolve, 1)

solved = all(x -> length(x) == size_of_sub+1, solutions)
end
end
end

pop!(vars)
for roots in solutions
delete!(roots, HAT)
end

return solutions
end

end # module
2 changes: 2 additions & 0 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ for sType in [Pair, Vector, Dict]
@eval substitute(expr::Arr, s::$sType; kw...) = wrap(substituter(s)(unwrap(expr); kw...))
end

include("solver/RootFinding.jl")

function symbolics_to_sympy end
export symbolics_to_sympy

Expand Down
22 changes: 22 additions & 0 deletions src/solver/RootFinding.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# module RootFinding

include("coeffs.jl")
include("nemo_stuff.jl")
include("solve_helpers.jl")
include("postprocess.jl")
include("univar.jl")
include("ia_helpers.jl")
include("polynomialization.jl")
include("attract.jl")
include("ia_main.jl")
include("main.jl")

# export solve
#
# # helper solvers
# export solve_univar
# export solve_multipoly
# export solve_multivar
# export ia_solve
#
# end
Loading

0 comments on commit 0d6395e

Please sign in to comment.