2Algebra for linear equation system with bordered band matrix.
10# Bordered band matrix.
12# \author Claus Kleinwort, DESY, 2011 (Claus.Kleinwort@desy.de)
74 nSizeBand = nSize - nBorder
86 self.
__mixed = np.zeros((nBorder, nSizeBand))
88 self.
__band = np.zeros((nBand + 1, nSizeBand))
100 for i
in range(len(aIndex)):
101 iIndex = aIndex[i] - 1
102 for j
in range(i + 1):
103 jIndex = aIndex[j] - 1
104 if (iIndex < nBorder):
105 self.
__border[iIndex, jIndex] += aMatrix[i, j]
106 if (iIndex != jIndex):
107 self.
__border[jIndex, iIndex] += aMatrix[i, j]
108 elif (jIndex < nBorder):
109 self.
__mixed[jIndex, iIndex - nBorder] += aMatrix[i, j]
111 nBand = iIndex - jIndex
112 self.
__band[nBand, jIndex - nBorder] += aMatrix[i, j]
128 aMatrix = np.empty((nSize, nSize))
129 for i
in range(nSize):
130 iIndex = aIndex[i] - 1
131 for j
in range(i + 1):
132 jIndex = aIndex[j] - 1
133 iMax = max(iIndex, jIndex)
134 iMin = min(iIndex, jIndex)
136 aMatrix[i, j] = self.
__border[iIndex, jIndex]
137 elif (iMin < nBorder):
138 aMatrix[i, j] = self.
__mixed[iMin, iMax - nBorder]
142 aMatrix[i, j] = self.
__band[iBand, iMin - nBorder]
145 aMatrix[j, i] = aMatrix[i, j]
153 diagMax = np.max(self.
__band[0])
154 diagMin = np.min(self.
__band[0])
155 return diagMax/diagMin
if diagMin > 0.
else 0.
162 for i
in range(nRow):
165 for i
in range(nRow):
166 print " row ", i, self.
__mixed[i]
169 for i
in range(nRow):
170 print " diagonal ", i, self.
__band[i]
189 nRow = self.__numBand + 1
191 auxVec = np.copy(self.__band[0]) * 16.0
192 for i
in range(nCol):
193 if ((self.__band[0, i] + auxVec[i]) != self.__band[0, i]):
194 self.__band[0, i] = 1.0 / self.__band[0, i]
196 self.__band[0, i] = 0.0
197 for j
in range(min(nRow, nCol - i) - 1):
198 rxw = self.__band[j + 1, i] * self.__band[0, i]
199 for k
in range(min(nRow, nCol - i) - j - 1):
200 self.__band[k, i + j + 1] -= self.__band[k + j + 1, i] * rxw
201 self.__band[j + 1, i] = rxw
209 nRow = self.__numBand + 1
211 aSolution = np.copy(aRightHandSide)
212 for i
in range(nCol):
213 for j
in range(min(nRow, nCol - i) - 1):
214 aSolution[j + i + 1] -= self.__band[j + 1, i] * aSolution[i]
215 for i
in range(nCol - 1, -1, -1):
216 rxw = self.__band[0, i] * aSolution[i]
217 for j
in range(min(nRow, nCol - i) - 1):
218 rxw -= self.__band[j + 1, i] * aSolution[j + i + 1]
227 nRow = self.__numBand + 1
229 inverseBand = np.zeros((nRow, nCol))
230 for i
in range(nCol - 1, -1, -1):
231 rxw = self.__band[0, i]
232 for j
in range(i, max(0, i - nRow + 1) - 1, -1):
233 for k
in range(j + 1, min(nCol, j + nRow)):
234 rxw -= inverseBand[abs(i - k), min(i, k)] * self.__band[k - j, j]
235 inverseBand[i - j, j] = rxw
247 nBand = self.__numBand
248 aBand = np.empty((nBand + 1, nCol))
249 for i
in range(nCol):
250 for j
in range(max(0, i - nBand), i + 1):
251 aBand[i - j, j] = np.dot(anArray[i], np.dot(aSymArray, anArray[j]))
255 nBorder = self.__numBorder
258 aSolution = np.empty(nBorder + nCol)
261 if ((self.__band[0] <= 0.0).any()):
262 raise ZeroDivisionError(
"Band matrix not positive definite")
265 auxMat = np.empty((nBorder, nCol))
267 for i
in range(nBorder):
271 auxVec = aRightHandSide[:nBorder] - np.dot(auxMat, aRightHandSide[nBorder:])
272 invBorder = np.linalg.inv(self.__border - np.dot(self.__mixed, auxMatT))
273 aSolution[:nBorder] = np.dot(invBorder, auxVec)
275 aSolution[nBorder:] =
solveBand(aRightHandSide[nBorder:]) \
276 -np.dot(auxMatT, aSolution[:nBorder])
278 self.__border = invBorder
279 self.__mixed = np.dot(-invBorder, auxMat)
(Symmetric) Bordered Band Matrix.
def solveAndInvertBorderedBand(self, aRightHandSide)
Solve linear equation A*x=b system with BBmatrix A, calculate BB part of inverse of A.
__numCol
size of band part of matrix; int
__border
border part B; matrix(float)
def addBlockMatrix(self, aIndex, aMatrix)
Add (compressed) block to BBmatrix:
__numBorder
size of border; int
__numBand
size of border; int
def __init__(self, nSize, nBorder=1, nBand=7)
Create new BBmatrix.
def getBandCond(self)
Get condition from band part decomposition.
__band
band part C; matrix(float)
__numSize
size of matrix; int
def printMatrix(self)
Print BBmatrix.
__mixed
mixed part M; matrix(float)
def getBlockMatrix(self, aIndex)
Retrieve (compressed) block from BBmatrix:
def invertBand()
Invert band part.
def bandOfAVAT(anArray, aSymArray)
Calculate band part of A*V*A^T.
def solveBand(aRightHandSide)
Solve linear equation system for band part.