Added scalar * operator to VectorValues

release/4.3a0
Richard Roberts 2013-08-01 21:57:26 +00:00
parent 2b9105b0ce
commit 5e3b4bf477
3 changed files with 23 additions and 0 deletions

View File

@ -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)
{

View File

@ -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);

View File

@ -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()));
}
/* ************************************************************************* */