| 101 | } |
| 102 | |
| 103 | void luDecomp(const matrix aMat, matrix &pMat, matrix &lMat, matrix &uMat, |
| 104 | const ring R) |
| 105 | { |
| 106 | int rr = aMat->rows(); |
| 107 | int cc = aMat->cols(); |
| 108 | pMat = mpNew(rr, rr); |
| 109 | uMat = mp_Copy(aMat,R); /* copy aMat into uMat: */ |
| 110 | |
| 111 | /* we use an int array to store all row permutations; |
| 112 | note that we only make use of the entries [1..rr] */ |
| 113 | int* permut = new int[rr + 1]; |
| 114 | for (int i = 1; i <= rr; i++) permut[i] = i; |
| 115 | |
| 116 | /* fill lMat with the (rr x rr) unit matrix */ |
| 117 | unitMatrix(rr, lMat,R); |
| 118 | |
| 119 | int bestR; int bestC; int intSwap; poly pSwap; int cOffset = 0; |
| 120 | for (int r = 1; r < rr; r++) |
| 121 | { |
| 122 | if (r > cc) break; |
| 123 | while ((r + cOffset <= cc) && |
| 124 | (!pivot(uMat, r, rr, r + cOffset, r + cOffset, &bestR, &bestC, R))) |
| 125 | cOffset++; |
| 126 | if (r + cOffset <= cc) |
| 127 | { |
| 128 | /* swap rows with indices r and bestR in permut */ |
| 129 | intSwap = permut[r]; |
| 130 | permut[r] = permut[bestR]; |
| 131 | permut[bestR] = intSwap; |
| 132 | |
| 133 | /* swap rows with indices r and bestR in uMat; |
| 134 | it is sufficient to do this for columns >= r + cOffset*/ |
| 135 | for (int c = r + cOffset; c <= cc; c++) |
| 136 | { |
| 137 | pSwap = MATELEM(uMat, r, c); |
| 138 | MATELEM(uMat, r, c) = MATELEM(uMat, bestR, c); |
| 139 | MATELEM(uMat, bestR, c) = pSwap; |
| 140 | } |
| 141 | |
| 142 | /* swap rows with indices r and bestR in lMat; |
| 143 | we must do this only for columns < r */ |
| 144 | for (int c = 1; c < r; c++) |
| 145 | { |
| 146 | pSwap = MATELEM(lMat, r, c); |
| 147 | MATELEM(lMat, r, c) = MATELEM(lMat, bestR, c); |
| 148 | MATELEM(lMat, bestR, c) = pSwap; |
| 149 | } |
| 150 | |
| 151 | /* perform next Gauss elimination step, i.e., below the |
| 152 | row with index r; |
| 153 | we need to adjust lMat and uMat; |
| 154 | we are certain that the matrix entry at [r, r + cOffset] |
| 155 | is non-zero: */ |
| 156 | number pivotElement = pGetCoeff(MATELEM(uMat, r, r + cOffset)); |
| 157 | poly p; |
| 158 | for (int rGauss = r + 1; rGauss <= rr; rGauss++) |
| 159 | { |
| 160 | p = MATELEM(uMat, rGauss, r + cOffset); |
no test coverage detected