Skip to content

Commit

Permalink
Added separator to Matrix::print_formatted().
Browse files Browse the repository at this point in the history
  • Loading branch information
marcfehling committed Oct 13, 2023
1 parent 0192f52 commit 7e67417
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 72 deletions.
23 changes: 13 additions & 10 deletions include/deal.II/lac/block_sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,23 +320,25 @@ class BlockSparseMatrix : public BlockMatrixBase<SparseMatrix<number>>
*/
/** @{ */
/**
* Print the matrix in the usual format, i.e. as a matrix and not as a list
* Print the matrix in the usual format, i.e., as a matrix and not as a list
* of nonzero elements. For better readability, elements not in the matrix
* are displayed as empty space, while matrix elements which are explicitly
* set to zero are displayed as such.
*
* The parameters allow for a flexible setting of the output format:
* <tt>precision</tt> and <tt>scientific</tt> are used to determine the
* number format, where <tt>scientific = false</tt> means fixed point
* notation. 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.
* @p precision and @p scientific are used to determine the number format,
* where <code>scientific = false</code> means fixed point notation. A zero
* entry for @p width makes the function compute a width, but it may be
* changed to a positive value, if output is crude.
*
* Additionally, a character for an empty value may be specified.
* Additionally, a character for an empty value may be specified in
* @p zero_string, and a character to separate row entries can be set in
* @p separator.
*
* Finally, the whole matrix can be multiplied with a common denominator to
* produce more readable output, even integers.
* Finally, the whole matrix can be multiplied with a common @p denominator
* to produce more readable output, even integers.
*
* @attention This function may produce <b>large</b> amounts of output if
* @attention This function may produce @em large amounts of output if
* applied to a large matrix!
*/
void
Expand All @@ -345,7 +347,8 @@ class BlockSparseMatrix : public BlockMatrixBase<SparseMatrix<number>>
const bool scientific = true,
const unsigned int width = 0,
const char *zero_string = " ",
const double denominator = 1.) const;
const double denominator = 1.,
const char *separator = " ") const;
/** @} */
/**
* @addtogroup Exceptions
Expand Down
12 changes: 9 additions & 3 deletions include/deal.II/lac/block_sparse_matrix.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,20 @@ BlockSparseMatrix<number>::print_formatted(std::ostream &out,
const bool scientific,
const unsigned int width,
const char *zero_string,
const double denominator) const
const double denominator,
const char *separator) const
{
for (size_type r = 0; r < this->n_block_rows(); ++r)
for (size_type c = 0; c < this->n_block_cols(); ++c)
{
out << "Component (" << r << ',' << c << ')' << std::endl;
this->block(r, c).print_formatted(
out, precision, scientific, width, zero_string, denominator);
this->block(r, c).print_formatted(out,
precision,
scientific,
width,
zero_string,
denominator,
separator);
}
}

Expand Down
23 changes: 13 additions & 10 deletions include/deal.II/lac/chunk_sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -1285,23 +1285,25 @@ class ChunkSparseMatrix : public virtual Subscriptor
print(std::ostream &out) const;

/**
* Print the matrix in the usual format, i.e. as a matrix and not as a list
* Print the matrix in the usual format, i.e., as a matrix and not as a list
* of nonzero elements. For better readability, elements not in the matrix
* are displayed as empty space, while matrix elements which are explicitly
* set to zero are displayed as such.
*
* The parameters allow for a flexible setting of the output format:
* <tt>precision</tt> and <tt>scientific</tt> are used to determine the
* number format, where <tt>scientific = false</tt> means fixed point
* notation. 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.
* @p precision and @p scientific are used to determine the number format,
* where <code>scientific = false</code> means fixed point notation. A zero
* entry for @p width makes the function compute a width, but it may be
* changed to a positive value, if output is crude.
*
* Additionally, a character for an empty value may be specified.
* Additionally, a character for an empty value may be specified in
* @p zero_string, and a character to separate row entries can be set in
* @p separator.
*
* Finally, the whole matrix can be multiplied with a common denominator to
* produce more readable output, even integers.
* Finally, the whole matrix can be multiplied with a common @p denominator
* to produce more readable output, even integers.
*
* @attention This function may produce <b>large</b> amounts of output if
* @attention This function may produce @em large amounts of output if
* applied to a large matrix!
*/
void
Expand All @@ -1310,7 +1312,8 @@ class ChunkSparseMatrix : public virtual Subscriptor
const bool scientific = true,
const unsigned int width = 0,
const char *zero_string = " ",
const double denominator = 1.) const;
const double denominator = 1.,
const char *separator = " ") const;

