diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index ad08a14b2..fbded1a9f 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -246,6 +246,21 @@ namespace gtsam { return *this - c; } + /* ************************************************************************* */ + VectorValues operator*(const double a, const VectorValues &v) + { + VectorValues result; + BOOST_FOREACH(const VectorValues::KeyValuePair& v, v) + result.values_.insert(result.values_.end(), make_pair(v.first, a * v.second)); + return result; + } + + /* ************************************************************************* */ + VectorValues VectorValues::scale(const double a) const + { + return a * *this; + } + /* ************************************************************************* */ VectorValues& VectorValues::operator*=(double alpha) { diff --git a/gtsam/linear/VectorValues.h b/gtsam/linear/VectorValues.h index e63de77af..0014f0e69 100644 --- a/gtsam/linear/VectorValues.h +++ b/gtsam/linear/VectorValues.h @@ -272,6 +272,12 @@ namespace gtsam { * structure (checked when NDEBUG is not defined). */ VectorValues subtract(const VectorValues& c) const; + /** Element-wise scaling by a constant. */ + friend VectorValues operator*(const double a, const VectorValues &v); + + /** Element-wise scaling by a constant. */ + VectorValues scale(const double a) const; + /** Element-wise scaling by a constant in-place. */ VectorValues& operator*=(double alpha); diff --git a/gtsam/linear/tests/testVectorValuesUnordered.cpp b/gtsam/linear/tests/testVectorValuesUnordered.cpp index 393985651..a651cdf72 100644 --- a/gtsam/linear/tests/testVectorValuesUnordered.cpp +++ b/gtsam/linear/tests/testVectorValuesUnordered.cpp @@ -154,6 +154,8 @@ TEST(VectorValues, LinearAlgebra) VectorValues scalActual = test1; scalActual *= 5.0; EXPECT(assert_equal(scalExpected, scalActual.vector())); + VectorValues scal2Actual = 5.0 * test1; + EXPECT(assert_equal(scalExpected, scal2Actual.vector())); } /* ************************************************************************* */