aboutsummaryrefslogtreecommitdiff
path: root/gcc/lambda-trans.c
blob: 2539ba6600fcc2174fb3affb72c405342f2fe151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "errors.h"
#include "ggc.h"
#include "tree.h"
#include "target.h"
#include "varray.h"
#include "lambda.h"

/* Allocate a new transformation matrix.  */

lambda_trans_matrix
lambda_trans_matrix_new (int colsize, int rowsize)
{
  lambda_trans_matrix ret;
  
  ret = xcalloc (1, sizeof (*ret));
  LTM_MATRIX (ret) = lambda_matrix_new (rowsize, colsize);
  LTM_ROWSIZE (ret) = rowsize;
  LTM_COLSIZE (ret) = colsize;
  LTM_DENOMINATOR (ret) = 1;
  return ret;
}

/* Return true if the transformation matrix is nonsingular.  */

bool
lambda_trans_matrix_is_nonsingular (lambda_trans_matrix t)
{
  return lambda_trans_matrix_is_fullrank (t);
}


/* Return true if the transformation matrix is full row rank.  */

bool
lambda_trans_matrix_is_fullrank (lambda_trans_matrix t)
{
  return (lambda_trans_matrix_rank (t) == LTM_ROWSIZE (t));
}

/* Compute the rank of the matrix.  */

int
lambda_trans_matrix_rank (lambda_trans_matrix t)
{
  lambda_matrix partial;
  int rowsize, colsize;
  int i, j, nextrow;
  
  lambda_matrix tempmatrix;
  lambda_vector row;
  int minimum_column, factor;
  
  partial = LTM_MATRIX (t);
  rowsize = LTM_ROWSIZE (t);
  colsize = LTM_COLSIZE (t);
  
  tempmatrix = lambda_matrix_new (rowsize, colsize);
  lambda_matrix_copy (partial, tempmatrix, rowsize, colsize);
  
  j = 0;
  while ((j < colsize) && (j < rowsize))
    {
      /* Considering the submatrix A[j:m, j:n], search for the first
	 row k >= k such that A[k, j:n] != 0 */
      
      nextrow = lambda_matrix_first_nz_vec (tempmatrix, rowsize, colsize, j);
      
      if (nextrow != j)
	return j;
      
      /* Delete rows j .. nextrow - 1 */

      lambda_matrix_delete_rows (tempmatrix, rowsize, j, nextrow);
      lambda_matrix_delete_rows (partial, rowsize, j, nextrow);
      
      rowsize = rowsize - nextrow + j;

      /* Nextrow becomes row j+1 in the matrix, but not necessary row
	 j + 1 in the array.  */
      
      /* Apply elementary column operations to make the diagonal
	 element non-zero and the others zero.  */
      
      row = tempmatrix[j];

      /* Make every element of tempmatrix[j, j:colsize] positive.  */

      for (i = j; i < colsize; i++)
	if (row[i] < 0)
	  lambda_matrix_col_negate (tempmatrix, rowsize, i);

      /* Stop only when the diagonal element is non-zero.  */
      while (lambda_vector_first_nz (row, colsize, j+1) < colsize)
	{
	  minimum_column = lambda_vector_min_nz (row, colsize, j);
	  lambda_matrix_col_exchange (tempmatrix, rowsize, j, minimum_column);
	  
	  for (i = j + 1; i < colsize; i++)
	    {
	      if (row[i])
		{
		  factor = row[i] / row[j];
		  /* Add (-1) * factor of column j to column i.  */
		  lambda_matrix_col_add (tempmatrix, rowsize, 
					 j, i, (-1) * factor);
		}
	    }
	}
      j++;
    }

  return rowsize;
}


/* Compute the base matrix.  */

