1 #include "cs.h"
2 /* compute nnz(V) = S->lnz, S->pinv, S->leftmost, S->m2 from A and S->parent */
cs_vcount(const cs * A,css * S)3 static CS_INT cs_vcount (const cs *A, css *S)
4 {
5     CS_INT i, k, p, pa, n = A->n, m = A->m, *Ap = A->p, *Ai = A->i, *next, *head,
6         *tail, *nque, *pinv, *leftmost, *w, *parent = S->parent ;
7     S->pinv = pinv = cs_malloc (m+n, sizeof (CS_INT)) ;        /* allocate pinv, */
8     S->leftmost = leftmost = cs_malloc (m, sizeof (CS_INT)) ;  /* and leftmost */
9     w = cs_malloc (m+3*n, sizeof (CS_INT)) ;   /* get workspace */
10     if (!pinv || !w || !leftmost)
11     {
12         cs_free (w) ;                       /* pinv and leftmost freed later */
13         return (0) ;                        /* out of memory */
14     }
15     next = w ; head = w + m ; tail = w + m + n ; nque = w + m + 2*n ;
16     for (k = 0 ; k < n ; k++) head [k] = -1 ;   /* queue k is empty */
17     for (k = 0 ; k < n ; k++) tail [k] = -1 ;
18     for (k = 0 ; k < n ; k++) nque [k] = 0 ;
19     for (i = 0 ; i < m ; i++) leftmost [i] = -1 ;
20     for (k = n-1 ; k >= 0 ; k--)
21     {
22         for (p = Ap [k] ; p < Ap [k+1] ; p++)
23         {
24             leftmost [Ai [p]] = k ;         /* leftmost[i] = min(find(A(i,:)))*/
25         }
26     }
27     for (i = m-1 ; i >= 0 ; i--)            /* scan rows in reverse order */
28     {
29         pinv [i] = -1 ;                     /* row i is not yet ordered */
30         k = leftmost [i] ;
31         if (k == -1) continue ;             /* row i is empty */
32         if (nque [k]++ == 0) tail [k] = i ; /* first row in queue k */
33         next [i] = head [k] ;               /* put i at head of queue k */
34         head [k] = i ;
35     }
36     S->lnz = 0 ;
37     S->m2 = m ;
38     for (k = 0 ; k < n ; k++)               /* find row permutation and nnz(V)*/
39     {
40         i = head [k] ;                      /* remove row i from queue k */
41         S->lnz++ ;                          /* count V(k,k) as nonzero */
42         if (i < 0) i = S->m2++ ;            /* add a fictitious row */
43         pinv [i] = k ;                      /* associate row i with V(:,k) */
44         if (--nque [k] <= 0) continue ;     /* skip if V(k+1:m,k) is empty */
45         S->lnz += nque [k] ;                /* nque [k] is nnz (V(k+1:m,k)) */
46         if ((pa = parent [k]) != -1)        /* move all rows to parent of k */
47         {
48             if (nque [pa] == 0) tail [pa] = tail [k] ;
49             next [tail [k]] = head [pa] ;
50             head [pa] = next [i] ;
51             nque [pa] += nque [k] ;
52         }
53     }
54     for (i = 0 ; i < m ; i++) if (pinv [i] < 0) pinv [i] = k++ ;
55     cs_free (w) ;
56     return (1) ;
57 }
58 
59 /* symbolic ordering and analysis for QR or LU */
cs_sqr(CS_INT order,const cs * A,CS_INT qr)60 css *cs_sqr (CS_INT order, const cs *A, CS_INT qr)
61 {
62     CS_INT n, k, ok = 1, *post ;
63     css *S ;
64     if (!CS_CSC (A)) return (NULL) ;        /* check inputs */
65     n = A->n ;
66     S = cs_calloc (1, sizeof (css)) ;       /* allocate result S */
67     if (!S) return (NULL) ;                 /* out of memory */
68     S->q = cs_amd (order, A) ;              /* fill-reducing ordering */
69     if (order && !S->q) return (cs_sfree (S)) ;
70     if (qr)                                 /* QR symbolic analysis */
71     {
72         cs *C = order ? cs_permute (A, NULL, S->q, 0) : ((cs *) A) ;
73         S->parent = cs_etree (C, 1) ;       /* etree of C'*C, where C=A(:,q) */
74         post = cs_post (S->parent, n) ;
75         S->cp = cs_counts (C, S->parent, post, 1) ;  /* col counts chol(C'*C) */
76         cs_free (post) ;
77         ok = C && S->parent && S->cp && cs_vcount (C, S) ;
78         if (ok) for (S->unz = 0, k = 0 ; k < n ; k++) S->unz += S->cp [k] ;
79         if (order) cs_spfree (C) ;
80     }
81     else
82     {
83         S->unz = 4*(A->p [n]) + n ;         /* for LU factorization only, */
84         S->lnz = S->unz ;                   /* guess nnz(L) and nnz(U) */
85     }
86     return (ok ? S : cs_sfree (S)) ;        /* return result S */
87 }
88