File indexing completed on 2025-08-05 08:18:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include "BorderedBandMatrix.h"
0031
0032
0033 namespace gbl {
0034
0035
0036 BorderedBandMatrix::BorderedBandMatrix() : numSize(0), numBorder(0), numBand(0), numCol(0) {
0037 }
0038
0039 BorderedBandMatrix::~BorderedBandMatrix() {
0040 }
0041
0042
0043
0044
0045
0046
0047
0048 void BorderedBandMatrix::resize(unsigned int nSize, unsigned int nBorder,
0049 unsigned int nBand) {
0050 numSize = nSize;
0051 numBorder = nBorder;
0052 numCol = nSize - nBorder;
0053 numBand = 0;
0054 theBorder.resize(numBorder);
0055 theMixed.resize(numBorder, numCol);
0056 theBand.resize((nBand + 1), numCol);
0057 }
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 void BorderedBandMatrix::addBlockMatrix(double aWeight,
0069 const std::vector<unsigned int>* anIndex,
0070 const std::vector<double>* aVector) {
0071 int nBorder = numBorder;
0072 for (unsigned int i = 0; i < anIndex->size(); ++i) {
0073 int iIndex = (*anIndex)[i] - 1;
0074 for (unsigned int j = 0; j <= i; ++j) {
0075 int jIndex = (*anIndex)[j] - 1;
0076 if (iIndex < nBorder) {
0077 theBorder(iIndex, jIndex) += (*aVector)[i] * aWeight
0078 * (*aVector)[j];
0079 } else if (jIndex < nBorder) {
0080 theMixed(jIndex, iIndex - nBorder) += (*aVector)[i] * aWeight
0081 * (*aVector)[j];
0082 } else {
0083 unsigned int nBand = iIndex - jIndex;
0084 theBand(nBand, jIndex - nBorder) += (*aVector)[i] * aWeight
0085 * (*aVector)[j];
0086 numBand = std::max(numBand, nBand);
0087 }
0088 }
0089 }
0090 }
0091
0092
0093
0094
0095
0096
0097 TMatrixDSym BorderedBandMatrix::getBlockMatrix(
0098 const std::vector<unsigned int> &anIndex) const {
0099
0100 TMatrixDSym aMatrix(anIndex.size());
0101 int nBorder = numBorder;
0102 for (unsigned int i = 0; i < anIndex.size(); ++i) {
0103 int iIndex = anIndex[i] - 1;
0104 for (unsigned int j = 0; j <= i; ++j) {
0105 int jIndex = anIndex[j] - 1;
0106 if (iIndex < nBorder) {
0107 aMatrix(i, j) = theBorder(iIndex, jIndex);
0108 } else if (jIndex < nBorder) {
0109 aMatrix(i, j) = -theMixed(jIndex, iIndex - nBorder);
0110 } else {
0111 unsigned int nBand = iIndex - jIndex;
0112 aMatrix(i, j) = theBand(nBand, jIndex - nBorder);
0113 }
0114 aMatrix(j, i) = aMatrix(i, j);
0115 }
0116 }
0117 return aMatrix;
0118 }
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 void BorderedBandMatrix::solveAndInvertBorderedBand(
0148 const VVector &aRightHandSide, VVector &aSolution) {
0149
0150
0151 decomposeBand();
0152
0153 VMatrix inverseBand = invertBand();
0154 if (numBorder > 0) {
0155
0156 const VMatrix auxMat = solveBand(theMixed);
0157 const VMatrix auxMatT = auxMat.transpose();
0158
0159 const VVector auxVec = aRightHandSide.getVec(numBorder)
0160 - auxMat * aRightHandSide.getVec(numCol, numBorder);
0161 VSymMatrix inverseBorder = theBorder - theMixed * auxMatT;
0162 inverseBorder.invert();
0163 const VVector borderSolution = inverseBorder * auxVec;
0164
0165 const VVector bandSolution = solveBand(
0166 aRightHandSide.getVec(numCol, numBorder));
0167 aSolution.putVec(borderSolution);
0168 aSolution.putVec(bandSolution - auxMatT * borderSolution, numBorder);
0169
0170 theBorder = inverseBorder;
0171 theMixed = inverseBorder * auxMat;
0172 theBand = inverseBand + bandOfAVAT(auxMatT, inverseBorder);
0173 } else {
0174 aSolution.putVec(solveBand(aRightHandSide));
0175 theBand = inverseBand;
0176 }
0177 }
0178
0179
0180 void BorderedBandMatrix::printMatrix() const {
0181 std::cout << "Border part " << std::endl;
0182 theBorder.print();
0183 std::cout << "Mixed part " << std::endl;
0184 theMixed.print();
0185 std::cout << "Band part " << std::endl;
0186 theBand.print();
0187 }
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 void BorderedBandMatrix::decomposeBand() {
0200
0201 int nRow = numBand + 1;
0202 int nCol = numCol;
0203 VVector auxVec(nCol);
0204 for (int i = 0; i < nCol; ++i) {
0205 auxVec(i) = theBand(0, i) * 16.0;
0206 }
0207 for (int i = 0; i < nCol; ++i) {
0208 if ((theBand(0, i) + auxVec(i)) != theBand(0, i)) {
0209 theBand(0, i) = 1.0 / theBand(0, i);
0210 if (theBand(0, i) < 0.) {
0211 throw 3;
0212 }
0213 } else {
0214 theBand(0, i) = 0.0;
0215 throw 2;
0216 }
0217 for (int j = 1; j < std::min(nRow, nCol - i); ++j) {
0218 double rxw = theBand(j, i) * theBand(0, i);
0219 for (int k = 0; k < std::min(nRow, nCol - i) - j; ++k) {
0220 theBand(k, i + j) -= theBand(k + j, i) * rxw;
0221 }
0222 theBand(j, i) = rxw;
0223 }
0224 }
0225 }
0226
0227
0228
0229
0230
0231
0232
0233
0234 VVector BorderedBandMatrix::solveBand(const VVector &aRightHandSide) const {
0235
0236 int nRow = theBand.getNumRows();
0237 int nCol = theBand.getNumCols();
0238 VVector aSolution(aRightHandSide);
0239 for (int i = 0; i < nCol; ++i)
0240 {
0241 for (int j = 1; j < std::min(nRow, nCol - i); ++j) {
0242 aSolution(j + i) -= theBand(j, i) * aSolution(i);
0243 }
0244 }
0245 for (int i = nCol - 1; i >= 0; i--)
0246 {
0247 double rxw = theBand(0, i) * aSolution(i);
0248 for (int j = 1; j < std::min(nRow, nCol - i); ++j) {
0249 rxw -= theBand(j, i) * aSolution(j + i);
0250 }
0251 aSolution(i) = rxw;
0252 }
0253 return aSolution;
0254 }
0255
0256
0257
0258
0259
0260
0261
0262
0263 VMatrix BorderedBandMatrix::solveBand(const VMatrix &aRightHandSide) const {
0264
0265 int nRow = theBand.getNumRows();
0266 int nCol = theBand.getNumCols();
0267 VMatrix aSolution(aRightHandSide);
0268 for (unsigned int iBorder = 0; iBorder < numBorder; iBorder++) {
0269 for (int i = 0; i < nCol; ++i)
0270 {
0271 for (int j = 1; j < std::min(nRow, nCol - i); ++j) {
0272 aSolution(iBorder, j + i) -= theBand(j, i)
0273 * aSolution(iBorder, i);
0274 }
0275 }
0276 for (int i = nCol - 1; i >= 0; i--)
0277 {
0278 double rxw = theBand(0, i) * aSolution(iBorder, i);
0279 for (int j = 1; j < std::min(nRow, nCol - i); ++j) {
0280 rxw -= theBand(j, i) * aSolution(iBorder, j + i);
0281 }
0282 aSolution(iBorder, i) = rxw;
0283 }
0284 }
0285 return aSolution;
0286 }
0287
0288
0289
0290
0291
0292 VMatrix BorderedBandMatrix::invertBand() {
0293
0294 int nRow = numBand + 1;
0295 int nCol = numCol;
0296 VMatrix inverseBand(nRow, nCol);
0297
0298 for (int i = nCol - 1; i >= 0; i--) {
0299 double rxw = theBand(0, i);
0300 for (int j = i; j >= std::max(0, i - nRow + 1); j--) {
0301 for (int k = j + 1; k < std::min(nCol, j + nRow); ++k) {
0302 rxw -= inverseBand(abs(i - k), std::min(i, k))
0303 * theBand(k - j, j);
0304 }
0305 inverseBand(i - j, j) = rxw;
0306 rxw = 0.;
0307 }
0308 }
0309 return inverseBand;
0310 }
0311
0312
0313
0314
0315
0316 VMatrix BorderedBandMatrix::bandOfAVAT(const VMatrix &anArray,
0317 const VSymMatrix &aSymArray) const {
0318 int nBand = numBand;
0319 int nCol = numCol;
0320 int nBorder = numBorder;
0321 double sum;
0322 VMatrix aBand((nBand + 1), nCol);
0323 for (int i = 0; i < nCol; ++i) {
0324 for (int j = std::max(0, i - nBand); j <= i; ++j) {
0325 sum = 0.;
0326 for (int l = 0; l < nBorder; ++l) {
0327 sum += anArray(i, l) * aSymArray(l, l) * anArray(j, l);
0328 for (int k = 0; k < l; ++k) {
0329 sum += anArray(i, l) * aSymArray(l, k) * anArray(j, k)
0330 + anArray(i, k) * aSymArray(l, k) * anArray(j, l);
0331 }
0332 }
0333 aBand(i - j, j) = sum;
0334 }
0335 }
0336 return aBand;
0337 }
0338
0339 }