From: Wolfgang Bangerth <bangerth@math.tamu.edu>
Date: Mon, 10 Jun 2002 08:14:58 +0000 (+0000)
Subject: Add scalar multiplication and division for tensors.
X-Git-Tag: v8.0.0~17915
X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d6b4911fc7d039185e236ec72f90fe9b5eefe1a3;p=dealii.git

Add scalar multiplication and division for tensors.


git-svn-id: https://svn.dealii.org/trunk@6035 0785d39b-7218-0410-832d-ea1e28bc413d
---

diff --git a/deal.II/base/include/base/tensor.h b/deal.II/base/include/base/tensor.h
index 1a1e2cadb5..771a26115c 100644
--- a/deal.II/base/include/base/tensor.h
+++ b/deal.II/base/include/base/tensor.h
@@ -1011,4 +1011,54 @@ invert (const Tensor<2,dim> &t)
 };
 
 
+
+/**
+ * Multiplication of a tensor of general rank with a scalar double
+ * from the right.
+ */
+template <int rank, int dim>
+inline
+Tensor<rank,dim>
+operator * (const Tensor<rank,dim> &t,
+	    const double            factor)
+{
+  Tensor<rank,dim> tt = t;
+  tt *= factor;
+  return tt;
+};
+
+
+
+/**
+ * Multiplication of a tensor of general rank with a scalar double
+ * from the left.
+ */
+template <int rank, int dim>
+inline
+Tensor<rank,dim>
+operator * (const double            factor,
+	    const Tensor<rank,dim> &t)
+{
+  Tensor<rank,dim> tt = t;
+  tt *= factor;
+  return tt;
+};
+
+
+
+/**
+ * Division of a tensor of general rank by a scalar double.
+ */
+template <int rank, int dim>
+inline
+Tensor<rank,dim>
+operator / (const Tensor<rank,dim> &t,
+	    const double            factor)
+{
+  Tensor<rank,dim> tt = t;
+  tt /= factor;
+  return tt;
+};
+
+
 #endif
diff --git a/deal.II/base/include/base/tensor_base.h b/deal.II/base/include/base/tensor_base.h
index 24eea78d27..c5b36825ec 100644
--- a/deal.II/base/include/base/tensor_base.h
+++ b/deal.II/base/include/base/tensor_base.h
@@ -85,7 +85,6 @@ class Tensor<1,dim>
 
     typedef double value_type;
     
