Fast Auxiliary Space Preconditioning 2.7.7 Aug/28/2022
Loading...
Searching...
No Matches
BlaSmallMatLU.c
Go to the documentation of this file.
1
13#include <math.h>
14
15#include "fasp.h"
16
17/*---------------------------------*/
18/*-- Public Functions --*/
19/*---------------------------------*/
20
53 INT pivot[],
54 const INT n)
55{
56 INT i, j, k;
57 REAL *p_k=NULL, *p_row=NULL, *p_col=NULL;
58 REAL max;
59
60 /* For each row and column, k = 0, ..., n-1, */
61 for (k = 0, p_k = A; k < n; p_k += n, k++) {
62
63 // find the pivot row
64 pivot[k] = k;
65 max = fabs( *(p_k + k) );
66 for (j = k + 1, p_row = p_k + n; j < n; ++j, p_row += n) {
67 if ( max < fabs(*(p_row + k)) ) {
68 max = fabs(*(p_row + k));
69 pivot[k] = j;
70 p_col = p_row;
71 }
72 }
73
74 // if the pivot row differs from the current row, interchange the two rows.
75 if (pivot[k] != k)
76 for (j = 0; j < n; ++j) {
77 max = *(p_k + j);
78 *(p_k + j) = *(p_col + j);
79 *(p_col + j) = max;
80 }
81
82 // if the matrix is singular, return error
83 if ( fabs( *(p_k + k) ) < SMALLREAL ) return -1;
84
85 // otherwise find the lower triangular matrix elements for column k.
86 for (i = k+1, p_row = p_k + n; i < n; p_row += n, ++i) {
87 *(p_row + k) /= *(p_k + k);
88 }
89
90 // update remaining matrix
91 for (i = k+1, p_row = p_k + n; i < n; p_row += n, ++i)
92 for (j = k+1; j < n; ++j)
93 *(p_row + j) -= *(p_row + k) * *(p_k + j);
94
95 }
96
97 return FASP_SUCCESS;
98}
99
125 REAL b[],
126 const INT pivot[],
127 REAL x[],
128 const INT n)
129{
130 INT i, k;
131 REAL dum;
132 const REAL *p_k;
133
134 /* solve Ly = b */
135 for (k = 0, p_k = A; k < n; p_k += n, k++) {
136 if (pivot[k] != k) {dum = b[k]; b[k] = b[pivot[k]]; b[pivot[k]] = dum; }
137 x[k] = b[k];
138 for (i = 0; i < k; ++i) x[k] -= x[i] * *(p_k + i);
139 }
140
141 /* solve Ux = y */
142 for (k = n-1, p_k = A + n*(n-1); k >= 0; k--, p_k -= n) {
143 if (pivot[k] != k) {dum = b[k]; b[k] = b[pivot[k]]; b[pivot[k]] = dum; }
144 for (i = k + 1; i < n; ++i) x[k] -= x[i] * *(p_k + i);
145 if (*(p_k + k) == 0.0) return -1;
146 x[k] /= *(p_k + k);
147 }
148
149 return FASP_SUCCESS;
150}
151
152/*---------------------------------*/
153/*-- End of File --*/
154/*---------------------------------*/
155
156/*
157
158 //A simple test example can be written as the following
159 INT main (INT argc, const char * argv[])
160 {
161 REAL A[3][3] = {{0.0, 1.0, 4.0},
162 {4.0, 1.0, 0.0},
163 {1.0, 4.0, 1.0}};
164
165 REAL b[3] = {1, 1, 1}, x[3];
166
167 INT pivot[3];
168
169 INT ret, i, j;
170
171 ret = lu_decomp(&A[0][0], pivot, 3); // LU decomposition
172
173 ret = lu_solve(&A[0][0], b, pivot, x, 3); // Solve decomposed Ax=b
174
175 return 1;
176 }
177
178*/
SHORT fasp_smat_lu_decomp(REAL *A, INT pivot[], const INT n)
LU decomposition of A using Doolittle's method.
Definition: BlaSmallMatLU.c:52
SHORT fasp_smat_lu_solve(const REAL *A, REAL b[], const INT pivot[], REAL x[], const INT n)
Solving Ax=b using LU decomposition.
Main header file for the FASP project.
#define REAL
Definition: fasp.h:75
#define SHORT
FASP integer and floating point numbers.
Definition: fasp.h:71
#define INT
Definition: fasp.h:72
#define FASP_SUCCESS
Definition of return status and error messages.
Definition: fasp_const.h:19
#define SMALLREAL
Definition: fasp_const.h:256