PIPS
test_matrice.c
Go to the documentation of this file.
1 /*
2 
3  $Id: test_matrice.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 #include <stdio.h>
28 
29 #include <math.h>
30 
31 #define MAX_CONTROLS_IN_UNSTRUCTURED 100
32 
33 void lu_decomposition(a, n, indx, pd)
34 int n;
36 int indx[];
37 int *pd;
38 {
39  float aamax;
41  int i,j,k;
42  float sum,dum;
43  int imax;
44  float tiny = 0.00;
45 
46  *pd = 1;
47 
48  /* loop over rows to get the implicit scaling information */
49  for (i=0; i<n; i++ ) {
50  aamax = 0.0;
51  for ( j=0; j<n; j++) {
52  if ( fabs(a[i][j]) > aamax ) {
53  aamax = fabs(a[i][j]);
54  }
55  }
56 
57  if ( aamax == 0.0 )
58  fprintf(stderr, "Singular matrix -- No nonzero element\n");
59  vv[i] = 1./aamax;
60  }
61 
62  /* loopover columns of Crout's method */
63  for ( j=0; j<n; j++ ) {
64  for ( i=0; i<=j; i++ ) {
65  sum = a[i][j];
66  for ( k=0; k<i; k++) {
67  sum = sum - a[i][k]*a[k][j];
68  }
69  a[i][j] = sum;
70  }
71 
72  aamax = 0.0;
73 
74  for ( i=j+1; (i<n) ; i++ ) {
75  sum = a[i][j];
76  for ( k=0; k<j; k++ ) {
77  sum = sum - a[i][k]*a[k][j];
78  }
79  a[i][j] = sum;
80  dum = vv[i]*fabs(sum);
81  if ( dum >= aamax ) {
82  imax = i;
83  aamax = dum;
84  }
85  }
86 
87  if ( j != imax ) {
88  for ( k=0; k<n; k++ ) {
89  dum = a[imax][k];
90  a[imax][k] = a[j][k];
91  a[j][k] = dum;
92  }
93  *pd = - *pd;
94  vv[imax] = vv[j];
95  }
96 
97  indx[j] = imax;
98  if ( a[j][j] == 0.0 )
99  a[j][j] = tiny;
100  if ( j != n-1 ) {
101  dum = 1./a[j][j];
102  for ( i=j+1; i<n; i++ ) {
103  a[i][j] = a[i][j]*dum;
104  }
105  }
106  }
107 }
108 
109 void lu_back_substitution(a, n, indx, b)
111 int n;
112 int indx[];
113 float b[];
114 {
115  int i,j;
116  int ii = -1;
117  float sum;
118  int ll;
119 
120  for (i=0; i<n; i++ ) {
121  ll = indx[i];
122  sum = b[ll];
123  b[ll] = b[i];
124  if ( ii != -1 ) {
125  for ( j=ii; j<=i-1; j++ ) {
126  sum = sum -a[i][j]*b[j];
127  }
128  }
129  else if ( sum != 0.0 ) {
130  ii = i;
131  }
132  b[i] = sum;
133  }
134 
135  /* back substitution */
136  for ( i=n-1; i>=0; i-- ) {
137  sum = b[i];
138  if ( i < n ) {
139  for ( j=i+1; j < n; j++ ) {
140  sum = sum - a[i][j]*b[j];
141  }
142  }
143  b[i] = sum/a[i][i];
144  }
145 }
146 
147 void float_matrice_inversion(a, n, indx, pd)
148 int n;
150 int indx[];
151 int *pd;
152 {
153  int i,j;
155 
156  /* set up identity matrix */
157  for (i=0; i<n; i++ ) {
158  for ( j=0; j<n; j++ )
159  y[i][j] = 0.0;
160  y[i][i] = 1.0;
161  }
162 
163  lu_decomposition(a, n, indx, pd);
164 
165  for ( j=0; j<n; j++ ) {
166  lu_back_substitution(a, n, indx, y[j]);
167  }
168  for ( j=0; j<n; j++ )
169  for ( i=0; i<n; i++ )
170  a[i][j] = y[j][i];
171 }
172 
174 {
176  int n=40;
178  int d;
179  int i,j;
180 
181  for (i=0; i<n; i++) {
182  a[i][i] = 2.0;
183  a[i][i+1] = -2.0;
184  }
185 
186  a[2][3] = a[2][37] = -1.;
187  a[6][7] = a[6][33] = -1.;
188  a[10][11] = a[10][29] = -1.;
189  a[15][16] = a[15][17] = -1.;
190  a[16][14] = -2.;
191  a[16][17] = 0.0;
192  a[17][18] = a[17][19] = -1.0;
193  a[18][14] = -2.0;
194  a[18][21] = 0.0;
195  a[21][22] = a[21][23] = -1.0;
196  a[22][12] = -2.;
197  a[22][23] = 0.0;
198  a[23][24] = a[23][28] = -1.;
199 
200  a[27][28] = 0.0;
201 
202  a[28][12] = -2.0;
203  a[28][29] = 0.0;
204 
205  a[29][30] = a[30][31] = -1.0;
206  a[30][12] = -2.0;
207  a[30][31] = 0.0;
208 
209  a[32][12] = -2.0;
210  a[32][33] = 0.0;
211 
212  a[33][34] = a[33][35] = -1.0;
213 
214  a[34][8] = -2.0;
215  a[34][35] = 0.0;
216 
217  a[36][8] = -2.0;
218  a[36][37] = 0.0;
219 
220  a[37][38] = a[37][39] = -1.0;
221 
222  a[38][5] = -2.0;
223  a[38][39] = 0.0;
224 
225  a[39][5] = -2.0;
226 
227  for ( i=0; i<n; i++ ) {
228  for(j=0; j<n; j++ ) {
229  a[i][j] = a[i][j]/2.0;
230  printf("%-4.1f ",a[i][j]);
231  }
232  printf("\n");
233  }
234 
235  float_matrice_inversion(a, n, indx, &d);
236 
237  printf("Final results -----------------\n");
238  printf("indx[0] indx[1] are %d %d\n",indx[0], indx[1] );
239  printf("d is %d\n",d);
240 
241  printf("a[0][0] a[0][1] %f %f\n", (a[0][0]), (a[0][1]) );
242  printf("a[1][0] a[1][1] %f %f\n", (a[1][0]), (a[1][1]) );
243 }
244 
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
int printf()
t_real sum(int n1, int n2, int n3, t_real u[n1][n2][n3])
Definition: stencil.c:57
#define MAX_CONTROLS_IN_UNSTRUCTURED
Definition: test_matrice.c:31
main()
Definition: test_matrice.c:173
void lu_decomposition(a, int n, indx, int *pd)
Definition: test_matrice.c:33
void float_matrice_inversion(a, int n, indx, int *pd)
Definition: test_matrice.c:147
void lu_back_substitution(a, int n, indx, b)
Definition: test_matrice.c:109