```  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
```
```// Copyright 2018-2020 argmin developers
//
// copied, modified, or distributed except according to those terms.

//! # References:
//!
//! [0] Jorge Nocedal and Stephen J. Wright (2006). Numerical Optimization.
//! Springer. ISBN 0-387-30303-0.

use crate::prelude::*;
use serde::{Deserialize, Serialize};

/// The Dogleg method computes the intersection of the trust region boundary with a path given by
/// the unconstraind minimum along the steepest descent direction and the optimum of the quadratic
/// approximation of the cost function at the current point.
///
/// # References:
///
/// [0] Jorge Nocedal and Stephen J. Wright (2006). Numerical Optimization.
/// Springer. ISBN 0-387-30303-0.
#[derive(Clone, Serialize, Deserialize, Debug, Copy, PartialEq, PartialOrd, Default)]
pub struct Dogleg<F> {
}

impl<F: ArgminFloat> Dogleg<F> {
/// Constructor
pub fn new() -> Self {
}
}

impl<O, F> Solver<O> for Dogleg<F>
where
O: ArgminOp<Output = F, Float = F>,
O::Param: std::fmt::Debug
+ ArgminMul<F, O::Param>
+ ArgminWeightedDot<O::Param, O::Float, O::Hessian>
+ ArgminNorm<F>
+ ArgminDot<O::Param, O::Float>
+ ArgminSub<O::Param, O::Param>,
O::Hessian: ArgminInv<O::Hessian> + ArgminDot<O::Param, O::Param>,
F: ArgminFloat,
{
const NAME: &'static str = "Dogleg";

fn next_iter(
&mut self,
op: &mut OpWrapper<O>,
state: &IterState<O>,
) -> Result<ArgminIterData<O>, Error> {
let param = state.get_param();
let g = state
let h = state
.get_hessian()
.unwrap_or_else(|| op.hessian(&param).unwrap());
let pstar;

// pb = -H^-1g
let pb = (h.inv()?).dot(&g).mul(&F::from_f64(-1.0).unwrap());

pstar = pb;
} else {
// pu = - (g^Tg)/(g^THg) * g
let pu = g.mul(&(-g.dot(&g) / g.weighted_dot(&h, &g)));
// println!("pb: {:?}, pu: {:?}", pb, pu);

let utu = pu.dot(&pu);
let btb = pb.dot(&pb);
let utb = pu.dot(&pb);

// compute tau
let t1 = F::from_f64(3.0).unwrap() * utb - btb - F::from_f64(2.0).unwrap() * utu;
let t2 = (utb.powi(2) - F::from_f64(2.0).unwrap() * utb * delta + delta * btb
- btb * utu
+ delta * utu)
.sqrt();
let t3 = F::from_f64(-2.0).unwrap() * utb + btb + utu;
let tau1: F = -(t1 + t2) / t3;
let tau2: F = -(t1 - t2) / t3;

// pick maximum value of both -- not sure if this is the proper way
let mut tau = tau1.max(tau2);

// if calculation failed because t3 is too small, use the third option
if tau.is_nan() || tau.is_infinite() {
tau = (delta + btb - F::from_f64(2.0).unwrap() * utu) / (btb - utu);
}

if tau >= F::from_f64(0.0).unwrap() && tau < F::from_f64(1.0).unwrap() {
pstar = pu.mul(&tau);
} else if tau >= F::from_f64(1.0).unwrap() && tau <= F::from_f64(2.0).unwrap() {
} else {
return Err(ArgminError::ImpossibleError {
text: "tau is bigger than 2, this is not supposed to happen.".to_string(),
}
.into());
}
}
let out = ArgminIterData::new().param(pstar);
Ok(out)
}

fn terminate(&mut self, state: &IterState<O>) -> TerminationReason {
if state.get_iter() >= 1 {
TerminationReason::MaxItersReached
} else {
TerminationReason::NotTerminated
}
}
}

impl<F: ArgminFloat> ArgminTrustRegion<F> for Dogleg<F> {