Loading [MathJax]/extensions/tex2jax.js

DUNE-FEM (unstable)

diagonalmatrix.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_DIAGONAL_MATRIX_HH
6#define DUNE_DIAGONAL_MATRIX_HH
7
12#include <algorithm>
13#include <cassert>
14#include <cmath>
15#include <complex>
16#include <cstddef>
17#include <initializer_list>
18#include <iostream>
19#include <memory>
20
29#include <dune/common/matrixconcepts.hh>
31
32
33namespace Dune {
34
35 template< class K, int n > class DiagonalRowVectorConst;
36 template< class K, int n > class DiagonalRowVector;
37 template< class DiagonalMatrixType > class DiagonalMatrixWrapper;
38 template< class C, class T, class R> class ContainerWrapperIterator;
39
54 template<class K, int n>
56 {
57 typedef DiagonalMatrixWrapper< DiagonalMatrix<K,n> > WrapperType;
58
59 public:
60 //===== type definitions and constants
61
63 typedef K value_type;
64 typedef value_type field_type;
65
67 typedef K block_type;
68
70 typedef std::size_t size_type;
71
73 constexpr static int blocklevel = 1;
74
76 typedef DiagonalRowVector<K,n> row_type;
77 typedef row_type reference;
78 typedef row_type row_reference;
79 typedef DiagonalRowVectorConst<K,n> const_row_type;
80 typedef const_row_type const_reference;
81 typedef const_row_type const_row_reference;
82
84 constexpr static int rows = n;
86 constexpr static int cols = n;
87
88 //==== size
89
90 static constexpr size_type size ()
91 {
92 return rows;
93 }
94
95 //===== constructors
96
98 constexpr DiagonalMatrix() = default;
99
101 DiagonalMatrix (const K& k)
102 : diag_(k)
103 {}
104
107 : diag_(diag)
108 {}
109
118 DiagonalMatrix (std::initializer_list<K> const &l)
119 {
120 std::copy_n(l.begin(), std::min<std::size_t>(rows, l.size()),
121 diag_.begin());
122 }
123
126 {
127 diag_ = k;
128 return *this;
129 }
130
132 bool identical(const DiagonalMatrix<K,n>& other) const
133 {
134 return (this==&other);
135 }
136
139 {
140 return *this;
141 }
142
143 //===== iterator interface to rows of the matrix
152
155 {
156 return Iterator(WrapperType(this),0);
157 }
158
161 {
162 return Iterator(WrapperType(this),n);
163 }
164
168 {
169 return Iterator(WrapperType(this),n-1);
170 }
171
175 {
176 return Iterator(WrapperType(this),-1);
177 }
178
179
188
191 {
192 return ConstIterator(WrapperType(this),0);
193 }
194
197 {
198 return ConstIterator(WrapperType(this),n);
199 }
200
204 {
205 return ConstIterator(WrapperType(this),n-1);
206 }
207
211 {
212 return ConstIterator(WrapperType(this),-1);
213 }
214
215
216
217 //===== vector space arithmetic
218
221 {
222 diag_ += y.diag_;
223 return *this;
224 }
225
228 {
229 diag_ -= y.diag_;
230 return *this;
231 }
232
235 {
236 diag_ += k;
237 return *this;
238 }
239
242 {
243 diag_ -= k;
244 return *this;
245 }
246
249 {
250 diag_ *= k;
251 return *this;
252 }
253
256 {
257 diag_ /= k;
258 return *this;
259 }
260
261 //===== comparison ops
262
264 bool operator==(const DiagonalMatrix& other) const
265 {
266 return diag_==other.diagonal();
267 }
268
270 bool operator!=(const DiagonalMatrix& other) const
271 {
272 return diag_!=other.diagonal();
273 }
274
275
276 //===== linear maps
277
279 template<class X, class Y>
280 void mv (const X& x, Y& y) const
281 {
282#ifdef DUNE_FMatrix_WITH_CHECKING
283 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
284 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
285#endif
286 for (size_type i=0; i<n; ++i)
287 y[i] = diag_[i] * x[i];
288 }
289
291 template<class X, class Y>
292 void mtv (const X& x, Y& y) const
293 {
294 mv(x, y);
295 }
296
298 template<class X, class Y>
299 void umv (const X& x, Y& y) const
300 {
301#ifdef DUNE_FMatrix_WITH_CHECKING
302 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
303 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
304#endif
305 for (size_type i=0; i<n; ++i)
306 y[i] += diag_[i] * x[i];
307 }
308
310 template<class X, class Y>
311 void umtv (const X& x, Y& y) const
312 {
313#ifdef DUNE_FMatrix_WITH_CHECKING
314 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
315 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
316#endif
317 for (size_type i=0; i<n; ++i)
318 y[i] += diag_[i] * x[i];
319 }
320
322 template<class X, class Y>
323 void umhv (const X& x, Y& y) const
324 {
325#ifdef DUNE_FMatrix_WITH_CHECKING
326 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
327 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
328#endif
329 for (size_type i=0; i<n; i++)
330 y[i] += conjugateComplex(diag_[i])*x[i];
331 }
332
334 template<class X, class Y>
335 void mmv (const X& x, Y& y) const
336 {
337#ifdef DUNE_FMatrix_WITH_CHECKING
338 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
339 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
340#endif
341 for (size_type i=0; i<n; ++i)
342 y[i] -= diag_[i] * x[i];
343 }
344
346 template<class X, class Y>
347 void mmtv (const X& x, Y& y) const
348 {
349#ifdef DUNE_FMatrix_WITH_CHECKING
350 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
351 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
352#endif
353 for (size_type i=0; i<n; ++i)
354 y[i] -= diag_[i] * x[i];
355 }
356
358 template<class X, class Y>
359 void mmhv (const X& x, Y& y) const
360 {
361#ifdef DUNE_FMatrix_WITH_CHECKING
362 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
363 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
364#endif
365 for (size_type i=0; i<n; i++)
366 y[i] -= conjugateComplex(diag_[i])*x[i];
367 }
368
370 template<class X, class Y>
371 void usmv (const typename FieldTraits<Y>::field_type & alpha,
372 const X& x, Y& y) const
373 {
374#ifdef DUNE_FMatrix_WITH_CHECKING
375 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
376 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
377#endif
378 for (size_type i=0; i<n; i++)
379 y[i] += alpha * diag_[i] * x[i];
380 }
381
383 template<class X, class Y>
384 void usmtv (const typename FieldTraits<Y>::field_type & alpha,
385 const X& x, Y& y) const
386 {
387#ifdef DUNE_FMatrix_WITH_CHECKING
388 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
389 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
390#endif
391 for (size_type i=0; i<n; i++)
392 y[i] += alpha * diag_[i] * x[i];
393 }
394
396 template<class X, class Y>
397 void usmhv (const typename FieldTraits<Y>::field_type & alpha,
398 const X& x, Y& y) const
399 {
400#ifdef DUNE_FMatrix_WITH_CHECKING
401 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
402 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
403#endif
404 for (size_type i=0; i<n; i++)
405 y[i] += alpha * conjugateComplex(diag_[i]) * x[i];
406 }
407
408 //===== norms
409
411 double frobenius_norm () const
412 {
413 return diag_.two_norm();
414 }
415
417 double frobenius_norm2 () const
418 {
419 return diag_.two_norm2();
420 }
421
423 double infinity_norm () const
424 {
425 return diag_.infinity_norm();
426 }
427
429 double infinity_norm_real () const
430 {
431 return diag_.infinity_norm_real();
432 }
433
434
435
436 //===== solve
437
439 template<class V>
440 void solve (V& x, const V& b) const
441 {
442 for (int i=0; i<n; i++)
443 x[i] = b[i]/diag_[i];
444 }
445
447 void invert()
448 {
449 using real_type = typename FieldTraits<K>::real_type;
450 for (int i=0; i<n; i++)
451 diag_[i] = real_type(1.0)/diag_[i];
452 }
453
455 K determinant () const
456 {
457 K det = diag_[0];
458 for (int i=1; i<n; i++)
459 det *= diag_[i];
460 return det;
461 }
462
463
464
465 //===== matrix-matrix multiplication
466
469 template <class OtherScalar>
470 friend auto operator* ( const DiagonalMatrix& matrixA,
471 const DiagonalMatrix<OtherScalar, n>& matrixB)
472 {
474 for(int i=0; i<n; ++i)
475 result.diagonal(i) = matrixA.diagonal(i)*matrixB.diagonal(i);
476 return result;
477 }
478
488 template <class OtherMatrix,
489 std::enable_if_t<(Impl::IsDenseMatrix<OtherMatrix>::value), int> = 0,
490 std::enable_if_t<(not Impl::IsFieldMatrix<OtherMatrix>::value), int> = 0>
491 friend auto operator* ( const DiagonalMatrix& matrixA,
492 const OtherMatrix& matrixB)
493 {
494 using OtherField = typename FieldTraits<OtherMatrix>::field_type;
495 using F = typename PromotionTraits<field_type, OtherField>::PromotedType;
496
497 auto result = [&]{
498 if constexpr (Impl::IsStaticSizeMatrix_v<OtherMatrix>) {
499 static_assert(n == OtherMatrix::rows);
501 } else {
502 assert(n == matrixB.N());
503 return DynamicMatrix<F>{n,matrixB.M()};
504 }
505 }();
506
507 for (int i = 0; i < result.N(); ++i)
508 for (int j = 0; j < result.M(); ++j)
509 result[i][j] = matrixA.diagonal(i) * matrixB[i][j];
510 return result;
511 }
512
513 //===== sizes
514
516 static constexpr size_type N ()
517 {
518 return n;
519 }
520
522 static constexpr size_type M ()
523 {
524 return n;
525 }
526
527
528
529 //===== query
530
532 bool exists (size_type i, size_type j) const
533 {
534 DUNE_ASSERT_BOUNDS(i >= 0 && i < n);
535 DUNE_ASSERT_BOUNDS(j >= 0 && j < n);
536 return i==j;
537 }
538
539
540
542 friend std::ostream& operator<< (std::ostream& s, const DiagonalMatrix<K,n>& a)
543 {
544 for (size_type i=0; i<n; i++) {
545 for (size_type j=0; j<n; j++)
546 s << ((i==j) ? a.diag_[i] : 0) << " ";
547 s << std::endl;
548 }
549 return s;
550 }
551
554 {
555 return reference(const_cast<K*>(&diag_[i]), i);
556 }
557
559 const_reference operator[](size_type i) const
560 {
561 return const_reference(const_cast<K*>(&diag_[i]), i);
562 }
563
565 const K& diagonal(size_type i) const
566 {
567 return diag_[i];
568 }
569
572 {
573 return diag_[i];
574 }
575
578 {
579 return diag_;
580 }
581
584 {
585 return diag_;
586 }
587
588 private:
589
590 // the data, a FieldVector storing the diagonal
591 FieldVector<K,n> diag_;
592 };
593
594 template< class K, int n >
595 struct FieldTraits< DiagonalMatrix<K,n> >
596 {
597 typedef typename FieldTraits<K>::field_type field_type;
598 typedef typename FieldTraits<K>::real_type real_type;
599 };
600
601
602#ifndef DOXYGEN // hide specialization
605 template< class K >
606 class DiagonalMatrix<K, 1> : public FieldMatrix<K, 1, 1>
607 {
608 typedef FieldMatrix<K,1,1> Base;
609 public:
611 typedef typename Base::size_type size_type;
612
615 constexpr static int blocklevel = 1;
616
617 typedef typename Base::row_type row_type;
618
619 typedef typename Base::row_reference row_reference;
620 typedef typename Base::const_row_reference const_row_reference;
621
624 constexpr static int rows = 1;
627 constexpr static int cols = 1;
628
629
631 constexpr DiagonalMatrix() = default;
632
634 DiagonalMatrix(const K& scalar)
635 {
636 (*this)[0][0] = scalar;
637 }
638
640 const K& diagonal(size_type) const
641 {
642 return (*this)[0][0];
643 }
644
647 {
648 return (*this)[0][0];
649 }
650
652 const FieldVector<K,1>& diagonal() const
653 {
654 return (*this)[0];
655 }
656
658 FieldVector<K,1>& diagonal()
659 {
660 return (*this)[0];
661 }
662
664 DiagonalMatrix<K, 1> transposed() const
665 {
666 return *this;
667 }
668
671 template <class OtherScalar>
672 friend auto operator* ( const DiagonalMatrix& matrixA,
673 const DiagonalMatrix<OtherScalar, 1>& matrixB)
674 {
675 return DiagonalMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType, 1>{matrixA.diagonal(0)*matrixB.diagonal(0)};
676 }
677
678 template <class OtherMatrix,
679 std::enable_if_t<(Impl::IsDenseMatrix<OtherMatrix>::value), int> = 0,
680 std::enable_if_t<(not Impl::IsFieldMatrix<OtherMatrix>::value), int> = 0>
681 friend auto operator* ( const DiagonalMatrix& matrixA,
682 const OtherMatrix& matrixB)
683 {
684 using OtherField = typename FieldTraits<OtherMatrix>::field_type;
685 using F = typename PromotionTraits<K, OtherField>::PromotedType;
686
687 auto result = [&]{
688 if constexpr (Impl::IsStaticSizeMatrix_v<OtherMatrix>) {
689 static_assert(1 == OtherMatrix::rows);
690 return FieldMatrix<F, 1, OtherMatrix::cols>{};
691 } else {
692 assert(1 == matrixB.N());
693 return DynamicMatrix<F>{1,matrixB.M()};
694 }
695 }();
696
697 for (int i = 0; i < result.N(); ++i)
698 for (int j = 0; j < result.M(); ++j)
699 result[i][j] = matrixA.diagonal(i) * matrixB[i][j];
700 return result;
701 }
702
703 };
704#endif
705
706
707 template<class DiagonalMatrixType>
708 class DiagonalMatrixWrapper
709 {
710 typedef typename DiagonalMatrixType::reference reference;
711 typedef typename DiagonalMatrixType::const_reference const_reference;
712 typedef typename DiagonalMatrixType::field_type K;
713 typedef DiagonalRowVector<K, DiagonalMatrixType::rows> row_type;
714 typedef std::size_t size_type;
715 typedef DiagonalMatrixWrapper< DiagonalMatrixType> MyType;
716
717 friend class ContainerWrapperIterator<const MyType, reference, reference>;
718 friend class ContainerWrapperIterator<const MyType, const_reference, const_reference>;
719
720 public:
721
722 DiagonalMatrixWrapper() :
723 mat_(0)
724 {}
725
726 DiagonalMatrixWrapper(const DiagonalMatrixType* mat) :
727 mat_(const_cast<DiagonalMatrixType*>(mat))
728 {}
729
730 size_type realIndex(int i) const
731 {
732 return i;
733 }
734
735 row_type* pointer(int i) const
736 {
737 row_ = row_type(&(mat_->diagonal(i)), i);
738 return &row_;
739 }
740
741 bool identical(const DiagonalMatrixWrapper& other) const
742 {
743 return mat_==other.mat_;
744 }
745
746 private:
747
748 mutable DiagonalMatrixType* mat_;
749 mutable row_type row_;
750 };
751
755 template< class K, int n >
756 class DiagonalRowVectorConst
757 {
758 template<class DiagonalMatrixType>
759 friend class DiagonalMatrixWrapper;
760 friend class ContainerWrapperIterator<DiagonalRowVectorConst<K,n>, const K, const K&>;
761
762 public:
763 // remember size of vector
764 constexpr static int dimension = n;
765
766 // standard constructor and everything is sufficient ...
767
768 //===== type definitions and constants
769
771 typedef K field_type;
772
774 typedef K block_type;
775
777 typedef std::size_t size_type;
778
780 constexpr static int blocklevel = 1;
781
783 constexpr static int size = n;
784
787 p_(0),
788 row_(0)
789 {}
790
792 explicit DiagonalRowVectorConst (K* p, int col) :
793 p_(p),
794 row_(col)
795 {}
796
797 //===== access to components
798
800 const K& operator[] ([[maybe_unused]] size_type i) const
801 {
802 DUNE_ASSERT_BOUNDS(i == row_);
803 return *p_;
804 }
805
806 // check if row is identical to other row (not only identical values)
807 // since this is a proxy class we need to check equality of the stored pointer
808 bool identical(const DiagonalRowVectorConst<K,n>& other) const
809 {
810 return ((p_ == other.p_)and (row_ == other.row_));
811 }
812
817
820 {
821 return ConstIterator(*this,0);
822 }
823
826 {
827 return ConstIterator(*this,1);
828 }
829
833 {
834 return ConstIterator(*this,0);
835 }
836
840 {
841 return ConstIterator(*this,-1);
842 }
843
845 bool operator== (const DiagonalRowVectorConst& y) const
846 {
847 return ((p_==y.p_)and (row_==y.row_));
848 }
849
850 //===== sizes
851
853 size_type N () const
854 {
855 return n;
856 }
857
859 size_type dim () const
860 {
861 return n;
862 }
863
866 {
867 return row_;
868 }
869
871 const K& diagonal() const
872 {
873 return *p_;
874 }
875
876 protected:
877
878 size_type realIndex([[maybe_unused]] int i) const
879 {
880 return rowIndex();
881 }
882
883 K* pointer([[maybe_unused]] size_type i) const
884 {
885 return const_cast<K*>(p_);
886 }
887
888 DiagonalRowVectorConst* operator&()
889 {
890 return this;
891 }
892
893 // the data, very simply a pointer to the diagonal value and the row number
894 K* p_;
895 size_type row_;
896 };
897
898 template< class K, int n >
899 class DiagonalRowVector : public DiagonalRowVectorConst<K,n>
900 {
901 template<class DiagonalMatrixType>
902 friend class DiagonalMatrixWrapper;
903 friend class ContainerWrapperIterator<DiagonalRowVector<K,n>, K, K&>;
904
905 public:
906 // standard constructor and everything is sufficient ...
907
908 //===== type definitions and constants
909
911 typedef K field_type;
912
914 typedef K block_type;
915
917 typedef std::size_t size_type;
918
920 DiagonalRowVector() : DiagonalRowVectorConst<K,n>()
921 {}
922
924 explicit DiagonalRowVector (K* p, int col) : DiagonalRowVectorConst<K,n>(p, col)
925 {}
926
927 //===== assignment from scalar
929 DiagonalRowVector& operator= (const K& k)
930 {
931 *p_ = k;
932 return *this;
933 }
934
935 //===== access to components
936
938 K& operator[] ([[maybe_unused]] size_type i)
939 {
940 DUNE_ASSERT_BOUNDS(i == row_);
941 return *p_;
942 }
943
948
951 {
952 return Iterator(*this, 0);
953 }
954
957 {
958 return Iterator(*this, 1);
959 }
960
964 {
965 return Iterator(*this, 0);
966 }
967
971 {
972 return Iterator(*this, -1);
973 }
974
979
980 using DiagonalRowVectorConst<K,n>::identical;
981 using DiagonalRowVectorConst<K,n>::operator[];
982 using DiagonalRowVectorConst<K,n>::operator==;
983 using DiagonalRowVectorConst<K,n>::begin;
984 using DiagonalRowVectorConst<K,n>::end;
985 using DiagonalRowVectorConst<K,n>::beforeEnd;
986 using DiagonalRowVectorConst<K,n>::beforeBegin;
987 using DiagonalRowVectorConst<K,n>::N;
988 using DiagonalRowVectorConst<K,n>::dim;
989 using DiagonalRowVectorConst<K,n>::rowIndex;
990 using DiagonalRowVectorConst<K,n>::diagonal;
991
992 protected:
993
994 DiagonalRowVector* operator&()
995 {
996 return this;
997 }
998
999 private:
1000
1001 using DiagonalRowVectorConst<K,n>::p_;
1002 using DiagonalRowVectorConst<K,n>::row_;
1003 };
1004
1005
1006 // implement type traits
1007 template<class K, int n>
1008 struct const_reference< DiagonalRowVector<K,n> >
1009 {
1010 typedef DiagonalRowVectorConst<K,n> type;
1011 };
1012
1013 template<class K, int n>
1014 struct const_reference< DiagonalRowVectorConst<K,n> >
1015 {
1016 typedef DiagonalRowVectorConst<K,n> type;
1017 };
1018
1019 template<class K, int n>
1020 struct mutable_reference< DiagonalRowVector<K,n> >
1021 {
1022 typedef DiagonalRowVector<K,n> type;
1023 };
1024
1025 template<class K, int n>
1026 struct mutable_reference< DiagonalRowVectorConst<K,n> >
1027 {
1028 typedef DiagonalRowVector<K,n> type;
1029 };
1030
1031
1032
1055 template<class CW, class T, class R>
1056 class ContainerWrapperIterator : public BidirectionalIteratorFacade<ContainerWrapperIterator<CW,T,R>,T, R, int>
1057 {
1058 typedef typename std::remove_const<CW>::type NonConstCW;
1059
1060 friend class ContainerWrapperIterator<CW, typename mutable_reference<T>::type, typename mutable_reference<R>::type>;
1061 friend class ContainerWrapperIterator<CW, typename const_reference<T>::type, typename const_reference<R>::type>;
1062
1063 typedef ContainerWrapperIterator<CW, typename mutable_reference<T>::type, typename mutable_reference<R>::type> MyType;
1064 typedef ContainerWrapperIterator<CW, typename const_reference<T>::type, typename const_reference<R>::type> MyConstType;
1065
1066 public:
1067
1068 // Constructors needed by the facade iterators.
1070 containerWrapper_(),
1071 position_(0)
1072 {}
1073
1074 ContainerWrapperIterator(CW containerWrapper, int position) :
1075 containerWrapper_(containerWrapper),
1076 position_(position)
1077 {}
1078
1079 template<class OtherContainerWrapperIteratorType>
1080 ContainerWrapperIterator(OtherContainerWrapperIteratorType& other) :
1081 containerWrapper_(other.containerWrapper_),
1082 position_(other.position_)
1083 {}
1084
1085 ContainerWrapperIterator(const MyType& other) :
1086 containerWrapper_(other.containerWrapper_),
1087 position_(other.position_)
1088 {}
1089
1091 containerWrapper_(other.containerWrapper_),
1092 position_(other.position_)
1093 {}
1094
1095 template<class OtherContainerWrapperIteratorType>
1096 ContainerWrapperIterator& operator=(OtherContainerWrapperIteratorType& other)
1097 {
1098 containerWrapper_ = other.containerWrapper_;
1099 position_ = other.position_;
1100 return *this;
1101 }
1102
1103 // This operator is needed since we can not get the address of the
1104 // temporary object returned by dereference
1105 T* operator->() const
1106 {
1107 return containerWrapper_.pointer(position_);
1108 }
1109
1110 // Methods needed by the forward iterator
1111 bool equals(const MyType& other) const
1112 {
1113 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1114 }
1115
1116 bool equals(const MyConstType& other) const
1117 {
1118 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1119 }
1120
1121 R dereference() const
1122 {
1123 return *containerWrapper_.pointer(position_);
1124 }
1125
1126 void increment()
1127 {
1128 ++position_;
1129 }
1130
1131 // Additional function needed by BidirectionalIterator
1132 void decrement()
1133 {
1134 --position_;
1135 }
1136
1137 // Additional function needed by RandomAccessIterator
1138 R elementAt(int i) const
1139 {
1140 return *containerWrapper_.pointer(position_+i);
1141 }
1142
1143 void advance(int n)
1144 {
1145 position_=position_+n;
1146 }
1147
1148 template<class OtherContainerWrapperIteratorType>
1149 std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType& other) const
1150 {
1151 assert(containerWrapper_.identical(other));
1152 return other.position_ - position_;
1153 }
1154
1155 std::ptrdiff_t index() const
1156 {
1157 return containerWrapper_.realIndex(position_);
1158 }
1159
1160 private:
1161 NonConstCW containerWrapper_;
1162 size_t position_;
1163 };
1164
1165 template <class DenseMatrix, class field, int N>
1167 static void apply(DenseMatrix& denseMatrix,
1168 DiagonalMatrix<field, N> const& rhs) {
1169#ifdef DUNE_CHECK_BOUNDS
1170 if (denseMatrix.M() != N || denseMatrix.N() != N)
1171 DUNE_THROW(Dune::RangeError, "Incompatible matrix dimensions in assignment.");
1172#endif // DUNE_CHECK_BOUNDS
1173
1174 denseMatrix = field(0);
1175 for (int i = 0; i < N; ++i)
1176 denseMatrix[i][i] = rhs.diagonal()[i];
1177 }
1178 };
1179 /* @} */
1180} // end namespace
1181#endif
Macro for wrapping boundary checks.
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:275
Iterator class for sparse vector-like containers.
Definition: diagonalmatrix.hh:1057
A dense n x m matrix.
Definition: densematrix.hh:145
constexpr size_type M() const
number of columns
Definition: densematrix.hh:708
constexpr size_type N() const
number of rows
Definition: densematrix.hh:702
constexpr FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition: densevector.hh:642
constexpr FieldTraits< value_type >::real_type two_norm2() const
square of two norm (sum over squared values of entries), need for block recursion
Definition: densevector.hh:651
constexpr FieldTraits< vt >::real_type infinity_norm() const
infinity norm (maximum of absolute values of entries)
Definition: densevector.hh:662
constexpr FieldTraits< vt >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: densevector.hh:678
constexpr Iterator begin()
begin iterator
Definition: densevector.hh:348
A diagonal matrix of static size.
Definition: diagonalmatrix.hh:56
Construct a matrix with a dynamic size.
Definition: dynmatrix.hh:61
Error thrown if operations of a FieldMatrix fail.
Definition: densematrix.hh:131
A dense n x m matrix.
Definition: fmatrix.hh:117
Default exception class for range errors.
Definition: exceptions.hh:346
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
A few common exception classes.
This file implements a dense matrix with dynamic numbers of rows and columns.
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Type traits to determine the type of reals (when working with complex numbers)
Implements a vector constructed from a given type representing a field and a compile-time given size.
Implements a generic iterator class for writing stl conformant iterators.
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:30
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:210
DiagonalMatrix< K, n > transposed() const
Return transposed of the matrix as DiagonalMatrix.
Definition: diagonalmatrix.hh:138
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: diagonalmatrix.hh:359
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition: diagonalmatrix.hh:248
std::size_t size_type
The type used for the index access and size operations.
Definition: diagonalmatrix.hh:70
size_type dim() const
dimension of the vector space
Definition: diagonalmatrix.hh:859
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:185
static constexpr int rows
The number of rows.
Definition: diagonalmatrix.hh:84
static constexpr size_type M()
number of blocks in column direction
Definition: diagonalmatrix.hh:522
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition: diagonalmatrix.hh:583
void usmhv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: diagonalmatrix.hh:397
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:947
static constexpr int blocklevel
The number of block levels we contain. This is the leaf, that is, 1.
Definition: diagonalmatrix.hh:73
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:771
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:832
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:187
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition: diagonalmatrix.hh:532
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:181
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:924
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition: diagonalmatrix.hh:571
void solve(V &x, const V &b) const
Solve system A x = b.
Definition: diagonalmatrix.hh:440
static constexpr size_type N()
number of blocks in row direction
Definition: diagonalmatrix.hh:516
Iterator beforeBegin()
Definition: diagonalmatrix.hh:970
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition: diagonalmatrix.hh:559
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:147
ConstIterator begin() const
begin ConstIterator
Definition: diagonalmatrix.hh:819
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:978
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition: diagonalmatrix.hh:227
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:792
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: diagonalmatrix.hh:347
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition: diagonalmatrix.hh:101
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:945
Iterator beforeEnd()
Definition: diagonalmatrix.hh:963
void umtv(const X &x, Y &y) const
y += A^T x
Definition: diagonalmatrix.hh:311
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:816
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: diagonalmatrix.hh:429
void umv(const X &x, Y &y) const
y += A x
Definition: diagonalmatrix.hh:299
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:145
void mv(const X &x, Y &y) const
y = A x
Definition: diagonalmatrix.hh:280
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: diagonalmatrix.hh:411
ConstIterator end() const
end iterator
Definition: diagonalmatrix.hh:196
static constexpr int cols
The number of columns.
Definition: diagonalmatrix.hh:86
size_type rowIndex() const
index of this row in surrounding matrix
Definition: diagonalmatrix.hh:865
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition: diagonalmatrix.hh:270
ConstIterator begin() const
begin iterator
Definition: diagonalmatrix.hh:190
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:950
void mmv(const X &x, Y &y) const
y -= A x
Definition: diagonalmatrix.hh:335
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition: diagonalmatrix.hh:255
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:183
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition: diagonalmatrix.hh:132
friend auto operator*(const DiagonalMatrix &matrixA, const DiagonalMatrix< OtherScalar, n > &matrixB)
Matrix-matrix multiplication.
Definition: diagonalmatrix.hh:470
Iterator end()
end iterator
Definition: diagonalmatrix.hh:160
DiagonalRowVector()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:920
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: diagonalmatrix.hh:417
constexpr DiagonalMatrix()=default
Default constructor.
void mtv(const X &x, Y &y) const
y = A^T x
Definition: diagonalmatrix.hh:292
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:976
void usmtv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: diagonalmatrix.hh:384
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition: diagonalmatrix.hh:577
void invert()
Compute inverse.
Definition: diagonalmatrix.hh:447
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition: diagonalmatrix.hh:565
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition: diagonalmatrix.hh:853
DiagonalMatrix(std::initializer_list< K > const &l)
Construct diagonal matrix from an initializer list.
Definition: diagonalmatrix.hh:118
row_type::Iterator ColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:151
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:911
reference operator[](size_type i)
Return reference object as row replacement.
Definition: diagonalmatrix.hh:553
Iterator end()
end iterator
Definition: diagonalmatrix.hh:956
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition: diagonalmatrix.hh:106
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:917
void usmv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A x
Definition: diagonalmatrix.hh:371
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: diagonalmatrix.hh:423
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition: diagonalmatrix.hh:220
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:67
void umhv(const X &x, Y &y) const
y += A^H x
Definition: diagonalmatrix.hh:323
Iterator beforeBegin()
Definition: diagonalmatrix.hh:174
ConstIterator end() const
end ConstIterator
Definition: diagonalmatrix.hh:825
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:777
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition: diagonalmatrix.hh:125
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:786
Iterator beforeEnd()
Definition: diagonalmatrix.hh:167
friend std::ostream & operator<<(std::ostream &s, const DiagonalMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition: diagonalmatrix.hh:542
K value_type
export the type representing the field
Definition: diagonalmatrix.hh:63
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:774
K determinant() const
calculates the determinant of this matrix
Definition: diagonalmatrix.hh:455
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:839
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:814
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:154
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition: diagonalmatrix.hh:264
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition: diagonalmatrix.hh:76
Iterator RowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:149
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:914
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:203
const K & diagonal() const
the diagonal value
Definition: diagonalmatrix.hh:871
#define DUNE_THROW(E,...)
Definition: exceptions.hh:312
constexpr EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:238
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:164
you have to specialize this structure for any type that should be assignable to a DenseMatrix
Definition: densematrix.hh:61
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:116
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 12, 23:28, 2025)