Skip to content

feat: use sources #571

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

Merged
merged 4 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"

[sources]
BracketingNonlinearSolve = {path = "lib/BracketingNonlinearSolve"}
NonlinearSolveBase = {path = "lib/NonlinearSolveBase"}
NonlinearSolveFirstOrder = {path = "lib/NonlinearSolveFirstOrder"}
NonlinearSolveQuasiNewton = {path = "lib/NonlinearSolveQuasiNewton"}
NonlinearSolveSpectralMethods = {path = "lib/NonlinearSolveSpectralMethods"}
SimpleNonlinearSolve = {path = "lib/SimpleNonlinearSolve"}

[weakdeps]
FastLevenbergMarquardt = "7a0df574-e128-4d35-8cbd-3d84502bf7ce"
FixedPointAcceleration = "817d07cb-a79a-5c30-9a31-890123675176"
Expand Down
3 changes: 3 additions & 0 deletions lib/BracketingNonlinearSolve/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"

[sources]
NonlinearSolveBase = {path = "../NonlinearSolveBase"}

[weakdeps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

Expand Down
32 changes: 16 additions & 16 deletions lib/BracketingNonlinearSolve/src/muller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ initial guesses `(left, middle, right)` for the root.

### Keyword Arguments

- `middle`: the initial guess for the middle point. If not provided, the
midpoint of the interval `(left, right)` is used.
- `middle`: the initial guess for the middle point. If not provided, the
midpoint of the interval `(left, right)` is used.
"""
struct Muller{T} <: AbstractBracketingAlgorithm
middle::T
Expand All @@ -18,7 +18,7 @@ end
Muller() = Muller(nothing)

function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Muller, args...;
abstol = nothing, maxiters = 1000, kwargs...)
abstol = nothing, maxiters = 1000, kwargs...)
@assert !SciMLBase.isinplace(prob) "`Muller` only supports out-of-place problems."
xᵢ₋₂, xᵢ = prob.tspan
xᵢ₋₁ = isnothing(alg.middle) ? (xᵢ₋₂ + xᵢ) / 2 : alg.middle
Expand All @@ -32,35 +32,35 @@ function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Muller, args...;
abstol = abs(NonlinearSolveBase.get_tolerance(
xᵢ₋₂, abstol, promote_type(eltype(xᵢ₋₂), eltype(xᵢ))))

for _ 1:maxiters
q = (xᵢ - xᵢ₋₁)/(xᵢ₋₁ - xᵢ₋₂)
A = q*fxᵢ - q*(1 + q)*fxᵢ₋₁ + q^2*fxᵢ₋₂
B = (2*q + 1)*fxᵢ - (1 + q)^2*fxᵢ₋₁ + q^2*fxᵢ₋₂
C = (1 + q)*fxᵢ
for _ in 1:maxiters
q = (xᵢ - xᵢ₋₁) / (xᵢ₋₁ - xᵢ₋₂)
A = q * fxᵢ - q * (1 + q) * fxᵢ₋₁ + q^2 * fxᵢ₋₂
B = (2 * q + 1) * fxᵢ - (1 + q)^2 * fxᵢ₋₁ + q^2 * fxᵢ₋₂
C = (1 + q) * fxᵢ

denom₊ = B + √(B^2 - 4*A*C)
denom₋ = B - √(B^2 - 4*A*C)
denom₊ = B + √(B^2 - 4 * A * C)
denom₋ = B - √(B^2 - 4 * A * C)

if abs(denom₊) ≥ abs(denom₋)
xᵢ₊₁ = xᵢ - (xᵢ - xᵢ₋₁)*2*C/denom₊
xᵢ₊₁ = xᵢ - (xᵢ - xᵢ₋₁) * 2 * C / denom₊
else
xᵢ₊₁ = xᵢ - (xᵢ - xᵢ₋₁)*2*C/denom₋
xᵢ₊₁ = xᵢ - (xᵢ - xᵢ₋₁) * 2 * C / denom₋
end

fxᵢ₊₁ = f(xᵢ₊₁)

# Termination Check
if abstol ≥ abs(fxᵢ₊₁)
return SciMLBase.build_solution(prob, alg, xᵢ₊₁, fxᵢ₊₁;
retcode = ReturnCode.Success,
left = xᵢ₊₁, right = xᵢ₊₁)
retcode = ReturnCode.Success,
left = xᵢ₊₁, right = xᵢ₊₁)
end

xᵢ₋₂, xᵢ₋₁, xᵢ = xᵢ₋₁, xᵢ, xᵢ₊₁
fxᵢ₋₂, fxᵢ₋₁, fxᵢ = fxᵢ₋₁, fxᵢ, fxᵢ₊₁
end

return SciMLBase.build_solution(prob, alg, xᵢ₊₁, fxᵢ₊₁;
retcode = ReturnCode.MaxIters,
left = xᵢ₊₁, right = xᵢ₊₁)
retcode = ReturnCode.MaxIters,
left = xᵢ₊₁, right = xᵢ₊₁)
end
18 changes: 9 additions & 9 deletions lib/BracketingNonlinearSolve/test/muller_tests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testitem "Muller" begin
f(u, p) = u^2 - p
g(u, p) = sin(u)
h(u, p) = exp(-u)*sin(u)
h(u, p) = exp(-u) * sin(u)
i(u, p) = u^3 - 1

@testset "Quadratic function" begin
Expand Down Expand Up @@ -30,7 +30,7 @@
prob = IntervalNonlinearProblem{false}(g, tspan)
sol = solve(prob, Muller())

@test sol.u ≈ 2*π
@test sol.u ≈ 2 * π
end

@testset "Exponential-sine function" begin
Expand All @@ -44,7 +44,7 @@
prob = IntervalNonlinearProblem{false}(h, tspan)
sol = solve(prob, Muller())

@test sol.u0 atol = 1e-15
@test sol.u0 atol=1e-15

tspan = (-1.0, 1.0)
prob = IntervalNonlinearProblem{false}(h, tspan)
Expand All @@ -54,17 +54,17 @@
end

@testset "Complex roots" begin
tspan = (-1.0, 1.0*im)
tspan = (-1.0, 1.0 * im)
prob = IntervalNonlinearProblem{false}(i, tspan)
sol = solve(prob, Muller())

@test sol.u ≈ (-1 + √3*im)/2
@test sol.u ≈ (-1 + √3 * im) / 2

tspan = (-1.0, -1.0*im)
tspan = (-1.0, -1.0 * im)
prob = IntervalNonlinearProblem{false}(i, tspan)
sol = solve(prob, Muller())

@test sol.u ≈ (-1 - √3*im)/2
@test sol.u ≈ (-1 - √3 * im) / 2
end

@testset "Middle" begin
Expand All @@ -87,10 +87,10 @@

@test sol.u ≈ -π

tspan = (-1.0, 1.0*im)
tspan = (-1.0, 1.0 * im)
prob = IntervalNonlinearProblem{false}(i, tspan)
sol = solve(prob, Muller(0.0))

@test sol.u ≈ (-1 + √3*im)/2
@test sol.u ≈ (-1 + √3 * im) / 2
end
end
6 changes: 4 additions & 2 deletions lib/BracketingNonlinearSolve/test/rootfind_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ end
@testitem "Interval Nonlinear Problems" setup=[RootfindingTestSnippet] tags=[:core] begin
using ForwardDiff

@testset for alg in (Alefeld(), Bisection(), Brent(), Falsi(), ITP(), Muller(), Ridder(), nothing)
@testset for alg in (
Alefeld(), Bisection(), Brent(), Falsi(), ITP(), Muller(), Ridder(), nothing)
tspan = (1.0, 20.0)

function g(p)
Expand Down Expand Up @@ -76,7 +77,8 @@ end
end

@testitem "Flipped Signs and Reversed Tspan" setup=[RootfindingTestSnippet] tags=[:core] begin
@testset for alg in (Alefeld(), Bisection(), Brent(), Falsi(), ITP(), Muller(), Ridder(), nothing)
@testset for alg in (
Alefeld(), Bisection(), Brent(), Falsi(), ITP(), Muller(), Ridder(), nothing)
f1(u, p) = u * u - p
f2(u, p) = p - u * u

Expand Down
3 changes: 3 additions & 0 deletions lib/NonlinearSolveFirstOrder/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ SciMLJacobianOperators = "19f34311-ddf3-4b8b-af20-060888a46c0e"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"

[sources]
NonlinearSolveBase = {path = "../NonlinearSolveBase"}

[compat]
ADTypes = "1.9.0"
Aqua = "0.8"
Expand Down
3 changes: 3 additions & 0 deletions lib/NonlinearSolveHomotopyContinuation/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
TaylorDiff = "b36ab563-344f-407b-a36a-4f200bebf99c"

[sources]
NonlinearSolveBase = {path = "../NonlinearSolveBase"}

[compat]
ADTypes = "1.11.0"
Aqua = "0.8"
Expand Down
3 changes: 3 additions & 0 deletions lib/NonlinearSolveQuasiNewton/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SciMLOperators = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"

[sources]
NonlinearSolveBase = {path = "../NonlinearSolveBase"}

[weakdeps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

Expand Down
3 changes: 3 additions & 0 deletions lib/NonlinearSolveSpectralMethods/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"

[sources]
NonlinearSolveBase = {path = "../NonlinearSolveBase"}

[weakdeps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

Expand Down
4 changes: 4 additions & 0 deletions lib/SimpleNonlinearSolve/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"

[sources]
BracketingNonlinearSolve = {path = "../BracketingNonlinearSolve"}
NonlinearSolveBase = {path = "../NonlinearSolveBase"}

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
Expand Down
2 changes: 1 addition & 1 deletion lib/SimpleNonlinearSolve/test/core/forward_diff_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
jacobian_f(u, p::Number) = one.(u) .* (1 / (2 * √p))
jacobian_f(u, p::AbstractArray) = diagm(vec(@. 1 / (2 * √p)))

@testset "#(nameof(typeof(alg)))" for alg in (
@testset "$(nameof(typeof(alg)))" for alg in (
SimpleNewtonRaphson(),
SimpleTrustRegion(),
SimpleTrustRegion(; nlsolve_update_rule = Val(true)),
Expand Down
Loading