]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Provide the first few parts of numbers::NumberTraits
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Sat, 3 Nov 2007 23:58:37 +0000 (23:58 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Sat, 3 Nov 2007 23:58:37 +0000 (23:58 +0000)
git-svn-id: https://svn.dealii.org/trunk@15434 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/numbers.h
deal.II/doc/news/changes.h
tests/base/Makefile
tests/base/number_traits_complex.cc [new file with mode: 0644]
tests/base/number_traits_complex/cmp/generic [new file with mode: 0644]
tests/base/number_traits_real.cc [new file with mode: 0644]
tests/base/number_traits_real/cmp/generic [new file with mode: 0644]

index e345a4a638c70575f8b577c75f6c47f60cfde321..934885ef2efb2527367e444173279c91facdb8eb 100644 (file)
@@ -145,6 +145,102 @@ namespace numbers
                                    * <code>long double</code>.
                                    */
   bool is_finite (const std::complex<long double> x);
+
+                                  /**
+                                   * A structure that, together with its
+                                   * partial specializations, provides traits
+                                   * and member functions that make it
+                                   * possible to write templates that work on
+                                   * both real number types and complex
+                                   * number types. This template is mostly
+                                   * used to implement linear algebra classes
+                                   * such as vectors and matrices that work
+                                   * for both real and complex numbers.
+                                   *
+                                   * @author Wolfgang Bangerth, 2007
+                                   */
+  template <typename number>
+  struct NumberTraits
+  {
+                                      /**
+                                       * For this data type, typedef the
+                                       * corresponding real type. Since the
+                                       * general template is selected for all
+                                       * data types that are not
+                                       * specializations of std::complex<T>,
+                                       * the underlying type must be
+                                       * real-values, so the real_type is
+                                       * equal to the underlying type.
+                                       */
+      typedef number real_type;
+
+                                      /**
+                                       * Return the complex-conjugate of the
+                                       * given number. Since the general
+                                       * template is selected if number is
+                                       * not a complex data type, this
+                                       * function simply returns the given
+                                       * number.
+                                       */
+      static
+      const number & conjugate (const number &x);
+
+                                      /**
+                                       * Return the square of the absolute
+                                       * value of the given number. Since the
+                                       * general template is chosen for types
+                                       * not equal to std::complex, this
+                                       * function simply returns the square
+                                       * of the given number.
+                                       */
+      static
+      real_type abs_square (const number &x);
+  };
+
+
+                                  /**
+                                   * Specialization of the general
+                                   * NumberTraits class that provides the
+                                   * relevant information if the underlying
+                                   * data type is std::complex<T>.
+                                   *
+                                   * @author Wolfgang Bangerth, 2007
+                                   */
+  template <typename number>
+  struct NumberTraits<std::complex<number> >
+  {
+                                      /**
+                                       * For this data type, typedef the
+                                       * corresponding real type. Since this
+                                       * specialization of the template is
+                                       * selected for number types
+                                       * std::complex<T>, the real type is
+                                       * equal to the type used to store the
+                                       * two components of the complex
+                                       * number.
+                                       */
+      typedef number real_type;
+
+                                      /**
+                                       * Return the complex-conjugate of the
+                                       * given number.
+                                       */
+      static
+      std::complex<number> conjugate (const std::complex<number> &x);
+
+                                      /**
+                                       * Return the square of the absolute
+                                       * value of the given number. Since
+                                       * this specialization of the general
+                                       * template is chosen for types equal
+                                       * to std::complex, this function
+                                       * returns the product of a number and
+                                       * its complex conjugate.
+                                       */
+      static
+      real_type abs_square (const std::complex<number> &x);
+  };
+  
 }
 
 
@@ -159,6 +255,46 @@ namespace numbers
 namespace deal_II_numbers = numbers;
 
 
+/* --------------- inline and template functions ---------------- */
+namespace numbers
+{
+  template <typename number>
+  const number &
+  NumberTraits<number>::conjugate (const number &x)
+  {
+    return x;
+  }
+
+
+
+  template <typename number>
+  typename NumberTraits<number>::real_type
+  NumberTraits<number>::abs_square (const number &x)
+  {
+    return x * x;
+  }
+
+
+
+  template <typename number>
+  std::complex<number>
+  NumberTraits<std::complex<number> >::conjugate (const std::complex<number> &x)
+  {
+    return std::conj(x);
+  }
+
+
+
+  template <typename number>
+  typename NumberTraits<std::complex<number> >::real_type
+  NumberTraits<std::complex<number> >::abs_square (const std::complex<number> &x)
+  {
+    return std::norm (x);
+  }
+  
+}
+
+  
 
 DEAL_II_NAMESPACE_CLOSE
 
index efbbc4059c26e5578a4794b5394f76726b99850e..67ddb23054b06364e147af405d7d3edd1f7898ae 100644 (file)
@@ -120,6 +120,12 @@ inconvenience this causes.
 <h3>base</h3>
 
 <ol>
+  <li> <p>New: There is now a template numbers::NumberTraits that provides
+  the means to implement linear algebra and other algorithms for both real
+  and complex data types.
+  <br>
+  (WB 2007/11/03)
+  </p> </li>
 
   <li> <p>New: The DataOutBase class and derived classes can now deal with data that is
   logically vector-valued, i.e. derived classes can pass down information that some of
index a9ed4112e999cdfd70efa80233a933501c3698be..8b6898977f7bec409fea0c0f1f55f9c8f0f5e709 100644 (file)
@@ -51,7 +51,8 @@ tests_x =  geometry_info_* \
            functions_* \
           function_parser \
           utilities_* \
-          is_finite
+          is_finite \
+          number_traits_*
 
 ifeq ($(enable-multithreading),no)
   tests_x += threads
