| 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 |
#ifndef DUNE_ISTL_SCALEDIDMATRIX_HH |
| 6 |
#define DUNE_ISTL_SCALEDIDMATRIX_HH |
| 7 |
|
| 8 |
/*! \file |
| 9 |
|
| 10 |
\brief This file implements a quadratic matrix of fixed size which is |
| 11 |
a multiple of the identity. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <cmath> |
| 15 |
#include <cstddef> |
| 16 |
#include <complex> |
| 17 |
#include <iostream> |
| 18 |
#include <dune/common/exceptions.hh> |
| 19 |
#include <dune/common/fmatrix.hh> |
| 20 |
#include <dune/common/diagonalmatrix.hh> |
| 21 |
#include <dune/common/ftraits.hh> |
| 22 |
|
| 23 |
namespace Dune { |
| 24 |
|
| 25 |
/** @brief A multiple of the identity matrix of static size |
| 26 |
* |
| 27 |
* This class provides all operations specified by the dune-istl matrix API. |
| 28 |
* The implementations exploit the fact that the matrix is a multiple of the identity. |
| 29 |
* |
| 30 |
* \tparam K The number used for matrix entries |
| 31 |
* \tparam n The number of matrix rows and columns |
| 32 |
* |
| 33 |
* \warning If `m` is an object of type `ScaledIdentityMatrix`, then code like |
| 34 |
* ~~~{.cpp} |
| 35 |
* K entry = m[0][1]; |
| 36 |
* ~~~ |
| 37 |
* will compile. However, the expression `m[0][1]` is *not* |
| 38 |
* guaranteed to return 0. Rather, its behavior is undefined. |
| 39 |
*/ |
| 40 |
template<class K, int n> |
| 41 |
class ScaledIdentityMatrix |
| 42 |
{ |
| 43 |
typedef DiagonalMatrixWrapper< ScaledIdentityMatrix<K,n> > WrapperType; |
| 44 |
|
| 45 |
public: |
| 46 |
//===== type definitions and constants |
| 47 |
|
| 48 |
//! The type representing numbers |
| 49 |
typedef K field_type; |
| 50 |
|
| 51 |
//! The type representing matrix entries (which may be matrices themselves) |
| 52 |
typedef K block_type; |
| 53 |
|
| 54 |
//! The type used for the indices and sizes |
| 55 |
typedef std::size_t size_type; |
| 56 |
|
| 57 |
/** \brief Type of a single matrix row |
| 58 |
* |
| 59 |
* Since the implementation does not store actual rows, |
| 60 |
* this is a proxy type, which tries to behave like an array |
| 61 |
* of matrix entries as much as possible. |
| 62 |
* \note The type is really `DiagonalRowVector`. Implementing a |
| 63 |
* dedicated `ScaledIdentityMatrixRowVector` would just be a copy of that. |
| 64 |
*/ |
| 65 |
typedef DiagonalRowVector<K,n> row_type; |
| 66 |
typedef row_type reference; |
| 67 |
|
| 68 |
/** \brief Const type of a single row */ |
| 69 |
typedef DiagonalRowVectorConst<K,n> const_row_type; |
| 70 |
typedef const_row_type const_reference; |
| 71 |
|
| 72 |
//! The number of matrix rows |
| 73 |
static constexpr std::integral_constant<size_type,n> rows = {}; |
| 74 |
|
| 75 |
//! The number of matrix columns |
| 76 |
static constexpr std::integral_constant<size_type,n> cols = {}; |
| 77 |
|
| 78 |
//===== constructors |
| 79 |
/** \brief Default constructor |
| 80 |
*/ |
| 81 |
ScaledIdentityMatrix () {} |
| 82 |
|
| 83 |
/** \brief Constructor initializing the whole matrix with a scalar |
| 84 |
*/ |
| 85 |
ScaledIdentityMatrix (const K& k) |
| 86 |
: p_(k) |
| 87 |
{} |
| 88 |
|
| 89 |
/** \brief Assignment from scalar |
| 90 |
*/ |
| 91 |
ScaledIdentityMatrix& operator= (const K& k) |
| 92 |
{ |
| 93 |
p_ = k; |
| 94 |
return *this; |
| 95 |
} |
| 96 |
|
| 97 |
/** \brief Check if matrix is identical to other matrix |
| 98 |
* |
| 99 |
* "Identical" means: Not just the same values, but the very same object. |
| 100 |
*/ |
| 101 |
bool identical(const ScaledIdentityMatrix<K,n>& other) const |
| 102 |
{ |
| 103 |
return (this==&other); |
| 104 |
} |
| 105 |
|
| 106 |
//===== iterator interface to rows of the matrix |
| 107 |
//! Iterator class for sequential access |
| 108 |
typedef ContainerWrapperIterator<const WrapperType, reference, reference> Iterator; |
| 109 |
//! typedef for stl compliant access |
| 110 |
typedef Iterator iterator; |
| 111 |
//! rename the iterators for easier access |
| 112 |
typedef Iterator RowIterator; |
| 113 |
//! rename the iterators for easier access |
| 114 |
typedef typename row_type::Iterator ColIterator; |
| 115 |
|
| 116 |
//! begin iterator |
| 117 |
Iterator begin () |
| 118 |
{ |
| 119 |
return Iterator(WrapperType(this),0); |
| 120 |
} |
| 121 |
|
| 122 |
//! end iterator |
| 123 |
Iterator end () |
| 124 |
{ |
| 125 |
return Iterator(WrapperType(this),n); |
| 126 |
} |
| 127 |
|
| 128 |
//! @returns an iterator that is positioned before |
| 129 |
//! the end iterator of the rows, i.e. at the last row. |
| 130 |
Iterator beforeEnd () |
| 131 |
{ |
| 132 |
return Iterator(WrapperType(this),n-1); |
| 133 |
} |
| 134 |
|
| 135 |
//! @returns an iterator that is positioned before |
| 136 |
//! the first row of the matrix. |
| 137 |
Iterator beforeBegin () |
| 138 |
{ |
| 139 |
return Iterator(WrapperType(this),-1); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
//! Iterator class for sequential access |
| 144 |
typedef ContainerWrapperIterator<const WrapperType, const_reference, const_reference> ConstIterator; |
| 145 |
//! typedef for stl compliant access |
| 146 |
typedef ConstIterator const_iterator; |
| 147 |
//! rename the iterators for easier access |
| 148 |
typedef ConstIterator ConstRowIterator; |
| 149 |
//! rename the iterators for easier access |
| 150 |
typedef typename const_row_type::ConstIterator ConstColIterator; |
| 151 |
|
| 152 |
//! begin iterator |
| 153 |
ConstIterator begin () const |
| 154 |
{ |
| 155 |
return ConstIterator(WrapperType(this),0); |
| 156 |
} |
| 157 |
|
| 158 |
//! end iterator |
| 159 |
ConstIterator end () const |
| 160 |
{ |
| 161 |
return ConstIterator(WrapperType(this),n); |
| 162 |
} |
| 163 |
|
| 164 |
//! @returns an iterator that is positioned before |
| 165 |
//! the end iterator of the rows. i.e. at the last row. |
| 166 |
ConstIterator beforeEnd() const |
| 167 |
{ |
| 168 |
return ConstIterator(WrapperType(this),n-1); |
| 169 |
} |
| 170 |
|
| 171 |
//! @returns an iterator that is positioned before |
| 172 |
//! the first row of the matrix. |
| 173 |
ConstIterator beforeBegin () const |
| 174 |
{ |
| 175 |
return ConstIterator(WrapperType(this),-1); |
| 176 |
} |
| 177 |
|
| 178 |
/** @name Vector space arithmetic |
| 179 |
*/ |
| 180 |
///@{ |
| 181 |
|
| 182 |
//! Vector space addition |
| 183 |
ScaledIdentityMatrix& operator+= (const ScaledIdentityMatrix& y) |
| 184 |
{ |
| 185 |
p_ += y.p_; |
| 186 |
return *this; |
| 187 |
} |
| 188 |
|
| 189 |
//! Vector space subtraction |
| 190 |
ScaledIdentityMatrix& operator-= (const ScaledIdentityMatrix& y) |
| 191 |
{ |
| 192 |
p_ -= y.p_; |
| 193 |
return *this; |
| 194 |
} |
| 195 |
|
| 196 |
/** \brief Addition of a scalar to the diagonal |
| 197 |
* |
| 198 |
* \warning This is not the same as adding a scalar to a FieldMatrix! |
| 199 |
*/ |
| 200 |
ScaledIdentityMatrix& operator+= (const K& k) |
| 201 |
{ |
| 202 |
p_ += k; |
| 203 |
return *this; |
| 204 |
} |
| 205 |
|
| 206 |
/** \brief Subtraction of a scalar from the diagonal |
| 207 |
* |
| 208 |
* \warning This is not the same as subtracting a scalar from a FieldMatrix! |
| 209 |
*/ |
| 210 |
ScaledIdentityMatrix& operator-= (const K& k) |
| 211 |
{ |
| 212 |
p_ -= k; |
| 213 |
return *this; |
| 214 |
} |
| 215 |
|
| 216 |
//! Vector space multiplication with scalar |
| 217 |
ScaledIdentityMatrix& operator*= (const K& k) |
| 218 |
{ |
| 219 |
p_ *= k; |
| 220 |
return *this; |
| 221 |
} |
| 222 |
|
| 223 |
//! Vector space division by scalar |
| 224 |
ScaledIdentityMatrix& operator/= (const K& k) |
| 225 |
{ |
| 226 |
p_ /= k; |
| 227 |
return *this; |
| 228 |
} |
| 229 |
|
| 230 |
//! Vector space multiplication with scalar |
| 231 |
template <class Scalar, |
| 232 |
std::enable_if_t<IsNumber<Scalar>::value, int> = 0> |
| 233 |
friend auto operator* ( const ScaledIdentityMatrix& matrix, Scalar scalar) |
| 234 |
{ |
| 235 |
return ScaledIdentityMatrix<typename PromotionTraits<K,Scalar>::PromotedType, n>{matrix.scalar()*scalar}; |
| 236 |
} |
| 237 |
|
| 238 |
//! Vector space multiplication with scalar |
| 239 |
template <class Scalar, |
| 240 |
std::enable_if_t<IsNumber<Scalar>::value, int> = 0> |
| 241 |
friend auto operator* (Scalar scalar, const ScaledIdentityMatrix& matrix) |
| 242 |
{ |
| 243 |
return ScaledIdentityMatrix<typename PromotionTraits<Scalar,K>::PromotedType, n>{scalar*matrix.scalar()}; |
| 244 |
} |
| 245 |
|
| 246 |
//! Addition of ScaledIdentityMatrix to FieldMatrix |
| 247 |
template <class OtherScalar> |
| 248 |
requires requires(K k, OtherScalar otherScalar) { k + otherScalar; } |
| 249 |
friend auto& operator+= (FieldMatrix<OtherScalar,n,n>& fieldMatrix, |
| 250 |
const ScaledIdentityMatrix& matrix) |
| 251 |
{ |
| 252 |
for (int i=0; i<n; i++) |
| 253 |
fieldMatrix[i][i] += matrix.p_; |
| 254 |
|
| 255 |
return fieldMatrix; |
| 256 |
} |
| 257 |
|
| 258 |
//! Addition of ScaledIdentityMatrix to FieldMatrix |
| 259 |
template <class OtherScalar> |
| 260 |
requires requires(K k, OtherScalar otherScalar) { k + otherScalar; } |
| 261 |
friend auto operator+ (const FieldMatrix<OtherScalar,n,n>& fieldMatrix, |
| 262 |
const ScaledIdentityMatrix& matrix) |
| 263 |
{ |
| 264 |
using Result = FieldMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,n,n>; |
| 265 |
Result result = fieldMatrix; |
| 266 |
result += matrix; |
| 267 |
return result; |
| 268 |
} |
| 269 |
|
| 270 |
//! Addition of FieldMatrix to ScaledIdentityMatrix |
| 271 |
template <class OtherScalar> |
| 272 |
requires requires(K k, OtherScalar otherScalar) { k + otherScalar; } |
| 273 |
friend auto operator+ (const ScaledIdentityMatrix& matrix, |
| 274 |
const FieldMatrix<OtherScalar,n,n>& fieldMatrix) |
| 275 |
{ |
| 276 |
return fieldMatrix + matrix; |
| 277 |
} |
| 278 |
|
| 279 |
//! Addition of ScaledIdentityMatrix to DiagonalMatrix |
| 280 |
template <class OtherScalar> |
| 281 |
requires requires(K k, OtherScalar otherScalar) { k + otherScalar; } |
| 282 |
friend auto operator+= (DiagonalMatrix<OtherScalar,n>& diagonalMatrix, |
| 283 |
const ScaledIdentityMatrix& matrix) |
| 284 |
{ |
| 285 |
for (std::size_t i=0; i<n; i++) |
| 286 |
diagonalMatrix.diagonal(i) += matrix.p_; |
| 287 |
|
| 288 |
return diagonalMatrix; |
| 289 |
} |
| 290 |
|
| 291 |
//! Addition of ScaledIdentityMatrix to DiagonalMatrix |
| 292 |
template <class OtherScalar> |
| 293 |
requires requires(K k, OtherScalar otherScalar) { k + otherScalar; } |
| 294 |
friend auto operator+ (const DiagonalMatrix<OtherScalar,n>& diagonalMatrix, |
| 295 |
const ScaledIdentityMatrix& matrix) |
| 296 |
{ |
| 297 |
using Result = DiagonalMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,n>; |
| 298 |
Result result = diagonalMatrix; |
| 299 |
result += matrix; |
| 300 |
return result; |
| 301 |
} |
| 302 |
|
| 303 |
//! Addition of DiagonalMatrix to ScaledIdentityMatrix |
| 304 |
template <class OtherScalar> |
| 305 |
requires requires(K k, OtherScalar otherScalar) { k + otherScalar; } |
| 306 |
friend auto operator+ (const ScaledIdentityMatrix& matrix, |
| 307 |
const DiagonalMatrix<OtherScalar,n>& diagonalMatrix) |
| 308 |
{ |
| 309 |
return diagonalMatrix + matrix; |
| 310 |
} |
| 311 |
///@} // Vector space arithmetic |
| 312 |
|
| 313 |
//===== comparison ops |
| 314 |
|
| 315 |
//! Test for equality |
| 316 |
bool operator==(const ScaledIdentityMatrix& other) const |
| 317 |
{ |
| 318 |
return p_==other.scalar(); |
| 319 |
} |
| 320 |
|
| 321 |
//! Test for inequality |
| 322 |
bool operator!=(const ScaledIdentityMatrix& other) const |
| 323 |
{ |
| 324 |
return p_!=other.scalar(); |
| 325 |
} |
| 326 |
|
| 327 |
/** \name Matrix-vector products |
| 328 |
*/ |
| 329 |
///@{ |
| 330 |
|
| 331 |
//! y = A x |
| 332 |
template<class X, class Y> |
| 333 |
void mv (const X& x, Y& y) const |
| 334 |
{ |
| 335 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 336 |
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 337 |
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 338 |
#endif |
| 339 |
for (size_type i=0; i<n; ++i) |
| 340 |
y[i] = p_ * x[i]; |
| 341 |
} |
| 342 |
|
| 343 |
//! y = A^T x |
| 344 |
template<class X, class Y> |
| 345 |
void mtv (const X& x, Y& y) const |
| 346 |
{ |
| 347 |
mv(x, y); |
| 348 |
} |
| 349 |
|
| 350 |
//! y += A x |
| 351 |
template<class X, class Y> |
| 352 |
void umv (const X& x, Y& y) const |
| 353 |
{ |
| 354 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 355 |
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 356 |
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 357 |
#endif |
| 358 |
for (size_type i=0; i<n; ++i) |
| 359 |
y[i] += p_ * x[i]; |
| 360 |
} |
| 361 |
|
| 362 |
//! y += A^T x |
| 363 |
template<class X, class Y> |
| 364 |
void umtv (const X& x, Y& y) const |
| 365 |
{ |
| 366 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 367 |
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 368 |
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 369 |
#endif |
| 370 |
for (size_type i=0; i<n; ++i) |
| 371 |
y[i] += p_ * x[i]; |
| 372 |
} |
| 373 |
|
| 374 |
//! y += A^H x |
| 375 |
template<class X, class Y> |
| 376 |
void umhv (const X& x, Y& y) const |
| 377 |
{ |
| 378 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 379 |
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 380 |
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 381 |
#endif |
| 382 |
for (size_type i=0; i<n; i++) |
| 383 |
y[i] += conjugateComplex(p_)*x[i]; |
| 384 |
} |
| 385 |
|
| 386 |
//! y -= A x |
| 387 |
template<class X, class Y> |
| 388 |
void mmv (const X& x, Y& y) const |
| 389 |
{ |
| 390 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 391 |
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 392 |
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 393 |
#endif |
| 394 |
for (size_type i=0; i<n; ++i) |
| 395 |
y[i] -= p_ * x[i]; |
| 396 |
} |
| 397 |
|
| 398 |
//! y -= A^T x |
| 399 |
template<class X, class Y> |
| 400 |
void mmtv (const X& x, Y& y) const |
| 401 |
{ |
| 402 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 403 |
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 404 |
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 405 |
#endif |
| 406 |
for (size_type i=0; i<n; ++i) |
| 407 |
y[i] -= p_ * x[i]; |
| 408 |
} |
| 409 |
|
| 410 |
//! y -= A^H x |
| 411 |
template<class X, class Y> |
| 412 |
void mmhv (const X& x, Y& y) const |
| 413 |
{ |
| 414 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 415 |
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 416 |
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 417 |
#endif |
| 418 |
for (size_type i=0; i<n; i++) |
| 419 |
y[i] -= conjugateComplex(p_)*x[i]; |
| 420 |
} |
| 421 |
|
| 422 |
//! y += alpha A x |
| 423 |
template<class X, class Y> |
| 424 |
void usmv (const K& alpha, const X& x, Y& y) const |
| 425 |
{ |
| 426 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 427 |
if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 428 |
if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 429 |
#endif |
| 430 |
for (size_type i=0; i<n; i++) |
| 431 |
y[i] += alpha * p_ * x[i]; |
| 432 |
} |
| 433 |
|
| 434 |
//! y += alpha A^T x |
| 435 |
template<class X, class Y> |
| 436 |
void usmtv (const K& alpha, const X& x, Y& y) const |
| 437 |
{ |
| 438 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 439 |
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 440 |
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 441 |
#endif |
| 442 |
for (size_type i=0; i<n; i++) |
| 443 |
y[i] += alpha * p_ * x[i]; |
| 444 |
} |
| 445 |
|
| 446 |
//! y += alpha A^H x |
| 447 |
template<class X, class Y> |
| 448 |
void usmhv (const K& alpha, const X& x, Y& y) const |
| 449 |
{ |
| 450 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 451 |
if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range"); |
| 452 |
if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range"); |
| 453 |
#endif |
| 454 |
for (size_type i=0; i<n; i++) |
| 455 |
y[i] += alpha * conjugateComplex(p_) * x[i]; |
| 456 |
} |
| 457 |
|
| 458 |
///@} // Matrix-vector products |
| 459 |
|
| 460 |
|
| 461 |
/** @name Norms |
| 462 |
*/ |
| 463 |
///@{ |
| 464 |
|
| 465 |
//! The Frobenius norm |
| 466 |
typename FieldTraits<field_type>::real_type frobenius_norm () const |
| 467 |
{ |
| 468 |
return fvmeta::sqrt(n*p_*p_); |
| 469 |
} |
| 470 |
|
| 471 |
//! The square of the Frobenius norm |
| 472 |
typename FieldTraits<field_type>::real_type frobenius_norm2 () const |
| 473 |
{ |
| 474 |
return n*p_*p_; |
| 475 |
} |
| 476 |
|
| 477 |
/** \brief The row sum norm |
| 478 |
* |
| 479 |
* For a multiple of the identity matrix, this is simply the absolute value |
| 480 |
* of any diagonal matrix entry. |
| 481 |
*/ |
| 482 |
typename FieldTraits<field_type>::real_type infinity_norm () const |
| 483 |
{ |
| 484 |
return std::abs(p_); |
| 485 |
} |
| 486 |
|
| 487 |
//! simplified infinity norm (uses Manhattan norm for complex values) |
| 488 |
typename FieldTraits<field_type>::real_type infinity_norm_real () const |
| 489 |
{ |
| 490 |
return fvmeta::absreal(p_); |
| 491 |
} |
| 492 |
|
| 493 |
///@} |
| 494 |
|
| 495 |
/** \brief Solve linear system A x = b |
| 496 |
* |
| 497 |
* \tparam V Vector data type |
| 498 |
*/ |
| 499 |
template<class V> |
| 500 |
void solve (V& x, const V& b) const |
| 501 |
{ |
| 502 |
for (int i=0; i<n; i++) |
| 503 |
x[i] = b[i]/p_; |
| 504 |
} |
| 505 |
|
| 506 |
/** \brief Compute inverse |
| 507 |
*/ |
| 508 |
void invert() |
| 509 |
{ |
| 510 |
p_ = 1/p_; |
| 511 |
} |
| 512 |
|
| 513 |
//! Calculates the determinant of this matrix |
| 514 |
K determinant () const { |
| 515 |
return std::pow(p_,n); |
| 516 |
} |
| 517 |
|
| 518 |
//===== sizes |
| 519 |
|
| 520 |
//! number of blocks in row direction |
| 521 |
size_type N () const |
| 522 |
{ |
| 523 |
return n; |
| 524 |
} |
| 525 |
|
| 526 |
//! number of blocks in column direction |
| 527 |
size_type M () const |
| 528 |
{ |
| 529 |
return n; |
| 530 |
} |
| 531 |
|
| 532 |
//===== query |
| 533 |
|
| 534 |
//! Return true if (i,j) is in the matrix pattern, i.e., if i==j |
| 535 |
bool exists (size_type i, size_type j) const |
| 536 |
{ |
| 537 |
#ifdef DUNE_FMatrix_WITH_CHECKING |
| 538 |
if (i<0 || i>=n) DUNE_THROW(FMatrixError,"row index out of range"); |
| 539 |
if (j<0 || j>=n) DUNE_THROW(FMatrixError,"column index out of range"); |
| 540 |
#endif |
| 541 |
return i==j; |
| 542 |
} |
| 543 |
|
| 544 |
//===== conversion operator |
| 545 |
|
| 546 |
/** \brief Sends the matrix to an output stream */ |
| 547 |
friend std::ostream& operator<< (std::ostream& s, const ScaledIdentityMatrix<K,n>& a) |
| 548 |
{ |
| 549 |
for (size_type i=0; i<n; i++) { |
| 550 |
for (size_type j=0; j<n; j++) |
| 551 |
s << ((i==j) ? a.p_ : 0) << " "; |
| 552 |
s << std::endl; |
| 553 |
} |
| 554 |
return s; |
| 555 |
} |
| 556 |
|
| 557 |
//! Return reference object as row replacement |
| 558 |
reference operator[](size_type i) |
| 559 |
{ |
| 560 |
return reference(const_cast<K*>(&p_), i); |
| 561 |
} |
| 562 |
|
| 563 |
//! Return const_reference object as row replacement |
| 564 |
const_reference operator[](size_type i) const |
| 565 |
{ |
| 566 |
return const_reference(const_cast<K*>(&p_), i); |
| 567 |
} |
| 568 |
|
| 569 |
//! Get const reference to diagonal entry |
| 570 |
const K& diagonal(size_type /*i*/) const |
| 571 |
{ |
| 572 |
return p_; |
| 573 |
} |
| 574 |
|
| 575 |
//! Get reference to diagonal entry |
| 576 |
K& diagonal(size_type /*i*/) |
| 577 |
{ |
| 578 |
return p_; |
| 579 |
} |
| 580 |
|
| 581 |
/** \brief Get const reference to the scalar diagonal value |
| 582 |
*/ |
| 583 |
const K& scalar() const |
| 584 |
{ |
| 585 |
return p_; |
| 586 |
} |
| 587 |
|
| 588 |
/** \brief Get reference to the scalar diagonal value |
| 589 |
*/ |
| 590 |
K& scalar() |
| 591 |
{ |
| 592 |
return p_; |
| 593 |
} |
| 594 |
|
| 595 |
private: |
| 596 |
// the data, very simply a single number |
| 597 |
K p_; |
| 598 |
|
| 599 |
}; |
| 600 |
|
| 601 |
template <class DenseMatrix, class field, int N> |
| 602 |
struct DenseMatrixAssigner<DenseMatrix, ScaledIdentityMatrix<field, N>> { |
| 603 |
static void apply(DenseMatrix& denseMatrix, |
| 604 |
ScaledIdentityMatrix<field, N> const& rhs) { |
| 605 |
assert(denseMatrix.M() == N); |
| 606 |
assert(denseMatrix.N() == N); |
| 607 |
denseMatrix = field(0); |
| 608 |
for (int i = 0; i < N; ++i) |
| 609 |
denseMatrix[i][i] = rhs.scalar(); |
| 610 |
} |
| 611 |
}; |
| 612 |
|
| 613 |
template<class K, int n> |
| 614 |
struct FieldTraits< ScaledIdentityMatrix<K, n> > |
| 615 |
{ |
| 616 |
using field_type = typename ScaledIdentityMatrix<K, n>::field_type; |
| 617 |
using real_type = typename FieldTraits<field_type>::real_type; |
| 618 |
}; |
| 619 |
|
| 620 |
} // end namespace |
| 621 |
|
| 622 |
#endif |