EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
cs_qr.c
Go to the documentation of this file.
1 #include "cs.h"
2 /* sparse QR factorization [V,beta,pinv,R] = qr (A) */
3 csn *cs_qr (const cs *A, const css *S)
4 {
5  CS_ENTRY *Rx, *Vx, *Ax, *x ;
6  double *Beta ;
7  CS_INT i, k, p,/* m,*/ n, vnz, p1, top, m2, len, col, rnz, *s, *leftmost, *Ap, *Ai,
8  *parent, *Rp, *Ri, *Vp, *Vi, *w, *pinv, *q ;
9  cs *R, *V ;
10  csn *N ;
11  if (!CS_CSC (A) || !S) return (NULL) ;
12  /*m = A->m ; */
13  n = A->n ; Ap = A->p ; Ai = A->i ; Ax = A->x ;
14  q = S->q ; parent = S->parent ; pinv = S->pinv ; m2 = S->m2 ;
15  vnz = S->lnz ; rnz = S->unz ; leftmost = S->leftmost ;
16  w = cs_malloc (m2+n, sizeof (CS_INT)) ; /* get CS_INT workspace */
17  x = cs_malloc (m2, sizeof (CS_ENTRY)) ; /* get CS_ENTRY workspace */
18  N = cs_calloc (1, sizeof (csn)) ; /* allocate result */
19  if (!w || !x || !N) return (cs_ndone (N, NULL, w, x, 0)) ;
20  s = w + m2 ; /* s is size n */
21  for (k = 0 ; k < m2 ; k++) x [k] = 0 ; /* clear workspace x */
22  N->L = V = cs_spalloc (m2, n, vnz, 1, 0) ; /* allocate result V */
23  N->U = R = cs_spalloc (m2, n, rnz, 1, 0) ; /* allocate result R */
24  N->B = Beta = cs_malloc (n, sizeof (double)) ; /* allocate result Beta */
25  if (!R || !V || !Beta) return (cs_ndone (N, NULL, w, x, 0)) ;
26  Rp = R->p ; Ri = R->i ; Rx = R->x ;
27  Vp = V->p ; Vi = V->i ; Vx = V->x ;
28  for (i = 0 ; i < m2 ; i++) w [i] = -1 ; /* clear w, to mark nodes */
29  rnz = 0 ; vnz = 0 ;
30  for (k = 0 ; k < n ; k++) /* compute V and R */
31  {
32  Rp [k] = rnz ; /* R(:,k) starts here */
33  Vp [k] = p1 = vnz ; /* V(:,k) starts here */
34  w [k] = k ; /* add V(k,k) to pattern of V */
35  Vi [vnz++] = k ;
36  top = n ;
37  col = q ? q [k] : k ;
38  for (p = Ap [col] ; p < Ap [col+1] ; p++) /* find R(:,k) pattern */
39  {
40  i = leftmost [Ai [p]] ; /* i = min(find(A(i,q))) */
41  for (len = 0 ; w [i] != k ; i = parent [i]) /* traverse up to k */
42  {
43  s [len++] = i ;
44  w [i] = k ;
45  }
46  while (len > 0) s [--top] = s [--len] ; /* push path on stack */
47  i = pinv [Ai [p]] ; /* i = permuted row of A(:,col) */
48  x [i] = Ax [p] ; /* x (i) = A(:,col) */
49  if (i > k && w [i] < k) /* pattern of V(:,k) = x (k+1:m) */
50  {
51  Vi [vnz++] = i ; /* add i to pattern of V(:,k) */
52  w [i] = k ;
53  }
54  }
55  for (p = top ; p < n ; p++) /* for each i in pattern of R(:,k) */
56  {
57  i = s [p] ; /* R(i,k) is nonzero */
58  cs_happly (V, i, Beta [i], x) ; /* apply (V(i),Beta(i)) to x */
59  Ri [rnz] = i ; /* R(i,k) = x(i) */
60  Rx [rnz++] = x [i] ;
61  x [i] = 0 ;
62  if (parent [i] == k) vnz = cs_scatter (V, i, 0, w, NULL, k, V, vnz);
63  }
64  for (p = p1 ; p < vnz ; p++) /* gather V(:,k) = x */
65  {
66  Vx [p] = x [Vi [p]] ;
67  x [Vi [p]] = 0 ;
68  }
69  Ri [rnz] = k ; /* R(k,k) = norm (x) */
70  Rx [rnz++] = cs_house (Vx+p1, Beta+k, vnz-p1) ; /* [v,beta]=house(x) */
71  }
72  Rp [n] = rnz ; /* finalize R */
73  Vp [n] = vnz ; /* finalize V */
74  return (cs_ndone (N, NULL, w, x, 1)) ; /* success */
75 }
csn * cs_ndone(csn *N, cs *C, void *w, void *x, CS_INT ok)
Definition: cs_util.c:106
#define cs
Definition: cs.h:637
CS_ENTRY cs_house(CS_ENTRY *x, double *beta, CS_INT n)
Definition: cs_house.c:6
#define css
Definition: cs.h:688
CS_INT cs_happly(const cs *V, CS_INT i, double beta, CS_ENTRY *x)
Definition: cs_happly.c:3
#define CS_ENTRY
Definition: cs.h:635
cs * cs_spalloc(CS_INT m, CS_INT n, CS_INT nzmax, CS_INT values, CS_INT triplet)
Definition: cs_util.c:3
#define csn
Definition: cs.h:689
#define CS_CSC(A)
Definition: cs.h:659
void * cs_calloc(CS_INT n, size_t size)
Definition: cs_malloc.c:16
#define CS_INT
Definition: cs.h:627
CS_INT cs_scatter(const cs *A, CS_INT j, CS_ENTRY beta, CS_INT *w, CS_ENTRY *x, CS_INT mark, cs *C, CS_INT nz)
Definition: cs_scatter.c:3
void * cs_malloc(CS_INT n, size_t size)
Definition: cs_malloc.c:10
csn * cs_qr(const cs *A, const css *S)
Definition: cs_qr.c:3