-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathiter0.c
435 lines (371 loc) · 10.1 KB
/
iter0.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**************************************************************************
**
** Copyright (C) 1993 David E. Stewart & 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.
**
***************************************************************************/
/* iter0.c 14/09/93 */
/* ITERATIVE METHODS - service functions */
/* functions for creating and releasing ITER structures;
for memory information;
for getting some values from an ITER variable;
for changing values in an ITER variable;
see also iter.c
*/
#include <stdio.h>
#include <math.h>
#include "iter.h"
static char rcsid[] = "$Id: iter0.c,v 1.3 1995/01/30 14:50:56 des Exp $";
/* standard functions */
/* standard information */
#ifndef ANSI_C
void iter_std_info(ip,nres,res,Bres)
ITER *ip;
double nres;
VEC *res, *Bres;
#else
void iter_std_info(const ITER *ip, double nres, VEC *res, VEC *Bres)
#endif
{
if (nres >= 0.0)
#ifndef MEX
printf(" %d. residual = %g\n",ip->steps,nres);
#else
mexPrintf(" %d. residual = %g\n",ip->steps,nres);
#endif
else
#ifndef MEX
printf(" %d. residual = %g (WARNING !!! should be >= 0) \n",
ip->steps,nres);
#else
mexPrintf(" %d. residual = %g (WARNING !!! should be >= 0) \n",
ip->steps,nres);
#endif
}
/* standard stopping criterion */
#ifndef ANSI_C
int iter_std_stop_crit(ip, nres, res, Bres)
ITER *ip;
double nres;
VEC *res, *Bres;
#else
int iter_std_stop_crit(const ITER *ip, double nres, VEC *res, VEC *Bres)
#endif
{
/* standard stopping criterium */
if (nres <= ip->init_res*ip->eps) return TRUE;
return FALSE;
}
/* iter_get - create a new structure pointing to ITER */
#ifndef ANSI_C
ITER *iter_get(lenb, lenx)
int lenb, lenx;
#else
ITER *iter_get(int lenb, int lenx)
#endif
{
ITER *ip;
if ((ip = NEW(ITER)) == (ITER *) NULL)
error(E_MEM,"iter_get");
else if (mem_info_is_on()) {
mem_bytes(TYPE_ITER,0,sizeof(ITER));
mem_numvar(TYPE_ITER,1);
}
/* default values */
ip->shared_x = FALSE;
ip->shared_b = FALSE;
ip->k = 0;
ip->limit = ITER_LIMIT_DEF;
ip->eps = ITER_EPS_DEF;
ip->steps = 0;
if (lenb > 0) ip->b = v_get(lenb);
else ip->b = (VEC *)NULL;
if (lenx > 0) ip->x = v_get(lenx);
else ip->x = (VEC *)NULL;
ip->Ax = (Fun_Ax) NULL;
ip->A_par = NULL;
ip->ATx = (Fun_Ax) NULL;
ip->AT_par = NULL;
ip->Bx = (Fun_Ax) NULL;
ip->B_par = NULL;
ip->info = iter_std_info;
ip->stop_crit = iter_std_stop_crit;
ip->init_res = 0.0;
return ip;
}
/* iter_free - release memory */
#ifndef ANSI_C
int iter_free(ip)
ITER *ip;
#else
int iter_free(ITER *ip)
#endif
{
if (ip == (ITER *)NULL) return -1;
if (mem_info_is_on()) {
mem_bytes(TYPE_ITER,sizeof(ITER),0);
mem_numvar(TYPE_ITER,-1);
}
if ( !ip->shared_x && ip->x != NULL ) v_free(ip->x);
if ( !ip->shared_b && ip->b != NULL ) v_free(ip->b);
free((char *)ip);
return 0;
}
#ifndef ANSI_C
ITER *iter_resize(ip,new_lenb,new_lenx)
ITER *ip;
int new_lenb, new_lenx;
#else
ITER *iter_resize(ITER *ip, int new_lenb, int new_lenx)
#endif
{
VEC *old;
if ( ip == (ITER *) NULL)
error(E_NULL,"iter_resize");
old = ip->x;
ip->x = v_resize(ip->x,new_lenx);
if ( ip->shared_x && old != ip->x )
warning(WARN_SHARED_VEC,"iter_resize");
old = ip->b;
ip->b = v_resize(ip->b,new_lenb);
if ( ip->shared_b && old != ip->b )
warning(WARN_SHARED_VEC,"iter_resize");
return ip;
}
#ifndef MEX
/* print out ip structure - for diagnostic purposes mainly */
#ifndef ANSI_C
void iter_dump(fp,ip)
ITER *ip;
FILE *fp;
#else
void iter_dump(FILE *fp, ITER *ip)
#endif
{
if (ip == NULL) {
fprintf(fp," ITER structure: NULL\n");
return;
}
fprintf(fp,"\n ITER structure:\n");
fprintf(fp," ip->shared_x = %s, ip->shared_b = %s\n",
(ip->shared_x ? "TRUE" : "FALSE"),
(ip->shared_b ? "TRUE" : "FALSE") );
fprintf(fp," ip->k = %d, ip->limit = %d, ip->steps = %d, ip->eps = %g\n",
ip->k,ip->limit,ip->steps,ip->eps);
fprintf(fp," ip->x = 0x%p, ip->b = 0x%p\n",ip->x,ip->b);
fprintf(fp," ip->Ax = 0x%p, ip->A_par = 0x%p\n",ip->Ax,ip->A_par);
fprintf(fp," ip->ATx = 0x%p, ip->AT_par = 0x%p\n",ip->ATx,ip->AT_par);
fprintf(fp," ip->Bx = 0x%p, ip->B_par = 0x%p\n",ip->Bx,ip->B_par);
fprintf(fp," ip->info = 0x%p, ip->stop_crit = 0x%p, ip->init_res = %g\n",
ip->info,ip->stop_crit,ip->init_res);
fprintf(fp,"\n");
}
#endif
/* copy the structure ip1 to ip2 preserving vectors x and b of ip2
(vectors x and b in ip2 are the same before and after iter_copy2)
if ip2 == NULL then a new structure is created with x and b being NULL
and other members are taken from ip1
*/
#ifndef ANSI_C
ITER *iter_copy2(ip1,ip2)
ITER *ip1, *ip2;
#else
ITER *iter_copy2(ITER *ip1, ITER *ip2)
#endif
{
VEC *x, *b;
int shx, shb;
if (ip1 == (ITER *)NULL)
error(E_NULL,"iter_copy2");
if (ip2 == (ITER *)NULL) {
if ((ip2 = NEW(ITER)) == (ITER *) NULL)
error(E_MEM,"iter_copy2");
else if (mem_info_is_on()) {
mem_bytes(TYPE_ITER,0,sizeof(ITER));
mem_numvar(TYPE_ITER,1);
}
ip2->x = ip2->b = NULL;
ip2->shared_x = ip2->shared_x = FALSE;
}
x = ip2->x;
b = ip2->b;
shb = ip2->shared_b;
shx = ip2->shared_x;
MEM_COPY(ip1,ip2,sizeof(ITER));
ip2->x = x;
ip2->b = b;
ip2->shared_x = shx;
ip2->shared_b = shb;
return ip2;
}
/* copy the structure ip1 to ip2 copying also the vectors x and b */
#ifndef ANSI_C
ITER *iter_copy(ip1,ip2)
ITER *ip1, *ip2;
#else
ITER *iter_copy(const ITER *ip1, ITER *ip2)
#endif
{
VEC *x, *b;
if (ip1 == (ITER *)NULL)
error(E_NULL,"iter_copy");
if (ip2 == (ITER *)NULL) {
if ((ip2 = NEW(ITER)) == (ITER *) NULL)
error(E_MEM,"iter_copy2");
else if (mem_info_is_on()) {
mem_bytes(TYPE_ITER,0,sizeof(ITER));
mem_numvar(TYPE_ITER,1);
}
}
x = ip2->x;
b = ip2->b;
MEM_COPY(ip1,ip2,sizeof(ITER));
if (ip1->x)
ip2->x = v_copy(ip1->x,x);
if (ip1->b)
ip2->b = v_copy(ip1->b,b);
ip2->shared_x = ip2->shared_b = FALSE;
return ip2;
}
/*** functions to generate sparse matrices with random entries ***/
/* iter_gen_sym -- generate symmetric positive definite
n x n matrix,
nrow - number of nonzero entries in a row
*/
#ifndef ANSI_C
SPMAT *iter_gen_sym(n,nrow)
int n, nrow;
#else
SPMAT *iter_gen_sym(int n, int nrow)
#endif
{
SPMAT *A;
VEC *u;
Real s1;
int i, j, k, k_max;
if (nrow <= 1) nrow = 2;
/* nrow should be even */
if ((nrow & 1)) nrow -= 1;
A = sp_get(n,n,nrow);
u = v_get(A->m);
v_zero(u);
for ( i = 0; i < A->m; i++ )
{
k_max = ((rand() >> 8) % (nrow/2));
for ( k = 0; k <= k_max; k++ )
{
j = (rand() >> 8) % A->n;
s1 = mrand();
sp_set_val(A,i,j,s1);
sp_set_val(A,j,i,s1);
u->ve[i] += fabs(s1);
u->ve[j] += fabs(s1);
}
}
/* ensure that A is positive definite */
for ( i = 0; i < A->m; i++ )
sp_set_val(A,i,i,u->ve[i] + 1.0);
V_FREE(u);
return A;
}
/* iter_gen_nonsym -- generate non-symmetric m x n sparse matrix, m >= n
nrow - number of entries in a row;
diag - number which is put in diagonal entries and then permuted
(if diag is zero then 1.0 is there)
*/
#ifndef ANSI_C
SPMAT *iter_gen_nonsym(m,n,nrow,diag)
int m, n, nrow;
double diag;
#else
SPMAT *iter_gen_nonsym(int m, int n, int nrow, double diag)
#endif
{
SPMAT *A;
PERM *px;
int i, j, k, k_max;
Real s1;
if (nrow <= 1) nrow = 2;
if (diag == 0.0) diag = 1.0;
A = sp_get(m,n,nrow);
px = px_get(n);
for ( i = 0; i < A->m; i++ )
{
k_max = (rand() >> 8) % (nrow-1);
for ( k = 0; k <= k_max; k++ )
{
j = (rand() >> 8) % A->n;
s1 = mrand();
sp_set_val(A,i,j,-s1);
}
}
/* to make it likely that A is nonsingular, use pivot... */
for ( i = 0; i < 2*A->n; i++ )
{
j = (rand() >> 8) % A->n;
k = (rand() >> 8) % A->n;
px_transp(px,j,k);
}
for ( i = 0; i < A->n; i++ )
sp_set_val(A,i,px->pe[i],diag);
PX_FREE(px);
return A;
}
#if ( 0 )
/* iter_gen_nonsym -- generate non-symmetric positive definite
n x n sparse matrix;
nrow - number of entries in a row
*/
#ifndef ANSI_C
SPMAT *iter_gen_nonsym_posdef(n,nrow)
int n, nrow;
#else
SPMAT *iter_gen_nonsym(int m, int n, int nrow, double diag)
#endif
{
SPMAT *A;
PERM *px;
VEC *u;
int i, j, k, k_max;
Real s1;
if (nrow <= 1) nrow = 2;
A = sp_get(n,n,nrow);
px = px_get(n);
u = v_get(A->m);
v_zero(u);
for ( i = 0; i < A->m; i++ )
{
k_max = (rand() >> 8) % (nrow-1);
for ( k = 0; k <= k_max; k++ )
{
j = (rand() >> 8) % A->n;
s1 = mrand();
sp_set_val(A,i,j,-s1);
u->ve[i] += fabs(s1);
}
}
/* ensure that A is positive definite */
for ( i = 0; i < A->m; i++ )
sp_set_val(A,i,i,u->ve[i] + 1.0);
PX_FREE(px);
V_FREE(u);
return A;
}
#endif