Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit e1e1be8

Browse files
committed
DFSane non-allocating for scalars now
1 parent a66084b commit e1e1be8

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
2020

2121
[weakdeps]
2222
PolyesterForwardDiff = "98d1487c-24ca-40b6-b7ab-df2af84e126b"
23+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2324

2425
[extensions]
2526
SimpleNonlinearSolvePolyesterForwardDiffExt = "PolyesterForwardDiff"
27+
SimpleNonlinearSolveStaticArraysExt = "StaticArrays"
2628

2729
[compat]
2830
ADTypes = "0.2.6"
@@ -38,4 +40,5 @@ PrecompileTools = "1"
3840
Reexport = "1"
3941
SciMLBase = "2.7"
4042
StaticArraysCore = "1.4"
43+
StaticArrays = "1"
4144
julia = "1.9"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module SimpleNonlinearSolveStaticArraysExt
2+
3+
using SimpleNonlinearSolve
4+
5+
@inline SimpleNonlinearSolve.__is_extension_loaded(::Val{:StaticArrays}) = true
6+
7+
end

src/linesearch.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ end
3737
end
3838

3939
(alg::LiFukushimaLineSearch)(prob, fu, u) = __generic_init(alg, prob, fu, u)
40-
function (alg::LiFukushimaLineSearch)(prob, fu::SArray, u::SArray)
40+
function (alg::LiFukushimaLineSearch)(prob, fu::Union{Number, SArray},
41+
u::Union{Number, SArray})
4142
(alg.nan_maxiters === missing || alg.nan_maxiters === nothing) &&
4243
return __static_init(alg, prob, fu, u)
4344
@warn "`LiFukushimaLineSearch` with NaN checking is not non-allocating" maxlog=1

src/nlsolve/dfsane.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ see the paper [1].
99
1010
### Keyword Arguments
1111
12-
- `σ_min`: the minimum value of the spectral coefficient `σ_k` which is related to the step
13-
size in the algorithm. Defaults to `1e-10`.
14-
- `σ_max`: the maximum value of the spectral coefficient `σ_k` which is related to the step
15-
size in the algorithm. Defaults to `1e10`.
12+
- `σ_min`: the minimum value of the spectral coefficient `σ_k` which is related to the
13+
step size in the algorithm. Defaults to `1e-10`.
14+
- `σ_max`: the maximum value of the spectral coefficient `σ_k` which is related to the
15+
step size in the algorithm. Defaults to `1e10`.
1616
- `σ_1`: the initial value of the spectral coefficient `σ_k` which is related to the step
1717
size in the algorithm.. Defaults to `1.0`.
1818
- `M`: The monotonicity of the algorithm is determined by a this positive integer.
1919
A value of 1 for `M` would result in strict monotonicity in the decrease of the L2-norm
20-
of the function `f`. However, higher values allow for more flexibility in this reduction.
21-
Despite this, the algorithm still ensures global convergence through the use of a
22-
non-monotone line-search algorithm that adheres to the Grippo-Lampariello-Lucidi
20+
of the function `f`. However, higher values allow for more flexibility in this
21+
reduction. Despite this, the algorithm still ensures global convergence through the use
22+
of a non-monotone line-search algorithm that adheres to the Grippo-Lampariello-Lucidi
2323
condition. Values in the range of 5 to 20 are usually sufficient, but some cases may call
2424
for a higher value of `M`. The default setting is 10.
25-
- `γ`: a parameter that influences if a proposed step will be accepted. Higher value of `γ`
26-
will make the algorithm more restrictive in accepting steps. Defaults to `1e-4`.
25+
- `γ`: a parameter that influences if a proposed step will be accepted. Higher value of
26+
`γ` will make the algorithm more restrictive in accepting steps. Defaults to `1e-4`.
2727
- `τ_min`: if a step is rejected the new step size will get multiplied by factor, and this
2828
parameter is the minimum value of that factor. Defaults to `0.1`.
2929
- `τ_max`: if a step is rejected the new step size will get multiplied by factor, and this
3030
parameter is the maximum value of that factor. Defaults to `0.5`.
3131
- `nexp`: the exponent of the loss, i.e. ``f_k=||F(x_k)||^{nexp}``. The paper uses
3232
`nexp ∈ {1,2}`. Defaults to `2`.
3333
- `η_strategy`: function to determine the parameter `η_k`, which enables growth
34-
of ``||F||^2``. Called as ``η_k = η_strategy(f_1, k, x, F)`` with `f_1` initialized as
34+
of ``||F||^2``. Called as `η_k = η_strategy(f_1, k, x, F)` with `f_1` initialized as
3535
``f_1=||F(x_1)||^{nexp}``, `k` is the iteration number, `x` is the current `x`-value and
3636
`F` the current residual. Should satisfy ``η_k > 0`` and ``∑ₖ ηₖ < ∞``. Defaults to
3737
``||F||^2 / k^2``.
@@ -83,7 +83,8 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleDFSane{M}, args...
8383
α_1 = one(T)
8484
f_1 = fx_norm
8585

86-
history_f_k = if x isa SArray
86+
history_f_k = if x isa SArray ||
87+
(x isa Number && __is_extension_loaded(Val(:StaticArrays)))
8788
ones(SVector{M, T}) * fx_norm
8889
else
8990
fill(fx_norm, M)

test/basictests.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ end
158158
@test true
159159
catch e
160160
@error e
161-
# History Vector Allocates
162-
@test false broken=(alg isa SimpleDFSane)
161+
@test false
163162
end
164163

165164
# ForwardDiff allocates for hessian since we don't propagate the chunksize

0 commit comments

Comments
 (0)