-
Notifications
You must be signed in to change notification settings - Fork 0
/
csparse.c
2161 lines (2065 loc) · 74.5 KB
/
csparse.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "csparse.h"
cs* cs_add(const cs* A, const cs* B, double alpha, double beta)
/*
Purpose:
CS_ADD computes C = alpha*A + beta*B for sparse A and B.
Reference:
Timothy Davis,
Direct Methods for Sparse Linear Systems,
SIAM, Philadelphia, 2006.
*/
{
int p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values;
double *x, *Bx, *Cx;
cs* C;
if(!A || !B) return (NULL); /* check inputs */
m = A->m;
anz = A->p[A->n];
n = B->n;
Bp = B->p;
Bx = B->x;
bnz = Bp[n];
w = cs_calloc(m, sizeof(int));
values = (A->x != NULL) && (Bx != NULL);
x = values ? cs_malloc(m, sizeof(double)) : NULL;
C = cs_spalloc(m, n, anz + bnz, values, 0);
if(!C || !w || (values && !x)) return (cs_done(C, w, x, 0));
Cp = C->p;
Ci = C->i;
Cx = C->x;
for(j = 0; j < n; j++) {
Cp[j] = nz; /* column j of C starts here */
nz = cs_scatter(A, j, alpha, w, x, j + 1, C, nz); /* alpha*A(:,j)*/
nz = cs_scatter(B, j, beta, w, x, j + 1, C, nz); /* beta*B(:,j) */
if(values)
for(p = Cp[j]; p < nz; p++)
Cx[p] = x[Ci[p]];
}
Cp[n] = nz; /* finalize the last column of C */
cs_sprealloc(C, 0); /* remove extra space from C */
return (cs_done(C, w, x, 1)); /* success; free workspace, return C */
}
static int cs_wclear(int mark, int lemax, int* w, int n)
/*
Purpose:
CS_WCLEAR clears W.
Reference:
Timothy Davis,
Direct Methods for Sparse Linear Systems,
SIAM, Philadelphia, 2006.
*/
{
int k;
if(mark < 2 || (mark + lemax < 0)) {
for(k = 0; k < n; k++)
if(w[k] != 0) w[k] = 1;
mark = 2;
}
return (mark); /* at this point, w [0..n-1] < mark holds */
}
/* keep off-diagonal entries; drop diagonal entries */
static int cs_diag(int i, int j, double aij, void* other) {
return (i != j);
}
/* p = amd(A+A') if symmetric is true, or amd(A'A) otherwise */
int* cs_amd(const cs* A, int order)
/*
Purpose:
CS_AMD carries out the approximate minimum degree algorithm.
Reference:
Timothy Davis,
Direct Methods for Sparse Linear Systems,
SIAM, Philadelphia, 2006.
Parameters:
Input, int ORDER:
-1:natural,
0:Cholesky,
1:LU,
2:QR
*/
{
cs *C, *A2, *AT;
int *Cp, *Ci, *last, *ww, *len, *nv, *next, *P, *head, *elen, *degree, *w,
*hhead, *ATp, *ATi, d, dk, dext,
lemax = 0, e, elenk, eln, i, j, k, k1, k2, k3, jlast, ln, dense, nzmax,
mindeg = 0, nvi, nvj, nvk, mark, wnvi, ok, cnz, nel = 0, p, p1, p2, p3,
p4, pj, pk, pk1, pk2, pn, q, n, m;
unsigned int h;
/* --- Construct matrix C ----------------------------------------------- */
if(!A || order < 0) return (NULL); /* check inputs; quick return */
AT = cs_transpose(A, 0); /* compute A' */
if(!AT) return (NULL);
m = A->m;
n = A->n;
dense = CS_MAX(16, 10 * sqrt((double)n)); /* find dense threshold */
dense = CS_MIN(n - 2, dense);
if(order == 0 && n == m) {
C = cs_add(A, AT, 0, 0); /* C = A+A' */
} else if(order == 1) {
ATp = AT->p; /* drop dense columns from AT */
ATi = AT->i;
for(p2 = 0, j = 0; j < m; j++) {
p = ATp[j]; /* column j of AT starts here */
ATp[j] = p2; /* new column j starts here */
if(ATp[j + 1] - p > dense) continue; /* skip dense col j */
for(; p < ATp[j + 1]; p++)
ATi[p2++] = ATi[p];
}
ATp[m] = p2; /* finalize AT */
A2 = cs_transpose(AT, 0); /* A2 = AT' */
C = A2 ? cs_multiply(AT, A2) : NULL; /* C=A'*A with no dense rows */
cs_spfree(A2);
} else {
C = cs_multiply(AT, A); /* C=A'*A */
}
cs_spfree(AT);
if(!C) return (NULL);
P = cs_malloc(n + 1, sizeof(int)); /* allocate result */
ww = cs_malloc(8 * (n + 1), sizeof(int)); /* get workspace */
len = ww;
nv = ww + (n + 1);
next = ww + 2 * (n + 1);
head = ww + 3 * (n + 1);
elen = ww + 4 * (n + 1);
degree = ww + 5 * (n + 1);
w = ww + 6 * (n + 1);
hhead = ww + 7 * (n + 1);
last = P; /* use P as workspace for last */
cs_fkeep(C, &cs_diag, NULL); /* drop diagonal entries */
Cp = C->p;
cnz = Cp[n];
if(!cs_sprealloc(C, cnz + cnz / 5 + 2 * n)) return (cs_idone(P, C, ww, 0));
/* --- Initialize quotient graph ---------------------------------------- */
for(k = 0; k < n; k++)
len[k] = Cp[k + 1] - Cp[k];
len[n] = 0;
nzmax = C->nzmax;
Ci = C->i;
for(i = 0; i <= n; i++) {
head[i] = -1; /* degree list i is empty */
last[i] = -1;
next[i] = -1;
hhead[i] = -1; /* hash list i is empty */
nv[i] = 1; /* node i is just one node */
w[i] = 1; /* node i is alive */
elen[i] = 0; /* Ek of node i is empty */
degree[i] = len[i]; /* degree of node i */
}
mark = cs_wclear(0, 0, w, n); /* clear w */
elen[n] = -2; /* n is a dead element */
Cp[n] = -1; /* n is a root of assembly tree */
w[n] = 0; /* n is a dead element */
/* --- Initialize degree lists ------------------------------------------ */
for(i = 0; i < n; i++) {
d = degree[i];
if(d == 0) /* node i is empty */
{
elen[i] = -2; /* element i is dead */
nel++;
Cp[i] = -1; /* i is a root of assemby tree */
w[i] = 0;
} else if(d > dense) /* node i is dense */
{
nv[i] = 0; /* absorb i into element n */
elen[i] = -1; /* node i is dead */
nel++;
Cp[i] = CS_FLIP(n);
nv[n]++;
} else {
if(head[d] != -1) last[head[d]] = i;
next[i] = head[d]; /* put node i in degree list d */
head[d] = i;
}
}
while(nel < n) /* while (selecting pivots) do */
{
/* --- Select node of minimum approximate degree -------------------- */
for(k = -1; mindeg < n && (k = head[mindeg]) == -1; mindeg++)
;
if(next[k] != -1) last[next[k]] = -1;
head[mindeg] = next[k]; /* remove k from degree list */
elenk = elen[k]; /* elenk = |Ek| */
nvk = nv[k]; /* # of nodes k represents */
nel += nvk; /* nv[k] nodes of A eliminated */
/* --- Garbage collection ------------------------------------------- */
if(elenk > 0 && cnz + mindeg >= nzmax) {
for(j = 0; j < n; j++) {
if((p = Cp[j]) >= 0) /* j is a live node or element */
{
Cp[j] = Ci[p]; /* save first entry of object */
Ci[p] = CS_FLIP(j); /* first entry is now CS_FLIP(j) */
}
}
for(q = 0, p = 0; p < cnz;) /* scan all of memory */
{
if((j = CS_FLIP(Ci[p++])) >= 0) /* found object j */
{
Ci[q] = Cp[j]; /* restore first entry of object */
Cp[j] = q++; /* new pointer to object j */
for(k3 = 0; k3 < len[j] - 1; k3++)
Ci[q++] = Ci[p++];
}
}
cnz = q; /* Ci [cnz...nzmax-1] now free */
}
/* --- Construct new element ---------------------------------------- */
dk = 0;
nv[k] = -nvk; /* flag k as in Lk */
p = Cp[k];
pk1 = (elenk == 0) ? p : cnz; /* do in place if elen[k] == 0 */
pk2 = pk1;
for(k1 = 1; k1 <= elenk + 1; k1++) {
if(k1 > elenk) {
e = k; /* search the nodes in k */
pj = p; /* list of nodes starts at Ci[pj]*/
ln = len[k] - elenk; /* length of list of nodes in k */
} else {
e = Ci[p++]; /* search the nodes in e */
pj = Cp[e];
ln = len[e]; /* length of list of nodes in e */
}
for(k2 = 1; k2 <= ln; k2++) {
i = Ci[pj++];
if((nvi = nv[i]) <= 0) continue; /* node i dead, or seen */
dk += nvi; /* degree[Lk] += size of node i */
nv[i] = -nvi; /* negate nv[i] to denote i in Lk*/
Ci[pk2++] = i; /* place i in Lk */
if(next[i] != -1) last[next[i]] = last[i];
if(last[i] != -1) /* remove i from degree list */
{
next[last[i]] = next[i];
} else {
head[degree[i]] = next[i];
}
}
if(e != k) {
Cp[e] = CS_FLIP(k); /* absorb e into k */
w[e] = 0; /* e is now a dead element */
}
}
if(elenk != 0) cnz = pk2; /* Ci [cnz...nzmax] is free */
degree[k] = dk; /* external degree of k - |Lk\i| */
Cp[k] = pk1; /* element k is in Ci[pk1..pk2-1] */
len[k] = pk2 - pk1;
elen[k] = -2; /* k is now an element */
/* --- Find set differences ----------------------------------------- */
mark = cs_wclear(mark, lemax, w, n); /* clear w if necessary */
for(pk = pk1; pk < pk2; pk++) /* scan 1: find |Le\Lk| */
{
i = Ci[pk];
if((eln = elen[i]) <= 0) continue; /* skip if elen[i] empty */
nvi = -nv[i]; /* nv [i] was negated */
wnvi = mark - nvi;
for(p = Cp[i]; p <= Cp[i] + eln - 1; p++) /* scan Ei */
{
e = Ci[p];
if(w[e] >= mark) {
w[e] -= nvi; /* decrement |Le\Lk| */
} else if(w[e] != 0) /* ensure e is a live element */
{
w[e] = degree[e] + wnvi; /* 1st time e seen in scan 1 */
}
}
}
/* --- Degree update ------------------------------------------------ */
for(pk = pk1; pk < pk2; pk++) /* scan2: degree update */
{
i = Ci[pk]; /* consider node i in Lk */
p1 = Cp[i];
p2 = p1 + elen[i] - 1;
pn = p1;
for(h = 0, d = 0, p = p1; p <= p2; p++) /* scan Ei */
{
e = Ci[p];
if(w[e] != 0) /* e is an unabsorbed element */
{
dext = w[e] - mark; /* dext = |Le\Lk| */
if(dext > 0) {
d += dext; /* sum up the set differences */
Ci[pn++] = e; /* keep e in Ei */
h += e; /* compute the hash of node i */
} else {
Cp[e] = CS_FLIP(k); /* aggressive absorb. e->k */
w[e] = 0; /* e is a dead element */
}
}
}
elen[i] = pn - p1 + 1; /* elen[i] = |Ei| */
p3 = pn;
p4 = p1 + len[i];
for(p = p2 + 1; p < p4; p++) /* prune edges in Ai */
{
j = Ci[p];
if((nvj = nv[j]) <= 0) continue; /* node j dead or in Lk */
d += nvj; /* degree(i) += |j| */
Ci[pn++] = j; /* place j in node list of i */
h += j; /* compute hash for node i */
}
if(d == 0) /* check for mass elimination */
{
Cp[i] = CS_FLIP(k); /* absorb i into k */
nvi = -nv[i];
dk -= nvi; /* |Lk| -= |i| */
nvk += nvi; /* |k| += nv[i] */
nel += nvi;
nv[i] = 0;
elen[i] = -1; /* node i is dead */
} else {
degree[i] = CS_MIN(degree[i], d); /* update degree(i) */
Ci[pn] = Ci[p3]; /* move first node to end */
Ci[p3] = Ci[p1]; /* move 1st el. to end of Ei */
Ci[p1] = k; /* add k as 1st element in of Ei */
len[i] = pn - p1 + 1; /* new len of adj. list of node i */
h %= n; /* finalize hash of i */
next[i] = hhead[h]; /* place i in hash bucket */
hhead[h] = i;
last[i] = h; /* save hash of i in last[i] */
}
} /* scan2 is done */
degree[k] = dk; /* finalize |Lk| */
lemax = CS_MAX(lemax, dk);
mark = cs_wclear(mark + lemax, lemax, w, n); /* clear w */
/* --- Supernode detection ------------------------------------------ */
for(pk = pk1; pk < pk2; pk++) {
i = Ci[pk];
if(nv[i] >= 0) continue; /* skip if i is dead */
h = last[i]; /* scan hash bucket of node i */
i = hhead[h];
hhead[h] = -1; /* hash bucket will be empty */
for(; i != -1 && next[i] != -1; i = next[i], mark++) {
ln = len[i];
eln = elen[i];
for(p = Cp[i] + 1; p <= Cp[i] + ln - 1; p++)
w[Ci[p]] = mark;
jlast = i;
for(j = next[i]; j != -1;) /* compare i with all j */
{
ok = (len[j] == ln) && (elen[j] == eln);
for(p = Cp[j] + 1; ok && p <= Cp[j] + ln - 1; p++) {
if(w[Ci[p]] != mark) ok = 0; /* compare i and j*/
}
if(ok) /* i and j are identical */
{
Cp[j] = CS_FLIP(i); /* absorb j into i */
nv[i] += nv[j];
nv[j] = 0;
elen[j] = -1; /* node j is dead */
j = next[j]; /* delete j from hash bucket */
next[jlast] = j;
} else {
jlast = j; /* j and i are different */
j = next[j];
}
}
}
}
/* --- Finalize new element------------------------------------------ */
for(p = pk1, pk = pk1; pk < pk2; pk++) /* finalize Lk */
{
i = Ci[pk];
if((nvi = -nv[i]) <= 0) continue; /* skip if i is dead */
nv[i] = nvi; /* restore nv[i] */
d = degree[i] + dk - nvi; /* compute external degree(i) */
d = CS_MIN(d, n - nel - nvi);
if(head[d] != -1) last[head[d]] = i;
next[i] = head[d]; /* put i back in degree list */
last[i] = -1;
head[d] = i;
mindeg = CS_MIN(mindeg, d); /* find new minimum degree */
degree[i] = d;
Ci[p++] = i; /* place i in Lk */
}
nv[k] = nvk; /* # nodes absorbed into k */
if((len[k] = p - pk1) == 0) /* length of adj list of element k*/
{
Cp[k] = -1; /* k is a root of the tree */
w[k] = 0; /* k is now a dead element */
}
if(elenk != 0) cnz = p; /* free unused space in Lk */
}
/* --- Postordering ----------------------------------------------------- */
for(i = 0; i < n; i++)
Cp[i] = CS_FLIP(Cp[i]); /* fix assembly tree */
for(j = 0; j <= n; j++)
head[j] = -1;
for(j = n; j >= 0; j--) /* place unordered nodes in lists */
{
if(nv[j] > 0) continue; /* skip if j is an element */
next[j] = head[Cp[j]]; /* place j in list of its parent */
head[Cp[j]] = j;
}
for(e = n; e >= 0; e--) /* place elements in lists */
{
if(nv[e] <= 0) continue; /* skip unless e is an element */
if(Cp[e] != -1) {
next[e] = head[Cp[e]]; /* place e in list of its parent */
head[Cp[e]] = e;
}
}
for(k = 0, i = 0; i <= n; i++) /* postorder the assembly tree */
{
if(Cp[i] == -1) k = cs_tdfs(i, k, head, next, P, w);
}
return (cs_idone(P, C, ww, 1));
}
/* compute nonzero pattern of L(k,:) */
static int cs_ereach(const cs* A, int k, const int* parent, int* s, int* w,
double* x, int top) {
int i, p, len, *Ap = A->p, *Ai = A->i;
double* Ax = A->x;
for(p = Ap[k]; p < Ap[k + 1]; p++) /* get pattern of L(k,:) */
{
i = Ai[p]; /* A(i,k) is nonzero */
if(i > k) continue; /* only use upper triangular part of A */
x[i] = Ax[p]; /* x(i) = A(i,k) */
for(len = 0; w[i] != k; i = parent[i]) /* traverse up etree */
{
s[len++] = i; /* L(k,i) is nonzero */
w[i] = k; /* mark i as visited */
}
while(len > 0)
s[--top] = s[--len]; /* push path onto stack */
}
return (top); /* s [top..n-1] contains pattern of L(k,:)*/
}
/* L = chol (A, [Pinv parent cp]), Pinv is optional */
csn* cs_chol(const cs* A, const css* S) {
double d, lki, *Lx, *x;
int top, i, p, k, n, *Li, *Lp, *cp, *Pinv, *w, *s, *c, *parent;
cs *L, *C, *E;
csn* N;
if(!A || !S || !S->cp || !S->parent) return (NULL); /* check inputs */
n = A->n;
N = cs_calloc(1, sizeof(csn));
w = cs_malloc(3 * n, sizeof(int));
s = w + n, c = w + 2 * n;
x = cs_malloc(n, sizeof(double));
cp = S->cp;
Pinv = S->Pinv;
parent = S->parent;
C = Pinv ? cs_symperm(A, Pinv, 1) : ((cs*)A);
E = Pinv ? C : NULL;
if(!N || !w || !x || !C) return (cs_ndone(N, E, w, x, 0));
N->L = L = cs_spalloc(n, n, cp[n], 1, 0);
if(!L) return (cs_ndone(N, E, w, x, 0));
Lp = L->p;
Li = L->i;
Lx = L->x;
for(k = 0; k < n; k++) {
/* --- Nonzero pattern of L(k,:) ------------------------------------ */
Lp[k] = c[k] = cp[k]; /* column k of L starts here */
x[k] = 0; /* x (0:k) is now zero */
w[k] = k; /* mark node k as visited */
top = cs_ereach(C, k, parent, s, w, x, n); /* find row k of L*/
d = x[k]; /* d = C(k,k) */
x[k] = 0; /* clear workspace for k+1st iteration */
/* --- Triangular solve --------------------------------------------- */
for(; top < n; top++) /* solve L(0:k-1,0:k-1) * x = C(:,k) */
{
i = s[top]; /* s [top..n-1] is pattern of L(k,:) */
lki = x[i] / Lx[Lp[i]]; /* L(k,i) = x (i) / L(i,i) */
x[i] = 0; /* clear workspace for k+1st iteration */
for(p = Lp[i] + 1; p < c[i]; p++) {
x[Li[p]] -= Lx[p] * lki;
}
d -= lki * lki; /* d = d - L(k,i)*L(k,i) */
p = c[i]++;
Li[p] = k; /* store L(k,i) in column i */
Lx[p] = lki;
}
/* --- Compute L(k,k) ----------------------------------------------- */
if(d <= 0) return (cs_ndone(N, E, w, x, 0)); /* not pos def */
p = c[k]++;
Li[p] = k; /* store L(k,k) = sqrt (d) in column k */
Lx[p] = sqrt(d);
}
Lp[n] = cp[n]; /* finalize L */
return (cs_ndone(N, E, w, x, 1)); /* success: free E,w,x; return N */
}
/* x=A\b where A is symmetric positive definite; b overwritten with solution */
int cs_cholsol(const cs* A, double* b, int order) {
double* x;
css* S;
csn* N;
int n, ok;
if(!A || !b) return (0); /* check inputs */
n = A->n;
S = cs_schol(A, order); /* ordering and symbolic analysis */
N = cs_chol(A, S); /* numeric Cholesky factorization */
x = cs_malloc(n, sizeof(double));
ok = (S && N && x);
if(ok) {
cs_ipvec(n, S->Pinv, b, x); /* x = P*b */
cs_lsolve(N->L, x); /* x = L\x */
cs_ltsolve(N->L, x); /* x = L'\x */
cs_pvec(n, S->Pinv, x, b); /* b = P'*x */
}
cs_free(x);
cs_sfree(S);
cs_nfree(N);
return (ok);
}
/* process edge (j,i) of the matrix */
static void cs_cedge(int j, int i, const int* first, int* maxfirst, int* delta,
int* prevleaf, int* ancestor) {
int q, s, sparent, jprev;
if(i <= j || first[j] <= maxfirst[i]) return;
maxfirst[i] = first[j]; /* update max first[j] seen so far */
jprev = prevleaf[i]; /* j is a leaf of the ith subtree */
delta[j]++; /* A(i,j) is in the skeleton matrix */
if(jprev != -1) {
/* q = least common ancestor of jprev and j */
for(q = jprev; q != ancestor[q]; q = ancestor[q])
;
for(s = jprev; s != q; s = sparent) {
sparent = ancestor[s]; /* path compression */
ancestor[s] = q;
}
delta[q]--; /* decrement to account for overlap in q */
}
prevleaf[i] = j; /* j is now previous leaf of ith subtree */
}
/* colcount = column counts of LL'=A or LL'=A'A, given parent & post ordering */
int* cs_counts(const cs* A, const int* parent, const int* post, int ata) {
int i, j, k, p, n, m, ii, s, *ATp, *ATi, *maxfirst, *prevleaf, *ancestor,
*head = NULL, *next = NULL, *colcount, *w, *first, *delta;
cs* AT;
if(!A || !parent || !post) return (NULL); /* check inputs */
m = A->m;
n = A->n;
s = 4 * n + (ata ? (n + m + 1) : 0);
w = cs_malloc(s, sizeof(int));
first = w + 3 * n; /* get workspace */
ancestor = w;
maxfirst = w + n;
prevleaf = w + 2 * n;
delta = colcount = cs_malloc(n, sizeof(int)); /* allocate result */
AT = cs_transpose(A, 0);
if(!AT || !colcount || !w) return (cs_idone(colcount, AT, w, 1));
for(k = 0; k < s; k++)
w[k] = -1; /* clear workspace w [0..s-1] */
for(k = 0; k < n; k++) /* find first [j] */
{
j = post[k];
delta[j] = (first[j] == -1) ? 1 : 0; /* delta[j]=1 if j is a leaf */
for(; j != -1 && first[j] == -1; j = parent[j])
first[j] = k;
}
ATp = AT->p;
ATi = AT->i;
if(ata) {
head = w + 4 * n;
next = w + 5 * n + 1;
for(k = 0; k < n; k++)
w[post[k]] = k; /* invert post */
for(i = 0; i < m; i++) {
k = n; /* k = least postordered column in row i */
for(p = ATp[i]; p < ATp[i + 1]; p++)
k = CS_MIN(k, w[ATi[p]]);
next[i] = head[k]; /* place row i in link list k */
head[k] = i;
}
}
for(i = 0; i < n; i++)
ancestor[i] = i; /* each node in its own set */
for(k = 0; k < n; k++) {
j = post[k]; /* j is the kth node in postordered etree */
if(parent[j] != -1) delta[parent[j]]--; /* j is not a root */
if(ata) {
for(ii = head[k]; ii != -1; ii = next[ii]) {
for(p = ATp[ii]; p < ATp[ii + 1]; p++)
cs_cedge(j, ATi[p], first, maxfirst, delta, prevleaf,
ancestor);
}
} else {
for(p = ATp[j]; p < ATp[j + 1]; p++)
cs_cedge(j, ATi[p], first, maxfirst, delta, prevleaf, ancestor);
}
if(parent[j] != -1) ancestor[j] = parent[j];
}
for(j = 0; j < n; j++) /* sum up delta's of each child */
{
if(parent[j] != -1) colcount[parent[j]] += colcount[j];
}
return (cs_idone(colcount, AT, w, 1)); /* success: free workspace */
}
/* p [0..n] = cumulative sum of c [0..n-1], and then copy p [0..n-1] into c */
int cs_cumsum(int* p, int* c, int n) {
int i, nz = 0;
if(!p || !c) return (-1); /* check inputs */
for(i = 0; i < n; i++) {
p[i] = nz;
nz += c[i];
c[i] = p[i];
}
p[n] = nz;
return (nz); /* return sum (c [0..n-1]) */
}
/* depth-first-search of the graph of a matrix, starting at node j */
int cs_dfs(int j, cs* L, int top, int* xi, int* pstack, const int* Pinv) {
int i, p, p2, done, jnew, head = 0, *Lp, *Li;
if(!L || !xi || !pstack) return (-1);
Lp = L->p;
Li = L->i;
xi[0] = j; /* initialize the recursion stack */
while(head >= 0) {
j = xi[head]; /* get j from the top of the recursion stack */
jnew = Pinv ? (Pinv[j]) : j;
if(!CS_MARKED(Lp, j)) {
CS_MARK(Lp, j); /* mark node j as visited */
pstack[head] = (jnew < 0) ? 0 : CS_UNFLIP(Lp[jnew]);
}
done = 1; /* node j done if no unvisited neighbors */
p2 = (jnew < 0) ? 0 : CS_UNFLIP(Lp[jnew + 1]);
for(p = pstack[head]; p < p2; p++) /* examine all neighbors of j */
{
i = Li[p]; /* consider neighbor node i */
if(CS_MARKED(Lp, i)) continue; /* skip visited node i */
pstack[head] = p; /* pause depth-first search of node j */
xi[++head] = i; /* start dfs at node i */
done = 0; /* node j is not done */
break; /* break, to start dfs (i) */
}
if(done) /* depth-first search at node j is done */
{
head--; /* remove j from the recursion stack */
xi[--top] = j; /* and place in the output stack */
}
}
return (top);
}
/* breadth-first search for coarse decomposition (C0,C1,R1 or R0,R3,C3) */
static int cs_bfs(const cs* A, int n, int* wi, int* wj, int* queue,
const int* imatch, const int* jmatch, int mark) {
int *Ap, *Ai, head = 0, tail = 0, j, i, p, j2;
cs* C;
for(j = 0; j < n; j++) /* place all unmatched nodes in queue */
{
if(imatch[j] >= 0) continue; /* skip j if matched */
wj[j] = 0; /* j in set C0 (R0 if transpose) */
queue[tail++] = j; /* place unmatched col j in queue */
}
if(tail == 0) return (1); /* quick return if no unmatched nodes */
C = (mark == 1) ? ((cs*)A) : cs_transpose(A, 0);
if(!C) return (0); /* bfs of C=A' to find R0,R3,C3 */
Ap = C->p;
Ai = C->i;
while(head < tail) /* while queue is not empty */
{
j = queue[head++]; /* get the head of the queue */
for(p = Ap[j]; p < Ap[j + 1]; p++) {
i = Ai[p];
if(wi[i] >= 0) continue; /* skip if i is marked */
wi[i] = mark; /* i in set R1 (C3 if transpose) */
j2 = jmatch[i]; /* traverse alternating path to j2 */
if(wj[j2] >= 0) continue; /* skip j2 if it is marked */
wj[j2] = mark; /* j2 in set C1 (R3 if transpose) */
queue[tail++] = j2; /* add j2 to queue */
}
}
if(mark != 1) cs_spfree(C); /* free A' if it was created */
return (1);
}
/* collect matched rows and columns into P and Q */
static void cs_matched(int m, const int* wi, const int* jmatch, int* P, int* Q,
int* cc, int* rr, int set, int mark) {
int kc = cc[set], i;
int kr = rr[set - 1];
for(i = 0; i < m; i++) {
if(wi[i] != mark) continue; /* skip if i is not in R set */
P[kr++] = i;
Q[kc++] = jmatch[i];
}
cc[set + 1] = kc;
rr[set] = kr;
}
static void cs_unmatched(int m, const int* wi, int* P, int* rr, int set)
/*
Purpose:
CS_UNMATCHED collects unmatched rows into the permutation vector P.
*/
{
int i, kr = rr[set];
for(i = 0; i < m; i++)
if(wi[i] == 0) P[kr++] = i;
rr[set + 1] = kr;
}
/* return 1 if row i is in R2 */
static int cs_rprune(int i, int j, double aij, void* other) {
int* rr = (int*)other;
return (i >= rr[1] && i < rr[2]);
}
/* Given A, find coarse dmperm */
csd* cs_dmperm(const cs* A) {
int m, n, i, j, k, p, cnz, nc, *jmatch, *imatch, *wi, *wj, *Pinv, *Cp, *Ci,
*Ps, *Rs, nb1, nb2, *P, *Q, *cc, *rr, *R, *S, ok;
cs* C;
csd *D, *scc;
/* --- Maximum matching ------------------------------------------------- */
if(!A) return (NULL); /* check inputs */
m = A->m;
n = A->n;
D = cs_dalloc(m, n); /* allocate result */
if(!D) return (NULL);
P = D->P;
Q = D->Q;
R = D->R;
S = D->S;
cc = D->cc;
rr = D->rr;
jmatch = cs_maxtrans(A); /* max transversal */
imatch = jmatch + m; /* imatch = inverse of jmatch */
if(!jmatch) return (cs_ddone(D, NULL, jmatch, 0));
/* --- Coarse decomposition --------------------------------------------- */
wi = R;
wj = S; /* use R and S as workspace */
for(j = 0; j < n; j++)
wj[j] = -1; /* unmark all cols for bfs */
for(i = 0; i < m; i++)
wi[i] = -1; /* unmark all rows for bfs */
cs_bfs(A, n, wi, wj, Q, imatch, jmatch, 1); /* find C0, C1, R1 */
ok = cs_bfs(A, m, wj, wi, P, jmatch, imatch, 3); /* find R0, R3, C3 */
if(!ok) return (cs_ddone(D, NULL, jmatch, 0));
cs_unmatched(n, wj, Q, cc, 0); /* unmatched set C0 */
cs_matched(m, wi, jmatch, P, Q, cc, rr, 1, 1); /* set R1 and C1 */
cs_matched(m, wi, jmatch, P, Q, cc, rr, 2, -1); /* set R2 and C2 */
cs_matched(m, wi, jmatch, P, Q, cc, rr, 3, 3); /* set R3 and C3 */
cs_unmatched(m, wi, P, rr, 3); /* unmatched set R0 */
cs_free(jmatch);
/* --- Fine decomposition ----------------------------------------------- */
Pinv = cs_pinv(P, m); /* Pinv=P' */
if(!Pinv) return (cs_ddone(D, NULL, NULL, 0));
C = cs_permute(A, Pinv, Q, 0); /* C=A(P,Q) (it will hold A(R2,C2)) */
cs_free(Pinv);
if(!C) return (cs_ddone(D, NULL, NULL, 0));
Cp = C->p;
Ci = C->i;
nc = cc[3] - cc[2]; /* delete cols C0, C1, and C3 from C */
if(cc[2] > 0)
for(j = cc[2]; j <= cc[3]; j++)
Cp[j - cc[2]] = Cp[j];
C->n = nc;
if(rr[2] - rr[1] < m) /* delete rows R0, R1, and R3 from C */
{
cs_fkeep(C, cs_rprune, rr);
cnz = Cp[nc];
if(rr[1] > 0)
for(p = 0; p < cnz; p++)
Ci[p] -= rr[1];
}
C->m = nc;
scc = cs_scc(C); /* find strongly-connected components of C*/
if(!scc) return (cs_ddone(D, C, NULL, 0));
/* --- Combine coarse and fine decompositions --------------------------- */
Ps = scc->P; /* C(Ps,Ps) is the permuted matrix */
Rs = scc->R; /* kth block is Rs[k]..Rs[k+1]-1 */
nb1 = scc->nb; /* # of blocks of A(*/
for(k = 0; k < nc; k++)
wj[k] = Q[Ps[k] + cc[2]]; /* combine */
for(k = 0; k < nc; k++)
Q[k + cc[2]] = wj[k];
for(k = 0; k < nc; k++)
wi[k] = P[Ps[k] + rr[1]];
for(k = 0; k < nc; k++)
P[k + rr[1]] = wi[k];
nb2 = 0; /* create the fine block partitions */
R[0] = 0;
S[0] = 0;
if(cc[2] > 0) nb2++; /* leading coarse block A (R1, [C0 C1]) */
for(k = 0; k < nb1; k++) /* coarse block A (R2,C2) */
{
R[nb2] = Rs[k] + rr[1]; /* A (R2,C2) splits into nb1 fine blocks */
S[nb2] = Rs[k] + cc[2];
nb2++;
}
if(rr[2] < m) {
R[nb2] = rr[2]; /* trailing coarse block A ([R3 R0], C3) */
S[nb2] = cc[3];
nb2++;
}
R[nb2] = m;
S[nb2] = n;
D->nb = nb2;
cs_dfree(scc);
return (cs_ddone(D, C, NULL, 1));
}
static int cs_tol(int i, int j, double aij, void* tol) {
return (fabs(aij) > *((double*)tol));
}
int cs_droptol(cs* A, double tol) {
return (cs_fkeep(A, &cs_tol, &tol)); /* keep all large entries */
}
static int cs_nonzero(int i, int j, double aij, void* other) {
return (aij != 0);
}
int cs_dropzeros(cs* A) {
return (cs_fkeep(A, &cs_nonzero, NULL)); /* keep all nonzero entries */
}
int cs_dupl(cs* A)
/*
Purpose:
CS_DUPL removes duplicate entries from A.
Reference:
Timothy Davis,
Direct Methods for Sparse Linear Systems,
SIAM, Philadelphia, 2006.
*/
{
int i, j, p, q, nz = 0, n, m, *Ap, *Ai, *w;
double* Ax;
if(!A) return (0); /* check inputs */
m = A->m;
n = A->n;
Ap = A->p;
Ai = A->i;
Ax = A->x;
w = cs_malloc(m, sizeof(int)); /* get workspace */
if(!w) return (0); /* out of memory */
for(i = 0; i < m; i++)
w[i] = -1; /* row i not yet seen */
for(j = 0; j < n; j++) {
q = nz; /* column j will start at q */
for(p = Ap[j]; p < Ap[j + 1]; p++) {
i = Ai[p]; /* A(i,j) is nonzero */
if(w[i] >= q) {
Ax[w[i]] += Ax[p]; /* A(i,j) is a duplicate */
} else {
w[i] = nz; /* record where row i occurs */
Ai[nz] = i; /* keep A(i,j) */
Ax[nz++] = Ax[p];
}
}
Ap[j] = q; /* record start of column j */
}
Ap[n] = nz; /* finalize A */
cs_free(w); /* free workspace */
return (cs_sprealloc(A, 0)); /* remove extra space from A */
}
/* add an entry to a triplet matrix; return 1 if ok, 0 otherwise */
int cs_entry(cs* T, int i, int j, double x) {
if(!T || (T->nz >= T->nzmax && !cs_sprealloc(T, 2 * (T->nzmax))))
return (0);
if(T->x) T->x[T->nz] = x;
T->i[T->nz] = i;
T->p[T->nz++] = j;
T->m = CS_MAX(T->m, i + 1);
T->n = CS_MAX(T->n, j + 1);
return (1);
}
/* compute the etree of A (using triu(A), or A'A without forming A'A */
int* cs_etree(const cs* A, int ata) {
int i, k, p, m, n, inext, *Ap, *Ai, *w, *parent, *ancestor, *prev;
if(!A) return (NULL); /* check inputs */
m = A->m;
n = A->n;
Ap = A->p;
Ai = A->i;
parent = cs_malloc(n, sizeof(int));
w = cs_malloc(n + (ata ? m : 0), sizeof(int));
ancestor = w;
prev = w + n;
if(!w || !parent) return (cs_idone(parent, NULL, w, 0));
if(ata)
for(i = 0; i < m; i++)
prev[i] = -1;
for(k = 0; k < n; k++) {
parent[k] = -1; /* node k has no parent yet */
ancestor[k] = -1; /* nor does k have an ancestor */
for(p = Ap[k]; p < Ap[k + 1]; p++) {
i = ata ? (prev[Ai[p]]) : (Ai[p]);
for(; i != -1 && i < k; i = inext) /* traverse from i to k */
{
inext = ancestor[i]; /* inext = ancestor of i */
ancestor[i] = k; /* path compression */
if(inext == -1) parent[i] = k; /* no anc., parent is k */
}
if(ata) prev[Ai[p]] = k;
}
}
return (cs_idone(parent, NULL, w, 1));
}
/* drop entries for which fkeep(A(i,j)) is false; return nz if OK, else -1 */
int cs_fkeep(cs* A, int (*fkeep)(int, int, double, void*), void* other) {
int j, p, nz = 0, n, *Ap, *Ai;
double* Ax;
if(!A || !fkeep) return (-1); /* check inputs */
n = A->n;
Ap = A->p;
Ai = A->i;
Ax = A->x;
for(j = 0; j < n; j++) {
p = Ap[j]; /* get current location of col j */
Ap[j] = nz; /* record new location of col j */
for(; p < Ap[j + 1]; p++) {
if(fkeep(Ai[p], j, Ax ? Ax[p] : 1, other)) {
if(Ax) Ax[nz] = Ax[p]; /* keep A(i,j) */
Ai[nz++] = Ai[p];
}
}
}
return (Ap[n] = nz); /* finalize A and return nnz(A) */
}
/* y = A*x+y */
int cs_gaxpy(const cs* A, const double* x, double* y) {
int p, j, n, *Ap, *Ai;
double* Ax;
if(!A || !x || !y) return (0); /* check inputs */
n = A->n;
Ap = A->p;
Ai = A->i;
Ax = A->x;
for(j = 0; j < n; j++) {
for(p = Ap[j]; p < Ap[j + 1]; p++) {
y[Ai[p]] += Ax[p] * x[j];
}
}
return (1);
}
/* apply the ith Householder vector to x */
int cs_happly(const cs* V, int i, double beta, double* x) {
int p, *Vp, *Vi;
double *Vx, tau = 0;
if(!V || !x) return (0); /* check inputs */
Vp = V->p;
Vi = V->i;
Vx = V->x;
for(p = Vp[i]; p < Vp[i + 1]; p++) /* tau = v'*x */
{
tau += Vx[p] * x[Vi[p]];
}
tau *= beta; /* tau = beta*(v'*x) */
for(p = Vp[i]; p < Vp[i + 1]; p++) /* x = x - v*tau */
{
x[Vi[p]] -= Vx[p] * tau;
}
return (1);
}
/* create a Householder reflection [v,beta,s]=house(x), overwrite x with v,
* where (I-beta*v*v')*x = s*x. See Algo 5.1.1, Golub & Van Loan, 3rd ed. */
double cs_house(double* x, double* beta, int n) {
double s, sigma = 0;
int i;
if(!x || !beta) return (-1); /* check inputs */
for(i = 1; i < n; i++)
sigma += x[i] * x[i];
if(sigma == 0) {
s = fabs(x[0]); /* s = |x(0)| */
(*beta) = (x[0] <= 0) ? 2 : 0;
x[0] = 1;
} else {
s = sqrt(x[0] * x[0] + sigma); /* s = norm (x) */
x[0] = (x[0] <= 0) ? (x[0] - s) : (-sigma / (x[0] + s));
(*beta) = -1. / (s * x[0]);
}
return (s);
}
/* x(P) = b, for dense vectors x and b; P=NULL denotes identity */