gtsam/gtsam/linear/Errors.cpp

129 lines
3.7 KiB
C++
Raw Normal View History

/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
* @file Errors.cpp
* @brief Factor Graph Values
* @brief Errors
* @author Carlos Nieto
* @author Christian Potthast
*/
#include <boost/foreach.hpp>
#include <boost/range/adaptor/map.hpp>
#include <gtsam/linear/Errors.h>
#include <gtsam/linear/VectorValues.h>
using namespace std;
namespace gtsam {
2010-10-15 00:08:16 +08:00
/* ************************************************************************* */
Errors::Errors(){}
/* ************************************************************************* */
Errors::Errors(const VectorValues& V) {
BOOST_FOREACH(const Vector& e, V | boost::adaptors::map_values) {
push_back(e);
}
2010-10-15 00:08:16 +08:00
}
/* ************************************************************************* */
void Errors::print(const std::string& s) const {
cout << s << endl;
2009-12-31 01:13:36 +08:00
BOOST_FOREACH(const Vector& v, *this)
gtsam::print(v);
}
/* ************************************************************************* */
2009-12-31 01:13:36 +08:00
struct equalsVector : public std::binary_function<const Vector&, const Vector&, bool> {
double tol_;
equalsVector(double tol = 1e-9) : tol_(tol) {}
bool operator()(const Vector& expected, const Vector& actual) {
return equal_with_abs_tol(expected, actual,tol_);
}
2009-12-31 01:13:36 +08:00
};
bool Errors::equals(const Errors& expected, double tol) const {
if( size() != expected.size() ) return false;
return equal(begin(),end(),expected.begin(),equalsVector(tol));
2009-12-31 18:27:16 +08:00
}
2010-10-15 00:08:16 +08:00
/* ************************************************************************* */
Errors Errors::operator+(const Errors& b) const {
#ifndef NDEBUG
size_t m = size();
if (b.size()!=m)
2010-10-15 00:08:16 +08:00
throw(std::invalid_argument("Errors::operator+: incompatible sizes"));
#endif
Errors result;
Errors::const_iterator it = b.begin();
2010-10-15 00:08:16 +08:00
BOOST_FOREACH(const Vector& ai, *this)
result.push_back(ai + *(it++));
return result;
2010-10-15 00:08:16 +08:00
}
2009-12-31 18:27:16 +08:00
/* ************************************************************************* */
Errors Errors::operator-(const Errors& b) const {
2010-02-21 08:01:43 +08:00
#ifndef NDEBUG
size_t m = size();
if (b.size()!=m)
2009-12-31 18:27:16 +08:00
throw(std::invalid_argument("Errors::operator-: incompatible sizes"));
2010-02-21 08:01:43 +08:00
#endif
Errors result;
Errors::const_iterator it = b.begin();
2009-12-31 18:27:16 +08:00
BOOST_FOREACH(const Vector& ai, *this)
result.push_back(ai - *(it++));
return result;
}
2012-06-09 10:42:45 +08:00
/* ************************************************************************* */
Errors Errors::operator-() const {
Errors result;
BOOST_FOREACH(const Vector& ai, *this)
result.push_back(-ai);
return result;
}
/* ************************************************************************* */
double dot(const Errors& a, const Errors& b) {
2010-02-21 08:01:43 +08:00
#ifndef NDEBUG
size_t m = a.size();
if (b.size()!=m)
throw(std::invalid_argument("Errors::dot: incompatible sizes"));
2010-02-21 08:01:43 +08:00
#endif
double result = 0.0;
Errors::const_iterator it = b.begin();
2009-12-31 01:13:36 +08:00
BOOST_FOREACH(const Vector& ai, a)
result += gtsam::dot(ai, *(it++));
return result;
}
2010-02-21 08:01:43 +08:00
/* ************************************************************************* */
template<>
void axpy<Errors,Errors>(double alpha, const Errors& x, Errors& y) {
Errors::const_iterator it = x.begin();
2010-02-21 08:01:43 +08:00
BOOST_FOREACH(Vector& yi, y)
axpy(alpha,*(it++),yi);
2010-02-21 08:01:43 +08:00
}
/* ************************************************************************* */
void print(const Errors& a, const string& s) {
a.print(s);
}
/* ************************************************************************* */
} // gtsam