Package parsimony :: Package utils :: Module linalgs :: Class SparseSolver
[hide private]
[frames] | no frames]

Class SparseSolver

source code

object --+    
         |    
    Solver --+
             |
            SparseSolver

Nested Classes [hide private]

Inherited from Solver: __metaclass__

Instance Methods [hide private]
 
solve(self, A, b, **kwargs)
Solves linear systems on the form
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]

Inherited from Solver: __abstractmethods__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

solve(self, A, b, **kwargs)

source code 
Solves linear systems on the form

    A.x = d,

for x.

Parameters
----------
A : A sparse matrix with shape n-by-p. The coefficient matrix.

b : Numpy array, n-by-1. The right-hand-side vector.

Examples
--------
>>> import numpy as np
>>> import scipy.sparse as sparse
>>> import parsimony.utils.linalgs as linalgs
>>> np.random.seed(42)
>>>
>>> n = 10
>>> a = np.random.rand(n); a[-1] = 0.0
>>> b = np.random.rand(n)
>>> c = np.random.rand(n); c[0] = 0.0
>>> A_ = np.random.rand(n, n)
>>> A_[A_ < 0.5] = 0.0
>>> A = sparse.csr_matrix(A_)
>>> d = np.random.rand(n, 1)
>>>
>>> solver = linalgs.SparseSolver()
>>> x = solver.solve(A, d)
>>> x_ = np.linalg.solve(A.toarray(), d)
>>> round(np.linalg.norm(x - x_), 15)
1e-15
>>>
>>> import time
>>> n = 100
>>> a = np.random.rand(n); a[-1] = 0.0
>>> b = np.random.rand(n)
>>> c = np.random.rand(n); c[0] = 0.0
>>> A_ = np.random.rand(n, n)
>>> A_[A_ < 0.5] = 0.0
>>> A = sparse.csr_matrix(A_)
>>> d = np.random.rand(n, 1)
>>>
>>> t = time.time()
>>> x = solver.solve(A, d)
>>> print "Time:", time.time() - t  # doctest: +SKIP
>>>
>>> t = time.time()
>>> x_ = np.linalg.solve(A.toarray(), d)
>>> print "Time:", time.time() - t  # doctest: +SKIP
>>> np.linalg.norm(x - x_) < 5e-14
True
>>>
>>> n = 1000
>>> a = np.random.rand(n); a[-1] = 0.0
>>> b = np.random.rand(n)
>>> c = np.random.rand(n); c[0] = 0.0
>>> A_ = np.random.rand(n, n)
>>> A_[A_ < 0.5] = 0.0
>>> A = sparse.csr_matrix(A_)
>>> d = np.random.rand(n, 1)
>>>
>>> t = time.time()
>>> x = solver.solve(A, d)
>>> print "Time:", time.time() - t  # doctest: +SKIP
>>>
>>> t = time.time()
>>> x_ = np.linalg.solve(A.toarray(), d)
>>> print "Time:", time.time() - t  # doctest: +SKIP
>>>
>>> np.linalg.norm(x - x_) < 5e-11
True

Overrides: Solver.solve