def test_asmatrix(self): a = [[1, 2, 3]] b = np.array(a) assert isinstance(sputils.asmatrix(a), np.matrix) assert isinstance(sputils.asmatrix(b), np.matrix) c = sputils.asmatrix(b) c[:, :] = 123 assert_equal(b, [[123, 123, 123]])
def rmatvec(self, x): """Adjoint matrix-vector multiplication. Performs the operation y = A^H * x where A is an MxN linear operator and x is a column vector or 1-d array. Parameters ---------- x : {matrix, ndarray} An array with shape (M,) or (M,1). Returns ------- y : {matrix, ndarray} A matrix or ndarray with shape (N,) or (N,1) depending on the type and shape of the x argument. Notes ----- This rmatvec wraps the user-specified rmatvec routine or overridden _rmatvec method to ensure that y has the correct shape and type. """ x = np.asanyarray(x) M, N = self.shape if x.shape != (M, ) and x.shape != (M, 1): raise ValueError('dimension mismatch') y = self._rmatvec(x) if isinstance(x, np.matrix): y = asmatrix(y) else: y = np.asarray(y) if x.ndim == 1: y = y.reshape(N) elif x.ndim == 2: y = y.reshape(N, 1) else: raise ValueError( 'invalid shape returned by user-defined rmatvec()') return y
def matmat(self, X): """Matrix-matrix multiplication. Performs the operation y=A*X where A is an MxN linear operator and X dense N*K matrix or ndarray. Parameters ---------- X : {matrix, ndarray} An array with shape (N,K). Returns ------- Y : {matrix, ndarray} A matrix or ndarray with shape (M,K) depending on the type of the X argument. Notes ----- This matmat wraps any user-specified matmat routine or overridden _matmat method to ensure that y has the correct type. """ X = np.asanyarray(X) if X.ndim != 2: raise ValueError('expected 2-d ndarray or matrix, not %d-d' % X.ndim) if X.shape[0] != self.shape[1]: raise ValueError('dimension mismatch: %r, %r' % (self.shape, X.shape)) Y = self._matmat(X) if isinstance(Y, np.matrix): Y = asmatrix(Y) return Y
def rmatmat(self, X): """Adjoint matrix-matrix multiplication. Performs the operation y = A^H * x where A is an MxN linear operator and x is a column vector or 1-d array, or 2-d array. The default implementation defers to the adjoint. Parameters ---------- X : {matrix, ndarray} A matrix or 2D array. Returns ------- Y : {matrix, ndarray} A matrix or 2D array depending on the type of the input. Notes ----- This rmatmat wraps the user-specified rmatmat routine. """ X = np.asanyarray(X) if X.ndim != 2: raise ValueError('expected 2-d ndarray or matrix, not %d-d' % X.ndim) if X.shape[0] != self.shape[0]: raise ValueError('dimension mismatch: %r, %r' % (self.shape, X.shape)) Y = self._rmatmat(X) if isinstance(Y, np.matrix): Y = asmatrix(Y) return Y
def postprocess(x): if isinstance(b, matrix): x = asmatrix(x) return x.reshape(b.shape)