Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmech committed Apr 15, 2024
1 parent c5c2cf4 commit 3439ac7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions russell_lab/src/algo/min_solver_brent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ mod tests {
approx_eq(xo, bracket.xo, test.tol_min);
approx_eq((test.f)(xo, args).unwrap(), bracket.fxo, 1e-15);
}
if let Some(bracket) = test.min3 {
let (a, b) = (bracket.a, bracket.b);
let (xo, stats) = min_solver_brent(a, b, None, args, test.f).unwrap();
println!("\nxo = {:?}", xo);
println!("\n{}", stats);
approx_eq(xo, bracket.xo, test.tol_min);
approx_eq((test.f)(xo, args).unwrap(), bracket.fxo, 1e-15);
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions russell_lab/src/algo/root_solver_brent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,21 @@ mod tests {
println!("\nxo = {:?}", xo);
println!("\n{}", stats);
approx_eq(xo, bracket.xo, 1e-11);
approx_eq((test.f)(xo, args).unwrap(), 0.0, 1e-10);
approx_eq((test.f)(xo, args).unwrap(), 0.0, test.tol_root);
}
if let Some(bracket) = test.root2 {
let (xo, stats) = root_solver_brent(bracket.a, bracket.b, None, args, test.f).unwrap();
println!("\nxo = {:?}", xo);
println!("\n{}", stats);
approx_eq(xo, bracket.xo, 1e-11);
approx_eq((test.f)(xo, args).unwrap(), 0.0, 1e-10);
approx_eq((test.f)(xo, args).unwrap(), 0.0, test.tol_root);
}
if let Some(bracket) = test.root3 {
let (xo, stats) = root_solver_brent(bracket.a, bracket.b, None, args, test.f).unwrap();
println!("\nxo = {:?}", xo);
println!("\n{}", stats);
approx_eq(xo, bracket.xo, 1e-17);
approx_eq((test.f)(xo, args).unwrap(), 0.0, 1e-15);
approx_eq(xo, bracket.xo, 1e-13);
approx_eq((test.f)(xo, args).unwrap(), 0.0, test.tol_root);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion russell_lab/src/algo/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub(super) struct TestFunction {

/// Tolerance for checking the minimum (using Brent's method)
pub tol_min: f64,

/// Tolerance for checking the root (using Brent's method)
pub tol_root: f64,
}

/// Allocates f(x) test functions
Expand Down Expand Up @@ -68,6 +71,7 @@ pub(super) fn get_functions() -> Vec<TestFunction> {
}),
root3: None,
tol_min: 1e-10,
tol_root: 1e-10,
},
TestFunction {
name: "1/2 - 1/(1 + 16 x²)", // (shifted) Runge equation
Expand Down Expand Up @@ -100,6 +104,7 @@ pub(super) fn get_functions() -> Vec<TestFunction> {
}),
root3: None,
tol_min: 1e-8,
tol_root: 1e-13,
},
TestFunction {
name: "x⁵ + 3x⁴ - 2x³ + x - 1",
Expand Down Expand Up @@ -139,6 +144,7 @@ pub(super) fn get_functions() -> Vec<TestFunction> {
fxo: 0.0,
}),
tol_min: 1e-8,
tol_root: 1e-11,
},
TestFunction {
name: "(x - 1)² + 5 sin(x)",
Expand Down Expand Up @@ -178,6 +184,7 @@ pub(super) fn get_functions() -> Vec<TestFunction> {
}),
root3: None,
tol_min: 1e-8,
tol_root: 1e-12,
},
TestFunction {
name: "1/(1 - exp(-2 x) sin²(5 π x)) - 3/2",
Expand Down Expand Up @@ -230,7 +237,8 @@ pub(super) fn get_functions() -> Vec<TestFunction> {
xo: 0.339787495774806018201668030092,
fxo: 0.0,
}),
tol_min: 1e-15,
tol_min: 1e-9,
tol_root: 1e-12,
},
]
}
Expand Down

0 comments on commit 3439ac7

Please sign in to comment.