/**
* Print the actual pattern of the matrix. For each entry with an absolute
Expand Down
7 changes: 4 additions & 3 deletions include/deal.II/lac/chunk_sparse_matrix.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,8 @@ ChunkSparseMatrix<number>::print_formatted(std::ostream &out,
const bool scientific,
const unsigned int width_,
const char *zero_string,
const double denominator) const
const double denominator,
const char *separator) const
{
AssertThrow(out.fail() == false, ExcIO());

Expand Down Expand Up @@ -1505,9 +1506,9 @@ ChunkSparseMatrix<number>::print_formatted(std::ostream &out,
for (size_type j = 0; j < n(); ++j)
if (cols->sparsity_pattern(i, j) != SparsityPattern::invalid_entry)
out << std::setw(width)
<< val[cols->sparsity_pattern(i, j)] * denominator << ' ';
<< val[cols->sparsity_pattern(i, j)] * denominator << separator;
else
out << std::setw(width) << zero_string << ' ';
out << std::setw(width) << zero_string << separator;
out << std::endl;
};
AssertThrow(out.fail() == false, ExcIO());
Expand Down
21 changes: 12 additions & 9 deletions include/deal.II/lac/full_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,22 +498,24 @@ class FullMatrix : public Table<2, number>
*
* The parameters allow for a flexible setting of the output format:
*
* @arg <tt>precision</tt> denotes the number of trailing digits.
* @param precision 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.
* @param scientific is used to determine the number format, where
* <code>scientific = false</code> 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
* @param width denotes the with of each column. A zero entry for
* @p width 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.
* @param zero_string specifies a string printed for zero entries.
*
* @arg <tt>denominator</tt> Multiply the whole matrix by this common
* @param denominator Multiply the whole matrix by this common
* denominator to get nicer numbers.
*
* @arg <tt>threshold</tt>: all entries with absolute value smaller than
* @param threshold all entries with absolute value smaller than
* this are considered zero.
*
* @param separator specifies a string printed to separate row entries.
*/
void
print_formatted(std::ostream &out,
Expand All @@ -522,7 +524,8 @@ class FullMatrix : public Table<2, number>
const unsigned int width = 0,
const char *zero_string = " ",
const double denominator = 1.,
const double threshold = 0.) const;
const double threshold = 0.,
const char *separator = " ") const;

