polynomials_on_simplices.calculus.error_measures module

Functionality for computing the error between exact and approximate values.

absolute_error(x0, x)[source]

Compute absolute error between a value x and its expected value x0.

Parameters:
  • x0 – Expected value.
  • x – Actual value.
Returns:

Absolute error between the actual and expected value.

Return type:

float

relative_error(x0, x, zero_tol=1e-05)[source]

Compute relative error between a value x and its expected value x0.

Parameters:
  • x0 – Expected value.
  • x – Actual value.
  • zero_tol – If x0 is smaller than this value, the absolute error is returned instead.
Returns:

Relative error between the actual and expected value.

Return type:

float

Examples

>>> abs(relative_error(0.1, 0.4) - 3) < 1e-10
True
>>> abs(relative_error(0.4, 0.1) - 0.75) < 1e-10
True

For small values the absolute error is used >>> abs(relative_error(1e-6, 1e-6 + 1e-12) - 1e-12) < 1e-20 True

relative_error_symmetric(x1, x2, zero_tol=1e-05)[source]

Compute relative error between two values x1 and x2.

Note

The relative_error() function is not symmetric, i.e. in general relative_error(a, b) != relative_error(b, a), which makes sense when comparing to a reference value. However when just checking the error between two values (without one of them being more correct than the other) it makes more sense with a symmetric error, which is what this function returns.

\[\varepsilon = \frac{|x_1 - x_2|}{\max(|x_1|, |x_2|)}.\]
Parameters:
  • x1 – First value.
  • x2 – Second value.
  • zero_tol – If max(abs(x1), abs(x2)) is smaller than this value, the absolute error is returned instead.
Returns:

Relative error between the two values.

Return type:

float

Examples

>>> relative_error_symmetric(0.1, 0.2)
0.5
>>> relative_error_symmetric(0.1, 0.2) == relative_error_symmetric(0.2, 0.1)
True