-
Notifications
You must be signed in to change notification settings - Fork 18
/
pxop.c
393 lines (343 loc) · 8.39 KB
/
pxop.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**************************************************************************
**
** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
**
** Meschach Library
**
** This Meschach Library is provided "as is" without any express
** or implied warranty of any kind with respect to this software.
** In particular the authors shall not be liable for any direct,
** indirect, special, incidental or consequential damages arising
** in any way from use of the software.
**
** Everyone is granted permission to copy, modify and redistribute this
** Meschach Library, provided:
** 1. All copies contain this copyright notice.
** 2. All modified copies shall carry a notice stating who
** made the last modification and the date of such modification.
** 3. No charge is made for this software or works derived from it.
** This clause shall not be construed as constraining other software
** distributed on the same medium as this software, nor is a
** distribution fee considered a charge.
**
***************************************************************************/
/* pxop.c 1.5 12/03/87 */
#include <stdio.h>
#include "matrix.h"
static char rcsid[] = "$Id: pxop.c,v 1.6 1995/06/08 14:57:11 des Exp $";
/**********************************************************************
Note: A permutation is often interpreted as a matrix
(i.e. a permutation matrix).
A permutation px represents a permutation matrix P where
P[i][j] == 1 if and only if px->pe[i] == j
**********************************************************************/
/* px_inv -- invert permutation -- in situ
-- taken from ACM Collected Algorithms #250 */
#ifndef ANSI_C
PERM *px_inv(px,out)
PERM *px, *out;
#else
PERM *px_inv(const PERM *px, PERM *out)
#endif
{
int i, j, k, n, *p;
out = px_copy(px, out);
n = out->size;
p = (int *)(out->pe);
for ( n--; n>=0; n-- )
{
i = p[n];
if ( i < 0 ) p[n] = -1 - i;
else if ( i != n )
{
k = n;
while (TRUE)
{
if ( i < 0 || i >= out->size )
error(E_BOUNDS,"px_inv");
j = p[i]; p[i] = -1 - k;
if ( j == n )
{ p[n] = i; break; }
k = i; i = j;
}
}
}
return out;
}
/* px_mlt -- permutation multiplication (composition) */
#ifndef ANSI_C
PERM *px_mlt(px1,px2,out)
PERM *px1,*px2,*out;
#else
PERM *px_mlt(const PERM *px1, const PERM *px2, PERM *out)
#endif
{
unsigned int i,size;
if ( px1==(PERM *)NULL || px2==(PERM *)NULL )
error(E_NULL,"px_mlt");
if ( px1->size != px2->size )
error(E_SIZES,"px_mlt");
if ( px1 == out || px2 == out )
error(E_INSITU,"px_mlt");
if ( out==(PERM *)NULL || out->size < px1->size )
out = px_resize(out,px1->size);
size = px1->size;
for ( i=0; i<size; i++ )
if ( px2->pe[i] >= size )
error(E_BOUNDS,"px_mlt");
else
out->pe[i] = px1->pe[px2->pe[i]];
return out;
}
/* px_vec -- permute vector */
#ifndef ANSI_C
VEC *px_vec(px,vector,out)
PERM *px;
VEC *vector,*out;
#else
VEC *px_vec(PERM *px, const VEC *vector, VEC *out)
#endif
{
unsigned int old_i, i, size, start;
Real tmp;
if ( px==PNULL || vector==VNULL )
error(E_NULL,"px_vec");
if ( px->size > vector->dim )
error(E_SIZES,"px_vec");
if ( out==VNULL || out->dim < vector->dim )
out = v_resize(out,vector->dim);
size = px->size;
if ( size == 0 )
return v_copy(vector,out);
if ( out != vector )
{
for ( i=0; i<size; i++ )
if ( px->pe[i] >= size )
error(E_BOUNDS,"px_vec");
else
out->ve[i] = vector->ve[px->pe[i]];
}
else
{ /* in situ algorithm */
start = 0;
while ( start < size )
{
old_i = start;
i = px->pe[old_i];
if ( i >= size )
{
start++;
continue;
}
tmp = vector->ve[start];
while ( TRUE )
{
vector->ve[old_i] = vector->ve[i];
px->pe[old_i] = i+size;
old_i = i;
i = px->pe[old_i];
if ( i >= size )
break;
if ( i == start )
{
vector->ve[old_i] = tmp;
px->pe[old_i] = i+size;
break;
}
}
start++;
}
for ( i = 0; i < size; i++ )
if ( px->pe[i] < size )
error(E_BOUNDS,"px_vec");
else
px->pe[i] = px->pe[i]-size;
}
return out;
}
/* pxinv_vec -- apply the inverse of px to x, returning the result in out */
#ifndef ANSI_C
VEC *pxinv_vec(px,x,out)
PERM *px;
VEC *x, *out;
#else
VEC *pxinv_vec(PERM *px, const VEC *x, VEC *out)
#endif
{
unsigned int i, size;
if ( ! px || ! x )
error(E_NULL,"pxinv_vec");
if ( px->size > x->dim )
error(E_SIZES,"pxinv_vec");
/* if ( x == out )
error(E_INSITU,"pxinv_vec"); */
if ( ! out || out->dim < x->dim )
out = v_resize(out,x->dim);
size = px->size;
if ( size == 0 )
return v_copy(x,out);
if ( out != x )
{
for ( i=0; i<size; i++ )
if ( px->pe[i] >= size )
error(E_BOUNDS,"pxinv_vec");
else
out->ve[px->pe[i]] = x->ve[i];
}
else
{ /* in situ algorithm --- cheat's way out */
px_inv(px,px);
px_vec(px,x,out);
px_inv(px,px);
}
return out;
}
/* px_transp -- transpose elements of permutation
-- Really multiplying a permutation by a transposition */
#ifndef ANSI_C
PERM *px_transp(px,i1,i2)
PERM *px; /* permutation to transpose */
unsigned int i1,i2; /* elements to transpose */
#else
PERM *px_transp(PERM *px, unsigned int i1, unsigned int i2)
#endif
{
unsigned int temp;
if ( px==(PERM *)NULL )
error(E_NULL,"px_transp");
if ( i1 < px->size && i2 < px->size )
{
temp = px->pe[i1];
px->pe[i1] = px->pe[i2];
px->pe[i2] = temp;
}
return px;
}
/* myqsort -- a cheap implementation of Quicksort on integers
-- returns number of swaps */
#ifndef ANSI_C
static int myqsort(a,num)
int *a, num;
#else
static int myqsort(int *a, int num)
#endif
{
int i, j, tmp, v;
int numswaps;
numswaps = 0;
if ( num <= 1 )
return 0;
i = 0; j = num; v = a[0];
for ( ; ; )
{
while ( a[++i] < v )
;
while ( a[--j] > v )
;
if ( i >= j ) break;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
numswaps++;
}
tmp = a[0];
a[0] = a[j];
a[j] = tmp;
if ( j != 0 )
numswaps++;
numswaps += myqsort(&a[0],j);
numswaps += myqsort(&a[j+1],num-(j+1));
return numswaps;
}
/* px_sign -- compute the ``sign'' of a permutation = +/-1 where
px is the product of an even/odd # transpositions */
#ifndef ANSI_C
int px_sign(px)
PERM *px;
#else
int px_sign(const PERM *px)
#endif
{
int numtransp;
PERM *px2;
if ( px==(PERM *)NULL )
error(E_NULL,"px_sign");
px2 = px_copy(px,PNULL);
numtransp = myqsort((int *)px2->pe,px2->size);
px_free(px2);
return ( numtransp % 2 ) ? -1 : 1;
}
/* px_cols -- permute columns of matrix A; out = A.px'
-- May NOT be in situ */
#ifndef ANSI_C
MAT *px_cols(px,A,out)
PERM *px;
MAT *A, *out;
#else
MAT *px_cols(const PERM *px, const MAT *A, MAT *out)
#endif
{
int i, j, m, n, px_j;
Real **A_me, **out_me;
#ifdef ANSI_C
MAT *m_get(int, int);
#else
extern MAT *m_get();
#endif
if ( ! A || ! px )
error(E_NULL,"px_cols");
if ( px->size != A->n )
error(E_SIZES,"px_cols");
if ( A == out )
error(E_INSITU,"px_cols");
m = A->m; n = A->n;
if ( ! out || out->m != m || out->n != n )
out = m_get(m,n);
A_me = A->me; out_me = out->me;
for ( j = 0; j < n; j++ )
{
px_j = px->pe[j];
if ( px_j >= n )
error(E_BOUNDS,"px_cols");
for ( i = 0; i < m; i++ )
out_me[i][px_j] = A_me[i][j];
}
return out;
}
/* px_rows -- permute columns of matrix A; out = px.A
-- May NOT be in situ */
#ifndef ANSI_C
MAT *px_rows(px,A,out)
PERM *px;
MAT *A, *out;
#else
MAT *px_rows(const PERM *px, const MAT *A, MAT *out)
#endif
{
int i, j, m, n, px_i;
Real **A_me, **out_me;
#ifdef ANSI_C
MAT *m_get(int, int);
#else
extern MAT *m_get();
#endif
if ( ! A || ! px )
error(E_NULL,"px_rows");
if ( px->size != A->m )
error(E_SIZES,"px_rows");
if ( A == out )
error(E_INSITU,"px_rows");
m = A->m; n = A->n;
if ( ! out || out->m != m || out->n != n )
out = m_get(m,n);
A_me = A->me; out_me = out->me;
for ( i = 0; i < m; i++ )
{
px_i = px->pe[i];
if ( px_i >= m )
error(E_BOUNDS,"px_rows");
for ( j = 0; j < n; j++ )
out_me[i][j] = A_me[px_i][j];
}
return out;
}