@@ -5,16 +5,27 @@ struct HessianCache{T,fdtype,inplace}
55 xmm:: T
66end
77
8+ # used to dispatch on StaticArrays
9+ _hessian_inplace (:: Type{T} ) where T = Val (ArrayInterface. ismutable (T))
10+ _hessian_inplace (x) = _hessian_inplace (typeof (x))
11+ __Symmetric (x) = Symmetric (x)
12+
13+ function mutable_zeromatrix (x)
14+ A = ArrayInterface. zeromatrix (x)
15+ ArrayInterface. ismutable (A) ? A : Base. copymutable (A)
16+ end
17+
18+
819function HessianCache (xpp,xpm,xmp,xmm,
920 fdtype= Val (:hcentral ),
10- inplace = x isa StaticArray ? Val ( false ) : Val ( true ))
21+ inplace = _hessian_inplace (x ))
1122 fdtype isa Type && (fdtype = fdtype ())
1223 inplace isa Type && (inplace = inplace ())
1324 HessianCache {typeof(xpp),fdtype,inplace} (xpp,xpm,xmp,xmm)
1425end
1526
1627function HessianCache (x, fdtype= Val (:hcentral ),
17- inplace = x isa StaticArray ? Val ( false ) : Val ( true ))
28+ inplace = _hessian_inplace (x ))
1829 cx = copy (x)
1930 fdtype isa Type && (fdtype = fdtype ())
2031 inplace isa Type && (inplace = inplace ())
2334
2435function finite_difference_hessian (f, x,
2536 fdtype = Val (:hcentral ),
26- inplace = x isa StaticArray ? Val ( false ) : Val ( true );
37+ inplace = _hessian_inplace (x );
2738 relstep = default_relstep (fdtype, eltype (x)),
2839 absstep = relstep)
2940
@@ -36,16 +47,15 @@ function finite_difference_hessian(
3647 cache:: HessianCache{T,fdtype,inplace} ;
3748 relstep= default_relstep (fdtype, eltype (x)),
3849 absstep= relstep) where {T,fdtype,inplace}
39- _H = false .* x .* x'
40- _H isa SMatrix ? H = MArray (_H) : H = _H
50+ H = mutable_zeromatrix (x)
4151 finite_difference_hessian! (H, f, x, cache; relstep= relstep, absstep= absstep)
42- Symmetric (_H isa SMatrix ? SArray (H) : H)
52+ __Symmetric ( H)
4353end
4454
4555function finite_difference_hessian! (H,f,
4656 x,
4757 fdtype = Val (:hcentral ),
48- inplace = x isa StaticArray ? Val ( false ) : Val ( true );
58+ inplace = _hessian_inplace (x );
4959 relstep= default_relstep (fdtype, eltype (x)),
5060 absstep= relstep)
5161
0 commit comments