2010-10-14 12:54:38 +08:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
|
|
2019-02-11 22:39:48 +08:00
|
|
|
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
|
2010-10-14 12:54:38 +08:00
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
2009-12-27 06:48:41 +08:00
|
|
|
/**
|
|
|
|
|
* @file Errors.cpp
|
2010-10-09 11:09:58 +08:00
|
|
|
* @brief Factor Graph Values
|
2009-12-27 06:48:41 +08:00
|
|
|
* @brief Errors
|
|
|
|
|
* @author Carlos Nieto
|
|
|
|
|
* @author Christian Potthast
|
|
|
|
|
*/
|
|
|
|
|
|
2013-08-03 06:09:49 +08:00
|
|
|
#include <boost/range/adaptor/map.hpp>
|
2010-08-20 01:23:19 +08:00
|
|
|
#include <gtsam/linear/Errors.h>
|
2013-08-03 06:09:49 +08:00
|
|
|
#include <gtsam/linear/VectorValues.h>
|
2009-12-27 06:48:41 +08:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
namespace gtsam {
|
|
|
|
|
|
2010-10-15 00:08:16 +08:00
|
|
|
/* ************************************************************************* */
|
2013-08-03 06:09:49 +08:00
|
|
|
Errors::Errors(const VectorValues& V) {
|
2016-05-22 00:44:46 +08:00
|
|
|
for(const Vector& e: V | boost::adaptors::map_values) {
|
2013-08-03 06:09:49 +08:00
|
|
|
push_back(e);
|
2012-10-02 22:40:07 +08:00
|
|
|
}
|
2010-10-15 00:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
2009-12-27 06:48:41 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
void Errors::print(const std::string& s) const {
|
2014-09-25 02:56:38 +08:00
|
|
|
cout << s << endl;
|
2016-05-22 00:44:46 +08:00
|
|
|
for(const Vector& v: *this)
|
2009-12-31 01:13:36 +08:00
|
|
|
gtsam::print(v);
|
2009-12-27 06:48:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ************************************************************************* */
|
2020-01-20 07:57:07 +08:00
|
|
|
struct equalsVector : public std::function<bool(const Vector&, const Vector&)> {
|
2012-10-02 22:40:07 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2009-12-27 06:48:41 +08:00
|
|
|
bool Errors::equals(const Errors& expected, double tol) const {
|
2012-10-02 22:40:07 +08:00
|
|
|
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
|
2012-10-02 22:40:07 +08:00
|
|
|
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
|
2012-10-02 22:40:07 +08:00
|
|
|
Errors result;
|
|
|
|
|
Errors::const_iterator it = b.begin();
|
2016-05-22 00:44:46 +08:00
|
|
|
for(const Vector& ai: *this)
|
2012-10-02 22:40:07 +08:00
|
|
|
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
|
2012-10-02 22:40:07 +08:00
|
|
|
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
|
2012-10-02 22:40:07 +08:00
|
|
|
Errors result;
|
|
|
|
|
Errors::const_iterator it = b.begin();
|
2016-05-22 00:44:46 +08:00
|
|
|
for(const Vector& ai: *this)
|
2012-10-02 22:40:07 +08:00
|
|
|
result.push_back(ai - *(it++));
|
|
|
|
|
return result;
|
2009-12-27 06:48:41 +08:00
|
|
|
}
|
|
|
|
|
|
2012-06-09 10:42:45 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
Errors Errors::operator-() const {
|
|
|
|
|
Errors result;
|
2016-05-22 00:44:46 +08:00
|
|
|
for(const Vector& ai: *this)
|
2012-06-09 10:42:45 +08:00
|
|
|
result.push_back(-ai);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-28 16:15:09 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
double dot(const Errors& a, const Errors& b) {
|
2010-02-21 08:01:43 +08:00
|
|
|
#ifndef NDEBUG
|
2012-10-02 22:40:07 +08:00
|
|
|
size_t m = a.size();
|
2009-12-28 16:15:09 +08:00
|
|
|
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;
|
2012-10-02 22:40:07 +08:00
|
|
|
Errors::const_iterator it = b.begin();
|
2016-05-22 00:44:46 +08:00
|
|
|
for(const Vector& ai: a)
|
2012-10-02 22:40:07 +08:00
|
|
|
result += gtsam::dot(ai, *(it++));
|
|
|
|
|
return result;
|
2009-12-28 16:15:09 +08:00
|
|
|
}
|
|
|
|
|
|
2010-02-21 08:01:43 +08:00
|
|
|
/* ************************************************************************* */
|
2021-11-30 03:00:26 +08:00
|
|
|
void axpy(double alpha, const Errors& x, Errors& y) {
|
2012-10-02 22:40:07 +08:00
|
|
|
Errors::const_iterator it = x.begin();
|
2016-05-22 00:44:46 +08:00
|
|
|
for(Vector& yi: y)
|
2021-11-30 03:00:26 +08:00
|
|
|
yi += alpha * (*(it++));
|
2010-02-21 08:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
2010-01-11 16:32:59 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
void print(const Errors& a, const string& s) {
|
2012-10-02 22:40:07 +08:00
|
|
|
a.print(s);
|
2010-01-11 16:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
2009-12-27 06:48:41 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
|
|
|
|
|
} // gtsam
|