###################################################################### # # Function to solve a banded system of linear equations using # Gaussian elimination and backsubstitution # # x = banded(A,v,up,down) # # This function returns the vector solution x of the equation A.x = v, # where v is an array representing a vector of N elements, either real # or complex, and A is an N by N banded matrix with "up" nonzero # elements above the diagonal and "down" nonzero elements below the # diagonal. The matrix is specified as a two-dimensional array of # (1+up+down) by N elements with the diagonals of the original matrix # along its rows, thus: # # ( - - A02 A13 A24 ... # ( - A01 A12 A23 A34 ... # ( A00 A11 A22 A33 A44 ... # ( A10 A21 A32 A43 A54 ... # ( A20 A31 A42 A53 A64 ... # # Elements represented by dashes are ignored: it doesn't matter what # these elements contain. The size of the system is taken from the # size of the vector v. If the matrix A is larger than NxN then the # extras are ignored. If it is smaller, the program will produce an # error. # # The function is compatible with version 2 and version 3 of Python. # # Written by Mark Newman , September 4, 2011 # You may use, share, or modify this file freely # ###################################################################### from numpy import copy def banded(Aa,va,up,down): # Copy the inputs and determine the size of the system A = copy(Aa) v = copy(va) N = len(v) # Gaussian elimination for m in range(N): # Normalization factor div = A[up,m] # Update the vector first v[m] /= div for k in range(1,down+1): if m+k