relentless.optimize.Tolerance#

class relentless.optimize.Tolerance(absolute, relative)#

Tolerance for convergence tests.

A tolerance can be used to check if one value is close to another in either an absolute or a relative sense. The test for closeness is based on the NumPy method numpy.isclose(), which can use both an absolute tolerance \(\varepsilon_{\rm a}\) and a relative tolerance \(\varepsilon_{\rm r}\).

A value \(a\) is close to a value \(b\) if and only if:

\[\lvert a-b\rvert\le\varepsilon_{\rm a}+\varepsilon_{\rm r}\lvert b\rvert\]

An absolute tolerance can be any non-negative numerical value. A relative tolerance must be a non-negative numerical value between 0 and 1. By setting \(\varepsilon_{\rm r}=0\), only an absolute tolerance test is performed. Similarly, setting \(\varepsilon_{\rm a}=0\) will result in the performance of only a relative tolerance test.

Parameters:
  • absolute (float) – The default absolute tolerance.

  • relative (float) – The default relative tolerance.

Methods

isclose(a, b[, key])

Check if the two values are equivalent within a tolerance.

Attributes

absolute

The absolute tolerance(s).

relative

The relative tolerance(s).

property absolute#

The absolute tolerance(s). Must be non-negative.

Type:

DefaultDict

isclose(a, b, key=None)#

Check if the two values are equivalent within a tolerance.

The test is performed using numpy.isclose().

The default absolute and relative tolerances can be overridden based on a key, which can be any valid dictionary key other than None. When testing for closeness, if a key is given, the keyed tolerance will be used if has been specified; otherwise, the default tolerance is used.

Parameters:
  • a (float) – The first value to compare.

  • b (float) – The second value to compare.

  • key (object) – The key to use for determining the tolerance (defaults to None).

Returns:

True if values are close.

Return type:

bool

Raises:
  • ValueError – If the absolute tolerance is not non-negative.

  • ValueError – If the relative tolerance is not between 0 and 1.

property relative#

The relative tolerance(s). Must be between 0 and 1.

Type:

DefaultDict