ddg.nonexact module

Module for global tolerance defaults and nonexact calculations.

Contains ways to set, get and reset the global tolerances and functions that use the global values as defaults.

ddg.nonexact.get_tol_defaults()[source]

Get current global tolerance defaults.

Returns:
dict {‘atol’: float, ‘rtol’: float}
ddg.nonexact.set_tol_defaults(atol=None, rtol=None)[source]

Set one or both global tolerance defaults.

Parameters:
atol, rtolfloat (default=None)

When None is given, the default will not be changed.

ddg.nonexact.reset_tol_defaults()[source]

Reset global tolerance defaults to their original values.

class ddg.nonexact.TolDefaults(atol=None, rtol=None)[source]

Bases: object

Context manager to temporarily set global tolerances.

All alterations made to the tolerance defaults inside the context will be reset upon exiting it.

Parameters:
atol, rtolfloat (default=None)

Examples

>>> from ddg.nonexact import TolDefaults, get_tol_defaults
>>> get_tol_defaults()["atol"]
1e-07
>>> with TolDefaults(atol=1e-5) as global_tols:
...     get_tol_defaults()["atol"] == global_tols["atol"] == 1e-5
...
True
>>> get_tol_defaults()["atol"]  # Context has been exited
1e-07

global_tols is the same as get_tol_defaults called right after entering the context.

ddg.nonexact.isclose(a, b, rtol=None, atol=None, equal_nan=False)[source]

Wrapper for numpy.isclose that uses global tolerance defaults.

ddg.nonexact.allclose(a, b, rtol=None, atol=None, equal_nan=False)[source]

Wrapper for numpy.allclose that uses global tolerance defaults.

ddg.nonexact.svd_rank(s, rtol=None, atol=None)[source]

Approximates the rank from singular values using global tolerance defaults.

Returns the number of singular values that are greater than or equal to max(atol, rtol * max(s)).

Parameters:
snumpy.ndarray of shape (n,)
rtol, atolfloat
Returns:
int
ddg.nonexact.combine_tols(*objects)[source]

Combine tolerances of objects by taking the maximum.

If none of the objects overrides the tolerance, returns the global default.

Parameters:
*objects

Objects that might have attributes atol, rtol containing float or None.

Returns:
atol, rtolfloat