[−][src]Struct mathru::optimization::ConjugateGradient
Conjugate Gradient method
The conjugate gradient method is a solver for systems of linear equations with a symmetric and positive-definite matrix. Ax = b where A is a symmetric and positive-definite matrix
input: $A \in \mathbb{R}^{n \times n}
$ and $b \in \mathbb{R}^{n}
$ and
initial approximation $x_{0} \in \mathbb{R}^{n}
$
output: $x_k
$
- $
d_{0} = r_{0} := b - Ax_{0}
$ and set $k := 0
$ - $
\alpha_{k} := \frac{\lvert \lvert r_{k} \rvert \rvert_{2}^{2}}{d_{k}^{T}Ad_{k}}
$
$x_{k+1} := x_{k} + \alpha_{j}d_{k}
$
$r_{k+1} := r_{k} - \alpha_{k}Ad_{k}
$
$\beta_{k} := \frac{\lvert \lvert r_{k+1} \rvert \rvert_{2}^{2}}{\lvert \lvert r_{k} \rvert \rvert_{2}^{2}}
$
$d_{k+1} := r_{k+1} + \beta_{k}d_{k}
$ - if $
\lvert \lvert r_{k+ 1} \rvert \rvert_{2} > \epsilon
$ increase $k:= k + 1
$ and goto 2.
Example
use mathru::*; use mathru::algebra::linear::{Vector, Matrix}; use mathru::optimization::{Optim, ConjugateGradient}; struct LinearEquation { a: Matrix<f64>, b: Vector<f64>, } //Ax = b impl LinearEquation { pub fn new() -> LinearEquation { LinearEquation { a: matrix![1.0, 3.0; 3.0, 5.0], b: vector![-7.0; 7.0] } } } impl Optim<f64> for LinearEquation { // A fn jacobian(&self, _input: &Vector<f64>) -> Matrix<f64> { return self.a.clone(); } // f = b-Ax fn eval(&self, x: &Vector<f64>) -> Vector<f64> { return self.b.clone() - self.a.clone() * x.clone() } //Computes the Hessian at the given value x fn hessian(&self, _x: &Vector<f64>) -> Matrix<f64> { unimplemented!(); } } //create optimizer instance let optim: ConjugateGradient<f64> = ConjugateGradient::new(10, 0.01); let leq: LinearEquation = LinearEquation::new(); // Initial approximation let x_0: Vector<f64> = vector![1.0; 1.0]; // Minimize function let x_min: Vector<f64> = optim.minimize(&leq, &x_0).arg();
Implementations
impl<T> ConjugateGradient<T>
[src]
pub fn new(iters: u64, epsilon: T) -> ConjugateGradient<T>
[src]
impl<T> ConjugateGradient<T> where
T: Real,
[src]
T: Real,
pub fn minimize<F>(&self, func: &F, x_0: &Vector<T>) -> OptimResult<Vector<T>> where
F: Optim<T>,
[src]
F: Optim<T>,
Auto Trait Implementations
impl<T> RefUnwindSafe for ConjugateGradient<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for ConjugateGradient<T> where
T: Send,
T: Send,
impl<T> Sync for ConjugateGradient<T> where
T: Sync,
T: Sync,
impl<T> Unpin for ConjugateGradient<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for ConjugateGradient<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,