Skip to content
Snippets Groups Projects
Commit c678491b authored by Oliver Sander's avatar Oliver Sander
Browse files

Implement two-argument vector / scalar multiplication

This is implemented for FieldVector only: For general
DenseVectors it is currently not possible to implement
the necessary type promotion.
parent 53544b2c
Branches
Tags
1 merge request!705Add various binary operators to DenseVector and DenseMatrix
......@@ -29,6 +29,11 @@
now have additional operators. In particular, there are
- Vector = - Vector
- Matrix = - Matrix
While these two work for any vector or matrix class that inherits from `DenseVector`
or `DenseMatrix`, the following additional methods only work for `FieldVector`:
- Vector = Scalar * Vector
- Vector = Vector * Scalar
- Vector = Vector / Scalar
- There is now (finally!) a method `power` in the file `math.hh` that computes
powers with an integer exponent, and is usable in compile-time expressions.
......
......@@ -22,6 +22,7 @@
#include "boundschecking.hh"
#include <dune/common/math.hh>
#include <dune/common/promotiontraits.hh>
namespace Dune {
......@@ -208,6 +209,46 @@ namespace Dune {
{
return _data.data();
}
//! vector space multiplication with scalar
template <class Scalar,
std::enable_if_t<IsNumber<Scalar>::value, int> = 0>
friend auto operator* ( const FieldVector& vector, Scalar scalar)
{
FieldVector<typename PromotionTraits<value_type,Scalar>::PromotedType,SIZE> result;
for (size_type i = 0; i < vector.size(); ++i)
result[i] = vector[i] * scalar;
return result;
}
//! vector space multiplication with scalar
template <class Scalar,
std::enable_if_t<IsNumber<Scalar>::value, int> = 0>
friend auto operator* ( Scalar scalar, const FieldVector& vector)
{
FieldVector<typename PromotionTraits<value_type,Scalar>::PromotedType,SIZE> result;
for (size_type i = 0; i < vector.size(); ++i)
result[i] = scalar * vector[i];
return result;
}
//! vector space division by scalar
template <class Scalar,
std::enable_if_t<IsNumber<Scalar>::value, int> = 0>
friend auto operator/ ( const FieldVector& vector, Scalar scalar)
{
FieldVector<typename PromotionTraits<value_type,Scalar>::PromotedType,SIZE> result;
for (size_type i = 0; i < vector.size(); ++i)
result[i] = vector[i] * scalar;
return result;
}
};
/** \brief Read a FieldVector from an input stream
......
......@@ -101,6 +101,9 @@ struct FieldVectorMainTestCommons
w -= a;
w *= a;
w /= a;
w = a * v;
w = v * a;
w = v / a;
// Negation
-v;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment