File: spswap.c

package info (click to toggle)
meschach 1.2b-14
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,164 kB
  • ctags: 2,188
  • sloc: ansic: 21,959; makefile: 484
file content (302 lines) | stat: -rw-r--r-- 7,368 bytes parent folder | download | duplicates (5)
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

/**************************************************************************
**
** 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.
**
***************************************************************************/


/*
	Sparse matrix swap and permutation routines
	Modified Mon 09th Nov 1992, 08:50:54 PM
	to use Karen George's suggestion to use unordered rows
*/

static	char	rcsid[] = "$Id: spswap.c,v 1.3 1994/01/13 05:44:43 des Exp $";

#include	<stdio.h>
#include	<math.h>
#include        "sparse2.h"


#define	btos(x)	((x) ? "TRUE" : "FALSE")

/* scan_to -- updates scan (int) vectors to point to the last row in each
	column with row # <= max_row, if any */
void	scan_to(A, scan_row, scan_idx, col_list, max_row)
SPMAT	*A;
IVEC	*scan_row, *scan_idx, *col_list;
int	max_row;
{
    int		col, idx, j_idx, row_num;
    SPROW	*r;
    row_elt	*e;

    if ( ! A || ! scan_row || ! scan_idx || ! col_list )
	error(E_NULL,"scan_to");
    if ( scan_row->dim != scan_idx->dim || scan_idx->dim != col_list->dim )
	error(E_SIZES,"scan_to");

    if ( max_row < 0 )
	return;

    if ( ! A->flag_col )
	sp_col_access(A);

    for ( j_idx = 0; j_idx < scan_row->dim; j_idx++ )
    {
	row_num = scan_row->ive[j_idx];
	idx = scan_idx->ive[j_idx];
	col = col_list->ive[j_idx];

	if ( col < 0 || col >= A->n )
	    error(E_BOUNDS,"scan_to");
	if ( row_num < 0 )
	{
	    idx = col;
	    continue;
	}
	r = &(A->row[row_num]);
	if ( idx < 0 )
	    error(E_INTERN,"scan_to");
	e = &(r->elt[idx]);
	if ( e->col != col )
	    error(E_INTERN,"scan_to");
	if ( idx < 0 )
	{
	    printf("scan_to: row_num = %d, idx = %d, col = %d\n",
		   row_num, idx, col);
	    error(E_INTERN,"scan_to");
	}
	/* if ( e->nxt_row <= max_row )
	    chase_col(A, col, &row_num, &idx, max_row); */
	while ( e->nxt_row >= 0 && e->nxt_row <= max_row )
	{
	    row_num = e->nxt_row;
	    idx = e->nxt_idx;
	    e = &(A->row[row_num].elt[idx]);
	}
	    
	/* printf("scan_to: computed j_idx = %d, row_num = %d, idx = %d\n",
	       j_idx, row_num, idx); */
	scan_row->ive[j_idx] = row_num;
	scan_idx->ive[j_idx] = idx;
    }
}

/* patch_col -- patches column access paths for fill-in */
void patch_col(A, col, old_row, old_idx, row_num, idx)
SPMAT	*A;
int	col, old_row, old_idx, row_num, idx;
{
    SPROW	*r;
    row_elt	*e;
    
    if ( old_row >= 0 )
    {
	r = &(A->row[old_row]);
	old_idx = sprow_idx2(r,col,old_idx);
	e = &(r->elt[old_idx]);
	e->nxt_row = row_num;
	e->nxt_idx = idx;
    }
    else
    {
	A->start_row[col] = row_num;
	A->start_idx[col] = idx;
    }
}

/* chase_col -- chases column access path in column col, starting with
   row_num and idx, to find last row # in this column <= max_row
   -- row_num is returned; idx is also set by this routine
   -- assumes that the column access paths (possibly without the
   nxt_idx fields) are set up */
