KASKADE 7 development version
Loading...
Searching...
No Matches
dynamicMatrix.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2013-2025 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef DYNAMICMATRIX_HH
14#define DYNAMICMATRIX_HH
15
16#include "dune/common/config.h"
17
18#include "fem/fixdune.hh"
19#include "utilities/scalar.hh"
20
21
22#include "dune/common/dynvector.hh"
23#include "dune/common/densematrix.hh"
24#include "dune/common/fmatrix.hh"
25
26#include <tuple>
27#include <type_traits>
28#include <vector>
29
30namespace Dune
31{
32 // Forward declaration
33 template <class B, class A>
35}
36
37
39namespace Kaskade {
40 template<class K> class DynamicMatrix;
41
43
44 // We support DynamicMatrix<Dune::FieldMatrix<double,n,m>> as well as DynamicMatrix<double>.
45 // Occasionally we need the dimensions of the entry types. For FieldMatrix entries, these
46 // are member enums, while for doubles this is implicitly 1x1. For a uniform access, we define
47 // some traits class here.
48
49 // --------------------------------------------------------------------------------------------
50
51 template <class EntryA, class EntryB, class Enable=void> struct ProductTraits;
52
53 template <class Scalar>
54 struct ProductTraits<Scalar,Scalar,std::enable_if_t<std::is_arithmetic_v<Scalar>>>
55 {
56 using type = Scalar;
57 };
58
59 template <class EntryA, class EntryB, int n, int m, int k>
60 struct ProductTraits<Dune::FieldMatrix<EntryA,n,m>,Dune::FieldMatrix<EntryB,m,k>,void>
61 {
63 };
64
65 // --------------------------------------------------------------------------------------------
66
67
68 // The row vector class accessing DynamicMatrix rows. It simply stores a pointer to the
69 // first element, the number (of columns in the matrix) and the stride (number of rows,
70 // as the matrix stores elements column-major as in BLAS/LAPACK). It does not own or manage
71 // the memory.
72 // The interface adheres to the Dune::DenseVector requirements.
73 template <class K>
74 class StridedVector: public Dune::DenseVector<StridedVector<K>>
75 {
76 using Self = StridedVector<K>;
77 typedef Dune::DenseVector<Self> Base;
78 using Entry = std::remove_const_t<K>;
79
80 public:
81 using size_type = typename Base::size_type;
82 using value_type = K;
84
85 StridedVector(StridedVector const& x): Dune::DenseVector<StridedVector<K>>(), p(x.p), n(x.n), stride(x.stride) {}
86
93 StridedVector(K* p_, size_type n_, size_type stride_): p(p_), n(n_), stride(stride_) { }
94
96 StridedVector& operator=(K const& x) { for (size_type i=0; i<n; ++i) vec_access(i) = x; return *this; }
97
98 // for DUNE 2.4.x
99 value_type& vec_access(size_type i) { return *(p+i*stride); }
100 value_type const& vec_access(size_type i) const { return *(p+i*stride); }
101 size_type vec_size() const { return n; }
102 // for DUNE 2.5
103 value_type& _access(size_type i) { return *(p+i*stride); }
104 value_type const& _access(size_type i) const { return *(p+i*stride); }
105 size_type _size() const { return n; }
106 size_type size() const { return n; } // bug in Dune 2.5 DenseVector<>::size() calling Implementation's size()
107
109 value_type const& operator[] (size_type i) const { return vec_access(i); }
110
111 // With Dune 2.4.1 and GCC 5.3 there is some weird thing going on that leads to an infinite loop.
112 // Maybe due to wrong detection of enable_if in DenseVector::operator+=()? We provide a fix here.
113 template <class X>
115 {
116 for (size_type i=0; i<n; ++i)
117 (*this)[i] += x[i];
118 return *this;
119 }
120 template <class X>
122 {
123 for (size_type i=0; i<n; ++i)
124 (*this)[i] -= x[i];
125 return *this;
126 }
127
128 // Dune::DenseVector cannot treat FieldMatrices as entries as of Dune 2.4.1
130 {
131 real_type r = 0;
132 for (size_type i=0; i<n; ++i)
134 return r;
135 }
136
138 {
139 return std::sqrt(two_norm2());
140 }
141
142 private:
143 K* p;
144 size_type n;
145 size_type stride;
146 };
147
148 // Helper class for extracting a pointer to the first entry (or the entry itself in case of elementary types
149 template <class K>
151 {
152 static K * from(K & k) { return &k; }
153 static K const* from(K const& k) { return &k; }
154 };
155
156 template <class K, int n, int m>
157 struct GetAddress<Dune::FieldMatrix<K,n,m>>
158 {
160
162 static field_type const* from(Dune::FieldMatrix<K,n,m> const& k) { return GetAddress<K>::from(k[0][0]); }
163 };
164
165 template <class K, int n>
166 struct GetAddress<Dune::FieldVector<K,n>>
167 {
169
171 static field_type const* from(Dune::FieldVector<K,n> const& k) { return GetAddress<K>::from(k[0]); }
172 };
173
174 template <class K, class A>
175 struct GetAddress<Dune::BlockVector<K,A>>
176 {
178
180 static field_type const* from(Dune::BlockVector<K,A> const& k) { return GetAddress<K>::from(k[0]); }
181 };
182
183 template <class K>
184 auto getAddress(K& a)
185 {
186 return GetAddress<std::remove_const_t<K>>::from(a);
187 }
188
189 // Helper class for assigning Dune::FieldMatrix of different scalar type (which leads to segmentation faults as of Dune 2.4)
190 template <class A, class B>
191 struct Copy
192 {
193 static void apply(A const& a, B& b)
194 {
195 b = a;
196 assert(!std::isnan(b));
197 }
198 };
199
200 template <class A, class B, int n, int m>
201 struct Copy<Dune::FieldMatrix<A,n,m>, Dune::FieldMatrix<B,n,m>>
202 {
204 {
205 for (int i=0; i<n; ++i)
206 for (int j=0; j<n; ++j)
207 Copy<A,B>::apply(a[i][j],b[i][j]);
208 }
209 };
210
211
212 // matrix-vector multiplication calling BLAS
213 // y <- alpha*A*x + beta*y
214 void gemv(bool transpose, int n, int m, double alpha, double const* A, int lda, double const* x, double beta, double* y);
215 void gemv(bool transpose, int n, int m, float alpha, float const* A, int lda, float const* x, float beta, float * y);
216
223 void gemm(bool transposeA, bool transposeB, int m, int n, int k, double alpha, double const* A, int lda, double const* B, int ldb,
224 double beta, double* C, int ldc);
225 void gemm(bool transposeA, bool transposeB, int m, int n, int k, float alpha, float const* A, int lda, float const* B, int ldb,
226 float beta, float* C, int ldc);
227
228 inline double transpose(double x) { return x; }
229 inline float transpose(float x) { return x; }
230
231 // For accessing flat matrices
232 template <class Scalar>
233 Scalar& scalarEntry(Scalar& x, int row, int col)
234 {
235 return x;
236 }
237
238 template <class Entry, int n, int m>
240 {
241 using L = EntryTraits<Entry>;
242 return scalarEntry(A[row/L::rows][col/L::cols],row%L::rows,col%L::cols);
243 }
244
245 template <class Entry, int n, int m>
247 {
248 using L = EntryTraits<Entry>;
249 return scalarEntry(A[row/L::rows][col/L::cols],row%L::rows,col%L::cols);
250 }
251
252 template <class Entry>
254 {
255 using L = EntryTraits<Entry>;
256 return scalarEntry(A[row/L::rows][col/L::cols],row%L::rows,col%L::cols);
257 }
258
259 template <class Entry>
261 {
262 using L = EntryTraits<Entry>;
263 return scalarEntry(A[row/L::rows][col/L::cols],row%L::rows,col%L::cols);
264 }
265
266
267
268
269
270 template <class Scalar, class enable = typename std::enable_if_t<std::is_floating_point_v<Scalar>,int>>
272 {
273 static_assert(EntryTraits<Scalar>::rows == 1 && EntryTraits<Scalar>::cols==1,"Default specialization only for scalars!");
274 return A;
275 }
276
277 template <class Entry, int n, int m>
279 {
281
282 DynamicMatrix<typename EntryTraits<Entry>::field_type> B(L::rows*A.N(),L::cols*A.M());
283 for (int j=0; j<B.M(); ++j)
284 for (int i=0; i<B.N(); ++i)
285 B[i][j] = scalarEntry(A,i,j);
286 return B;
287 }
288
289 template <class Entry>
291 {
292 using L = EntryTraits<Entry>;
293
294 A.resize(B.N()/L::rows,B.M()/L::cols);
295
296 for (int j=0; j<B.M(); ++j)
297 for (int i=0; i<B.N(); ++i)
298 scalarEntry(A,i,j) = B[i][j];
299 }
300
301 }
302}
303
304namespace Dune
305{
306 namespace Impl
307 {
308 // As of Dune 2.11, Dune's DenseMatrixAssigner does not provide the assignment from a
309 // non-number matrix entry type to the matrix (e.g. *this = field_type() in DenseMatrix::invert()
310 // for block matrices). We provide it here for such entry types.
311 template <class DenseMatrix>
312 requires (!Dune::IsNumber<typename DenseMatrix::value_type>::value)
313 class DenseMatrixAssigner<DenseMatrix, typename DenseMatrix::value_type>
314 {
315 public:
316 static void apply(DenseMatrix& denseMatrix, typename DenseMatrix::value_type const& a)
317 {
318 std::fill(denseMatrix.begin(),denseMatrix.end(),a);
319 }
320 };
321 }
322
323 // type traits accessed by Dune::Dense matrix base class
324 template <class K>
325 struct DenseMatVecTraits<Kaskade::DynamicMatrix<K>>
326 {
330
333
334 typedef std::vector<K> container_type;
335 typedef K value_type;
336 typedef typename container_type::size_type size_type;
337 };
338
339 template <class K>
340 struct DenseMatVecTraits<Kaskade::DynamicMatrixDetail::StridedVector<K>>
341 {
344
345 typedef K value_type;
346 typedef typename std::vector<typename std::remove_const<K>::type>::size_type size_type;
347 };
348
349 template <class K>
350 struct FieldTraits<Kaskade::DynamicMatrix<K>>
351 {
352 typedef typename FieldTraits<K>::field_type field_type;
353 typedef typename FieldTraits<K>::real_type real_type;
354 };
355}
357
358namespace Kaskade {
375 template<class K>
376 class DynamicMatrix: public Dune::DenseMatrix<DynamicMatrix<K>>
377 {
378 typedef Dune::DenseMatrix<DynamicMatrix<K>> Base;
379 using Self = DynamicMatrix<K>;
380 typedef typename Base::row_reference row_reference;
381 typedef typename Base::const_row_reference const_row_reference;
383 typedef DynamicMatrixDetail::StridedVector<K const> const_row_base;
384 using ET = EntryTraits<K>;
385
386 public:
387 using size_type = typename Base::size_type;
388 using value_type = typename Base::value_type;
389 using field_type = typename Dune::FieldTraits<K>::field_type; // buggy in base class (defined as value_type there)
390 typedef typename Base::row_type row_type;
391
396
400 DynamicMatrix (): rs(0), cs(0), dat(1) {} // allocate at least one entry to prevent segmentation faults
401
405 DynamicMatrix(Self const& a) = default;
406
407 template <class OtherK>
408 friend class DynamicMatrix;
409
415 template <class OtherK>
417 : DynamicMatrix(A.N()*EntryTraits<OtherK>::rows/ET::rows,
418 A.M()*EntryTraits<OtherK>::cols/ET::cols)
419 {
420 constexpr int otherRows = EntryTraits<OtherK>::rows;
421 constexpr int otherCols = EntryTraits<OtherK>::cols;
422
423 int n = A.N()*otherRows; // number of scalar rows
424 int m = A.M()*otherCols; // and cols
425
426 assert(n % ET::rows == 0); // make sure the matrices can have the same scalar size
427 assert(m % ET::cols == 0);
428
429 // Copy scalar by scalar. Even if the entries have compatible row and column sizes,
430 // there need not be an assignment operator (e.g., one is a block matrix and the other one not).
431 for (int j=0; j<m; ++j)
432 for (int i=0; i<n; ++i)
434 }
435
439 DynamicMatrix (Self&& a) = default;
440
445 rs(r), cs(c), dat(r*c,v)
446 {}
447
451
455 using Base::operator=;
456
457
464
469
473 template <class OtherK>
475 {
477 "assignment only from compatible entries");
478 resize(A.N(),A.M());
479 copy(A.dat,dat);
480 return *this;
481 }
482
489 {
490 rs = r;
491 cs = c;
492 dat.resize(r*c);
493 }
494
501 {
502 resize(r,c);
503 }
504
505 void fill (K val)
506 {
507 for (int i=0; i<rs*cs; ++i)
508 dat[i]=val;
509 }
510
520 template <class RRange, class CRange>
521 DynamicMatrix operator()(RRange const& ridx, CRange const& cidx) const
522 {
523 DynamicMatrix B(ridx.size(),cidx.size());
524 for (int c=0; c<cidx.size(); ++c)
525 for (int r=0; r<ridx.size(); ++r)
526 B[r][c] = (*this)[ridx[r]][cidx[c]];
527 return B;
528 }
529
534
554 template<class X, class Y>
555 void mv(X const& x, Y& y) const
556 {
557 using namespace DynamicMatrixDetail;
558
559 auto xAddr = GetAddress<typename X::value_type>::from(x[0]);
560 auto yAddr = GetAddress<typename Y::value_type>::from(y[0]);
561
562 assert((void*)xAddr != (void*)yAddr);
563
564 if (ET::lapackLayout && rs>0)
565 // If the scalar entries are arranged in LAPACK memory layout (and there is anything to do),
566 // we just call BLAS.
567 gemv(false,rs*ET::rows,cs*ET::cols,1.0,data(),lda(),xAddr,0.0,yAddr);
568 else
569 // As of Dune 2.4, the DenseMatrix::mv implementation is buggy and prevents the use of
570 // non-scalar entry types. We provide a fixed version here. (2016-02) Apparently corrected
571 // in later Dune version.
572 for (size_type i=0; i<rs; ++i)
573 {
574 y[i] = 0; // this was initialized by value_type(0), which can be a matrix...
575 for (size_type j=0; j<cs; ++j)
576 y[i] += (*this)[i][j] * x[j];
577 }
578 }
579
586 template <class X, class Y>
587 void mtv(const X& x, Y& y) const
588 {
589 for (int i=0; i<this->M(); ++i) // we could use y = 0, but the loop works for std::vector as well
590 y[i] = 0;
591 umtv(x,y);
592 }
593
600 template <class X, class Y>
601 void umtv(const X& x, Y& y) const
602 {
603 usmtv(1.0,x,y);
604 }
605
617 template <class X, class Y>
618 void usmv(typename Dune::FieldTraits<Y>::field_type alpha, X const& x, Y& y) const
619 {
620 using namespace DynamicMatrixDetail;
621
622 if (ET::lapackLayout && rs>0)
623 gemv(false,rs*ET::rows,cs*ET::cols,alpha,data(),lda(),
624 GetAddress<typename X::value_type>::from(x[0]),
625 1.0,GetAddress<typename Y::value_type>::from(y[0]));
626 else
627 for (size_type i=0; i<rs; ++i)
628 {
629 typename Y::value_type tmp(0.0);
630 for (size_type j=0; j<cs; ++j)
631 tmp += (*this)[i][j] * x[j];
632 y[i] += alpha*tmp;
633 }
634 }
635
647 template <class X, class Y>
648 void usmtv(typename Dune::FieldTraits<Y>::field_type alpha, X const& x, Y& y) const
649 {
650 using namespace DynamicMatrixDetail;
651
652 if (ET::lapackLayout && rs>0)
653 gemv(true,rs*ET::rows,cs*ET::cols,alpha,data(),lda(),
654 GetAddress<typename X::value_type>::from(x[0]),
655 1.0,GetAddress<typename Y::value_type>::from(y[0]));
656 else
657 for(size_type i = 0; i<cs; ++i)
658 for(size_type j=0; j<rs; ++j)
659 y[i] += alpha * (transpose((*this)[j][i]) * x[j]);
660 }
661
670 {
671 for (K& a: dat)
672 a *= s;
673 return *this;
674 }
675
679
684
691 size_type lda() const
692 {
693 return rs*EntryTraits<K>::rows;
694 }
695
701
707
708 // make this thing a matrix in the sense of the Dune interface
709 size_type mat_rows() const { return rs; }
710 size_type mat_cols() const { return cs; }
711 row_reference mat_access(size_type i) { return row_reference(&dat[i],cs,rs); }
712 const_row_reference mat_access(size_type i) const { return const_row_reference(&dat[i],cs,rs); }
713
717
718 private:
719 size_type rs, cs;
720 std::vector<K> dat;
721
722 // The assignment Dune::FieldMatrix<float,3,3> = Dune::FieldMatrix<double,3,3> leads to a segmentation fault
723 // possibly after interminate recursion as of Dune 2.4. Thus we provide a fix. (2016-02)
724 template <class OtherK>
725 void copy(std::vector<OtherK> const& a, std::vector<K>& b)
726 {
727 for (size_t i=0; i<a.size(); ++i)
729 }
730 };
731
732 // --------------------------------------------------------------------------------------------------------
733 // --------------------------------------------------------------------------------------------------------
734
740 template <class Entry>
741 std::ostream& operator<<(std::ostream& out, DynamicMatrix<Entry> const& A)
742 {
743 using namespace DynamicMatrixDetail;
744 using L = EntryTraits<Entry>;
745
746 int n = A.N()*L::rows;
747 int m = A.M()*L::cols;
748
749 for (int i=0; i<n; ++i)
750 {
751 for (int j=0; j<m; ++j)
752 out << scalarEntry(A,i,j) << " ";
753 out << '\n';
754 }
755 return out;
756 }
757
758 template <class T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
760 {
761 return x;
762 }
763
770 template <class T, int n, int m>
772 {
774 for (int i=0; i<n; ++i)
775 for (int j=0; j<m; ++j)
776 At[j][i] = Kaskade::transpose(A[i][j]);
777 return At;
778 }
779
788 template <class Entry>
789 DynamicMatrix<typename EntryTraits<Entry>::transpose_type>
791 {
793 for (int j=0; j<C.M(); ++j)
794 for (int i=0; i<C.N(); ++i)
795 C[i][j] = Kaskade::transpose(A[j][i]);
796 return C;
797 }
798
799 // ----------------------------------------------------------------------------------------------
800
804 namespace DynamicMatrixDetail
805 {
806 template <class Scalar>
807 std::tuple<DynamicMatrix<Dune::FieldMatrix<Scalar,1,1>>,
808 std::vector<typename ScalarTraits<Scalar>::Real>,
810 svd(DynamicMatrix<Scalar>& A, bool fullU, bool fullV);
811 }
815
836 template <class Entry>
837 std::tuple<DynamicMatrix<Dune::FieldMatrix<typename EntryTraits<Entry>::field_type,1,1>>,
838 std::vector<typename EntryTraits<Entry>::real_type>,
839 DynamicMatrix<Dune::FieldMatrix<typename EntryTraits<Entry>::field_type,1,1>>>
840 svd(DynamicMatrix<Entry> const& A, bool fullU=true, bool fullV=true)
841 {
843 return DynamicMatrixDetail::svd(Acopy,fullU,fullV);
844 }
845
856 template <class Entry>
858 {
859 // Condition number is largest singular value.
860 // TODO: there are faster ways of computing the spectral norm (at least approximately and for square matrices).
861 auto sigma = std::get<1>(svd(A));
862 return *std::max_element(begin(sigma),end(sigma));
863 }
864
865
876 template <class Entry>
878 {
879 assert(A.N() == A.M());
880
881 // Condition number is ratio of largest and smallest singular value. Take care if a singular value is zero.
882 // TODO: there are faster ways of computing the condition number (at least in special cases).
883 auto sigma = std::get<1>(svd(A));
884 auto minmax = std::minmax_element(begin(sigma),end(sigma));
885 if (*minmax.first <= 0)
886 return std::numeric_limits<typename EntryTraits<Entry>::real_type>::infinity();
887 else
888 return *minmax.second / *minmax.first;
889 }
890
891 // -----------------------------------------------------------------------------------------------
892
902 template <class MEntry, class Vector>
903 Vector gesv(DynamicMatrix<MEntry> const& A, Vector const& b);
904
916 template <class MEntry, class Vector>
917 Vector posv(DynamicMatrix<MEntry> const& A, Vector const& b);
918
931 template <class MEntry, class Vector>
932 Vector gelss(DynamicMatrix<MEntry> const& A, Vector const& b);
933
934 // -----------------------------------------------------------------------------------------------
935
944 template <class Vector, class Matrix>
945 Vector project(Vector const& y, Matrix const& A);
946
953 template <class Scalar>
955
967 template <class Entry>
969
980 template <class Entry>
983
984
991 template <class Entry>
992 std::vector<typename EntryTraits<Entry>::real_type> eigenvalues(DynamicMatrix<Entry> A);
993
994
1004 template <class Entry>
1005 std::vector<typename EntryTraits<Entry>::real_type> eig(DynamicMatrix<Entry>& A,
1006 bool computeEigenvectors=true);
1007
1008
1014 template <class Scalar, int d>
1016
1017
1030 template <class Scalar, int d>
1032
1033
1041 template <class Scalar, int d>
1043
1044
1052 template <class Entry>
1054 typename ScalarTraits<typename EntryTraits<Entry>::field_type>::Real threshold = 0);
1055
1076 template <class Entry>
1077 std::tuple<std::vector<typename EntryTraits<Entry>::field_type>,
1079 std::vector<int>>
1081
1082 // ----------------------------------------------------------------------------------------------
1083 // ----------------------------------------------------------------------------------------------
1084
1085
1099 template<int dimIn, int dimOut>
1101 {
1102 assert( (dimIn == 1 && b.N()%dimOut == 0) || (dimIn > 1 && dimOut == 1) );
1103
1105
1106 if(dimIn == 1) // --> dimOut > 1
1107 {
1108 c.resize(b.N()/dimOut);
1109 for(auto i=c.begin(); i!=c.end(); ++i)
1110 for(int k=0; k<dimOut; ++k)
1111 c[i.index()][k] = b[i.index()*dimOut+k][0];
1112 } else // --> dimIn > 1 and dimOut == 1
1113 {
1114 c.resize(b.N()*dimIn);
1115 for(auto i=b.begin(); i!=b.end(); ++i)
1116 for(int k=0; k<dimIn; ++k)
1117 c[i.index()*dimIn+k][0] = b[i.index()][k];
1118 }
1119 return c;
1120 }
1121
1122}
1123
1124#endif
static void apply(DenseMatrix &denseMatrix, typename DenseMatrix::value_type const &a)
value_type const & _access(size_type i) const
Self & operator-=(StridedVector< X > const &x)
value_type const & vec_access(size_type i) const
StridedVector & operator=(StridedVector const &v)=default
typename EntryTraits< Entry >::real_type real_type
StridedVector(K *p_, size_type n_, size_type stride_)
Constructor.
Self & operator+=(StridedVector< X > const &x)
A LAPACK-compatible dense matrix class with shape specified at runtime.
field_type * data()
Raw access to data (for calling BLAS/LAPACK).
void setSize(size_type r, size_type c)
Resizes the matrix to r x c, leaving the entries in an undefined state.
void resize(size_type r, size_type c)
Resizes the matrix to r x c, leaving the entries in an undefined state.
void mv(X const &x, Y &y) const
matrix-vector product
row_reference mat_access(size_type i)
field_type const * data() const
Raw access to data (for calling BLAS/LAPACK).
DynamicMatrix & operator=(DynamicMatrix const &a)=default
Copy assignment.
size_type lda() const
The leading dimension (for calling BLAS/LAPACK).
size_type mat_rows() const
const_row_reference mat_access(size_type i) const
DynamicMatrix()
Creates a 0 x 0 matrix.
void umtv(const X &x, Y &y) const
update transpose matrix vector product .
size_type mat_cols() const
DynamicMatrix & operator=(DynamicMatrix &&a)=default
Move assignment.
DynamicMatrix & operator=(DynamicMatrix< OtherK > const &A)
Assignment from matrix with other scalar type.
DynamicMatrix(size_type r, size_type c, value_type v=value_type())
Creates a r x c matrix, initialized with given value.
typename Dune::FieldTraits< Dune::FieldMatrix< Scalar, 1, 1 > >::field_type field_type
DynamicMatrix & operator*=(field_type s)
Scales the matrix by a scalar: .
void usmv(typename Dune::FieldTraits< Y >::field_type alpha, X const &x, Y &y) const
update and scale matrix vector product .
DynamicMatrix operator()(RRange const &ridx, CRange const &cidx) const
Submatrix extraction.
std::ostream & operator<<(std::ostream &out, DynamicMatrix< Entry > const &A)
pretty output of dense matrices
DynamicMatrix(Self &&a)=default
Move constructor.
void usmtv(typename Dune::FieldTraits< Y >::field_type alpha, X const &x, Y &y) const
update and scale transposed matrix vector product .
DynamicMatrix(Self const &a)=default
Copy constructor.
void mtv(const X &x, Y &y) const
transpose matrix vector product
DynamicMatrix(DynamicMatrix< OtherK > const &A)
Copies from a matrix with different entry type.
Helper class for working with (real or complex) scalar field types.
Definition scalar.hh:36
This file contains various utility functions that augment the basic functionality of Dune.
std::vector< typename EntryTraits< Entry >::real_type > eig(DynamicMatrix< Entry > &A, bool computeEigenvectors=true)
Computes the eigenvalues and eigenvectors of a symmetric matrix .
Dune::FieldVector< Scalar, d > eigenvalues(Dune::FieldMatrix< Scalar, d, d > A)
Computes the eigenvalues of a symmetric matrix .
std::vector< typename EntryTraits< Entry >::real_type > eigenvalues(DynamicMatrix< Entry > A)
Computes the eigenvalues of a symmetric matrix .
Dune::FieldVector< Scalar, d > eig(Dune::FieldMatrix< Scalar, d, d > &A, bool computeEigenvectors=true)
Computes the eigenvalues and eigenvectors of a symmetric matrix .
Vector gesv(DynamicMatrix< MEntry > const &A, Vector const &b)
Computes the solution of for general invertible matrices.
DynamicMatrix< Entry > & invertSpd(DynamicMatrix< Entry > &A, bool symmetrize=true)
In place computation of the inverse .
Vector project(Vector const &y, Matrix const &A)
Computes the projection of onto the range of .
void invert(DynamicMatrix< Scalar > &A)
Computes the inverse .
DynamicMatrix< typename EntryTraits< Entry >::transpose_type > pseudoinverse(DynamicMatrix< Entry > A)
Computes the right pseudoinverse of a wide matrix such that .
std::tuple< std::vector< typename EntryTraits< Entry >::field_type >, DynamicMatrix< typename EntryTraits< Entry >::field_type >, std::vector< int > > qr(DynamicMatrix< Entry > const &A)
Computes a rank-revealing QR decomposition of using column pivoting.
Vector posv(DynamicMatrix< MEntry > const &A, Vector const &b)
Computes the solution of if is symmetric positive definite.
Vector gelss(DynamicMatrix< MEntry > const &A, Vector const &b)
Computes the solution of the standard least squares problem .
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
bool makePositiveSemiDefinite(DynamicMatrix< Entry > &A, typename ScalarTraits< typename EntryTraits< Entry >::field_type >::Real threshold=0)
Computes a positive semi-definite approximation to by cutting off negative eigenvalues.
bool makePositiveSemiDefinite(Dune::FieldMatrix< Scalar, d, d > &A)
Computes a positive semi-definite approximation to by cutting off negative eigenvalues.
EntryTraits< Entry >::real_type two_norm(DynamicMatrix< Entry > const &A)
Computes the spectral norm of the given matrix.
std::tuple< DynamicMatrix< Dune::FieldMatrix< typename EntryTraits< Entry >::field_type, 1, 1 > >, std::vector< typename EntryTraits< Entry >::real_type >, DynamicMatrix< Dune::FieldMatrix< typename EntryTraits< Entry >::field_type, 1, 1 > > > svd(DynamicMatrix< Entry > const &A, bool fullU=true, bool fullV=true)
Computes the singular value decomposition .
EntryTraits< Entry >::real_type condition(DynamicMatrix< Entry > const &A)
Computes the condition number of the given square matrix.
DynamicMatrix< typename EntryTraits< Entry >::transpose_type > transpose(DynamicMatrix< Entry > const &A)
Computes the transpose of a matrix .
void gemv(bool transpose, int n, int m, double alpha, double const *A, int lda, double const *x, double beta, double *y)
void unflatten(DynamicMatrix< Entry > &A, DynamicMatrix< typename EntryTraits< Entry >::field_type > const &B)
Scalar & scalarEntry(Scalar &x, int row, int col)
DynamicMatrix< Scalar > flatMatrix(DynamicMatrix< Scalar > const &A)
T transpose(T x)
Dune::BlockVector< Dune::FieldVector< double, dimOut > > reshapeBlocks(Dune::BlockVector< Dune::FieldVector< double, dimIn > > const &b)
reshapes a Dune::BlockVector block structure
Kaskade::DynamicMatrixDetail::StridedVector< K const > const_row_type
Kaskade::DynamicMatrixDetail::StridedVector< K > row_type
std::vector< typenamestd::remove_const< K >::type >::size_type size_type
static void apply(Dune::FieldMatrix< A, n, m > const &a, Dune::FieldMatrix< B, n, m > &b)
static void apply(A const &a, B &b)
static field_type const * from(Dune::BlockVector< K, A > const &k)
static field_type * from(Dune::BlockVector< K, A > &k)
static field_type const * from(Dune::FieldMatrix< K, n, m > const &k)
static field_type * from(Dune::FieldMatrix< K, n, m > &k)
static field_type const * from(Dune::FieldVector< K, n > const &k)
static field_type * from(Dune::FieldVector< K, n > &k)
static K const * from(K const &k)
A traits class for getting static information about block matrix entries.
Definition scalar.hh:76
Entry field_type
Definition scalar.hh:80
static real_type frobenius_norm2(Entry const &x)
Definition scalar.hh:84
typename ScalarTraits< field_type >::Real real_type
Definition scalar.hh:81
static int const rows
Definition scalar.hh:77
static int const lapackLayout
Definition scalar.hh:82
static int const cols
Definition scalar.hh:78