Skip to content

Commit

Permalink
Add doc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmech committed Jun 25, 2024
1 parent 48b22c2 commit 4ee1499
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions russell_lab/src/algo/multi_root_solver_cheby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ impl MultiRootSolverCheby {
/// # Output
///
/// Returns a sorted list (from xa to xb) with the roots.
///
/// # Example
///
/// ```
/// use russell_lab::*;
///
/// fn main() -> Result<(), StrError> {
/// // function
/// let f = |x, _: &mut NoArgs| Ok(x * x - 1.0);
/// let (xa, xb) = (-2.0, 2.0);
/// let args = &mut 0;
///
/// // interpolant
/// let nn = 2;
/// let interp = InterpChebyshev::new_with_f(nn, xa, xb, args, f)?;
///
/// // find all roots in the interval
/// let mut solver = MultiRootSolverCheby::new(nn)?;
/// let roots = Vector::from(&solver.find(&interp)?);
/// vec_approx_eq(&roots, &[-1.0, 1.0], 1e-15);
/// Ok(())
/// }
/// ```
pub fn find(&mut self, interp: &InterpChebyshev) -> Result<&[f64], StrError> {
// check
let nn = interp.get_degree();
Expand Down Expand Up @@ -181,6 +204,34 @@ impl MultiRootSolverCheby {
}

/// Polishes the roots using Newton's method
///
/// # Examples
///
/// ```
/// use russell_lab::*;
///
/// fn main() -> Result<(), StrError> {
/// // function
/// let f = |x, _: &mut NoArgs| Ok(x * x * x * x - 1.0);
/// let (xa, xb) = (-2.0, 2.0);
/// let args = &mut 0;
///
/// // interpolant
/// let nn = 2;
/// let interp = InterpChebyshev::new_with_f(nn, xa, xb, args, f)?;
///
/// // find all roots in the interval
/// let mut solver = MultiRootSolverCheby::new(nn)?;
/// let roots = Vector::from(&solver.find(&interp)?);
/// vec_approx_eq(&roots, &[-0.5, 0.5], 1e-15); // inaccurate
///
/// // polish the roots
/// let mut roots_polished = Vector::new(roots.dim());
/// solver.polish_roots_newton(roots_polished.as_mut_data(), roots.as_data(), xa, xb, args, f)?;
/// vec_approx_eq(&roots_polished, &[-1.0, 1.0], 1e-15); // accurate
/// Ok(())
/// }
///```
pub fn polish_roots_newton<F, A>(
&self,
roots_out: &mut [f64],
Expand Down

0 comments on commit 4ee1499

Please sign in to comment.