g2o
Loading...
Searching...
No Matches
csparse_extension.cpp
Go to the documentation of this file.
1// CSparse: a Concise Sparse matrix package.
2// Copyright (c) 2005, Timothy A. Davis.
3// http://www.cise.ufl.edu/research/sparse/CSparse
4//
5// --------------------------------------------------------------------------------
6//
7// CSparse is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Lesser General Public
9// License as published by the Free Software Foundation; either
10// version 1.1 of the License, or (at your option) any later version.
11//
12// CSparse is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15// Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public
18// License along with this Module; if not, write to the Free Software
19// Foundation, Inc., 50 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21#include "csparse_extension.h"
22
23#include <cassert>
24#include <iostream>
25
26#include "g2o/stuff/logger.h"
27#include "g2o/stuff/macros.h"
28
29using namespace std;
30
31namespace g2o {
32namespace csparse_extension {
33
38int cs_cholsolsymb(const cs* A, double* b, const css* S, double* x, int* work) {
39 csn* N;
40 int n, ok;
41 if (!CS_CSC(A) || !b || !S || !x) {
42 G2O_DEBUG("{}: No valid input!", __PRETTY_FUNCTION__);
43 assert(0); // get a backtrace in debug mode
44 return (0); /* check inputs */
45 }
46 n = A->n;
47 N = cs_chol_workspace(A, S, work, x); /* numeric Cholesky factorization */
48 if (!N) {
49 G2O_DEBUG("{}: cholesky failed!", __PRETTY_FUNCTION__);
50 }
51 ok = (N != NULL);
52 if (ok) {
53 cs_ipvec(S->pinv, b, x, n); /* x = P*b */
54 cs_lsolve(N->L, x); /* x = L\x */
55 cs_ltsolve(N->L, x); /* x = L'\x */
56 cs_pvec(S->pinv, x, b, n); /* b = P'*x */
57 }
58 cs_nfree(N);
59 return (ok);
60}
61
66/* L = chol (A, [pinv parent cp]), pinv is optional */
67csn* cs_chol_workspace(const cs* A, const css* S, int* cin, double* xin) {
68 double lki, *Lx, *x, *Cx;
69 int i, p, k, n, *Li, *Lp, *cp, *pinv, *s, *c, *parent, *Cp, *Ci;
70 cs *L, *C, *E;
71 csn* N;
72 if (!CS_CSC(A) || !S || !S->cp || !S->parent) return (NULL);
73 n = A->n;
74 N = static_cast<csn*>(cs_calloc(1, sizeof(csn))); /* allocate result */
75 c = cin; /* get int workspace */
76 x = xin; /* get double workspace */
77 cp = S->cp;
78 pinv = S->pinv;
79 parent = S->parent;
80 C = pinv ? cs_symperm(A, pinv, 1) : const_cast<cs*>(A);
81 E = pinv ? C : NULL; /* E is alias for A, or a copy E=A(p,p) */
82 if (!N || !c || !x || !C) return (cs_ndone(N, E, NULL, NULL, 0));
83 s = c + n;
84 Cp = C->p;
85 Ci = C->i;
86 Cx = C->x;
87 N->L = L = cs_spalloc(n, n, cp[n], 1, 0); /* allocate result */
88 if (!L) return (cs_ndone(N, E, NULL, NULL, 0));
89 Lp = L->p;
90 Li = L->i;
91 Lx = L->x;
92 for (k = 0; k < n; k++) Lp[k] = c[k] = cp[k];
93 for (k = 0; k < n; k++) /* compute L(k,:) for L*L' = C */
94 {
95 /* --- Nonzero pattern of L(k,:) ------------------------------------ */
96 int top = cs_ereach(C, k, parent, s, c); /* find pattern of L(k,:) */
97 x[k] = 0; /* x (0:k) is now zero */
98 for (p = Cp[k]; p < Cp[k + 1]; p++) /* x = full(triu(C(:,k))) */
99 {
100 if (Ci[p] <= k) x[Ci[p]] = Cx[p];
101 }
102 double d = x[k]; /* d = C(k,k) */
103 x[k] = 0; /* clear x for k+1st iteration */
104 /* --- Triangular solve --------------------------------------------- */
105 for (; top < n; top++) /* solve L(0:k-1,0:k-1) * x = C(:,k) */
106 {
107 i = s[top]; /* s [top..n-1] is pattern of L(k,:) */
108 lki = x[i] / Lx[Lp[i]]; /* L(k,i) = x (i) / L(i,i) */
109 x[i] = 0; /* clear x for k+1st iteration */
110 for (p = Lp[i] + 1; p < c[i]; p++) {
111 x[Li[p]] -= Lx[p] * lki;
112 }
113 d -= lki * lki; /* d = d - L(k,i)*L(k,i) */
114 p = c[i]++;
115 Li[p] = k; /* store L(k,i) in column i */
116 Lx[p] = lki;
117 }
118 /* --- Compute L(k,k) ----------------------------------------------- */
119 if (d <= 0) return (cs_ndone(N, E, NULL, NULL, 0)); /* not pos def */
120 p = c[k]++;
121 Li[p] = k; /* store L(k,k) = sqrt (d) in column k */
122 Lx[p] = sqrt(d);
123 }
124 Lp[n] = cp[n]; /* finalize L */
125 return (cs_ndone(N, E, NULL, NULL, 1)); /* success: free E,s,x; return N */
126}
127
128} // namespace csparse_extension
129} // namespace g2o
#define G2O_DEBUG(...)
Definition logger.h:90
#define __PRETTY_FUNCTION__
Definition macros.h:90
csn * cs_chol_workspace(const cs *A, const css *S, int *cin, double *xin)
int cs_cholsolsymb(const cs *A, double *b, const css *S, double *x, int *work)
Definition jet.h:876