From acfe742c29a91e89f6882713f62cc4df32e32263 Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Fri, 20 Nov 2009 05:10:55 +0000 Subject: [PATCH] Added a simple scaling function for VectorConfigs --- cpp/VectorConfig.cpp | 10 ++++++++++ cpp/VectorConfig.h | 3 +++ cpp/testVectorConfig.cpp | 15 +++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/cpp/VectorConfig.cpp b/cpp/VectorConfig.cpp index 4e25e4557..619f17cb1 100644 --- a/cpp/VectorConfig.cpp +++ b/cpp/VectorConfig.cpp @@ -26,6 +26,16 @@ void check_size(const string& key, const Vector & vj, const Vector & dj) { } } +/* ************************************************************************* */ +VectorConfig VectorConfig::scale(double gain) { + VectorConfig scaled; + string key; Vector val; + FOREACH_PAIR(key, val, values) { + scaled.insert(key, gain*val); + } + return scaled; +} + /* ************************************************************************* */ VectorConfig VectorConfig::exmap(const VectorConfig & delta) const { diff --git a/cpp/VectorConfig.h b/cpp/VectorConfig.h index f6f744d08..4f7cb7d96 100644 --- a/cpp/VectorConfig.h +++ b/cpp/VectorConfig.h @@ -52,6 +52,9 @@ namespace gtsam { * For VectorConfig, this is just addition. */ VectorConfig exmap(const VectorConfig & delta) const; + + /** Scales the configuration by a gain */ + VectorConfig scale(double gain); const_iterator begin() const {return values.begin();} const_iterator end() const {return values.end();} diff --git a/cpp/testVectorConfig.cpp b/cpp/testVectorConfig.cpp index d5076f38f..e2e012226 100644 --- a/cpp/testVectorConfig.cpp +++ b/cpp/testVectorConfig.cpp @@ -88,6 +88,21 @@ TEST( VectorConfig, plus) CHECK(actual.equals(expected)); } +/* ************************************************************************* */ +TEST( VectorConfig, scale) { + VectorConfig cfg; + cfg.insert("x", Vector_(2, 1.0, 2.0)); + cfg.insert("y", Vector_(2,-1.0,-2.0)); + + VectorConfig actual = cfg.scale(2.0); + + VectorConfig expected; + expected.insert("x", Vector_(2, 2.0, 4.0)); + expected.insert("y", Vector_(2,-2.0,-4.0)); + + CHECK(assert_equal(actual, expected)); +} + /* ************************************************************************* */ #ifdef HAVE_BOOST_SERIALIZATION TEST( VectorConfig, serialize)