Permalink
Browse files

expm and inv no longer methods

  • Loading branch information...
1 parent 6199119 commit 4c9692d8b2c03bf2442cb745dc27f2c72fa96eac @jakevdp jakevdp committed Aug 24, 2012
View
@@ -626,30 +626,6 @@ def _process_toarray_args(self, order, out):
return out
else:
return np.zeros(self.shape, dtype=self.dtype, order=order)
-
- def expm(self):
- """Compute the matrix exponential using Pade approximation.
-
- Returns
- -------
- expA : array, shape(M,M)
- Matrix exponential of A
-
- See Also
- --------
- scipy.sparse.linalg.expm
- """
- from scipy.sparse.linalg import expm as mfexpm
- return mfexpm(self)
-
- def inv(self):
- """Computes the inverse of the sparse matrix.
- """
- from construct import eye
- from linalg import spsolve
- I = eye(self.shape[0], self.shape[1], dtype=self.dtype, format=self.format)
- selfinv = spsolve(self, I)
- return selfinv
def isspmatrix(x):
@@ -14,6 +14,15 @@
LinearOperator -- abstract representation of a linear operator
aslinearoperator -- convert an object to an abstract linear operator
+Matrix Operations
+-----------------
+
+.. autosummary::
+ :toctree: generated/
+
+ inv -- compute the sparse matrix inverse
+ expm -- compute the sparse matrix exponential
+
Solving linear problems
-----------------------
@@ -3,7 +3,7 @@
# Author: Anthony Scopatz, August 2012 (Sparse Updates)
#
-__all__ = ['expm', ]
+__all__ = ['expm', 'inv']
from numpy import asarray, dot, eye, ceil, log2
from numpy import matrix as mat
@@ -14,8 +14,31 @@
from scipy.sparse.base import isspmatrix
from scipy.sparse.construct import eye as speye
+from scipy.sparse.linalg import spsolve
+def inv(A):
+ """Compute the inverse of a sparse matrix
+
+ Parameters
+ ----------
+ A : ndarray or sparse matrix
+ square matrix to be inverted
+
+ Returns
+ -------
+ Ainv : inverse of A
+
+ Notes
+ -----
+ This computes the sparse inverse of A. If the inverse of A is expected
+ to be non-sparse, it will likely be faster to convert A to dense and use
+ scipy.linalg.inv.
+ """
+ I = speye(A.shape[0], A.shape[1], dtype=A.dtype, format=A.format)
+ Ainv = spsolve(A, I)
+ return Ainv
+
def expm(A):
"""Compute the matrix exponential using Pade approximation.
@@ -15,7 +15,6 @@
from scipy.sparse.linalg import expm
from scipy.linalg import logm
-
class TestExpM(TestCase):
def test_zero(self):
a = array([[0.,0],[0,0]])
@@ -33,7 +33,7 @@
coo_matrix, lil_matrix, dia_matrix, bsr_matrix, \
eye, isspmatrix, SparseEfficiencyWarning
from scipy.sparse.sputils import supported_dtypes
-from scipy.sparse.linalg import splu
+from scipy.sparse.linalg import splu, expm, inv
warnings.simplefilter('ignore', SparseEfficiencyWarning)
@@ -162,19 +162,19 @@ def test_expm(self):
M = array([[1, 0, 2], [0, 0, 3], [-4, 5, 6]], float)
sM = self.spmatrix(M, shape=(3,3), dtype=float)
Mexp = scipy.linalg.expm(M)
- sMexp = sM.expm().todense()
+ sMexp = expm(sM).todense()
assert_array_almost_equal((sMexp - Mexp), zeros((3, 3)))
N = array([[ 3., 0., 1.], [ 0., 2., 0.], [ 0., 0., 0.]])
sN = self.spmatrix(N, shape=(3,3), dtype=float)
Nexp = scipy.linalg.expm(N)
- sNexp = sN.expm().todense()
+ sNexp = expm(sN).todense()
assert_array_almost_equal((sNexp - Nexp), zeros((3, 3)))
def test_inv(self):
M = array([[1, 0, 2], [0, 0, 3], [-4, 5, 6]], float)
sM = self.spmatrix(M, shape=(3,3), dtype=float)
- sMinv = sM.inv()
+ sMinv = inv(sM)
assert_array_almost_equal(sMinv.dot(sM).todense(), np.eye(3))
def test_from_array(self):

0 comments on commit 4c9692d

Please sign in to comment.