Skip to content

Commit edd6dae

Browse files
committed
Pretty printing
1 parent 3cd445d commit edd6dae

12 files changed

+109
-30
lines changed

src/NonlinearSolve.jl

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,6 @@ const False = Val(false)
8888

8989
# __reinit_internal!(::AbstractNonlinearSolveCache; kwargs...) = nothing
9090

91-
# function Base.show(io::IO, alg::AbstractNonlinearSolveAlgorithm)
92-
# str = "$(nameof(typeof(alg)))("
93-
# modifiers = String[]
94-
# if __getproperty(alg, Val(:ad)) !== nothing
95-
# push!(modifiers, "ad = $(nameof(typeof(alg.ad)))()")
96-
# end
97-
# if __getproperty(alg, Val(:linsolve)) !== nothing
98-
# push!(modifiers, "linsolve = $(nameof(typeof(alg.linsolve)))()")
99-
# end
100-
# if __getproperty(alg, Val(:linesearch)) !== nothing
101-
# ls = alg.linesearch
102-
# if ls isa LineSearch
103-
# ls.method !== nothing &&
104-
# push!(modifiers, "linesearch = $(nameof(typeof(ls.method)))()")
105-
# else
106-
# push!(modifiers, "linesearch = $(nameof(typeof(alg.linesearch)))()")
107-
# end
108-
# end
109-
# append!(modifiers, __alg_print_modifiers(alg))
110-
# if __getproperty(alg, Val(:radius_update_scheme)) !== nothing
111-
# push!(modifiers, "radius_update_scheme = $(alg.radius_update_scheme)")
112-
# end
113-
# str = str * join(modifiers, ", ")
114-
# print(io, "$(str))")
115-
# return nothing
116-
# end
117-
118-
# __alg_print_modifiers(_) = String[]
119-
12091
include("abstract_types.jl")
12192
include("internal/helpers.jl")
12293

src/abstract_types.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ Abstract Type for all Line Search Algorithms used in NonlinearSolve.jl.
105105
"""
106106
abstract type AbstractNonlinearSolveLineSearchAlgorithm end
107107

108+
function Base.show(io::IO, alg::AbstractNonlinearSolveLineSearchAlgorithm)
109+
print(io, "$(nameof(typeof(alg)))()")
110+
end
111+
108112
abstract type AbstractNonlinearSolveLineSearchCache end
109113

110114
"""
@@ -179,8 +183,25 @@ end
179183

180184
abstract type AbstractJacobianInitialization end
181185

186+
function Base.show(io::IO, alg::AbstractJacobianInitialization)
187+
modifiers = String[]
188+
hasfield(typeof(alg), :structure) &&
189+
push!(modifiers, "structure = $(nameof(typeof(alg.structure)))()")
190+
print(io, "$(nameof(typeof(alg)))($(join(modifiers, ", ")))")
191+
return nothing
192+
end
193+
182194
abstract type AbstractApproximateJacobianUpdateRule{INV} end
183195

196+
function Base.show(io::IO, alg::AbstractApproximateJacobianUpdateRule{INV}) where {INV}
197+
if INV
198+
print(io, "$(nameof(typeof(alg)))(stores_inverse = true)")
199+
else
200+
print(io, "$(nameof(typeof(alg)))()")
201+
end
202+
return nothing
203+
end
204+
184205
store_inverse_jacobian(::AbstractApproximateJacobianUpdateRule{INV}) where {INV} = INV
185206

186207
abstract type AbstractApproximateJacobianUpdateRuleCache{INV} end
@@ -189,6 +210,11 @@ store_inverse_jacobian(::AbstractApproximateJacobianUpdateRuleCache{INV}) where
189210

190211
abstract type AbstractResetCondition end
191212

213+
function Base.show(io::IO, alg::AbstractResetCondition)
214+
print(io, "$(nameof(typeof(alg)))()")
215+
return nothing
216+
end
217+
192218
abstract type AbstractTrustRegionMethod end
193219

194220
abstract type AbstractTrustRegionMethodCache end

src/core/approximate_jacobian.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99
initialization
1010
end
1111

