]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix FullMatrix::print_formatted for NaN entries. 4499/head
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Thu, 8 Jun 2017 13:32:06 +0000 (15:32 +0200)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Thu, 8 Jun 2017 13:48:53 +0000 (15:48 +0200)
doc/news/changes/minor/20170608MartinKronbichler.txt [new file with mode: 0644]
include/deal.II/lac/full_matrix.templates.h
source/lac/lapack_full_matrix.cc
tests/lac/print_formatted_full_matrix.cc [new file with mode: 0644]
tests/lac/print_formatted_full_matrix.output [new file with mode: 0644]
tests/lac/print_formatted_lapack.cc [new file with mode: 0644]
tests/lac/print_formatted_lapack.with_lapack=true.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20170608MartinKronbichler.txt b/doc/news/changes/minor/20170608MartinKronbichler.txt
new file mode 100644 (file)
index 0000000..0ccd599
--- /dev/null
@@ -0,0 +1,4 @@
+Fixed: FullMatrix::print_formatted() and LAPACKFullMatrix::print_formatted
+would previously print NaN values as zero. This is now fixed.
+<br>
+(Simon Sticko, Martin Kronbichler, 2017/06/08)
index 6f6e001931cf96b78036864386087a6f16a31558..bcc5f2e1f4aab62b0ac124b2da4e56868266f360 100644 (file)
@@ -1663,7 +1663,11 @@ FullMatrix<number>::print_formatted (
   for (size_type i=0; i<m(); ++i)
     {
       for (size_type j=0; j<n(); ++j)
-        if (std::abs((*this)(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::abs((*this)(i,j)) > threshold)
           out << std::setw(width)
               << (*this)(i,j) * number(denominator) << ' ';
         else
index 61031f54c078f14a68ab8d176f974fe5170e7956..aa3254f7f4c758d53b2344bd2e119d1451d45a69 100644 (file)
@@ -1010,7 +1010,11 @@ LAPACKFullMatrix<number>::print_formatted (
   for (size_type i=0; i<this->n_rows(); ++i)
     {
       for (size_type j=0; j<this->n_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 (file)
index 0000000..bdc59e2
--- /dev/null
@@ -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 <deal.II/lac/full_matrix.h>
+
+#include <iomanip>
+#include <fstream>
+
+int main()
+{
+  initlog();
+
+  FullMatrix<double> matrix(2,2);
+  matrix(0,0) = std::numeric_limits<double>::quiet_NaN();
+  matrix(0,1) = std::numeric_limits<double>::infinity();
+  matrix(1,1) = -std::numeric_limits<double>::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 (file)
index 0000000..deac438
--- /dev/null
@@ -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 (file)
index 0000000..9adb111
--- /dev/null
@@ -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 <deal.II/lac/lapack_full_matrix.h>
+
+#include <iomanip>
+#include <fstream>
+
+int main()
+{
+  initlog();
+
+  LAPACKFullMatrix<double> matrix(2,2);
+  matrix(0,0) = std::numeric_limits<double>::quiet_NaN();
+  matrix(0,1) = std::numeric_limits<double>::infinity();
+  matrix(1,1) = -std::numeric_limits<double>::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 (file)
index 0000000..84785c4
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::Using print_formatted
+nan        inf        
+0          -inf       

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.