pub struct CgOptions {
pub max_iter: usize,
pub rtol: f64,
pub atol: f64,
pub hermitian_tol: f64,
}Expand description
Convergence and iteration controls for cg.
Unlike super::GmresOptions, CG needs no restart dimension: it keeps a
fixed number of vectors. Start with the defaults for double precision;
use looser tolerances (for example rtol = hermitian_tol = 1e-5) for
single precision.
§Examples
use tensor4all_core::{krylov::{cg, CgOptions}, DynIndex, IdxTensor};
let b = IdxTensor::from_dense(vec![DynIndex::new_dyn(2)], vec![1.0_f64, 2.0])?;
let options = CgOptions { max_iter: 2, ..CgOptions::default() };
let x0 = IdxTensor::zeros::<f64>(b.external_indices())?;
let result = cg(|x: &IdxTensor| Ok(x.clone()), &b, &x0, &options)?;
assert_eq!(result.solution.to_vec::<f64>()?, vec![1.0, 2.0]);Fields§
§max_iter: usizeMaximum completed CG steps; zero only evaluates the initial residual.
Default: 100. Increase this when the true residual has not converged.
rtol: f64Nonnegative, finite residual tolerance relative to ||b||.
Default: 1e-10. The stopping threshold is max(atol, rtol * ||b||).
atol: f64Nonnegative, finite absolute residual tolerance; default: 0.0.
Set a positive value when a very small RHS needs an absolute error
target. With both tolerances zero, only an exact residual passes.
hermitian_tol: f64Nonnegative, finite allowance for the imaginary part of the directional
Rayleigh quotient q = p^H A p / p^H p:
|Im(q)| <= hermitian_tol * max(1, |q|). Default: 1e-10.
The absolute floor admits cancellation roundoff, matching the projected
Hermitian solvers. This checks visited directions, not the full operator,
and does not relax the positive-definiteness requirement.
Implementations§
Source§impl CgOptions
impl CgOptions
Sourcepub fn validate(&self) -> Result<(), KrylovError>
pub fn validate(&self) -> Result<(), KrylovError>
Validate finite, nonnegative static tolerances without applying an operator.
A zero iteration budget and zero tolerances are valid. The RHS-dependent stopping threshold is checked when solving.
§Errors
Returns KrylovError::InvalidOptions when a tolerance is negative or
non-finite.
§Examples
use tensor4all_core::krylov::{CgOptions, KrylovError};
let options = CgOptions { rtol: f64::NAN, ..Default::default() };
assert!(matches!(options.validate(), Err(KrylovError::InvalidOptions { .. })));
assert!(CgOptions::default().validate().is_ok());Trait Implementations§
Auto Trait Implementations§
impl Freeze for CgOptions
impl RefUnwindSafe for CgOptions
impl Send for CgOptions
impl Sync for CgOptions
impl Unpin for CgOptions
impl UnsafeUnpin for CgOptions
impl UnwindSafe for CgOptions
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
U: Sized,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
U: Sized,
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more