PIPS
comp_matrice.c
Go to the documentation of this file.
1 /*
2 
3  $Id: comp_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 /* comp_matrice.c
28  *
29  * matrice inversion for floating point. 20 Oct. 92 L. Zhou
30  *
31  * void lu_decomposition(a, n, indx, pd)
32  * Given an NxN matrix A, with physical dimension NP,
33  * here we give NP the value MAX_CONTROLS_IN_UNSTRUCTURED ( that is 100 )
34  * this routine
35  * replaces it by the LU decomposition of a rowwise permutation of itself.
36  * A and N are input, A is output
37  * INDX is output vector which recoreds the row permutation effected by the
38  * partial pivoting;
39  * D is out put as +- 1 depending on whether the number of row interchanges
40  * was even or odd, respectively.
41  *
42  * void lu_back_substitution(a, n, indx, b)
43  *
44  */
45 
46 #include <stdio.h>
47 #include <math.h>
48 
49 #include "linear.h"
50 
51 #include "genC.h"
52 #include "ri.h"
53 #include "effects.h"
54 #include "complexity_ri.h"
55 #include "ri-util.h"
56 #include "effects-util.h"
57 #include "misc.h"
58 #include "matrice.h"
59 #include "complexity.h"
60 
61 
62 /* Added by AP, March 15th 93 */
63 #define A(i,j) a[n*i + j]
64 
65 void lu_decomposition(a, n, indx, pd)
66 int n;
67 
68 /* Modif by AP, March 15th 93
69 float a[n][n];
70 int indx[n];
71 */
72 float *a;
73 int *indx;
74 
75 int *pd;
76 {
77  float aamax;
78 
79 /* Added by AP, March 15th 93 */
81 /* If using gcc to compile, vv can be declared as vv[n] LZ 29/10/92
82  float vv[n];
83 */
84 
85  int i,j,k;
86  float sum,dum;
87  int imax = 0;
88  float tiny = 0.000001;
89 
90  *pd = 1;
91 
92  /* loop over rows to get the implicit scaling information */
93  for (i=0; i<n; i++ ) {
94  aamax = 0.0;
95  for ( j=0; j<n; j++) {
96  if ( fabs(A(i,j)) > aamax ) {
97  aamax = fabs(A(i,j));
98  }
99  }
100 
101  if ( aamax == 0.0 ) {
102  fprintf(stderr, "Singular matrix -- No non-zero element\n");
103  user_error("lu_decomposition", "Module does not terminate\n");
104  }
105  vv[i] = 1./aamax;
106  }
107 
108  /* loopover columns of Crout's method */
109  for ( j=0; j<n; j++ ) {
110  for ( i=0; i<=j-1; i++ ) {
111  sum = A(i,j);
112  for ( k=0; k<i; k++) {
113  sum = sum - A(i,k)*A(k,j);
114  }
115  A(i,j) = sum;
116  }
117 
118  aamax = 0.0;
119 
120  for ( i=j; (i<n) ; i++ ) {
121  sum = A(i,j);
122  for ( k=0; k<j; k++ ) {
123  sum = sum - A(i,k)*A(k,j);
124  }
125  A(i,j) = sum;
126  dum = vv[i]*fabs(sum);
127  if ( dum >= aamax ) {
128  imax = i;
129  aamax = dum;
130  }
131  }
132 
133  if ( j != imax ) {
134  for ( k=0; k<n; k++ ) {
135  dum = A(imax,k);
136  A(imax,k) = A(j,k);
137  A(j,k) = dum;
138  }
139  *pd = - *pd;
140  vv[imax] = vv[j];
141  }
142 
143  indx[j] = imax;
144  if ( A(j,j) == 0.0 )
145  A(j,j) = tiny;
146  if ( j != n-1 ) {
147  dum = 1./A(j,j);
148  for ( i=j+1; i<n; i++ ) {
149  A(i,j) = A(i,j)*dum;
150  }
151  }
152  }
153 }
154 
155 void lu_back_substitution(a, n, indx, b)
156 int n;
157 float *a;
158 int *indx;
159 float *b;
160 {
161  int i,j;
162  int ii = -1;
163  float sum;
164  int ll;
165 
166  for (i=0; i<n; i++ ) {
167  ll = indx[i];
168  sum = b[ll];
169  b[ll] = b[i];
170  if ( ii != -1 ) {
171  for ( j=ii; j<=i-1; j++ ) {
172  sum = sum -A(i,j)*b[j];
173  }
174  }
175  else if ( sum != 0.0 ) {
176  ii = i;
177  }
178  b[i] = sum;
179  }
180 
181  /* back substitution */
182  for ( i=n-1; i>=0; i-- ) {
183  sum = b[i];
184  if ( i < n ) {
185  for ( j=i+1; j < n; j++ ) {
186  sum = sum - A(i,j)*b[j];
187  }
188  }
189  b[i] = sum/A(i,i);
190  }
191 }
192 
193 /* Added by AP, March 23rd 93: allows the simulation of a two-dimensional array
194  * with a mono-dimensional memory allocation.
195  */
196 #define Y(i,j) y[n*i + j]
197 
198 void float_matrice_inversion(a, n, indx, pd)
199 int n;
200 float *a;
201 int *indx;
202 int *pd;
203 {
204  int i,j;
205  float *y = (float *) malloc(sizeof(float) * n * n);
206 
207  /* set up identity matrix */
208  for (i=0; i<n; i++ ) {
209  for ( j=0; j<n; j++ )
210  Y(i,j) = 0.0;
211  Y(i,i) = 1.0;
212  }
213 
214  lu_decomposition(a, n, indx, pd);
215 
216  for ( j=0; j<n; j++ ) {
217  lu_back_substitution(a, n, indx, &(Y(j,0)) );
218  }
219  for ( j=0; j<n; j++ )
220  for ( i=0; i<n; i++ )
221  A(i,j) = Y(j,i);
222 }
#define Y(i, j)
Added by AP, March 23rd 93: allows the simulation of a two-dimensional array with a mono-dimensional ...
Definition: comp_matrice.c:196
void lu_decomposition(float *a, int n, int *indx, int *pd)
comp_matrice.c
Definition: comp_matrice.c:65
void lu_back_substitution(float *a, int n, int *indx, float *b)
Definition: comp_matrice.c:155
#define A(i, j)
comp_matrice.c
Definition: comp_matrice.c:63
void float_matrice_inversion(float *a, int n, int *indx, int *pd)
Definition: comp_matrice.c:198
#define MAX_CONTROLS_IN_UNSTRUCTURED
void * malloc(YYSIZE_T)
#define user_error(fn,...)
Definition: misc-local.h:265
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
t_real sum(int n1, int n2, int n3, t_real u[n1][n2][n3])
Definition: stencil.c:57