/usr/include/deal.II/lac/full_matrix.h is in libdeal.ii-dev 6.3.1-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 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 | //---------------------------------------------------------------------------
// $Id: full_matrix.h 20220 2009-12-09 18:12:05Z kronbichler $
// Version: $Name$
//
// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by the deal.II authors
//
// This file is subject to QPL and may not be distributed
// without copyright and license information. Please refer
// to the file deal.II/doc/license.html for the text and
// further information on this license.
//
//---------------------------------------------------------------------------
#ifndef __deal2__full_matrix_h
#define __deal2__full_matrix_h
#include <base/config.h>
#include <base/numbers.h>
#include <base/table.h>
#include <lac/exceptions.h>
#include <lac/identity_matrix.h>
#include <lac/lapack_full_matrix.h>
#include <vector>
#include <iomanip>
DEAL_II_NAMESPACE_OPEN
// forward declarations
template <typename number> class Vector;
template <int rank, int dim> class Tensor;
/*! @addtogroup Matrix1
*@{
*/
/**
* Implementation of a classical rectangular scheme of numbers. The
* data type of the entries is provided in the template argument
* <tt>number</tt>. The interface is quite fat and in fact has grown every
* time a new feature was needed. So, a lot of functions are provided.
*
* Internal calculations are usually done with the accuracy of the
* vector argument to functions. If there is no argument with a number
* type, the matrix number type is used.
*
* @note Instantiations for this template are provided for
* <tt>@<float@>, @<double@>, @<long double@>,
* @<std::complex@<float@>@>, @<std::complex@<double@>@>,
* @<std::complex@<long double@>@></tt>; others can be generated in
* application programs (see the section on @ref Instantiations in the
* manual).
*
* @author Guido Kanschat, Franz-Theo Suttmeier, Wolfgang Bangerth, 1993-2004
*/
template<typename number>
class FullMatrix : public Table<2,number>
{
public:
/**
* Type of matrix entries. In analogy to
* the STL container classes.
*/
typedef number value_type;
/**
* Declare a type that has holds
* real-valued numbers with the
* same precision as the template
* argument to this class. If the
* template argument of this
* class is a real data type,
* then real_type equals the
* template argument. If the
* template argument is a
* std::complex type then
* real_type equals the type
* underlying the complex
* numbers.
*
* This typedef is used to
* represent the return type of
* norms.
*/
typedef typename numbers::NumberTraits<number>::real_type real_type;
class const_iterator;
/**
* Accessor class for iterators
*/
class Accessor
{
public:
/**
* Constructor. Since we use
* accessors only for read
* access, a const matrix
* pointer is sufficient.
*/
Accessor (const FullMatrix<number> *matrix,
const unsigned int row,
const unsigned int col);
/**
* Row number of the element
* represented by this
* object.
*/
unsigned int row() const;
/**
* Column number of the
* element represented by
* this object.
*/
unsigned int column() const;
/**
* Value of this matrix entry.
*/
number value() const;
protected:
/**
* The matrix accessed.
*/
const FullMatrix<number>* matrix;
/**
* Current row number.
*/
unsigned int a_row;
/**
* Current column number.
*/
unsigned short a_col;
/*
* Make enclosing class a
* friend.
*/
friend class const_iterator;
};
/**
* STL conforming iterator.
*/
class const_iterator
{
public:
/**
* Constructor.
*/
const_iterator(const FullMatrix<number> *matrix,
const unsigned int row,
const unsigned int col);
/**
* Prefix increment.
*/
const_iterator& operator++ ();
/**
* Postfix increment.
*/
const_iterator& operator++ (int);
/**
* Dereferencing operator.
*/
const Accessor& operator* () const;
/**
* Dereferencing operator.
*/
const Accessor* operator-> () const;
/**
* Comparison. True, if
* both iterators point to
* the same matrix
* position.
*/
bool operator == (const const_iterator&) const;
/**
* Inverse of <tt>==</tt>.
*/
bool operator != (const const_iterator&) const;
/**
* Comparison operator. Result is
* true if either the first row
* number is smaller or if the row
* numbers are equal and the first
* index is smaller.
*/
bool operator < (const const_iterator&) const;
/**
* Comparison operator. Compares just
* the other way around than the
* operator above.
*/
bool operator > (const const_iterator&) const;
private:
/**
* Store an object of the
* accessor class.
*/
Accessor accessor;
};
/**
* @name Constructors and initalization.
* See also the base class Table.
*/
//@{
/**
* Constructor. Initialize the
* matrix as a square matrix with
* dimension <tt>n</tt>.
*
* In order to avoid the implicit
* conversion of integers and
* other types to a matrix, this
* constructor is declared
* <tt>explicit</tt>.
*
* By default, no memory is
* allocated.
*/
explicit FullMatrix (const unsigned int n = 0);
/**
* Constructor. Initialize the
* matrix as a rectangular
* matrix.
*/
FullMatrix (const unsigned int rows,
const unsigned int cols);
/**
* Copy constructor. This
* constructor does a deep copy
* of the matrix. Therefore, it
* poses a possible efficiency
* problem, if for example,
* function arguments are passed
* by value rather than by
* reference. Unfortunately, we
* can't mark this copy
* constructor <tt>explicit</tt>,
* since that prevents the use of
* this class in containers, such
* as <tt>std::vector</tt>. The
* responsibility to check
* performance of programs must
* therefore remain with the
* user of this class.
*/
FullMatrix (const FullMatrix&);
/**
* Constructor initializing from
* an array of numbers. The array
* is arranged line by line. No
* range checking is performed.
*/
FullMatrix (const unsigned int rows,
const unsigned int cols,
const number* entries);
/**
* Construct a full matrix that
* equals the identity matrix of
* the size of the
* argument. Using this
* constructor, one can easily
* create an identity matrix of
* size <code>n</code> by saying
* @verbatim
* FullMatrix<double> M(IdentityMatrix(n));
* @endverbatim
*/
explicit FullMatrix (const IdentityMatrix &id);
/**
* Assignment operator.
*/
FullMatrix<number> &
operator = (const FullMatrix<number>&);
/**
* Variable assignment operator.
*/
template<typename number2>
FullMatrix<number> &
operator = (const FullMatrix<number2>&);
/**
* This operator assigns a scalar
* to a matrix. To avoid
* confusion with the semantics
* of this function, zero is the
* only value allowed for
* <tt>d</tt>, allowing you to
* clear a matrix in an intuitive
* way.
*/
FullMatrix<number> &
operator = (const number d);
/**
* Copy operator to create a full
* matrix that equals the
* identity matrix of the size of
* the argument. This way, one can easily
* create an identity matrix of
* size <code>n</code> by saying
* @verbatim
* M = IdentityMatrix(n);
* @endverbatim
*/
FullMatrix<number> &
operator = (const IdentityMatrix &id);
/**
* Assignment operator for a
* LapackFullMatrix. The calling matrix
* must be of the same size as the
* LAPACK matrix.
*/
template <typename number2>
FullMatrix<number> &
operator = (const LAPACKFullMatrix<number2>&);
/**
* Assignment from different
* matrix classes. This
* assignment operator uses
* iterators of the class
* MATRIX. Therefore, sparse
* matrices are possible sources.
*/
template <class MATRIX>
void copy_from (const MATRIX&);
/**
* Fill rectangular block.
*
* A rectangular block of the
* matrix <tt>src</tt> is copied into
* <tt>this</tt>. The upper left
* corner of the block being
* copied is
* <tt>(src_offset_i,src_offset_j)</tt>.
* The upper left corner of the
* copied block is
* <tt>(dst_offset_i,dst_offset_j)</tt>.
* The size of the rectangular
* block being copied is the
* maximum size possible,
* determined either by the size
* of <tt>this</tt> or <tt>src</tt>.
*/
template<typename number2>
void fill (const FullMatrix<number2> &src,
const unsigned int dst_offset_i = 0,
const unsigned int dst_offset_j = 0,
const unsigned int src_offset_i = 0,
const unsigned int src_offset_j = 0);
/**
* Make function of base class
* available.
*/
template<typename number2>
void fill (const number2*);
/**
* Fill with permutation of
* another matrix.
*
* The matrix <tt>src</tt> is copied
* into the target. The two
* permutation <tt>p_r</tt> and
* <tt>p_c</tt> operate in a way, such
* that <tt>result(i,j) =
* src(p_r[i], p_c[j])</tt>.
*
* The vectors may also be a
* selection from a larger set of
* integers, if the matrix
* <tt>src</tt> is bigger. It is also
* possible to duplicate rows or
* columns by this method.
*/
template<typename number2>
void fill_permutation (const FullMatrix<number2> &src,
const std::vector<unsigned int> &p_rows,
const std::vector<unsigned int> &p_cols);
//@}
///@name Non-modifying operators
//@{
/**
* Comparison operator. Be
* careful with this thing, it
* may eat up huge amounts of
* computing time! It is most
* commonly used for internal
* consistency checks of
* programs.
*/
bool operator == (const FullMatrix<number> &) const;
/**
* Number of rows of this matrix.
* To remember: this matrix is an
* <i>m x n</i>-matrix.
*/
unsigned int m () const;
/**
* Number of columns of this matrix.
* To remember: this matrix is an
* <i>m x n</i>-matrix.
*/
unsigned int n () const;
/**
* Return whether the matrix
* contains only elements with
* value zero. This function is
* mainly for internal
* consistency checks and should
* seldomly be used when not in
* debug mode since it uses quite
* some time.
*/
bool all_zero () const;
/**
* Return the square of the norm
* of the vector <tt>v</tt> induced by
* this matrix,
* i.e. <i>(v,Mv)</i>. This is
* useful, e.g. in the finite
* element context, where the
* <i>L<sup>2</sup></i> norm of a
* function equals the matrix
* norm with respect to the mass
* matrix of the vector
* representing the nodal values
* of the finite element
* function.
*
* Obviously, the matrix needs to be
* quadratic for this operation, and for
* the result to actually be a norm it
* also needs to be either real symmetric
* or complex hermitian.
*
* The underlying template types of both
* this matrix and the given vector
* should either both be real or
* complex-valued, but not mixed, for
* this function to make sense.
*/
template<typename number2>
number2 matrix_norm_square (const Vector<number2> &v) const;
/**
* Build the matrix scalar
* product <tt>u<sup>T</sup> M
* v</tt>. This function is
* mostly useful when building
* the cellwise scalar product of
* two functions in the finite
* element context.
*
* The underlying template types of both
* this matrix and the given vector
* should either both be real or
* complex-valued, but not mixed, for
* this function to make sense.
*/
template<typename number2>
number2 matrix_scalar_product (const Vector<number2> &u,
const Vector<number2> &v) const;
/**
* Return the
* <i>l<sub>1</sub></i>-norm of
* the matrix, where
* $||M||_1 = \max_j \sum_i
* |M_{ij}|$ (maximum of
* the sums over columns).
*/
real_type l1_norm () const;
/**
* Return the $l_\infty$-norm of
* the matrix, where
* $||M||_\infty = \max_i \sum_j
* |M_{ij}|$ (maximum of the sums
* over rows).
*/
real_type linfty_norm () const;
/**
* Compute the Frobenius norm of
* the matrix. Return value is
* the root of the square sum of
* all matrix entries.
*
* @note For the timid among us:
* this norm is not the norm
* compatible with the
* <i>l<sub>2</sub></i>-norm of
* the vector space.
*/
real_type frobenius_norm () const;
/**
* Compute the relative norm of
* the skew-symmetric part. The
* return value is the Frobenius
* norm of the skew-symmetric
* part of the matrix divided by
* that of the matrix.
*
* Main purpose of this function
* is to check, if a matrix is
* symmetric within a certain
* accuracy, or not.
*/
real_type relative_symmetry_norm2 () const;
/**
* Computes the determinant of a
* matrix. This is only
* implemented for one, two, and
* three dimensions, since for
* higher dimensions the
* numerical work explodes.
* Obviously, the matrix needs to
* be quadratic for this function.
*/
number determinant () const;
/**
* Return the trace of the matrix,
* i.e. the sum of the diagonal values
* (which happens to also equal the sum
* of the eigenvalues of a matrix).
* Obviously, the matrix needs to
* be quadratic for this function.
*/
number trace () const;
/**
* Output of the matrix in
* user-defined format.
*/
template <class STREAM>
void print (STREAM &s,
const unsigned int width=5,
const unsigned int precision=2) const;
/**
* Print the matrix and allow
* formatting of entries.
*
* The parameters allow for a
* flexible setting of the output
* format:
*
* @arg <tt>precision</tt>
* denotes the number of trailing
* digits.
*
* @arg <tt>scientific</tt> is
* used to determine the number
* format, where
* <tt>scientific</tt> =
* <tt>false</tt> means fixed
* point notation.
*
* @arg <tt>width</tt> denotes
* the with of each column. A
* zero entry for <tt>width</tt>
* makes the function compute a
* width, but it may be changed
* to a positive value, if output
* is crude.
*
* @arg <tt>zero_string</tt>
* specifies a string printed for
* zero entries.
*
* @arg <tt>denominator</tt>
* Multiply the whole matrix by
* this common denominator to get
* nicer numbers.
*
* @arg <tt>threshold</tt>: all
* entries with absolute value
* smaller than this are
* considered zero.
*/
void print_formatted (std::ostream &out,
const unsigned int precision=3,
const bool scientific = true,
const unsigned int width = 0,
const char *zero_string = " ",
const double denominator = 1.,
const double threshold = 0.) const;
/**
* Determine an estimate for the
* memory consumption (in bytes)
* of this object.
*/
unsigned int memory_consumption () const;
//@}
///@name Iterator functions
//@{
/**
* STL-like iterator with the
* first entry.
*/
const_iterator begin () const;
/**
* Final iterator.
*/
const_iterator end () const;
/**
* STL-like iterator with the
* first entry of row <tt>r</tt>.
*/
const_iterator begin (const unsigned int r) const;
/**
* Final iterator of row <tt>r</tt>.
*/
const_iterator end (const unsigned int r) const;
//@}
///@name Modifying operators
//@{
/**
* Scale the entire matrix by a
* fixed factor.
*/
FullMatrix & operator *= (const number factor);
/**
* Scale the entire matrix by the
* inverse of the given factor.
*/
FullMatrix & operator /= (const number factor);
/**
* Simple addition of a scaled
* matrix, i.e. <tt>*this +=
* a*A</tt>.
*
* The matrix <tt>A</tt> may be a
* full matrix over an arbitrary
* underlying scalar type, as
* long as its data type is
* convertible to the data type
* of this matrix.
*/
template<typename number2>
void add (const number a,
const FullMatrix<number2> &A);
/**
* Multiple addition of scaled
* matrices, i.e. <tt>*this +=
* a*A + b*B</tt>.
*
* The matrices <tt>A</tt> and
* <tt>B</tt> may be a full
* matrix over an arbitrary
* underlying scalar type, as
* long as its data type is
* convertible to the data type
* of this matrix.
*/
template<typename number2>
void add (const number a,
const FullMatrix<number2> &A,
const number b,
const FullMatrix<number2> &B);
/**
* Multiple addition of scaled
* matrices, i.e. <tt>*this +=
* a*A + b*B + c*C</tt>.
*
* The matrices <tt>A</tt>,
* <tt>B</tt> and <tt>C</tt> may
* be a full matrix over an
* arbitrary underlying scalar
* type, as long as its data type
* is convertible to the data
* type of this matrix.
*/
template<typename number2>
void add (const number a,
const FullMatrix<number2> &A,
const number b,
const FullMatrix<number2> &B,
const number c,
const FullMatrix<number2> &C);
/**
* Add rectangular block.
*
* A rectangular block of the matrix
* <tt>src</tt> is added to
* <tt>this</tt>. The upper left corner
* of the block being copied is
* <tt>(src_offset_i,src_offset_j)</tt>.
* The upper left corner of the copied
* block is
* <tt>(dst_offset_i,dst_offset_j)</tt>.
* The size of the rectangular block
* being copied is the maximum size
* possible, determined either by the
* size of <tt>this</tt> or <tt>src</tt>
* and the given offsets.
*/
template<typename number2>
void add (const FullMatrix<number2> &src,
const number factor,
const unsigned int dst_offset_i = 0,
const unsigned int dst_offset_j = 0,
const unsigned int src_offset_i = 0,
const unsigned int src_offset_j = 0);
/**
* Weighted addition of the
* transpose of <tt>B</tt> to
* <tt>this</tt>.
*
* <i>A += s B<sup>T</sup></i>
*/
template<typename number2>
void Tadd (const number s,
const FullMatrix<number2> &B);
/**
* Add transpose of a rectangular block.
*
* A rectangular block of the
* matrix <tt>src</tt> is
* transposed and addedadded to
* <tt>this</tt>. The upper left
* corner of the block being
* copied is
* <tt>(src_offset_i,src_offset_j)</tt>
* in the coordinates of the
* <b>non</b>-transposed matrix.
* The upper left corner of the
* copied block is
* <tt>(dst_offset_i,dst_offset_j)</tt>.
* The size of the rectangular
* block being copied is the
* maximum size possible,
* determined either by the size
* of <tt>this</tt> or
* <tt>src</tt>.
*/
template<typename number2>
void Tadd (const FullMatrix<number2> &src,
const number factor,
const unsigned int dst_offset_i = 0,
const unsigned int dst_offset_j = 0,
const unsigned int src_offset_i = 0,
const unsigned int src_offset_j = 0);
/**
* <i>A(i,1...n) +=
* s*A(j,1...n)</i>. Simple
* addition of rows of this
*/
void add_row (const unsigned int i,
const number s,
const unsigned int j);
/**
* <i>A(i,1...n) += s*A(j,1...n)
* + t*A(k,1...n)</i>. Multiple
* addition of rows of this.
*/
void add_row (const unsigned int i,
const number s, const unsigned int j,
const number t, const unsigned int k);
/**
* <i>A(1...n,i) += s*A(1...n,j)</i>.
* Simple addition of columns of this.
*/
void add_col (const unsigned int i,
const number s,
const unsigned int j);
/**
* <i>A(1...n,i) += s*A(1...n,j)
* + t*A(1...n,k)</i>. Multiple
* addition of columns of this.
*/
void add_col (const unsigned int i,
const number s, const unsigned int j,
const number t, const unsigned int k);
/**
* Swap <i>A(i,1...n) <->
* A(j,1...n)</i>. Swap rows i
* and j of this
*/
void swap_row (const unsigned int i,
const unsigned int j);
/**
* Swap <i>A(1...n,i) <->
* A(1...n,j)</i>. Swap columns
* i and j of this
*/
void swap_col (const unsigned int i,
const unsigned int j);
/**
* Add constant to diagonal
* elements of this, i.e. add a
* multiple of the identity
* matrix.
*/
void diagadd (const number s);
/**
* Assignment <tt>*this =
* a*A</tt>.
*/
template<typename number2>
void equ (const number a,
const FullMatrix<number2> &A);
/**
* Assignment <tt>*this = a*A +
* b*B</tt>.
*/
template<typename number2>
void equ (const number a,
const FullMatrix<number2> &A,
const number b,
const FullMatrix<number2> &B);
/**
* Assignment <tt>*this = a*A +
* b*B + c*C</tt>.
*/
template<typename number2>
void equ (const number a,
const FullMatrix<number2> &A,
const number b,
const FullMatrix<number2> &B,
const number c,
const FullMatrix<number2> &C);
/**
* Symmetrize the matrix by
* forming the mean value between
* the existing matrix and its
* transpose, <i>A =
* 1/2(A+A<sup>T</sup>)</i>.
*
* Obviously the matrix must be
* quadratic for this operation.
*/
void symmetrize ();
/**
* A=Inverse(A). A must be a square matrix.
* Inversion of
* this matrix by Gauss-Jordan
* algorithm with partial
* pivoting. This process is
* well-behaved for positive
* definite matrices, but be
* aware of round-off errors in
* the indefinite case.
*
* In case deal.II was configured with
* LAPACK, the functions Xgetrf and
* Xgetri build an LU factorization and
* invert the matrix upon that
* factorization, providing best
* performance up to matrices with a
* few hundreds rows and columns.
*
* The numerical effort to invert
* an <tt>n x n</tt> matrix is of the
* order <tt>n**3</tt>.
*/
void gauss_jordan ();
/**
* Assign the inverse of the given matrix
* to <tt>*this</tt>. This function is
* hardcoded for quadratic matrices of
* dimension one to four. However, since
* the amount of code needed grows
* quickly, the method gauss_jordan() is
* invoked implicitly if the dimension is
* larger.
*/
template <typename number2>
void invert (const FullMatrix<number2> &M);
/**
* Assign the Cholesky decomposition
* of the given matrix to <tt>*this</tt>.
* The given matrix must be symmetric
* positive definite.
*
* ExcMatrixNotPositiveDefinite
* will be thrown in the case that the
* matrix is not positive definite.
*/
template <typename number2>
void cholesky (const FullMatrix<number2> &A);
/**
* <tt>*this(i,j)</tt> = $V(i) W(j)$
* where $V,W$
* are vectors of the same length.
*/
template <typename number2>
void outer_product (const Vector<number2> &V,
const Vector<number2> &W);
/**
* Assign the left_inverse of the given matrix
* to <tt>*this</tt>. The calculation being
* performed is <i>(A<sup>T</sup>*A)<sup>-1</sup>
* *A<sup>T</sup></i>.
*/
template <typename number2>
void left_invert (const FullMatrix<number2> &M);
/**
* Assign the right_inverse of the given matrix
* to <tt>*this</tt>. The calculation being
* performed is <i>A<sup>T</sup>*(A*A<sup>T</sup>)
* <sup>-1</sup></i>.
*/
template <typename number2>
void right_invert (const FullMatrix<number2> &M);
/**
* Fill matrix with elements
* extracted from a tensor,
* taking rows included between
* <tt>r_i</tt> and <tt>r_j</tt>
* and columns between
* <tt>c_i</tt> and
* <tt>c_j</tt>. The resulting
* matrix is then inserted in the
* destination matrix at position
* <tt>(dst_r, dst_c)</tt> Checks
* on the indices are made.
*/
template <int dim>
void
copy_from (Tensor<2,dim> &T,
const unsigned int src_r_i=0,
const unsigned int src_r_j=dim-1,
const unsigned int src_c_i=0,
const unsigned int src_c_j=dim-1,
const unsigned int dst_r=0,
const unsigned int dst_c=0);
/**
* Insert a submatrix (also
* rectangular) into a tensor,
* putting its upper left element
* at the specified position
* <tt>(dst_r, dst_c)</tt> and
* the other elements
* consequently. Default values
* are chosen so that no
* parameter needs to be specified
* if the size of the tensor and
* that of the matrix coincide.
*/
template <int dim>
void
copy_to(Tensor<2,dim> &T,
const unsigned int src_r_i=0,
const unsigned int src_r_j=dim-1,
const unsigned int src_c_i=0,
const unsigned int src_c_j=dim-1,
const unsigned int dst_r=0,
const unsigned int dst_c=0);
//@}
///@name Multiplications
//@{
/**
* Matrix-matrix-multiplication.
*
* The optional parameter
* <tt>adding</tt> determines, whether the
* result is stored in <tt>C</tt> or added
* to <tt>C</tt>.
*
* if (adding)
* <i>C += A*B</i>
*
* if (!adding)
* <i>C = A*B</i>
*
* Assumes that <tt>A</tt> and
* <tt>B</tt> have compatible sizes and
* that <tt>C</tt> already has the
* right size.
*
* This function uses the BLAS function
* Xgemm if the calling matrix has more
* than 15 rows and BLAS was detected
* during configuration. Using BLAS
* usually results in considerable
* performance gains.
*/
template<typename number2>
void mmult (FullMatrix<number2> &C,
const FullMatrix<number2> &B,
const bool adding=false) const;
/**
* Matrix-matrix-multiplication using
* transpose of <tt>this</tt>.
*
* The optional parameter
* <tt>adding</tt> determines, whether the
* result is stored in <tt>C</tt> or added
* to <tt>C</tt>.
*
* if (adding)
* <i>C += A<sup>T</sup>*B</i>
*
* if (!adding)
* <i>C = A<sup>T</sup>*B</i>
*
* Assumes that <tt>A</tt> and
* <tt>B</tt> have compatible
* sizes and that <tt>C</tt>
* already has the right size.
*
* This function uses the BLAS function
* Xgemm if the calling matrix has more
* than 15 columns and BLAS was
* detected during configuration. Using
* BLAS usually results in considerable
* performance gains.
*/
template<typename number2>
void Tmmult (FullMatrix<number2> &C,
const FullMatrix<number2> &B,
const bool adding=false) const;
/**
* Matrix-matrix-multiplication using
* transpose of <tt>B</tt>.
*
* The optional parameter
* <tt>adding</tt> determines, whether the
* result is stored in <tt>C</tt> or added
* to <tt>C</tt>.
*
* if (adding)
* <i>C += A*B<sup>T</sup></i>
*
* if (!adding)
* <i>C = A*B<sup>T</sup></i>
*
* Assumes that <tt>A</tt> and
* <tt>B</tt> have compatible sizes and
* that <tt>C</tt> already has the
* right size.
*
* This function uses the BLAS function
* Xgemm if the calling matrix has more
* than 15 rows and BLAS was detected
* during configuration. Using BLAS
* usually results in considerable
* performance gains.
*/
template<typename number2>
void mTmult (FullMatrix<number2> &C,
const FullMatrix<number2> &B,
const bool adding=false) const;
/**
* Matrix-matrix-multiplication using
* transpose of <tt>this</tt> and
* <tt>B</tt>.
*
* The optional parameter
* <tt>adding</tt> determines, whether the
* result is stored in <tt>C</tt> or added
* to <tt>C</tt>.
*
* if (adding)
* <i>C += A<sup>T</sup>*B<sup>T</sup></i>
*
* if (!adding)
* <i>C = A<sup>T</sup>*B<sup>T</sup></i>
*
* Assumes that <tt>A</tt> and
* <tt>B</tt> have compatible
* sizes and that <tt>C</tt>
* already has the right size.
*
* This function uses the BLAS function
* Xgemm if the calling matrix has more
* than 15 columns and BLAS was
* detected during configuration. Using
* BLAS usually results in considerable
* performance gains.
*/
template<typename number2>
void TmTmult (FullMatrix<number2> &C,
const FullMatrix<number2> &B,
const bool adding=false) const;
/**
* Matrix-vector-multiplication.
*
* The optional parameter
* <tt>adding</tt> determines, whether the
* result is stored in <tt>w</tt> or added
* to <tt>w</tt>.
*
* if (adding)
* <i>w += A*v</i>
*
* if (!adding)
* <i>w = A*v</i>
*
* Source and destination must
* not be the same vector.
*/
template<typename number2>
void vmult (Vector<number2> &w,
const Vector<number2> &v,
const bool adding=false) const;
/**
* Adding Matrix-vector-multiplication.
* <i>w += A*v</i>
*
* Source and destination must
* not be the same vector.
*/
template<typename number2>
void vmult_add (Vector<number2> &w,
const Vector<number2> &v) const;
/**
* Transpose
* matrix-vector-multiplication.
*
* The optional parameter
* <tt>adding</tt> determines, whether the
* result is stored in <tt>w</tt> or added
* to <tt>w</tt>.
*
* if (adding)
* <i>w += A<sup>T</sup>*v</i>
*
* if (!adding)
* <i>w = A<sup>T</sup>*v</i>
*
*
* Source and destination must
* not be the same vector.
*/
template<typename number2>
void Tvmult (Vector<number2> &w,
const Vector<number2> &v,
const bool adding=false) const;
/**
* Adding transpose
* matrix-vector-multiplication.
* <i>w += A<sup>T</sup>*v</i>
*
* Source and destination must
* not be the same vector.
*/
template<typename number2>
void Tvmult_add (Vector<number2> &w,
const Vector<number2> &v) const;
/**
* Apply the Jacobi
* preconditioner, which
* multiplies every element of
* the <tt>src</tt> vector by the
* inverse of the respective
* diagonal element and
* multiplies the result with the
* damping factor <tt>omega</tt>.
*/
template <typename somenumber>
void precondition_Jacobi (Vector<somenumber> &dst,
const Vector<somenumber> &src,
const number omega = 1.) const;
/**
* <i>dst=b-A*x</i>. Residual calculation,
* returns the <i>l<sub>2</sub></i>-norm
* |<i>dst</i>|.
*
* Source <i>x</i> and destination
* <i>dst</i> must not be the same
* vector.
*/
template<typename number2, typename number3>
number residual (Vector<number2> &dst,
const Vector<number2> &x,
const Vector<number3> &b) const;
/**
* Forward elimination of lower
* triangle. Inverts the lower
* triangle of a rectangular matrix
* for a given right hand side.
*
* If the matrix has more columns
* than rows, this function only
* operates on the left quadratic
* submatrix. If there are more
* rows, the upper quadratic part
* of the matrix is considered.
*
* @note It is safe to use the
* same object for @p dst and @p
* src.
*/
template<typename number2>
void forward (Vector<number2> &dst,
const Vector<number2> &src) const;
/**
* Backward elimination of upper
* triangle.
*
* See forward()
*
* @note It is safe to use the
* same object for @p dst and @p
* src.
*/
template<typename number2>
void backward (Vector<number2> &dst,
const Vector<number2> &src) const;
//@}
/** @addtogroup Exceptions
* @{ */
/**
* Exception
*/
DeclException0 (ExcEmptyMatrix);
/**
* Exception
*/
DeclException1 (ExcNotRegular,
number,
<< "The maximal pivot is " << arg1
<< ", which is below the threshold. The matrix may be singular.");
/**
* Exception
*/
DeclException3 (ExcInvalidDestination,
int, int, int,
<< "Target region not in matrix: size in this direction="
<< arg1 << ", size of new matrix=" << arg2
<< ", offset=" << arg3);
/**
* Exception
*/
DeclException0 (ExcSourceEqualsDestination);
/**
* Exception
*/
DeclException0 (ExcMatrixNotPositiveDefinite);
//@}
friend class Accessor;
};
/**@}*/
#ifndef DOXYGEN
/*-------------------------Inline functions -------------------------------*/
template <typename number>
inline
unsigned int
FullMatrix<number>::m() const
{
return this->n_rows();
}
template <typename number>
inline
unsigned int
FullMatrix<number>::n() const
{
return this->n_cols();
}
template <typename number>
FullMatrix<number> &
FullMatrix<number>::operator = (const number d)
{
Assert (d==number(0), ExcScalarAssignmentOnlyForZeroValue());
if (this->n_elements() != 0)
memset (this->val, 0, this->n_elements()*sizeof(number));
return *this;
}
template <typename number>
template <typename number2>
inline
void FullMatrix<number>::fill (const number2* src)
{
Table<2,number>::fill(src);
}
template <typename number>
template <class MATRIX>
void
FullMatrix<number>::copy_from (const MATRIX& M)
{
this->reinit (M.m(), M.n());
const typename MATRIX::const_iterator end = M.end();
for (typename MATRIX::const_iterator entry = M.begin();
entry != end; ++entry)
this->el(entry->row(), entry->column()) = entry->value();
}
template <typename number>
template<typename number2>
void
FullMatrix<number>::vmult_add (Vector<number2> &w,
const Vector<number2> &v) const
{
vmult(w, v, true);
}
template <typename number>
template<typename number2>
void
FullMatrix<number>::Tvmult_add (Vector<number2> &w,
const Vector<number2> &v) const
{
Tvmult(w, v, true);
}
//---------------------------------------------------------------------------
template <typename number>
inline
FullMatrix<number>::Accessor::
Accessor (const FullMatrix<number>* matrix,
const unsigned int r,
const unsigned int c)
:
matrix(matrix),
a_row(r),
a_col(c)
{}
template <typename number>
inline
unsigned int
FullMatrix<number>::Accessor::row() const
{
return a_row;
}
template <typename number>
inline
unsigned int
FullMatrix<number>::Accessor::column() const
{
return a_col;
}
template <typename number>
inline
number
FullMatrix<number>::Accessor::value() const
{
Assert (numbers::is_finite( matrix->el(a_row, a_col) ),
ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
return matrix->el(a_row, a_col);
}
template <typename number>
inline
FullMatrix<number>::const_iterator::
const_iterator(const FullMatrix<number> *matrix,
const unsigned int r,
const unsigned int c)
:
accessor(matrix, r, c)
{}
template <typename number>
inline
typename FullMatrix<number>::const_iterator &
FullMatrix<number>::const_iterator::operator++ ()
{
Assert (accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd());
++accessor.a_col;
if (accessor.a_col >= accessor.matrix->n())
{
accessor.a_col = 0;
accessor.a_row++;
}
return *this;
}
template <typename number>
inline
const typename FullMatrix<number>::Accessor &
FullMatrix<number>::const_iterator::operator* () const
{
return accessor;
}
template <typename number>
inline
const typename FullMatrix<number>::Accessor *
FullMatrix<number>::const_iterator::operator-> () const
{
return &accessor;
}
template <typename number>
inline
bool
FullMatrix<number>::const_iterator::
operator == (const const_iterator& other) const
{
return (accessor.row() == other.accessor.row() &&
accessor.column() == other.accessor.column());
}
template <typename number>
inline
bool
FullMatrix<number>::const_iterator::
operator != (const const_iterator& other) const
{
return ! (*this == other);
}
template <typename number>
inline
bool
FullMatrix<number>::const_iterator::
operator < (const const_iterator& other) const
{
return (accessor.row() < other.accessor.row() ||
(accessor.row() == other.accessor.row() &&
accessor.column() < other.accessor.column()));
}
template <typename number>
inline
bool
FullMatrix<number>::const_iterator::
operator > (const const_iterator& other) const
{
return (other < *this);
}
template <typename number>
inline
typename FullMatrix<number>::const_iterator
FullMatrix<number>::begin () const
{
return const_iterator(this, 0, 0);
}
template <typename number>
inline
typename FullMatrix<number>::const_iterator
FullMatrix<number>::end () const
{
return const_iterator(this, m(), 0);
}
template <typename number>
inline
typename FullMatrix<number>::const_iterator
FullMatrix<number>::begin (const unsigned int r) const
{
Assert (r<m(), ExcIndexRange(r,0,m()));
return const_iterator(this, r, 0);
}
template <typename number>
inline
typename FullMatrix<number>::const_iterator
FullMatrix<number>::end (const unsigned int r) const
{
Assert (r<m(), ExcIndexRange(r,0,m()));
return const_iterator(this, r+1, 0);
}
template <typename number>
template <class STREAM>
inline
void
FullMatrix<number>::print (STREAM &s,
const unsigned int w,
const unsigned int p) const
{
Assert (!this->empty(), ExcEmptyMatrix());
for (unsigned int i=0; i<this->m(); ++i)
{
for (unsigned int j=0; j<this->n(); ++j)
s << std::setw(w) << std::setprecision(p) << this->el(i,j);
s << std::endl;
}
}
#endif // DOXYGEN
DEAL_II_NAMESPACE_CLOSE
#endif
|