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)
    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_matmul(self):
        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]),
            'rmatmat':
            lambda x: np.dot(self.A.T.conj(), x),
            '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.º 6
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.º 7
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),
                'rmatmat': 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(matrix([[1], [2], [3]])), [[14], [32]])
            assert_equal(A * matrix([[1], [2], [3]]), [[14], [32]])
            assert_equal(A.dot(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) * [[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]])

            X = np.array([[1, 2], [3, 4]])
            # A_asarray = np.array([[1, 2, 3], [4, 5, 6]])
            assert_equal((2 * A).rmatmat(X), np.dot((2 * self.A).T, X))
            assert_equal((A * 2).rmatmat(X), np.dot((self.A * 2).T, X))
            assert_equal((2j * A).rmatmat(X), np.dot((2j * self.A).T.conj(),
                                                     X))
            assert_equal((A * 2j).rmatmat(X), np.dot((self.A * 2j).T.conj(),
                                                     X))
            assert_equal((A + A).rmatmat(X), np.dot((self.A + self.A).T, X))
            assert_equal((A + 2j * A).rmatmat(X),
                         np.dot((self.A + 2j * self.A).T.conj(), X))
            assert_equal((-A).rmatmat(X), np.dot((-self.A).T, X))
            assert_equal((A - A).rmatmat(X), np.dot((self.A - self.A).T, X))
            assert_equal((2j * A).rmatmat(2j * X),
                         np.dot((2j * self.A).T.conj(), 2j * X))

            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(matrix([[1], [2], [3]])), np.ndarray))
            assert_(isinstance(A * matrix([[1], [2], [3]]), np.ndarray))
            assert_(isinstance(A.dot(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)
            # AtimesB = np.array([[22, 28], [49, 64]])
            AtimesB = self.A.dot(self.B)
            X = np.array([[1, 2], [3, 4]])

            assert_equal((A * B).rmatmat(X), np.dot((AtimesB).T, X))
            assert_equal((2j * A * B).rmatmat(X),
                         np.dot((2j * AtimesB).T.conj(), X))

            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_(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)
            X = np.array([[1, 2], [3, 4]])

            assert_equal(C.rmatmat(X), np.dot((self.C).T, X))
            assert_equal((C**2).rmatmat(X),
                         np.dot((np.dot(self.C, self.C)).T, X))

            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).matmat([[1], [1]]), [[17], [37]])

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