From: Martin Kronbichler Date: Thu, 8 Jun 2017 13:32:06 +0000 (+0200) Subject: Fix FullMatrix::print_formatted for NaN entries. X-Git-Tag: v9.0.0-rc1~1520^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6cd854e8f3d8eb3edd89f2d091cca504133020b0;p=dealii.git Fix FullMatrix::print_formatted for NaN entries. --- diff --git a/doc/news/changes/minor/20170608MartinKronbichler.txt b/doc/news/changes/minor/20170608MartinKronbichler.txt new file mode 100644 index 0000000000..0ccd59907a --- /dev/null +++ b/doc/news/changes/minor/20170608MartinKronbichler.txt @@ -0,0 +1,4 @@ +Fixed: FullMatrix::print_formatted() and LAPACKFullMatrix::print_formatted +would previously print NaN values as zero. This is now fixed. +
+(Simon Sticko, Martin Kronbichler, 2017/06/08) diff --git a/include/deal.II/lac/full_matrix.templates.h b/include/deal.II/lac/full_matrix.templates.h index 6f6e001931..bcc5f2e1f4 100644 --- a/include/deal.II/lac/full_matrix.templates.h +++ b/include/deal.II/lac/full_matrix.templates.h @@ -1663,7 +1663,11 @@ FullMatrix::print_formatted ( for (size_type i=0; i threshold) + // we might have complex numbers, so use abs also to check for nan + // since there is no isnan on complex numbers + if (std::isnan(std::abs((*this)(i,j)))) + out << std::setw(width) << (*this)(i,j) << ' '; + else if (std::abs((*this)(i,j)) > threshold) out << std::setw(width) << (*this)(i,j) * number(denominator) << ' '; else diff --git a/source/lac/lapack_full_matrix.cc b/source/lac/lapack_full_matrix.cc index 61031f54c0..aa3254f7f4 100644 --- a/source/lac/lapack_full_matrix.cc +++ b/source/lac/lapack_full_matrix.cc @@ -1010,7 +1010,11 @@ LAPACKFullMatrix::print_formatted ( for (size_type i=0; in_rows(); ++i) { for (size_type j=0; jn_cols(); ++j) - if (std::fabs(this->el(i,j)) > threshold) + // we might have complex numbers, so use abs also to check for nan + // since there is no isnan on complex numbers + if (std::isnan(std::abs((*this)(i,j)))) + out << std::setw(width) << (*this)(i,j) << ' '; + else if (std::fabs(this->el(i,j)) > threshold) out << std::setw(width) << this->el(i,j) * denominator << ' '; else diff --git a/tests/lac/print_formatted_full_matrix.cc b/tests/lac/print_formatted_full_matrix.cc new file mode 100644 index 0000000000..bdc59e2b01 --- /dev/null +++ b/tests/lac/print_formatted_full_matrix.cc @@ -0,0 +1,42 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// Check FullMatrix::print_formatted on NaN entry + +#include "../tests.h" + +#include + +#include +#include + +int main() +{ + initlog(); + + FullMatrix matrix(2,2); + matrix(0,0) = std::numeric_limits::quiet_NaN(); + matrix(0,1) = std::numeric_limits::infinity(); + matrix(1,1) = -std::numeric_limits::infinity(); + + deallog << "Using print" << std::endl; + matrix.print(deallog.get_file_stream()); + + deallog << "Using print_formatted" << std::endl; + matrix.print_formatted(deallog.get_file_stream(), 3, true, 0, "0"); + + return 0; +} diff --git a/tests/lac/print_formatted_full_matrix.output b/tests/lac/print_formatted_full_matrix.output new file mode 100644 index 0000000000..deac438622 --- /dev/null +++ b/tests/lac/print_formatted_full_matrix.output @@ -0,0 +1,7 @@ + +DEAL::Using print +nan inf +0.0 -inf +DEAL::Using print_formatted +nan inf +0 -inf diff --git a/tests/lac/print_formatted_lapack.cc b/tests/lac/print_formatted_lapack.cc new file mode 100644 index 0000000000..9adb1111d4 --- /dev/null +++ b/tests/lac/print_formatted_lapack.cc @@ -0,0 +1,39 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// Check LAPACKFullMatrix::print_formatted on NaN entry + +#include "../tests.h" + +#include + +#include +#include + +int main() +{ + initlog(); + + LAPACKFullMatrix matrix(2,2); + matrix(0,0) = std::numeric_limits::quiet_NaN(); + matrix(0,1) = std::numeric_limits::infinity(); + matrix(1,1) = -std::numeric_limits::infinity(); + + deallog << "Using print_formatted" << std::endl; + matrix.print_formatted(deallog.get_file_stream(), 3, true, 0, "0"); + + return 0; +} diff --git a/tests/lac/print_formatted_lapack.with_lapack=true.output b/tests/lac/print_formatted_lapack.with_lapack=true.output new file mode 100644 index 0000000000..84785c43b7 --- /dev/null +++ b/tests/lac/print_formatted_lapack.with_lapack=true.output @@ -0,0 +1,4 @@ + +DEAL::Using print_formatted +nan inf +0 -inf