================ Lösungsvorschlag ================ Zum ersten Punkt: Hier kommt die Variable `last_value` hinzu, die wir in der _capture_ als Referenz aufnehmen müssen: ---- CODE (type=cpp) ---------------------------------------------------------- std::random_device random; std::mt19937 mt(random()); std::uniform_real_distribution uniform(1, 1<<16); double last_value = 0; initGeMatrix(A, [=,&last_value] (std::size_t i, std::size_t j) mutable -> double { last_value += uniform(mt); return last_value; }); ------------------------------------------------------------------------------- Die für die zweite Aufgabe entwickelte Template-Funktion `applyGeMatrix` ist recht ähnlich zu `initGeMatrix`. Nur die Zuweisung wird hier ersetzt durch den Aufruf von `apply`: ---- CODE (type=cpp) ---------------------------------------------------------- template void applyGeMatrix(GeMatrix &A, ApplyOperator apply) { for (typename GeMatrix::Index i = 0; i < A.m; ++i) { for (typename GeMatrix::Index j = 0; j < A.n; ++j) { apply(A(i, j), i, j); } } } ------------------------------------------------------------------------------- So könnte dann eine Initialisierung mit `applyGeMatrix` aussehen: ---- CODE (type=cpp) ---------------------------------------------------------- std::random_device random; std::mt19937 mt(random()); std::uniform_real_distribution uniform(1, 1<<16); double last_value = 0; applyGeMatrix(B, [=,&last_value] (double& Bij, std::size_t i, std::size_t j) mutable { last_value += uniform(mt); Bij = last_value; }); ------------------------------------------------------------------------------- Und die Berechnung der Summe aller Elemente: ---- CODE (type=cpp) ---------------------------------------------------------- double sum = 0; applyGeMatrix(B, [&sum] (double Bij, std::size_t i, std::size_t j) mutable { sum += Bij; }); printf("sum of all elements of B: %10.2lf\n", sum); ------------------------------------------------------------------------------- Und schließlich der Betrag des betragsgrößten Elements: ---- CODE (type=cpp) ---------------------------------------------------------- double amax = 0; applyGeMatrix(B, [&amax] (double& Bij, std::size_t i, std::size_t j) mutable { if (abs(Bij) > amax) amax = abs(Bij); }); printf("absolute max of B: %10.2lf\n", amax); ------------------------------------------------------------------------------- Für die letzte Aufgabe musste zunächst die Template-Klasse `GeMatrixCombinedConstView` erstellt werden: ---- CODE (type=cpp) ---------------------------------------------------------- template struct GeMatrixCombinedConstView { typedef typename std::common_type::type ElementType; typedef typename std::common_type::type Index; template GeMatrixCombinedConstView(const Matrix1& A, const Matrix2& B, ApplyOperator apply) : m(A.m), n(A.n), A(A), B(B), apply(apply) { assert(A.m == B.m && A.n == B.n); } const ElementType operator()(Index i, Index j) const { return apply(A(i, j), B(i, j)); } const Index m; const Index n; const Matrix1& A; const Matrix2& B; std::function apply; }; ------------------------------------------------------------------------------- Mit Hilfe von `GeMatrixCombinedConstView` lässt sich dann die Summe der Beträge der Differenzen zweier Matrizen leicht berechnen: ---- CODE (type=cpp) ---------------------------------------------------------- GeMatrixCombinedConstView, GeMatrix> C(A, B, [](double a, double b) -> double { return abs(a - b); }); printf("C:\n"); print_matrix(C); { double sum = 0; applyGeMatrix(C, [&sum] (double Cij, std::size_t i, std::size_t j) mutable { sum += Cij; }); printf("sum of all elements of C: %10.2lf\n", sum); } ------------------------------------------------------------------------------- Hier sind alle Dateien zum Lösungsvorschlag: :import: session11/step06/bench.h :import: session11/step06/gematrix.h :import: session11/step06/test_initmatrix.cpp :navigate: up -> doc:index back -> doc:session11/page05