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
|