13#ifndef DYNAMICMATRIXOPS_HH
14#define DYNAMICMATRIXOPS_HH
33 template <
class MEntry,
class VEntry>
36 using YEntry =
decltype(A[0][0]*x[0]);
37 Dune::DynamicVector<YEntry> y(A.N());
50 template <
class MEntry,
class VEntry>
53 using YEntry =
decltype(A[0][0]*x[0]);
64 template <
class MEntry>
70 for (
int j=0; j<A.M(); ++j)
71 for (
int i=0; i<A.N(); ++i)
81 template <
class MEntry>
92 template <
class MEntry>
95 assert(A.N()==B.N() && A.M()==B.M());
97 for (
int i=0; i<A.N(); ++i)
98 for (
int j=0; j<A.N(); ++j)
108 template <
class MEntry>
111 assert(A.N()==B.N() && A.M()==B.M());
122 template <
class EntryA,
class EntryB>
126 using namespace DynamicMatrixDetail;
127 using EntryC =
typename ProductTraits<EntryA,EntryB>::type;
128 assert(A.M()==B.N() && C.N()==A.N() && C.M()==B.M());
132 gemm(
false,
false,A.N(),B.M(),A.M(),1.0,A.
data(),A.
lda(),B.
data(),B.
lda(),0.0,C.data(),C.lda());
134 for (
int j=0; j<B.M(); ++j)
135 for (
int i=0; i<A.N(); ++i)
138 for (
int l=0; l<A.M(); ++l)
139 C[i][j] += A[i][l]*B[l][j];
153 template <
class EntryA,
class EntryB>
157 using namespace DynamicMatrixDetail;
158 using EntryC =
typename ProductTraits<EntryA,EntryB>::type;
161 matrixMatrixProduct(A,B,C);
177 template <
class MEntry>
179 int rowstart,
int rowend,
int colstart,
int colend)
181 assert(rowend>=rowstart && colend>=colstart);
182 assert(0<=rowstart && 0<=colstart);
183 assert(rowend<=A.N() && colend<=A.M());
185 for (
int j=0; j<colend-colstart; ++j)
186 for (
int i=0; i<rowend-rowstart; ++i)
187 S[i][j] = A[i+rowstart][j+colstart];
205 template <
class EntryA,
class EntryB>
208 using EntryC =
decltype(A[0][0]*B[0][0]);
211 for (
int i=0; i<A.N(); ++i)
212 for (
int j=0; j<A.M(); ++j)
213 for (
int k=0; k<B.N(); ++k)
214 for (
int l=0; l<B.M(); ++l)
215 C[i*B.N()+k][j*B.M()+l] = A[i][j]*B[k][l];
222 namespace DynamicMatrixDetail
225 template <
class EntryA,
class EntryB>
230 using field_type =
typename EntryA::field_type;
231 static_assert(std::is_same<field_type,typename EntryB::field_type>::value,
"A and B need to have the same underlying field type!");
233 int const N = A.N()*EntryA::rows;
234 assert(N==B.N()*EntryB::rows);
243 static auto vert(Dune::DynamicVector<EntryA>
const& x, Dune::DynamicVector<EntryB>
const& y)
248 template <
class Entry>
253 assert(A.N()==B.N());
255 for (
int i=0; i<A.N(); ++i)
257 for (
int j=0; j<A.M(); ++j)
259 for (
int j=0; j<B.M(); ++j)
260 C[i][j+A.M()] = B[i][j];
265 static auto vert(Dune::DynamicVector<Entry>
const& x, Dune::DynamicVector<Entry>
const& y)
267 Dune::DynamicVector<Entry> z(x.N()+y.N());
268 for (
int i=0; i<x.N(); ++i)
270 for (
int i=0; i<y.N(); ++i)
284 template <
class EntryA,
class EntryB>
295 template <
class EntryX,
class EntryY>
296 auto vertcat(Dune::DynamicVector<EntryX>
const& x, Dune::DynamicVector<EntryY>
const& y)
308 template <
class Entry>
311 assert(A.M()==B.M());
313 for (
int j=0; j<A.M(); ++j)
315 for (
int i=0; i<A.N(); ++i)
317 for (
int i=0; i<B.N(); ++i)
318 C[i+A.N()][j] = B[i][j];
333 template <
class Entry>
338 for (
int i=0; i<n; ++i)
350 template <
class Entry,
int n>
355 for (
int i=0; i<n; ++i)
A LAPACK-compatible dense matrix class with shape specified at runtime.
field_type * data()
Raw access to data (for calling BLAS/LAPACK).
void mv(const X &x, Y &y) const
matrix-vector product
size_type lda() const
The leading dimension (for calling BLAS/LAPACK).
auto operator*(DynamicMatrix< MEntry > const &A, Dune::DynamicVector< VEntry > const &x)
matrix-vector product
void gemm(bool transposeA, bool transposeB, int m, int n, int k, double alpha, double const *A, int lda, double const *B, int ldb, double beta, double *C, int ldc)
computes
auto dynamicUnitMatrix(Entry const &d, int n)
Returns the diagonal matrix with the given entry replicated on the diagonal.
void matrixMatrixProduct(DynamicMatrix< EntryA > const &A, DynamicMatrix< EntryB > const &B, DynamicMatrix< typename DynamicMatrixDetail::ProductTraits< EntryA, EntryB >::type > &C)
matrix-matrix product
auto vertcat(Dune::DynamicVector< EntryX > const &x, Dune::DynamicVector< EntryY > const &y)
Concatenes two (column) vectors vertically.
DynamicMatrix< Entry > vertcat(DynamicMatrix< Entry > const &A, DynamicMatrix< Entry > const &B)
Concatenates two matrices vertically.
DynamicMatrix< typename DynamicMatrixDetail::ProductTraits< EntryA, EntryB >::type > operator*(DynamicMatrix< EntryA > const &A, DynamicMatrix< EntryB > const &B)
matrix-matrix product
auto operator+(DynamicMatrix< MEntry > A, DynamicMatrix< MEntry > const &B)
matrix addition
DynamicMatrix< MEntry > submatrix(DynamicMatrix< MEntry > const &A, int rowstart, int rowend, int colstart, int colend)
extracts contiguous submatrices, copying the data
auto operator*(typename Dune::FieldTraits< DynamicMatrix< MEntry > >::field_type x, DynamicMatrix< MEntry > A)
multiplication with scalar
auto operator*(DynamicMatrix< MEntry > const &A, Dune::BlockVector< VEntry > const &x)
matrix-vector product
auto operator-(DynamicMatrix< MEntry > const &A)
Multiplication with -1.
Dune::FieldMatrix< Entry, n, n > diag(Dune::FieldVector< Entry, n > const &d)
Creates a fixed-size diagonal matrix from the given diagonal vector.
auto kron(DynamicMatrix< EntryA > const &A, DynamicMatrix< EntryB > const &B)
Computes the Kronecker product of two matrices.
auto horzcat(DynamicMatrix< EntryA > const &A, DynamicMatrix< EntryB > const &B)
Concatenates two matrices horizontally.
auto operator-(DynamicMatrix< MEntry > A, DynamicMatrix< MEntry > const &B)
matrix subtraction
void unflatten(DynamicMatrix< Entry > &A, DynamicMatrix< typename EntryTraits< Entry >::field_type > const &B)
DynamicMatrix< Scalar > flatMatrix(DynamicMatrix< Scalar > const &A)
auto horzcat(Dune::FieldVector< Scalar, n > const &c1)
static auto vert(Dune::DynamicVector< Entry > const &x, Dune::DynamicVector< Entry > const &y)
static auto horz(DynamicMatrix< Entry > const &A, DynamicMatrix< Entry > const &B)
static auto horz(DynamicMatrix< EntryA > const &A, DynamicMatrix< EntryB > const &B)
static auto vert(Dune::DynamicVector< EntryA > const &x, Dune::DynamicVector< EntryB > const &y)