approx_exp_root_log

Function approx_exp_root_log 

Source
pub fn approx_exp_root_log(
    x: &[f64],
    y: &[f64],
    b_list: &[f64],
    poly_deg: usize,
    log_lambdas: &[f64],
) -> impl Fn(f64) -> f64
Expand description

Fits an ExpRoot+Log approximation to the (x, y) data, and returns a closure representing the approximate function.

Examples found in repository?
examples/demo.rs (lines 9-15)
3fn main() {
4    // Sample data: x values and corresponding y values
5    let x: Vec<f64> = (0..100).map(|i| i as f64 / 100.0).collect();
6    let y: Vec<f64> = x.iter().map(|&x| (2.0 * std::f64::consts::PI * x).sin()).collect();
7
8    // Create the approximation function using ExpRoot+Log
9    let approx_fn = approx_exp_root_log(
10        &x,
11        &y,
12        &[0.5, 2.0, 5.0, 10.0, 20.0],    //  b_i
13        5,                                  //  x^5
14        &[1.0, 5.0, 10.0, 20.0],           // log params
15    );
16
17    // Evaluate the approximation
18    let y_pred: Vec<f64> = x.iter().map(|&xi| approx_fn(xi)).collect();
19    
20    // Print the result
21    println!("Approximated values: {:?}", y_pred);
22}
More examples
Hide additional examples
examples/benchmark.rs (lines 47-53)
38fn bench_one<F>(name: &str, f: F)
39where
40    F: Fn(f64) -> f64,
41{
42    let x: Vec<f64> = (0..2000).map(|i| i as f64 / 2000.0).collect();
43    let y: Vec<f64> = x.iter().map(|&xi| f(xi)).collect();
44
45    // -------- ExpRoot+Log ---------------------------------------------------
46    let t0 = Instant::now();
47    let exp_fn = approx_exp_root_log(
48        &x,
49        &y,
50        &[0.5, 2.0, 5.0, 10.0],
51        5,
52        &[1.0, 5.0, 10.0, 20.0],
53    );
54    let y_pred_exp: Vec<f64> = x.iter().map(|&xi| exp_fn(xi)).collect();
55    let mse_exp = mse(&y, &y_pred_exp);
56    let dt_exp = t0.elapsed();
57
58    // -------- Полином (deg = 10) -------------------------------------------
59    let t1 = Instant::now();
60    let coeffs = poly_ls(&x, &y, 10);
61    let y_pred_poly: Vec<f64> =
62        x.iter().map(|&xi| poly_predict(coeffs.as_slice(), xi)).collect();
63    let mse_poly = mse(&y, &y_pred_poly);
64    let dt_poly = t1.elapsed();
65
66    println!(
67        "{:<8} | ExpRoot MSE = {:8.2e} {:>6?} || Poly MSE = {:8.2e} {:>6?}",
68        name, mse_exp, dt_exp, mse_poly, dt_poly
69    );
70}