def test_dtypes(self): for dtype_op in allowed_types: for dtype_in in allowed_types: dtype_out = np.result_type(dtype_op, dtype_in) matvecs = get_matvecs(self.A) A = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp'], dtype=dtype_op) x = np.array([1, 1, 1]).astype(dtype_in) assert_((A * x).dtype == dtype_out)
def test_init(self): matvecs = get_matvecs(self.A) A = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec']) assert_(hasattr(A, '_matvec')) assert_(hasattr(A, 'dtype')) assert_(A.T is None) assert_(A.H is None) A = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp']) assert_(A.T is not None) assert_(A.H is A.T) A = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp']) B = lo.LinearOperator(nargin=matvecs['shape'][0], nargout=matvecs['shape'][1], matvec=matvecs['matvec_transp']) x = np.random.random(A.shape[0]) assert_(np.allclose(B * x, A.T * x)) assert_(np.allclose(B * x, A.H * x)) matvecs = get_matvecs(self.D) D = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp'], matvec_adj=matvecs['matvec_adj'], dtype=self.D.dtype) assert_(D.T is not None) assert_(D.H is not None) E = lo.LinearOperator(nargin=matvecs['shape'][0], nargout=matvecs['shape'][1], matvec=matvecs['matvec_transp'], matvec_transp=matvecs['matvec'], dtype=self.E.dtype) x = np.random.random(E.shape[1]) + 1j * np.random.random(E.shape[1]) assert_(np.allclose(E * x, D.T * x)) assert_(E.H is not None) # E.H was inferred. y = np.random.random(E.shape[0]) + 1j * np.random.random(E.shape[0]) assert_(np.allclose(E.H * y, E.rmatvec(y))) F = lo.LinearOperator(nargin=matvecs['shape'][0], nargout=matvecs['shape'][1], matvec=matvecs['matvec_adj'], dtype=self.F.dtype) x = np.random.random(F.shape[1]) + 1j * np.random.random(F.shape[1]) assert_(np.allclose(F * x, D.H * x)) matvecs = get_matvecs(self.G) G = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp'], matvec_adj=matvecs['matvec_adj'], dtype=self.G.dtype) x = np.random.random(G.shape[1]) + 1j * np.random.random(G.shape[1]) assert_(np.allclose(G * x, G.H * x)) assert_(np.allclose(G.T.H * x, G.H.T * x)) matvecs = get_matvecs(self.H) H = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp'], matvec_adj=matvecs['matvec_adj'], dtype=self.H.dtype) x = np.random.random(H.shape[1]) + 1j * np.random.random(H.shape[1]) assert_(np.allclose(H * x, H.T * x)) assert_(np.allclose(H * x, H.T.T * x))
def test_runtime(self): matvecs = get_matvecs(self.A) A = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp']) matvecs = get_matvecs(self.B) B = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp']) matvecs = get_matvecs(self.C) C = lo.LinearOperator(nargin=matvecs['shape'][1], nargout=matvecs['shape'][0], matvec=matvecs['matvec'], matvec_transp=matvecs['matvec_transp']) u = np.array([1, 1]) v = np.array([1, 1, 1]) assert_equal(A * v, [6, 15]) assert_equal(A._matvec(v), [6, 15]) assert_equal(A.T * u, [5, 7, 9]) assert_equal(A.H * u, [5, 7, 9]) assert_equal((A * 2) * v, A * (2 * v)) assert_equal((A * 2) * v, (2 * A) * v) assert_equal((A / 2) * v, A * (v / 2)) assert_equal((-A) * v, A * (-v)) assert_equal((A - A) * v, [0, 0]) assert_equal((C**2) * u, [17, 37]) assert_equal((C**2) * u, (C * C) * u) assert_(isinstance(A + A, lo.LinearOperator)) assert_(isinstance(A - A, lo.LinearOperator)) assert_(isinstance(-A, lo.LinearOperator)) assert_(isinstance(2 * A, lo.LinearOperator)) assert_(isinstance(A * 2, lo.LinearOperator)) assert_(isinstance(A * 0, lo.ZeroOperator)) assert_(isinstance(A / 2, lo.LinearOperator)) assert_(isinstance(C**2, lo.LinearOperator)) assert_(isinstance(C**0, lo.IdentityOperator)) sum_A = lambda x: A + x assert_raises(ValueError, sum_A, 3) assert_raises(ValueError, sum_A, v) assert_raises(ShapeError, sum_A, B) sub_A = lambda x: A - x assert_raises(ValueError, sub_A, 3) assert_raises(ValueError, sub_A, v) assert_raises(ShapeError, sub_A, B) mul_A = lambda x: A * x assert_raises(ValueError, mul_A, u) assert_raises(ShapeError, mul_A, A) div_A = lambda x: A / x assert_raises(ValueError, div_A, B) assert_raises(ValueError, div_A, u) assert_raises(ZeroDivisionError, div_A, 0) pow_A = lambda x: A**x pow_C = lambda x: C**x assert_raises(ShapeError, pow_A, 2) assert_raises(ValueError, pow_C, -2) assert_raises(ValueError, pow_C, 2.1)