/usr/share/pyshared/openopt/solvers/Standalone/defaultSLEsolver_oo.py is in python-openopt 0.38+svn1589-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | from numpy.linalg import norm
from numpy import dot, asfarray, atleast_1d, zeros, ones, int, float64, where, inf, linalg, ndarray, prod
from openopt.kernel.baseSolver import baseSolver
from openopt.kernel.nonOptMisc import scipyAbsentMsg, isspmatrix
try:
import scipy
scipyInstalled = True
from scipy.sparse import linalg
except:
scipyInstalled = False
class defaultSLEsolver(baseSolver):
__name__ = 'defaultSLEsolver'
__license__ = "BSD"
__authors__ = 'Dmitrey'
__alg__ = ''
__info__ = ''
matrixSLEsolver = 'autoselect'
sparseSolvers = ['bicg', 'bicgstab', 'cg', 'cgs', 'gmres', 'minres', 'qmr', 'spsolve']
denseSolvers = ['numpy_linalg_solve']
defaultSparseSolver = 'spsolve'
defaultDenseSolver = 'numpy_linalg_solve'
#__optionalDataThatCanBeHandled__ = []
def __init__(self): pass
def __solver__(self, p):
#p.debug = 1
# TODO: code clean up
solver = self.matrixSLEsolver
if solver in self.denseSolvers:
useDense = True
elif solver in self.sparseSolvers:
useDense = False
elif solver == 'autoselect':
if not scipyInstalled:
useDense = True
if isspmatrix(p.C) and prod(p.C.shape)>100000:
s = """You have no scipy installed .
Thus the SLE will be solved as dense.
"""
p.pWarn(s)
solver = self.defaultDenseSolver
self.matrixSLEsolver = solver
else:
solver = self.defaultSparseSolver
self.matrixSLEsolver = solver
useDense = False
else:
p.err('Incorrect SLE solver (%s)' % solver)
if isinstance(solver, str):
if solver == 'numpy_linalg_solve':
solver = linalg.solve
else:
solver = getattr(scipy.sparse.linalg, solver)
if useDense:
#p.debugmsg('dense SLE solver')
try:
C = p.C.toarray() if not isinstance(p.C, ndarray) else p.C
if self.matrixSLEsolver == 'autoselect':
self.matrixSLEsolver = linalg.solve
xf = solver(C, p.d)
istop, msg = 10, 'solved'
p.xf = xf
p.ff = norm(dot(C, xf)-p.d, inf)
except linalg.LinAlgError:
istop, msg = -10, 'singular matrix'
else: # is sparse
#p.debugmsg('sparse SLE solver')
try:
if not hasattr(p, 'C_as_csc'):p.C_as_csc = scipy.sparse.csc_matrix(p.C)
xf = solver(p.C_as_csc, p.d)#, maxiter=10000)
solver_istop = 0
if solver_istop == 0:
istop, msg = 10, 'solved'
else:
istop, msg = -104, 'the solver involved failed to solve the SLE'
if solver_istop < 0: msg += ', matter: illegal input or breakdown'
else: msg += ', matter: convergence to tolerance not achieved, number of iterations: %d' % solver_istop
p.xf = xf
p.ff = norm(p.C_as_csc._mul_sparse_matrix(scipy.sparse.csr_matrix(xf.reshape(-1, 1))).toarray().flatten()-p.d, inf)
except:
istop, msg = -100, 'unimplemented exception while solving sparse SLE'
p.istop, p.msg = istop, msg
|