1
#include "cs_di_demo.h"
3
/* 1 if A is square & upper tri., -1 if square & lower tri., 0 otherwise */
4
static int is_sym (cs_di *A)
6
int is_upper, is_lower, j, p, n = A->n, m = A->m, *Ap = A->p, *Ai = A->i ;
7
if (m != n) return (0) ;
10
for (j = 0 ; j < n ; j++)
12
for (p = Ap [j] ; p < Ap [j+1] ; p++)
14
if (Ai [p] > j) is_upper = 0 ;
15
if (Ai [p] < j) is_lower = 0 ;
18
return (is_upper ? 1 : (is_lower ? -1 : 0)) ;
21
/* true for off-diagonal entries */
22
static int dropdiag (int i, int j, double aij, void *other) { return (i != j) ;}
24
/* C = A + triu(A,1)' */
25
static cs_di *make_sym (cs_di *A)
28
AT = cs_di_transpose (A, 1) ; /* AT = A' */
29
cs_di_fkeep (AT, &dropdiag, NULL) ; /* drop diagonal entries from AT */
30
C = cs_di_add (A, AT, 1, 1) ; /* C = A+AT */
35
/* create a right-hand side */
36
static void rhs (double *x, double *b, int m)
39
for (i = 0 ; i < m ; i++) b [i] = 1 + ((double) i) / m ;
40
for (i = 0 ; i < m ; i++) x [i] = b [i] ;
43
/* infinity-norm of x */
44
static double norm (double *x, int n)
48
for (i = 0 ; i < n ; i++) normx = CS_MAX (normx, fabs (x [i])) ;
52
/* compute residual, norm(A*x-b,inf) / (norm(A,1)*norm(x,inf) + norm(b,inf)) */
53
static void print_resid (int ok, cs_di *A, double *x, double *b, double *resid)
56
if (!ok) { printf (" (failed)\n") ; return ; }
58
for (i = 0 ; i < m ; i++) resid [i] = -b [i] ; /* resid = -b */
59
cs_di_gaxpy (A, x, resid) ; /* resid = resid + A*x */
60
printf ("resid: %8.2e\n", norm (resid,m) / ((n == 0) ? 1 :
61
(cs_di_norm (A) * norm (x,n) + norm (b,m)))) ;
64
static double tic (void) { return (clock () / (double) CLOCKS_PER_SEC) ; }
65
static double toc (double t) { double s = tic () ; return (CS_MAX (0, s-t)) ; }
67
static void print_order (int order)
71
case 0: printf ("natural ") ; break ;
72
case 1: printf ("amd(A+A') ") ; break ;
73
case 2: printf ("amd(S'*S) ") ; break ;
74
case 3: printf ("amd(A'*A) ") ; break ;
78
/* read a problem from a file */
79
problem *get_problem (FILE *f, double tol)
82
int sym, m, n, mn, nz1, nz2 ;
84
Prob = cs_di_calloc (1, sizeof (problem)) ;
85
if (!Prob) return (NULL) ;
86
T = cs_di_load (f) ; /* load triplet matrix T from a file */
87
Prob->A = A = cs_di_compress (T) ; /* A = compressed-column form of T */
88
cs_di_spfree (T) ; /* clear T */
89
if (!cs_di_dupl (A)) return (free_problem (Prob)) ; /* sum up duplicates */
90
Prob->sym = sym = is_sym (A) ; /* determine if A is symmetric */
94
cs_di_dropzeros (A) ; /* drop zero entries */
96
if (tol > 0) cs_di_droptol (A, tol) ; /* drop tiny entries (just to test) */
97
Prob->C = C = sym ? make_sym (A) : A ; /* C = A + triu(A,1)', or C=A */
98
if (!C) return (free_problem (Prob)) ;
99
printf ("\n--- Matrix: %d-by-%d, nnz: %d (sym: %d: nnz %d), norm: %8.2e\n",
100
m, n, A->p [n], sym, sym ? C->p [n] : 0, cs_di_norm (C)) ;
101
if (nz1 != nz2) printf ("zero entries dropped: %d\n", nz1 - nz2) ;
102
if (nz2 != A->p [n]) printf ("tiny entries dropped: %d\n", nz2 - A->p [n]) ;
103
Prob->b = cs_di_malloc (mn, sizeof (double)) ;
104
Prob->x = cs_di_malloc (mn, sizeof (double)) ;
105
Prob->resid = cs_di_malloc (mn, sizeof (double)) ;
106
return ((!Prob->b || !Prob->x || !Prob->resid) ? free_problem (Prob) : Prob) ;
110
problem *free_problem (problem *Prob)
112
if (!Prob) return (NULL) ;
113
cs_di_spfree (Prob->A) ;
114
if (Prob->sym) cs_di_spfree (Prob->C) ;
115
cs_di_free (Prob->b) ;
116
cs_di_free (Prob->x) ;
117
cs_di_free (Prob->resid) ;
118
return (cs_di_free (Prob)) ;
121
/* solve a linear system using Cholesky, LU, and QR, with various orderings */
122
int demo2 (problem *Prob)
125
double *b, *x, *resid, t, tol ;
126
int k, m, n, ok, order, nb, ns, *r, *s, *rr, sprank ;
128
if (!Prob) return (0) ;
129
A = Prob->A ; C = Prob->C ; b = Prob->b ; x = Prob->x ; resid = Prob->resid;
130
m = A->m ; n = A->n ;
131
tol = Prob->sym ? 0.001 : 1 ; /* partial pivoting tolerance */
132
D = cs_di_dmperm (C, 1) ; /* randomized dmperm analysis */
134
nb = D->nb ; r = D->r ; s = D->s ; rr = D->rr ;
136
for (ns = 0, k = 0 ; k < nb ; k++)
138
ns += ((r [k+1] == r [k]+1) && (s [k+1] == s [k]+1)) ;
140
printf ("blocks: %d singletons: %d structural rank: %d\n", nb, ns, sprank) ;
142
for (order = 0 ; order <= 3 ; order += 3) /* natural and amd(A'*A) */
144
if (!order && m > 1000) continue ;
146
print_order (order) ;
147
rhs (x, b, m) ; /* compute right-hand side */
149
ok = cs_di_qrsol (order, C, x) ; /* min norm(Ax-b) with QR */
150
printf ("time: %8.2f ", toc (t)) ;
151
print_resid (ok, C, x, b, resid) ; /* print residual */
153
if (m != n || sprank < n) return (1) ; /* return if rect. or singular*/
154
for (order = 0 ; order <= 3 ; order++) /* try all orderings */
156
if (!order && m > 1000) continue ;
158
print_order (order) ;
159
rhs (x, b, m) ; /* compute right-hand side */
161
ok = cs_di_lusol (order, C, x, tol) ; /* solve Ax=b with LU */
162
printf ("time: %8.2f ", toc (t)) ;
163
print_resid (ok, C, x, b, resid) ; /* print residual */
165
if (!Prob->sym) return (1) ;
166
for (order = 0 ; order <= 1 ; order++) /* natural and amd(A+A') */
168
if (!order && m > 1000) continue ;
170
print_order (order) ;
171
rhs (x, b, m) ; /* compute right-hand side */
173
ok = cs_di_cholsol (order, C, x) ; /* solve Ax=b with Cholesky */
174
printf ("time: %8.2f ", toc (t)) ;
175
print_resid (ok, C, x, b, resid) ; /* print residual */
180
/* free workspace for demo3 */
181
static int done3 (int ok, cs_dis *S, cs_din *N, double *y, cs_di *W, cs_di *E, int *p)
192
/* Cholesky update/downdate */
193
int demo3 (problem *Prob)
195
cs_di *A, *C, *W = NULL, *WW, *WT, *E = NULL, *W2 ;
196
int n, k, *Li, *Lp, *Wi, *Wp, p1, p2, *p = NULL, ok ;
197
double *b, *x, *resid, *y = NULL, *Lx, *Wx, s, t, t1 ;
200
if (!Prob || !Prob->sym || Prob->A->n == 0) return (0) ;
201
A = Prob->A ; C = Prob->C ; b = Prob->b ; x = Prob->x ; resid = Prob->resid;
203
if (!Prob->sym || n == 0) return (1) ;
204
rhs (x, b, n) ; /* compute right-hand side */
205
printf ("\nchol then update/downdate ") ;
207
y = cs_di_malloc (n, sizeof (double)) ;
209
S = cs_di_schol (1, C) ; /* symbolic Chol, amd(A+A') */
210
printf ("\nsymbolic chol time %8.2f\n", toc (t)) ;
212
N = cs_di_chol (C, S) ; /* numeric Cholesky */
213
printf ("numeric chol time %8.2f\n", toc (t)) ;
214
if (!S || !N || !y) return (done3 (0, S, N, y, W, E, p)) ;
216
cs_di_ipvec (S->pinv, b, y, n) ; /* y = P*b */
217
cs_di_lsolve (N->L, y) ; /* y = L\y */
218
cs_di_ltsolve (N->L, y) ; /* y = L'\y */
219
cs_di_pvec (S->pinv, y, x, n) ; /* x = P'*y */
220
printf ("solve chol time %8.2f\n", toc (t)) ;
221
printf ("original: ") ;
222
print_resid (1, C, x, b, resid) ; /* print residual */
223
k = n/2 ; /* construct W */
224
W = cs_di_spalloc (n, 1, n, 1, 0) ;
225
if (!W) return (done3 (0, S, N, y, W, E, p)) ;
226
Lp = N->L->p ; Li = N->L->i ; Lx = N->L->x ;
227
Wp = W->p ; Wi = W->i ; Wx = W->x ;
230
Wp [1] = Lp [k+1] - p1 ;
233
for ( ; p1 < Lp [k+1] ; p1++)
237
Wx [p2] = s * rand () / ((double) RAND_MAX) ;
240
ok = cs_di_updown (N->L, +1, W, S->parent) ; /* update: L*L'+W*W' */
242
printf ("update: time: %8.2f\n", t1) ;
243
if (!ok) return (done3 (0, S, N, y, W, E, p)) ;
245
cs_di_ipvec (S->pinv, b, y, n) ; /* y = P*b */
246
cs_di_lsolve (N->L, y) ; /* y = L\y */
247
cs_di_ltsolve (N->L, y) ; /* y = L'\y */
248
cs_di_pvec (S->pinv, y, x, n) ; /* x = P'*y */
250
p = cs_di_pinv (S->pinv, n) ;
251
W2 = cs_di_permute (W, p, NULL, 1) ; /* E = C + (P'W)*(P'W)' */
252
WT = cs_di_transpose (W2,1) ;
253
WW = cs_di_multiply (W2, WT) ;
256
E = cs_di_add (C, WW, 1, 1) ;
258
if (!E || !p) return (done3 (0, S, N, y, W, E, p)) ;
259
printf ("update: time: %8.2f (incl solve) ", t1+t) ;
260
print_resid (1, E, x, b, resid) ; /* print residual */
261
cs_di_nfree (N) ; /* clear N */
263
N = cs_di_chol (E, S) ; /* numeric Cholesky */
264
if (!N) return (done3 (0, S, N, y, W, E, p)) ;
265
cs_di_ipvec (S->pinv, b, y, n) ; /* y = P*b */
266
cs_di_lsolve (N->L, y) ; /* y = L\y */
267
cs_di_ltsolve (N->L, y) ; /* y = L'\y */
268
cs_di_pvec (S->pinv, y, x, n) ; /* x = P'*y */
270
printf ("rechol: time: %8.2f (incl solve) ", t) ;
271
print_resid (1, E, x, b, resid) ; /* print residual */
273
ok = cs_di_updown (N->L, -1, W, S->parent) ; /* downdate: L*L'-W*W' */
275
if (!ok) return (done3 (0, S, N, y, W, E, p)) ;
276
printf ("downdate: time: %8.2f\n", t1) ;
278
cs_di_ipvec (S->pinv, b, y, n) ; /* y = P*b */
279
cs_di_lsolve (N->L, y) ; /* y = L\y */
280
cs_di_ltsolve (N->L, y) ; /* y = L'\y */
281
cs_di_pvec (S->pinv, y, x, n) ; /* x = P'*y */
283
printf ("downdate: time: %8.2f (incl solve) ", t1+t) ;
284
print_resid (1, C, x, b, resid) ; /* print residual */
285
return (done3 (1, S, N, y, W, E, p)) ;