12+
function Base.show(io::IO,
13+
alg::ApproximateJacobianSolveAlgorithm{concrete_jac, name}) where {concrete_jac, name}
14+
modifiers = String[]
15+
__is_present(alg.linesearch) && push!(modifiers, "linesearch = $(alg.linesearch)")
16+
__is_present(alg.trustregion) && push!(modifiers, "trustregion = $(alg.trustregion)")
17+
push!(modifiers, "descent = $(alg.descent)")
18+
push!(modifiers, "update_rule = $(alg.update_rule)")
19+
push!(modifiers, "reinit_rule = $(alg.reinit_rule)")
20+
push!(modifiers, "max_resets = $(alg.max_resets)")
21+
push!(modifiers, "initialization = $(alg.initialization)")
22+
print(io, "$(name)(\n $(join(modifiers, ",\n "))\n)")
23+
end
24+
1225
function ApproximateJacobianSolveAlgorithm(; concrete_jac = nothing,
1326
name::Symbol = :unknown, kwargs...)
1427
return ApproximateJacobianSolveAlgorithm{concrete_jac, name}(; kwargs...)

src/core/generalized_first_order.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
reverse_ad
99
end
1010

11+
function Base.show(io::IO,
12+
alg::GeneralizedFirstOrderAlgorithm{concrete_jac, name}) where {concrete_jac, name}
13+
modifiers = String[]
14+
__is_present(alg.linesearch) && push!(modifiers, "linesearch = $(alg.linesearch)")
15+
__is_present(alg.trustregion) && push!(modifiers, "trustregion = $(alg.trustregion)")
16+
push!(modifiers, "descent = $(alg.descent)")
17+
__is_present(alg.jacobian_ad) && push!(modifiers, "jacobian_ad = $(alg.jacobian_ad)")
18+
__is_present(alg.forward_ad) && push!(modifiers, "forward_ad = $(alg.forward_ad)")
19+
__is_present(alg.reverse_ad) && push!(modifiers, "reverse_ad = $(alg.reverse_ad)")
20+
print(io, "$(name)(\n $(join(modifiers, ",\n "))\n)")
21+
end
22+
1123
function GeneralizedFirstOrderAlgorithm(; concrete_jac = nothing,
1224
name::Symbol = :unknown, kwargs...)
1325
return GeneralizedFirstOrderAlgorithm{concrete_jac, name}(; kwargs...)

src/core/spectral_methods.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
σ_1
99
end
1010

11+
function Base.show(io::IO, alg::GeneralizedDFSane{name}) where {name}
12+
modifiers = String[]
13+
__is_present(alg.linesearch) && push!(modifiers, "linesearch = $(alg.linesearch)")
14+
push!(modifiers, "σ_min = $(alg.σ_min)")
15+
push!(modifiers, "σ_max = $(alg.σ_max)")
16+
push!(modifiers, "σ_1 = $(alg.σ_1)")
17+
print(io, "$(name)(\n $(join(modifiers, ",\n "))\n)")
18+
end
19+
1120
concrete_jac(::GeneralizedDFSane) = nothing
1221

1322
@concrete mutable struct GeneralizedDFSaneCache{iip} <: AbstractNonlinearSolveCache{iip}

src/descent/damped_newton.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Based on the formulation we expect the damping factor returned to be a non-negat
1818
damping_fn
1919
end
2020

21+
function Base.show(io::IO, d::DampedNewtonDescent)
22+
modifiers = String[]
23+
d.linsolve !== nothing && push!(modifiers, "linsolve = $(d.linsolve)")
24+
d.precs !== DEFAULT_PRECS && push!(modifiers, "precs = $(d.precs)")
25+
push!(modifiers, "initial_damping = $(d.initial_damping)")
26+
push!(modifiers, "damping_fn = $(d.damping_fn)")
27+
print(io, "DampedNewtonDescent($(join(modifiers, ", ")))")
28+
end
29+
2130
supports_line_search(::DampedNewtonDescent) = true
2231
supports_trust_region(::DampedNewtonDescent) = true
2332

