mirror of
https://github.com/BachoSeven/homotopy-continuation.git
synced 2026-10-06 22:55:15 +00:00
Add more examples, homogenize() and change step function
This commit is contained in:
@@ -14,7 +14,9 @@ This is a project for the "Laboratorio Computazionale" exam at the University of
|
|||||||
- Parallelization
|
- Parallelization
|
||||||
- Extract functions in separate modules(?)
|
- Extract functions in separate modules(?)
|
||||||
|
|
||||||
## Example system
|
## Example systems
|
||||||
|
|
||||||
|
Here's some tests on 2x2 systems, with the plotted real approximate solutions
|
||||||
|
|
||||||
$$
|
$$
|
||||||
\begin{align*}
|
\begin{align*}
|
||||||
@@ -23,6 +25,26 @@ xy - 1 &= 0 \\
|
|||||||
\end{align*}
|
\end{align*}
|
||||||
$$
|
$$
|
||||||
|
|
||||||
Plot of the approximate solutions:
|

|
||||||
|
|
||||||

|
---
|
||||||
|
|
||||||
|
$$
|
||||||
|
\begin{align*}
|
||||||
|
x^2 + y^2 - 2 &= 0 \\
|
||||||
|
xy - 1 &= 0 \\
|
||||||
|
\end{align*}
|
||||||
|
$$
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
$$
|
||||||
|
\begin{align*}
|
||||||
|
x^3 + 5x^2 - y - 10 &= 0 \\
|
||||||
|
2x^2 - y - 10 &= 0 \\
|
||||||
|
\end{align*}
|
||||||
|
$$
|
||||||
|
|
||||||
|