lambda_trans_matrix
lambda_trans_matrix_base (lambda_trans_matrix mat)
{
  int rowsize, colsize;
  int i, j, nextrow;
  lambda_matrix partial, tempmatrix;
  lambda_vector row;
  int minimum_column, factor;
  
  lambda_trans_matrix base;
  
  rowsize = LTM_ROWSIZE (mat);
  colsize = LTM_COLSIZE (mat);
  base = lambda_trans_matrix_new (rowsize, colsize);
  partial = LTM_MATRIX (base);
  lambda_matrix_copy (LTM_MATRIX (mat), partial, rowsize, colsize);
  tempmatrix = lambda_matrix_new (rowsize, colsize);
  lambda_matrix_copy (partial, tempmatrix, rowsize, colsize);

  j = 0;
  
  while ((j < colsize) 
	 && (nextrow = lambda_matrix_first_nz_vec (tempmatrix, 
						   rowsize, 
						   colsize, j)) < rowsize)
    {
      /* Consider the submatrix A[j:m, j:n].
	 Search for the first row k >= j such that A[k, j:n] != 0.  */
      
      /* Delete rows j .. nextrow - 1.  */
      lambda_matrix_delete_rows (tempmatrix, rowsize, j, nextrow);
      lambda_matrix_delete_rows (partial, rowsize, j, nextrow);

      /* nextrow becomes row j+1 in the matrix, though not necessarily
	 row j+1 in the array.  */
      /* Apply elementary column oeprations to make the diagonal
	 element nonzero and the others zero.  */
      row = tempmatrix[j];

      /* Make every element of tempmatrix[j, j:colsize] positive.  */
      
      for (i = j; i < colsize; i++)
	if (row[i] < 0)
	  lambda_matrix_col_negate (tempmatrix, rowsize, i);
      
      /* Stop when only the diagonal element is nonzero.  */
      
      while (lambda_vector_first_nz (row, colsize, j+1) < colsize)
	{
	  minimum_column = lambda_vector_min_nz (row, colsize, j);
	  lambda_matrix_col_exchange (tempmatrix, rowsize, j, minimum_column);
	  
	  for (i = j + 1; i < colsize; i++)
	    {
	      if (row[i])
		{
		  factor = row[i] / row[j];
		  /* Add (-1) * factor of column j to column i.  */
		  lambda_matrix_col_add (tempmatrix, rowsize, 
					 j, i, (-1) * factor);
		}
	    }
	}
      j++;
    }
  /* Store the rank.  */
  LTM_ROWSIZE (base) = j;
  return base;
}

/* Pad the legal base matrix to an invertable matrix.  */

lambda_trans_matrix
lambda_trans_matrix_padding (lambda_trans_matrix matrix)
{
  int i, k;
  int currrow, minimum_column, factor;

  lambda_matrix tempmatrix, padmatrix;
  lambda_vector row;

  lambda_trans_matrix padded;
  lambda_matrix partial;
  int rowsize, colsize;
  
  rowsize = LTM_ROWSIZE (matrix);
  colsize = LTM_COLSIZE (matrix);
  
  padded = lambda_trans_matrix_new (rowsize, colsize);
  partial = LTM_MATRIX (padded);
  lambda_matrix_copy(LTM_MATRIX (matrix), partial, rowsize, colsize);

  /* full rank, no need for padding */
  if (rowsize==colsize)
    return(padded);

  tempmatrix = lambda_matrix_new (rowsize, colsize);
  lambda_matrix_copy (partial, tempmatrix, rowsize, colsize);

  padmatrix = lambda_matrix_new (colsize, colsize);
  lambda_matrix_id (padmatrix, colsize);

  for(currrow = 0; currrow < rowsize; currrow++)
    {
      /* consider the submatrix A[i:m, i:n].  */

      /* apply elementary column operations to make the diag element nonzero 
	 and others zero.  */

      /* only consider columns from currrow to colsize.  */
      
      row = tempmatrix[currrow];

      /* make every element of tempmatrix[currrow, currrow:colsize]
	 positive. */

      for(i = currrow; i < colsize; i++)
	if(row[i] < 0)
	  lambda_matrix_col_negate (tempmatrix, rowsize, i);

      /* stop when only the diagonal element is nonzero  */
      while (lambda_vector_first_nz (row, colsize, currrow + 1) < colsize)
	{
	  minimum_column = lambda_vector_min_nz (row, colsize, currrow);
	  
	  lambda_matrix_col_exchange (tempmatrix, rowsize, currrow, 
				      minimum_column);
	  lambda_matrix_row_exchange (padmatrix, currrow, minimum_column);

	  for (i = currrow + 1; i < colsize; i++)
	    {
	      if(row[i])
		{
		  factor = row[i] / row[currrow];
		  lambda_matrix_col_add (tempmatrix, rowsize, 
					 currrow, i, (-1) * factor);
		}
	    }
	}
    }


  for(k = rowsize; k < colsize; k++)
    partial[k] = padmatrix[k];

  return(padded);
}

/* Compute the inverse of the transformation.  */

lambda_trans_matrix 
lambda_trans_matrix_inverse (lambda_trans_matrix mat)
{
  lambda_trans_matrix inverse;
  int determinant;
  
  inverse = lambda_trans_matrix_new (LTM_ROWSIZE (mat), LTM_COLSIZE (mat));
  determinant = lambda_matrix_inverse (LTM_MATRIX (mat), LTM_MATRIX (inverse), 
				       LTM_ROWSIZE (mat));
  LTM_DENOMINATOR (inverse) = determinant;
  return inverse;
}


/* Print out a transformation matrix.  */

void
print_lambda_trans_matrix (FILE *outfile, lambda_trans_matrix mat)
{
  print_lambda_matrix (outfile, LTM_MATRIX (mat), LTM_ROWSIZE (mat), 
		       LTM_COLSIZE (mat));
}