Dune Core Modules (2.7.1)

matrix.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 #ifndef DUNE_ISTL_MATRIX_HH
4 #define DUNE_ISTL_MATRIX_HH
5 
10 #include <cmath>
11 #include <memory>
12 
13 #include <dune/common/ftraits.hh>
17 
18 #include <dune/istl/bvector.hh>
19 #include <dune/istl/istlexception.hh>
20 
21 namespace Dune {
22 
23 namespace MatrixImp
24 {
36  template<class B, class A=std::allocator<B> >
37  class DenseMatrixBase : public Imp::block_vector_unmanaged<B,A>
38  // this derivation gives us all the blas level 1 and norms
39  // on the large array. However, access operators have to be
40  // overwritten.
41  {
42  public:
43 
44  //===== type definitions and constants
45 
47  using field_type = typename Imp::BlockTraits<B>::field_type;
48 
50  typedef A allocator_type;
51 
53  typedef typename A::size_type size_type;
54 
61 
65 
66  // just a shorthand
67  typedef Imp::BlockVectorWindow<B,A> window_type;
68 
69  typedef window_type reference;
70 
71  typedef const window_type const_reference;
72 
73 
74  //===== constructors and such
75 
79  DenseMatrixBase () : Imp::block_vector_unmanaged<B,A>()
80  {
81  // nothing is known ...
82  rows_ = 0;
83  columns_ = 0;
84  }
85 
92  DenseMatrixBase (size_type rows, size_type columns) : Imp::block_vector_unmanaged<B,A>()
93  {
94  // and we can allocate the big array in the base class
95  this->n = rows*columns;
96  columns_ = columns;
97  if (this->n>0)
98  {
99  this->p = allocator_.allocate(this->n);
100  new (this->p)B[this->n];
101  }
102  else
103  {
104  this->n = 0;
105  this->p = 0;
106  }
107 
108  // we can allocate the windows now
109  rows_ = rows;
110  }
111 
114  {
115  // allocate the big array in the base class
116  this->n = a.n;
117  columns_ = a.columns_;
118  if (this->n>0)
119  {
120  // allocate and construct objects
121  this->p = allocator_.allocate(this->n);
122  new (this->p)B[this->n];
123 
124  // copy data
125  for (size_type i=0; i<this->n; i++)
126  this->p[i]=a.p[i];
127  }
128  else
129  {
130  this->n = 0;
131  this->p = nullptr;
132  }
133 
134  // we can allocate the windows now
135  rows_ = a.rows_;
136  }
137 
140  {
141  if (this->n>0) {
142  size_type i=this->n;
143  while (i)
144  this->p[--i].~B();
145  allocator_.deallocate(this->p,this->n);
146  }
147  }
148 
150  void resize (size_type rows, size_type columns)
151  {
152  // deconstruct objects and deallocate memory if necessary
153  if (this->n>0) {
154  size_type i=this->n;
155  while (i)
156  this->p[--i].~B();
157  allocator_.deallocate(this->p,this->n);
158  }
159 
160  // and we can allocate the big array in the base class
161  this->n = rows*columns;
162  if (this->n>0)
163  {
164  this->p = allocator_.allocate(this->n);
165  new (this->p)B[this->n];
166  }
167  else
168  {
169  this->n = 0;
170  this->p = nullptr;
171  }
172 
173  // we can allocate the windows now
174  rows_ = rows;
175  columns_ = columns;
176  }
177 
180  {
181  if (&a!=this) // check if this and a are different objects
182  {
183  columns_ = a.columns_;
184  // reallocate arrays if necessary
185  // Note: still the block sizes may vary !
186  if (this->n!=a.n || rows_!=a.rows_)
187  {
188  // deconstruct objects and deallocate memory if necessary
189  if (this->n>0) {
190  size_type i=this->n;
191  while (i)
192  this->p[--i].~B();
193  allocator_.deallocate(this->p,this->n);
194  }
195 
196  // allocate the big array in the base class
197  this->n = a.n;
198  if (this->n>0)
199  {
200  // allocate and construct objects
201  this->p = allocator_.allocate(this->n);
202  new (this->p)B[this->n];
203  }
204  else
205  {
206  this->n = 0;
207  this->p = nullptr;
208  }
209 
210  // Copy number of rows
211  rows_ = a.rows_;
212  }
213 
214  // and copy the data
215  for (size_type i=0; i<this->n; i++)
216  this->p[i]=a.p[i];
217  }
218 
219  return *this;
220  }
221 
222 
223  //===== assignment from scalar
224 
227  {
228  (static_cast<Imp::block_vector_unmanaged<B,A>&>(*this)) = k;
229  return *this;
230  }
231 
232 
233  //===== access to components
234  // has to be overwritten from base class because it must
235  // return access to the windows
236 
238  reference operator[] (size_type i)
239  {
240 #ifdef DUNE_ISTL_WITH_CHECKING
241  if (i>=rows_) DUNE_THROW(ISTLError,"index out of range");
242 #endif
243  return window_type(this->p + i*columns_, columns_);
244  }
245 
247  const_reference operator[] (size_type i) const
248  {
249 #ifdef DUNE_ISTL_WITH_CHECKING
250  if (i<0 || i>=rows_) DUNE_THROW(ISTLError,"index out of range");
251 #endif
252  return window_type(this->p + i*columns_, columns_);
253  }
254 
255  // forward declaration
256  class ConstIterator;
257 
259  class Iterator
260  {
261  public:
264  : window_(nullptr,0)
265  {
266  i = 0;
267  }
268 
269  Iterator (Iterator& other) = default;
270  Iterator (Iterator&& other) = default;
271 
273  Iterator (B* data, size_type columns, size_type _i)
274  : i(_i),
275  window_(data + _i*columns, columns)
276  {}
277 
280  {
281  i = other.i;
282  // Do NOT use window_.operator=, because that copies the window content, not just the window!
283  window_.set(other.window_.getsize(),other.window_.getptr());
284  return *this;
285  }
286 
289  {
290  i = other.i;
291  // Do NOT use window_.operator=, because that copies the window content, not just the window!
292  window_.set(other.window_.getsize(),other.window_.getptr());
293  return *this;
294  }
295 
298  {
299  ++i;
300  window_.setptr(window_.getptr()+window_.getsize());
301  return *this;
302  }
303 
306  {
307  --i;
308  window_.setptr(window_.getptr()-window_.getsize());
309  return *this;
310  }
311 
313  bool operator== (const Iterator& it) const
314  {
315  return window_.getptr() == it.window_.getptr();
316  }
317 
319  bool operator!= (const Iterator& it) const
320  {
321  return window_.getptr() != it.window_.getptr();
322  }
323 
325  bool operator== (const ConstIterator& it) const
326  {
327  return window_.getptr() == it.window_.getptr();
328  }
329 
331  bool operator!= (const ConstIterator& it) const
332  {
333  return window_.getptr() != it.window_.getptr();
334  }
335 
337  window_type& operator* () const
338  {
339  return window_;
340  }
341 
343  window_type* operator-> () const
344  {
345  return &window_;
346  }
347 
348  // return index corresponding to pointer
349  size_type index () const
350  {
351  return i;
352  }
353 
354  friend class ConstIterator;
355 
356  private:
357  size_type i;
358  mutable window_type window_;
359  };
360 
363  {
364  return Iterator(this->p, columns_, 0);
365  }
366 
369  {
370  return Iterator(this->p, columns_, rows_);
371  }
372 
376  {
377  return Iterator(this->p, columns_, rows_-1);
378  }
379 
383  {
384  return Iterator(this->p, columns_, -1);
385  }
386 
389  {
390  return Iterator(this->p, columns_, std::min(i,rows_));
391  }
392 
395  {
396  return ConstIterator(this->p, columns_, std::min(i,rows_));
397  }
398 
401  {
402  public:
405  : window_(nullptr,0)
406  {
407  i = 0;
408  }
409 
411  ConstIterator (const B* data, size_type columns, size_type _i)
412  : i(_i),
413  window_(const_cast<B*>(data + _i * columns), columns)
414  {}
415 
418  : i(it.i), window_(it.window_.getptr(),it.window_.getsize())
419  {}
420 
421  ConstIterator& operator=(Iterator&& other)
422  {
423  i = other.i;
424  // Do NOT use window_.operator=, because that copies the window content, not just the window!
425  window_.set(other.window_.getsize(),other.window_.getptr());
426  return *this;
427  }
428 
429  ConstIterator& operator=(Iterator& other)
430  {
431  i = other.i;
432  // Do NOT use window_.operator=, because that copies the window content, not just the window!
433  window_.set(other.window_.getsize(),other.window_.getptr());
434  return *this;
435  }
436 
439  {
440  ++i;
441  window_.setptr(window_.getptr()+window_.getsize());
442  return *this;
443  }
444 
447  {
448  --i;
449  window_.setptr(window_.getptr()-window_.getsize());
450  return *this;
451  }
452 
454  bool operator== (const ConstIterator& it) const
455  {
456  return window_.getptr() == it.window_.getptr();
457  }
458 
460  bool operator!= (const ConstIterator& it) const
461  {
462  return window_.getptr() != it.window_.getptr();
463  }
464 
466  bool operator== (const Iterator& it) const
467  {
468  return window_.getptr() == it.window_.getptr();
469  }
470 
472  bool operator!= (const Iterator& it) const
473  {
474  return window_.getptr() != it.window_.getptr();
475  }
476 
478  const window_type& operator* () const
479  {
480  return window_;
481  }
482 
484  const window_type* operator-> () const
485  {
486  return &window_;
487  }
488 
489  // return index corresponding to pointer
490  size_type index () const
491  {
492  return i;
493  }
494 
495  friend class Iterator;
496 
497  private:
498  size_type i;
499  mutable window_type window_;
500  };
501 
504 
507 
510  {
511  return ConstIterator(this->p, columns_, 0);
512  }
513 
516  {
517  return ConstIterator(this->p, columns_, rows_);
518  }
519 
523  {
524  return ConstIterator(this->p, columns_, rows_-1);
525  }
526 
529  {
530  return ConstIterator(this->p, columns_, -1);
531  }
532 
533  //===== sizes
534 
536  size_type N () const
537  {
538  return rows_;
539  }
540 
541 
542  private:
543  size_type rows_; // number of matrix rows
544  size_type columns_; // number of matrix columns
545 
546  A allocator_;
547  };
548 
549 } // namespace MatrixImp
550 
556  template<class T, class A=std::allocator<T> >
557  class Matrix
558  {
559  public:
560 
562  using field_type = typename Imp::BlockTraits<T>::field_type;
563 
565  typedef T block_type;
566 
568  typedef A allocator_type;
569 
571  typedef typename MatrixImp::DenseMatrixBase<T,A>::window_type row_type;
572 
574  typedef typename A::size_type size_type;
575 
578 
580  typedef typename row_type::iterator ColIterator;
581 
584 
586  typedef typename row_type::const_iterator ConstColIterator;
587 
589  static constexpr unsigned int blocklevel = Imp::BlockTraits<T>::blockLevel()+1;
590 
592  Matrix() : data_(0,0), cols_(0)
593  {}
594 
597  Matrix(size_type rows, size_type cols) : data_(rows,cols), cols_(cols)
598  {}
599 
604  void setSize(size_type rows, size_type cols) {
605  data_.resize(rows,cols);
606  cols_ = cols;
607  }
608 
611  {
612  return data_.begin();
613  }
614 
617  {
618  return data_.end();
619  }
620 
624  {
625  return data_.beforeEnd();
626  }
627 
631  {
632  return data_.beforeBegin();
633  }
634 
637  {
638  return data_.begin();
639  }
640 
643  {
644  return data_.end();
645  }
646 
650  {
651  return data_.beforeEnd();
652  }
653 
657  {
658  return data_.beforeBegin();
659  }
660 
663  {
664  data_ = t;
665  return *this;
666  }
667 
670 #ifdef DUNE_ISTL_WITH_CHECKING
671  if (row<0)
672  DUNE_THROW(ISTLError, "Can't access negative rows!");
673  if (row>=N())
674  DUNE_THROW(ISTLError, "Row index out of range!");
675 #endif
676  return data_[row];
677  }
678 
680  const row_type operator[](size_type row) const {
681 #ifdef DUNE_ISTL_WITH_CHECKING
682  if (row<0)
683  DUNE_THROW(ISTLError, "Can't access negative rows!");
684  if (row>=N())
685  DUNE_THROW(ISTLError, "Row index out of range!");
686 #endif
687  return data_[row];
688  }
689 
691  size_type N() const {
692  return data_.N();
693  }
694 
696  size_type M() const {
697  return cols_;
698  }
699 
701  Matrix<T>& operator*=(const field_type& scalar) {
702  data_ *= scalar;
703  return (*this);
704  }
705 
707  Matrix<T>& operator/=(const field_type& scalar) {
708  data_ /= scalar;
709  return (*this);
710  }
711 
717  Matrix& operator+= (const Matrix& b) {
718 #ifdef DUNE_ISTL_WITH_CHECKING
719  if(N()!=b.N() || M() != b.M())
720  DUNE_THROW(RangeError, "Matrix sizes do not match!");
721 #endif
722  data_ += b.data_;
723  return (*this);
724  }
725 
731  Matrix& operator-= (const Matrix& b) {
732 #ifdef DUNE_ISTL_WITH_CHECKING
733  if(N()!=b.N() || M() != b.M())
734  DUNE_THROW(RangeError, "Matrix sizes do not match!");
735 #endif
736  data_ -= b.data_;
737  return (*this);
738  }
739 
741  Matrix transpose() const {
742  Matrix out(M(), N());
743  for (size_type i=0; i<N(); i++)
744  for (size_type j=0; j<M(); j++)
745  out[j][i] = (*this)[i][j];
746 
747  return out;
748  }
749 
751  friend Matrix<T> operator*(const Matrix<T>& m1, const Matrix<T>& m2) {
752  Matrix<T> out(m1.N(), m2.M());
753  out = 0;
754 
755  for (size_type i=0; i<out.N(); i++ ) {
756  for ( size_type j=0; j<out.M(); j++ )
757  for (size_type k=0; k<m1.M(); k++)
758  out[i][j] += m1[i][k]*m2[k][j];
759  }
760 
761  return out;
762  }
763 
765  template <class X, class Y>
766  friend Y operator*(const Matrix<T>& m, const X& vec) {
767 #ifdef DUNE_ISTL_WITH_CHECKING
768  if (m.M()!=vec.size())
769  DUNE_THROW(ISTLError, "Vector size doesn't match the number of matrix columns!");
770 #endif
771  Y out(m.N());
772  out = 0;
773 
774  for (size_type i=0; i<out.size(); i++ ) {
775  for ( size_type j=0; j<vec.size(); j++ )
776  out[i] += m[i][j]*vec[j];
777  }
778 
779  return out;
780  }
781 
783  template <class X, class Y>
784  void mv(const X& x, Y& y) const
785  {
786 #ifdef DUNE_ISTL_WITH_CHECKING
787  if (x.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
788  if (y.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
789 #endif
790  for (size_type i=0; i<data_.N(); i++) {
791  y[i]=0;
792  for (size_type j=0; j<cols_; j++)
793  {
794  auto&& xj = Impl::asVector(x[j]);
795  auto&& yi = Impl::asVector(y[i]);
796  Impl::asMatrix((*this)[i][j]).umv(xj, yi);
797  }
798  }
799  }
800 
802  template<class X, class Y>
803  void mtv (const X& x, Y& y) const
804  {
805 #ifdef DUNE_ISTL_WITH_CHECKING
806  if (x.N()!=N()) DUNE_THROW(ISTLError,"index out of range");
807  if (y.N()!=M()) DUNE_THROW(ISTLError,"index out of range");
808 #endif
809  for(size_type i=0; i<y.N(); ++i)
810  y[i]=0;
811  umtv(x,y);
812  }
813 
815  template <class X, class Y>
816  void umv(const X& x, Y& y) const
817  {
818 #ifdef DUNE_ISTL_WITH_CHECKING
819  if (x.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
820  if (y.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
821 #endif
822  for (size_type i=0; i<data_.N(); i++)
823  for (size_type j=0; j<cols_; j++)
824  {
825  auto&& xj = Impl::asVector(x[j]);
826  auto&& yi = Impl::asVector(y[i]);
827  Impl::asMatrix((*this)[i][j]).umv(xj, yi);
828  }
829  }
830 
832  template<class X, class Y>
833  void mmv (const X& x, Y& y) const
834  {
835 #ifdef DUNE_ISTL_WITH_CHECKING
836  if (x.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
837  if (y.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
838 #endif
839  for (size_type i=0; i<data_.N(); i++)
840  for (size_type j=0; j<cols_; j++)
841  {
842  auto&& xj = Impl::asVector(x[j]);
843  auto&& yi = Impl::asVector(y[i]);
844  Impl::asMatrix((*this)[i][j]).mmv(xj, yi);
845  }
846  }
847 
849  template <class X, class Y>
850  void usmv(const field_type& alpha, const X& x, Y& y) const
851  {
852 #ifdef DUNE_ISTL_WITH_CHECKING
853  if (x.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
854  if (y.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
855 #endif
856  for (size_type i=0; i<data_.N(); i++)
857  for (size_type j=0; j<cols_; j++)
858  {
859  auto&& xj = Impl::asVector(x[j]);
860  auto&& yi = Impl::asVector(y[i]);
861  Impl::asMatrix((*this)[i][j]).usmv(alpha, xj, yi);
862  }
863  }
864 
866  template<class X, class Y>
867  void umtv (const X& x, Y& y) const
868  {
869 #ifdef DUNE_ISTL_WITH_CHECKING
870  if (x.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
871  if (y.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
872 #endif
873  for (size_type i=0; i<data_.N(); i++)
874  for (size_type j=0; j<cols_; j++)
875  {
876  auto&& xi = Impl::asVector(x[i]);
877  auto&& yj = Impl::asVector(y[j]);
878  Impl::asMatrix((*this)[i][j]).umtv(xi, yj);
879  }
880  }
881 
883  template<class X, class Y>
884  void mmtv (const X& x, Y& y) const
885  {
886 #ifdef DUNE_ISTL_WITH_CHECKING
887  if (x.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
888  if (y.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
889 #endif
890  for (size_type i=0; i<data_.N(); i++)
891  for (size_type j=0; j<cols_; j++)
892  {
893  auto&& xi = Impl::asVector(x[i]);
894  auto&& yj = Impl::asVector(y[j]);
895  Impl::asMatrix((*this)[i][j]).mmtv(xi, yj);
896  }
897  }
898 
900  template<class X, class Y>
901  void usmtv (const field_type& alpha, const X& x, Y& y) const
902  {
903 #ifdef DUNE_ISTL_WITH_CHECKING
904  if (x.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
905  if (y.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
906 #endif
907  for (size_type i=0; i<data_.N(); i++)
908  for (size_type j=0; j<cols_; j++)
909  {
910  auto&& xi = Impl::asVector(x[i]);
911  auto&& yj = Impl::asVector(y[j]);
912  Impl::asMatrix((*this)[i][j]).usmtv(alpha, xi, yj);
913  }
914  }
915 
917  template<class X, class Y>
918  void umhv (const X& x, Y& y) const
919  {
920 #ifdef DUNE_ISTL_WITH_CHECKING
921  if (x.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
922  if (y.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
923 #endif
924  for (size_type i=0; i<data_.N(); i++)
925  for (size_type j=0; j<cols_; j++)
926  {
927  auto&& xi = Impl::asVector(x[i]);
928  auto&& yj = Impl::asVector(y[j]);
929  Impl::asMatrix((*this)[i][j]).umhv(xi,yj);
930  }
931  }
932 
934  template<class X, class Y>
935  void mmhv (const X& x, Y& y) const
936  {
937 #ifdef DUNE_ISTL_WITH_CHECKING
938  if (x.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
939  if (y.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
940 #endif
941  for (size_type i=0; i<data_.N(); i++)
942  for (size_type j=0; j<cols_; j++)
943  {
944  auto&& xi = Impl::asVector(x[i]);
945  auto&& yj = Impl::asVector(y[j]);
946  Impl::asMatrix((*this)[i][j]).mmhv(xi,yj);
947  }
948  }
949 
951  template<class X, class Y>
952  void usmhv (const field_type& alpha, const X& x, Y& y) const
953  {
954 #ifdef DUNE_ISTL_WITH_CHECKING
955  if (x.N()!=N()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
956  if (y.N()!=M()) DUNE_THROW(ISTLError,"vector/matrix size mismatch!");
957 #endif
958  for (size_type i=0; i<data_.N(); i++)
959  for (size_type j=0; j<cols_; j++)
960  {
961  auto&& xi = Impl::asVector(x[i]);
962  auto&& yj = Impl::asVector(y[j]);
963  Impl::asMatrix((*this)[i][j]).usmhv(alpha,xi,yj);
964  }
965  }
966 
967  //===== norms
968 
970  typename FieldTraits<field_type>::real_type frobenius_norm () const
971  {
972  return std::sqrt(frobenius_norm2());
973  }
974 
976  typename FieldTraits<field_type>::real_type frobenius_norm2 () const
977  {
978  typename FieldTraits<field_type>::real_type sum=0;
979  for (size_type i=0; i<this->N(); i++)
980  for (size_type j=0; j<this->M(); j++)
981  sum += Impl::asMatrix(data_[i][j]).frobenius_norm2();
982  return sum;
983  }
984 
986  template <typename ft = field_type,
987  typename std::enable_if<!HasNaN<ft>::value, int>::type = 0>
988  typename FieldTraits<ft>::real_type infinity_norm() const {
989  using real_type = typename FieldTraits<ft>::real_type;
990  using std::max;
991 
992  real_type norm = 0;
993  for (auto const &x : *this) {
994  real_type sum = 0;
995  for (auto const &y : x)
996  sum += Impl::asMatrix(y).infinity_norm();
997  norm = max(sum, norm);
998  isNaN += sum;
999  }
1000 
1001  return norm;
1002  }
1003 
1005  template <typename ft = field_type,
1006  typename std::enable_if<!HasNaN<ft>::value, int>::type = 0>
1007  typename FieldTraits<ft>::real_type infinity_norm_real() const {
1008  using real_type = typename FieldTraits<ft>::real_type;
1009  using std::max;
1010 
1011  real_type norm = 0;
1012  for (auto const &x : *this) {
1013  real_type sum = 0;
1014  for (auto const &y : x)
1015  sum += Impl::asMatrix(y).infinity_norm_real();
1016  norm = max(sum, norm);
1017  }
1018  return norm;
1019  }
1020 
1022  template <typename ft = field_type,
1023  typename std::enable_if<HasNaN<ft>::value, int>::type = 0>
1024  typename FieldTraits<ft>::real_type infinity_norm() const {
1025  using real_type = typename FieldTraits<ft>::real_type;
1026  using std::max;
1027 
1028  real_type norm = 0;
1029  real_type isNaN = 1;
1030  for (auto const &x : *this) {
1031  real_type sum = 0;
1032  for (auto const &y : x)
1033  sum += Impl::asMatrix(y).infinity_norm();
1034  norm = max(sum, norm);
1035  isNaN += sum;
1036  }
1037 
1038  return norm * (isNaN / isNaN);
1039  }
1040 
1042  template <typename ft = field_type,
1043  typename std::enable_if<HasNaN<ft>::value, int>::type = 0>
1044  typename FieldTraits<ft>::real_type infinity_norm_real() const {
1045  using real_type = typename FieldTraits<ft>::real_type;
1046  using std::max;
1047 
1048  real_type norm = 0;
1049  real_type isNaN = 1;
1050  for (auto const &x : *this) {
1051  real_type sum = 0;
1052  for (auto const &y : x)
1053  sum += Impl::asMatrix(y).infinity_norm_real();
1054  norm = max(sum, norm);
1055  isNaN += sum;
1056  }
1057 
1058  return norm * (isNaN / isNaN);
1059  }
1060 
1061  //===== query
1062 
1064  bool exists (size_type i, size_type j) const
1065  {
1066 #ifdef DUNE_ISTL_WITH_CHECKING
1067  if (i<0 || i>=N()) DUNE_THROW(ISTLError,"row index out of range");
1068  if (j<0 || i>=M()) DUNE_THROW(ISTLError,"column index out of range");
1069 #else
1071 #endif
1072  return true;
1073  }
1074 
1075  protected:
1076 
1080 
1087  };
1088 
1090 } // end namespace Dune
1091 
1092 #endif
This file implements a vector space as a tensor product of a given vector space. The number of compon...
A vector of blocks with memory management.
Definition: bvector.hh:403
derive error class from the base class in common
Definition: istlexception.hh:16
ConstIterator class for sequential access.
Definition: matrix.hh:401
const window_type & operator*() const
dereferencing
Definition: matrix.hh:478
ConstIterator & operator++()
prefix increment
Definition: matrix.hh:438
ConstIterator & operator--()
prefix decrement
Definition: matrix.hh:446
ConstIterator(const B *data, size_type columns, size_type _i)
constructor from pointer
Definition: matrix.hh:411
ConstIterator(const Iterator &it)
constructor from non_const iterator
Definition: matrix.hh:417
bool operator!=(const ConstIterator &it) const
inequality
Definition: matrix.hh:460
ConstIterator()
constructor
Definition: matrix.hh:404
bool operator==(const ConstIterator &it) const
equality
Definition: matrix.hh:454
const window_type * operator->() const
arrow
Definition: matrix.hh:484
Iterator class for sequential access.
Definition: matrix.hh:260
Iterator & operator++()
prefix increment
Definition: matrix.hh:297
bool operator!=(const Iterator &it) const
inequality
Definition: matrix.hh:319
Iterator & operator--()
prefix decrement
Definition: matrix.hh:305
window_type & operator*() const
dereferencing
Definition: matrix.hh:337
Iterator & operator=(Iterator &&other)
Move assignment.
Definition: matrix.hh:279
Iterator()
constructor, no arguments
Definition: matrix.hh:263
window_type * operator->() const
arrow
Definition: matrix.hh:343
bool operator==(const Iterator &it) const
equality
Definition: matrix.hh:313
Iterator(B *data, size_type columns, size_type _i)
constructor
Definition: matrix.hh:273
Iterator & operator=(Iterator &other)
Copy assignment.
Definition: matrix.hh:288
A Vector of blocks with different blocksizes.
Definition: matrix.hh:41
BlockVector< B, A > block_type
Same as value_type, here for historical reasons.
Definition: matrix.hh:64
typename Imp::BlockTraits< B >::field_type field_type
export the type representing the field
Definition: matrix.hh:47
Iterator beforeBegin() const
Definition: matrix.hh:382
void resize(size_type rows, size_type columns)
same effect as constructor with same argument
Definition: matrix.hh:150
DenseMatrixBase(size_type rows, size_type columns)
Definition: matrix.hh:92
DenseMatrixBase & operator=(const DenseMatrixBase &a)
assignment
Definition: matrix.hh:179
Iterator end()
end Iterator
Definition: matrix.hh:368
reference operator[](size_type i)
random access to blocks
Definition: matrix.hh:238
BlockVector< B, A > value_type
Type of the elements of the outer vector, i.e., dynamic vectors of B.
Definition: matrix.hh:60
Iterator find(size_type i)
random access returning iterator (end if not contained)
Definition: matrix.hh:388
size_type N() const
number of blocks in the vector (are of variable size here)
Definition: matrix.hh:536
ConstIterator beforeEnd() const
Definition: matrix.hh:522
ConstIterator end() const
end ConstIterator
Definition: matrix.hh:515
ConstIterator rend() const
end ConstIterator
Definition: matrix.hh:528
Iterator beforeEnd()
Definition: matrix.hh:375
ConstIterator begin() const
begin ConstIterator
Definition: matrix.hh:509
A allocator_type
export the allocator type
Definition: matrix.hh:50
DenseMatrixBase()
Definition: matrix.hh:79
ConstIterator find(size_type i) const
random access returning iterator (end if not contained)
Definition: matrix.hh:394
A::size_type size_type
The size type for the index access.
Definition: matrix.hh:53
~DenseMatrixBase()
free dynamic memory
Definition: matrix.hh:139
Iterator begin()
begin Iterator
Definition: matrix.hh:362
DenseMatrixBase(const DenseMatrixBase &a)
copy constructor, has copy semantics
Definition: matrix.hh:113
A generic dynamic dense matrix.
Definition: matrix.hh:558
size_type cols_
Number of columns of the matrix.
Definition: matrix.hh:1086
A allocator_type
Export the allocator.
Definition: matrix.hh:568
FieldTraits< field_type >::real_type frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: matrix.hh:970
void usmhv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: matrix.hh:952
void usmv(const field_type &alpha, const X &x, Y &y) const
Definition: matrix.hh:850
A::size_type size_type
Type for indices and sizes.
Definition: matrix.hh:574
MatrixImp::DenseMatrixBase< T, A > data_
Abuse DenseMatrixBase as an engine for a 2d array ISTL-style.
Definition: matrix.hh:1079
Matrix transpose() const
Return the transpose of the matrix.
Definition: matrix.hh:741
Matrix & operator=(const field_type &t)
Assignment from scalar.
Definition: matrix.hh:662
void mtv(const X &x, Y &y) const
y = A^T x
Definition: matrix.hh:803
void umv(const X &x, Y &y) const
y += A x
Definition: matrix.hh:816
Matrix< T > & operator/=(const field_type &scalar)
Division by a scalar.
Definition: matrix.hh:707
void mv(const X &x, Y &y) const
y = A x
Definition: matrix.hh:784
MatrixImp::DenseMatrixBase< T, A >::ConstIterator ConstRowIterator
Const iterator over the matrix rows.
Definition: matrix.hh:583
void setSize(size_type rows, size_type cols)
Change the matrix size.
Definition: matrix.hh:604
RowIterator beforeBegin()
Definition: matrix.hh:630
RowIterator beforeEnd()
Definition: matrix.hh:623
Matrix()
Create empty matrix.
Definition: matrix.hh:592
FieldTraits< ft >::real_type infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: matrix.hh:988
row_type::iterator ColIterator
Iterator for the entries of each row.
Definition: matrix.hh:580
ConstRowIterator beforeEnd() const
Definition: matrix.hh:649
Matrix & operator-=(const Matrix &b)
Subtract the entries of another matrix from this one.
Definition: matrix.hh:731
RowIterator end()
Get iterator to one beyond last row.
Definition: matrix.hh:616
const row_type operator[](size_type row) const
The const index operator.
Definition: matrix.hh:680
ConstRowIterator end() const
Get const iterator to one beyond last row.
Definition: matrix.hh:642
friend Y operator*(const Matrix< T > &m, const X &vec)
Generic matrix-vector multiplication.
Definition: matrix.hh:766
row_type operator[](size_type row)
The index operator.
Definition: matrix.hh:669
void mmv(const X &x, Y &y) const
y -= A x
Definition: matrix.hh:833
static constexpr unsigned int blocklevel
The number of nesting levels the matrix contains.
Definition: matrix.hh:589
FieldTraits< ft >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: matrix.hh:1007
ConstRowIterator begin() const
Get const iterator to first row.
Definition: matrix.hh:636
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: matrix.hh:935
RowIterator begin()
Get iterator to first row.
Definition: matrix.hh:610
typename Imp::BlockTraits< T >::field_type field_type
Export the type representing the underlying field.
Definition: matrix.hh:562
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:586
friend Matrix< T > operator*(const Matrix< T > &m1, const Matrix< T > &m2)
Generic matrix multiplication.
Definition: matrix.hh:751
size_type M() const
Return the number of columns.
Definition: matrix.hh:696
T block_type
Export the type representing the components.
Definition: matrix.hh:565
bool exists(size_type i, size_type j) const
return true if (i,j) is in pattern
Definition: matrix.hh:1064
void umtv(const X &x, Y &y) const
y += A^T x
Definition: matrix.hh:867
FieldTraits< field_type >::real_type frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: matrix.hh:976
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: matrix.hh:884
MatrixImp::DenseMatrixBase< T, A >::window_type row_type
The type implementing a matrix row.
Definition: matrix.hh:571
Matrix & operator+=(const Matrix &b)
Add the entries of another matrix to this one.
Definition: matrix.hh:717
void usmtv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: matrix.hh:901
void umhv(const X &x, Y &y) const
y += A^H x
Definition: matrix.hh:918
size_type N() const
Return the number of rows.
Definition: matrix.hh:691
ConstRowIterator beforeBegin() const
Definition: matrix.hh:656
Matrix< T > & operator*=(const field_type &scalar)
Multiplication with a scalar.
Definition: matrix.hh:701
Matrix(size_type rows, size_type cols)
Create uninitialized matrix of size rows x cols.
Definition: matrix.hh:597
MatrixImp::DenseMatrixBase< T, A >::Iterator RowIterator
Iterator over the matrix rows.
Definition: matrix.hh:577
Default exception class for range errors.
Definition: exceptions.hh:252
Type traits to determine the type of reals (when working with complex numbers)
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
auto min(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::min()
Definition: defaults.hh:87
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:79
Dune namespace.
Definition: alignedallocator.hh:14
Implements a scalar matrix view wrapper around an existing scalar.
Implements a scalar vector view wrapper around an existing scalar.
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 5, 22:29, 2024)