CompressedSetSparsityPattern Class Reference
[Sparsity patterns]

Inheritance diagram for CompressedSetSparsityPattern:

Inheritance graph
[legend]

List of all members.

Classes

struct  Line

Public Types

typedef std::set< unsigned int >
::const_iterator 
row_iterator

Public Member Functions

 CompressedSetSparsityPattern ()
 CompressedSetSparsityPattern (const CompressedSetSparsityPattern &)
 CompressedSetSparsityPattern (const unsigned int m, const unsigned int n)
 CompressedSetSparsityPattern (const unsigned int n)
CompressedSetSparsityPatternoperator= (const CompressedSetSparsityPattern &)
void reinit (const unsigned int m, const unsigned int n)
void compress ()
bool empty () const
unsigned int max_entries_per_row () const
void add (const unsigned int i, const unsigned int j)
template<typename ForwardIterator >
void add_entries (const unsigned int row, ForwardIterator begin, ForwardIterator end, const bool indices_are_sorted=false)
bool exists (const unsigned int i, const unsigned int j) const
void symmetrize ()
void print (std::ostream &out) const
void print_gnuplot (std::ostream &out) const
unsigned int n_rows () const
unsigned int n_cols () const
unsigned int row_length (const unsigned int row) const
row_iterator row_begin (const unsigned int row) const
row_iterator row_end (const unsigned int row) const
unsigned int bandwidth () const
unsigned int n_nonzero_elements () const

Static Public Member Functions

static bool stores_only_added_elements ()

Private Attributes

unsigned int rows
unsigned int cols
std::vector< Linelines


Detailed Description

This class acts as an intermediate form of the SparsityPattern class. From the interface it mostly represents a SparsityPattern object that is kept compressed at all times. However, since the final sparsity pattern is not known while constructing it, keeping the pattern compressed at all times can only be achieved at the expense of either increased memory or run time consumption upon use. The main purpose of this class is to avoid some memory bottlenecks, so we chose to implement it memory conservative, but the chosen data format is too unsuited to be used for actual matrices. It is therefore necessary to first copy the data of this object over to an object of type SparsityPattern before using it in actual matrices.

Another viewpoint is that this class does not need up front allocation of a certain amount of memory, but grows as necessary. An extensive description of sparsity patterns can be found in the documentation of the Sparsity patterns module.

This class is an example of the "dynamic" type of Sparsity patterns. It is discussed in the step-27 and step-22 tutorial programs.

Interface

Since this class is intended as an intermediate replacement of the SparsityPattern class, it has mostly the same interface, with small changes where necessary. In particular, the add() function, and the functions inquiring properties of the sparsity pattern are the same.

Usage

Use this class as follows:

 * CompressedSetSparsityPattern compressed_pattern (dof_handler.n_dofs());
 * DoFTools::make_sparsity_pattern (dof_handler,
 *                                  compressed_pattern);
 * constraints.condense (compressed_pattern);
 *
 * SparsityPattern sp;
 * sp.copy_from (compressed_pattern);
 * 

See also step-11 and step-18 for usage patterns of the related CompressedSparsityPattern class, and step-27 of the current class.

Notes

There are several, exchangeable variations of this class, see Sparsity patterns, section '"Dynamic" or "compressed" sparsity patterns' for more information.

This class is a variation of the CompressedSparsityPattern class. Instead of using sorted vectors together with a caching algorithm for storing the column indices of nonzero entries, the std::set container is used. This solution might not be the fastest in all situations, but seems to work much better than the CompressedSparsityPattern in the context of hp-adaptivity (see for example step-27), or generally when there are many nonzero entries in each row of a matrix (see step-22). On the other hand, a benchmark where nonzero entries were randomly inserted into the sparsity pattern revealed that this class is slower by a factor 4-6 in this situation. Hence, currently the suggestion is to carefully analyze which of the CompressedSparsityPattern classes works best in a certain setting. An algorithm which performs equally well in all situations still has to be found.

Author:
Oliver Kayser-Herold, 2007

Member Typedef Documentation

typedef std::set<unsigned int>::const_iterator CompressedSetSparsityPattern::row_iterator

An iterator that can be used to iterate over the elements of a single row. The result of dereferencing such an iterator is a column index.


Constructor & Destructor Documentation

CompressedSetSparsityPattern::CompressedSetSparsityPattern (  ) 

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.

CompressedSetSparsityPattern::CompressedSetSparsityPattern ( const CompressedSetSparsityPattern  ) 

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 v.push_back (CompressedSetSparsityPattern());, with v a vector of CompressedSetSparsityPattern objects.