row_elt *chase_col(A, col, row_num, idx, max_row)
SPMAT	*A;
int	col, *row_num, *idx, max_row;
{
    int		old_idx, old_row, tmp_idx, tmp_row;
    SPROW	*r;
    row_elt	*e;
    
    if ( col < 0 || col >= A->n )
	error(E_BOUNDS,"chase_col");
    tmp_row = *row_num;
    if ( tmp_row < 0 )
    {
	if ( A->start_row[col] > max_row )
	{
	    tmp_row = -1;
	    tmp_idx = col;
	    return (row_elt *)NULL;
	}
	else
	{
	    tmp_row = A->start_row[col];
	    tmp_idx = A->start_idx[col];
	}
    }
    else
	tmp_idx = *idx;
    
    old_row = tmp_row;
    old_idx = tmp_idx;
    while ( tmp_row >= 0 && tmp_row < max_row )
    {
	r = &(A->row[tmp_row]);
	/* tmp_idx = sprow_idx2(r,col,tmp_idx); */
	if ( tmp_idx < 0 || tmp_idx >= r->len ||
	     r->elt[tmp_idx].col != col )
	{
#ifdef DEBUG
	    printf("chase_col:error: col = %d, row # = %d, idx = %d\n",
		   col, tmp_row, tmp_idx);
	    printf("chase_col:error: old_row = %d, old_idx = %d\n",
		   old_row, old_idx);
	    printf("chase_col:error: A =\n");
	    sp_dump(stdout,A);
#endif
	    error(E_INTERN,"chase_col");
	}
	e = &(r->elt[tmp_idx]);
	old_row = tmp_row;
	old_idx = tmp_idx;
	tmp_row = e->nxt_row;
	tmp_idx = e->nxt_idx;
    }
    if ( old_row > max_row )
    {
	old_row = -1;
	old_idx = col;
	e = (row_elt *)NULL;
    }
    else if ( tmp_row <= max_row && tmp_row >= 0 )
    {
	old_row = tmp_row;
	old_idx = tmp_idx;
    }

    *row_num = old_row;
    if ( old_row >= 0 )
	*idx = old_idx;
    else
	*idx = col;

    return e;
}

/* chase_past -- as for chase_col except that we want the first
	row whose row # >= min_row; -1 indicates no such row */
row_elt *chase_past(A, col, row_num, idx, min_row)
SPMAT	*A;
int	col, *row_num, *idx, min_row;
{
    SPROW	*r;
    row_elt	*e;
    int		tmp_idx, tmp_row;

    tmp_row = *row_num;
    tmp_idx = *idx;
    chase_col(A,col,&tmp_row,&tmp_idx,min_row);
    if ( tmp_row < 0 )	/* use A->start_row[..] etc. */
    {
	if ( A->start_row[col] < 0 )
	    tmp_row = -1;
	else
	{
	    tmp_row = A->start_row[col];
	    tmp_idx = A->start_idx[col];
	}
    }
    else if ( tmp_row < min_row )
    {
	r = &(A->row[tmp_row]);
	if ( tmp_idx < 0 || tmp_idx >= r->len ||
	     r->elt[tmp_idx].col != col )
	    error(E_INTERN,"chase_past");
	tmp_row = r->elt[tmp_idx].nxt_row;
	tmp_idx = r->elt[tmp_idx].nxt_idx;
    }

    *row_num = tmp_row;
    *idx = tmp_idx;
    if ( tmp_row < 0 )
	e = (row_elt *)NULL;
    else
    {
	if ( tmp_idx < 0 || tmp_idx >= A->row[tmp_row].len ||
	     A->row[tmp_row].elt[tmp_idx].col != col )
	    error(E_INTERN,"bump_col");
	e = &(A->row[tmp_row].elt[tmp_idx]);
    }

    return e;
}

/* bump_col -- move along to next nonzero entry in column col after row_num
	-- update row_num and idx */
row_elt *bump_col(A, col, row_num, idx)
SPMAT	*A;
int	col, *row_num, *idx;
{
    SPROW	*r;
    row_elt	*e;
    int		tmp_row, tmp_idx;

    tmp_row = *row_num;
    tmp_idx = *idx;
    /* printf("bump_col: col = %d, row# = %d, idx = %d\n",
	   col, *row_num, *idx); */
    if ( tmp_row < 0 )
    {
	tmp_row = A->start_row[col];
	tmp_idx = A->start_idx[col];
    }
    else
    {
	r = &(A->row[tmp_row]);
	if ( tmp_idx < 0 || tmp_idx >= r->len ||
	     r->elt[tmp_idx].col != col )
	    error(E_INTERN,"bump_col");
	e = &(r->elt[tmp_idx]);
	tmp_row = e->nxt_row;
	tmp_idx = e->nxt_idx;
    }
    if ( tmp_row < 0 )
    {
	e = (row_elt *)NULL;
	tmp_idx = col;
    }
    else
    {
	if ( tmp_idx < 0 || tmp_idx >= A->row[tmp_row].len ||
	     A->row[tmp_row].elt[tmp_idx].col != col )
	    error(E_INTERN,"bump_col");
	e = &(A->row[tmp_row].elt[tmp_idx]);
    }
    *row_num = tmp_row;
    *idx = tmp_idx;

    return e;
}