Recursive print

release/4.3a0
Frank Dellaert 2015-07-08 23:21:35 -07:00
parent f7c5f0cb79
commit 6df0d49769
3 changed files with 46 additions and 13 deletions

View File

@ -29,7 +29,7 @@ namespace gtsam {
template<typename T> template<typename T>
void Expression<T>::print(const std::string& s) const { void Expression<T>::print(const std::string& s) const {
std::cout << s << *root_ << std::endl; root_->print(s);
} }
template<typename T> template<typename T>
@ -155,7 +155,7 @@ size_t Expression<T>::traceSize() const {
template<typename T> template<typename T>
T Expression<T>::valueAndDerivatives(const Values& values, T Expression<T>::valueAndDerivatives(const Values& values,
const FastVector<Key>& keys, const FastVector<int>& dims, const KeyVector& keys, const FastVector<int>& dims,
std::vector<Matrix>& H) const { std::vector<Matrix>& H) const {
// H should be pre-allocated // H should be pre-allocated
@ -205,6 +205,7 @@ T Expression<T>::valueAndJacobianMap(const Values& values,
internal::ExecutionTrace<T> trace; internal::ExecutionTrace<T> trace;
T value(this->traceExecution(values, trace, traceStorage)); T value(this->traceExecution(values, trace, traceStorage));
GTSAM_PRINT(trace);
trace.startReverseAD1(jacobians); trace.startReverseAD1(jacobians);
#ifdef _MSC_VER #ifdef _MSC_VER
@ -219,7 +220,7 @@ typename Expression<T>::KeysAndDims Expression<T>::keysAndDims() const {
std::map<Key, int> map; std::map<Key, int> map;
dims(map); dims(map);
size_t n = map.size(); size_t n = map.size();
KeysAndDims pair = std::make_pair(FastVector<Key>(n), FastVector<int>(n)); KeysAndDims pair = std::make_pair(KeyVector(n), FastVector<int>(n));
boost::copy(map | boost::adaptors::map_keys, pair.first.begin()); boost::copy(map | boost::adaptors::map_keys, pair.first.begin());
boost::copy(map | boost::adaptors::map_values, pair.second.begin()); boost::copy(map | boost::adaptors::map_values, pair.second.begin());
return pair; return pair;

View File

@ -173,11 +173,11 @@ public:
private: private:
/// Keys and dimensions in same order /// Keys and dimensions in same order
typedef std::pair<FastVector<Key>, FastVector<int> > KeysAndDims; typedef std::pair<KeyVector, FastVector<int> > KeysAndDims;
KeysAndDims keysAndDims() const; KeysAndDims keysAndDims() const;
/// private version that takes keys and dimensions, returns derivatives /// private version that takes keys and dimensions, returns derivatives
T valueAndDerivatives(const Values& values, const FastVector<Key>& keys, T valueAndDerivatives(const Values& values, const KeyVector& keys,
const FastVector<int>& dims, std::vector<Matrix>& H) const; const FastVector<int>& dims, std::vector<Matrix>& H) const;
/// trace execution, very unsafe /// trace execution, very unsafe

View File

@ -78,13 +78,14 @@ public:
virtual ~ExpressionNode() { virtual ~ExpressionNode() {
} }
/// Print
virtual void print(const std::string& indent = "") const = 0;
/// Streaming /// Streaming
GTSAM_EXPORT GTSAM_EXPORT
friend std::ostream &operator<<(std::ostream &os, friend std::ostream& operator<<(std::ostream& os, const ExpressionNode& node) {
const ExpressionNode& node) {
os << "Expression of type " << typeid(T).name(); os << "Expression of type " << typeid(T).name();
if (node.traceSize_ > 0) if (node.traceSize_ > 0) os << ", trace size = " << node.traceSize_;
os << ", trace size = " << node.traceSize_;
os << "\n"; os << "\n";
return os; return os;
} }
@ -133,6 +134,11 @@ public:
virtual ~ConstantExpression() { virtual ~ConstantExpression() {
} }
/// Print
virtual void print(const std::string& indent = "") const {
std::cout << indent << "Constant" << std::endl;
}
/// Return value /// Return value
virtual T value(const Values& values) const { virtual T value(const Values& values) const {
return constant_; return constant_;
@ -159,7 +165,7 @@ class LeafExpression: public ExpressionNode<T> {
key_(key) { key_(key) {
} }
friend class Expression<T> ; friend class Expression<T>;
public: public:
@ -167,6 +173,11 @@ public:
virtual ~LeafExpression() { virtual ~LeafExpression() {
} }
/// Print
virtual void print(const std::string& indent = "") const {
std::cout << indent << "Leaf, key = " << key_ << std::endl;
}
/// Return keys that play in this expression /// Return keys that play in this expression
virtual std::set<Key> keys() const { virtual std::set<Key> keys() const {
std::set<Key> keys; std::set<Key> keys;
@ -218,7 +229,7 @@ class UnaryExpression: public ExpressionNode<T> {
ExpressionNode<T>::traceSize_ = upAligned(sizeof(Record)) + e1.traceSize(); ExpressionNode<T>::traceSize_ = upAligned(sizeof(Record)) + e1.traceSize();
} }
friend class Expression<T> ; friend class Expression<T>;
public: public:
@ -226,6 +237,12 @@ public:
virtual ~UnaryExpression() { virtual ~UnaryExpression() {
} }
/// Print
virtual void print(const std::string& indent = "") const {
std::cout << indent << "UnaryExpression" << std::endl;
expression1_->print(indent + " ");
}
/// Return value /// Return value
virtual T value(const Values& values) const { virtual T value(const Values& values) const {
return function_(expression1_->value(values), boost::none); return function_(expression1_->value(values), boost::none);
@ -320,7 +337,7 @@ class BinaryExpression: public ExpressionNode<T> {
upAligned(sizeof(Record)) + e1.traceSize() + e2.traceSize(); upAligned(sizeof(Record)) + e1.traceSize() + e2.traceSize();
} }
friend class Expression<T> ; friend class Expression<T>;
friend class ::ExpressionFactorBinaryTest; friend class ::ExpressionFactorBinaryTest;
public: public:
@ -329,6 +346,13 @@ public:
virtual ~BinaryExpression() { virtual ~BinaryExpression() {
} }
/// Print
virtual void print(const std::string& indent = "") const {
std::cout << indent << "BinaryExpression" << std::endl;
expression1_->print(indent + " ");
expression2_->print(indent + " ");
}
/// Return value /// Return value
virtual T value(const Values& values) const { virtual T value(const Values& values) const {
using boost::none; using boost::none;
@ -420,7 +444,7 @@ class TernaryExpression: public ExpressionNode<T> {
e1.traceSize() + e2.traceSize() + e3.traceSize(); e1.traceSize() + e2.traceSize() + e3.traceSize();
} }
friend class Expression<T> ; friend class Expression<T>;
public: public:
@ -428,6 +452,14 @@ public:
virtual ~TernaryExpression() { virtual ~TernaryExpression() {
} }
/// Print
virtual void print(const std::string& indent = "") const {
std::cout << indent << "TernaryExpression" << std::endl;
expression1_->print(indent + " ");
expression2_->print(indent + " ");
expression3_->print(indent + " ");
}
/// Return value /// Return value
virtual T value(const Values& values) const { virtual T value(const Values& values) const {
using boost::none; using boost::none;