Debian Codegraph @

dune/istl/bvector.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5
6 #ifndef DUNE_ISTL_BVECTOR_HH
7 #define DUNE_ISTL_BVECTOR_HH
8
9 #include <algorithm>
10 #include <cmath>
11 #include <complex>
12 #include <initializer_list>
13 #include <limits>
14 #include <memory>
15 #include <utility>
16 #include <vector>
17
18 #include <dune/common/dotproduct.hh>
19 #include <dune/common/ftraits.hh>
20 #include <dune/common/fmatrix.hh>
21 #include <dune/common/fvector.hh>
22 #include <dune/common/promotiontraits.hh>
23 #include <dune/common/typetraits.hh>
24 #include <dune/common/scalarvectorview.hh>
25
26 #include <dune/istl/blocklevel.hh>
27
28 #include "basearray.hh"
29 #include "istlexception.hh"
30
31 /*! \file
32
33 \brief This file implements a vector space as a tensor product of
34 a given vector space. The number of components can be given at
35 run-time.
36 */
37
38 namespace Dune {
39
40 /** \brief Everything in this namespace is internal to dune-istl, and may change without warning */
41 namespace Imp {
42
43 /** \brief Define some derived types transparently for number types and dune-istl vector types
44 *
45 * This is the actual implementation. Calling code should use BlockTraits instead.
46 * \tparam isNumber Whether B is a number type (true) or a dune-istl matrix or vector type (false)
47 */
48 template <class B, bool isNumber>
49 class BlockTraitsImp;
50
51 template <class B>
52 class BlockTraitsImp<B,true>
53 {
54 public:
55 using field_type = B;
56 };
57
58 template <class B>
59 class BlockTraitsImp<B,false>
60 {
61 public:
62 using field_type = typename B::field_type;
63 };
64
65 /** \brief Define some derived types transparently for number types and dune-istl matrix/vector types
66 */
67 template <class B>
68 using BlockTraits = BlockTraitsImp<B,IsNumber<B>::value>;
69
70 /**
71 \brief An unmanaged vector of blocks.
72
73 block_vector_unmanaged extends the base_array_unmanaged by
74 vector operations such as addition and scalar multiplication.
75 No memory management is added.
76
77 Error checking: no error checking is provided normally.
78 Setting the compile time switch DUNE_ISTL_WITH_CHECKING
79 enables error checking.
80
81 \internal This class is an implementation detail, and should not be used outside of dune-istl.
82 */
83 template<class B, class ST=std::size_t >
84 class block_vector_unmanaged : public base_array_unmanaged<B,ST>
85 {
86 public:
87
88 //===== type definitions and constants
89 using field_type = typename Imp::BlockTraits<B>::field_type;
90
91 //! export the type representing the components
92 typedef B block_type;
93
94 //! The size type for the index access
95 typedef ST size_type;
96
97 //! make iterators available as types
98 typedef typename base_array_unmanaged<B,ST>::iterator Iterator;
99
100 //! make iterators available as types
101 typedef typename base_array_unmanaged<B,ST>::const_iterator ConstIterator;
102
103 //! for STL compatibility
104 typedef B value_type;
105
106 //! Type used for references
107 typedef B& reference;
108
109 //! Type used for const references
110 typedef const B& const_reference;
111
112 //===== assignment from scalar
113 //! Assignment from a scalar
114
115 block_vector_unmanaged& operator= (const field_type& k)
116 {
117 for (size_type i=0; i<this->n; i++)
118 (*this)[i] = k;
119 return *this;
120 }
121
122 //===== vector space arithmetic
123 //! vector space addition
124 block_vector_unmanaged& operator+= (const block_vector_unmanaged& y)
125 {
126 #ifdef DUNE_ISTL_WITH_CHECKING
127 if (this->n!=y.N()) DUNE_THROW(ISTLError,"vector size mismatch");
128 #endif
129 for (size_type i=0; i<this->n; ++i) (*this)[i] += y[i];
130 return *this;
131 }
132
133 //! vector space subtraction
134 block_vector_unmanaged& operator-= (const block_vector_unmanaged& y)
135 {
136 #ifdef DUNE_ISTL_WITH_CHECKING
137 if (this->n!=y.N()) DUNE_THROW(ISTLError,"vector size mismatch");
138 #endif
139 for (size_type i=0; i<this->n; ++i) (*this)[i] -= y[i];
140 return *this;
141 }
142
143 //! vector space multiplication with scalar
144 block_vector_unmanaged& operator*= (const field_type& k)
145 {
146 for (size_type i=0; i<this->n; ++i) (*this)[i] *= k;
147 return *this;
148 }
149
150 //! vector space division by scalar
151 block_vector_unmanaged& operator/= (const field_type& k)
152 {
153 for (size_type i=0; i<this->n; ++i) (*this)[i] /= k;
154 return *this;
155 }
156
157 //! vector space axpy operation
158 block_vector_unmanaged& axpy (const field_type& a, const block_vector_unmanaged& y)
159 {
160 #ifdef DUNE_ISTL_WITH_CHECKING
161 if (this->n!=y.N()) DUNE_THROW(ISTLError,"vector size mismatch");
162 #endif
163 for (size_type i=0; i<this->n; ++i)
164 Impl::asVector((*this)[i]).axpy(a,Impl::asVector(y[i]));
165
166 return *this;
167 }
168
169
170 /**
171 * \brief indefinite vector dot product \f$\left (x^T \cdot y \right)\f$ which corresponds to Petsc's VecTDot
172 *
173 * http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Vec/VecTDot.html
174 * @param y other (compatible) vector
175 * @return
176 */
177 template<class OtherB, class OtherST>
178 auto operator* (const block_vector_unmanaged<OtherB,OtherST>& y) const
179 {
180 typedef typename PromotionTraits<field_type,typename BlockTraits<OtherB>::field_type>::PromotedType PromotedType;
181 PromotedType sum(0);
182 #ifdef DUNE_ISTL_WITH_CHECKING
183 if (this->n!=y.N()) DUNE_THROW(ISTLError,"vector size mismatch");
184 #endif
185 for (size_type i=0; i<this->n; ++i) {
186 sum += PromotedType(((*this)[i])*y[i]);
187 }
188 return sum;
189 }
190
191 /**
192 * @brief vector dot product \f$\left (x^H \cdot y \right)\f$ which corresponds to Petsc's VecDot
193 *
194 * http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Vec/VecDot.html
195 * @param y other (compatible) vector
196 * @return
197 */
198 template<class OtherB, class OtherST>
199 auto dot(const block_vector_unmanaged<OtherB,OtherST>& y) const
200 {
201 typedef typename PromotionTraits<field_type,typename BlockTraits<OtherB>::field_type>::PromotedType PromotedType;
202 PromotedType sum(0);
203 #ifdef DUNE_ISTL_WITH_CHECKING
204 if (this->n!=y.N()) DUNE_THROW(ISTLError,"vector size mismatch");
205 #endif
206
207 for (size_type i=0; i<this->n; ++i)
208 sum += Impl::asVector((*this)[i]).dot(Impl::asVector(y[i]));
209
210 return sum;
211 }
212
213 //===== norms
214
215 //! one norm (sum over absolute values of entries)
216 typename FieldTraits<field_type>::real_type one_norm () const
217 {
218 typename FieldTraits<field_type>::real_type sum=0;
219 for (size_type i=0; i<this->n; ++i)
220 sum += Impl::asVector((*this)[i]).one_norm();
221 return sum;
222 }
223
224 //! simplified one norm (uses Manhattan norm for complex values)
225 typename FieldTraits<field_type>::real_type one_norm_real () const
226 {
227 typename FieldTraits<field_type>::real_type sum=0;
228 for (size_type i=0; i<this->n; ++i)
229 sum += Impl::asVector((*this)[i]).one_norm_real();
230 return sum;
231 }
232
233 //! two norm sqrt(sum over squared values of entries)
234 typename FieldTraits<field_type>::real_type two_norm () const
235 {
236 using std::sqrt;
237 return sqrt(two_norm2());
238 }
239
240 //! Square of the two-norm (the sum over the squared values of the entries)
241 typename FieldTraits<field_type>::real_type two_norm2 () const
242 {
243 typename FieldTraits<field_type>::real_type sum=0;
244 for (size_type i=0; i<this->n; ++i)
245 sum += Impl::asVector((*this)[i]).two_norm2();
246 return sum;
247 }
248
249 //! infinity norm (maximum of absolute values of entries)
250 template <typename ft = field_type,
251 typename std::enable_if<!HasNaN<ft>::value, int>::type = 0>
252 typename FieldTraits<ft>::real_type infinity_norm() const {
253 using real_type = typename FieldTraits<ft>::real_type;
254 using std::max;
255
256 real_type norm = 0;
257 for (auto const &xi : *this) {
258 real_type const a = Impl::asVector(xi).infinity_norm();
259 norm = max(a, norm);
260 }
261 return norm;
262 }
263
264 //! simplified infinity norm (uses Manhattan norm for complex values)
265 template <typename ft = field_type,
266 typename std::enable_if<!HasNaN<ft>::value, int>::type = 0>
267 typename FieldTraits<ft>::real_type infinity_norm_real() const {
268 using real_type = typename FieldTraits<ft>::real_type;
269 using std::max;
270
271 real_type norm = 0;
272 for (auto const &xi : *this) {
273 real_type const a = Impl::asVector(xi).infinity_norm_real();
274 norm = max(a, norm);
275 }
276 return norm;
277 }
278
279 //! infinity norm (maximum of absolute values of entries)
280 template <typename ft = field_type,
281 typename std::enable_if<HasNaN<ft>::value, int>::type = 0>
282 typename FieldTraits<ft>::real_type infinity_norm() const {
283 using real_type = typename FieldTraits<ft>::real_type;
284 using std::max;
285 using std::abs;
286
287 real_type norm = 0;
288 real_type isNaN = 1;
289
290 for (auto const &xi : *this) {
291 real_type const a = Impl::asVector(xi).infinity_norm();
292 norm = max(a, norm);
293 isNaN += a;
294 }
295 return norm * (isNaN / isNaN);
296 }
297
298 //! simplified infinity norm (uses Manhattan norm for complex values)
299 template <typename ft = field_type,
300 typename std::enable_if<HasNaN<ft>::value, int>::type = 0>
301 typename FieldTraits<ft>::real_type infinity_norm_real() const {
302 using real_type = typename FieldTraits<ft>::real_type;
303 using std::max;
304
305 real_type norm = 0;
306 real_type isNaN = 1;
307
308 for (auto const &xi : *this) {
309 real_type const a = Impl::asVector(xi).infinity_norm_real();
310 norm = max(a, norm);
311 isNaN += a;
312 }
313
314 return norm * (isNaN / isNaN);
315 }
316
317 //===== sizes
318
319 //! number of blocks in the vector (are of size 1 here)
320 size_type N () const
321 {
322 return this->n;
323 }
324
325 //! dimension of the vector space
326 size_type dim () const
327 {
328 size_type d=0;
329
330 for (size_type i=0; i<this->n; i++)
331 d += Impl::asVector((*this)[i]).dim();
332
333 return d;
334 }
335
336 protected:
337 //! make constructor protected, so only derived classes can be instantiated
338 block_vector_unmanaged () : base_array_unmanaged<B,ST>()
339 { }
340 };
341
342 //! simple scope guard, execute the provided functor on scope exit
343 /**
344 * The guard may not be copied or moved. This avoids executing the cleanup
345 * function twice. The cleanup function should not throw, as it may be
346 * called during stack unwinding.
347 */
348 template<class F>
349 class ScopeGuard {
350 F cleanupFunc_;
351 public:
352 ScopeGuard(F cleanupFunc) : cleanupFunc_(std::move(cleanupFunc)) {}
353 ScopeGuard(const ScopeGuard &) = delete;
354 ScopeGuard(ScopeGuard &&) = delete;
355 ScopeGuard &operator=(ScopeGuard) = delete;
356 ~ScopeGuard() { cleanupFunc_(); }
357 };
358
359 //! create a scope guard
360 /**
361 * Use like
362 * ```c++
363 * {
364 * const auto &guard = makeScopeGuard([&]{ doSomething(); });
365 * doSomethingThatMightThrow();
366 * }
367 * ```
368 */
369 template<class F>
370 ScopeGuard<F> makeScopeGuard(F cleanupFunc)
371 {
372 return { std::move(cleanupFunc) };
373 }
374
375 } // end namespace Imp
376 /**
377 @addtogroup ISTL_SPMV
378 @{
379 */
380 /**
381 \brief A vector of blocks with memory management.
382
383 BlockVector adds memory management with ordinary
384 copy semantics to the block_vector_unmanaged template.
385
386 Error checking: no error checking is provided normally.
387 Setting the compile time switch DUNE_ISTL_WITH_CHECKING
388 enables error checking.
389 */
390 template<class B, class A=std::allocator<B> >
391 class BlockVector : public Imp::block_vector_unmanaged<B,typename A::size_type>
392 {
393 public:
394
395 //===== type definitions and constants
396
397 //! export the type representing the field
398 using field_type = typename Imp::BlockTraits<B>::field_type;
399
400 //! export the type representing the components
401 typedef B block_type;
402
403 //! export the allocator type
404 typedef A allocator_type;
405
406 //! The type for the index access
407 typedef typename A::size_type size_type;
408
409 //! make iterators available as types
410 typedef typename Imp::block_vector_unmanaged<B,size_type>::Iterator Iterator;
411
412 //! make iterators available as types
413 typedef typename Imp::block_vector_unmanaged<B,size_type>::ConstIterator ConstIterator;
414
415 //===== constructors and such
416
417 //! makes empty vector
418 BlockVector ()
419 {
420 syncBaseArray();
421 }
422
423 //! make vector with _n components
424 explicit BlockVector (size_type _n) : storage_(_n)
425 {
426 syncBaseArray();
427 }
428
429 /** \brief Construct from a std::initializer_list */
430 BlockVector (std::initializer_list<B> const &l) : storage_(l)
431 {
432 syncBaseArray();
433 }
434
435
436 /** \brief Make vector with _n components but preallocating capacity components
437
438 If _n > capacity then space for _n entries is allocated.
439 \note This constructor is somewhat dangerous. People may be tempted to
440 write something like
441 \code
442 BlockVector<FieldVector<double,1> > my_vector(100,0);
443 \endcode
444 expecting to obtain a vector of 100 doubles initialized with zero.
445 However, the code calls this constructor which tacitly does something else!
446 */
447 template<typename S>
448 BlockVector (size_type _n, S _capacity)
449 {
450 static_assert(std::numeric_limits<S>::is_integer,
451 "capacity must be an unsigned integral type (be aware, that this constructor does not set the default value!)" );
452 if((size_type)_capacity > _n)
453 storage_.reserve(_capacity);
454 storage_.resize(_n);
455 syncBaseArray();
456 }
457
458
459 /**
460 * @brief Reserve space.
461 *
462 * Allocate storage for up to `capacity` blocks. Resizing won't cause
463 * reallocation until the size exceeds the `capacity`
464 *
465 * @param capacity The maximum number of elements the vector
466 * needs to hold.
467 */
468 void reserve(size_type capacity)
469 {
470 [[maybe_unused]] const auto &guard =
471 Imp::makeScopeGuard([this]{ syncBaseArray(); });
472 storage_.reserve(capacity);
473 }
474
475 /**
476 * @brief Get the capacity of the vector.
477 *
478 * I. e. the maximum number of elements the vector can hold.
479 * @return The capacity of the vector.
480 */
481 size_type capacity() const
482 {
483 return storage_.capacity();
484 }
485
486 /**
487 * @brief Resize the vector.
488 *
489 * Resize the vector to the given number of blocks. Blocks below the
490 * given size are copied (moved if possible). Old blocks above the given
491 * size are destructed, new blocks above the given size are
492 * value-initialized.
493 *
494 * @param size The new number of blocks of the vector.
495 */
496 void resize(size_type size)
497 {
498 [[maybe_unused]] const auto &guard =
499 Imp::makeScopeGuard([this]{ syncBaseArray(); });
500 storage_.resize(size);
501 }
502
503 //! copy constructor
504 BlockVector(const BlockVector &a)
505 noexcept(noexcept(std::declval<BlockVector>().storage_ = a.storage_))
506 {
507 storage_ = a.storage_;
508 syncBaseArray();
509 }
510
511 //! move constructor
512 BlockVector(BlockVector &&a)
513 noexcept(noexcept(std::declval<BlockVector>().swap(a)))
514 {
515 swap(a);
516 }
517
518 //! assignment
519 BlockVector& operator= (const BlockVector& a)
520 noexcept(noexcept(std::declval<BlockVector>().storage_ = a.storage_))
521 {
522 [[maybe_unused]] const auto &guard =
523 Imp::makeScopeGuard([this]{ syncBaseArray(); });
524 storage_ = a.storage_;
525 return *this;
526 }
527
528 //! move assignment
529 BlockVector& operator= (BlockVector&& a)
530 noexcept(noexcept(std::declval<BlockVector>().swap(a)))
531 {
532 swap(a);
533 return *this;
534 }
535
536 //! swap operation
537 void swap(BlockVector &other)
538 noexcept(noexcept(
539 std::declval<BlockVector&>().storage_.swap(other.storage_)))
540 {
541 [[maybe_unused]] const auto &guard = Imp::makeScopeGuard([&]{
542 syncBaseArray();
543 other.syncBaseArray();
544 });
545 storage_.swap(other.storage_);
546 }
547
548 //! assign from scalar
549 BlockVector& operator= (const field_type& k)
550 {
551 // forward to operator= in base class
552 (static_cast<Imp::block_vector_unmanaged<B,size_type>&>(*this)) = k;
553 return *this;
554 }
555
556 private:
557 void syncBaseArray() noexcept
558 {
559 this->p = storage_.data();
560 this->n = storage_.size();
561 }
562
563 std::vector<B, A> storage_;
564 };
565
566 /** @} */
567
568 /** @addtogroup DenseMatVec
569 @{
570 */
571 template<class B, class A>
572 struct FieldTraits< BlockVector<B, A> >
573 {
574 typedef typename FieldTraits<B>::field_type field_type;
575 typedef typename FieldTraits<B>::real_type real_type;
576 };
577 /**
578 @}
579 */
580
581 //! Send BlockVector to an output stream
582 template<class K, class A>
583 std::ostream& operator<< (std::ostream& s, const BlockVector<K, A>& v)
584 {
585 typedef typename BlockVector<K, A>::size_type size_type;
586
587 for (size_type i=0; i<v.size(); i++)
588 s << v[i] << std::endl;
589
590 return s;
591 }
592
593 /** \brief Everything in this namespace is internal to dune-istl, and may change without warning */
594 namespace Imp {
595
596 /** BlockVectorWindow adds window manipulation functions
597 to the block_vector_unmanaged template.
598
599 This class has no memory management. It assumes that the storage
600 for the entries of the vector is maintained outside of this class.
601
602 But you can copy objects of this class and of the base class
603 with reference semantics.
604
605 Assignment copies the data, if the format is incompatible with
606 the argument an exception is thrown in debug mode.
607
608 Error checking: no error checking is provided normally.
609 Setting the compile time switch DUNE_ISTL_WITH_CHECKING
610 enables error checking.
611
612 \internal This class is an implementation detail, and should not be used outside of dune-istl.
613 */
614 #ifndef DOXYGEN
615 template<class B, class A>
616 #else
617 template<class B, class A=std::allocator<B> >
618 #endif
619 class BlockVectorWindow : public Imp::block_vector_unmanaged<B,typename A::size_type>
620 {
621 public:
622
623 //===== type definitions and constants
624
625 //! export the type representing the field
626 using field_type = typename Imp::BlockTraits<B>::field_type;
627
628 //! export the type representing the components
629 typedef B block_type;
630
631 //! export the allocator type
632 typedef A allocator_type;
633
634 //! The type for the index access
635 typedef typename A::size_type size_type;
636
637 //! make iterators available as types
638 typedef typename Imp::block_vector_unmanaged<B,size_type>::Iterator Iterator;
639
640 //! make iterators available as types
641 typedef typename Imp::block_vector_unmanaged<B,size_type>::ConstIterator ConstIterator;
642
643
644 //===== constructors and such
645 //! makes empty array
646 BlockVectorWindow () : Imp::block_vector_unmanaged<B,size_type>()
647 { }
648
649 //! make array from given pointer and size
650 BlockVectorWindow (B* _p, size_type _n)
651 {
652 this->n = _n;
653 this->p = _p;
654 }
655
656 //! copy constructor, this has reference semantics!
657 BlockVectorWindow (const BlockVectorWindow& a)
658 {
659 this->n = a.n;
660 this->p = a.p;
661 }
662
663 //! assignment
664 BlockVectorWindow& operator= (const BlockVectorWindow& a)
665 {
666 // check correct size
667 #ifdef DUNE_ISTL_WITH_CHECKING
668 if (this->n!=a.N()) DUNE_THROW(ISTLError,"vector size mismatch");
669 #endif
670
671 if (&a!=this) // check if this and a are different objects
672 {
673 // copy data
674 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
675 }
676 return *this;
677 }
678
679 //! assign from scalar
680 BlockVectorWindow& operator= (const field_type& k)
681 {
682 (static_cast<Imp::block_vector_unmanaged<B,size_type>&>(*this)) = k;
683 return *this;
684 }
685
686 //! copy into an independent BlockVector object
687 operator BlockVector<B, A>() const {
688 auto bv = BlockVector<B, A>(this->n);
689
690 std::copy(this->begin(), this->end(), bv.begin());
691
692 return bv;
693 }
694
695 //===== window manipulation methods
696
697 //! set size and pointer
698 void set (size_type _n, B* _p)
699 {
700 this->n = _n;
701 this->p = _p;
702 }
703
704 //! set size only
705 void setsize (size_type _n)
706 {
707 this->n = _n;
708 }
709
710 //! set pointer only
711 void setptr (B* _p)
712 {
713 this->p = _p;
714 }
715
716 //! get pointer
717 B* getptr ()
718 {
719 return this->p;
720 }
721
722 //! get size
723 size_type getsize () const
724 {
725 return this->n;
726 }
727 };
728
729
730
731 /** compressed_block_vector_unmanaged extends the compressed base_array_unmanaged by
732 vector operations such as addition and scalar multiplication.
733 No memory management is added.
734
735 Error checking: no error checking is provided normally.
736 Setting the compile time switch DUNE_ISTL_WITH_CHECKING
737 enables error checking.
738
739 \internal This class is an implementation detail, and should not be used outside of dune-istl.
740 */
741 template<class B, class ST=std::size_t >
742 class compressed_block_vector_unmanaged : public compressed_base_array_unmanaged<B,ST>
743 {
744 public:
745
746 //===== type definitions and constants
747
748 //! export the type representing the field
749 using field_type = typename Imp::BlockTraits<B>::field_type;
750
751 //! export the type representing the components
752 typedef B block_type;
753
754 //! make iterators available as types
755 typedef typename compressed_base_array_unmanaged<B,ST>::iterator Iterator;
756
757 //! make iterators available as types
758 typedef typename compressed_base_array_unmanaged<B,ST>::const_iterator ConstIterator;
759
760 //! The type for the index access
761 typedef ST size_type;
762
763 //===== assignment from scalar
764
765 compressed_block_vector_unmanaged& operator= (const field_type& k)
766 {
767 for (size_type i=0; i<this->n; i++)
768 (this->p)[i] = k;
769 return *this;
770 }
771
772
773 //===== vector space arithmetic
774
775 //! vector space addition
776 template<class V>
777 compressed_block_vector_unmanaged& operator+= (const V& y)
778 {
779 #ifdef DUNE_ISTL_WITH_CHECKING
780 if (!includesindexset(y)) DUNE_THROW(ISTLError,"index set mismatch");
781 #endif
782 for (size_type i=0; i<y.n; ++i) this->operator[](y.j[i]) += y.p[i];
783 return *this;
784 }
785
786 //! vector space subtraction
787 template<class V>
788 compressed_block_vector_unmanaged& operator-= (const V& y)
789 {
790 #ifdef DUNE_ISTL_WITH_CHECKING
791 if (!includesindexset(y)) DUNE_THROW(ISTLError,"index set mismatch");
792 #endif
793 for (size_type i=0; i<y.n; ++i) this->operator[](y.j[i]) -= y.p[i];
794 return *this;
795 }
796
797 //! vector space axpy operation
798 template<class V>
799 compressed_block_vector_unmanaged& axpy (const field_type& a, const V& y)
800 {
801 #ifdef DUNE_ISTL_WITH_CHECKING
802 if (!includesindexset(y)) DUNE_THROW(ISTLError,"index set mismatch");
803 #endif
804 for (size_type i=0; i<y.n; ++i)
805 Impl::asVector((*this)[y.j[i]]).axpy(a,Impl::asVector(y.p[i]));
806 return *this;
807 }
808
809 //! vector space multiplication with scalar
810 compressed_block_vector_unmanaged& operator*= (const field_type& k)
811 {
812 for (size_type i=0; i<this->n; ++i) (this->p)[i] *= k;
813 return *this;
814 }
815
816 //! vector space division by scalar
817 compressed_block_vector_unmanaged& operator/= (const field_type& k)
818 {
819 for (size_type i=0; i<this->n; ++i) (this->p)[i] /= k;
820 return *this;
821 }
822
823
824 //===== Euclidean scalar product
825
826 //! scalar product
827 field_type operator* (const compressed_block_vector_unmanaged& y) const
828 {
829 #ifdef DUNE_ISTL_WITH_CHECKING
830 if (!includesindexset(y) || !y.includesindexset(*this) )
831 DUNE_THROW(ISTLError,"index set mismatch");
832 #endif
833 field_type sum=0;
834 for (size_type i=0; i<this->n; ++i)
835 sum += (this->p)[i] * y[(this->j)[i]];
836 return sum;
837 }
838
839
840 //===== norms
841
842 //! one norm (sum over absolute values of entries)
843 typename FieldTraits<field_type>::real_type one_norm () const
844 {
845 typename FieldTraits<field_type>::real_type sum=0;
846 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].one_norm();
847 return sum;
848 }
849
850 //! simplified one norm (uses Manhattan norm for complex values)
851 typename FieldTraits<field_type>::real_type one_norm_real () const
852 {
853 typename FieldTraits<field_type>::real_type sum=0;
854 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].one_norm_real();
855 return sum;
856 }
857
858 //! two norm sqrt(sum over squared values of entries)
859 typename FieldTraits<field_type>::real_type two_norm () const
860 {
861 using std::sqrt;
862 typename FieldTraits<field_type>::real_type sum=0;
863 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].two_norm2();
864 return sqrt(sum);
865 }
866
867 //! Square of the two-norm (the sum over the squared values of the entries)
868 typename FieldTraits<field_type>::real_type two_norm2 () const
869 {
870 typename FieldTraits<field_type>::real_type sum=0;
871 for (size_type i=0; i<this->n; ++i) sum += (this->p)[i].two_norm2();
872 return sum;
873 }
874
875 //! infinity norm (maximum of absolute values of entries)
876 template <typename ft = field_type,
877 typename std::enable_if<!HasNaN<ft>::value, int>::type = 0>
878 typename FieldTraits<ft>::real_type infinity_norm() const {
879 using real_type = typename FieldTraits<ft>::real_type;
880 using std::max;
881
882 real_type norm = 0;
883 for (auto const &x : *this) {
884 real_type const a = x.infinity_norm();
885 norm = max(a, norm);
886 }
887 return norm;
888 }
889
890 //! simplified infinity norm (uses Manhattan norm for complex values)
891 template <typename ft = field_type,
892 typename std::enable_if<!HasNaN<ft>::value, int>::type = 0>
893 typename FieldTraits<ft>::real_type infinity_norm_real() const {
894 using real_type = typename FieldTraits<ft>::real_type;
895 using std::max;
896
897 real_type norm = 0;
898 for (auto const &x : *this) {
899 real_type const a = x.infinity_norm_real();
900 norm = max(a, norm);
901 }
902 return norm;
903 }
904
905 //! infinity norm (maximum of absolute values of entries)
906 template <typename ft = field_type,
907 typename std::enable_if<HasNaN<ft>::value, int>::type = 0>
908 typename FieldTraits<ft>::real_type infinity_norm() const {
909 using real_type = typename FieldTraits<ft>::real_type;
910 using std::max;
911
912 real_type norm = 0;
913 real_type isNaN = 1;
914 for (auto const &x : *this) {
915 real_type const a = x.infinity_norm();
916 norm = max(a, norm);
917 isNaN += a;
918 }
919 return norm * (isNaN / isNaN);
920 }
921
922 //! simplified infinity norm (uses Manhattan norm for complex values)
923 template <typename ft = field_type,
924 typename std::enable_if<HasNaN<ft>::value, int>::type = 0>
925 typename FieldTraits<ft>::real_type infinity_norm_real() const {
926 using real_type = typename FieldTraits<ft>::real_type;
927 using std::max;
928
929 real_type norm = 0;
930 real_type isNaN = 1;
931 for (auto const &x : *this) {
932 real_type const a = x.infinity_norm_real();
933 norm = max(a, norm);
934 isNaN += a;
935 }
936 return norm * (isNaN / isNaN);
937 }
938
939 //===== sizes
940
941 //! number of blocks in the vector (are of size 1 here)
942 size_type N () const
943 {
944 return this->n;
945 }
946
947 //! dimension of the vector space
948 size_type dim () const
949 {
950 size_type d=0;
951 for (size_type i=0; i<this->n; i++)
952 d += (this->p)[i].dim();
953 return d;
954 }
955
956 protected:
957 //! make constructor protected, so only derived classes can be instantiated
958 compressed_block_vector_unmanaged () : compressed_base_array_unmanaged<B,ST>()
959 { }
960
961 //! return true if index sets coincide
962 template<class V>
963 bool includesindexset (const V& y)
964 {
965 typename V::ConstIterator e=this->end();
966 for (size_type i=0; i<y.n; i++)
967 if (this->find(y.j[i])==e)
968 return false;
969 return true;
970 }
971 };
972
973
974 /** CompressedBlockVectorWindow adds window manipulation functions
975 to the compressed_block_vector_unmanaged template.
976
977 This class has no memory management. It assumes that the storage
978 for the entries of the vector and its index set is maintained outside of this class.
979
980 But you can copy objects of this class and of the base class
981 with reference semantics.
982
983 Assignment copies the data, if the format is incopmpatible with
984 the argument an exception is thrown in debug mode.
985
986 Error checking: no error checking is provided normally.
987 Setting the compile time switch DUNE_ISTL_WITH_CHECKING
988 enables error checking.
989
990 \internal This class is an implementation detail, and should not be used outside of dune-istl.
991 */
992 template<class B, class ST=std::size_t >
993 class CompressedBlockVectorWindow : public compressed_block_vector_unmanaged<B,ST>
994 {
995 public:
996
997 //===== type definitions and constants
998
999 //! export the type representing the field
1000 using field_type = typename Imp::BlockTraits<B>::field_type;
1001
1002 //! export the type representing the components
1003 typedef B block_type;
1004
1005 //! The type for the index access
1006 typedef ST size_type;
1007
1008 //! make iterators available as types
1009 typedef typename compressed_block_vector_unmanaged<B,ST>::Iterator Iterator;
1010
1011 //! make iterators available as types
1012 typedef typename compressed_block_vector_unmanaged<B,ST>::ConstIterator ConstIterator;
1013
1014
1015 //===== constructors and such
1016 //! makes empty array
1017 CompressedBlockVectorWindow () : compressed_block_vector_unmanaged<B,ST>()
1018 { }
1019
1020 //! make array from given pointers and size
1021 CompressedBlockVectorWindow (B* _p, size_type* _j, size_type _n)
1022 {
1023 this->n = _n;
1024 this->p = _p;
1025 this->j = _j;
1026 }
1027
1028 //! copy constructor, this has reference semantics!
1029 CompressedBlockVectorWindow (const CompressedBlockVectorWindow& a)
1030 {
1031 this->n = a.n;
1032 this->p = a.p;
1033 this->j = a.j;
1034 }
1035
1036 //! assignment
1037 CompressedBlockVectorWindow& operator= (const CompressedBlockVectorWindow& a)
1038 {
1039 // check correct size
1040 #ifdef DUNE_ISTL_WITH_CHECKING
1041 if (this->n!=a.N()) DUNE_THROW(ISTLError,"vector size mismatch");
1042 #endif
1043
1044 if (&a!=this) // check if this and a are different objects
1045 {
1046 // copy data
1047 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
1048 for (size_type i=0; i<this->n; i++) this->j[i]=a.j[i];
1049 }
1050 return *this;
1051 }
1052
1053 //! assign from scalar
1054 CompressedBlockVectorWindow& operator= (const field_type& k)
1055 {
1056 (static_cast<compressed_block_vector_unmanaged<B,ST>&>(*this)) = k;
1057 return *this;
1058 }
1059
1060
1061 //===== window manipulation methods
1062
1063 //! set size and pointer
1064 void set (size_type _n, B* _p, size_type* _j)
1065 {
1066 this->n = _n;
1067 this->p = _p;
1068 this->j = _j;
1069 }
1070
1071 //! set size only
1072 void setsize (size_type _n)
1073 {
1074 this->n = _n;
1075 }
1076
1077 //! set pointer only
1078 void setptr (B* _p)
1079 {
1080 this->p = _p;
1081 }
1082
1083 //! set pointer only
1084 void setindexptr (size_type* _j)
1085 {
1086 this->j = _j;
1087 }
1088
1089 //! get pointer
1090 B* getptr ()
1091 {
1092 return this->p;
1093 }
1094
1095 //! get pointer
1096 size_type* getindexptr ()
1097 {
1098 return this->j;
1099 }
1100
1101 //! get pointer
1102 const B* getptr () const
1103 {
1104 return this->p;
1105 }
1106
1107 //! get pointer
1108 const size_type* getindexptr () const
1109 {
1110 return this->j;
1111 }
1112 //! get size
1113 size_type getsize () const
1114 {
1115 return this->n;
1116 }
1117 };
1118
1119 } // end namespace 'Imp'
1120
1121
1122 //! Specialization for the proxies of `BlockVectorWindow`
1123 template<typename B, typename A>
1124 struct AutonomousValueType<Imp::BlockVectorWindow<B,A>>
1125 {
1126 using type = BlockVector<B, A>;
1127 };
1128
1129
1130 } // end namespace 'Dune'
1131
1132 #endif