solve

Function solve 

Source
pub fn solve(
    x: f64,
    sigma: f64,
    lower: Option<f64>,
    upper: Option<f64>,
) -> Option<f64>
Expand description

Solve the problem descirbed at the head of this file with Newtown’s method, which requires f(x) and f’(x) to solve f(x) = 0. Here f(x) is truncated_bandwidth and f’(x) us derivative_truncated_bandwidth

Parameters: x : target mathematical expectation of the truncated normal distribution sigma: the standard deviation of the normal distribution before truncation lower: the lower bound of the truncation, default 0 if None is provided upper: the upper bound of the truncation, default +inf if None is provided

Return value: if a solution is found for the problem, returns the cernter of the normal distribution before truncation else (aka the sanity check of the parameters failed), returns None.

The units of the parameters above should be consistent, which is the unit of the return value.

§Examples

use netem_trace::model::solve_truncate::solve;
let a = solve(8.0, 2.0, Some(4.0), Some(12.0)).unwrap();
assert!((a-8.0).abs() < 0.000001);

let a = solve(10.0, 4.0, Some(4.0), Some(12.0)).unwrap();
assert_eq!(a, 11.145871035156846);

let a = solve(10.0, 20.0, None, None).unwrap();
assert_eq!(a, 3.7609851997619734);

let a = solve(5.0, 18.0, None, None).unwrap();
assert_eq!(a, -4.888296757781897);

let a = solve(10.0, 20.0, Some(7.0), Some(15.0)).unwrap();
assert_eq!(a, 4.584705225916618);

let a = solve(10.0, 0.01, Some(7.0), Some(15.0)).unwrap();
assert_eq!(a, 10.0);

let a = solve(10.0, 0.01, None, Some(15.0)).unwrap();
assert_eq!(a, 10.0);

let a = solve(10.0, 0.01, None, None).unwrap();
assert_eq!(a, 10.0);

let a = solve(10.0, 0.01, Some(3.0), None).unwrap();
assert_eq!(a, 10.0);