/usr/include/deal.II/lac/sparsity_pattern.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 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 | //---------------------------------------------------------------------------
// $Id: sparsity_pattern.h 20820 2010-03-14 00:52:27Z bangerth $
// Version: $Name$
//
// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 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__sparsity_pattern_h
#define __deal2__sparsity_pattern_h
#include <base/config.h>
#include <base/exceptions.h>
#include <base/subscriptor.h>
#include <vector>
#include <iostream>
DEAL_II_NAMESPACE_OPEN
class SparsityPattern;
class ChunkSparsityPattern;
template <typename number> class FullMatrix;
template <typename number> class SparseMatrix;
template <class VECTOR> class VectorSlice;
class CompressedSparsityPattern;
class CompressedSetSparsityPattern;
class CompressedSimpleSparsityPattern;
/*! @addtogroup Sparsity
*@{
*/
namespace internals
{
namespace SparsityPatternTools
{
/**
* Optimized replacement for
* <tt>std::lower_bound</tt> for
* searching within the range of
* column indices. Slashes
* execution time by
* approximately one half for the
* present application, partly
* because we have eliminated
* templates and the compiler
* seems to be able to optimize
* better, and partly because the
* binary search is replaced by a
* linear search for small loop
* lengths.
*/
static
inline
const unsigned int *
optimized_lower_bound (const unsigned int *first,
const unsigned int *last,
const unsigned int &val)
{
// this function is mostly copied
// over from the STL __lower_bound
// function, but with template args
// replaced by the actual data
// types needed here, and above all
// with a rolled out search on the
// last four elements
unsigned int len = last-first;
if (len==0)
return first;
while (true)
{
// if length equals 8 or less,
// then do a rolled out
// search. use a switch without
// breaks for that and roll-out
// the loop somehow
if (len < 8)
{
switch (len)
{
case 7:
if (*first >= val)
return first;
++first;
case 6:
if (*first >= val)
return first;
++first;
case 5:
if (*first >= val)
return first;
++first;
case 4:
if (*first >= val)
return first;
++first;
case 3:
if (*first >= val)
return first;
++first;
case 2:
if (*first >= val)
return first;
++first;
case 1:
if (*first >= val)
return first;
return first+1;
default:
// indices seem
// to not be
// sorted
// correctly!? or
// did len
// become==0
// somehow? that
// shouln't have
// happened
Assert (false, ExcInternalError());
}
}
const unsigned int half = len >> 1;
const unsigned int * const middle = first + half;
// if the value is larger than
// that pointed to by the
// middle pointer, then the
// insertion point must be
// right of it
if (*middle < val)
{
first = middle + 1;
len -= half + 1;
}
else
len = half;
}
}
/**
* Helper function to get the
* column index from a
* dereferenced iterator in the
* copy_from() function, if
* the inner iterator type points
* to plain unsigned integers.
*/
unsigned int
get_column_index_from_iterator (const unsigned int i);
/**
* Helper function to get the
* column index from a
* dereferenced iterator in the
* copy_from() function, if
* the inner iterator type points
* to pairs of unsigned integers
* and some other value.
*/
template <typename value>
unsigned int
get_column_index_from_iterator (const std::pair<unsigned int, value> &i);
/**
* Likewise, but sometimes needed
* for certain types of
* containers that make the first
* element of the pair constant
* (such as <tt>std::map</tt>).
*/
template <typename value>
unsigned int
get_column_index_from_iterator (const std::pair<const unsigned int, value> &i);
}
}
namespace SparsityPatternIterators
{
// forward declaration
class Iterator;
/**
* Accessor class for iterators into
* sparsity patterns. This class is also
* the base class for both const and
* non-const accessor classes into sparse
* matrices.
*
* Note that this class only allow read
* access to elements, providing their
* row and column number. It does not
* allow to modify the sparsity pattern
* itself.
*/
class Accessor
{
public:
/**
* Constructor.
*/
Accessor (const SparsityPattern *matrix,
const unsigned int row,
const unsigned int index);
/**
* Constructor. Construct the end
* accessor for the given sparsity
* pattern.
*/
Accessor (const SparsityPattern *matrix);
/**
* Row number of the element
* represented by this object. This
* function can only be called for
* entries for which is_valid_entry()
* is true.
*/
unsigned int row () const;
/**
* Index in row of the element
* represented by this object. This
* function can only be called for
* entries for which is_valid_entry()
* is true.
*/
unsigned int index () const;
/**
* Column number of the element
* represented by this object. This
* function can only be called for
* entries for which is_valid_entry() is
* true.
*/
unsigned int column () const;
/**
* Return whether the sparsity
* pattern entry pointed to by this
* iterator is valid or not. Note
* that after compressing the
* sparsity pattern, all entries are
* valid. However, before
* compression, the sparsity pattern
* allocated some memory to be used
* while still adding new nonzero
* entries; if you create iterators
* in this phase of the sparsity
* pattern's lifetime, you will
* iterate over elements that are not
* valid. If this is so, then this
* function will return false.
*/
inline bool is_valid_entry () const;
/**
* Comparison. True, if
* both iterators point to
* the same matrix
* position.
*/
bool operator == (const Accessor &) 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.
*
* This function is only valid if
* both iterators point into the same
* sparsity pattern.
*/
bool operator < (const Accessor &) const;
/** @addtogroup Exceptions
* @{ */
//@}
protected:
/**
* The sparsity pattern we operate on
* accessed.
*/
const SparsityPattern * sparsity_pattern;
/**
* Current row number.
*/
unsigned int a_row;
/**
* Current index in row.
*/
unsigned int a_index;
/**
* Move the accessor to the next
* nonzero entry in the matrix.
*/
void advance ();
/**
* Grant access to iterator class.
*/
friend class Iterator;
};
/**
* STL conforming iterator walking over
* the elements of a sparsity pattern.
*/
class Iterator
{
public:
/**
* Constructor. Create an iterator
* into the sparsity pattern @p sp for the
* given row and the index within it.
*/
Iterator (const SparsityPattern *sp,
const unsigned int row,
const unsigned int index);
/**
* Prefix increment.
*/
Iterator& operator++ ();
/**
* Postfix increment.
*/
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 Iterator&) const;
/**
* Inverse of <tt>==</tt>.
*/
bool operator != (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.
*
* This function is only valid if
* both iterators point into the same
* matrix.
*/
bool operator < (const Iterator&) const;
private:
/**
* Store an object of the
* accessor class.
*/
Accessor accessor;
};
}
/**
* Structure representing the sparsity pattern of a sparse matrix.
*
* This class is an example of the "static" type of @ref Sparsity.
*
* It uses the compressed row storage (CSR) format to store data.
*
* @author Wolfgang Bangerth, Guido Kanschat and others
*/
class SparsityPattern : public Subscriptor
{
public:
/**
* Typedef an iterator class that allows
* to walk over all nonzero elements of a
* sparsity pattern.
*/
typedef
SparsityPatternIterators::Iterator
const_iterator;
/**
* Typedef an iterator class that allows
* to walk over the nonzero elements of a
* row of a sparsity pattern.
*/
typedef
const unsigned int * row_iterator;
/**
* Typedef an iterator class that allows
* to walk over all nonzero elements of a
* sparsity pattern.
*
* Since the iterator does not allow to
* modify the sparsity pattern, this type
* is the same as that for @p
* const_iterator.
*/
typedef
SparsityPatternIterators::Iterator
iterator;
/**
* Define a value which is used
* to indicate that a certain
* value in the #colnums array
* is unused, i.e. does not
* represent a certain column
* number index.
*
* Indices with this invalid
* value are used to insert new
* entries to the sparsity
* pattern using the add() member
* function, and are removed when
* calling compress().
*
* You should not assume that the
* variable declared here has a
* certain value. The
* initialization is given here
* only to enable the compiler to
* perform some optimizations,
* but the actual value of the
* variable may change over time.
*/
static const unsigned int invalid_entry = numbers::invalid_unsigned_int;
/**
* Initialize the matrix empty,
* that is with no memory
* allocated. This is useful if
* you want such objects as
* member variables in other
* classes. You can make the
* structure usable by calling
* the reinit() function.
*/
SparsityPattern ();
/**
* Copy constructor. This
* constructor is only allowed to
* be called if the matrix
* structure to be copied is
* empty. This is so in order to
* prevent involuntary copies of
* objects for temporaries, which
* can use large amounts of
* computing time. However, copy
* constructors are needed if yo
* want to use the STL data types
* on classes like this, e.g. to
* write such statements like
* <tt>v.push_back
* (SparsityPattern());</tt>,
* with <tt>v</tt> a vector of
* SparsityPattern objects.
*
* Usually, it is sufficient to
* use the explicit keyword to
* disallow unwanted temporaries,
* but for the STL vectors, this
* does not work. Since copying a
* structure like this is not
* useful anyway because multiple
* matrices can use the same
* sparsity structure, copies are
* only allowed for empty
* objects, as described above.
*/
SparsityPattern (const SparsityPattern &);
/**
* Initialize a rectangular
* matrix.
*
* @arg m number of rows
* @arg n number of columns
* @arg max_per_row maximum
* number of nonzero entries per row
*
* @arg optimize_diagonal store
* diagonal entries first in row;
* see optimize_diagonal(). This
* takes effect for quadratic
* matrices only.
*/
SparsityPattern (const unsigned int m,
const unsigned int n,
const unsigned int max_per_row,
const bool optimize_diagonal = true);
/**
* Initialize a rectangular
* matrix.
*
* @arg m number of rows
* @arg n number of columns
*
* @arg row_lengths possible
* number of nonzero entries for
* each row. This vector must
* have one entry for each row.
*
* @arg optimize_diagonal store
* diagonal entries first in row;
* see optimize_diagonal(). This
* takes effect for quadratic
* matrices only.
*/
SparsityPattern (const unsigned int m,
const unsigned int n,
const std::vector<unsigned int>& row_lengths,
const bool optimize_diagonal = true);
/**
* Initialize a quadratic matrix
* of dimension <tt>n</tt> with
* at most <tt>max_per_row</tt>
* nonzero entries per row.
*
* This constructor automatically
* enables optimized storage of
* diagonal elements. To avoid
* this, use the constructor
* taking row and column numbers
* separately.
*/
SparsityPattern (const unsigned int n,
const unsigned int max_per_row);
/**
* Initialize a quadratic matrix.
*
* @arg m number of rows and columns
*
* @arg row_lengths possible
* number of nonzero entries for
* each row. This vector must
* have one entry for each row.
*
* @arg optimize_diagonal store
* diagonal entries first in row;
* see optimize_diagonal().
*/
SparsityPattern (const unsigned int m,
const std::vector<unsigned int>& row_lengths,
const bool optimize_diagonal = true);
/**
* Make a copy with extra off-diagonals.
*
* This constructs objects intended for
* the application of the ILU(n)-method
* or other incomplete decompositions.
* Therefore, additional to the original
* entry structure, space for
* <tt>extra_off_diagonals</tt>
* side-diagonals is provided on both
* sides of the main diagonal.
*
* <tt>max_per_row</tt> is the
* maximum number of nonzero
* elements per row which this
* structure is to hold. It is
* assumed that this number is
* sufficiently large to
* accomodate both the elements
* in <tt>original</tt> as well
* as the new off-diagonal
* elements created by this
* constructor. You will usually
* want to give the same number
* as you gave for
* <tt>original</tt> plus the
* number of side diagonals times
* two. You may however give a
* larger value if you wish to
* add further nonzero entries
* for the decomposition based on
* other criteria than their
* being on side-diagonals.
*
* This function requires that
* <tt>original</tt> refers to a
* quadratic matrix structure.
* It must be compressed. The
* matrix structure is not
* compressed after this function
* finishes.
*/
SparsityPattern (const SparsityPattern &original,
const unsigned int max_per_row,
const unsigned int extra_off_diagonals);
/**
* Destructor.
*/
~SparsityPattern ();
/**
* Copy operator. For this the
* same holds as for the copy
* constructor: it is declared,
* defined and fine to be called,
* but the latter only for empty
* objects.
*/
SparsityPattern & operator = (const SparsityPattern &);
/**
* Reallocate memory and set up data
* structures for a new matrix with
* <tt>m </tt>rows and <tt>n</tt> columns,
* with at most <tt>max_per_row</tt>
* nonzero entries per row.
*
* This function simply maps its
* operations to the other
* <tt>reinit</tt> function.
*/
void reinit (const unsigned int m,
const unsigned int n,
const unsigned int max_per_row,
const bool optimize_diagonal = true);
/**
* Reallocate memory for a matrix
* of size <tt>m x n</tt>. The
* number of entries for each row
* is taken from the array
* <tt>row_lengths</tt> which has to
* give this number of each row
* <tt>i=1...m</tt>.
*
* If <tt>m*n==0</tt> all memory is freed,
* resulting in a total reinitialization
* of the object. If it is nonzero, new
* memory is only allocated if the new
* size extends the old one. This is done
* to save time and to avoid fragmentation
* of the heap.
*
* If the number of rows equals
* the number of columns and the
* last parameter is true,
* diagonal elements are stored
* first in each row to allow
* optimized access in relaxation
* methods of SparseMatrix.
*/
void reinit (const unsigned int m,
const unsigned int n,
const std::vector<unsigned int> &row_lengths,
const bool optimize_diagonal = true);
/**
* Same as above, but with a
* VectorSlice argument instead.
*/
void reinit (const unsigned int m,
const unsigned int n,
const VectorSlice<const std::vector<unsigned int> > &row_lengths,
const bool optimize_diagonal = true);
/**
* This function compresses the sparsity
* structure that this object represents.
* It does so by eliminating unused
* entries and sorting the remaining ones
* to allow faster access by usage of
* binary search algorithms. A special
* sorting scheme is used for the
* diagonal entry of quadratic matrices,
* which is always the first entry of
* each row.
*
* The memory which is no more
* needed is released.
*
* SparseMatrix objects require the
* SparsityPattern objects they are
* initialized with to be compressed, to
* reduce memory requirements.
*/
void compress ();
/**
* STL-like iterator with the first entry
* of the matrix. The resulting iterator
* can be used to walk over all nonzero
* entries of the sparsity pattern.
*/
inline iterator begin () const;
/**
* Final iterator.
*/
inline iterator end () const;
/**
* STL-like iterator with the first entry
* of row <tt>r</tt>.
*
* Note that if the given row is empty,
* i.e. does not contain any nonzero
* entries, then the iterator returned by
* this function equals
* <tt>end(r)</tt>. Note also that the
* iterator may not be dereferencable in
* that case.
*/
inline iterator begin (const unsigned int r) const;
/**
* Final iterator of row <tt>r</tt>. It
* points to the first element past the
* end of line @p r, or past the end of
* the entire sparsity pattern.
*
* Note that the end iterator is not
* necessarily dereferencable. This is in
* particular the case if it is the end
* iterator for the last row of a matrix.
*/
inline iterator end (const unsigned int r) const;
/**
* STL-like iterator with the first entry
* of row <tt>r</tt>.
*
* Note that if the given row is empty,
* i.e. does not contain any nonzero
* entries, then the iterator returned by
* this function equals
* <tt>end(r)</tt>. Note also that the
* iterator may not be dereferencable in
* that case.
*/
inline row_iterator row_begin (const unsigned int r) const;
/**
* Final iterator of row <tt>r</tt>. It
* points to the first element past the
* end of line @p r, or past the end of
* the entire sparsity pattern.
*
* Note that the end iterator is not
* necessarily dereferencable. This is in
* particular the case if it is the end
* iterator for the last row of a matrix.
*/
inline row_iterator row_end (const unsigned int r) const;
/**
* This function can be used as a
* replacement for reinit(),
* subsequent calls to add() and
* a final call to close() if you
* know exactly in advance the
* entries that will form the
* matrix sparsity pattern.
*
* The first two parameters
* determine the size of the
* matrix. For the two last ones,
* note that a sparse matrix can
* be described by a sequence of
* rows, each of which is
* represented by a sequence of
* pairs of column indices and
* values. In the present
* context, the begin() and
* end() parameters designate
* iterators (of forward iterator
* type) into a container, one
* representing one row. The
* distance between begin()
* and end() should therefore
* be equal to
* n_rows(). These iterators
* may be iterators of
* <tt>std::vector</tt>,
* <tt>std::list</tt>, pointers into a
* C-style array, or any other
* iterator satisfying the
* requirements of a forward
* iterator. The objects pointed
* to by these iterators
* (i.e. what we get after
* applying <tt>operator*</tt> or
* <tt>operator-></tt> to one of these
* iterators) must be a container
* itself that provides functions
* <tt>begin</tt> and <tt>end</tt>
* designating a range of
* iterators that describe the
* contents of one
* line. Dereferencing these
* inner iterators must either
* yield a pair of an unsigned
* integer as column index and a
* value of arbitrary type (such
* a type would be used if we
* wanted to describe a sparse
* matrix with one such object),
* or simply an unsigned integer
* (of we only wanted to describe
* a sparsity pattern). The
* function is able to determine
* itself whether an unsigned
* integer or a pair is what we
* get after dereferencing the
* inner iterators, through some
* template magic.
*
* While the order of the outer
* iterators denotes the
* different rows of the matrix,
* the order of the inner
* iterator denoting the columns
* does not matter, as they are
* sorted internal to this
* function anyway.
*
* Since that all sounds very
* complicated, consider the
* following example code, which
* may be used to fill a sparsity
* pattern:
* @code
* std::vector<std::vector<unsigned int> > column_indices (n_rows);
* for (unsigned int row=0; row<n_rows; ++row)
* // generate necessary columns in this row
* fill_row (column_indices[row]);
*
* sparsity.copy_from (n_rows, n_cols,
* column_indices.begin(),
* column_indices.end());
* @endcode
*
* Note that this example works
* since the iterators
* dereferenced yield containers
* with functions <tt>begin</tt> and
* <tt>end</tt> (namely
* <tt>std::vector</tt>s), and the
* inner iterators dereferenced
* yield unsigned integers as
* column indices. Note that we
* could have replaced each of
* the two <tt>std::vector</tt>
* occurrences by <tt>std::list</tt>,
* and the inner one by
* <tt>std::set</tt> as well.
*
* Another example would be as
* follows, where we initialize a
* whole matrix, not only a
* sparsity pattern:
* @code
* std::vector<std::map<unsigned int,double> > entries (n_rows);
* for (unsigned int row=0; row<n_rows; ++row)
* // generate necessary pairs of columns
* // and corresponding values in this row
* fill_row (entries[row]);
*
* sparsity.copy_from (n_rows, n_cols,
* column_indices.begin(),
* column_indices.end());
* matrix.reinit (sparsity);
* matrix.copy_from (column_indices.begin(),
* column_indices.end());
* @endcode
*
* This example works because
* dereferencing iterators of the
* inner type yields a pair of
* unsigned integers and a value,
* the first of which we take as
* column index. As previously,
* the outer <tt>std::vector</tt>
* could be replaced by
* <tt>std::list</tt>, and the inner
* <tt>std::map<unsigned int,double></tt>
* could be replaced by
* <tt>std::vector<std::pair<unsigned int,double> ></tt>,
* or a list or set of such
* pairs, as they all return
* iterators that point to such
* pairs.
*/
template <typename ForwardIterator>
void copy_from (const unsigned int n_rows,
const unsigned int n_cols,
const ForwardIterator begin,
const ForwardIterator end,
const bool optimize_diagonal = true);
/**
* Copy data from an object of type
* CompressedSparsityPattern,
* CompressedSetSparsityPattern or
* CompressedSimpleSparsityPattern.
* Previous content of this object is
* lost, and the sparsity pattern is in
* compressed mode afterwards.
*/
template <typename CompressedSparsityType>
void copy_from (const CompressedSparsityType &csp,
const bool optimize_diagonal = true);
/**
* Take a full matrix and use its
* nonzero entries to generate a
* sparse matrix entry pattern
* for this object.
*
* Previous content of this
* object is lost, and the
* sparsity pattern is in
* compressed mode afterwards.
*/
template <typename number>
void copy_from (const FullMatrix<number> &matrix,
const bool optimize_diagonal = true);
/**
* Return whether the object is empty. It
* is empty if no memory is allocated,
* which is the same as that both
* dimensions are zero.
*/
bool empty () const;
/**
* Return the maximum number of entries per
* row. Before compression, this equals the
* number given to the constructor, while
* after compression, it equals the maximum
* number of entries actually allocated by
* the user.
*/
unsigned int max_entries_per_row () const;
/**
* Return the index of the matrix
* element with row number <tt>i</tt>
* and column number <tt>j</tt>. If
* the matrix element is not a
* nonzero one, return
* SparsityPattern::invalid_entry.
*
* This function is usually
* called by the
* SparseMatrix::operator()(). It
* may only be called for
* compressed sparsity patterns,
* since in this case searching
* whether the entry exists can
* be done quite fast with a
* binary sort algorithm because
* the column numbers are sorted.
*
* If <tt>m</tt> is the number of
* entries in <tt>row</tt>, then the
* complexity of this function is
* <i>log(m)</i> if the sparsity
* pattern is compressed.
*
* @deprecated Use
* SparseMatrix::const_iterator
*/
unsigned int operator() (const unsigned int i,
const unsigned int j) const;
/**
* This is the inverse operation
* to operator()(): given a
* global index, find out row and
* column of the matrix entry to
* which it belongs. The returned
* value is the pair composed of
* row and column index.
*
* This function may only be
* called if the sparsity pattern
* is closed. The global index
* must then be between zero and
* n_nonzero_elements().
*
* If <tt>N</tt> is the number of
* rows of this matrix, then the
* complexity of this function is
* <i>log(N)</i>.
*/
std::pair<unsigned int, unsigned int>
matrix_position (const unsigned int global_index) const;
/**
* Add a nonzero entry to the matrix.
* This function may only be called
* for non-compressed sparsity patterns.
*
* If the entry already exists, nothing
* bad happens.
*/
void add (const unsigned int i,
const unsigned int j);
/**
* Add several nonzero entries to the
* specified matrix row. This function
* may only be called for
* non-compressed sparsity patterns.
*
* If some of the entries already
* exist, nothing bad happens.
*/
template <typename ForwardIterator>
void add_entries (const unsigned int row,
ForwardIterator begin,
ForwardIterator end,
const bool indices_are_sorted = false);
/**
* Make the sparsity pattern
* symmetric by adding the
* sparsity pattern of the
* transpose object.
*
* This function throws an
* exception if the sparsity
* pattern does not represent a
* quadratic matrix.
*/
void symmetrize ();
/**
* Return number of rows of this
* matrix, which equals the dimension
* of the image space.
*/
inline unsigned int n_rows () const;
/**
* Return number of columns of this
* matrix, which equals the dimension
* of the range space.
*/
inline unsigned int n_cols () const;
/**
* Check if a value at a certain
* position may be non-zero.
*/
bool exists (const unsigned int i,
const unsigned int j) const;
/**
* Number of entries in a specific row.
*/
unsigned int row_length (const unsigned int row) const;
/**
* Access to column number field.
* Return the column number of
* the <tt>index</tt>th entry in
* <tt>row</tt>. Note that if
* diagonal elements are
* optimized, the first element
* in each row is the diagonal
* element,
* i.e. <tt>column_number(row,0)==row</tt>.
*
* If the sparsity pattern is
* already compressed, then
* (except for the diagonal
* element), the entries are
* sorted by columns,
* i.e. <tt>column_number(row,i)</tt>
* <tt><</tt> <tt>column_number(row,i+1)</tt>.
*/
unsigned int column_number (const unsigned int row,
const unsigned int index) const;
/**
* Compute the bandwidth of the matrix
* represented by this structure. The
* bandwidth is the maximum of $|i-j|$
* for which the index pair $(i,j)$
* represents a nonzero entry of the
* matrix. Consequently, the maximum
* bandwidth a $n\times m$ matrix can
* have is $\max\{n-1,m-1\}$.
*/
unsigned int bandwidth () const;
/**
* Return the number of nonzero elements of
* this matrix. Actually, it returns the
* number of entries in the sparsity
* pattern; if any of the entries should
* happen to be zero, it is counted
* anyway.
*
* This function may only be called if the
* matrix struct is compressed. It does not
* make too much sense otherwise anyway.
*/
std::size_t n_nonzero_elements () const;
/**
* Return whether the structure is
* compressed or not.
*/
bool is_compressed () const;
/**
* Determine whether the matrix
* uses special convention for
* quadratic matrices.
*
* A return value <tt>true</tt> means
* that diagonal elements are stored
* first in each row. A number of
* functions in this class and the
* library in general, for example
* relaxation methods like Jacobi() and
* SOR(), require this to make their
* operations more efficient, since they
* need to quickly access the diagonal
* elements and do not have to search for
* them if they are the first element of
* each row. A side effect of this scheme
* is that each row contains at least one
* element, even if the row is empty
* (i.e. the diagonal element exists, but
* has value zero).
*
* A return value <tt>false</tt> means
* that diagonal elements are stored
* anywhere in the row, or not at all. In
* particular, a row or even the whole
* matrix may be empty. This can be used
* if you have block matrices where the
* off-diagonal blocks are quadratic but
* are never used for operations like the
* ones mentioned above. In this case,
* some memory can be saved by not using
* the diagonal storage optimization.
*/
bool optimize_diagonal () const;
/**
* Return whether this object stores only
* those entries that have been added
* explicitly, or if the sparsity pattern
* contains elements that have been added
* through other means (implicitly) while
* building it. For the current class,
* the result is true iff optimize_diag
* in the constructor or reinit() calls
* has been set to false, or if the
* represented matrix is not square.
*
* This function mainly serves the
* purpose of describing the current
* class in cases where several kinds of
* sparsity patterns can be passed as
* template arguments.
*/
bool stores_only_added_elements () const;
/**
* @deprecated
*
* This function is deprecated. Use
* SparsityTools::partition instead.
*
* Use the METIS partitioner to generate
* a partitioning of the degrees of
* freedom represented by this sparsity
* pattern. In effect, we view this
* sparsity pattern as a graph of
* connections between various degrees of
* freedom, where each nonzero entry in
* the sparsity pattern corresponds to an
* edge between two nodes in the
* connection graph. The goal is then to
* decompose this graph into groups of
* nodes so that a minimal number of
* edges are cut by the boundaries
* between node groups. This partitioning
* is done by METIS. Note that METIS can
* only partition symmetric sparsity
* patterns, and that of course the
* sparsity pattern has to be square. We
* do not check for symmetry of the
* sparsity pattern, since this is an
* expensive operation, but rather leave
* this as the responsibility of caller
* of this function.
*
* After calling this function, the
* output array will have values between
* zero and @p n_partitions-1 for each
* node (i.e. row or column of the
* matrix).
*
* This function will generate an error
* if METIS is not installed unless
* @p n_partitions is one. I.e., you can
* write a program so that it runs in the
* single-processor single-partition case
* without METIS installed, and only
* requires METIS when multiple
* partitions are required.
*
* Note that the sparsity pattern itself
* is not changed by calling this
* function. However, you will likely use
* the information generated by calling
* this function to renumber degrees of
* freedom, after which you will of
* course have to regenerate the sparsity
* pattern.
*
* This function will rarely be called
* separately, since in finite element
* methods you will want to partition the
* mesh, not the matrix. This can be done
* by calling
* @p GridTools::partition_triangulation.
*/
void partition (const unsigned int n_partitions,
std::vector<unsigned int> &partition_indices) const;
/**
* Write the data of this object
* en bloc to a file. This is
* done in a binary mode, so the
* output is neither readable by
* humans nor (probably) by other
* computers using a different
* operating system of number
* format.
*
* The purpose of this function
* is that you can swap out
* matrices and sparsity pattern
* if you are short of memory,
* want to communicate between
* different programs, or allow
* objects to be persistent
* across different runs of the
* program.
*/
void block_write (std::ostream &out) const;
/**
* Read data that has previously
* been written by block_write()
* from a file. This is done
* using the inverse operations
* to the above function, so it
* is reasonably fast because the
* bitstream is not interpreted
* except for a few numbers up
* front.
*
* The object is resized on this
* operation, and all previous
* contents are lost.
*
* A primitive form of error
* checking is performed which
* will recognize the bluntest
* attempts to interpret some
* data as a vector stored
* bitwise to a file, but not
* more.
*/
void block_read (std::istream &in);
/**
* Print the sparsity of the
* matrix. The output consists of
* one line per row of the format
* <tt>[i,j1,j2,j3,...]</tt>. <i>i</i>
* is the row number and
* <i>jn</i> are the allocated
* columns in this row.
*/
void print (std::ostream &out) const;
/**
* Print the sparsity of the matrix
* in a format that <tt>gnuplot</tt> understands
* and which can be used to plot the
* sparsity pattern in a graphical
* way. The format consists of pairs
* <tt>i j</tt> of nonzero elements, each
* representing one entry of this
* matrix, one per line of the output
* file. Indices are counted from
* zero on, as usual. Since sparsity
* patterns are printed in the same
* way as matrices are displayed, we
* print the negative of the column
* index, which means that the
* <tt>(0,0)</tt> element is in the top left
* rather than in the bottom left
* corner.
*
* Print the sparsity pattern in
* gnuplot by setting the data style
* to dots or points and use the
* <tt>plot</tt> command.
*/
void print_gnuplot (std::ostream &out) const;
/**
* Determine an estimate for the
* memory consumption (in bytes)
* of this object. See
* MemoryConsumption.
*/
std::size_t memory_consumption () const;
/**
* This is kind of an expert mode. Get
* access to the rowstart array, but
* read-only.
*
* Use of this function is highly
* deprecated. Use @p row_length and
* @p column_number instead. Also, using
* iterators may get you most of the
* information you may want.
*
* Though the return value is declared
* <tt>const</tt>, you should be aware that it
* may change if you call any nonconstant
* function of objects which operate on
* it.
*
* You should use this interface very
* carefully and only if you are absolutely
* sure to know what you do. You should
* also note that the structure of these
* arrays may change over time.
* If you change the layout yourself, you
* should also rename this function to
* avoid programs relying on outdated
* information!
*/
inline const std::size_t * get_rowstart_indices () const;
/**
* @deprecated. Use @p row_length and
* @p column_number instead. Also, using
* iterators may get you most of the
* information you may want.
*
* This is kind of an expert mode: get
* access to the colnums array, but
* readonly.
*
* Though the return value is declared
* <tt>const</tt>, you should be aware that it
* may change if you call any nonconstant
* function of objects which operate on
* it.
*
* You should use this interface very
* carefully and only if you are absolutely
* sure to know what you do. You should
* also note that the structure of these
* arrays may change over time.
* If you change the layout yourself, you
* should also rename this function to
* avoid programs relying on outdated
* information!
*/
inline const unsigned int * get_column_numbers () const;
/** @addtogroup Exceptions
* @{ */
/**
* You tried to add an element to
* a row, but there was no space left.
*/
DeclException2 (ExcNotEnoughSpace,
int, int,
<< "Upon entering a new entry to row " << arg1
<< ": there was no free entry any more. " << std::endl
<< "(Maximum number of entries for this row: "
<< arg2 << "; maybe the matrix is already compressed?)");
/**
* The operation is only allowed
* after the SparsityPattern has
* been set up and compress() was
* called.
*/
DeclException0 (ExcNotCompressed);
/**
* This operation changes the
* structure of the
* SparsityPattern and is not
* possible after compress() has
* been called.
*/
DeclException0 (ExcMatrixIsCompressed);
/**
* Exception
*/
DeclException0 (ExcInvalidConstructorCall);
/**
* This exception is thrown if
* the matrix does not follow the
* convention of storing diagonal
* elements first in row. Refer
* to
* SparityPattern::optimize_diagonal()
* for more information.
*/
DeclException0 (ExcDiagonalNotOptimized);
/**
* Exception
*/
DeclException2 (ExcIteratorRange,
int, int,
<< "The iterators denote a range of " << arg1
<< " elements, but the given number of rows was " << arg2);
/**
* Exception
*/
DeclException1 (ExcInvalidNumberOfPartitions,
int,
<< "The number of partitions you gave is " << arg1
<< ", but must be greater than zero.");
//@}
private:
/**
* Maximum number of rows that can
* be stored in the #rowstart array.
* Since reallocation of that array
* only happens if the present one is
* too small, but never when the size
* of this matrix structure shrinks,
* #max_dim might be larger than
* #rows and in this case #rowstart
* has more elements than are used.
*/
unsigned int max_dim;
/**
* Number of rows that this sparsity
* structure shall represent.
*/
unsigned int rows;
/**
* Number of columns that this sparsity
* structure shall represent.
*/
unsigned int cols;
/**
* Size of the actually allocated array
* #colnums. Here, the same applies as
* for the #rowstart array, i.e. it
* may be larger than the actually used
* part of the array.
*/
std::size_t max_vec_len;
/**
* Maximum number of elements per
* row. This is set to the value
* given to the reinit() function
* (or to the constructor), or to
* the maximum row length
* computed from the vectors in
* case the more flexible
* constructors or reinit
* versions are called. Its value
* is more or less meaningsless
* after compress() has been
* called.
*/
unsigned int max_row_length;
/**
* Array which hold for each row
* which is the first element in
* #colnums belonging to that
* row. Note that the size of the
* array is one larger than the
* number of rows, because the
* last element is used for
* <tt>row</tt>=#rows, i.e. the
* row past the last used
* one. The value of
* #rowstart[#rows]} equals the
* index of the element past the
* end in #colnums; this way, we
* are able to write loops like
* <tt>for (i=rowstart[k];
* i<rowstart[k+1]; ++i)</tt>
* also for the last row.
*
* Note that the actual size of the
* allocated memory may be larger than
* the region that is used. The actual
* number of elements that was allocated
* is stored in #max_dim.
*/
std::size_t *rowstart;
/**
* Array of column numbers. In
* this array, we store for each
* non-zero element its column
* number. The column numbers for
* the elements in row <i>r</i>
* are stored within the index
* range
* #rowstart[<i>r</i>]...#rowstart[<i>r+1</i>]. Therefore
* to find out whether a given
* element (<i>r,c</i>) exists,
* we have to check whether the
* column number <i>c</i> exists
* in the abovementioned range
* within this array. If it
* exists, say at position
* <i>p</i> within this array,
* the value of the respective
* element in the sparse matrix
* will also be at position
* <i>p</i> of the values array
* of that class.
*
* At the beginning, all elements
* of this array are set to
* @p -1 indicating invalid
* (unused) column numbers
* (diagonal elements are preset
* if optimized storage is
* requested, though). Now, if
* nonzero elements are added,
* one column number in the row's
* respective range after the
* other is set to the column
* number of the added
* element. When compress is
* called, unused elements
* (indicated by column numbers
* @p -1) are eliminated by
* copying the column number of
* subsequent rows and the column
* numbers within each row (with
* possible exception of the
* diagonal element) are sorted,
* such that finding whether an
* element exists and determining
* its position can be done by a
* binary search.
*/
unsigned int *colnums;
/**
* Store whether the compress()
* function was called for this
* object.
*/
bool compressed;
/**
* Is special treatment of
* diagonals enabled?
*/
bool diagonal_optimized;
/**
* Make all sparse matrices
* friends of this class.
*/
template <typename number> friend class SparseMatrix;
template <typename number> friend class ChunkSparseMatrix;
friend class ChunkSparsityPattern;
};
/*@}*/
/*---------------------- Inline functions -----------------------------------*/
#ifndef DOXYGEN
namespace SparsityPatternIterators
{
inline
Accessor::
Accessor (const SparsityPattern *sparsity_pattern,
const unsigned int r,
const unsigned int i)
:
sparsity_pattern(sparsity_pattern),
a_row(r),
a_index(i)
{}
inline
Accessor::
Accessor (const SparsityPattern *sparsity_pattern)
:
sparsity_pattern(sparsity_pattern),
a_row(sparsity_pattern->n_rows()),
a_index(0)
{}
inline
bool
Accessor::is_valid_entry () const
{
return (sparsity_pattern
->get_column_numbers()[sparsity_pattern
->get_rowstart_indices()[a_row]+a_index]
!= SparsityPattern::invalid_entry);
}
inline
unsigned int
Accessor::row() const
{
Assert (is_valid_entry() == true, ExcInvalidIterator());
return a_row;
}
inline
unsigned int
Accessor::column() const
{
Assert (is_valid_entry() == true, ExcInvalidIterator());
return (sparsity_pattern
->get_column_numbers()[sparsity_pattern
->get_rowstart_indices()[a_row]+a_index]);
}
inline
unsigned int
Accessor::index() const
{
Assert (is_valid_entry() == true, ExcInvalidIterator());
return a_index;
}
inline
bool
Accessor::operator == (const Accessor& other) const
{
return (sparsity_pattern == other.sparsity_pattern &&
a_row == other.a_row &&
a_index == other.a_index);
}
inline
bool
Accessor::operator < (const Accessor& other) const
{
Assert (sparsity_pattern == other.sparsity_pattern,
ExcInternalError());
return (a_row < other.a_row ||
(a_row == other.a_row &&
a_index < other.a_index));
}
inline
void
Accessor::advance ()
{
Assert (a_row < sparsity_pattern->n_rows(), ExcIteratorPastEnd());
++a_index;
// if at end of line: cycle until we
// find a row with a nonzero number of
// entries
while (a_index >= sparsity_pattern->row_length(a_row))
{
a_index = 0;
++a_row;
// if we happened to find the end
// of the matrix, then stop here
if (a_row == sparsity_pattern->n_rows())
break;
}
}
inline
Iterator::Iterator (const SparsityPattern *sparsity_pattern,
const unsigned int r,
const unsigned int i)
:
accessor(sparsity_pattern, r, i)
{}
inline
Iterator &
Iterator::operator++ ()
{
accessor.advance ();
return *this;
}
inline
Iterator
Iterator::operator++ (int)
{
const Iterator iter = *this;
accessor.advance ();
return iter;
}
inline
const Accessor &
Iterator::operator* () const
{
return accessor;
}
inline
const Accessor *
Iterator::operator-> () const
{
return &accessor;
}
inline
bool
Iterator::operator == (const Iterator& other) const
{
return (accessor == other.accessor);
}
inline
bool
Iterator::operator != (const Iterator& other) const
{
return ! (*this == other);
}
inline
bool
Iterator::operator < (const Iterator& other) const
{
return accessor < other.accessor;
}
}
inline
SparsityPattern::iterator
SparsityPattern::begin () const
{
// search for the first line with a nonzero
// number of entries
for (unsigned int r=0; r<n_rows(); ++r)
if (row_length(r) > 0)
return iterator(this, r, 0);
// alright, this matrix is completely
// empty. that's strange but ok. simply
// return the end() iterator
return end();
}
inline
SparsityPattern::iterator
SparsityPattern::end () const
{
return iterator(this, n_rows(), 0);
}
inline
SparsityPattern::iterator
SparsityPattern::begin (const unsigned int r) const
{
Assert (r<n_rows(), ExcIndexRange(r,0,n_rows()));
if (row_length(r) > 0)
return iterator(this, r, 0);
else
return end (r);
}
inline
SparsityPattern::iterator
SparsityPattern::end (const unsigned int r) const
{
Assert (r<n_rows(), ExcIndexRange(r,0,n_rows()));
// place the iterator on the first entry
// past this line, or at the end of the
// matrix
for (unsigned int i=r+1; i<n_rows(); ++i)
if (row_length(i) > 0)
return iterator(this, i, 0);
// if there is no such line, then take the
// end iterator of the matrix
return end();
}
inline
SparsityPattern::row_iterator
SparsityPattern::row_begin (const unsigned int r) const
{
Assert (r<n_rows(), ExcIndexRange(r,0,n_rows()));
return &colnums[rowstart[r]];
}
inline
SparsityPattern::row_iterator
SparsityPattern::row_end (const unsigned int r) const
{
Assert (r<n_rows(), ExcIndexRange(r,0,n_rows()));
return &colnums[rowstart[r+1]];
}
inline
unsigned int
SparsityPattern::n_rows () const
{
return rows;
}
inline
unsigned int
SparsityPattern::n_cols () const
{
return cols;
}
inline
bool
SparsityPattern::is_compressed () const
{
return compressed;
}
inline
bool
SparsityPattern::optimize_diagonal () const
{
return diagonal_optimized;
}
inline
bool
SparsityPattern::stores_only_added_elements () const
{
if ((diagonal_optimized == true)
&&
(n_cols() == n_rows()))
return false;
else
return true;
}
inline
const std::size_t *
SparsityPattern::get_rowstart_indices () const
{
return rowstart;
}
inline
const unsigned int *
SparsityPattern::get_column_numbers () const
{
return colnums;
}
inline
unsigned int
SparsityPattern::row_length (const unsigned int row) const
{
Assert(row<rows, ExcIndexRange(row,0,rows));
return rowstart[row+1]-rowstart[row];
}
inline
unsigned int
SparsityPattern::column_number (const unsigned int row,
const unsigned int index) const
{
Assert(row<rows, ExcIndexRange(row,0,rows));
Assert(index<row_length(row), ExcIndexRange(index,0,row_length(row)));
return colnums[rowstart[row]+index];
}
inline
std::size_t
SparsityPattern::n_nonzero_elements () const
{
Assert ((rowstart!=0) && (colnums!=0), ExcEmptyObject());
Assert (compressed, ExcNotCompressed());
return rowstart[rows]-rowstart[0];
}
namespace internal
{
namespace SparsityPatternTools
{
inline
unsigned int
get_column_index_from_iterator (const unsigned int i)
{
return i;
}
template <typename value>
inline
unsigned int
get_column_index_from_iterator (const std::pair<unsigned int, value> &i)
{
return i.first;
}
template <typename value>
inline
unsigned int
get_column_index_from_iterator (const std::pair<const unsigned int, value> &i)
{
return i.first;
}
}
}
template <typename ForwardIterator>
void
SparsityPattern::copy_from (const unsigned int n_rows,
const unsigned int n_cols,
const ForwardIterator begin,
const ForwardIterator end,
const bool optimize_diag)
{
Assert (static_cast<unsigned int>(std::distance (begin, end)) == n_rows,
ExcIteratorRange (std::distance (begin, end), n_rows));
// first determine row lengths for
// each row. if the matrix is
// quadratic, then we might have to
// add an additional entry for the
// diagonal, if that is not yet
// present. as we have to call
// compress anyway later on, don't
// bother to check whether that
// diagonal entry is in a certain
// row or not
const bool is_square = optimize_diag && (n_rows == n_cols);
std::vector<unsigned int> row_lengths;
row_lengths.reserve(n_rows);
for (ForwardIterator i=begin; i!=end; ++i)
row_lengths.push_back (std::distance (i->begin(), i->end())
+
(is_square ? 1 : 0));
reinit (n_rows, n_cols, row_lengths, is_square);
// now enter all the elements into
// the matrix. note that if the
// matrix is quadratic, then we
// already have the diagonal
// element preallocated
//
// for use in the inner loop, we
// define a typedef to the type of
// the inner iterators
unsigned int row = 0;
typedef typename std::iterator_traits<ForwardIterator>::value_type::const_iterator inner_iterator;
for (ForwardIterator i=begin; i!=end; ++i, ++row)
{
unsigned int *cols = &colnums[rowstart[row]] + (is_square ? 1 : 0);
const inner_iterator end_of_row = i->end();
for (inner_iterator j=i->begin(); j!=end_of_row; ++j)
{
const unsigned int col
= internal::SparsityPatternTools::get_column_index_from_iterator(*j);
Assert (col < n_cols, ExcIndexRange(col,0,n_cols));
if ((col!=row) || !is_square)
*cols++ = col;
};
};
// finally compress
// everything. this also sorts the
// entries within each row
compress ();
}
#endif // DOXYGEN
DEAL_II_NAMESPACE_CLOSE
#endif
|