Function root

Source
pub fn root<F, J, S>(
    func: F,
    x0: &ArrayBase<S, Ix1>,
    method: Method,
    jac: Option<J>,
    options: Option<Options>,
) -> OptimizeResult<OptimizeResults<f64>>
where F: Fn(&[f64]) -> Array1<f64>, J: Fn(&[f64]) -> Array2<f64>, S: Data<Elem = f64>,
Expand description

Find a root of a vector function.

This function finds the roots of a vector function, which are the input values where the function evaluates to zero.

§Arguments

  • func - Function for which to find the roots
  • x0 - Initial guess
  • method - Method to use for solving the problem
  • jac - Jacobian of the function (optional)
  • options - Options for the solver

§Returns

  • OptimizeResults containing the root finding results

§Example

use ndarray::{array, Array1, Array2};
use scirs2_optimize::roots::{root, Method};

// Define a function for which we want to find the root
fn f(x: &[f64]) -> Array1<f64> {
    let x0 = x[0];
    let x1 = x[1];
    array![
        x0.powi(2) + x1.powi(2) - 1.0,  // x^2 + y^2 - 1 = 0 (circle equation)
        x0 - x1                         // x = y (line equation)
    ]
}

// Optional Jacobian function that we're not using in this example
fn jac(_x: &[f64]) -> Array2<f64> {
    Array2::zeros((2,2))
}

// Initial guess
let x0 = array![2.0, 2.0];

// Find the root - with explicit type annotation for None
let result = root(f, &x0, Method::Hybr, None::<fn(&[f64]) -> Array2<f64>>, None)?;

// The root should be close to [sqrt(0.5), sqrt(0.5)]
assert!(result.success);