diff --git a/tests/base/number_traits_complex.cc b/tests/base/number_traits_complex.cc
new file mode 100644 (file)
index 0000000..c37530b
--- /dev/null
@@ -0,0 +1,62 @@
+//----------------------------  number_traits_complex.cc  ---------------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 2005, 2006, 2007 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  number_traits_complex.cc  ---------------------------
+
+// check numbers::NumberTraits for real data types
+
+#include "../tests.h"
+#include <base/logstream.h>
+#include <fstream>
+#include <iostream>
+#include <limits>
+#include <typeinfo>
+
+
+template <typename number>
+void check (const number &x)
+{
+  deallog << "typeid(x).name() = " << typeid(x).name()
+         << std::endl;
+
+  deallog << "typeid(NumberTraits<number>::real_type).name() = "
+         << typeid(typename numbers::NumberTraits<number>::real_type).name()
+         << std::endl;
+
+  deallog << numbers::NumberTraits<number>::conjugate (x)
+         << std::endl;
+
+  deallog << numbers::NumberTraits<number>::abs_square (x)
+         << std::endl;
+} 
+
+
+
+int main ()
+{
+  std::ofstream logfile("number_traits_complex/output");
+  logfile.precision(3);
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  check (std::complex<float>(1.5, 2.5));
+  check (std::complex<float>(-1.5, -2.5));
+
+  check (std::complex<double>(1.5, 2.5));
+  check (std::complex<double>(-1.5, -2.5));
+
+  check (std::complex<long double>(1.5, 2.5));
+  check (std::complex<long double>(-1.5, -2.5));
+
+  return 0;
+}
+
diff --git a/tests/base/number_traits_complex/cmp/generic b/tests/base/number_traits_complex/cmp/generic
new file mode 100644 (file)
index 0000000..1802a34
--- /dev/null
@@ -0,0 +1,25 @@
+
+DEAL::typeid(x).name() = St7complexIfE
+DEAL::typeid(NumberTraits<number>::real_type).name() = f
+DEAL::(1.50,-2.50)
+DEAL::8.50
+DEAL::typeid(x).name() = St7complexIfE
+DEAL::typeid(NumberTraits<number>::real_type).name() = f
+DEAL::(-1.50,2.50)
+DEAL::8.50
+DEAL::typeid(x).name() = St7complexIdE
+DEAL::typeid(NumberTraits<number>::real_type).name() = d
+DEAL::(1.50,-2.50)
+DEAL::8.50
+DEAL::typeid(x).name() = St7complexIdE
+DEAL::typeid(NumberTraits<number>::real_type).name() = d
+DEAL::(-1.50,2.50)
+DEAL::8.50
+DEAL::typeid(x).name() = St7complexIeE
+DEAL::typeid(NumberTraits<number>::real_type).name() = e
+DEAL::(1.50,-2.50)
+DEAL::8.50
+DEAL::typeid(x).name() = St7complexIeE
+DEAL::typeid(NumberTraits<number>::real_type).name() = e
+DEAL::(-1.50,2.50)
+DEAL::8.50
diff --git a/tests/base/number_traits_real.cc b/tests/base/number_traits_real.cc
new file mode 100644 (file)
index 0000000..5d44968
--- /dev/null
@@ -0,0 +1,62 @@
+//----------------------------  number_traits_real.cc  ---------------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 2005, 2006, 2007 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  number_traits_real.cc  ---------------------------
+
+// check numbers::NumberTraits for real data types
+
+#include "../tests.h"
+#include <base/logstream.h>
+#include <fstream>
+#include <iostream>
+#include <limits>
+#include <typeinfo>
+
+
+template <typename number>
+void check (const number &x)
+{
+  deallog << "typeid(x).name() = " << typeid(x).name()
+         << std::endl;
+
+  deallog << "typeid(NumberTraits<number>::real_type).name() = "
+         << typeid(typename numbers::NumberTraits<number>::real_type).name()
+         << std::endl;
+
+  deallog << numbers::NumberTraits<number>::conjugate (x)
+         << std::endl;
+
+  deallog << numbers::NumberTraits<number>::abs_square (x)
+         << std::endl;
+} 
+
+
+
+int main ()
+{
+  std::ofstream logfile("number_traits_real/output");
+  logfile.precision(3);
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  check ((float)1.5);
+  check ((float)-1.5);
+
+  check ((double)1.5);
+  check ((double)-1.5);
+
+  check ((long double)1.5);
+  check ((long double)-1.5);  
+
+  return 0;
+}
+
diff --git a/tests/base/number_traits_real/cmp/generic b/tests/base/number_traits_real/cmp/generic
new file mode 100644 (file)
index 0000000..bfcaccf
--- /dev/null
@@ -0,0 +1,25 @@
+
+DEAL::typeid(x).name() = f
+DEAL::typeid(NumberTraits<number>::real_type).name() = f
+DEAL::1.50
+DEAL::2.25
+DEAL::typeid(x).name() = f
+DEAL::typeid(NumberTraits<number>::real_type).name() = f
+DEAL::-1.50
+DEAL::2.25
+DEAL::typeid(x).name() = d
+DEAL::typeid(NumberTraits<number>::real_type).name() = d
+DEAL::1.50
+DEAL::2.25
+DEAL::typeid(x).name() = d
+DEAL::typeid(NumberTraits<number>::real_type).name() = d
+DEAL::-1.50
+DEAL::2.25
+DEAL::typeid(x).name() = e
+DEAL::typeid(NumberTraits<number>::real_type).name() = e
+DEAL::1.50
+DEAL::2.25
+DEAL::typeid(x).name() = e
+DEAL::typeid(NumberTraits<number>::real_type).name() = e
+DEAL::-1.50
+DEAL::2.25

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.