-//TODO:[WB] Do we need this overflow? Maybe there is use for a zero-size Tensor?    
 				     /**
 				      * Declare an array type which can
 				      * be used to initialize statically
@@ -107,87 +106,99 @@ class Tensor<1,dim>
     explicit Tensor (const bool initialize = true);
 
 				     /**
-				      * Copy constructor, where the data is
-				      * copied from a C-style array.
+				      * Copy constructor, where the
+				      * data is copied from a C-style
+				      * array.
 				      */
     Tensor (const array_type &initializer);
     
     				     /**
-				      *  Copy constructor.
+				      * Copy constructor.
 				      */
     Tensor (const Tensor<1,dim> &);
 
 				     /**
-				      *  Read access to the @p{index}th coordinate.
+				      * Read access to the @p{index}th
+				      * coordinate.
 				      *
-				      * Note that the derived @p{Point} class also
-				      * provides access through the @p{()}
-				      * operator for backcompatibility.
+				      * Note that the derived
+				      * @p{Point} class also provides
+				      * access through the @p{()}
+				      * operator for
+				      * backcompatibility.
 				      */
     double   operator [] (const unsigned int index) const;
 
     				     /**
-				      *  Read and write access to the @p{index}th
-				      *  coordinate.
+				      * Read and write access to the
+				      * @p{index}th coordinate.
 				      *
-				      * Note that the derived @p{Point} class also
-				      * provides access through the @p{()}
-				      * operator for backcompatibility.
+				      * Note that the derived
+				      * @p{Point} class also provides
+				      * access through the @p{()}
+				      * operator for
+				      * backcompatibility.
 				      */
     double & operator [] (const unsigned int index);
 
 				     /**
-				      *  Assignment operator.
+				      * Assignment operator.
 				      */
     Tensor<1,dim> & operator = (const Tensor<1,dim> &);
 
 				     /**
-				      *  Test for equality of two points.
+				      * Test for equality of two points.
 				      */
     bool operator == (const Tensor<1,dim> &) const;
 
     				     /**
-				      *  Test for inequality of two points.
+				      * Test for inequality of two points.
 				      */
     bool operator != (const Tensor<1,dim> &) const;
 
 				     /**
-				      *  Add another vector, i.e. move this point by
-				      *  the given offset.
+				      * Add another vector, i.e. move
+				      * this point by the given
+				      * offset.
 				      */
     Tensor<1,dim> & operator += (const Tensor<1,dim> &);
 				     /**
-				      *  Subtract another vector.
+				      * Subtract another vector.
 				      */
     Tensor<1,dim> & operator -= (const Tensor<1,dim> &);
 
 				     /**
-				      *  Scale the vector by @p{factor}, i.e. multiply
-				      *  all coordinates by @p{factor}.
+				      * Scale the vector by
+				      * @p{factor}, i.e. multiply all
+				      * coordinates by @p{factor}.
 				      */
     Tensor<1,dim> & operator *= (const double &factor);
 
 				     /**
-				      *  Scale the vector by @p{1/factor}.
+				      * Scale the vector by @p{1/factor}.
 				      */
     Tensor<1,dim> & operator /= (const double &factor);
 
 				     /**
-				      *  Returns the scalar product of two vectors.
+				      * Returns the scalar product of
+				      * two vectors.
 				      */
     double          operator * (const Tensor<1,dim> &) const;
 
 				     /**
-				      *  Add two tensors. If possible, use
-				      *  @p{operator +=} instead since this does not
-				      *  need to copy a point at least once.
+				      * Add two tensors. If possible,
+				      * use @p{operator +=} instead
+				      * since this does not need to
+				      * copy a point at least once.
 				      */
     Tensor<1,dim>   operator + (const Tensor<1,dim> &) const;
 
 				     /**
-				      *  Subtract two tensors. If possible, use
-				      *  @p{operator +=} instead since this does not
-				      *  need to copy a point at least once.
+				      * Subtract two tensors. If
+				      * possible, use @p{operator +=}
+				      * instead since this does not
+				      * need to copy a point at least
+				      * once.
 				      */
     Tensor<1,dim>   operator - (const Tensor<1,dim> &) const;
 
@@ -222,12 +233,13 @@ class Tensor<1,dim>
 
   private:
 				     /**
-				      * Store the values in a simple array.
-				      * For @p{dim==0} store one element, because
-				      * otherways the compiler would choke.
-				      * We catch this case in the constructor
-				      * to disallow the creation of such
-				      * an object.
+				      * Store the values in a simple
+				      * array.  For @p{dim==0} store
+				      * one element, because otherways
+				      * the compiler would choke.  We
+				      * catch this case in the
+				      * constructor to disallow the
+				      * creation of such an object.
 				      */
     double values[(dim!=0) ? (dim) : 1];
 
@@ -552,4 +564,58 @@ std::ostream & operator << (std::ostream &out, const Tensor<1,1> &p)
 };
 
 
+
+/**
+ * Multiplication of a tensor of rank 1 with a scalar double from the right.
+ */
+template <int dim>
+inline
+Tensor<1,dim>
+operator * (const Tensor<1,dim> &t,
+	    const double         factor)
+{
+  Tensor<1,dim> tt;
+  for (unsigned int d=0; d<dim; ++d)
+    tt[d] = t[d] * factor;
+  return tt;
+};
+
+
+
+/**
+ * Multiplication of a tensor of rank 1 with a scalar double from the left.
+ */
+template <int dim>
+inline
+Tensor<1,dim>
+operator * (const double         factor,
+	    const Tensor<1,dim> &t)
+{
+  Tensor<1,dim> tt;
+  for (unsigned int d=0; d<dim; ++d)
+    tt[d] = t[d] * factor;
+  return tt;
+};
+
+
+
+/**
+ * Division of a tensor of rank 1 by a scalar double.
+ */
+template <int dim>
+inline
+Tensor<1,dim>
+operator / (const Tensor<1,dim> &t,
+	    const double         factor)
+{
+  Tensor<1,dim> tt;
+  for (unsigned int d=0; d<dim; ++d)
+    tt[d] = t[d] / factor;
+  return tt;
+};
+
+
+
 #endif
+
+