99 using Real =
typename MatrixType::ElemType;
101 if (A.getNRow() != A.getNCol() || A.getNRow() != AI.getNRow() ||
102 A.getNCol() != AI.getNCol()) {
106 int size = A.getNRow();
110 int iScratch[MAX_SCRATCH_ARRAY_SIZE];
111 int* index = (size <= MAX_SCRATCH_ARRAY_SIZE ? iScratch :
new int[size]);
112 Real dScratch[MAX_SCRATCH_ARRAY_SIZE];
113 Real* column = (size <= MAX_SCRATCH_ARRAY_SIZE ? dScratch :
new Real[size]);
117 if (size > MAX_SCRATCH_ARRAY_SIZE) {
137 typename MatrixType::ElemPoinerType tmp2Size) {
138 if (A.getNRow() != A.getNCol() || A.getNRow() != AI.getNRow() ||
139 A.getNCol() != AI.getNCol() || A.getNRow() != size) {
154 for (j = 0; j < size; ++j) {
155 for (i = 0; i < size; ++i) {
162 for (i = 0; i < size; i++) {
163 AI(i, j) = tmp2Size[i];
182 typename MatrixType::ElemPoinerType tmpSize) {
183 using Real =
typename MatrixType::ElemType;
187 Real largest, temp1, temp2, sum;
192 for (i = 0; i < size; ++i) {
193 for (largest = 0.0, j = 0; j < size; ++j) {
194 if ((temp2 = std::abs(A(i, j))) > largest) { largest = temp2; }
197 if (largest == 0.0) {
198 std::cerr <<
"Unable to factor linear system";
201 tmpSize[i] = 1.0 / largest;
206 for (j = 0; j < size; ++j) {
207 for (i = 0; i < j; ++i) {
209 for (k = 0; k < i; ++k) {
210 sum -= A(i, k) * A(k, j);
217 for (largest = 0.0, i = j; i < size; ++i) {
219 for (k = 0; k < j; ++k) {
220 sum -= A(i, k) * A(k, j);
224 if ((temp1 = tmpSize[i] * std::abs(sum)) >= largest) {
233 for (k = 0; k < size; ++k) {
234 std::swap(A(maxI, k), A(j, k));
236 tmpSize[maxI] = tmpSize[j];
243 if (std::abs(A(j, j)) <= SMALL_NUMBER) {
244 std::cerr <<
"Unable to factor linear system";
248 if (j != (size - 1)) {
249 temp1 = 1.0 / A(j, j);
250 for (i = j + 1; i < size; ++i) {
272 typename MatrixType::ElemPoinerType x,
int size) {
273 using Real =
typename MatrixType::ElemType;
281 for (ii = -1, i = 0; i < size; ++i) {
287 for (j = ii; j <= (i - 1); ++j) {
288 sum -= A(i, j) * x[j];
290 }
else if (sum != 0.0) {
299 for (i = size - 1; i >= 0; i--) {
301 for (j = i + 1; j < size; ++j) {
302 sum -= A(i, j) * x[j];
304 x[i] = sum / A(i, i);
void LUSolveLinearSystem(MatrixType &A, int *index, typename MatrixType::ElemPoinerType x, int size)
Solve linear equations Ax = b using LU decompostion A = LU where L is lower triangular matrix and U i...
int LUFactorLinearSystem(MatrixType &A, int *index, int size, typename MatrixType::ElemPoinerType tmpSize)
Factor linear equations Ax = b using LU decompostion A = LU where L is lower triangular matrix and U ...