/**
* Determine an estimate for the memory consumption (in bytes) of this
Expand Down
10 changes: 6 additions & 4 deletions include/deal.II/lac/full_matrix.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,8 @@ FullMatrix<number>::print_formatted(std::ostream &out,
const unsigned int width_,
const char *zero_string,
const double denominator,
const double threshold) const
const double threshold,
const char *separator) const
{
unsigned int width = width_;

Expand Down Expand Up @@ -1688,11 +1689,12 @@ FullMatrix<number>::print_formatted(std::ostream &out,
// we might have complex numbers, so use abs also to check for nan
// since there is no isnan on complex numbers
if (numbers::is_nan(std::abs((*this)(i, j))))
out << std::setw(width) << (*this)(i, j) << ' ';
out << std::setw(width) << (*this)(i, j) << separator;
else if (std::abs((*this)(i, j)) > threshold)
out << std::setw(width) << (*this)(i, j) * number(denominator) << ' ';
out << std::setw(width) << (*this)(i, j) * number(denominator)
<< separator;
else
out << std::setw(width) << zero_string << ' ';
out << std::setw(width) << zero_string << separator;
out << std::endl;
};

Expand Down
5 changes: 4 additions & 1 deletion include/deal.II/lac/lapack_full_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ class LAPACKFullMatrix : public TransposeTable<number>
* @param threshold all entries with absolute value smaller than
* this are considered zero.
*
* @param separator specifies a string printed to separate row entries.
*
* @note The entries stored resemble a matrix only if the state is either
* LAPACKSupport::matrix or LAPACK::inverse_matrix. Otherwise, calling this
* function is not allowed.
Expand All @@ -913,7 +915,8 @@ class LAPACKFullMatrix : public TransposeTable<number>
const unsigned int width = 0,
const char *zero_string = " ",
const double denominator = 1.,
const double threshold = 0.) const;
const double threshold = 0.,
const char *separator = " ") const;

private:
/**
Expand Down
23 changes: 13 additions & 10 deletions include/deal.II/lac/sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -1584,23 +1584,25 @@ class SparseMatrix : public virtual Subscriptor
const bool diagonal_first = true) const;

/**
* Print the matrix in the usual format, i.e. as a matrix and not as a list
* Print the matrix in the usual format, i.e., as a matrix and not as a list
* of nonzero elements. For better readability, elements not in the matrix
* are displayed as empty space, while matrix elements which are explicitly
* set to zero are displayed as such.
*
* The parameters allow for a flexible setting of the output format:
* <tt>precision</tt> and <tt>scientific</tt> are used to determine the
* number format, where <tt>scientific = false</tt> means fixed point
* notation. 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.
* @p precision and @p scientific are used to determine the number format,
* where <code>scientific = false</code> means fixed point notation. A zero
* entry for @p width makes the function compute a width, but it may be
* changed to a positive value, if output is crude.
*
* Additionally, a character for an empty value may be specified.
* Additionally, a character for an empty value may be specified in
* @p zero_string, and a character to separate row entries can be set in
* @p separator.
*
* Finally, the whole matrix can be multiplied with a common denominator to
* produce more readable output, even integers.
* Finally, the whole matrix can be multiplied with a common @p denominator
* to produce more readable output, even integers.
*
* @attention This function may produce <b>large</b> amounts of output if
* @attention This function may produce @em large amounts of output if
* applied to a large matrix!
*/
void
Expand All @@ -1609,7 +1611,8 @@ class SparseMatrix : public virtual Subscriptor
const bool scientific = true,
const unsigned int width = 0,
const char *zero_string = " ",
const double denominator = 1.) const;
const double denominator = 1.,
const char *separator = " ") const;

