Added ForwardBlock operator* for scalars.

This commit is contained in:
Jens Olav Nygaard
2013-08-02 12:24:12 +02:00
parent 23e2034118
commit d3a02e4891
2 changed files with 90 additions and 33 deletions

View File

@@ -242,7 +242,17 @@ namespace AutoDiff
V val_;
std::vector<M> jac_;
};
template <typename Sclr>
friend
ForwardBlock<Sclr> operator*(const ForwardBlock<Sclr> &lhs,
const Sclr &rhs);
template <typename Sclr>
friend
ForwardBlock<Sclr> operator*(const Sclr &lhs,
const ForwardBlock<Sclr> &rhs);
};
template <class Ostream, typename Scalar>
@@ -333,6 +343,41 @@ namespace AutoDiff
}
/**
* @brief Operator for multiplication with a scalar on the right-hand side
*
* @param lhs The left-hand side AD forward block
* @param rhs The scalar to multiply with
* @return The product
*/
template <typename Scalar>
ForwardBlock<Scalar> operator*(const ForwardBlock<Scalar> &lhs,
const Scalar &rhs)
{
std::vector< Eigen::SparseMatrix<Scalar> > jac = lhs.jac_;
for (int block=0; block<lhs.numBlocks(); block++) {
jac[block] = lhs.jac_[block] * rhs;
}
auto val = lhs.val_ * rhs;
return ForwardBlock<Scalar>::function(val, jac);
}
/**
* @brief Operator for multiplication with a scalar on the left-hand side
*
* @param lhs The scalar to multiply with
* @param rhs The right-hand side AD forward block
* @return The product
*/
template <typename Scalar>
ForwardBlock<Scalar> operator*(const Scalar &lhs,
const ForwardBlock<Scalar> &rhs)
{
return rhs * lhs; // Commutative operation.
}
} // namespace Autodiff