diff --git a/cpp/FactorGraph-inl.h b/cpp/FactorGraph-inl.h index c00ed062b..81571cb90 100644 --- a/cpp/FactorGraph-inl.h +++ b/cpp/FactorGraph-inl.h @@ -11,18 +11,47 @@ #pragma once #include +#include #include #include +#include #include "Ordering.h" #include "FactorGraph.h" - -#include - using namespace std; namespace gtsam { +/* ************************************************************************* */ +template +void FactorGraph::print(const string& s) const { + cout << s << endl; + printf("size: %d\n", (int) size()); + for (int i = 0; i < factors_.size(); i++) { + stringstream ss; + ss << "factor " << i << ":"; + if (factors_[i] != NULL) factors_[i]->print(ss.str()); + } +} + +/* ************************************************************************* */ +template +bool FactorGraph::equals + (const FactorGraph& fg, double tol) const { + /** check whether the two factor graphs have the same number of factors_ */ + if (factors_.size() != fg.size()) return false; + + /** check whether the factors_ are the same */ + for (size_t i = 0; i < factors_.size(); i++) { + // TODO: Doesn't this force order of factor insertion? + shared_factor f1 = factors_[i], f2 = fg.factors_[i]; + if (f1 == NULL && f2 == NULL) continue; + if (f1 == NULL || f2 == NULL) return false; + if (!f1->equals(*f2, tol)) return false; + } + return true; +} + /* ************************************************************************* */ template void FactorGraph::push_back(shared_factor factor) { @@ -53,10 +82,10 @@ void FactorGraph::push_back(shared_factor factor) { * @param columns map from keys to a sparse column of non-zero row indices */ template -Ordering colamd(int n_col, int n_row, int nrNonZeros, const std::map >& columns) { +Ordering colamd(int n_col, int n_row, int nrNonZeros, const map >& columns) { // Convert to compressed column major format colamd wants it in (== MATLAB format!) - std::vector initialOrder; + vector initialOrder; int Alen = nrNonZeros*30; /* colamd arg 3: size of the array A TODO: use Tim's function ! */ int * A = new int[Alen]; /* colamd arg 4: row indices of A, of size Alen */ int * p = new int[n_col + 1]; /* colamd arg 5: column pointers of A, of size n_col+1 */ @@ -64,10 +93,10 @@ Ordering colamd(int n_col, int n_row, int nrNonZeros, const std::map >::const_iterator iterator; + typedef typename map >::const_iterator iterator; for(iterator it = columns.begin(); it != columns.end(); it++) { const Key& key = it->first; - const std::vector& column = it->second; + const vector& column = it->second; initialOrder.push_back(key); BOOST_FOREACH(int i, column) A[count++] = i; // copy sparse column p[j] = count; // column j (base 1) goes from A[j-1] to A[j]-1 @@ -98,14 +127,14 @@ Ordering FactorGraph::getOrdering() const { // A factor graph is really laid out in row-major format, each factor a row // Below, we compute a symbolic matrix stored in sparse columns. - typedef std::string Key; // default case with string keys - std::map > columns; // map from keys to a sparse column of non-zero row indices + typedef string Key; // default case with string keys + map > columns; // map from keys to a sparse column of non-zero row indices int nrNonZeros = 0; // number of non-zero entries int n_row = factors_.size(); /* colamd arg 1: number of rows in A */ // loop over all factors = rows for (int i = 0; i < n_row; i++) { - std::list keys = factors_[i]->keys(); + list keys = factors_[i]->keys(); BOOST_FOREACH(Key key, keys) columns[key].push_back(i); nrNonZeros+= keys.size(); } diff --git a/cpp/FactorGraph.h b/cpp/FactorGraph.h index 89d2597f9..78b1c5d43 100644 --- a/cpp/FactorGraph.h +++ b/cpp/FactorGraph.h @@ -47,23 +47,29 @@ namespace gtsam { public: + /** print out graph */ + void print(const std::string& s = "FactorGraph") const; + + /** Check equality */ + bool equals(const FactorGraph& fg, double tol = 1e-9) const; + /** STL like, return the iterator pointing to the first factor */ - const_iterator begin() const { + inline const_iterator begin() const { return factors_.begin(); } /** STL like, return the iterator pointing to the last factor */ - const_iterator end() const { + inline const_iterator end() const { return factors_.end(); } /** clear the factor graph */ - void clear() { + inline void clear() { factors_.clear(); } /** Get a specific factor by index */ - shared_factor operator[](size_t i) const { + inline shared_factor operator[](size_t i) const { return factors_[i]; } @@ -94,30 +100,6 @@ namespace gtsam { return exp(-0.5 * error(c)); } - /** print out graph */ - void print(const std::string& s = "FactorGraph") const { - std::cout << s << std::endl; - printf("size: %d\n", (int) size()); - for (const_iterator factor = factors_.begin(); factor != factors_.end(); factor++) - if(*factor != NULL) (*factor)->print(); - } - - /** Check equality */ - bool equals(const FactorGraph& fg, double tol = 1e-9) const { - /** check whether the two factor graphs have the same number of factors_ */ - if (factors_.size() != fg.size()) return false; - - /** check whether the factors_ are the same */ - for (size_t i = 0; i < factors_.size(); i++) { - // TODO: Doesn't this force order of factor insertion? - shared_factor f1 = factors_[i], f2 = fg.factors_[i]; - if (f1==NULL && f2==NULL) continue; - if (f1==NULL || f2==NULL) return false; - if (!f1->equals(*f2, tol)) return false; - } - return true; - } - /** * Compute colamd ordering */