/usr/include/deal.II/lac/matrix_lib.h is in libdeal.ii-dev 8.4.2-2+b1.
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 | // ---------------------------------------------------------------------
//
// Copyright (C) 2002 - 2015 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef dealii__matrix_lib_h
#define dealii__matrix_lib_h
#include <deal.II/base/subscriptor.h>
#include <deal.II/lac/vector_memory.h>
#include <deal.II/lac/pointer_matrix.h>
#include <deal.II/lac/solver_richardson.h>
DEAL_II_NAMESPACE_OPEN
template<typename number> class Vector;
template<typename number> class BlockVector;
template<typename number> class SparseMatrix;
/*! @addtogroup Matrix2
*@{
*/
/**
* Poor man's matrix product of two quadratic matrices. Stores two quadratic
* matrices #m1 and #m2 of arbitrary types and implements matrix-vector
* multiplications for the product <i>M<sub>1</sub>M<sub>2</sub></i> by
* performing multiplication with both factors consecutively. Because the
* types of the matrices are opaque (i.e., they can be arbitrary), you can
* stack products of three or more matrices by making one of the two matrices
* an object of the current type handles be a ProductMatrix itself.
*
* Here is an example multiplying two different FullMatrix objects:
* @include product_matrix.cc
*
* @deprecated If deal.II was configured with C++11 support, use the
* LinearOperator class instead, see the module on
* @ref LAOperators "linear operators"
* for further details.
*
* @author Guido Kanschat, 2000, 2001, 2002, 2005
*/
template<typename VectorType>
class ProductMatrix : public PointerMatrixBase<VectorType>
{
public:
/**
* Standard constructor. Matrices and the memory pool must be added later
* using initialize().
*/
ProductMatrix();
/**
* Constructor only assigning the memory pool. Matrices must be added by
* reinit() later.
*/
ProductMatrix(VectorMemory<VectorType> &mem);
/**
* Constructor. Additionally to the two constituting matrices, a memory
* pool for the auxiliary vector must be provided.
*/
template <typename MatrixType1, typename MatrixType2>
ProductMatrix (const MatrixType1 &m1,
const MatrixType2 &m2,
VectorMemory<VectorType> &mem);
/**
* Destructor.
*/
~ProductMatrix();
/**
* Change the matrices.
*/
template <typename MatrixType1, typename MatrixType2>
void reinit (const MatrixType1 &m1, const MatrixType2 &m2);
/**
* Change the matrices and memory pool.
*/
template <typename MatrixType1, typename MatrixType2>
void initialize (const MatrixType1 &m1,
const MatrixType2 &m2,
VectorMemory<VectorType> &mem);
// Doc in PointerMatrixBase
void clear();
/**
* Matrix-vector product <i>w = m1 * m2 * v</i>.
*/
virtual void vmult (VectorType &w,
const VectorType &v) const;
/**
* Transposed matrix-vector product <i>w = m2<sup>T</sup> * m1<sup>T</sup> *
* v</i>.
*/
virtual void Tvmult (VectorType &w,
const VectorType &v) const;
/**
* Adding matrix-vector product <i>w += m1 * m2 * v</i>
*/
virtual void vmult_add (VectorType &w,
const VectorType &v) const;
/**
* Adding, transposed matrix-vector product <i>w += m2<sup>T</sup> *
* m1<sup>T</sup> * v</i>.
*/
virtual void Tvmult_add (VectorType &w,
const VectorType &v) const;
private:
/**
* The left matrix of the product.
*/
PointerMatrixBase<VectorType> *m1;
/**
* The right matrix of the product.
*/
PointerMatrixBase<VectorType> *m2;
/**
* Memory for auxiliary vector.
*/
SmartPointer<VectorMemory<VectorType>,ProductMatrix<VectorType> > mem;
};
/**
* A matrix that is the multiple of another matrix.
*
* Matrix-vector products of this matrix are composed of those of the original
* matrix with the vector and then scaling of the result by a constant factor.
*
* @deprecated If deal.II was configured with C++11 support, use the
* LinearOperator class instead, see the module on
* @ref LAOperators "linear operators"
* for further details.
*
* @author Guido Kanschat, 2007
*/
template<typename VectorType>
class ScaledMatrix : public Subscriptor
{
public:
/**
* Constructor leaving an uninitialized object.
*/
ScaledMatrix ();
/**
* Constructor with initialization.
*/
template <typename MatrixType>
ScaledMatrix (const MatrixType &M, const double factor);
/**
* Destructor
*/
~ScaledMatrix ();
/**
* Initialize for use with a new matrix and factor.
*/
template <typename MatrixType>
void initialize (const MatrixType &M, const double factor);
/**
* Reset the object to its original state.
*/
void clear ();
/**
* Matrix-vector product.
*/
void vmult (VectorType &w, const VectorType &v) const;
/**
* Transposed matrix-vector product.
*/
void Tvmult (VectorType &w, const VectorType &v) const;
private:
/**
* The matrix.
*/
PointerMatrixBase<VectorType> *m;
/**
* The scaling factor;
*/
double factor;
};
/**
* Poor man's matrix product of two sparse matrices. Stores two matrices #m1
* and #m2 of arbitrary type SparseMatrix and implements matrix-vector
* multiplications for the product <i>M<sub>1</sub>M<sub>2</sub></i> by
* performing multiplication with both factors consecutively.
*
* The documentation of ProductMatrix applies with exception that these
* matrices here may be rectangular.
*
* @deprecated If deal.II was configured with C++11 support, use the
* LinearOperator class instead, see the module on
* @ref LAOperators "linear operators"
* for further details.
*
* @author Guido Kanschat, 2000, 2001, 2002, 2005
*/
template<typename number, typename vector_number>
class ProductSparseMatrix : public PointerMatrixBase<Vector<vector_number> >
{
public:
/**
* Define the type of matrices used.
*/
typedef SparseMatrix<number> MatrixType;
/**
* Define the type of vectors we plly this matrix to.
*/
typedef Vector<vector_number> VectorType;
/**
* Constructor. Additionally to the two constituting matrices, a memory
* pool for the auxiliary vector must be provided.
*/
ProductSparseMatrix (const MatrixType &m1,
const MatrixType &m2,
VectorMemory<VectorType> &mem);
/**
* Constructor leaving an uninitialized matrix. initialize() must be called,
* before the matrix can be used.
*/
ProductSparseMatrix();
void initialize (const MatrixType &m1,
const MatrixType &m2,
VectorMemory<VectorType> &mem);
// Doc in PointerMatrixBase
void clear();
/**
* Matrix-vector product <i>w = m1 * m2 * v</i>.
*/
virtual void vmult (VectorType &w,
const VectorType &v) const;
/**
* Transposed matrix-vector product <i>w = m2<sup>T</sup> * m1<sup>T</sup> *
* v</i>.
*/
virtual void Tvmult (VectorType &w,
const VectorType &v) const;
/**
* Adding matrix-vector product <i>w += m1 * m2 * v</i>
*/
virtual void vmult_add (VectorType &w,
const VectorType &v) const;
/**
* Adding, transposed matrix-vector product <i>w += m2<sup>T</sup> *
* m1<sup>T</sup> * v</i>.
*/
virtual void Tvmult_add (VectorType &w,
const VectorType &v) const;
private:
/**
* The left matrix of the product.
*/
SmartPointer<const MatrixType,ProductSparseMatrix<number,vector_number> > m1;
/**
* The right matrix of the product.
*/
SmartPointer<const MatrixType,ProductSparseMatrix<number,vector_number> > m2;
/**
* Memory for auxiliary vector.
*/
SmartPointer<VectorMemory<VectorType>,ProductSparseMatrix<number,vector_number> > mem;
};
/**
* Mean value filter. The vmult() functions of this matrix filter out mean
* values of the vector. If the vector is of type BlockVector, then an
* additional parameter selects a single component for this operation.
*
* In mathematical terms, this class acts as if it was the matrix $I-\frac
* 1n{\mathbf 1}_n{\mathbf 1}_n^T$ where ${\mathbf 1}_n$ is a vector of size
* $n$ that has only ones as its entries. Thus, taking the dot product between
* a vector $\mathbf v$ and $\frac 1n {\mathbf 1}_n$ yields the <i>mean
* value</i> of the entries of ${\mathbf v}$. Consequently, $ \left[I-\frac
* 1n{\mathbf 1}_n{\mathbf 1}_n^T\right] \mathbf v = \mathbf v - \left[\frac
* 1n {\mathbf v} \cdot {\mathbf 1}_n\right]{\mathbf 1}_n$ subtracts from every
* vector element the mean value of all elements.
*
* @author Guido Kanschat, 2002, 2003
*/
class MeanValueFilter : public Subscriptor
{
public:
/**
* Declare type for container size.
*/
typedef types::global_dof_index size_type;
/**
* Constructor, optionally selecting a component.
*/
MeanValueFilter(const size_type component = numbers::invalid_size_type);
/**
* Subtract mean value from @p v.
*/
template <typename number>
void filter (Vector<number> &v) const;
/**
* Subtract mean value from @p v.
*/
template <typename number>
void filter (BlockVector<number> &v) const;
/**
* Return the source vector with subtracted mean value.
*/
template <typename number>
void vmult (Vector<number> &dst,
const Vector<number> &src) const;
/**
* Add source vector with subtracted mean value to dest.
*/
template <typename number>
void vmult_add (Vector<number> &dst,
const Vector<number> &src) const;
/**
* Return the source vector with subtracted mean value in selected
* component.
*/
template <typename number>
void vmult (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Add a source to dest, where the mean value in the selected component is
* subtracted.
*/
template <typename number>
void vmult_add (BlockVector<number> &dst,
const BlockVector<number> &src) const;
/**
* Not implemented.
*/
template <typename VectorType>
void Tvmult(VectorType &, const VectorType &) const;
/**
* Not implemented.
*/
template <typename VectorType>
void Tvmult_add(VectorType &, const VectorType &) const;
private:
/**
* Component for filtering block vectors.
*/
const size_type component;
};
/**
* Objects of this type represent the inverse of a matrix as computed
* approximately by using the SolverRichardson iterative solver. In other
* words, if you set up an object of the current type for a matrix $A$, then
* calling the vmult() function with arguments $v,w$ amounts to setting
* $w=A^{-1}v$ by solving the linear system $Aw=v$ using the Richardson solver
* with a preconditioner that can be chosen. Similarly, this class allows to
* also multiple with the transpose of the inverse (i.e., the inverse of the
* transpose) using the function SolverRichardson::Tsolve().
*
* The functions vmult() and Tvmult() approximate the inverse iteratively
* starting with the vector <tt>dst</tt>. Functions vmult_add() and
* Tvmult_add() start the iteration with a zero vector. All of the matrix-
* vector multiplication functions expect that the Richardson solver with the
* given preconditioner actually converge. If the Richardson solver does not
* converge within the specified number of iterations, the exception that will
* result in the solver will simply be propagated to the caller of the member
* function of the current class.
*
* @note A more powerful version of this class is provided by the
* IterativeInverse class.
*
* @note Instantiations for this template are provided for <tt>@<float@> and
* @<double@></tt>; others can be generated in application programs (see the
* section on
* @ref Instantiations
* in the manual).
*
* @deprecated If deal.II was configured with C++11 support, use the
* LinearOperator class instead, see the module on
* @ref LAOperators "linear operators"
* for further details.
*
* @author Guido Kanschat, 2005
*/
template<typename VectorType>
class InverseMatrixRichardson : public Subscriptor
{
public:
/**
* Constructor, initializing the solver with a control and memory object.
* The inverted matrix and the preconditioner are added in initialize().
*/
InverseMatrixRichardson (SolverControl &control,
VectorMemory<VectorType> &mem);
/**
* Since we use two pointers, we must implement a destructor.
*/
~InverseMatrixRichardson();
/**
* Initialization function. Provide a solver object, a matrix, and another
* preconditioner for this.
*/
template <typename MatrixType, typename PreconditionerType>
void initialize (const MatrixType &,
const PreconditionerType &);
/**
* Access to the SolverControl object used by the solver.
*/
SolverControl &control() const;
/**
* Execute solver.
*/
void vmult (VectorType &, const VectorType &) const;
/**
* Execute solver.
*/
void vmult_add (VectorType &, const VectorType &) const;
/**
* Execute transpose solver.
*/
void Tvmult (VectorType &, const VectorType &) const;
/**
* Execute transpose solver.
*/
void Tvmult_add (VectorType &, const VectorType &) const;
private:
/**
* A reference to the provided VectorMemory object.
*/
VectorMemory<VectorType> &mem;
/**
* The solver object.
*/
mutable SolverRichardson<VectorType> solver;
/**
* The matrix in use.
*/
PointerMatrixBase<VectorType> *matrix;
/**
* The preconditioner to use.
*/
PointerMatrixBase<VectorType> *precondition;
};
/*@}*/
//---------------------------------------------------------------------------
template<typename VectorType>
inline
ScaledMatrix<VectorType>::ScaledMatrix()
:
m(0)
{}
template<typename VectorType>
template<typename MatrixType>
inline
ScaledMatrix<VectorType>::ScaledMatrix(const MatrixType &mat, const double factor)
:
m(new_pointer_matrix_base(mat, VectorType())),
factor(factor)
{}
template<typename VectorType>
template<typename MatrixType>
inline
void
ScaledMatrix<VectorType>::initialize(const MatrixType &mat, const double f)
{
if (m) delete m;
m = new_pointer_matrix_base(mat, VectorType());
factor = f;
}
template<typename VectorType>
inline
void
ScaledMatrix<VectorType>::clear()
{
if (m) delete m;
m = 0;
}
template<typename VectorType>
inline
ScaledMatrix<VectorType>::~ScaledMatrix()
{
clear ();
}
template<typename VectorType>
inline
void
ScaledMatrix<VectorType>::vmult (VectorType &w, const VectorType &v) const
{
m->vmult(w, v);
w *= factor;
}
template<typename VectorType>
inline
void
ScaledMatrix<VectorType>::Tvmult (VectorType &w, const VectorType &v) const
{
m->Tvmult(w, v);
w *= factor;
}
//---------------------------------------------------------------------------
template<typename VectorType>
ProductMatrix<VectorType>::ProductMatrix ()
: m1(0), m2(0), mem(0)
{}
template<typename VectorType>
ProductMatrix<VectorType>::ProductMatrix (VectorMemory<VectorType> &m)
: m1(0), m2(0), mem(&m)
{}
template<typename VectorType>
template<typename MatrixType1, typename MatrixType2>
ProductMatrix<VectorType>::ProductMatrix (const MatrixType1 &mat1,
const MatrixType2 &mat2,
VectorMemory<VectorType> &m)
: mem(&m)
{
m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
}
template<typename VectorType>
template<typename MatrixType1, typename MatrixType2>
void
ProductMatrix<VectorType>::reinit (const MatrixType1 &mat1, const MatrixType2 &mat2)
{
if (m1) delete m1;
if (m2) delete m2;
m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
}
template<typename VectorType>
template<typename MatrixType1, typename MatrixType2>
void
ProductMatrix<VectorType>::initialize (const MatrixType1 &mat1,
const MatrixType2 &mat2,
VectorMemory<VectorType> &memory)
{
mem = &memory;
if (m1) delete m1;
if (m2) delete m2;
m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
}
template<typename VectorType>
ProductMatrix<VectorType>::~ProductMatrix ()
{
if (m1) delete m1;
if (m2) delete m2;
}
template<typename VectorType>
void
ProductMatrix<VectorType>::clear ()
{
if (m1) delete m1;
m1 = 0;
if (m2) delete m2;
m2 = 0;
}
template<typename VectorType>
void
ProductMatrix<VectorType>::vmult (VectorType &dst, const VectorType &src) const
{
Assert (mem != 0, ExcNotInitialized());
Assert (m1 != 0, ExcNotInitialized());
Assert (m2 != 0, ExcNotInitialized());
VectorType *v = mem->alloc();
v->reinit(dst);
m2->vmult (*v, src);
m1->vmult (dst, *v);
mem->free(v);
}
template<typename VectorType>
void
ProductMatrix<VectorType>::vmult_add (VectorType &dst, const VectorType &src) const
{
Assert (mem != 0, ExcNotInitialized());
Assert (m1 != 0, ExcNotInitialized());
Assert (m2 != 0, ExcNotInitialized());
VectorType *v = mem->alloc();
v->reinit(dst);
m2->vmult (*v, src);
m1->vmult_add (dst, *v);
mem->free(v);
}
template<typename VectorType>
void
ProductMatrix<VectorType>::Tvmult (VectorType &dst, const VectorType &src) const
{
Assert (mem != 0, ExcNotInitialized());
Assert (m1 != 0, ExcNotInitialized());
Assert (m2 != 0, ExcNotInitialized());
VectorType *v = mem->alloc();
v->reinit(dst);
m1->Tvmult (*v, src);
m2->Tvmult (dst, *v);
mem->free(v);
}
template<typename VectorType>
void
ProductMatrix<VectorType>::Tvmult_add (VectorType &dst, const VectorType &src) const
{
Assert (mem != 0, ExcNotInitialized());
Assert (m1 != 0, ExcNotInitialized());
Assert (m2 != 0, ExcNotInitialized());
VectorType *v = mem->alloc();
v->reinit(dst);
m1->Tvmult (*v, src);
m2->Tvmult_add (dst, *v);
mem->free(v);
}
//---------------------------------------------------------------------------
template <typename VectorType>
inline void
MeanValueFilter::Tvmult(VectorType &, const VectorType &) const
{
Assert(false, ExcNotImplemented());
}
template <typename VectorType>
inline void
MeanValueFilter::Tvmult_add(VectorType &, const VectorType &) const
{
Assert(false, ExcNotImplemented());
}
//-----------------------------------------------------------------------//
template <typename VectorType>
template <typename MatrixType, typename PreconditionerType>
inline void
InverseMatrixRichardson<VectorType>::initialize (const MatrixType &m,
const PreconditionerType &p)
{
if (matrix != 0)
delete matrix;
matrix = new PointerMatrix<MatrixType, VectorType>(&m);
if (precondition != 0)
delete precondition;
precondition = new PointerMatrix<PreconditionerType, VectorType>(&p);
}
DEAL_II_NAMESPACE_CLOSE
#endif
|