<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import scipy . sparse as sparse <EOL> import scipy . sparse . linalg as splinalg <EOL> from tools import getresidual <EOL> def coarseSolve ( A , b ) : <EOL> """<STR_LIT>""" <EOL> if sparse . issparse ( A ) : <EOL> toreturn = splinalg . spsolve ( A , b ) <EOL> else : <EOL> toreturn = np . linalg . solve ( A , b ) <EOL> return np . ravel ( toreturn ) <EOL> def smooth ( A , b , x , iterations , verbose = False ) : <EOL> return gaussSeidel ( A , b , x , iterations = iterations , verbose = verbose ) <EOL> def smoothToThreshold ( A , b , x , threshold , verbose = False ) : <EOL> return gaussSeidel ( A , b , x , threshold = threshold , verbose = verbose ) <EOL> def gaussSeidel ( A , b , x , iterations = None , threshold = None , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> if iterations is None and threshold is None : <EOL> iterations = <NUM_LIT:1> <EOL> N = x . size <EOL> def stop ( iteration , x ) : <EOL> iterStatus = threshStatus = False <EOL> if iterations is not None : <EOL> iterStatus = ( iteration >= iterations ) <EOL> if threshold is not None : <EOL> norm = np . linalg . norm ( getresidual ( b , A , x , N ) ) <EOL> threshStatus = ( norm < threshold ) <EOL> return iterStatus or threshStatus <EOL> iteration = <NUM_LIT:0> <EOL> stopping = stop ( iteration , x ) <EOL> while not stopping : <EOL> if sparse . issparse ( A ) : <EOL> for i in range ( N ) : <EOL> rowstart = A . indptr [ i ] <EOL> rowend = A . indptr [ i + <NUM_LIT:1> ] <EOL> Aix = np . dot ( A . data [ rowstart : rowend ] , <EOL> x [ A . indices [ rowstart : rowend ] ] <EOL> ) <EOL> x [ i ] = x [ i ] + ( b [ i ] - Aix ) / A [ i , i ] <EOL> else : <EOL> for i in range ( N ) : <EOL> x [ i ] = x [ i ] + ( np . array ( b ) . ravel ( ) [ i ] - np . dot ( A [ i , : ] , x . reshape ( ( N , <NUM_LIT:1> ) ) ) ) / A [ i , i ] <EOL> iteration += <NUM_LIT:1> <EOL> stopping = stop ( iteration , x ) <EOL> return x <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
