From 0fa6ca62b0792abce55c35bcfe77cb1c4a9bd8d6 Mon Sep 17 00:00:00 2001 From: Marc Fehling Date: Fri, 13 Oct 2023 16:03:10 -0600 Subject: [PATCH] Added separator to Matrix::print_formatted(). --- doc/news/changes/minor/20231015Fehling | 7 +++++ include/deal.II/lac/block_sparse_matrix.h | 23 ++++++++------ .../lac/block_sparse_matrix.templates.h | 12 +++++-- include/deal.II/lac/chunk_sparse_matrix.h | 23 ++++++++------ .../lac/chunk_sparse_matrix.templates.h | 7 +++-- include/deal.II/lac/cuda_sparse_matrix.h | 31 +++++++++++-------- include/deal.II/lac/full_matrix.h | 23 ++++++++------ include/deal.II/lac/full_matrix.templates.h | 10 +++--- include/deal.II/lac/lapack_full_matrix.h | 5 ++- include/deal.II/lac/sparse_matrix.h | 23 ++++++++------ include/deal.II/lac/sparse_matrix.templates.h | 7 +++-- include/deal.II/lac/sparse_matrix_ez.h | 27 +++++++++------- .../deal.II/lac/sparse_matrix_ez.templates.h | 7 +++-- source/lac/lapack_full_matrix.cc | 9 +++--- 14 files changed, 129 insertions(+), 85 deletions(-) create mode 100644 doc/news/changes/minor/20231015Fehling diff --git a/doc/news/changes/minor/20231015Fehling b/doc/news/changes/minor/20231015Fehling new file mode 100644 index 0000000000..e5bd7f7802 --- /dev/null +++ b/doc/news/changes/minor/20231015Fehling @@ -0,0 +1,7 @@ +New: The print_formatted() functions of the following matrix classes +now offer the option to specify a string that separates row entries: +
+SparseMatrixEZ, SparseMatrix, LAPACKFullMatrix, FullMatrix, +ChunkSparseMatrix, BlockSparseMatrix, CUDAWrappers::SparseMatrix +
+(Marc Fehling, 2023/10/15) diff --git a/include/deal.II/lac/block_sparse_matrix.h b/include/deal.II/lac/block_sparse_matrix.h index 88d086e353..cd06f2e71f 100644 --- a/include/deal.II/lac/block_sparse_matrix.h +++ b/include/deal.II/lac/block_sparse_matrix.h @@ -320,23 +320,25 @@ public: */ /** @{ */ /** - * 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: - * precision and scientific are used to determine the - * number format, where scientific = false means fixed point - * notation. A zero entry for width 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 scientific = 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. * - * 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 large amounts of output if + * @attention This function may produce @em large amounts of output if * applied to a large matrix! */ void @@ -345,7 +347,8 @@ public: 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 diff --git a/include/deal.II/lac/block_sparse_matrix.templates.h b/include/deal.II/lac/block_sparse_matrix.templates.h index 360296de9e..3044e5e61c 100644 --- a/include/deal.II/lac/block_sparse_matrix.templates.h +++ b/include/deal.II/lac/block_sparse_matrix.templates.h @@ -178,14 +178,20 @@ BlockSparseMatrix::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); } } diff --git a/include/deal.II/lac/chunk_sparse_matrix.h b/include/deal.II/lac/chunk_sparse_matrix.h index 1ed8821919..ec7c428cf7 100644 --- a/include/deal.II/lac/chunk_sparse_matrix.h +++ b/include/deal.II/lac/chunk_sparse_matrix.h @@ -1285,23 +1285,25 @@ public: 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: - * precision and scientific are used to determine the - * number format, where scientific = false means fixed point - * notation. A zero entry for width 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 scientific = 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. * - * 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 large amounts of output if + * @attention This function may produce @em large amounts of output if * applied to a large matrix! */ void @@ -1310,7 +1312,8 @@ public: 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 diff --git a/include/deal.II/lac/chunk_sparse_matrix.templates.h b/include/deal.II/lac/chunk_sparse_matrix.templates.h index 914b131e97..2eca5897c5 100644 --- a/include/deal.II/lac/chunk_sparse_matrix.templates.h +++ b/include/deal.II/lac/chunk_sparse_matrix.templates.h @@ -1473,7 +1473,8 @@ ChunkSparseMatrix::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()); @@ -1505,9 +1506,9 @@ ChunkSparseMatrix::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()); diff --git a/include/deal.II/lac/cuda_sparse_matrix.h b/include/deal.II/lac/cuda_sparse_matrix.h index 6755b3bb6f..6b711c0ddf 100644 --- a/include/deal.II/lac/cuda_sparse_matrix.h +++ b/include/deal.II/lac/cuda_sparse_matrix.h @@ -166,23 +166,25 @@ namespace CUDAWrappers 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: - * precision and scientific are used to determine the - * number format, where scientific = false means fixed point - * notation. A zero entry for width 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 scientific = 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. * - * 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 large amounts of output if + * @attention This function may produce @em large amounts of output if * applied to a large matrix! */ void @@ -191,7 +193,8 @@ namespace CUDAWrappers 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; /** @} */ /** @@ -467,7 +470,8 @@ namespace CUDAWrappers const bool scientific, const unsigned int width_, const char *zero_string, - const double denominator) const + const double denominator, + const char *separator) const { Assert(column_index_dev != nullptr, ExcNotInitialized()); Assert(val_dev != nullptr, ExcNotInitialized()); @@ -505,11 +509,12 @@ namespace CUDAWrappers { if (k == cols[j]) { - out << std::setw(width) << val[j] * Number(denominator) << ' '; + out << std::setw(width) << val[j] * Number(denominator) + << separator; ++j; } else - out << std::setw(width) << zero_string << ' '; + out << std::setw(width) << zero_string << separator; } out << std::endl; }; diff --git a/include/deal.II/lac/full_matrix.h b/include/deal.II/lac/full_matrix.h index d025ab1bfe..787330ad7d 100644 --- a/include/deal.II/lac/full_matrix.h +++ b/include/deal.II/lac/full_matrix.h @@ -498,22 +498,26 @@ public: * * The parameters allow for a flexible setting of the output format: * - * @arg precision denotes the number of trailing digits. + * @param out This specifies the stream to write to. * - * @arg scientific is used to determine the number format, where - * scientific = false means fixed point notation. + * @param precision denotes the number of trailing digits. * - * @arg width denotes the with of each column. A zero entry for - * width makes the function compute a width, but it may be changed + * @param scientific is used to determine the number format, where + * scientific = false means fixed point notation. + * + * @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 zero_string specifies a string printed for zero entries. + * @param zero_string specifies a string printed for zero entries. * - * @arg denominator Multiply the whole matrix by this common + * @param denominator Multiply the whole matrix by this common * denominator to get nicer numbers. * - * @arg threshold: 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, @@ -522,7 +526,8 @@ public: 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 diff --git a/include/deal.II/lac/full_matrix.templates.h b/include/deal.II/lac/full_matrix.templates.h index 4f8163a4e9..69c1466685 100644 --- a/include/deal.II/lac/full_matrix.templates.h +++ b/include/deal.II/lac/full_matrix.templates.h @@ -1657,7 +1657,8 @@ FullMatrix::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_; @@ -1688,11 +1689,12 @@ FullMatrix::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; }; diff --git a/include/deal.II/lac/lapack_full_matrix.h b/include/deal.II/lac/lapack_full_matrix.h index 6f77da50fb..9a71f305bf 100644 --- a/include/deal.II/lac/lapack_full_matrix.h +++ b/include/deal.II/lac/lapack_full_matrix.h @@ -902,6 +902,8 @@ public: * @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. @@ -913,7 +915,8 @@ public: 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: /** diff --git a/include/deal.II/lac/sparse_matrix.h b/include/deal.II/lac/sparse_matrix.h index cc3c481d04..d1400eab43 100644 --- a/include/deal.II/lac/sparse_matrix.h +++ b/include/deal.II/lac/sparse_matrix.h @@ -1584,23 +1584,25 @@ public: 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: - * precision and scientific are used to determine the - * number format, where scientific = false means fixed point - * notation. A zero entry for width 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 scientific = 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. * - * 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 large amounts of output if + * @attention This function may produce @em large amounts of output if * applied to a large matrix! */ void @@ -1609,7 +1611,8 @@ public: 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 diff --git a/include/deal.II/lac/sparse_matrix.templates.h b/include/deal.II/lac/sparse_matrix.templates.h index 5910ca5a76..c1c97cae50 100644 --- a/include/deal.II/lac/sparse_matrix.templates.h +++ b/include/deal.II/lac/sparse_matrix.templates.h @@ -1912,7 +1912,8 @@ SparseMatrix::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()); @@ -1940,9 +1941,9 @@ SparseMatrix::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()); diff --git a/include/deal.II/lac/sparse_matrix_ez.h b/include/deal.II/lac/sparse_matrix_ez.h index 46fa52da60..ab0d0c2c48 100644 --- a/include/deal.II/lac/sparse_matrix_ez.h +++ b/include/deal.II/lac/sparse_matrix_ez.h @@ -749,24 +749,26 @@ public: 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 scientific = 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. * - * 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, @@ -774,7 +776,8 @@ public: 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. diff --git a/include/deal.II/lac/sparse_matrix_ez.templates.h b/include/deal.II/lac/sparse_matrix_ez.templates.h index 29d30e5672..f40ff31fbd 100644 --- a/include/deal.II/lac/sparse_matrix_ez.templates.h +++ b/include/deal.II/lac/sparse_matrix_ez.templates.h @@ -485,7 +485,8 @@ SparseMatrixEZ::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()); @@ -516,9 +517,9 @@ SparseMatrixEZ::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; }; diff --git a/source/lac/lapack_full_matrix.cc b/source/lac/lapack_full_matrix.cc index 3a08525826..f6d95c2a05 100644 --- a/source/lac/lapack_full_matrix.cc +++ b/source/lac/lapack_full_matrix.cc @@ -2523,7 +2523,8 @@ LAPACKFullMatrix::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_; @@ -2559,11 +2560,11 @@ LAPACKFullMatrix::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; } -- 2.39.5