interface.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. """Abstract linear algebra library.
  2. This module defines a class hierarchy that implements a kind of "lazy"
  3. matrix representation, called the ``LinearOperator``. It can be used to do
  4. linear algebra with extremely large sparse or structured matrices, without
  5. representing those explicitly in memory. Such matrices can be added,
  6. multiplied, transposed, etc.
  7. As a motivating example, suppose you want have a matrix where almost all of
  8. the elements have the value one. The standard sparse matrix representation
  9. skips the storage of zeros, but not ones. By contrast, a LinearOperator is
  10. able to represent such matrices efficiently. First, we need a compact way to
  11. represent an all-ones matrix::
  12. >>> import numpy as np
  13. >>> class Ones(LinearOperator):
  14. ... def __init__(self, shape):
  15. ... super(Ones, self).__init__(dtype=None, shape=shape)
  16. ... def _matvec(self, x):
  17. ... return np.repeat(x.sum(), self.shape[0])
  18. Instances of this class emulate ``np.ones(shape)``, but using a constant
  19. amount of storage, independent of ``shape``. The ``_matvec`` method specifies
  20. how this linear operator multiplies with (operates on) a vector. We can now
  21. add this operator to a sparse matrix that stores only offsets from one::
  22. >>> from scipy.sparse import csr_matrix
  23. >>> offsets = csr_matrix([[1, 0, 2], [0, -1, 0], [0, 0, 3]])
  24. >>> A = aslinearoperator(offsets) + Ones(offsets.shape)
  25. >>> A.dot([1, 2, 3])
  26. array([13, 4, 15])
  27. The result is the same as that given by its dense, explicitly-stored
  28. counterpart::
  29. >>> (np.ones(A.shape, A.dtype) + offsets.toarray()).dot([1, 2, 3])
  30. array([13, 4, 15])
  31. Several algorithms in the ``scipy.sparse`` library are able to operate on
  32. ``LinearOperator`` instances.
  33. """
  34. from __future__ import division, print_function, absolute_import
  35. import warnings
  36. import numpy as np
  37. from scipy.sparse import isspmatrix
  38. from scipy.sparse.sputils import isshape, isintlike
  39. __all__ = ['LinearOperator', 'aslinearoperator']
  40. class LinearOperator(object):
  41. """Common interface for performing matrix vector products
  42. Many iterative methods (e.g. cg, gmres) do not need to know the
  43. individual entries of a matrix to solve a linear system A*x=b.
  44. Such solvers only require the computation of matrix vector
  45. products, A*v where v is a dense vector. This class serves as
  46. an abstract interface between iterative solvers and matrix-like
  47. objects.
  48. To construct a concrete LinearOperator, either pass appropriate
  49. callables to the constructor of this class, or subclass it.
  50. A subclass must implement either one of the methods ``_matvec``
  51. and ``_matmat``, and the attributes/properties ``shape`` (pair of
  52. integers) and ``dtype`` (may be None). It may call the ``__init__``
  53. on this class to have these attributes validated. Implementing
  54. ``_matvec`` automatically implements ``_matmat`` (using a naive
  55. algorithm) and vice-versa.
  56. Optionally, a subclass may implement ``_rmatvec`` or ``_adjoint``
  57. to implement the Hermitian adjoint (conjugate transpose). As with
  58. ``_matvec`` and ``_matmat``, implementing either ``_rmatvec`` or
  59. ``_adjoint`` implements the other automatically. Implementing
  60. ``_adjoint`` is preferable; ``_rmatvec`` is mostly there for
  61. backwards compatibility.
  62. Parameters
  63. ----------
  64. shape : tuple
  65. Matrix dimensions (M,N).
  66. matvec : callable f(v)
  67. Returns returns A * v.
  68. rmatvec : callable f(v)
  69. Returns A^H * v, where A^H is the conjugate transpose of A.
  70. matmat : callable f(V)
  71. Returns A * V, where V is a dense matrix with dimensions (N,K).
  72. dtype : dtype
  73. Data type of the matrix.
  74. Attributes
  75. ----------
  76. args : tuple
  77. For linear operators describing products etc. of other linear
  78. operators, the operands of the binary operation.
  79. See Also
  80. --------
  81. aslinearoperator : Construct LinearOperators
  82. Notes
  83. -----
  84. The user-defined matvec() function must properly handle the case
  85. where v has shape (N,) as well as the (N,1) case. The shape of
  86. the return type is handled internally by LinearOperator.
  87. LinearOperator instances can also be multiplied, added with each
  88. other and exponentiated, all lazily: the result of these operations
  89. is always a new, composite LinearOperator, that defers linear
  90. operations to the original operators and combines the results.
  91. Examples
  92. --------
  93. >>> import numpy as np
  94. >>> from scipy.sparse.linalg import LinearOperator
  95. >>> def mv(v):
  96. ... return np.array([2*v[0], 3*v[1]])
  97. ...
  98. >>> A = LinearOperator((2,2), matvec=mv)
  99. >>> A
  100. <2x2 _CustomLinearOperator with dtype=float64>
  101. >>> A.matvec(np.ones(2))
  102. array([ 2., 3.])
  103. >>> A * np.ones(2)
  104. array([ 2., 3.])
  105. """
  106. def __new__(cls, *args, **kwargs):
  107. if cls is LinearOperator:
  108. # Operate as _CustomLinearOperator factory.
  109. return super(LinearOperator, cls).__new__(_CustomLinearOperator)
  110. else:
  111. obj = super(LinearOperator, cls).__new__(cls)
  112. if (type(obj)._matvec == LinearOperator._matvec
  113. and type(obj)._matmat == LinearOperator._matmat):
  114. warnings.warn("LinearOperator subclass should implement"
  115. " at least one of _matvec and _matmat.",
  116. category=RuntimeWarning, stacklevel=2)
  117. return obj
  118. def __init__(self, dtype, shape):
  119. """Initialize this LinearOperator.
  120. To be called by subclasses. ``dtype`` may be None; ``shape`` should
  121. be convertible to a length-2 tuple.
  122. """
  123. if dtype is not None:
  124. dtype = np.dtype(dtype)
  125. shape = tuple(shape)
  126. if not isshape(shape):
  127. raise ValueError("invalid shape %r (must be 2-d)" % (shape,))
  128. self.dtype = dtype
  129. self.shape = shape
  130. def _init_dtype(self):
  131. """Called from subclasses at the end of the __init__ routine.
  132. """
  133. if self.dtype is None:
  134. v = np.zeros(self.shape[-1])
  135. self.dtype = np.asarray(self.matvec(v)).dtype
  136. def _matmat(self, X):
  137. """Default matrix-matrix multiplication handler.
  138. Falls back on the user-defined _matvec method, so defining that will
  139. define matrix multiplication (though in a very suboptimal way).
  140. """
  141. return np.hstack([self.matvec(col.reshape(-1,1)) for col in X.T])
  142. def _matvec(self, x):
  143. """Default matrix-vector multiplication handler.
  144. If self is a linear operator of shape (M, N), then this method will
  145. be called on a shape (N,) or (N, 1) ndarray, and should return a
  146. shape (M,) or (M, 1) ndarray.
  147. This default implementation falls back on _matmat, so defining that
  148. will define matrix-vector multiplication as well.
  149. """
  150. return self.matmat(x.reshape(-1, 1))
  151. def matvec(self, x):
  152. """Matrix-vector multiplication.
  153. Performs the operation y=A*x where A is an MxN linear
  154. operator and x is a column vector or 1-d array.
  155. Parameters
  156. ----------
  157. x : {matrix, ndarray}
  158. An array with shape (N,) or (N,1).
  159. Returns
  160. -------
  161. y : {matrix, ndarray}
  162. A matrix or ndarray with shape (M,) or (M,1) depending
  163. on the type and shape of the x argument.
  164. Notes
  165. -----
  166. This matvec wraps the user-specified matvec routine or overridden
  167. _matvec method to ensure that y has the correct shape and type.
  168. """
  169. x = np.asanyarray(x)
  170. M,N = self.shape
  171. if x.shape != (N,) and x.shape != (N,1):
  172. raise ValueError('dimension mismatch')
  173. y = self._matvec(x)
  174. if isinstance(x, np.matrix):
  175. y = np.asmatrix(y)
  176. else:
  177. y = np.asarray(y)
  178. if x.ndim == 1:
  179. y = y.reshape(M)
  180. elif x.ndim == 2:
  181. y = y.reshape(M,1)
  182. else:
  183. raise ValueError('invalid shape returned by user-defined matvec()')
  184. return y
  185. def rmatvec(self, x):
  186. """Adjoint matrix-vector multiplication.
  187. Performs the operation y = A^H * x where A is an MxN linear
  188. operator and x is a column vector or 1-d array.
  189. Parameters
  190. ----------
  191. x : {matrix, ndarray}
  192. An array with shape (M,) or (M,1).
  193. Returns
  194. -------
  195. y : {matrix, ndarray}
  196. A matrix or ndarray with shape (N,) or (N,1) depending
  197. on the type and shape of the x argument.
  198. Notes
  199. -----
  200. This rmatvec wraps the user-specified rmatvec routine or overridden
  201. _rmatvec method to ensure that y has the correct shape and type.
  202. """
  203. x = np.asanyarray(x)
  204. M,N = self.shape
  205. if x.shape != (M,) and x.shape != (M,1):
  206. raise ValueError('dimension mismatch')
  207. y = self._rmatvec(x)
  208. if isinstance(x, np.matrix):
  209. y = np.asmatrix(y)
  210. else:
  211. y = np.asarray(y)
  212. if x.ndim == 1:
  213. y = y.reshape(N)
  214. elif x.ndim == 2:
  215. y = y.reshape(N,1)
  216. else:
  217. raise ValueError('invalid shape returned by user-defined rmatvec()')
  218. return y
  219. def _rmatvec(self, x):
  220. """Default implementation of _rmatvec; defers to adjoint."""
  221. if type(self)._adjoint == LinearOperator._adjoint:
  222. # _adjoint not overridden, prevent infinite recursion
  223. raise NotImplementedError
  224. else:
  225. return self.H.matvec(x)
  226. def matmat(self, X):
  227. """Matrix-matrix multiplication.
  228. Performs the operation y=A*X where A is an MxN linear
  229. operator and X dense N*K matrix or ndarray.
  230. Parameters
  231. ----------
  232. X : {matrix, ndarray}
  233. An array with shape (N,K).
  234. Returns
  235. -------
  236. Y : {matrix, ndarray}
  237. A matrix or ndarray with shape (M,K) depending on
  238. the type of the X argument.
  239. Notes
  240. -----
  241. This matmat wraps any user-specified matmat routine or overridden
  242. _matmat method to ensure that y has the correct type.
  243. """
  244. X = np.asanyarray(X)
  245. if X.ndim != 2:
  246. raise ValueError('expected 2-d ndarray or matrix, not %d-d'
  247. % X.ndim)
  248. M,N = self.shape
  249. if X.shape[0] != N:
  250. raise ValueError('dimension mismatch: %r, %r'
  251. % (self.shape, X.shape))
  252. Y = self._matmat(X)
  253. if isinstance(Y, np.matrix):
  254. Y = np.asmatrix(Y)
  255. return Y
  256. def __call__(self, x):
  257. return self*x
  258. def __mul__(self, x):
  259. return self.dot(x)
  260. def dot(self, x):
  261. """Matrix-matrix or matrix-vector multiplication.
  262. Parameters
  263. ----------
  264. x : array_like
  265. 1-d or 2-d array, representing a vector or matrix.
  266. Returns
  267. -------
  268. Ax : array
  269. 1-d or 2-d array (depending on the shape of x) that represents
  270. the result of applying this linear operator on x.
  271. """
  272. if isinstance(x, LinearOperator):
  273. return _ProductLinearOperator(self, x)
  274. elif np.isscalar(x):
  275. return _ScaledLinearOperator(self, x)
  276. else:
  277. x = np.asarray(x)
  278. if x.ndim == 1 or x.ndim == 2 and x.shape[1] == 1:
  279. return self.matvec(x)
  280. elif x.ndim == 2:
  281. return self.matmat(x)
  282. else:
  283. raise ValueError('expected 1-d or 2-d array or matrix, got %r'
  284. % x)
  285. def __matmul__(self, other):
  286. if np.isscalar(other):
  287. raise ValueError("Scalar operands are not allowed, "
  288. "use '*' instead")
  289. return self.__mul__(other)
  290. def __rmatmul__(self, other):
  291. if np.isscalar(other):
  292. raise ValueError("Scalar operands are not allowed, "
  293. "use '*' instead")
  294. return self.__rmul__(other)
  295. def __rmul__(self, x):
  296. if np.isscalar(x):
  297. return _ScaledLinearOperator(self, x)
  298. else:
  299. return NotImplemented
  300. def __pow__(self, p):
  301. if np.isscalar(p):
  302. return _PowerLinearOperator(self, p)
  303. else:
  304. return NotImplemented
  305. def __add__(self, x):
  306. if isinstance(x, LinearOperator):
  307. return _SumLinearOperator(self, x)
  308. else:
  309. return NotImplemented
  310. def __neg__(self):
  311. return _ScaledLinearOperator(self, -1)
  312. def __sub__(self, x):
  313. return self.__add__(-x)
  314. def __repr__(self):
  315. M,N = self.shape
  316. if self.dtype is None:
  317. dt = 'unspecified dtype'
  318. else:
  319. dt = 'dtype=' + str(self.dtype)
  320. return '<%dx%d %s with %s>' % (M, N, self.__class__.__name__, dt)
  321. def adjoint(self):
  322. """Hermitian adjoint.
  323. Returns the Hermitian adjoint of self, aka the Hermitian
  324. conjugate or Hermitian transpose. For a complex matrix, the
  325. Hermitian adjoint is equal to the conjugate transpose.
  326. Can be abbreviated self.H instead of self.adjoint().
  327. Returns
  328. -------
  329. A_H : LinearOperator
  330. Hermitian adjoint of self.
  331. """
  332. return self._adjoint()
  333. H = property(adjoint)
  334. def transpose(self):
  335. """Transpose this linear operator.
  336. Returns a LinearOperator that represents the transpose of this one.
  337. Can be abbreviated self.T instead of self.transpose().
  338. """
  339. return self._transpose()
  340. T = property(transpose)
  341. def _adjoint(self):
  342. """Default implementation of _adjoint; defers to rmatvec."""
  343. shape = (self.shape[1], self.shape[0])
  344. return _CustomLinearOperator(shape, matvec=self.rmatvec,
  345. rmatvec=self.matvec,
  346. dtype=self.dtype)
  347. class _CustomLinearOperator(LinearOperator):
  348. """Linear operator defined in terms of user-specified operations."""
  349. def __init__(self, shape, matvec, rmatvec=None, matmat=None, dtype=None):
  350. super(_CustomLinearOperator, self).__init__(dtype, shape)
  351. self.args = ()
  352. self.__matvec_impl = matvec
  353. self.__rmatvec_impl = rmatvec
  354. self.__matmat_impl = matmat
  355. self._init_dtype()
  356. def _matmat(self, X):
  357. if self.__matmat_impl is not None:
  358. return self.__matmat_impl(X)
  359. else:
  360. return super(_CustomLinearOperator, self)._matmat(X)
  361. def _matvec(self, x):
  362. return self.__matvec_impl(x)
  363. def _rmatvec(self, x):
  364. func = self.__rmatvec_impl
  365. if func is None:
  366. raise NotImplementedError("rmatvec is not defined")
  367. return self.__rmatvec_impl(x)
  368. def _adjoint(self):
  369. return _CustomLinearOperator(shape=(self.shape[1], self.shape[0]),
  370. matvec=self.__rmatvec_impl,
  371. rmatvec=self.__matvec_impl,
  372. dtype=self.dtype)
  373. def _get_dtype(operators, dtypes=None):
  374. if dtypes is None:
  375. dtypes = []
  376. for obj in operators:
  377. if obj is not None and hasattr(obj, 'dtype'):
  378. dtypes.append(obj.dtype)
  379. return np.find_common_type(dtypes, [])
  380. class _SumLinearOperator(LinearOperator):
  381. def __init__(self, A, B):
  382. if not isinstance(A, LinearOperator) or \
  383. not isinstance(B, LinearOperator):
  384. raise ValueError('both operands have to be a LinearOperator')
  385. if A.shape != B.shape:
  386. raise ValueError('cannot add %r and %r: shape mismatch'
  387. % (A, B))
  388. self.args = (A, B)
  389. super(_SumLinearOperator, self).__init__(_get_dtype([A, B]), A.shape)
  390. def _matvec(self, x):
  391. return self.args[0].matvec(x) + self.args[1].matvec(x)
  392. def _rmatvec(self, x):
  393. return self.args[0].rmatvec(x) + self.args[1].rmatvec(x)
  394. def _matmat(self, x):
  395. return self.args[0].matmat(x) + self.args[1].matmat(x)
  396. def _adjoint(self):
  397. A, B = self.args
  398. return A.H + B.H
  399. class _ProductLinearOperator(LinearOperator):
  400. def __init__(self, A, B):
  401. if not isinstance(A, LinearOperator) or \
  402. not isinstance(B, LinearOperator):
  403. raise ValueError('both operands have to be a LinearOperator')
  404. if A.shape[1] != B.shape[0]:
  405. raise ValueError('cannot multiply %r and %r: shape mismatch'
  406. % (A, B))
  407. super(_ProductLinearOperator, self).__init__(_get_dtype([A, B]),
  408. (A.shape[0], B.shape[1]))
  409. self.args = (A, B)
  410. def _matvec(self, x):
  411. return self.args[0].matvec(self.args[1].matvec(x))
  412. def _rmatvec(self, x):
  413. return self.args[1].rmatvec(self.args[0].rmatvec(x))
  414. def _matmat(self, x):
  415. return self.args[0].matmat(self.args[1].matmat(x))
  416. def _adjoint(self):
  417. A, B = self.args
  418. return B.H * A.H
  419. class _ScaledLinearOperator(LinearOperator):
  420. def __init__(self, A, alpha):
  421. if not isinstance(A, LinearOperator):
  422. raise ValueError('LinearOperator expected as A')
  423. if not np.isscalar(alpha):
  424. raise ValueError('scalar expected as alpha')
  425. dtype = _get_dtype([A], [type(alpha)])
  426. super(_ScaledLinearOperator, self).__init__(dtype, A.shape)
  427. self.args = (A, alpha)
  428. def _matvec(self, x):
  429. return self.args[1] * self.args[0].matvec(x)
  430. def _rmatvec(self, x):
  431. return np.conj(self.args[1]) * self.args[0].rmatvec(x)
  432. def _matmat(self, x):
  433. return self.args[1] * self.args[0].matmat(x)
  434. def _adjoint(self):
  435. A, alpha = self.args
  436. return A.H * np.conj(alpha)
  437. class _PowerLinearOperator(LinearOperator):
  438. def __init__(self, A, p):
  439. if not isinstance(A, LinearOperator):
  440. raise ValueError('LinearOperator expected as A')
  441. if A.shape[0] != A.shape[1]:
  442. raise ValueError('square LinearOperator expected, got %r' % A)
  443. if not isintlike(p) or p < 0:
  444. raise ValueError('non-negative integer expected as p')
  445. super(_PowerLinearOperator, self).__init__(_get_dtype([A]), A.shape)
  446. self.args = (A, p)
  447. def _power(self, fun, x):
  448. res = np.array(x, copy=True)
  449. for i in range(self.args[1]):
  450. res = fun(res)
  451. return res
  452. def _matvec(self, x):
  453. return self._power(self.args[0].matvec, x)
  454. def _rmatvec(self, x):
  455. return self._power(self.args[0].rmatvec, x)
  456. def _matmat(self, x):
  457. return self._power(self.args[0].matmat, x)
  458. def _adjoint(self):
  459. A, p = self.args
  460. return A.H ** p
  461. class MatrixLinearOperator(LinearOperator):
  462. def __init__(self, A):
  463. super(MatrixLinearOperator, self).__init__(A.dtype, A.shape)
  464. self.A = A
  465. self.__adj = None
  466. self.args = (A,)
  467. def _matmat(self, X):
  468. return self.A.dot(X)
  469. def _adjoint(self):
  470. if self.__adj is None:
  471. self.__adj = _AdjointMatrixOperator(self)
  472. return self.__adj
  473. class _AdjointMatrixOperator(MatrixLinearOperator):
  474. def __init__(self, adjoint):
  475. self.A = adjoint.A.T.conj()
  476. self.__adjoint = adjoint
  477. self.args = (adjoint,)
  478. self.shape = adjoint.shape[1], adjoint.shape[0]
  479. @property
  480. def dtype(self):
  481. return self.__adjoint.dtype
  482. def _adjoint(self):
  483. return self.__adjoint
  484. class IdentityOperator(LinearOperator):
  485. def __init__(self, shape, dtype=None):
  486. super(IdentityOperator, self).__init__(dtype, shape)
  487. def _matvec(self, x):
  488. return x
  489. def _rmatvec(self, x):
  490. return x
  491. def _matmat(self, x):
  492. return x
  493. def _adjoint(self):
  494. return self
  495. def aslinearoperator(A):
  496. """Return A as a LinearOperator.
  497. 'A' may be any of the following types:
  498. - ndarray
  499. - matrix
  500. - sparse matrix (e.g. csr_matrix, lil_matrix, etc.)
  501. - LinearOperator
  502. - An object with .shape and .matvec attributes
  503. See the LinearOperator documentation for additional information.
  504. Notes
  505. -----
  506. If 'A' has no .dtype attribute, the data type is determined by calling
  507. :func:`LinearOperator.matvec()` - set the .dtype attribute to prevent this
  508. call upon the linear operator creation.
  509. Examples
  510. --------
  511. >>> from scipy.sparse.linalg import aslinearoperator
  512. >>> M = np.array([[1,2,3],[4,5,6]], dtype=np.int32)
  513. >>> aslinearoperator(M)
  514. <2x3 MatrixLinearOperator with dtype=int32>
  515. """
  516. if isinstance(A, LinearOperator):
  517. return A
  518. elif isinstance(A, np.ndarray) or isinstance(A, np.matrix):
  519. if A.ndim > 2:
  520. raise ValueError('array must have ndim <= 2')
  521. A = np.atleast_2d(np.asarray(A))
  522. return MatrixLinearOperator(A)
  523. elif isspmatrix(A):
  524. return MatrixLinearOperator(A)
  525. else:
  526. if hasattr(A, 'shape') and hasattr(A, 'matvec'):
  527. rmatvec = None
  528. dtype = None
  529. if hasattr(A, 'rmatvec'):
  530. rmatvec = A.rmatvec
  531. if hasattr(A, 'dtype'):
  532. dtype = A.dtype
  533. return LinearOperator(A.shape, A.matvec,
  534. rmatvec=rmatvec, dtype=dtype)
  535. else:
  536. raise TypeError('type not understood')