From 8d7c04da4c603f0514fef9a37b3e8a99515125b5 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Sun, 4 Nov 2007 05:34:29 +0000 Subject: [PATCH] Add NumberTraits::abs as well as a variant of numbers::is_finite for complex. git-svn-id: https://svn.dealii.org/trunk@15437 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/numbers.h | 40 ++++++++++++++++++++ deal.II/base/source/config.cc | 13 ++++++- tests/base/number_traits_complex.cc | 3 ++ tests/base/number_traits_complex/cmp/generic | 6 +++ tests/base/number_traits_real.cc | 3 ++ tests/base/number_traits_real/cmp/generic | 6 +++ 6 files changed, 70 insertions(+), 1 deletion(-) diff --git a/deal.II/base/include/base/numbers.h b/deal.II/base/include/base/numbers.h index 934885ef2e..89751ec455 100644 --- a/deal.II/base/include/base/numbers.h +++ b/deal.II/base/include/base/numbers.h @@ -132,6 +132,13 @@ namespace numbers */ bool is_finite (const std::complex x); + /** + * Return @p true if real and + * imaginary parts of the given + * complex number are finite. + */ + bool is_finite (const std::complex x); + /** * Return @p true if real and * imaginary parts of the given @@ -195,6 +202,13 @@ namespace numbers */ static real_type abs_square (const number &x); + + /** + * Return the absolute value of a + * number. + */ + static + real_type abs (const number &x); }; @@ -239,6 +253,14 @@ namespace numbers */ static real_type abs_square (const std::complex &x); + + + /** + * Return the absolute value of a + * complex number. + */ + static + real_type abs (const std::complex &x); }; } @@ -276,6 +298,15 @@ namespace numbers + template + typename NumberTraits::real_type + NumberTraits::abs (const number &x) + { + return std::fabs(x); + } + + + template std::complex NumberTraits >::conjugate (const std::complex &x) @@ -285,6 +316,15 @@ namespace numbers + template + typename NumberTraits >::real_type + NumberTraits >::abs (const std::complex &x) + { + return std::abs(x); + } + + + template typename NumberTraits >::real_type NumberTraits >::abs_square (const std::complex &x) diff --git a/deal.II/base/source/config.cc b/deal.II/base/source/config.cc index 4153b2a9eb..890eca248e 100644 --- a/deal.II/base/source/config.cc +++ b/deal.II/base/source/config.cc @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 2006 by the deal.II authors +// Copyright (C) 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 @@ -48,6 +48,17 @@ namespace numbers + bool is_finite (const std::complex x) + { + // Check complex numbers for infinity + // by testing real and imaginary part + return ( is_finite (x.real()) + && + is_finite (x.imag()) ); + } + + + bool is_finite (const std::complex x) { // Same for std::complex diff --git a/tests/base/number_traits_complex.cc b/tests/base/number_traits_complex.cc index c37530b072..c78e081767 100644 --- a/tests/base/number_traits_complex.cc +++ b/tests/base/number_traits_complex.cc @@ -36,6 +36,9 @@ void check (const number &x) deallog << numbers::NumberTraits::abs_square (x) << std::endl; + + deallog << numbers::NumberTraits::abs (x) + << std::endl; } diff --git a/tests/base/number_traits_complex/cmp/generic b/tests/base/number_traits_complex/cmp/generic index 1802a34257..41298771aa 100644 --- a/tests/base/number_traits_complex/cmp/generic +++ b/tests/base/number_traits_complex/cmp/generic @@ -3,23 +3,29 @@ DEAL::typeid(x).name() = St7complexIfE DEAL::typeid(NumberTraits::real_type).name() = f DEAL::(1.50,-2.50) DEAL::8.50 +DEAL::2.92 DEAL::typeid(x).name() = St7complexIfE DEAL::typeid(NumberTraits::real_type).name() = f DEAL::(-1.50,2.50) DEAL::8.50 +DEAL::2.92 DEAL::typeid(x).name() = St7complexIdE DEAL::typeid(NumberTraits::real_type).name() = d DEAL::(1.50,-2.50) DEAL::8.50 +DEAL::2.92 DEAL::typeid(x).name() = St7complexIdE DEAL::typeid(NumberTraits::real_type).name() = d DEAL::(-1.50,2.50) DEAL::8.50 +DEAL::2.92 DEAL::typeid(x).name() = St7complexIeE DEAL::typeid(NumberTraits::real_type).name() = e DEAL::(1.50,-2.50) DEAL::8.50 +DEAL::2.92 DEAL::typeid(x).name() = St7complexIeE DEAL::typeid(NumberTraits::real_type).name() = e DEAL::(-1.50,2.50) DEAL::8.50 +DEAL::2.92 diff --git a/tests/base/number_traits_real.cc b/tests/base/number_traits_real.cc index 5d44968af5..3e93a5c2bf 100644 --- a/tests/base/number_traits_real.cc +++ b/tests/base/number_traits_real.cc @@ -36,6 +36,9 @@ void check (const number &x) deallog << numbers::NumberTraits::abs_square (x) << std::endl; + + deallog << numbers::NumberTraits::abs (x) + << std::endl; } diff --git a/tests/base/number_traits_real/cmp/generic b/tests/base/number_traits_real/cmp/generic index bfcaccfd64..340da47975 100644 --- a/tests/base/number_traits_real/cmp/generic +++ b/tests/base/number_traits_real/cmp/generic @@ -3,23 +3,29 @@ DEAL::typeid(x).name() = f DEAL::typeid(NumberTraits::real_type).name() = f DEAL::1.50 DEAL::2.25 +DEAL::1.50 DEAL::typeid(x).name() = f DEAL::typeid(NumberTraits::real_type).name() = f DEAL::-1.50 DEAL::2.25 +DEAL::1.50 DEAL::typeid(x).name() = d DEAL::typeid(NumberTraits::real_type).name() = d DEAL::1.50 DEAL::2.25 +DEAL::1.50 DEAL::typeid(x).name() = d DEAL::typeid(NumberTraits::real_type).name() = d DEAL::-1.50 DEAL::2.25 +DEAL::1.50 DEAL::typeid(x).name() = e DEAL::typeid(NumberTraits::real_type).name() = e DEAL::1.50 DEAL::2.25 +DEAL::1.50 DEAL::typeid(x).name() = e DEAL::typeid(NumberTraits::real_type).name() = e DEAL::-1.50 DEAL::2.25 +DEAL::1.50 -- 2.39.5