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

Implement negation for dense vectors and matrices

In other words, with this patch you can now write

FieldVector<double, 3> a = {1,2,3};
FieldVector<double, 3> b = -a;

and similarly for matrices.
parent b9c753b8
No related branches found
No related tags found
1 merge request!705Add various binary operators to DenseVector and DenseMatrix
......@@ -25,6 +25,11 @@
implementation in `dune-common` is automatically disabled, and the official
implementation from the standard library is used instead.
- By popular demand, dense vectors and matrices like `FieldVector` and `FieldMatrix`
now have additional operators. In particular, there are
- Vector = - Vector
- Matrix = - Matrix
- 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.
The use of the old power methods in `power.hh` is henceforth discouraged.
......
......@@ -321,6 +321,19 @@ namespace Dune
return asImp();
}
//! Matrix negation
derived_type operator- () const
{
MAT result;
typedef typename decltype(result)::size_type size_type;
for (size_type i = 0; i < rows(); ++i)
for (size_type j = 0; j < cols(); ++j)
result[i][j] = - asImp()[i][j];
return result;
}
//! vector space subtraction
template <class Other>
derived_type &operator-= (const DenseMatrix<Other>& x)
......
......@@ -448,6 +448,18 @@ namespace Dune {
return (z-=b);
}
//! Vector negation
derived_type operator- () const
{
V result;
typedef typename decltype(result)::size_type size_type;
for (size_type i = 0; i < size(); ++i)
result[i] = -asImp()[i];
return result;
}
//! \brief vector space add scalar to all comps
/**
we use enable_if to avoid an ambiguity, if the
......
......@@ -416,6 +416,15 @@ void test_matrix()
if (tmp.infinity_norm() > 1e-12)
DUNE_THROW(FMatrixError, "Return value of axpy() incorrect!");
}
// -Matrix
{
FM neg = -A;
FM ref = typename FM::field_type(-1) * A;
if ((neg-ref).infinity_norm() > 1e-12)
DUNE_THROW(FMatrixError, "Return value of operator-(matrix) incorrect!");
}
}
{
using std::abs;
......
......@@ -102,6 +102,9 @@ struct FieldVectorMainTestCommons
w *= a;
w /= a;
// Negation
-v;
// test scalar product, axpy
a = v * w;
a = v.dot(w);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment