Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add NonlinearSolveHomotopyContinuation.jl #523

Merged
merged 24 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
879fbd3
feat: add `NonlinearSolveHomotopyContinuation.jl`
AayushSabharwal Dec 26, 2024
6addac3
feat: add `solve` implementations
AayushSabharwal Dec 26, 2024
12ea884
test: add tests for all roots solve
AayushSabharwal Dec 26, 2024
c83c95a
fix: fix jacobian implementation, add taylor implementation
AayushSabharwal Jan 6, 2025
fea7609
fix: fix system evaluate and jacobian
AayushSabharwal Jan 10, 2025
a048ca0
fix: fix inplace system taylor
AayushSabharwal Jan 10, 2025
80ff4a7
fix: fix homotopy and implement taylor
AayushSabharwal Jan 10, 2025
48d0f7a
fix: fix jacobian building for systems
AayushSabharwal Jan 10, 2025
af30f5e
fix: fix single root solve implementation
AayushSabharwal Jan 10, 2025
2026034
build: add TaylorSeries as a dependency
AayushSabharwal Jan 10, 2025
db21cf8
fix: fix jacobian and taylor implementations
AayushSabharwal Jan 26, 2025
75aabbb
test: add tests for single roots, fix allroots tests
AayushSabharwal Jan 26, 2025
d7972af
build: add compat entries
AayushSabharwal Jan 26, 2025
4a8def3
docs: add docstrings to NonlinearSolveHomotopyContinuation.jl
AayushSabharwal Jan 26, 2025
c2aa812
ci: add NonlinearSolveHomotopyContinuation.jl to CI
AayushSabharwal Jan 26, 2025
a6f6e7b
docs: add NonlinearSolveHomotopyContinuation to docs
AayushSabharwal Jan 26, 2025
9c9bc79
build: fix LinearAlgebra compat
AayushSabharwal Jan 26, 2025
d9b3378
test: do not rely on singular roots for tests
AayushSabharwal Jan 27, 2025
f66af4e
test: sort roots in `allroots` test
AayushSabharwal Jan 27, 2025
ac6c3dc
refactor: format
AayushSabharwal Jan 27, 2025
8fa843c
test: do not rely on singular roots
AayushSabharwal Jan 27, 2025
1decea6
Update homotopycontinuation.md
ChrisRackauckas Jan 28, 2025
5426e99
docs: add `NonlinearSolveHomotopyContinuation` to doc build
AayushSabharwal Jan 29, 2025
d2e8ec6
refactor: use TaylorDiff.jl instead of TaylorSeries.jl
AayushSabharwal Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: use TaylorDiff.jl instead of TaylorSeries.jl
AayushSabharwal committed Jan 29, 2025
commit d2e8ec6eb29e5a2e0ebdc758ef20f839bd715722
4 changes: 2 additions & 2 deletions lib/NonlinearSolveHomotopyContinuation/Project.toml
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NonlinearSolveBase = "be0214bd-f91f-a760-ac4e-3421ce2b2da0"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
TaylorSeries = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
TaylorDiff = "b36ab563-344f-407b-a36a-4f200bebf99c"

[compat]
ADTypes = "1.11.0"
@@ -29,7 +29,7 @@ NonlinearSolve = "4"
NonlinearSolveBase = "1.3.3"
SciMLBase = "2.71"
SymbolicIndexingInterface = "0.3.36"
TaylorSeries = "0.18.2"
TaylorDiff = "0.3.1"
Test = "1.10"
julia = "1.10"

Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ using NonlinearSolveBase
using SymbolicIndexingInterface
using LinearAlgebra
using ADTypes
using TaylorSeries
using TaylorDiff
using DocStringExtensions
import CommonSolve
import HomotopyContinuation as HC
85 changes: 62 additions & 23 deletions lib/NonlinearSolveHomotopyContinuation/src/interface_types.jl
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@
"""
vars
"""
The `TaylorSeries.Taylor1` objects used to compute the taylor series of `f`.
The `TaylorDiff.TaylorScalar` objects used to compute the taylor series of `f`.
"""
taylorvars
"""
@@ -161,47 +161,74 @@
end

function update_taylorvars_from_taylorvector!(
vars, x::HC.ModelKit.TaylorVector{M}) where {M}
for i in eachindex(vars)
for j in 0:(M - 1)
vars[i][j] = x[i, j + 1]
vars, x::HC.ModelKit.TaylorVector)
for i in eachindex(x)
xvar = x[i]
realx = ntuple(Val(4)) do j
j <= length(xvar) ? real(xvar[j - 1]) : 0.0
end
for j in M:4
vars[i][j] = zero(vars[i][j])
imagx = ntuple(Val(4)) do j
j <= length(xvar) ? imag(xvar[j - 1]) : 0.0
end

vars[2i - 1] = TaylorScalar(realx)
vars[2i] = TaylorScalar(imagx)
end
end

function update_taylorvars_from_taylorvector!(vars, x::AbstractVector)
for i in eachindex(vars)
vars[i][0] = x[i]
for j in 1:4
vars[i][j] = zero(vars[i][j])
end
for i in eachindex(x)
vars[2i - 1] = TaylorScalar(real(x[i]), ntuple(Returns(0.0), Val(3)))
vars[2i] = TaylorScalar(imag(x[i]), ntuple(Returns(0.0), Val(3)))
end
end