|
||||||
|
|||||||
@@ -5,12 +5,20 @@ using Plots
|
|||||||
# Define start system based on total degree
|
# Define start system based on total degree
|
||||||
function start_system(F)
|
function start_system(F)
|
||||||
degrees = [maxdegree(p) for p in F]
|
degrees = [maxdegree(p) for p in F]
|
||||||
|
# @polyvar h
|
||||||
|
# G = [x_i^d - h^d for (d, x_i) in zip(degrees, variables(F))]
|
||||||
G = [x_i^d - 1 for (d, x_i) in zip(degrees, variables(F))]
|
G = [x_i^d - 1 for (d, x_i) in zip(degrees, variables(F))]
|
||||||
r = [[exp(2im*pi/d)^k for k=0:d-1] for d in degrees]
|
r = [[exp(2im*pi/d)^k for k=0:d-1] for d in degrees]
|
||||||
roots = collect(Iterators.product(r...))
|
# roots = vec([vcat(collect(root), 1) for root in collect(Iterators.product(r...))])
|
||||||
|
roots = vec([collect(root) for root in collect(Iterators.product(r...))])
|
||||||
return (G, roots)
|
return (G, roots)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function homogenize(F)
|
||||||
|
@polyvar h
|
||||||
|
return [sum([h^(maxdegree(p)-maxdegree(t))*t for t in p.terms]) for p in F]
|
||||||
|
end
|
||||||
|
|
||||||
# Define homotopy function
|
# Define homotopy function
|
||||||
function homotopy(F, G)
|
function homotopy(F, G)
|
||||||
γ = cis(2π * rand())
|
γ = cis(2π * rand())
|
||||||
@@ -32,7 +40,7 @@ function en_step(H, x, t, step_size)
|
|||||||
xp = x .+ Δx * step_size
|
xp = x .+ Δx * step_size
|
||||||
|
|
||||||
# Corrector step
|
# Corrector step
|
||||||
for _ in 1:5
|
for _ in 1:10
|
||||||
JH = [jh(vars=>xp) for jh in differentiate(H(t+step_size), vars)]
|
JH = [jh(vars=>xp) for jh in differentiate(H(t+step_size), vars)]
|
||||||
Δx = JH \ -[h(vars=>xp) for h in H(t+step_size)]
|
Δx = JH \ -[h(vars=>xp) for h in H(t+step_size)]
|
||||||
xp = xp .+ Δx
|
xp = xp .+ Δx
|
||||||
@@ -45,11 +53,14 @@ function en_step(H, x, t, step_size)
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Adaptive step size
|
# Adaptive step size
|
||||||
function adapt_step(H, x, t, step, m)
|
function adapt_step(x, x_old, step, m)
|
||||||
Δ = LinearAlgebra.norm([h(variables(H(t))=>x) for h in H(t)])
|
Δ = LinearAlgebra.norm(x - x_old)
|
||||||
|
# function adapt_step(H, x, t, step, m)
|
||||||
|
# Δ = LinearAlgebra.norm([h(variables(H(t))=>x) for h in H(t)])
|
||||||
if Δ > 0.1
|
if Δ > 0.1
|
||||||
step = 0.5 * step
|
step = 0.5 * step
|
||||||
elseif Δ < 0.001
|
m = 0
|
||||||
|
else
|
||||||
m+=1
|
m+=1
|
||||||
if (m == 5)
|
if (m == 5)
|
||||||
step = 2 * step
|
step = 2 * step
|
||||||
@@ -61,20 +72,22 @@ function adapt_step(H, x, t, step, m)
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Main homotopy continuation loop
|
# Main homotopy continuation loop
|
||||||
function solve(F, maxsteps=10000)
|
function solve(F, (G, roots) = start_system(F), maxsteps=10000)
|
||||||
(G, roots) = start_system(F)
|
# F=homogenize(F)
|
||||||
H=homotopy(F,G)
|
H=homotopy(F,G)
|
||||||
solutions = []
|
solutions = []
|
||||||
|
|
||||||
for r in roots
|
for r in roots
|
||||||
t = 1.0
|
t = 1.0
|
||||||
step_size = 0.1
|
step_size = 0.01
|
||||||
x0 = r
|
x0 = r
|
||||||
m = 0
|
m = 0
|
||||||
|
|
||||||
while t > 0 && maxsteps > 0
|
while t > 0 && maxsteps > 0
|
||||||
x0 = en_step(H, x0, t, step_size)
|
x = en_step(H, x0, t, step_size)
|
||||||
(m, step_size) = adapt_step(H, x0, t, step_size, m)
|
(m, step_size) = adapt_step(x, x0, step_size, m)
|
||||||
|
# (m, step_size) = adapt_step(H, x, t, step_size, m)
|
||||||
|
x0 = x
|
||||||
t -= step_size
|
t -= step_size
|
||||||
maxsteps -= 1
|
maxsteps -= 1
|
||||||
end
|
end
|
||||||
@@ -84,20 +97,29 @@ function solve(F, maxsteps=10000)
|
|||||||
return solutions
|
return solutions
|
||||||
end
|
end
|
||||||
|
|
||||||
function plot_real(solutions, F)
|
function plot_real(solutions, F, h, v, name)
|
||||||
p=plot(xlim = (-3, 3), ylim = (-3, 3), aspect_ratio = :equal)
|
p=plot(xlim = (-h, h), ylim = (-v, v), aspect_ratio = :equal)
|
||||||
contour!(-3:0.1:3, -3:0.1:3, (x,y)->F[1](variables(F)=>[x,y]), levels=[0], cbar=false, color=:cyan)
|
contour!(-h:0.1:h, -v:0.1:v, (x,y)->F[1](variables(F)=>[x,y]), levels=[0], cbar=false, color=:cyan)
|
||||||
contour!(-3:0.1:3, -3:0.1:3, (x,y)->F[2](variables(F)=>[x,y]), levels=[0], cbar=false, color=:green)
|
contour!(-h:0.1:h, -v:0.1:v, (x,y)->F[2](variables(F)=>[x,y]), levels=[0], cbar=false, color=:green)
|
||||||
scatter!([real(sol[1]) for sol in solutions], [real(sol[2]) for sol in solutions], color = "red", label = "Solutions")
|
scatter!([real(sol[1]) for sol in solutions], [real(sol[2]) for sol in solutions], color = "red", label = "Real solutions")
|
||||||
|
|
||||||
png("solutions")
|
png("solutions" * name)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Input polynomial system
|
# Input polynomial system
|
||||||
@polyvar x y
|
@polyvar x y
|
||||||
F = [x*y - 1, x^2 + y^2 - 4]
|
F = [x*y - 1, x^2 + y^2 - 4]
|
||||||
|
T = [x*y - 1, x^2 + y^2 - 2]
|
||||||
|
C = [x^3 - y + 5x^2 - 10, 2x^2 - y - 10]
|
||||||
|
P = [x*y - 1, x*y]
|
||||||
|
|
||||||
sF = solve(F)
|
sF = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, solve(F))
|
||||||
|
sT = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, solve(T))
|
||||||
|
sC = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, solve(C))
|
||||||
|
# sP = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, solve(P))
|
||||||
|
|
||||||
# Plotting the system and the real solutions
|
# Plotting the system and the real solutions
|
||||||
plot_real(sF, F)
|
plot_real(sF, F, 4, 4, "1")
|
||||||
|
plot_real(sT, T, 4, 4, "2")
|
||||||
|
plot_real(sC, C, 6, 12, "3")
|
||||||
|
# plot_real(sP, P, 5, 5, "4")
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 18 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
Reference in New Issue
Block a user