utils — Testing and analysis utilities

Testing

allclose(a: Array, b: Array, rtol: float = 1e-5, atol: float = 1e-8) bool[source]

Array-namespace agnostic equivalent of np.allclose.

Works for scalars (0-d arrays) and multi-dimensional arrays alike.

Parameters:
  • a (Array) – Arrays to compare.

  • b (Array) – Arrays to compare.

  • rtol (float, optional) – Relative tolerance.

  • atol (float, optional) – Absolute tolerance.

Returns:

True if all elements satisfy |a - b| <= atol + rtol * |b|.

Return type:

bool

dottest(linop: LinOp, num: int = 1, rtol: float = 1e-5, atol: float = 1e-8, echo: bool = False, xp=np) bool[source]

The dot test.

Verify the validity of forward and adjoint methods with equality

(Aᴴ·u)ᴴ·v = uᴴ·(A·v).

where u and v are random vectors, to detect errors in implementation.

Parameters:
  • linop (LinOp) – The linear operator to test.

  • num (int, optional) – The number of tests. They must all pass.

  • rtol (float, optional) – The relative tolerance parameter (see allclose).

  • atol (float, optional) – The absolute tolerance parameter (see allclose).

  • echo (bool, optional) – If True, print the two sides of the equality for each test.

  • xp (array namespace, optional) – The array namespace for generating test vectors (default: numpy).

Returns:

True if all num tests pass.

Return type:

bool

Notes

Numpy is used for random number generation; arrays are converted to xp if xp is not numpy.

fwadjtest(linop: LinOp, num: int = 1, rtol: float = 1e-5, atol: float = 1e-8, echo: bool = False, xp=np) bool[source]

Test fwadj validity.

Verify the validity of fwadj wrt. forward and adjoint methods with equality

(Aᴴ·A)·v = Aᴴ·(A·v).

where v is a random vector, to detect errors in implementation.

Parameters:
  • linop (LinOp) – The linear operator to test.

  • num (int, optional) – The number of tests. They must all pass.

  • rtol (float, optional) – The relative tolerance parameter (see allclose).

  • atol (float, optional) – The absolute tolerance parameter (see allclose).

  • echo (bool, optional) – If True, print the two sides of the equality for each test.

  • xp (array namespace, optional) – The array namespace for generating test vectors (default: numpy).

Returns:

True if all num tests pass.

Return type:

bool

Matrix properties

is_sym(linop: Array | LinOp) bool[source]

Return True if linop is symmetric.

Parameters:

linop (Array or LinOp) – The operator or matrix to test.

Return type:

bool

is_pos_def(linop: Array | LinOp) bool[source]

Return True if linop is positive definite.

linopArray or LinOp

The operator or matrix to test.

bool

A positive definite matrix $M$ has strictly positive eigenvalues, but the converse is not true for non-symmetric matrices. The function therefore tests all eigenvalues of $M + M^T$: since $x^T M x = x^T

rac{M + M^T}{2} x$

for any vector $x$, positive definiteness of $M$ is equivalent to positive definiteness of $M + M^T$.

is_semi_pos_def(linop: Array | LinOp) bool[source]

Return True if linop is semi positive definite.

Parameters:

linop (Array or LinOp) – The operator or matrix to test.

Return type:

bool

See also

is_pos_def

is_neg_def(linop: Array | LinOp) bool[source]

Return True if linop is negative definite.

Parameters:

linop (Array or LinOp) – The operator or matrix to test.

Return type:

bool

See also

is_pos_def

is_semi_neg_def(linop: Array | LinOp) bool[source]

Return True if linop is semi negative definite.

Parameters:

linop (Array or LinOp) – The operator or matrix to test.

Return type:

bool

See also

is_pos_def

Condition number

cond(linop: Array | LinOp) float[source]

Return the condition number κ.

The condition number κ is defined as:

κ = max(|λ|) / min(|λ|)

where λ are eigenvalues of linop.

Parameters:

linop (Array or LinOp) – An implicit linear operator or a matrix.

Returns:

The condition number.

Return type:

float

fcond(linop: LinOp, tol: float = 0.1) float[source]

Estimate the condition number κ.

The condition number κ is defined as:

κ = max(|λ|) / min(|λ|)

where the two extreme eigenvalues λ of linop are estimated with the Lanczos algorithm via scipy.sparse.linalg.eigsh.

Parameters:
  • linop (LinOp) – An implicit linear operator.

  • tol (float, optional) – Tolerance for scipy.sparse.linalg.eigsh.

Returns:

Estimated condition number.

Return type:

float