cs_qrsol.c
Go to the documentation of this file.
1 #include "cs.h"
2 /* x=A\b where A can be rectangular; b overwritten with solution */
3 int cs_qrsol (int order, const cs *A, double *b)
4 {
5  double *x ;
6  css *S ;
7  csn *N ;
8  cs *AT = NULL ;
9  int k, m, n, ok ;
10  if (!CS_CSC (A) || !b) return (0) ; /* check inputs */
11  n = A->n ;
12  m = A->m ;
13  if (m >= n)
14  {
15  S = cs_sqr (order, A, 1) ; /* ordering and symbolic analysis */
16  N = cs_qr (A, S) ; /* numeric QR factorization */
17  x = (double*) cs_calloc (S ? S->m2 : 1, sizeof (double)) ; /* get workspace */
18  ok = (S && N && x) ;
19  if (ok)
20  {
21  cs_ipvec (S->pinv, b, x, m) ; /* x(0:m-1) = b(p(0:m-1) */
22  for (k = 0 ; k < n ; k++) /* apply Householder refl. to x */
23  {
24  cs_happly (N->L, k, N->B [k], x) ;
25  }
26  cs_usolve (N->U, x) ; /* x = R\x */
27  cs_ipvec (S->q, x, b, n) ; /* b(q(0:n-1)) = x(0:n-1) */
28  }
29  }
30  else
31  {
32  AT = cs_transpose (A, 1) ; /* Ax=b is underdetermined */
33  S = cs_sqr (order, AT, 1) ; /* ordering and symbolic analysis */
34  N = cs_qr (AT, S) ; /* numeric QR factorization of A' */
35  x = (double*) cs_calloc (S ? S->m2 : 1, sizeof (double)) ; /* get workspace */
36  ok = (AT && S && N && x) ;
37  if (ok)
38  {
39  cs_pvec (S->q, b, x, m) ; /* x(q(0:m-1)) = b(0:m-1) */
40  cs_utsolve (N->U, x) ; /* x = R'\x */
41  for (k = m-1 ; k >= 0 ; k--) /* apply Householder refl. to x */
42  {
43  cs_happly (N->L, k, N->B [k], x) ;
44  }
45  cs_pvec (S->pinv, x, b, n) ; /* b(0:n-1) = x(p(0:n-1)) */
46  }
47  }
48  cs_free (x) ;
49  cs_sfree (S) ;
50  cs_nfree (N) ;
51  cs_spfree (AT) ;
52  return (ok) ;
53 }
#define N
css * cs_sqr(int order, const cs *A, int qr)
Definition: cs_sqr.c:60
double * B
Definition: cs.h:67
csn * cs_nfree(csn *N)
Definition: cs_util.c:42
cs * cs_spfree(cs *A)
Definition: cs_util.c:32
csn * cs_qr(const cs *A, const css *S)
Definition: cs_qr.c:3
int n
Definition: cs.h:20
cs * U
Definition: cs.h:65
void * cs_free(void *p)
Definition: cs_malloc.c:22
Definition: cs.h:62
int cs_ipvec(const int *p, const double *b, double *x, int n)
Definition: cs_ipvec.c:3
cs * cs_transpose(const cs *A, int values)
Definition: cs_transpose.c:3
int cs_usolve(const cs *U, double *x)
Definition: cs_usolve.c:3
int cs_happly(const cs *V, int i, double beta, double *x)
Definition: cs_happly.c:3
int cs_utsolve(const cs *U, double *x)
Definition: cs_utsolve.c:3
#define CS_CSC(A)
Definition: cs.h:140
Definition: cs.h:16
void * cs_calloc(int n, size_t size)
Definition: cs_malloc.c:16
int cs_pvec(const int *p, const double *b, double *x, int n)
Definition: cs_pvec.c:3
int * pinv
Definition: cs.h:52
int cs_qrsol(int order, const cs *A, double *b)
Definition: cs_qrsol.c:3
int * q
Definition: cs.h:53
int m2
Definition: cs.h:57
css * cs_sfree(css *S)
Definition: cs_util.c:53
int m
Definition: cs.h:19
cs * L
Definition: cs.h:64
Definition: cs.h:50


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:31