/
hessian_plan.jl
575 lines (513 loc) · 20.7 KB
/
hessian_plan.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
@doc raw"""
AbstractHessianSolverState <: AbstractGradientSolverState
An [`AbstractManoptSolverState`](@ref) type to represent algorithms that employ the Hessian.
These options are assumed to have a field (`gradient`) to store the current gradient ``\operatorname{grad}f(x)``
"""
abstract type AbstractHessianSolverState <: AbstractGradientSolverState end
"""
AbstractManifoldHessianObjective{T<:AbstractEvaluationType,TC,TG,TH} <: AbstractManifoldGradientObjective{T,TC,TG}
An abstract type for all objectives that provide a (full) Hessian, where
`T` is a [`AbstractEvaluationType`](@ref) for the gradient and Hessian functions.
"""
abstract type AbstractManifoldHessianObjective{E<:AbstractEvaluationType,TC,TG,TH} <:
AbstractManifoldGradientObjective{E,TC,TG} end
@doc raw"""
ManifoldHessianObjective{T<:AbstractEvaluationType,C,G,H,Pre} <: AbstractManifoldHessianObjective{T,C,G,H}
specify a problem for Hessian based algorithms.
# Fields
* `cost`: a function ``f:\mathcal M→ℝ`` to minimize
* `gradient`: the gradient ``\operatorname{grad}f:\mathcal M → \mathcal T\mathcal M`` of the cost function ``f``
* `hessian`: the Hessian ``\operatorname{Hess}f(x)[⋅]: \mathcal T_{x} \mathcal M → \mathcal T_{x} \mathcal M`` of the cost function ``f``
* `preconditioner`: the symmetric, positive definite preconditioner
as an approximation of the inverse of the Hessian of ``f``, a map with the same
input variables as the `hessian` to numerically stabilize iterations when the Hessian is
ill-conditioned
Depending on the [`AbstractEvaluationType`](@ref) `T` the gradient and can have to forms
* as a function `(M, p) -> X` and `(M, p, X) -> Y`, resp., an [`AllocatingEvaluation`](@ref)
* as a function `(M, X, p) -> X` and (M, Y, p, X), resp., an [`InplaceEvaluation`](@ref)
# Constructor
ManifoldHessianObjective(f, grad_f, Hess_f, preconditioner = (M, p, X) -> X;
evaluation=AllocatingEvaluation())
# See also
[`truncated_conjugate_gradient_descent`](@ref), [`trust_regions`](@ref)
"""
struct ManifoldHessianObjective{T<:AbstractEvaluationType,C,G,H,Pre} <:
AbstractManifoldHessianObjective{T,C,G,H}
cost::C
gradient!!::G
hessian!!::H
preconditioner!!::Pre
function ManifoldHessianObjective(
cost::C,
grad::G,
hess::H,
precond=nothing;
evaluation::AbstractEvaluationType=AllocatingEvaluation(),
) where {C,G,H}
if isnothing(precond)
if evaluation isa InplaceEvaluation
precond = (M, Y, p, X) -> (Y .= X)
else
precond = (M, p, X) -> X
end
end
return new{typeof(evaluation),C,G,H,typeof(precond)}(cost, grad, hess, precond)
end
end
@doc raw"""
Y = get_hessian(amp::AbstractManoptProblem{T}, p, X)
get_hessian!(amp::AbstractManoptProblem{T}, Y, p, X)
evaluate the Hessian of an [`AbstractManoptProblem`](@ref) `amp` at `p`
applied to a tangent vector `X`, computing ``\operatorname{Hess}f(q)[X]``,
which can also happen in-place of `Y`.
"""
function get_hessian(amp::AbstractManoptProblem, p, X)
return get_hessian(get_manifold(amp), get_objective(amp), p, X)
end
function get_hessian!(amp::AbstractManoptProblem, Y, p, X)
return get_hessian!(get_manifold(amp), Y, get_objective(amp), p, X)
end
function get_hessian(M::AbstractManifold, admo::AbstractDecoratedManifoldObjective, p, X)
return get_hessian(M, get_objective(admo, false), p, X)
end
function get_hessian(
M::AbstractManifold, mho::ManifoldHessianObjective{AllocatingEvaluation}, p, X
)
return mho.hessian!!(M, p, X)
end
function get_hessian(
M::AbstractManifold, mho::ManifoldHessianObjective{InplaceEvaluation}, p, X
)
Y = zero_vector(M, p)
mho.hessian!!(M, Y, p, X)
return Y
end
function get_hessian!(
M::AbstractManifold, Y, admo::AbstractDecoratedManifoldObjective, p, X
)
return get_hessian!(M, Y, get_objective(admo, false), p, X)
end
function get_hessian!(
M::AbstractManifold, Y, mho::ManifoldHessianObjective{AllocatingEvaluation}, p, X
)
copyto!(M, Y, p, mho.hessian!!(M, p, X))
return Y
end
function get_hessian!(
M::AbstractManifold, Y, mho::ManifoldHessianObjective{InplaceEvaluation}, p, X
)
mho.hessian!!(M, Y, p, X)
return Y
end
@doc raw"""
get_gradient_function(amgo::AbstractManifoldGradientObjective{E<:AbstractEvaluationType})
return the function to evaluate (just) the Hessian ``\operatorname{Hess} f(p)``.
Depending on the [`AbstractEvaluationType`](@ref) `E` this is a function
* `(M, p, X) -> Y` for the [`AllocatingEvaluation`](@ref) case
* `(M, Y, p, X) -> X` for the [`InplaceEvaluation`](@ref), working in-place of `Y`.
"""
get_hessian_function(mho::ManifoldHessianObjective, recursive=false) = mho.hessian!!
function get_hessian_function(admo::AbstractDecoratedManifoldObjective, recursive=false)
return get_hessian_function(get_objective(admo, recursive))
end
@doc raw"""
get_preconditioner(amp::AbstractManoptProblem, p, X)
evaluate the symmetric, positive definite preconditioner (approximation of the
inverse of the Hessian of the cost function `f`) of a
[`AbstractManoptProblem`](@ref) `amp`s objective at the point `p` applied to a
tangent vector `X`.
"""
function get_preconditioner(amp::AbstractManoptProblem, p, X)
return get_preconditioner(get_manifold(amp), get_objective(amp), p, X)
end
function get_preconditioner!(amp::AbstractManoptProblem, Y, p, X)
return get_preconditioner!(get_manifold(amp), Y, get_objective(amp), p, X)
end
@doc raw"""
get_preconditioner(M::AbstractManifold, mho::ManifoldHessianObjective, p, X)
evaluate the symmetric, positive definite preconditioner (approximation of the
inverse of the Hessian of the cost function `F`) of a
[`ManifoldHessianObjective`](@ref) `mho` at the point `p` applied to a
tangent vector `X`.
"""
function get_preconditioner(
M::AbstractManifold, mho::ManifoldHessianObjective{AllocatingEvaluation}, p, X
)
return mho.preconditioner!!(M, p, X)
end
function get_preconditioner(
M::AbstractManifold, mho::ManifoldHessianObjective{InplaceEvaluation}, p, X
)
Y = zero_vector(M, p)
mho.preconditioner!!(M, Y, p, X)
return Y
end
function get_preconditioner(
M::AbstractManifold, admo::AbstractDecoratedManifoldObjective, p, X
)
return get_preconditioner(M, get_objective(admo, false), p, X)
end
function get_preconditioner!(
M::AbstractManifold, Y, mho::ManifoldHessianObjective{AllocatingEvaluation}, p, X
)
copyto!(M, Y, p, mho.preconditioner!!(M, p, X))
return Y
end
function get_preconditioner!(
M::AbstractManifold, Y, admo::AbstractDecoratedManifoldObjective, p, X
)
return get_preconditioner!(M, Y, get_objective(admo, false), p, X)
end
function get_preconditioner!(
M::AbstractManifold, Y, mho::ManifoldHessianObjective{InplaceEvaluation}, p, X
)
mho.preconditioner!!(M, Y, p, X)
return Y
end
update_hessian!(M, f, p, p_proposal, X) = f
update_hessian_basis!(M, f, p) = f
@doc raw"""
AbstractApproxHessian <: Function
An abstract supertypes for approximate Hessian functions, declares them also to be functions.
"""
abstract type AbstractApproxHessian <: Function end
@doc raw"""
ApproxHessianFiniteDifference{E, P, T, G, RTR,, VTR, R <: Real} <: AbstractApproxHessian
A functor to approximate the Hessian by a finite difference of gradient evaluation.
Given a point `p` and a direction `X` and the gradient ``\operatorname{grad}F: \mathcal M → T\mathcal M``
of a function ``F`` the Hessian is approximated as follows:
let ``c`` be a stepsize, ``X∈ T_p\mathcal M`` a tangent vector and ``q = \operatorname{retr}_p(\frac{c}{\lVert X \rVert_p}X)``
be a step in direction ``X`` of length ``c`` following a retraction
Then the Hessian is approximated by the finite difference of the gradients, where ``\mathcal T_{\cdot\gets\cdot}`` is a vector transport.
```math
\operatorname{Hess}F(p)[X] ≈
\frac{\lVert X \rVert_p}{c}\Bigl(
\mathcal T_{p\gets q}\bigr(\operatorname{grad}F(q)\bigl) - \operatorname{grad}F(p)
\Bigl)
```
# Fields
* `gradient!!`: the gradient function (either allocating or mutating, see `evaluation` parameter)
* `step_length`: a step length for the finite difference
* `retraction_method`: a retraction to use
* `vector_transport_method`: a vector transport to use
## Internal temporary fields
* `grad_tmp`: a temporary storage for the gradient at the current `p`
* `grad_dir_tmp`: a temporary storage for the gradient at the current `p_dir`
* `p_dir::P`: a temporary storage to the forward direction (or the ``q`` in the formula)
# Constructor
ApproximateFiniteDifference(M, p, grad_f; kwargs...)
## Keyword arguments
* `evaluation`: ([`AllocatingEvaluation`](@ref)) whether the gradient is given as an allocation function or an in-place ([`InplaceEvaluation`](@ref)).
* `steplength`: (``2^{-14}``) step length ``c`` to approximate the gradient evaluations
* `retraction_method`: (`default_retraction_method(M, typeof(p))`) a `retraction(M, p, X)` to use in the approximation.
* `vector_transport_method`: (`default_vector_transport_method(M, typeof(p))`) a vector transport to use
"""
mutable struct ApproxHessianFiniteDifference{E,P,T,G,RTR,VTR,R<:Real} <:
AbstractApproxHessian
p_dir::P
gradient!!::G
grad_tmp::T
grad_tmp_dir::T
retraction_method::RTR
vector_transport_method::VTR
steplength::R
end
function ApproxHessianFiniteDifference(
M::mT,
p::P,
grad_f::G;
tangent_vector=zero_vector(M, p),
steplength::R=2^-14,
evaluation=AllocatingEvaluation(),
retraction_method::RTR=default_retraction_method(M, typeof(p)),
vector_transport_method::VTR=default_vector_transport_method(M, typeof(p)),
) where {
mT<:AbstractManifold,
P,
G,
R<:Real,
RTR<:AbstractRetractionMethod,
VTR<:AbstractVectorTransportMethod,
}
X = copy(M, p, tangent_vector)
Y = copy(M, p, tangent_vector)
return ApproxHessianFiniteDifference{typeof(evaluation),P,typeof(X),G,RTR,VTR,R}(
p, grad_f, X, Y, retraction_method, vector_transport_method, steplength
)
end
function (f::ApproxHessianFiniteDifference{AllocatingEvaluation})(M, p, X)
norm_X = norm(M, p, X)
(norm_X ≈ zero(norm_X)) && return zero_vector(M, p)
c = f.steplength / norm_X
f.grad_tmp .= f.gradient!!(M, p)
retract!(M, f.p_dir, p, c * X, f.retraction_method)
f.grad_tmp_dir .= f.gradient!!(M, f.p_dir)
vector_transport_to!(
M, f.grad_tmp_dir, f.p_dir, f.grad_tmp_dir, p, f.vector_transport_method
)
return (1 / c) * (f.grad_tmp_dir - f.grad_tmp)
end
function (f::ApproxHessianFiniteDifference{InplaceEvaluation})(M, Y, p, X)
norm_X = norm(M, p, X)
(norm_X ≈ zero(norm_X)) && return zero_vector!(M, X, p)
c = f.steplength / norm_X
f.gradient!!(M, f.grad_tmp, p)
retract!(M, f.p_dir, p, c * X, f.retraction_method)
f.gradient!!(M, f.grad_tmp_dir, f.p_dir)
vector_transport_to!(
M, f.grad_tmp_dir, f.p_dir, f.grad_tmp_dir, p, f.vector_transport_method
)
Y .= (1 / c) .* (f.grad_tmp_dir .- f.grad_tmp)
return Y
end
@doc raw"""
ApproxHessianSymmetricRankOne{E, P, G, T, B<:AbstractBasis{ℝ}, VTR, R<:Real} <: AbstractApproxHessian
A functor to approximate the Hessian by the symmetric rank one update.
# Fields
* `gradient!!` the gradient function (either allocating or mutating, see `evaluation` parameter).
* `ν` a small real number to ensure that the denominator in the update does not become too small and thus the method does not break down.
* `vector_transport_method` a vector transport to use.
## Internal temporary fields
* `p_tmp` a temporary storage the current point `p`.
* `grad_tmp` a temporary storage for the gradient at the current `p`.
* `matrix` a temporary storage for the matrix representation of the approximating operator.
* `basis` a temporary storage for an orthonormal basis at the current `p`.
# Constructor
ApproxHessianSymmetricRankOne(M, p, gradF; kwargs...)
## Keyword arguments
* `initial_operator` (`Matrix{Float64}(I, manifold_dimension(M), manifold_dimension(M))`) the matrix representation of the initial approximating operator.
* `basis` (`DefaultOrthonormalBasis()`) an orthonormal basis in the tangent space of the initial iterate p.
* `nu` (`-1`)
* `evaluation` ([`AllocatingEvaluation`](@ref)) whether the gradient is given as an allocation function or an in-place ([`InplaceEvaluation`](@ref)).
* `vector_transport_method` (`ParallelTransport()`) vector transport ``\mathcal T_{\cdot\gets\cdot}`` to use.
"""
mutable struct ApproxHessianSymmetricRankOne{E,P,G,T,B<:AbstractBasis{ℝ},VTR,R<:Real} <:
AbstractApproxHessian
p_tmp::P
gradient!!::G
grad_tmp::T
matrix::Matrix
basis::B
vector_transport_method::VTR
ν::R
end
function ApproxHessianSymmetricRankOne(
M::mT,
p::P,
gradient::G;
initial_operator::AbstractMatrix=Matrix{Float64}(
I, manifold_dimension(M), manifold_dimension(M)
),
basis::B=DefaultOrthonormalBasis(),
nu::R=-1.0,
evaluation=AllocatingEvaluation(),
vector_transport_method::VTR=ParallelTransport(),
) where {
mT<:AbstractManifold,P,G,B<:AbstractBasis{ℝ},R<:Real,VTR<:AbstractVectorTransportMethod
}
if evaluation isa AllocatingEvaluation
grad_tmp = gradient(M, p)
elseif evaluation isa InplaceEvaluation
grad_tmp = zero_vector(M, p)
gradient(M, grad_tmp, p)
end
return ApproxHessianSymmetricRankOne{typeof(evaluation),P,G,typeof(grad_tmp),B,VTR,R}(
p, gradient, grad_tmp, initial_operator, basis, vector_transport_method, nu
)
end
function (f::ApproxHessianSymmetricRankOne{AllocatingEvaluation})(M, p, X)
# Update Basis if necessary
if p != f.p_tmp
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(M, f.p_tmp, p)
f.grad_tmp = f.gradient!!(M, f.p_tmp)
end
# Apply Hessian approximation on vector
return get_vector(
M, f.p_tmp, f.matrix * get_coordinates(M, f.p_tmp, X, f.basis), f.basis
)
end
function (f::ApproxHessianSymmetricRankOne{InplaceEvaluation})(M, Y, p, X)
# Update Basis if necessary
if p != f.p_tmp
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(f.p_tmp, p)
f.gradient!!(M, f.grad_tmp, f.p_tmp)
end
# Apply Hessian approximation on vector
Y .= get_vector(M, f.p_tmp, f.matrix * get_coordinates(M, f.p_tmp, X, f.basis), f.basis)
return Y
end
function update_hessian!(
M, f::ApproxHessianSymmetricRankOne{AllocatingEvaluation}, p, p_proposal, X
)
yk_c = get_coordinates(
M,
p,
vector_transport_to(
M, p_proposal, f.gradient!!(M, p_proposal), p, f.vector_transport_method
) - f.grad_tmp,
f.basis,
)
sk_c = get_coordinates(M, p, X, f.basis)
srvec = yk_c - f.matrix * sk_c
if f.ν < 0 || abs(dot(srvec, sk_c)) >= f.ν * norm(srvec) * norm(sk_c)
f.matrix = f.matrix + srvec * srvec' / (srvec' * sk_c)
end
end
function update_hessian!(
M::AbstractManifold,
f::ApproxHessianSymmetricRankOne{InplaceEvaluation},
p,
p_proposal,
X,
)
grad_proposal = zero_vector(M, p_proposal)
f.gradient!!(M, grad_proposal, p_proposal)
yk_c = get_coordinates(
M,
p,
vector_transport_to(M, p_proposal, grad_proposal, p, f.vector_transport_method) -
f.grad_tmp,
f.basis,
)
sk_c = get_coordinates(M, p, X, f.basis)
srvec = yk_c - f.matrix * sk_c
if f.ν < 0 || abs(dot(srvec, sk_c)) >= f.ν * norm(srvec) * norm(sk_c)
f.matrix = f.matrix + srvec * srvec' / (srvec' * sk_c)
end
end
function update_hessian_basis!(M, f::ApproxHessianSymmetricRankOne{AllocatingEvaluation}, p)
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(f.p_tmp, p)
return f.grad_tmp = f.gradient!!(M, f.p_tmp)
end
function update_hessian_basis!(M, f::ApproxHessianSymmetricRankOne{InplaceEvaluation}, p)
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(f.p_tmp, p)
return f.gradient!!(M, f.grad_tmp, f.p_tmp)
end
@doc raw"""
ApproxHessianBFGS{E, P, G, T, B<:AbstractBasis{ℝ}, VTR, R<:Real} <: AbstractApproxHessian
A functor to approximate the Hessian by the BFGS update.
# Fields
* `gradient!!` the gradient function (either allocating or mutating, see `evaluation` parameter).
* `scale`
* `vector_transport_method` a vector transport to use.
## Internal temporary fields
* `p_tmp` a temporary storage the current point `p`.
* `grad_tmp` a temporary storage for the gradient at the current `p`.
* `matrix` a temporary storage for the matrix representation of the approximating operator.
* `basis` a temporary storage for an orthonormal basis at the current `p`.
# Constructor
ApproxHessianBFGS(M, p, gradF; kwargs...)
## Keyword arguments
* `initial_operator` (`Matrix{Float64}(I, manifold_dimension(M), manifold_dimension(M))`) the matrix representation of the initial approximating operator.
* `basis` (`DefaultOrthonormalBasis()`) an orthonormal basis in the tangent space of the initial iterate p.
* `nu` (`-1`)
* `evaluation` ([`AllocatingEvaluation`](@ref)) whether the gradient is given as an allocation function or an in-place ([`InplaceEvaluation`](@ref)).
* `vector_transport_method` (`ParallelTransport()`) vector transport ``\mathcal T_{\cdot\gets\cdot}`` to use.
"""
mutable struct ApproxHessianBFGS{
E,P,G,T,B<:AbstractBasis{ℝ},VTR<:AbstractVectorTransportMethod
} <: AbstractApproxHessian
p_tmp::P
gradient!!::G
grad_tmp::T
matrix::Matrix
basis::B
vector_transport_method::VTR
scale::Bool
end
function ApproxHessianBFGS(
M::mT,
p::P,
gradient::G;
initial_operator::AbstractMatrix=Matrix{Float64}(
I, manifold_dimension(M), manifold_dimension(M)
),
basis::B=DefaultOrthonormalBasis(),
scale::Bool=true,
evaluation=AllocatingEvaluation(),
vector_transport_method::VTR=ParallelTransport(),
) where {mT<:AbstractManifold,P,G,B<:AbstractBasis{ℝ},VTR<:AbstractVectorTransportMethod}
if evaluation == AllocatingEvaluation()
grad_tmp = gradient(M, p)
elseif evaluation == InplaceEvaluation()
grad_tmp = zero_vector(M, p)
gradient(M, grad_tmp, p)
end
return ApproxHessianBFGS{typeof(evaluation),P,G,typeof(grad_tmp),B,VTR}(
p, gradient, grad_tmp, initial_operator, basis, vector_transport_method, scale
)
end
function (f::ApproxHessianBFGS{AllocatingEvaluation})(M, p, X)
# Update Basis if necessary
if p != f.p_tmp
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(M, f.p_tmp, p)
f.grad_tmp = f.gradient!!(M, f.p_tmp)
end
# Apply Hessian approximation on vector
return get_vector(
M, f.p_tmp, f.matrix * get_coordinates(M, f.p_tmp, X, f.basis), f.basis
)
end
function (f::ApproxHessianBFGS{InplaceEvaluation})(M, Y, p, X)
# Update Basis if necessary
if p != f.p_tmp
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(M, f.p_tmp, p)
f.gradient!!(M, f.grad_tmp, f.p_tmp)
end
# Apply Hessian approximation on vector
Y .= get_vector(M, f.p_tmp, f.matrix * get_coordinates(M, f.p_tmp, X, f.basis), f.basis)
return Y
end
function update_hessian!(
M::AbstractManifold, f::ApproxHessianBFGS{AllocatingEvaluation}, p, p_proposal, X
)
yk_c = get_coordinates(
M,
p,
vector_transport_to(
M, p_proposal, f.gradient!!(M, p_proposal), p, f.vector_transport_method
) - f.grad_tmp,
f.basis,
)
sk_c = get_coordinates(M, p, X, f.basis)
skyk_c = dot(sk_c, yk_c)
f.matrix =
f.matrix + yk_c * yk_c' / skyk_c -
f.matrix * sk_c * sk_c' * f.matrix / dot(sk_c, f.matrix * sk_c)
return f
end
function update_hessian!(M, f::ApproxHessianBFGS{InplaceEvaluation}, p, p_proposal, X)
grad_proposal = zero_vector(M, p_proposal)
f.gradient!!(M, grad_proposal, p_proposal)
yk_c = get_coordinates(
M,
p,
vector_transport_to(M, p_proposal, grad_proposal, p, f.vector_transport_method) -
f.grad_tmp,
f.basis,
)
sk_c = get_coordinates(M, p, X, f.basis)
skyk_c = dot(sk_c, yk_c)
f.matrix =
f.matrix + yk_c * yk_c' / skyk_c -
f.matrix * sk_c * sk_c' * f.matrix / dot(sk_c, f.matrix * sk_c)
return f
end
function update_hessian_basis!(M, f::ApproxHessianBFGS{AllocatingEvaluation}, p)
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(f.p_tmp, p)
f.grad_tmp = f.gradient!!(M, f.p_tmp)
return f
end
function update_hessian_basis!(M, f::ApproxHessianBFGS{InplaceEvaluation}, p)
update_basis!(f.basis, M, f.p_tmp, p, f.vector_transport_method)
copyto!(f.p_tmp, p)
f.gradient!!(M, f.grad_tmp, f.p_tmp)
return f
end