]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Testsuite: Add a test for mixed type operations
authorMatthias Maier <tamiko@43-1.org>
Tue, 1 Sep 2015 00:40:51 +0000 (19:40 -0500)
committerMatthias Maier <tamiko@43-1.org>
Mon, 7 Sep 2015 18:27:20 +0000 (13:27 -0500)
include/deal.II/base/tensor_base.h
tests/base/tensor_mixing_types_01.cc [new file with mode: 0644]
tests/base/tensor_mixing_types_01.output [new file with mode: 0644]

index 5778ac93f0ae085e998d873cbd1b7e2e2f184920..f3768ddadb7ea7fc6a1512eb9c450525166e2959 100644 (file)
@@ -137,7 +137,8 @@ public:
   /**
    * Constructor, where the data is copied from a C-style array.
    */
-  Tensor (const value_type &initializer);
+  template <typename OtherNumber>
+  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 <int dim, typename Number>
+template <typename OtherNumber>
 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 <int dim,
          typename = typename EnableIfScalar<OtherNumber>::type>
 inline
 Tensor<0,dim,typename ProductType<OtherNumber, Number>::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<Number>(t);
 }
diff --git a/tests/base/tensor_mixing_types_01.cc b/tests/base/tensor_mixing_types_01.cc
new file mode 100644 (file)
index 0000000..e82d6ff
--- /dev/null
@@ -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 <deal.II/base/tensor.h>
+
+#include<complex>
+
+int main ()
+{
+  initlog();
+
+  {
+    dealii::Tensor<0,3,float> f = 2.;
+    dealii::Tensor<0,3,double> d = 4.;
+    dealii::Tensor<0,3,std::complex<double> > 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<double> > 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<double> > 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 (file)
index 0000000..286b684
--- /dev/null
@@ -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!

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.