function check_taylor_equality(vars, x::HC.ModelKit.TaylorVector)
for i in eachindex(x)
TaylorDiff.flatten(vars[2i-1]) == map(real, x[i]) || return false
TaylorDiff.flatten(vars[2i]) == map(imag, x[i]) || return false
end
return true
end
function check_taylor_equality(vars, x::AbstractVector)
for i in eachindex(x)
TaylorDiff.value(vars[2i-1]) != real(x[i]) && return false
TaylorDiff.value(vars[2i]) != imag(x[i]) && return false
end
return true
end

function update_maybe_taylorvector_from_taylorvars!(
u::Vector, vars, buffer, ::Val{N}) where {N}
for i in eachindex(vars)
u[i] = buffer[i][N]
rval = TaylorDiff.flatten(real(buffer[i]))
ival = TaylorDiff.flatten(imag(buffer[i]))
u[i] = rval[N] + im * ival[N]
end
end

function update_maybe_taylorvector_from_taylorvars!(
u::HC.ModelKit.TaylorVector, vars, buffer, ::Val{N}) where {N}
u::HC.ModelKit.TaylorVector{M}, vars, buffer, ::Val{N}) where {M, N}
for i in eachindex(vars)
u[i] = ntuple(j -> buffer[i][j - 1], Val(N + 1))
rval = TaylorDiff.flatten(real(buffer[i]))
ival = TaylorDiff.flatten(imag(buffer[i]))
u[i] = ntuple(i -> rval[i] + im * ival[i], Val(length(rval)))
end
end

function HC.ModelKit.taylor!(u::AbstractVector, ::Val{N},
sys::HomotopySystemWrapper{Inplace}, x, p = nothing) where {N}
f = sys.f
p = sys.p
buffer, vars = sys.taylorvars
update_taylorvars_from_taylorvector!(vars, x)
f(buffer, vars, p)
vars, buffer = sys.taylorvars
if !check_taylor_equality(vars, x)
update_taylorvars_from_taylorvector!(vars, x)
vars = reinterpret(Complex{eltype(vars)}, vars)
buffer = reinterpret(Complex{eltype(buffer)}, buffer)
f(buffer, vars, p)
else
vars = reinterpret(Complex{eltype(vars)}, vars)
end
update_maybe_taylorvector_from_taylorvars!(u, vars, buffer, Val(N))
return u
end
@@ -211,8 +238,14 @@
f = sys.f
p = sys.p
vars = sys.taylorvars
update_taylorvars_from_taylorvector!(vars, x)
buffer = f(vars, p)
if !check_taylor_equality(vars, x)
update_taylorvars_from_taylorvector!(vars, x)
vars = reinterpret(Complex{eltype(vars)}, vars)
buffer = f(vars, p)
copyto!(vars, buffer)
else
vars = buffer = reinterpret(Complex{eltype(vars)}, vars)
end
update_maybe_taylorvector_from_taylorvars!(u, vars, buffer, Val(N))
return u
end
@@ -222,16 +255,22 @@
f = sys.f
p = sys.p
var = sys.taylorvars
update_taylorvars_from_taylorvector!((var,), x)
buffer = f(var, p)
update_maybe_taylorvector_from_taylorvars!(u, (var,), (buffer,), Val(N))
if !check_taylor_equality(var, x)
update_taylorvars_from_taylorvector!(var, x)
var = reinterpret(Complex{eltype(var)}, var)
buffer = f(var[1], p)
var[1] = buffer
else
var = buffer = reinterpret(Complex{eltype(var)}, var)
end
update_maybe_taylorvector_from_taylorvars!(u, var, buffer, Val(N))
return u
end

"""
$(TYPEDEF)

A `HomotopyContinuation.AbstractHomotopy` which uses an inital guess ``x_0`` to construct

Check warning on line 273 in lib/NonlinearSolveHomotopyContinuation/src/interface_types.jl

GitHub Actions / Spell Check with Typos

"inital" should be "initial".
the start system for the homotopy. The homotopy is

```math
10 changes: 6 additions & 4 deletions lib/NonlinearSolveHomotopyContinuation/src/solve.jl
Original file line number Diff line number Diff line change
@@ -44,12 +44,14 @@ function homotopy_continuation_preprocessing(
end

taylorvars = if isscalar
Taylor1(zeros(ComplexF64, 5), 4)
[TaylorScalar(ntuple(Returns(0.0), 4)), TaylorScalar(ntuple(Returns(0.0), 4))]
elseif iip
([Taylor1(zeros(ComplexF64, 5), 4) for _ in u0],
[Taylor1(zeros(ComplexF64, 5), 4) for _ in u0])
(
[TaylorScalar(ntuple(Returns(0.0), 4)) for _ in 1:2length(u0)],
[TaylorScalar(ntuple(Returns(0.0), 4)) for _ in 1:2length(u0)]
)
else
[Taylor1(zeros(ComplexF64, 5), 4) for _ in u0]
[TaylorScalar(ntuple(Returns(0.0), 4)) for _ in 1:2length(u0)]
end

jacobian_buffers = if isscalar