/**
* Print the actual pattern of the matrix. For each entry with an absolute
Expand Down
7 changes: 4 additions & 3 deletions include/deal.II/lac/sparse_matrix.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,8 @@ SparseMatrix<number>::print_formatted(std::ostream &out,
const bool scientific,
const unsigned int width_,
const char *zero_string,
const double denominator) const
const double denominator,
const char *separator) const
{
Assert(cols != nullptr, ExcNeedsSparsityPattern());
Assert(val != nullptr, ExcNotInitialized());
Expand Down Expand Up @@ -1940,9 +1941,9 @@ SparseMatrix<number>::print_formatted(std::ostream &out,
for (size_type j = 0; j < n(); ++j)
if ((*cols)(i, j) != SparsityPattern::invalid_entry)
out << std::setw(width)
<< val[cols->operator()(i, j)] * number(denominator) << ' ';
<< val[cols->operator()(i, j)] * number(denominator) << separator;
else
out << std::setw(width) << zero_string << ' ';
out << std::setw(width) << zero_string << separator;
out << std::endl;
};
AssertThrow(out.fail() == false, ExcIO());
Expand Down
27 changes: 15 additions & 12 deletions include/deal.II/lac/sparse_matrix_ez.h
Original file line number Diff line number Diff line change
Expand Up @@ -749,32 +749,35 @@ class SparseMatrixEZ : public Subscriptor
print(std::ostream &out) const;

/**
* Print the matrix in the usual format, i.e. as a matrix and not as a list
* Print the matrix in the usual format, i.e., as a matrix and not as a list
* of nonzero elements. For better readability, elements not in the matrix
* are displayed as empty space, while matrix elements which are explicitly
* set to zero are displayed as such.
*
* The parameters allow for a flexible setting of the output format: @p
* precision and @p scientific are used to determine the number format,
* where @p scientific = @p false means fixed point notation. A zero entry
* for @p width makes the function compute a width, but it may be changed to
* a positive value, if output is crude.
* The parameters allow for a flexible setting of the output format:
* @p precision and @p scientific are used to determine the number format,
* where <code>scientific = false</code> means fixed point notation. A zero
* entry for @p width makes the function compute a width, but it may be
* changed to a positive value, if output is crude.
*
* Additionally, a character for an empty value may be specified.
* Additionally, a character for an empty value may be specified in
* @p zero_string, and a character to separate row entries can be set in
* @p separator.
*
* Finally, the whole matrix can be multiplied with a common denominator to
* produce more readable output, even integers.
* Finally, the whole matrix can be multiplied with a common @p denominator
* to produce more readable output, even integers.
*
* This function may produce @em large amounts of output if applied to a
* large matrix!
* @attention This function may produce @em large amounts of output if
* applied to a large matrix!
*/
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;
const double denominator = 1.,
const char *separator = " ") const;

/**
* Write the data of this object in binary mode to a file.
Expand Down
7 changes: 4 additions & 3 deletions include/deal.II/lac/sparse_matrix_ez.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ SparseMatrixEZ<number>::print_formatted(std::ostream &out,
const bool scientific,
const unsigned int width_,
const char *zero_string,
const double denominator) const
const double denominator,
const char *separator) const
{
AssertThrow(out.fail() == false, ExcIO());
Assert(m() != 0, ExcNotInitialized());
Expand Down Expand Up @@ -516,9 +517,9 @@ SparseMatrixEZ<number>::print_formatted(std::ostream &out,
{
const Entry *entry = locate(i, j);
if (entry)
out << std::setw(width) << entry->value * denominator << ' ';
out << std::setw(width) << entry->value * denominator << separator;
else
out << std::setw(width) << zero_string << ' ';
out << std::setw(width) << zero_string << separator;
}
out << std::endl;
};
Expand Down
9 changes: 5 additions & 4 deletions source/lac/lapack_full_matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2523,7 +2523,8 @@ LAPACKFullMatrix<number>::print_formatted(std::ostream &out,
const unsigned int width_,
const char *zero_string,
const double denominator,
const double threshold) const
const double threshold,
const char *separator) const
{
unsigned int width = width_;

Expand Down Expand Up @@ -2559,11 +2560,11 @@ LAPACKFullMatrix<number>::print_formatted(std::ostream &out,
// we might have complex numbers, so use abs also to check for nan
// since there is no isnan on complex numbers
if (numbers::is_nan(std::abs((*this)(i, j))))
out << std::setw(width) << (*this)(i, j) << ' ';
out << std::setw(width) << (*this)(i, j) << separator;
else if (std::abs(this->el(i, j)) > threshold)
out << std::setw(width) << this->el(i, j) * denominator << ' ';
out << std::setw(width) << this->el(i, j) * denominator << separator;
else
out << std::setw(width) << zero_string << ' ';
out << std::setw(width) << zero_string << separator;
out << std::endl;
}

Expand Down

0 comments on commit 7e67417

Please sign in to comment.