1#ifndef __DUNE_ACFEM_VECTOREXPRESSION_HH__
2#define __DUNE_ACFEM_VECTOREXPRESSION_HH__
28 template<
class ExpressionImp>
35 template<
class BinOP,
class Vector1,
class Vector2>
39 template<
class OP,
class Vector>
46 template<
class BinOp,
class Vector1,
class Vector2>
50 typedef BinOp BinOpType;
53 decltype(std::declval<Vector1>()[0]),
54 decltype(std::declval<Vector2>()[0])>
57 static_assert(std::is_same<typename Vector1::FieldType, typename Vector2::FieldType>::value,
58 "Cannot combine vector expression over different fields.");
60 typedef typename Vector1::FieldType FieldType;
61 typedef typename ResultType::Type ElementType;
64 : vector1_(v1), vector2_(v2)
67 ElementType operator[](
size_t index)
const
69 return ResultType::apply(vector1_()[index], vector2_()[index]);
73 assert(vector1_().size() == vector2_().size());
74 return vector1_().size();
83 template<
class Scalar,
class Vector>
85 :
public VectorExpression<BinaryVectorExpression<SMultiplyOperation, Scalar, Vector> >
91 decltype(std::declval<Vector>()[0])>
94 typedef typename Vector::FieldType FieldType;
95 typedef typename ResultType::Type ElementType;
98 : scalar_(s), vector_(v)
101 ElementType operator[](
size_t index)
const
103 return ResultType::apply(
parameterValue(scalar_()), vector_()[index]);
106 size_t size()
const {
107 return vector_().size();
116 template<
class Op,
class Vector>
122 typename std::remove_reference<decltype(std::declval<Vector>()[0])>::type
126 typedef typename Vector::FieldType FieldType;
127 typedef typename ResultType::Type ElementType;
133 ElementType operator[](
size_t index)
const
135 return ResultType::apply(vector_()[index]);
138 size_t size()
const {
139 return vector_().size();
156 template<
class Vector>
158 :
public VectorExpression<UnaryVectorExpression<IdentityOperation, Vector> >
162 typename std::remove_reference<decltype(std::declval<Vector>()[0])>::type
165 typedef typename FieldTraits<ComponentType>::field_type FieldType;
168 hasFieldComponents = std::is_same<ComponentType, FieldType>::value
172 typename std::conditional<hasFieldComponents,
183 ElementType operator[](
size_t index)
const
185 return ElementType(vector_()[index]);
188 size_t size()
const {
189 return vector_().size();
197 template<
class Field,
int rows,
int cols>
201 UnaryVectorExpression<IdentityOperation, FieldMatrix<Field, rows, cols> > >
204 typedef FieldMatrix<Field, rows, cols> MatrixType;
205 typedef typename MatrixType::row_type RowType;
207 typedef typename FieldTraits<Field>::field_type FieldType;
219 size_t size()
const {
220 return matrix_().size();
228 template<
class Expr1,
class Expr2>
237 return ExpressionType(e1, e2);
241 template<
class Expr1,
class Expr2>
243 BinaryVectorExpression<MinusOperation, Expr1, Expr2>
250 return ExpressionType(e1, e2);
257 template<
class Expr1,
class Expr2>
258 struct InnerProductHelper
260 template<
bool haveFieldVectors,
class dummy =
void>
261 struct ProductFactory;
263 template<
class dummy>
264 struct ProductFactory<true, dummy>
266 typedef typename Expr1::FieldType ResultType;
268 static ResultType apply(
const Expr1& l,
const Expr2& r)
270 typename Expr1::FieldType res(0);
272 for (
unsigned i = 0; i < l.size(); ++i) {
280 template<
class dummy>
281 struct ProductFactory<false, dummy>
284 BinaryVectorExpression<MultiplyOperation, Expr1, Expr2>
287 static ResultType apply(
const Expr1& l,
const Expr2& r)
289 return ResultType(l, r);
294 sameElements = std::is_same<typename Expr1::ElementType, typename Expr2::ElementType>::value,
295 fieldVectors = (sameElements &&
296 std::is_same<typename Expr1::FieldType, typename Expr1::ElementType>::value)
299 typedef typename ProductFactory<fieldVectors>::ResultType ResultType;
300 static ResultType apply(
const Expr1& l,
const Expr2& r)
302 return ProductFactory<fieldVectors>::apply(l, r);
315 template<
class Expr1,
class Expr2>
317 typename InnerProductHelper<Expr1, Expr2>::ResultType
326 BinaryVectorExpression<SMultiplyOperation, typename Expr::FieldType, Expr>
329 typedef typename Expr::FieldType FieldType;
332 return ExpressionType(s, e);
348 BinaryVectorExpression<SMultiplyOperation, typename Expr::FieldType, Expr>
351 typedef typename Expr::FieldType FieldType;
354 return ExpressionType(s[0], e);
360 BinaryVectorExpression<SMultiplyOperation, typename Expr::FieldType, Expr>
363 typedef typename Expr::FieldType FieldType;
366 return ExpressionType(s[0], e);
372 UnaryVectorExpression<MinusOperation, Expr>
377 return ExpressionType(arg);
381 template<
class Vector>
383 UnaryVectorExpression<IdentityOperation, Vector>
390 template<
class Field,
int rows,
int cols>
392 UnaryVectorExpression<IdentityOperation, FieldMatrix<Field, rows, cols> >
393 asExpr(
const FieldMatrix<Field, rows, cols>& arg)
402 template<
class Op,
class Expr>
404 UnaryVectorExpression<Op, Expr>
414 template<
class Op,
class Expr1,
class Expr2>
416 BinaryVectorExpression<Op, Expr1, Expr2>
423 template<
class Vector,
class Op,
class Expr1,
class Expr2>
428 for (
unsigned i = 0; i < v.size(); ++i) {
436 template<
class Vector,
class Op,
class Expr>
441 for (
unsigned i = 0; i < v.size(); ++i) {
449 template<
class Vector,
class Op,
class Expr1,
class Expr2>
454 for (
unsigned i = 0; i < v.size(); ++i) {
462 template<
class Vector,
class Op,
class Expr>
467 for (
unsigned i = 0; i < v.size(); ++i) {
478 template<
bool hasFieldElements,
class Dummy =
void>
479 struct VectorAssignHelper;
482 struct VectorAssignHelper<true>
484 template<
class To,
class From>
485 static void doAssign(To& to,
const From& from)
492 struct VectorAssignHelper<false>
494 template<
class To,
class From>
495 static void doAssign(To& to,
const From& from)
503 template<
class Vector,
class Expr>
505 Vector& assign(Vector& v,
const VectorExpression<Expr>& e_)
507 const Expr& e(e_.expression());
510 std::is_same<typename Expr::ElementType, typename Expr::FieldType>::value
513 for (
unsigned i = 0; i < v.size(); ++i) {
514 VectorAssignHelper<hasFieldElements>::doAssign(v[i], e[i]);
Binary vector expressions.
Definition: vectorexpression.hh:49
This is the only allowed starting point for vector-expressions: here we allow vectors to be wrapped i...
Definition: vectorexpression.hh:159
Unary vector expressions.
Definition: vectorexpression.hh:119
Tag structure, flag all these as ExpressionTemplates.
Definition: vectorexpression.hh:31
A list of supported expression operations as class-tags.
ParameterValue< Value >::ResultType parameterValue(const Value &value)
Return the unaltered argument for non-parameters and otherwise the parameter value.
Definition: parameterinterface.hh:263
BinaryParameterExpression< MultiplyOperation, Left, Right > operator*(const ParameterInterface< Left > &left, const ParameterInterface< Right > &right)
Scalar product between parameters.
Definition: parameterexpression.hh:321
static Vector & operator-=(Vector &v, const BinaryVectorExpression< Op, Expr1, Expr2 > &e)
Minus-assignment to result vector.
Definition: vectorexpression.hh:451
static Vector & operator+=(Vector &v, const BinaryVectorExpression< Op, Expr1, Expr2 > &e)
Add-assignment to result vector.
Definition: vectorexpression.hh:425
static UnaryVectorExpression< IdentityOperation, Vector > asExpr(const Vector &arg)
Catch all starting point.
Definition: vectorexpression.hh:384
Automatically deduce the type by applying the respective operation.
Definition: expressionoperations.hh:476
Provide up-cast functionality for expression templates.
Definition: expressionoperations.hh:37
const ExpressionType & expression() const
Return a const reference to the underlying expression.
Definition: expressionoperations.hh:42
Identity, i.e. just wrap the object.
Definition: expressionoperations.hh:284
Multiplication by scalars from the left.
Definition: expressionoperations.hh:257