Ejemplo n.º 1
0
 def _adjoint(self):
     shape = self.shape[1], self.shape[0]
     matvec = partial(rmv, dtype=self.dtype)
     rmatvec = partial(mv, dtype=self.dtype)
     return interface.LinearOperator(matvec=matvec,
                                     rmatvec=rmatvec,
                                     dtype=self.dtype,
                                     shape=shape)
Ejemplo n.º 2
0
def test_pickle():
    import pickle

    for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
        A = interface.LinearOperator((3, 3), matvec)
        s = pickle.dumps(A, protocol=protocol)
        B = pickle.loads(s)

        for k in A.__dict__:
            assert_equal(getattr(A, k), getattr(B, k))
Ejemplo n.º 3
0
def test_no_double_init():
    call_count = [0]

    def matvec(v):
        call_count[0] += 1
        return v

    # It should call matvec exactly once (in order to determine the
    # operator dtype)
    A = interface.LinearOperator((2, 2), matvec=matvec)
    assert_equal(call_count[0], 1)
Ejemplo n.º 4
0
def test_attributes():
    A = interface.aslinearoperator(np.arange(16).reshape(4, 4))

    def always_four_ones(x):
        x = np.asarray(x)
        assert_(x.shape == (3, ) or x.shape == (3, 1))
        return np.ones(4)

    B = interface.LinearOperator(shape=(4, 3), matvec=always_four_ones)

    for op in [A, B, A * B, A.H, A + A, B + B, A**4]:
        assert_(hasattr(op, "dtype"))
        assert_(hasattr(op, "shape"))
        assert_(hasattr(op, "_matvec"))
Ejemplo n.º 5
0
    def test_transpose_basic(self):
        M = np.array([[1, 2, 3], [4, 5, 6]])

        A = interface.LinearOperator(
            shape=M.shape,
            matvec=lambda x: np.dot(M, x).reshape(M.shape[0]),
            rmatvec=lambda x: np.dot(M.T, x).reshape(M.shape[1]),
            matmat=lambda x: np.dot(M, x),
            rmatmat=lambda x: np.dot(M.T, x))

        assert_equal(A.T * [1, 1], [5, 7, 9])  # multiply by vector
        assert_equal(
            A.T * np.array([[1, 2], [3, 4]]),  # multiply by matrix
            np.array([[13, 18], [17, 24], [21, 30]]))
Ejemplo n.º 6
0
    def test_r_defaulting(self):
        M = np.array([[1, 2, 3], [4, 5, 6]])

        # check rmatmat defaults to rmatvec
        A = interface.LinearOperator(
            shape=M.shape,
            matvec=lambda x: np.dot(M, x).reshape(M.shape[0]),
            rmatvec=lambda x: np.dot(M.T, x).reshape(M.shape[1]))

        assert_equal(A.T * [1, 1], [5, 7, 9])  # multiply by vector
        assert_equal(
            A.T * np.array([[1, 2], [3, 4]]),  # multiply by matrix
            np.array([[13, 18], [17, 24], [21, 30]]))

        # check rmatvec defaults to rmatmat
        A = interface.LinearOperator(
            shape=M.shape,
            matvec=lambda x: np.dot(M, x).reshape(M.shape[0]),
            rmatvec=lambda x: np.dot(M.T, x).reshape(M.shape[1]))

        assert_equal(A.T * [1, 1], [5, 7, 9])  # multiply by vector
        assert_equal(
            A.T * np.array([[1, 2], [3, 4]]),  # multiply by matrix
            np.array([[13, 18], [17, 24], [21, 30]]))
Ejemplo n.º 7
0
    def test_matmul(self):
        if not TEST_MATMUL:
            pytest.skip("matmul is only tested in Python 3.5+")

        D = {
            'shape':
            self.A.shape,
            'matvec':
            lambda x: np.dot(self.A, x).reshape(self.A.shape[0]),
            'rmatvec':
            lambda x: np.dot(self.A.T.conj(), x).reshape(self.A.shape[1]),
            'matmat':
            lambda x: np.dot(self.A, x)
        }
        A = interface.LinearOperator(**D)
        B = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        b = B[0]

        assert_equal(operator.matmul(A, b), A * b)
        assert_equal(operator.matmul(A, B), A * B)
        assert_raises(ValueError, operator.matmul, A, 2)
        assert_raises(ValueError, operator.matmul, 2, A)
Ejemplo n.º 8
0
def test_repr():
    A = interface.LinearOperator(shape=(1, 1), matvec=lambda x: 1)
    repr_A = repr(A)
    assert_('unspecified dtype' not in repr_A, repr_A)
