From fb8bfaf27d18e83c14989d4f01af0629498eb1d5 Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Mon, 31 Aug 2015 19:40:51 -0500 Subject: [PATCH] Testsuite: Add a test for mixed type operations --- include/deal.II/base/tensor_base.h | 10 +- tests/base/tensor_mixing_types_01.cc | 112 +++++++++++++++++++++++ tests/base/tensor_mixing_types_01.output | 34 +++++++ 3 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 tests/base/tensor_mixing_types_01.cc create mode 100644 tests/base/tensor_mixing_types_01.output diff --git a/include/deal.II/base/tensor_base.h b/include/deal.II/base/tensor_base.h index 5778ac93f0..f3768ddadb 100644 --- a/include/deal.II/base/tensor_base.h +++ b/include/deal.II/base/tensor_base.h @@ -137,7 +137,8 @@ public: /** * Constructor, where the data is copied from a C-style array. */ - Tensor (const value_type &initializer); + template + Tensor (const OtherNumber initializer); /** * Conversion to Number. Since rank-0 tensors are scalars, this is a natural @@ -636,8 +637,9 @@ Tensor<0,dim,Number>::Tensor (const Tensor<0,dim,Number> &p) template +template inline -Tensor<0,dim,Number>::Tensor (const value_type &initializer) +Tensor<0,dim,Number>::Tensor (const OtherNumber initializer) { Assert (dim>0, ExcDimTooSmall(dim)); @@ -850,8 +852,8 @@ template ::type> inline Tensor<0,dim,typename ProductType::type> -operator * (const Number factor, - const Tensor<0,dim,OtherNumber> &t) +operator * (const OtherNumber factor, + const Tensor<0,dim,Number> &t) { return factor * static_cast(t); } diff --git a/tests/base/tensor_mixing_types_01.cc b/tests/base/tensor_mixing_types_01.cc new file mode 100644 index 0000000000..e82d6ffbaf --- /dev/null +++ b/tests/base/tensor_mixing_types_01.cc @@ -0,0 +1,112 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 1998 - 2015 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. +// +// --------------------------------------------------------------------- + + + +#include "../tests.h" +#include + +#include + +int main () +{ + initlog(); + + { + dealii::Tensor<0,3,float> f = 2.; + dealii::Tensor<0,3,double> d = 4.; + dealii::Tensor<0,3,std::complex > c = 8.; + + deallog << f + d << std::endl; + deallog << f - d << std::endl; + deallog << f * d << std::endl; + + deallog << d + c << std::endl; + deallog << d - c << std::endl; + deallog << d * c << std::endl; + + float f_scalar = 10.; + deallog << (d * f_scalar) << std::endl; + deallog << (d / f_scalar) << std::endl; + deallog << (f_scalar * d) << std::endl; + deallog << (d *= f_scalar) << std::endl; + deallog << (d /= f_scalar) << std::endl; + + double d_scalar = 10.; + deallog << (c * d_scalar) << std::endl; + deallog << (c / d_scalar) << std::endl; + deallog << (d_scalar * c) << std::endl; + deallog << (c *= d_scalar) << std::endl; + deallog << (c /= d_scalar) << std::endl; + } + + { + dealii::Tensor<1,2,float> f = { {1., 2.} }; + dealii::Tensor<1,2,double> d = { {4., 8.} }; + dealii::Tensor<1,2,std::complex > c = { {16., 32.} }; + + deallog << f + d << std::endl; + deallog << f - d << std::endl; + deallog << f * d << std::endl; + + deallog << d + c << std::endl; + deallog << d - c << std::endl; + deallog << d * c << std::endl; + + float f_scalar = 10.; + deallog << (d * f_scalar) << std::endl; + deallog << (d / f_scalar) << std::endl; + deallog << (f_scalar * d) << std::endl; + deallog << (d *= f_scalar) << std::endl; + deallog << (d /= f_scalar) << std::endl; + + double d_scalar = 10.; + deallog << (c * d_scalar) << std::endl; + deallog << (c / d_scalar) << std::endl; + deallog << (d_scalar * c) << std::endl; + deallog << (c *= d_scalar) << std::endl; + deallog << (c /= d_scalar) << std::endl; + } + + { + dealii::Tensor<1,3,float> f = { {1., 2., 4.} }; + dealii::Tensor<1,3,double> d = { {4., 8., 16.} }; + dealii::Tensor<1,3,std::complex > c = { {32., 64., 128.} }; + + deallog << f + d << std::endl; + deallog << f - d << std::endl; + deallog << f * d << std::endl; + + deallog << d + c << std::endl; + deallog << d - c << std::endl; + deallog << d * c << std::endl; + + float f_scalar = 10.; + deallog << (d * f_scalar) << std::endl; + deallog << (d / f_scalar) << std::endl; + deallog << (f_scalar * d) << std::endl; + deallog << (d *= f_scalar) << std::endl; + deallog << (d /= f_scalar) << std::endl; + + double d_scalar = 10.; + deallog << (c * d_scalar) << std::endl; + deallog << (c / d_scalar) << std::endl; + deallog << (d_scalar * c) << std::endl; + deallog << (c *= d_scalar) << std::endl; + deallog << (c /= d_scalar) << std::endl; + } + + deallog << "OK!" << std::endl; +} diff --git a/tests/base/tensor_mixing_types_01.output b/tests/base/tensor_mixing_types_01.output new file mode 100644 index 0000000000..286b6841d7 --- /dev/null +++ b/tests/base/tensor_mixing_types_01.output @@ -0,0 +1,34 @@ + +DEAL::6.00000 +DEAL::-2.00000 +DEAL::8.00000 +DEAL::(12.0000,0.00000) +DEAL::(-4.00000,0.00000) +DEAL::(32.0000,0.00000) +DEAL::40.0000 +DEAL::0.400000 +DEAL::40.0000 +DEAL::40.0000 +DEAL::4.00000 +DEAL::(80.0000,0.00000) +DEAL::(0.800000,0.00000) +DEAL::(80.0000,0.00000) +DEAL::(80.0000,0.00000) +DEAL::(8.00000,0.00000) +DEAL::5.00000 10.0000 +DEAL::-3.00000 -6.00000 +DEAL::20.0000 +DEAL::(20.0000,0.00000) (40.0000,0.00000) +DEAL::(-12.0000,0.00000) (-24.0000,0.00000) +DEAL::(320.000,0.00000) +DEAL::40.0000 80.0000 +DEAL::0.400000 0.800000 +DEAL::40.0000 80.0000 +DEAL::40.0000 80.0000 +DEAL::4.00000 8.00000 +DEAL::(160.000,0.00000) (320.000,0.00000) +DEAL::(1.60000,0.00000) (3.20000,0.00000) +DEAL::(160.000,0.00000) (320.000,0.00000) +DEAL::(160.000,0.00000) (320.000,0.00000) +DEAL::(16.0000,0.00000) (32.0000,0.00000) +DEAL::OK! -- 2.39.5