g2o
Classes | Functions
g2o::csparse_extension Namespace Reference

Classes

struct  SparseMatrixEntry
 
struct  SparseMatrixEntryColSort
 

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

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 88 of file csparse_helper.cpp.

Referenced by cs_cholsolsymb(), g2o::LinearSolverCSparse< MatrixType >::solveBlocks(), and g2o::LinearSolverCSparse< MatrixType >::solvePattern().

89  {
90  double d, lki, *Lx, *x, *Cx ;
91  int top, i, p, k, n, *Li, *Lp, *cp, *pinv, *s, *c, *parent, *Cp, *Ci ;
92  cs *L, *C, *E ;
93  csn *N ;
94  if (!CS_CSC (A) || !S || !S->cp || !S->parent) return (NULL) ;
95  n = A->n ;
96  N = (csn*) cs_calloc (1, sizeof (csn)) ; /* allocate result */
97  c = cin ; /* get int workspace */
98  x = xin ; /* get double workspace */
99  cp = S->cp ; pinv = S->pinv ; parent = S->parent ;
100  C = pinv ? cs_symperm (A, pinv, 1) : ((cs *) A) ;
101  E = pinv ? C : NULL ; /* E is alias for A, or a copy E=A(p,p) */
102  if (!N || !c || !x || !C) return (cs_ndone (N, E, NULL, NULL, 0)) ;
103  s = c + n ;
104  Cp = C->p ; Ci = C->i ; Cx = C->x ;
105  N->L = L = cs_spalloc (n, n, cp [n], 1, 0) ; /* allocate result */
106  if (!L) return (cs_ndone (N, E, NULL, NULL, 0)) ;
107  Lp = L->p ; Li = L->i ; Lx = L->x ;
108  for (k = 0 ; k < n ; k++) Lp [k] = c [k] = cp [k] ;
109  for (k = 0 ; k < n ; k++) /* compute L(k,:) for L*L' = C */
110  {
111  /* --- Nonzero pattern of L(k,:) ------------------------------------ */
112  top = cs_ereach (C, k, parent, s, c) ; /* find pattern of L(k,:) */
113  x [k] = 0 ; /* x (0:k) is now zero */
114  for (p = Cp [k] ; p < Cp [k+1] ; p++) /* x = full(triu(C(:,k))) */
115  {
116  if (Ci [p] <= k) x [Ci [p]] = Cx [p] ;
117  }
118  d = x [k] ; /* d = C(k,k) */
119  x [k] = 0 ; /* clear x for k+1st iteration */
120  /* --- Triangular solve --------------------------------------------- */
121  for ( ; top < n ; top++) /* solve L(0:k-1,0:k-1) * x = C(:,k) */
122  {
123  i = s [top] ; /* s [top..n-1] is pattern of L(k,:) */
124  lki = x [i] / Lx [Lp [i]] ; /* L(k,i) = x (i) / L(i,i) */
125  x [i] = 0 ; /* clear x for k+1st iteration */
126  for (p = Lp [i] + 1 ; p < c [i] ; p++)
127  {
128  x [Li [p]] -= Lx [p] * lki ;
129  }
130  d -= lki * lki ; /* d = d - L(k,i)*L(k,i) */
131  p = c [i]++ ;
132  Li [p] = k ; /* store L(k,i) in column i */
133  Lx [p] = lki ;
134  }
135  /* --- Compute L(k,k) ----------------------------------------------- */
136  if (d <= 0) return (cs_ndone (N, E, NULL, NULL, 0)) ; /* not pos def */
137  p = c [k]++ ;
138  Li [p] = k ; /* store L(k,k) = sqrt (d) in column k */
139  Lx [p] = sqrt (d) ;
140  }
141  Lp [n] = cp [n] ; /* finalize L */
142  return (cs_ndone (N, E, NULL, NULL, 1)) ; /* success: free E,s,x; return N */
143  }
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 56 of file csparse_helper.cpp.