Ejemplo n.º 9
0
    def test_matvec(self):
        def get_matvecs(A):
            return [{
                'shape':
                A.shape,
                'matvec':
                lambda x: np.dot(A, x).reshape(A.shape[0]),
                'rmatvec':
                lambda x: np.dot(A.T.conj(), x).reshape(A.shape[1])
            }, {
                'shape': A.shape,
                'matvec': lambda x: np.dot(A, x),
                'rmatvec': lambda x: np.dot(A.T.conj(), x),
                'matmat': lambda x: np.dot(A, x)
            }]

        for matvecs in get_matvecs(self.A):
            A = interface.LinearOperator(**matvecs)

            assert_(A.args == ())

            assert_equal(A.matvec(np.array([1, 2, 3])), [14, 32])
            assert_equal(A.matvec(np.array([[1], [2], [3]])), [[14], [32]])
            assert_equal(A * np.array([1, 2, 3]), [14, 32])
            assert_equal(A * np.array([[1], [2], [3]]), [[14], [32]])
            assert_equal(A.dot(np.array([1, 2, 3])), [14, 32])
            assert_equal(A.dot(np.array([[1], [2], [3]])), [[14], [32]])

            assert_equal(A.matvec(np.matrix([[1], [2], [3]])), [[14], [32]])
            assert_equal(A * np.matrix([[1], [2], [3]]), [[14], [32]])
            assert_equal(A.dot(np.matrix([[1], [2], [3]])), [[14], [32]])

            assert_equal((2 * A) * [1, 1, 1], [12, 30])
            assert_equal((2 * A).rmatvec([1, 1]), [10, 14, 18])
            assert_equal((2 * A).H.matvec([1, 1]), [10, 14, 18])
            assert_equal((2 * A).T.matvec([1, 1]), [10, 14, 18])
            assert_equal((2 * A) * [[1], [1], [1]], [[12], [30]])
            assert_equal((2 * A).matmat([[1], [1], [1]]), [[12], [30]])
            assert_equal((A * 2) * [1, 1, 1], [12, 30])
            assert_equal((A * 2) * [[1], [1], [1]], [[12], [30]])
            assert_equal((2j * A) * [1, 1, 1], [12j, 30j])
            assert_equal((A + A) * [1, 1, 1], [12, 30])
            assert_equal((A + A).rmatvec([1, 1]), [10, 14, 18])
            assert_equal((A + A).H.matvec([1, 1]), [10, 14, 18])
            assert_equal((A + A) * [[1], [1], [1]], [[12], [30]])
            assert_equal((A + A).matmat([[1], [1], [1]]), [[12], [30]])
            assert_equal((-A) * [1, 1, 1], [-6, -15])
            assert_equal((-A) * [[1], [1], [1]], [[-6], [-15]])
            assert_equal((A - A) * [1, 1, 1], [0, 0])
            assert_equal((A - A) * [[1], [1], [1]], [[0], [0]])

            z = A + A
            assert_(len(z.args) == 2 and z.args[0] is A and z.args[1] is A)
            z = 2 * A
            assert_(len(z.args) == 2 and z.args[0] is A and z.args[1] == 2)

            assert_(isinstance(A.matvec([1, 2, 3]), np.ndarray))
            assert_(isinstance(A.matvec(np.array([[1], [2], [3]])),
                               np.ndarray))
            assert_(isinstance(A * np.array([1, 2, 3]), np.ndarray))
            assert_(isinstance(A * np.array([[1], [2], [3]]), np.ndarray))
            assert_(isinstance(A.dot(np.array([1, 2, 3])), np.ndarray))
            assert_(isinstance(A.dot(np.array([[1], [2], [3]])), np.ndarray))

            assert_(
                isinstance(A.matvec(np.matrix([[1], [2], [3]])), np.ndarray))
            assert_(isinstance(A * np.matrix([[1], [2], [3]]), np.ndarray))
            assert_(isinstance(A.dot(np.matrix([[1], [2], [3]])), np.ndarray))

            assert_(isinstance(2 * A, interface._ScaledLinearOperator))
            assert_(isinstance(2j * A, interface._ScaledLinearOperator))
            assert_(isinstance(A + A, interface._SumLinearOperator))
            assert_(isinstance(-A, interface._ScaledLinearOperator))
            assert_(isinstance(A - A, interface._SumLinearOperator))

            assert_((2j * A).dtype == np.complex_)

            assert_raises(ValueError, A.matvec, np.array([1, 2]))
            assert_raises(ValueError, A.matvec, np.array([1, 2, 3, 4]))
            assert_raises(ValueError, A.matvec, np.array([[1], [2]]))
            assert_raises(ValueError, A.matvec, np.array([[1], [2], [3], [4]]))

            assert_raises(ValueError, lambda: A * A)
            assert_raises(ValueError, lambda: A**2)

        for matvecsA, matvecsB in product(get_matvecs(self.A),
                                          get_matvecs(self.B)):
            A = interface.LinearOperator(**matvecsA)
            B = interface.LinearOperator(**matvecsB)

            assert_equal((A * B) * [1, 1], [50, 113])
            assert_equal((A * B) * [[1], [1]], [[50], [113]])
            assert_equal((A * B).matmat([[1], [1]]), [[50], [113]])

            assert_equal((A * B).rmatvec([1, 1]), [71, 92])
            assert_equal((A * B).H.matvec([1, 1]), [71, 92])
            assert_equal((A * B).T.matvec([1, 1]), [71, 92])

            assert_(isinstance(A * B, interface._ProductLinearOperator))

            assert_raises(ValueError, lambda: A + B)
            assert_raises(ValueError, lambda: A**2)

            z = A * B
            assert_(len(z.args) == 2 and z.args[0] is A and z.args[1] is B)

        for matvecsC in get_matvecs(self.C):
            C = interface.LinearOperator(**matvecsC)

            assert_equal((C**2) * [1, 1], [17, 37])
            assert_equal((C**2).rmatvec([1, 1]), [22, 32])
            assert_equal((C**2).H.matvec([1, 1]), [22, 32])
            assert_equal((C**2).T.matvec([1, 1]), [22, 32])
            assert_equal((C**2).matmat([[1], [1]]), [[17], [37]])

            assert_(isinstance(C**2, interface._PowerLinearOperator))