Пример #1
0
def test_inv(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    if linop.is_square:
        expected_exception = None

        try:
            matrix_inv = np.linalg.inv(matrix)
        except Exception as e:  # pylint: disable=broad-except
            expected_exception = e

        if expected_exception is None:
            linop_inv = linop.inv()

            assert isinstance(linop_inv, pn.linops.LinearOperator)
            assert linop_inv.shape == matrix_inv.shape
            assert linop_inv.dtype == matrix_inv.dtype

            np.testing.assert_allclose(linop_inv.todense(),
                                       matrix_inv,
                                       atol=1e-12)
        else:
            with pytest.raises(type(expected_exception)):
                linop.inv()
    else:
        with pytest.raises(np.linalg.LinAlgError):
            linop.inv()
Пример #2
0
def test_transpose(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    matrix_transpose = matrix.transpose()

    for linop_transpose in [
            linop.transpose(),
            linop.transpose(1, 0),
            linop.transpose((1, 0)),
            linop.transpose(1, -2),
    ]:
        linop_transpose = linop.transpose()

        assert isinstance(linop_transpose, pn.linops.LinearOperator)
        assert linop_transpose.shape == matrix_transpose.shape
        assert linop_transpose.dtype == matrix_transpose.dtype

        np.testing.assert_allclose(linop_transpose.todense(), matrix_transpose)

    # Transpose with (0, 1) argument
    for ax0 in [0, -2]:
        for ax1 in [1, -1]:
            assert linop.transpose(ax0, ax1) is linop
            assert linop.transpose((ax0, ax1)) is linop

    # Errors
    for axes in [(0, 2), (-3, 1), ((0, 1), 1), (0, 1, 2), (0, 0), (1, 1)]:
        try:
            matrix.transpose(*axes)
        except Exception as e:  # pylint: disable=broad-except
            expected_exception = e

        with pytest.raises(type(expected_exception)):
            linop.transpose(*axes)
Пример #3
0
def test_symmetrize(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    if linop.is_square:
        sym_linop = linop.symmetrize()

        assert sym_linop.is_symmetric

        np.testing.assert_array_equal(sym_linop.todense().T,
                                      sym_linop.todense())
    else:
        with pytest.raises(np.linalg.LinAlgError):
            linop.symmetrize()
Пример #4
0
def test_trace(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    if linop.is_square:
        linop_trace = linop.trace()
        matrix_trace = np.trace(matrix)

        assert linop_trace.shape == ()
        assert linop_trace.dtype == matrix_trace.dtype

        np.testing.assert_allclose(linop_trace, matrix_trace)
    else:
        with pytest.raises(np.linalg.LinAlgError):
            linop.trace()
Пример #5
0
def test_logabsdet(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    if linop.is_square:
        linop_logabsdet = linop.logabsdet()
        _, matrix_logabsdet = np.linalg.slogdet(matrix)

        assert linop_logabsdet.shape == ()
        assert linop_logabsdet.dtype == matrix_logabsdet.dtype

        np.testing.assert_allclose(linop_logabsdet, matrix_logabsdet)
    else:
        with pytest.raises(np.linalg.LinAlgError):
            linop.logabsdet()
Пример #6
0
def test_det(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    if linop.is_square:
        linop_det = linop.det()
        matrix_det = np.linalg.det(matrix)

        assert isinstance(linop_det, np.inexact)
        assert linop_det.shape == ()
        assert linop_det.dtype == matrix_det.dtype

        np.testing.assert_allclose(linop_det, matrix_det)
    else:
        with pytest.raises(np.linalg.LinAlgError):
            linop.det()
Пример #7
0
def test_eigvals(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    if linop.is_square:
        linop_eigvals = linop.eigvals()
        matrix_eigvals = np.linalg.eigvals(matrix)

        assert isinstance(linop_eigvals, np.ndarray)
        assert linop_eigvals.shape == matrix_eigvals.shape
        assert linop_eigvals.dtype == matrix_eigvals.dtype

        np.testing.assert_allclose(linop_eigvals, matrix_eigvals)
    else:
        with pytest.raises(np.linalg.LinAlgError):
            linop.eigvals()
Пример #8
0
def test_todense(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    linop_dense = linop.todense()

    assert isinstance(linop_dense, np.ndarray)
    assert linop_dense.shape == matrix.shape
    assert linop_dense.dtype == matrix.dtype

    np.testing.assert_allclose(linop_dense, matrix)
Пример #9
0
def test_conjugate(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    linop_conj = linop.conj()
    matrix_conj = matrix.conj()

    assert isinstance(linop_conj, pn.linops.LinearOperator)
    assert linop_conj.shape == matrix_conj.shape
    assert linop_conj.dtype == matrix_conj.dtype

    np.testing.assert_allclose(linop_conj.todense(), matrix_conj)
Пример #10
0
def test_rank(linop: pn.linops.LinearOperator, matrix: np.ndarray):
    linop_rank = linop.rank()
    matrix_rank = np.linalg.matrix_rank(matrix)

    assert isinstance(linop_rank, np.intp)
    assert linop_rank.shape == ()
    assert linop_rank.dtype == matrix_rank.dtype

    assert linop_rank == matrix_rank
Пример #11
0
def test_cond(linop: pn.linops.LinearOperator, matrix: np.ndarray,
              p: Union[None, int, float, str]):
    if linop.is_square:
        linop_cond = linop.cond(p=p)
        matrix_cond = np.linalg.cond(matrix, p=p)

        assert linop_cond.shape == ()
        assert linop_cond.dtype == matrix_cond.dtype

        try:
            np.testing.assert_allclose(linop_cond, matrix_cond)
        except AssertionError as e:
            if p == -2 and 0 < linop.rank(
            ) < linop.shape[0] and linop_cond == np.inf:
                # `np.linalg.cond` returns 0.0 for p = -2 if the matrix is singular but
                # not zero. This is a bug.
                pass
            else:
                raise e
    else:
        with pytest.raises(np.linalg.LinAlgError):
            linop.cond(p=p)
Пример #12
0
def test_positive_definite_linop_must_be_symmetric(
        linop: pn.linops.LinearOperator, matrix: np.ndarray):
    if not linop.is_symmetric:
        with pytest.raises(ValueError):
            linop.is_positive_definite = True
Пример #13
0
def test_symmetric_linop_must_be_square(linop: pn.linops.LinearOperator,
                                        matrix: np.ndarray):
    if not linop.is_square:
        with pytest.raises(ValueError):
            linop.is_symmetric = True