Module russell_lab::check

source ·
Expand description

This module contains functions to compare float numbers and arrays for unit testing

§Examples

§Check floating point numbers (real)

use russell_lab::*;

fn main() {
    approx_eq(0.123456789, 0.12345678, 1e-8);
    approx_eq(0.123456789, 0.1234567, 1e-7);
    approx_eq(0.123456789, 0.123456, 1e-6);
    approx_eq(0.123456789, 0.12345, 1e-5);
    approx_eq(0.123456789, 0.1234, 1e-4);
}

§Check floating point numbers (complex)

use russell_lab::*;

fn main() {
    // check floating point number
    approx_eq(0.0000123, 0.000012, 1e-6);

    // check vector of floating point numbers
    array_approx_eq(&[0.01, 0.012], &[0.012, 0.01], 1e-2);

    // check derivative using central differences
    struct Arguments {}
    let f = |x: f64, _: &mut Arguments| Ok(-x);
    let args = &mut Arguments {};
    let at_x = 8.0;
    let dfdx = -1.01;
    deriv1_approx_eq(dfdx, at_x, args, 1e-2, f);

    // check complex numbers
    complex_approx_eq(Complex64::new(1.0, 8.0), Complex64::new(1.001, 8.0), 1e-2);
}

§Check vectors of floating point numbers (real)

use russell_lab::*;

fn main() {
    let a = [0.123456789, 0.123456789, 0.123456789];
    let b = [0.12345678,  0.1234567,   0.123456];
    array_approx_eq(&a, &b, 1e-6);
}

§Check vectors of floating point numbers (complex)

use russell_lab::*;

fn main() {
    let a = &[
        Complex64::new(0.123456789, 5.01),
        Complex64::new(0.123456789, 5.01),
        Complex64::new(0.123456789, 5.01)];
    let b = &[
        Complex64::new(0.12345678, 5.01),
        Complex64::new(0.1234567, 5.01),
        Complex64::new(0.123456, 5.01)];
    complex_array_approx_eq(a, b, 1e-6);
}

§Check derivatives using finite differences

use russell_lab::*;

struct Arguments {}

fn main() {
    let f = |x: f64, _: &mut Arguments| Ok(-x);
    let args = &mut Arguments {};
    let at_x = 8.0;
    let dfdx = -1.01;
    deriv1_approx_eq(dfdx, at_x, args, 1e-2, f);
}

Functions§

  • Panics if two numbers are not approximately equal to each other
  • Panics if two arrays (vectors) are not approximately equal to each other
  • Asserts that two numbers are NaN at the same time or equal to each other (including ±Inf)
  • Panics if two numbers are not approximately equal to each other
  • Panics if two complex arrays (vectors) are not approximately equal to each other
  • Panics if the first derivative is not approximately equal to a numerical derivative (central differences)
  • Panics if the first derivative is not approximately equal to a numerical derivative (backward differences)
  • Panics if the first derivative is not approximately equal to a numerical derivative (forward differences)
  • Approximates the first derivative using backward difference with 7 points
  • Approximates the first derivative using central differences with 5 points
  • Approximates the first derivative using forward difference with 7 points
  • Panics if the second derivative is not approximately equal to a numerical derivative (central differences)
  • Panics if the second derivative is not approximately equal to a numerical derivative (backward differences)
  • Panics if the second derivative is not approximately equal to a numerical derivative (forward differences)
  • Approximates the second derivative using backward difference with 8 points
  • Approximates the second derivative using central difference with 9 points
  • Approximates the second derivative using forward difference with 8 points