/usr/include/dune/common/diagonalmatrix.hh is in libdune-common-dev 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_DIAGONAL_MATRIX_HH
#define DUNE_DIAGONAL_MATRIX_HH
/*! \file
\brief This file implements a quadratic diagonal matrix of fixed size.
*/
#include <cassert>
#include <cmath>
#include <complex>
#include <cstddef>
#include <iostream>
#include <memory>
#include <dune/common/densematrix.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>
#include <dune/common/genericiterator.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/unused.hh>
namespace Dune {
template< class K, int n > class DiagonalRowVectorConst;
template< class K, int n > class DiagonalRowVector;
template< class DiagonalMatrixType > class DiagonalMatrixWrapper;
template< class C, class T, class R> class ContainerWrapperIterator;
/**
@addtogroup DenseMatVec
@{
*/
/**
*@brief A diagonal matrix of static size.
*
* This is meant to be a replacement of FieldMatrix for the case that
* it is a diagonal matrix.
*
* \tparam K Type used for scalars
* \tparam n Matrix size
*/
template<class K, int n>
class DiagonalMatrix
{
typedef DiagonalMatrixWrapper< DiagonalMatrix<K,n> > WrapperType;
public:
//===== type definitions and constants
//! export the type representing the field
typedef K value_type;
typedef value_type field_type;
//! export the type representing the components
typedef K block_type;
//! The type used for the index access and size operations.
typedef std::size_t size_type;
//! We are at the leaf of the block recursion
enum {
//! The number of block levels we contain. This is 1.
blocklevel = 1
};
//! Each row is implemented by a field vector
typedef DiagonalRowVector<K,n> row_type;
typedef row_type reference;
typedef row_type row_reference;
typedef DiagonalRowVectorConst<K,n> const_row_type;
typedef const_row_type const_reference;
typedef const_row_type const_row_reference;
//! export size
enum {
//! The number of rows
rows = n,
//! The number of columns
cols = n
};
//==== size
size_type size () const
{
return rows;
}
//===== constructors
//! Default constructor
DiagonalMatrix () {}
//! Constructor initializing the whole matrix with a scalar
DiagonalMatrix (const K& k)
: diag_(k)
{}
//! Constructor initializing the diagonal with a vector
DiagonalMatrix (const FieldVector<K,n>& diag)
: diag_(diag)
{}
/** \brief Assignment from a scalar */
DiagonalMatrix& operator= (const K& k)
{
diag_ = k;
return *this;
}
/** \brief Check if matrix is the same object as the other matrix */
bool identical(const DiagonalMatrix<K,n>& other) const
{
return (this==&other);
}
//===== iterator interface to rows of the matrix
//! Iterator class for sequential access
typedef ContainerWrapperIterator<const WrapperType, reference, reference> Iterator;
//! typedef for stl compliant access
typedef Iterator iterator;
//! rename the iterators for easier access
typedef Iterator RowIterator;
//! rename the iterators for easier access
typedef typename row_type::Iterator ColIterator;
//! begin iterator
Iterator begin ()
{
return Iterator(WrapperType(this),0);
}
//! end iterator
Iterator end ()
{
return Iterator(WrapperType(this),n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the rows, i.e. at the last row.
Iterator beforeEnd ()
{
return Iterator(WrapperType(this),n-1);
}
//! @returns an iterator that is positioned before
//! the first row of the matrix.
Iterator beforeBegin ()
{
return Iterator(WrapperType(this),-1);
}
//! Iterator class for sequential access
typedef ContainerWrapperIterator<const WrapperType, const_reference, const_reference> ConstIterator;
//! typedef for stl compliant access
typedef ConstIterator const_iterator;
//! rename the iterators for easier access
typedef ConstIterator ConstRowIterator;
//! rename the iterators for easier access
typedef typename const_row_type::ConstIterator ConstColIterator;
//! begin iterator
ConstIterator begin () const
{
return ConstIterator(WrapperType(this),0);
}
//! end iterator
ConstIterator end () const
{
return ConstIterator(WrapperType(this),n);
}
//! @returns an iterator that is positioned before
//! the end iterator of the rows. i.e. at the last row.
ConstIterator beforeEnd() const
{
return ConstIterator(WrapperType(this),n-1);
}
//! @returns an iterator that is positioned before
//! the first row of the matrix.
ConstIterator beforeBegin () const
{
return ConstIterator(WrapperType(this),-1);
}
//===== vector space arithmetic
//! vector space addition
DiagonalMatrix& operator+= (const DiagonalMatrix& y)
{
diag_ += y.diag_;
return *this;
}
//! vector space subtraction
DiagonalMatrix& operator-= (const DiagonalMatrix& y)
{
diag_ -= y.diag_;
return *this;
}
//! vector space multiplication with scalar
DiagonalMatrix& operator+= (const K& k)
{
diag_ += k;
return *this;
}
//! vector space division by scalar
DiagonalMatrix& operator-= (const K& k)
{
diag_ -= k;
return *this;
}
//! vector space multiplication with scalar
DiagonalMatrix& operator*= (const K& k)
{
diag_ *= k;
return *this;
}
//! vector space division by scalar
DiagonalMatrix& operator/= (const K& k)
{
diag_ /= k;
return *this;
}
//===== comparison ops
//! comparison operator
bool operator==(const DiagonalMatrix& other) const
{
return diag_==other.diagonal();
}
//! incomparison operator
bool operator!=(const DiagonalMatrix& other) const
{
return diag_!=other.diagonal();
}
//===== linear maps
//! y = A x
template<class X, class Y>
void mv (const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; ++i)
y[i] = diag_[i] * x[i];
}
//! y = A^T x
template<class X, class Y>
void mtv (const X& x, Y& y) const
{
mv(x, y);
}
//! y += A x
template<class X, class Y>
void umv (const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; ++i)
y[i] += diag_[i] * x[i];
}
//! y += A^T x
template<class X, class Y>
void umtv (const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; ++i)
y[i] += diag_[i] * x[i];
}
//! y += A^H x
template<class X, class Y>
void umhv (const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; i++)
y[i] += conjugateComplex(diag_[i])*x[i];
}
//! y -= A x
template<class X, class Y>
void mmv (const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; ++i)
y[i] -= diag_[i] * x[i];
}
//! y -= A^T x
template<class X, class Y>
void mmtv (const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; ++i)
y[i] -= diag_[i] * x[i];
}
//! y -= A^H x
template<class X, class Y>
void mmhv (const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; i++)
y[i] -= conjugateComplex(diag_[i])*x[i];
}
//! y += alpha A x
template<class X, class Y>
void usmv (const K& alpha, const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; i++)
y[i] += alpha * diag_[i] * x[i];
}
//! y += alpha A^T x
template<class X, class Y>
void usmtv (const K& alpha, const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; i++)
y[i] += alpha * diag_[i] * x[i];
}
//! y += alpha A^H x
template<class X, class Y>
void usmhv (const K& alpha, const X& x, Y& y) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
#endif
for (size_type i=0; i<n; i++)
y[i] += alpha * conjugateComplex(diag_[i]) * x[i];
}
//===== norms
//! frobenius norm: sqrt(sum over squared values of entries)
double frobenius_norm () const
{
return diag_.two_norm();
}
//! square of frobenius norm, need for block recursion
double frobenius_norm2 () const
{
return diag_.two_norm2();
}
//! infinity norm (row sum norm, how to generalize for blocks?)
double infinity_norm () const
{
return diag_.infinity_norm();
}
//! simplified infinity norm (uses Manhattan norm for complex values)
double infinity_norm_real () const
{
return diag_.infinity_norm_real();
}
//===== solve
//! Solve system A x = b
template<class V>
void solve (V& x, const V& b) const
{
for (int i=0; i<n; i++)
x[i] = b[i]/diag_[i];
}
//! Compute inverse
void invert()
{
for (int i=0; i<n; i++)
diag_[i] = 1/diag_[i];
}
//! calculates the determinant of this matrix
K determinant () const
{
K det = diag_[0];
for (int i=1; i<n; i++)
det *= diag_[i];
return det;
}
//===== sizes
//! number of blocks in row direction
size_type N () const
{
return n;
}
//! number of blocks in column direction
size_type M () const
{
return n;
}
//===== query
//! return true when (i,j) is in pattern
bool exists (size_type i, size_type j) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (i<0 || i>=n) DUNE_THROW(FMatrixError,"row index out of range");
if (j<0 || j>=n) DUNE_THROW(FMatrixError,"column index out of range");
#endif
return i==j;
}
//! Sends the matrix to an output stream
friend std::ostream& operator<< (std::ostream& s, const DiagonalMatrix<K,n>& a)
{
for (size_type i=0; i<n; i++) {
for (size_type j=0; j<n; j++)
s << ((i==j) ? a.diag_[i] : 0) << " ";
s << std::endl;
}
return s;
}
//! Return reference object as row replacement
reference operator[](size_type i)
{
return reference(const_cast<K*>(&diag_[i]), i);
}
//! Return const_reference object as row replacement
const_reference operator[](size_type i) const
{
return const_reference(const_cast<K*>(&diag_[i]), i);
}
//! Get const reference to diagonal entry
const K& diagonal(size_type i) const
{
return diag_[i];
}
//! Get reference to diagonal entry
K& diagonal(size_type i)
{
return diag_[i];
}
//! Get const reference to diagonal vector
const FieldVector<K,n>& diagonal() const
{
return diag_;
}
//! Get reference to diagonal vector
FieldVector<K,n>& diagonal()
{
return diag_;
}
private:
// the data, a FieldVector storing the diagonal
FieldVector<K,n> diag_;
};
#ifndef DOXYGEN // hide specialization
/** \brief Special type for 1x1 matrices
*/
template< class K >
class DiagonalMatrix<K, 1> : public FieldMatrix<K, 1, 1>
{
typedef FieldMatrix<K,1,1> Base;
public:
//! The type used for index access and size operations
typedef typename Base::size_type size_type;
//! We are at the leaf of the block recursion
enum {
//! The number of block levels we contain.
//! This is always one for this type.
blocklevel = 1
};
typedef typename Base::row_type row_type;
typedef typename Base::row_reference row_reference;
typedef typename Base::const_row_reference const_row_reference;
//! export size
enum {
//! \brief The number of rows.
//! This is always one for this type.
rows = 1,
//! \brief The number of columns.
//! This is always one for this type.
cols = 1
};
//! Default Constructor
DiagonalMatrix()
{}
//! Constructor initializing the whole matrix with a scalar
DiagonalMatrix(const K& scalar)
{
(*this)[0][0] = scalar;
}
//! Get const reference to diagonal entry
const K& diagonal(size_type) const
{
return (*this)[0][0];
}
//! Get reference to diagonal entry
K& diagonal(size_type)
{
return (*this)[0][0];
}
//! Get const reference to diagonal vector
const FieldVector<K,1>& diagonal() const
{
return (*this)[0];
}
//! Get reference to diagonal vector
FieldVector<K,1>& diagonal()
{
return (*this)[0];
}
};
#endif
template<class DiagonalMatrixType>
class DiagonalMatrixWrapper
{
typedef typename DiagonalMatrixType::reference reference;
typedef typename DiagonalMatrixType::const_reference const_reference;
typedef typename DiagonalMatrixType::field_type K;
typedef DiagonalRowVector<K, DiagonalMatrixType::rows> row_type;
typedef std::size_t size_type;
typedef DiagonalMatrixWrapper< DiagonalMatrixType> MyType;
friend class ContainerWrapperIterator<const MyType, reference, reference>;
friend class ContainerWrapperIterator<const MyType, const_reference, const_reference>;
public:
DiagonalMatrixWrapper() :
mat_(0)
{}
DiagonalMatrixWrapper(const DiagonalMatrixType* mat) :
mat_(const_cast<DiagonalMatrixType*>(mat))
{}
size_type realIndex(int i) const
{
return i;
}
row_type* pointer(int i) const
{
row_ = row_type(&(mat_->diagonal(i)), i);
return &row_;
}
bool identical(const DiagonalMatrixWrapper& other) const
{
return mat_==other.mat_;
}
private:
mutable DiagonalMatrixType* mat_;
mutable row_type row_;
};
/** \brief
*
*/
template< class K, int n >
class DiagonalRowVectorConst
{
template<class DiagonalMatrixType>
friend class DiagonalMatrixWrapper;
friend class ContainerWrapperIterator<DiagonalRowVectorConst<K,n>, const K, const K&>;
public:
// remember size of vector
enum { dimension = n };
// standard constructor and everything is sufficient ...
//===== type definitions and constants
//! export the type representing the field
typedef K field_type;
//! export the type representing the components
typedef K block_type;
//! The type used for the index access and size operation
typedef std::size_t size_type;
//! We are at the leaf of the block recursion
enum {
//! The number of block levels we contain
blocklevel = 1
};
//! export size
enum {
//! The size of this vector.
size = n
};
//! Constructor making uninitialized vector
DiagonalRowVectorConst() :
p_(0),
row_(0)
{}
//! Constructor making vector with identical coordinates
explicit DiagonalRowVectorConst (K* p, int col) :
p_(p),
row_(col)
{}
//===== access to components
//! same for read only access
const K& operator[] (size_type i) const
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (i!=row_)
DUNE_THROW(FMatrixError,"index is not contained in pattern");
#else
DUNE_UNUSED_PARAMETER(i);
#endif
return *p_;
}
// check if row is identical to other row (not only identical values)
// since this is a proxy class we need to check equality of the stored pointer
bool identical(const DiagonalRowVectorConst<K,n>& other) const
{
return ((p_ == other.p_)and (row_ == other.row_));
}
//! ConstIterator class for sequential access
typedef ContainerWrapperIterator<DiagonalRowVectorConst<K,n>, const K, const K&> ConstIterator;
//! typedef for stl compliant access
typedef ConstIterator const_iterator;
//! begin ConstIterator
ConstIterator begin () const
{
return ConstIterator(*this,0);
}
//! end ConstIterator
ConstIterator end () const
{
return ConstIterator(*this,1);
}
//! @returns an iterator that is positioned before
//! the end iterator of the rows. i.e. at the row.
ConstIterator beforeEnd() const
{
return ConstIterator(*this,0);
}
//! @returns an iterator that is positioned before
//! the first row of the matrix.
ConstIterator beforeBegin () const
{
return ConstIterator(*this,-1);
}
//! Binary vector comparison
bool operator== (const DiagonalRowVectorConst& y) const
{
return ((p_==y.p_)and (row_==y.row_));
}
//===== sizes
//! number of blocks in the vector (are of size 1 here)
size_type N () const
{
return n;
}
//! dimension of the vector space
size_type dim () const
{
return n;
}
//! index of this row in surrounding matrix
size_type rowIndex() const
{
return row_;
}
//! the diagonal value
const K& diagonal() const
{
return *p_;
}
protected:
size_type realIndex(int i) const
{
return rowIndex();
}
K* pointer(size_type i) const
{
return const_cast<K*>(p_);
}
DiagonalRowVectorConst* operator&()
{
return this;
}
// the data, very simply a pointer to the diagonal value and the row number
K* p_;
size_type row_;
};
template< class K, int n >
class DiagonalRowVector : public DiagonalRowVectorConst<K,n>
{
template<class DiagonalMatrixType>
friend class DiagonalMatrixWrapper;
friend class ContainerWrapperIterator<DiagonalRowVector<K,n>, K, K&>;
public:
// standard constructor and everything is sufficient ...
//===== type definitions and constants
//! export the type representing the field
typedef K field_type;
//! export the type representing the components
typedef K block_type;
//! The type used for the index access and size operation
typedef std::size_t size_type;
//! Constructor making uninitialized vector
DiagonalRowVector() : DiagonalRowVectorConst<K,n>()
{}
//! Constructor making vector with identical coordinates
explicit DiagonalRowVector (K* p, int col) : DiagonalRowVectorConst<K,n>(p, col)
{}
//===== assignment from scalar
//! Assignment operator for scalar
DiagonalRowVector& operator= (const K& k)
{
*p_ = k;
return *this;
}
//===== access to components
//! random access
K& operator[] (size_type i)
{
#ifdef DUNE_FMatrix_WITH_CHECKING
if (i!=row_)
DUNE_THROW(FMatrixError,"index is contained in pattern");
#endif
return *p_;
}
//! Iterator class for sequential access
typedef ContainerWrapperIterator<DiagonalRowVector<K,n>, K, K&> Iterator;
//! typedef for stl compliant access
typedef Iterator iterator;
//! begin iterator
Iterator begin ()
{
return Iterator(*this, 0);
}
//! end iterator
Iterator end ()
{
return Iterator(*this, 1);
}
//! @returns an iterator that is positioned before
//! the end iterator of the rows, i.e. at the last row.
Iterator beforeEnd ()
{
return Iterator(*this, 0);
}
//! @returns an iterator that is positioned before
//! the first row of the matrix.
Iterator beforeBegin ()
{
return Iterator(*this, -1);
}
//! ConstIterator class for sequential access
typedef ContainerWrapperIterator<DiagonalRowVectorConst<K,n>, const K, const K&> ConstIterator;
//! typedef for stl compliant access
typedef ConstIterator const_iterator;
using DiagonalRowVectorConst<K,n>::identical;
using DiagonalRowVectorConst<K,n>::operator[];
using DiagonalRowVectorConst<K,n>::operator==;
using DiagonalRowVectorConst<K,n>::begin;
using DiagonalRowVectorConst<K,n>::end;
using DiagonalRowVectorConst<K,n>::beforeEnd;
using DiagonalRowVectorConst<K,n>::beforeBegin;
using DiagonalRowVectorConst<K,n>::N;
using DiagonalRowVectorConst<K,n>::dim;
using DiagonalRowVectorConst<K,n>::rowIndex;
using DiagonalRowVectorConst<K,n>::diagonal;
protected:
DiagonalRowVector* operator&()
{
return this;
}
private:
using DiagonalRowVectorConst<K,n>::p_;
using DiagonalRowVectorConst<K,n>::row_;
};
// implement type traits
template<class K, int n>
struct const_reference< DiagonalRowVector<K,n> >
{
typedef DiagonalRowVectorConst<K,n> type;
};
template<class K, int n>
struct const_reference< DiagonalRowVectorConst<K,n> >
{
typedef DiagonalRowVectorConst<K,n> type;
};
template<class K, int n>
struct mutable_reference< DiagonalRowVector<K,n> >
{
typedef DiagonalRowVector<K,n> type;
};
template<class K, int n>
struct mutable_reference< DiagonalRowVectorConst<K,n> >
{
typedef DiagonalRowVector<K,n> type;
};
/** \brief Iterator class for sparse vector-like containers
*
* This class provides an iterator for sparse vector like containers.
* It contains a ContainerWrapper that must provide the translation
* from the position in the underlying container to the index
* in the sparse container.
*
* The ContainerWrapper must be default and copy-constructable.
* Furthermore it must provide the methods:
*
* bool identical(other) - check if this is identical to other (same container, not only equal)
* T* pointer(position) - get pointer to data at position in underlying container
* size_t realIndex(position) - get index in sparse container for position in underlying container
*
* Notice that the iterator stores a ContainerWrapper.
* This allows to use proxy classes as underlying container
* and as returned reference type.
*
* \tparam CW The container wrapper class
* \tparam T The contained type
* \tparam R The reference type returned by dereference
*/
template<class CW, class T, class R>
class ContainerWrapperIterator : public BidirectionalIteratorFacade<ContainerWrapperIterator<CW,T,R>,T, R, int>
{
typedef typename remove_const<CW>::type NonConstCW;
friend class ContainerWrapperIterator<CW, typename mutable_reference<T>::type, typename mutable_reference<R>::type>;
friend class ContainerWrapperIterator<CW, typename const_reference<T>::type, typename const_reference<R>::type>;
typedef ContainerWrapperIterator<CW, typename mutable_reference<T>::type, typename mutable_reference<R>::type> MyType;
typedef ContainerWrapperIterator<CW, typename const_reference<T>::type, typename const_reference<R>::type> MyConstType;
public:
// Constructors needed by the facade iterators.
ContainerWrapperIterator() :
containerWrapper_(),
position_(0)
{}
ContainerWrapperIterator(CW containerWrapper, int position) :
containerWrapper_(containerWrapper),
position_(position)
{}
template<class OtherContainerWrapperIteratorType>
ContainerWrapperIterator(OtherContainerWrapperIteratorType& other) :
containerWrapper_(other.containerWrapper_),
position_(other.position_)
{}
ContainerWrapperIterator(const MyType& other) :
containerWrapper_(other.containerWrapper_),
position_(other.position_)
{}
ContainerWrapperIterator(const MyConstType& other) :
containerWrapper_(other.containerWrapper_),
position_(other.position_)
{}
template<class OtherContainerWrapperIteratorType>
ContainerWrapperIterator& operator=(OtherContainerWrapperIteratorType& other)
{
containerWrapper_ = other.containerWrapper_;
position_ = other.position_;
}
// This operator is needed since we can not get the address of the
// temporary object returned by dereference
T* operator->() const
{
return containerWrapper_.pointer(position_);
}
// Methods needed by the forward iterator
bool equals(const MyType& other) const
{
return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
}
bool equals(const MyConstType& other) const
{
return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
}
R dereference() const
{
return *containerWrapper_.pointer(position_);
}
void increment()
{
++position_;
}
// Additional function needed by BidirectionalIterator
void decrement()
{
--position_;
}
// Additional function needed by RandomAccessIterator
R elementAt(int i) const
{
return *containerWrapper_.pointer(position_+i);
}
void advance(int n)
{
position_=position_+n;
}
template<class OtherContainerWrapperIteratorType>
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType& other) const
{
assert(containerWrapper_.identical(other));
return other.position_ - position_;
}
std::ptrdiff_t index() const
{
return containerWrapper_.realIndex(position_);
}
private:
NonConstCW containerWrapper_;
size_t position_;
};
template<class M, class K, int n>
void istl_assign_to_fmatrix(DenseMatrix<M>& fm, const DiagonalMatrix<K,n>& s)
{
assert( fm.rows() == n );
assert( fm.cols() == n );
fm = K();
for(int i=0; i<n; ++i)
fm[i][i] = s.diagonal()[i];
}
/* @} */
} // end namespace
#endif
|