src/descent/dogleg.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ See also [`SteepestDescent`](@ref), [`NewtonDescent`](@ref), [`DampedNewtonDesce
2424
steepest_descent
2525
end
2626

27+
function Base.show(io::IO, d::Dogleg)
28+
print(io,
29+
"Dogleg(newton_descent = $(d.newton_descent), steepest_descent = $(d.steepest_descent))")
30+
end
31+
2732
supports_trust_region(::Dogleg) = true
2833

2934
function Dogleg(; linsolve = nothing, precs = DEFAULT_PRECS, damping = False,

src/descent/geodesic_acceleration.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ algorithm for nonlinear least-squares minimization." arXiv preprint arXiv:1201.5
2020
α
2121
end
2222

23+
function Base.show(io::IO, alg::GeodesicAcceleration)
24+
print(io, "GeodesicAcceleration(descent = $(alg.descent), finite_diff_step_geodesic = ",
25+
"$(alg.finite_diff_step_geodesic), α = $(alg.α))")
26+
end
27+
2328
supports_trust_region(::GeodesicAcceleration) = true
2429

2530
@concrete mutable struct GeodesicAccelerationCache <: AbstractDescentCache

src/descent/newton.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ See also [`Dogleg`](@ref), [`SteepestDescent`](@ref), [`DampedNewtonDescent`](@r
2323
precs = DEFAULT_PRECS
2424
end
2525

26+
function Base.show(io::IO, d::NewtonDescent)
27+
modifiers = String[]
28+
d.linsolve !== nothing && push!(modifiers, "linsolve = $(d.linsolve)")
29+
d.precs !== DEFAULT_PRECS && push!(modifiers, "precs = $(d.precs)")
30+
print(io, "NewtonDescent($(join(modifiers, ", ")))")
31+
end
32+
2633
supports_line_search(::NewtonDescent) = true
2734

2835
@concrete mutable struct NewtonDescentCache{pre_inverted, normalform} <:

src/descent/steepest.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ See also [`Dogleg`](@ref), [`NewtonDescent`](@ref), [`DampedNewtonDescent`](@ref
2424
precs = DEFAULT_PRECS
2525
end
2626

27+
function Base.show(io::IO, d::SteepestDescent)
28+
modifiers = String[]
29+
d.linsolve !== nothing && push!(modifiers, "linsolve = $(d.linsolve)")
30+
d.precs !== DEFAULT_PRECS && push!(modifiers, "precs = $(d.precs)")
31+
print(io, "SteepestDescent($(join(modifiers, ", ")))")
32+
end
33+
2734
supports_line_search(::SteepestDescent) = true
2835

2936
@concrete mutable struct SteepestDescentCache{pre_inverted} <: AbstractDescentCache

src/globalization/line_search.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,24 @@ differentiation for fast Vector Jacobian Products.
3333
to `AutoFiniteDiff()`, which means that finite differencing is used to compute the VJP.
3434
`AutoZygote()` will be faster in most cases, but it requires `Zygote.jl` to be manually
3535
installed and loaded.
36-
- `alpha`: the initial step size to use. Defaults to `true` (which is equivalent to `1`).
36+
- `α`: the initial step size to use. Defaults to `true` (which is equivalent to `1`).
3737
"""
3838
@concrete struct LineSearchesJL <: AbstractNonlinearSolveLineSearchAlgorithm
3939
method
4040
initial_alpha
4141
autodiff
4242
end
4343

44+
function Base.show(io::IO, alg::LineSearchesJL)
45+
str = "$(nameof(typeof(alg)))("
46+
modifiers = String[]
47+
alg.autodiff !== nothing &&
48+
push!(modifiers, "autodiff = $(nameof(typeof(alg.autodiff)))()")
49+
alg.initial_alpha != true && push!(modifiers, "initial_alpha = $(alg.initial_alpha)")
50+
push!(modifiers, "method = $(alg.method)")
51+
print(io, str, join(modifiers, ", "), ")")
52+
end
53+
4454
LineSearchesJL(method; kwargs...) = LineSearchesJL(; method, kwargs...)
4555
function LineSearchesJL(; method = LineSearches.Static(), autodiff = nothing, α = true)
4656
return LineSearchesJL(method, α, autodiff)

src/utils.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ LazyArrays.applied_axes(::typeof(__zero), x) = axes(x)
8181
@inline __get_nonsparse_ad(::AutoSparseFiniteDiff) = AutoFiniteDiff()
8282
@inline __get_nonsparse_ad(::AutoSparseZygote) = AutoZygote()
8383
@inline __get_nonsparse_ad(ad) = ad
84+
85+
# Simple Checks
86+
@inline __is_present(::Nothing) = false
87+
@inline __is_present(::Missing) = false
88+
@inline __is_present(::Any) = true

0 commit comments

Comments
 (0)