References __PRETTY_FUNCTION__, and cs_chol_workspace().

Referenced by g2o::LinearSolverCSparse< MatrixType >::solve().

57  {
58  csn *N ;
59  int n, ok ;
60  if (!CS_CSC (A) || !b || ! S || !x) {
61  fprintf(stderr, "%s: No valid input!\n", __PRETTY_FUNCTION__);
62  assert(0); // get a backtrace in debug mode
63  return (0) ; /* check inputs */
64  }
65  n = A->n ;
66  N = cs_chol_workspace (A, S, work, x) ; /* numeric Cholesky factorization */
67  if (!N) {
68  fprintf(stderr, "%s: cholesky failed!\n", __PRETTY_FUNCTION__);
69  /*assert(0);*/
70  }
71  ok = (N != NULL) ;
72  if (ok)
73  {
74  cs_ipvec (S->pinv, b, x, n) ; /* x = P*b */
75  cs_lsolve (N->L, x) ; /* x = L\x */
76  cs_ltsolve (N->L, x) ; /* x = L'\x */
77  cs_pvec (S->pinv, x, b, n) ; /* b = P'*x */
78  }
79  cs_nfree (N) ;
80  return (ok) ;
81  }
#define __PRETTY_FUNCTION__
Definition: macros.h:89
csn * cs_chol_workspace(const cs *A, const css *S, int *cin, double *xin)
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 145 of file csparse_helper.cpp.

References g2o::csparse_extension::SparseMatrixEntry::_c, g2o::csparse_extension::SparseMatrixEntry::_r, and g2o::csparse_extension::SparseMatrixEntry::_x.

Referenced by g2o::LinearSolverCSparse< MatrixType >::solve().

146  {
147  int cols = A->n;
148  int rows = A->m;
149 
150  string name = filename;
151  std::string::size_type lastDot = name.find_last_of('.');
152  if (lastDot != std::string::npos)
153  name = name.substr(0, lastDot);
154 
155  vector<SparseMatrixEntry> entries;
156  if (A->nz == -1) { // CCS matrix
157  const int* Ap = A->p;
158  const int* Ai = A->i;
159  const double* Ax = A->x;
160  for (int i=0; i < cols; i++) {
161  const int& rbeg = Ap[i];
162  const int& rend = Ap[i+1];
163  for (int j = rbeg; j < rend; j++) {
164  entries.push_back(SparseMatrixEntry(Ai[j], i, Ax[j]));
165  if (upperTriangular && Ai[j] != i)
166  entries.push_back(SparseMatrixEntry(i, Ai[j], Ax[j]));
167  }
168  }
169  } else { // Triplet matrix
170  entries.reserve(A->nz);
171  int *Aj = A->p; // column indeces
172  int *Ai = A->i; // row indices
173  double *Ax = A->x; // values;
174  for (int i = 0; i < A->nz; ++i) {
175  entries.push_back(SparseMatrixEntry(Ai[i], Aj[i], Ax[i]));
176  if (upperTriangular && Ai[i] != Aj[i])
177  entries.push_back(SparseMatrixEntry(Aj[i], Ai[i], Ax[i]));
178  }
179  }
180  sort(entries.begin(), entries.end(), SparseMatrixEntryColSort());
181 
182  std::ofstream fout(filename);
183  fout << "# name: " << name << std::endl;
184  fout << "# type: sparse matrix" << std::endl;
185  fout << "# nnz: " << entries.size() << std::endl;
186  fout << "# rows: " << rows << std::endl;
187  fout << "# columns: " << cols << std::endl;
188  //fout << fixed;
189  fout << setprecision(9) << endl;
190 
191  for (vector<SparseMatrixEntry>::const_iterator it = entries.begin(); it != entries.end(); ++it) {
192  const SparseMatrixEntry& entry = *it;
193  fout << entry._r+1 << " " << entry._c+1 << " " << entry._x << std::endl;
194  }
195  return fout.good();
196  }