Commit 0bf48824 by Praetorius, Simon

### Merge branch 'feature/more_fieldmatvec_operations' into 'develop'

```Added some missing FieldVector and FieldMatrix operations

See merge request !62```
parents 0af5986d 5f82708b
Pipeline #1441 passed with stage
in 20 minutes and 49 seconds
 ... @@ -6,31 +6,40 @@ ... @@ -6,31 +6,40 @@ #include #include #include #include namespace std { template struct common_type, T> { using type = T; }; template struct common_type, T> { using type = T; }; } namespace Dune namespace Dune { { // some arithmetic operations with FieldVector // some arithmetic operations with FieldVector template FieldVector operator-(FieldVector v); template ::value,int> = 0 > std::enable_if_t::value, int> = 0> FieldVector operator*(FieldVector v, S factor); FieldVector operator*(FieldVector v, S factor); template ::value,int> = 0 > std::enable_if_t::value, int> = 0> FieldVector operator*(S factor, FieldVector v); FieldVector operator*(S factor, FieldVector v); template ::value,int> = 0 > std::enable_if_t::value, int> = 0> FieldVector operator/(FieldVector v, S factor); FieldVector operator/(FieldVector v, S factor); template FieldVector operator*(FieldVector const& v, FieldVector const& w); template FieldVector operator*(FieldVector const& factor, FieldVector v); template FieldVector operator*(FieldVector v, FieldVector const& factor); // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- /// Cross-product a 2d-vector = orthogonal vector /// Cross-product a 2d-vector = orthogonal vector ... @@ -45,10 +54,6 @@ namespace Dune ... @@ -45,10 +54,6 @@ namespace Dune template template auto dot(FieldVector const& vec1, FieldVector const& vec2); auto dot(FieldVector const& vec1, FieldVector const& vec2); template = 0> auto operator*(FieldVector const& v, FieldVector const& w); template template auto dot(FieldMatrix const& vec1, FieldMatrix const& vec2); auto dot(FieldMatrix const& vec1, FieldMatrix const& vec2); ... @@ -194,15 +199,15 @@ namespace Dune ... @@ -194,15 +199,15 @@ namespace Dune template ::value,int> = 0 > std::enable_if_t::value, int> = 0> FieldMatrix operator*(S scalar, FieldMatrix A); FieldMatrix operator*(S scalar, FieldMatrix A); template ::value,int> = 0 > std::enable_if_t::value, int> = 0> FieldMatrix operator*(FieldMatrix A, S scalar); FieldMatrix operator*(FieldMatrix A, S scalar); template ::value,int> = 0 > std::enable_if_t::value, int> = 0> FieldMatrix operator/(FieldMatrix A, S scalar); FieldMatrix operator/(FieldMatrix A, S scalar); ... @@ -216,19 +221,11 @@ namespace Dune ... @@ -216,19 +221,11 @@ namespace Dune template template FieldVector operator*(FieldMatrix const& mat, FieldVector const& vec); FieldVector operator*(FieldMatrix const& mat, FieldVector const& vec); template // ----------------------------------------------------------------------------- FieldMatrix operator*(FieldMatrix mat, FieldVector const& scalar); template FieldMatrix operator*(FieldMatrix mat, FieldVector const& scalar); template template FieldMatrix multiplies(FieldMatrix const& A, FieldMatrix const& B); FieldMatrix multiplies(FieldMatrix const& A, FieldMatrix const& B); template template FieldMatrix multiplies_AtB(FieldMatrix const& A, FieldMatrix const& B); FieldMatrix multiplies_AtB(FieldMatrix const& A, FieldMatrix const& B); ... @@ -241,6 +238,35 @@ namespace Dune ... @@ -241,6 +238,35 @@ namespace Dune template template FieldMatrix& multiplies_ABt(FieldMatrix const& A, DiagonalMatrix const& B, FieldMatrix& C); FieldMatrix& multiplies_ABt(FieldMatrix const& A, DiagonalMatrix const& B, FieldMatrix& C); // ----------------------------------------------------------------------------- template T operator*(FieldVector lhs, FieldVector rhs); template T operator*(FieldMatrix lhs, FieldMatrix rhs); template T operator*(FieldVector lhs, FieldMatrix rhs); template T operator*(FieldMatrix lhs, FieldVector rhs); // ----------------------------------------------------------------------------- template T const& at(FieldMatrix const& vec, std::size_t i); template T const& at(FieldMatrix const& vec, std::size_t i); // necessary specialization to resolve ambiguous calls template T const& at(FieldMatrix const& vec, std::size_t i); template T const& at(FieldVector const& vec, std::size_t i); } // end namespace Dune } // end namespace Dune namespace AMDiS namespace AMDiS ... ...
 ... @@ -13,45 +13,33 @@ namespace Dune { ... @@ -13,45 +13,33 @@ namespace Dune { // some arithmetic operations with FieldVector // some arithmetic operations with FieldVector template FieldVector operator-(FieldVector v) { return v *= -1; } template ::value,int> > std::enable_if_t::value, int>> FieldVector operator*(FieldVector v, S factor) FieldVector operator*(FieldVector v, S factor) { { return v *= factor; return v *= factor; } } template ::value,int> > std::enable_if_t::value, int>> FieldVector operator*(S factor, FieldVector v) FieldVector operator*(S factor, FieldVector v) { { return v *= factor; return v *= factor; } } template ::value,int> > std::enable_if_t::value, int>> FieldVector operator/(FieldVector v, S factor) FieldVector operator/(FieldVector v, S factor) { { return v /= factor; return v /= factor; } } template FieldVector operator*(FieldVector const& v, FieldVector const& w) { return {v[0] * w[0]}; } template FieldVector operator*(FieldVector const& factor, FieldVector v) { return v *= factor[0]; } template FieldVector operator*(FieldVector v, FieldVector const& factor) { return v *= factor[0]; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- /// Cross-product a 2d-vector = orthogonal vector /// Cross-product a 2d-vector = orthogonal vector ... @@ -77,14 +65,6 @@ auto dot(FieldVector const& vec1, FieldVector const& vec2) ... @@ -77,14 +65,6 @@ auto dot(FieldVector const& vec1, FieldVector const& vec2) return vec1.dot(vec2); return vec1.dot(vec2); } } template > auto operator*(FieldVector const& v, FieldVector const& w) { static_assert(M == N, "Requires vectors of the same type!"); return v.dot(w); } template template auto dot(FieldMatrix const& vec1, FieldMatrix const& vec2) auto dot(FieldMatrix const& vec1, FieldMatrix const& vec2) { { ... @@ -371,21 +351,21 @@ FieldMatrix trans(FieldMatrix const& A) ... @@ -371,21 +351,21 @@ FieldMatrix trans(FieldMatrix const& A) template ::value,int> > std::enable_if_t::value, int>> FieldMatrix operator*(S scalar, FieldMatrix A) FieldMatrix operator*(S scalar, FieldMatrix A) { { return A *= scalar; return A *= scalar; } } template ::value,int> > std::enable_if_t::value, int>> FieldMatrix operator*(FieldMatrix A, S scalar) FieldMatrix operator*(FieldMatrix A, S scalar) { { return A *= scalar; return A *= scalar; } } template ::value,int> > std::enable_if_t::value, int>> FieldMatrix operator/(FieldMatrix A, S scalar) FieldMatrix operator/(FieldMatrix A, S scalar) { { return A /= scalar; return A /= scalar; ... @@ -410,28 +390,12 @@ FieldVector operator*(FieldMatrix const& mat, FieldVector const ... @@ -410,28 +390,12 @@ FieldVector operator*(FieldMatrix const& mat, FieldVector const return Dune::FMatrixHelp::mult(mat, vec); return Dune::FMatrixHelp::mult(mat, vec); } } template FieldMatrix operator*(FieldMatrix mat, FieldVector const& scalar) { return mat *= scalar[0]; } template FieldMatrix operator*(FieldMatrix mat, FieldVector const& scalar) { return mat *= scalar[0]; } template template FieldMatrix multiplies(FieldMatrix const& A, FieldMatrix const& B) FieldMatrix multiplies(FieldMatrix const& A, FieldMatrix const& B) { { return A.rightmultiplyany(B); return A.rightmultiplyany(B); } } template template FieldMatrix multiplies_AtB(FieldMatrix const& A, FieldMatrix const& B) FieldMatrix multiplies_AtB(FieldMatrix const& A, FieldMatrix const& B) { { ... @@ -451,15 +415,7 @@ template ... @@ -451,15 +415,7 @@ template FieldMatrix multiplies_ABt(FieldMatrix const& A, FieldMatrix const& B) FieldMatrix multiplies_ABt(FieldMatrix const& A, FieldMatrix const& B) { { FieldMatrix C; FieldMatrix C; return multiplies_ABt(A,B,C); for (int m = 0; m < M; ++m) { for (int n = 0; n < N; ++n) { C[m][n] = 0; for (int l = 0; l < L; ++l) C[m][n] += A[m][l] * B[n][l]; } } return C; } } template template ... @@ -486,6 +442,56 @@ FieldMatrix& multiplies_ABt(FieldMatrix const& A, DiagonalMatri ... @@ -486,6 +442,56 @@ FieldMatrix& multiplies_ABt(FieldMatrix const& A, DiagonalMatri return C; return C; } } template T operator*(FieldVector lhs, FieldVector rhs) { return lhs[0] * rhs[0]; } template T operator*(FieldMatrix lhs, FieldMatrix rhs) { return lhs[0][0] * rhs[0][0]; } template T operator*(FieldVector lhs, FieldMatrix rhs) { return lhs[0] * rhs[0][0]; } template T operator*(FieldMatrix lhs, FieldVector rhs) { return lhs[0][0] * rhs[0]; } template T const& at(FieldMatrix const& vec, std::size_t i) { return vec[i][0]; } template T const& at(FieldMatrix const& vec, std::size_t i) { return vec[0][i]; } template T const& at(FieldMatrix const& vec, std::size_t i) { return vec[0][i]; } template T const& at(FieldVector const& vec, std::size_t i) { return vec[i]; } } // end namespace AMDiS } // end namespace AMDiS #endif #endif
 ... @@ -128,12 +128,88 @@ void test2() ... @@ -128,12 +128,88 @@ void test2() AMDIS_TEST_EQ( sol, a ); AMDIS_TEST_EQ( sol, a ); } } // test of scalar wrapper FieldVector and FieldMatrix void test3() { using V = FieldVector; using M = FieldMatrix; V a{1.0, 2.0, 3.0}; M A{ {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0} }; using VD = FieldVector; using MD = FieldMatrix; VD vd1 = 1.0; MD md1 = 1.0; using VI = FieldVector; using MI = FieldMatrix; VI vi1 = 1; MI mi1 = 1; // scale a vector AMDIS_TEST_EQ( 1*a, a ); AMDIS_TEST_EQ( 1.0*a, a ); AMDIS_TEST_EQ( a*1, a ); AMDIS_TEST_EQ( a*1.0, a ); AMDIS_TEST_EQ( vd1*a, a ); AMDIS_TEST_EQ( a*vd1, a ); AMDIS_TEST_EQ( vi1*a, a ); AMDIS_TEST_EQ( a*vi1, a ); AMDIS_TEST_EQ( md1*a, a ); AMDIS_TEST_EQ( a*md1, a ); AMDIS_TEST_EQ( mi1*a, a ); AMDIS_TEST_EQ( a*mi1, a ); AMDIS_TEST_EQ( a/1, a ); AMDIS_TEST_EQ( a/1.0, a ); AMDIS_TEST_EQ( a/vd1, a ); AMDIS_TEST_EQ( a/vi1, a ); AMDIS_TEST_EQ( a/md1, a ); AMDIS_TEST_EQ( a/mi1, a ); // scale a matrix AMDIS_TEST_EQ( 1*A, A ); AMDIS_TEST_EQ( 1.0*A, A ); AMDIS_TEST_EQ( A*1, A ); AMDIS_TEST_EQ( A*1.0, A ); AMDIS_TEST_EQ( vd1*A, A ); AMDIS_TEST_EQ( A*vd1, A ); AMDIS_TEST_EQ( vi1*A, A ); AMDIS_TEST_EQ( A*vi1, A ); AMDIS_TEST_EQ( md1*A, A ); AMDIS_TEST_EQ( A*md1, A ); AMDIS_TEST_EQ( mi1*A, A ); AMDIS_TEST_EQ( A*mi1, A ); AMDIS_TEST_EQ( A/1, A ); AMDIS_TEST_EQ( A/1.0, A ); AMDIS_TEST_EQ( A/vd1, A ); AMDIS_TEST_EQ( A/vi1, A ); AMDIS_TEST_EQ( A/md1, A ); AMDIS_TEST_EQ( A/mi1, A ); AMDIS_TEST_EQ( vd1*vd1, 1.0 ); AMDIS_TEST_EQ( vd1*md1, 1.0 ); AMDIS_TEST_EQ( md1*md1, 1.0 ); AMDIS_TEST_EQ( md1*vd1, 1.0 ); AMDIS_TEST_EQ( vd1*1.0, 1.0 ); AMDIS_TEST_EQ( md1*1.0, 1.0 ); AMDIS_TEST_EQ( 1.0*md1, 1.0 ); AMDIS_TEST_EQ( 1.0*vd1, 1.0 ); AMDIS_TEST_EQ( vi1*vi1, 1 ); AMDIS_TEST_EQ( vi1*mi1, 1 ); AMDIS_TEST_EQ( mi1*mi1, 1 ); AMDIS_TEST_EQ( mi1*vi1, 1 ); } int main() int main() { { test0(); test0(); test1(); test1(); test2(); test2(); test3(); return report_errors(); return report_errors(); } }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!