g2o
Loading...
Searching...
No Matches
Functions
g2o::csparse_extension Namespace Reference

Functions

int cs_cholsolsymb (const cs *A, double *b, const css *S, double *x, int *work)
 
csn * cs_chol_workspace (const cs *A, const css *S, int *cin, double *xin)
 
bool writeCs2Octave (const char *filename, const cs *A, bool upperTriangular)
 

Function Documentation

◆ cs_chol_workspace()

G2O_CSPARSE_EXTENSION_API csn * g2o::csparse_extension::cs_chol_workspace ( const cs *  A,
const css *  S,
int *  cin,
double *  xin 
)

Originally from CSparse, avoid memory re-allocations by giving workspace pointers CSparse: Copyright (c) 2006-2011, Timothy A. Davis.

Definition at line 67 of file csparse_extension.cpp.

67 {
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}
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition jet.h:444

Referenced by cs_cholsolsymb(), and g2o::csparse::CSparse::factorize().

◆ cs_cholsolsymb()

G2O_CSPARSE_EXTENSION_API int g2o::csparse_extension::cs_cholsolsymb ( const cs *  A,
double *  b,
const css *  S,
double *  x,
int *  work 
)

Originally from CSparse, avoid memory re-allocations by giving workspace pointers CSparse: Copyright (c) 2006-2011, Timothy A. Davis.

Definition at line 38 of file csparse_extension.cpp.

38 {
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}
#define G2O_DEBUG(...)
Definition logger.h:90
#define __PRETTY_FUNCTION__
Definition macros.h:90

References __PRETTY_FUNCTION__, cs_chol_workspace(), and G2O_DEBUG.

Referenced by g2o::csparse::CSparse::solve().

◆ writeCs2Octave()

G2O_CSPARSE_EXTENSION_API bool g2o::csparse_extension::writeCs2Octave ( const char *  filename,
const cs *  A,
bool  upperTriangular = true 
)

write the sparse matrix to a file loadable with ocatve

Definition at line 36 of file csparse_helper.cpp.

36 {
37 int cols = A->n;
38 int rows = A->m;
39
40 if (A->nz == -1) { // CCS matrix
41 const int* Ap = A->p;
42 const int* Ai = A->i;
43 const double* Ax = A->x;
44 return writeCCSMatrix(filename, rows, cols, Ap, Ai, Ax, upperTriangular);
45 } else { // Triplet matrix
46 const int* Aj = A->p;
47 const int* Ai = A->i;
48 const double* Ax = A->x;
49 return writeTripletMatrix(filename, A->nz, rows, cols, Ai, Aj, Ax,
50 upperTriangular);
51 }
52}
bool writeCCSMatrix(const string &filename, int rows, int cols, const int *Ap, const int *Ai, const double *Ax, bool upperTriangleSymmetric)
bool writeTripletMatrix(const std::string &filename, int nz, int rows, int cols, const int *Ai, const int *Aj, const double *Ax, bool upperTriangleSymmetric)

References g2o::writeCCSMatrix(), and g2o::writeTripletMatrix().

Referenced by g2o::csparse::CSparse::writeSparse().