CompressedSetSparsityPattern::CompressedSetSparsityPattern ( const unsigned int  m,
const unsigned int  n 
)

Initialize a rectangular matrix with m rows and n columns.

CompressedSetSparsityPattern::CompressedSetSparsityPattern ( const unsigned int  n  ) 

Initialize a square matrix of dimension n.


Member Function Documentation

CompressedSetSparsityPattern& CompressedSetSparsityPattern::operator= ( const CompressedSetSparsityPattern  ) 

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.

void CompressedSetSparsityPattern::reinit ( const unsigned int  m,
const unsigned int  n 
)

Reallocate memory and set up data structures for a new matrix with m rows and n columns, with at most max_entries_per_row() nonzero entries per row.

void CompressedSetSparsityPattern::compress (  ) 

Since this object is kept compressed at all times anway, this function does nothing, but is declared to make the interface of this class as much alike as that of the SparsityPattern class.

bool CompressedSetSparsityPattern::empty (  )  const

Return whether the object is empty. It is empty if no memory is allocated, which is the same as that both dimensions are zero.

unsigned int CompressedSetSparsityPattern::max_entries_per_row (  )  const

Return the maximum number of entries per row. Note that this number may change as entries are added.

void CompressedSetSparsityPattern::add ( const unsigned int  i,
const unsigned int  j 
) [inline]

Add a nonzero entry to the matrix. If the entry already exists, nothing bad happens.

References Assert, cols, lines, and rows.

template<typename ForwardIterator >
void CompressedSetSparsityPattern::add_entries ( const unsigned int  row,
ForwardIterator  begin,
ForwardIterator  end,
const bool  indices_are_sorted = false 
) [inline]

Add several nonzero entries to the specified row of the matrix. If the entries already exist, nothing bad happens.

References Assert, lines, and rows.

bool CompressedSetSparsityPattern::exists ( const unsigned int  i,
const unsigned int  j 
) const

Check if a value at a certain position may be non-zero.

void CompressedSetSparsityPattern::symmetrize (  ) 

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 square matrix.

void CompressedSetSparsityPattern::print ( std::ostream &  out  )  const

Print the sparsity of the matrix. The output consists of one line per row of the format [i,j1,j2,j3,...]. i is the row number and jn are the allocated columns in this row.

void CompressedSetSparsityPattern::print_gnuplot ( std::ostream &  out  )  const

Print the sparsity of the matrix in a format that gnuplot understands and which can be used to plot the sparsity pattern in a graphical way. The format consists of pairs i j 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 (0,0) 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 plot command.

unsigned int CompressedSetSparsityPattern::n_rows (  )  const [inline]

Return number of rows of this matrix, which equals the dimension of the image space.

References rows.

Referenced by row_length().

unsigned int CompressedSetSparsityPattern::n_cols (  )  const [inline]

Return number of columns of this matrix, which equals the dimension of the range space.

References cols.

unsigned int CompressedSetSparsityPattern::row_length ( const unsigned int  row  )  const [inline]

Number of entries in a specific row.

References Assert, lines, and n_rows().

CompressedSetSparsityPattern::row_iterator CompressedSetSparsityPattern::row_begin ( const unsigned int  row  )  const [inline]

Return an iterator that can loop over all entries in the given row. Dereferencing the iterator yields a column index.

References lines.

CompressedSetSparsityPattern::row_iterator CompressedSetSparsityPattern::row_end ( const unsigned int  row  )  const [inline]

End iterator for the given row.

References lines.

unsigned int CompressedSetSparsityPattern::bandwidth (  )  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.

unsigned int CompressedSetSparsityPattern::n_nonzero_elements (  )  const

Return the number of nonzero elements allocated through this sparsity pattern.

bool CompressedSetSparsityPattern::stores_only_added_elements (  )  [inline, static]

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 always true.

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.


Member Data Documentation

Number of rows that this sparsity structure shall represent.

Referenced by add(), add_entries(), and n_rows().

Number of columns that this sparsity structure shall represent.

Referenced by add(), and n_cols().

std::vector<Line> CompressedSetSparsityPattern::lines [private]

Actual data: store for each row the set of nonzero entries.

Referenced by add(), add_entries(), row_begin(), row_end(), and row_length().


The documentation for this class was generated from the following file:

deal.II documentation generated on Sat Aug 15 16:51:44 2009 by doxygen 1.5.9