[docs]classBaseSolver:"""Base class for all solvers."""def__init__(self):...
[docs]defsolve(self):"""Solves the given linear system of equations Ax=b."""raiseNotImplementedError
[docs]classDirectSolver(BaseSolver):"""Base class for all direct solvers."""...
[docs]classIterativeSolver(BaseSolver):"""Base class for iterative solvers."""def__init__(self,tol=1e-8,maxiter=1000):self.tol=tolself.maxiter=maxiterself.atol=None# needs to be evaluated laterself.rtol=None# needs to be evaluated laterdef_get_atol(self,b):r""" Returns the absolute tolerance ``atol`` that corresponds to the the given tolerance ``tol``. Notes ----- ``atol`` is calculated to satisfy the following stopping criterion: ``norm(A*x-b)`` <= ``atol`` """returnnorm(b)*self.toldef_get_rtol(self,A,b,x0):r""" Returns the relative tolerance ``rtol`` that corresponds to the the given tolerance ``tol``. Notes ----- ``rtol`` is defined based on the following formula: ``rtol = residual(@x_final) / residual(@x0)`` """res0=self._get_residual(A,b,x0)atol=self._get_atol(b)rtol=atol/res0returnrtoldef_get_residual(self,A,b,x):r""" Calculates the residual based on the given ``x`` using: ``res = norm(A*x - b)`` """returnnorm(A*x-b)