From 4682d7eae3cca57dee2797365f445582f4311437 Mon Sep 17 00:00:00 2001
From: Wolfgang Bangerth <bangerth@math.tamu.edu>
Date: Sat, 7 Nov 1998 02:13:12 +0000
Subject: [PATCH] More tensor work.

git-svn-id: https://svn.dealii.org/trunk@654 0785d39b-7218-0410-832d-ea1e28bc413d
---
 deal.II/base/include/base/function.h    |  8 ++---
 deal.II/base/include/base/tensor.h      | 42 +++++++++++++++++++++++++
 deal.II/base/include/base/tensor_base.h | 32 ++++++++++++++++++-
 deal.II/base/source/function.cc         | 12 +++----
 4 files changed, 83 insertions(+), 11 deletions(-)

diff --git a/deal.II/base/include/base/function.h b/deal.II/base/include/base/function.h
index c7a5687917..f992406d9c 100644
--- a/deal.II/base/include/base/function.h
+++ b/deal.II/base/include/base/function.h
@@ -116,7 +116,7 @@ class Function {
 				      * Return the gradient of the function
 				      * at the given point.
 				      */
-    virtual Point<dim> gradient (const Point<dim> &p) const;
+    virtual Tensor<1,dim> gradient (const Point<dim> &p) const;
 
 				     /**
 				      * Set #gradients# to the gradients of
@@ -126,7 +126,7 @@ class Function {
 				      * the same size as the #points# array.
 				      */
     virtual void gradient_list (const vector<Point<dim> > &points,
-				vector<Point<dim> >       &gradients) const;
+				vector<Tensor<1,dim> >    &gradients) const;
 
 				     /**
 				      * Return the value of the time variable/
@@ -203,7 +203,7 @@ class ZeroFunction : public Function<dim> {
 				      * Return the gradient of the function
 				      * at the given point.
 				      */
-    virtual Point<dim> gradient (const Point<dim> &p) const;
+    virtual Tensor<1,dim> gradient (const Point<dim> &p) const;
 
 				     /**
 				      * Set #gradients# to the gradients of
@@ -213,7 +213,7 @@ class ZeroFunction : public Function<dim> {
 				      * the same size as the #points# array.
 				      */
     virtual void gradient_list (const vector<Point<dim> > &points,
-				vector<Point<dim> >       &gradients) const;
+				vector<Tensor<1,dim> >    &gradients) const;
 };
 
 
diff --git a/deal.II/base/include/base/tensor.h b/deal.II/base/include/base/tensor.h
index de90959cdf..ac90f9920b 100644
--- a/deal.II/base/include/base/tensor.h
+++ b/deal.II/base/include/base/tensor.h
@@ -97,6 +97,20 @@ class Tensor {
 				      */
     Tensor<rank_,dim> & operator /= (const double &factor);
 
+				     /**
+				      *  Add two tensors. If possible, use
+				      *  #operator +=# instead since this does not
+				      *  need to copy a point at least once.
+				      */
+    Tensor<rank_,dim>   operator + (const Tensor<rank_,dim> &) const;
+
+				     /**
+				      *  Subtract two tensors. If possible, use
+				      *  #operator +=# instead since this does not
+				      *  need to copy a point at least once.
+				      */
+    Tensor<rank_,dim>   operator - (const Tensor<rank_,dim> &) const;
+
 				     /**
 				      * Reset all values to zero.
 				      */
@@ -212,6 +226,34 @@ Tensor<rank_,dim> & Tensor<rank_,dim>::operator /= (const double &s) {
 
 
 
+template <int rank_, int dim>
+inline
+Tensor<rank_,dim>
+Tensor<rank_,dim>::operator + (const Tensor<rank_,dim> &t) const {
+  Tensor<rank_,dim> tmp(*this);
+  
+  for (unsigned int i=0; i<dim; ++i)
+    tmp.subtensor[i] += t.subtensor[i];
+
+  return tmp;
+};
+
+
+
+template <int rank_, int dim>
+inline
+Tensor<rank_,dim>
+Tensor<rank_,dim>::operator - (const Tensor<rank_,dim> &t) const {
+  Tensor<rank_,dim> tmp(*this);
+  
+  for (unsigned int i=0; i<dim; ++i)
+    tmp.subtensor[i] -= t.subtensor[i];
+
+  return tmp;
+};
+
+
+
 template <int rank_, int dim>
 inline
 void Tensor<rank_,dim>::clear () {
diff --git a/deal.II/base/include/base/tensor_base.h b/deal.II/base/include/base/tensor_base.h
index 077c722c53..51f42ee7b0 100644
--- a/deal.II/base/include/base/tensor_base.h
+++ b/deal.II/base/include/base/tensor_base.h
@@ -126,7 +126,21 @@ class Tensor<1,dim> {
 				     /**
 				      *  Returns the scalar product of two vectors.
 				      */
-    double              operator * (const Tensor<1,dim> &) const;
+    double          operator * (const Tensor<1,dim> &) const;
+
+				     /**
+				      *  Add two tensors. If possible, use
+				      *  #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
+				      *  #operator +=# instead since this does not
+				      *  need to copy a point at least once.
+				      */
+    Tensor<1,dim>   operator - (const Tensor<1,dim> &) const;
 
 				     /**
 				      * Reset all values to zero.
@@ -285,6 +299,22 @@ double Tensor<1,dim>::operator * (const Tensor<1,dim> &p) const {
 
 
 
+template <int dim>
+inline
+Tensor<1,dim> Tensor<1,dim>::operator + (const Tensor<1,dim> &p) const {
+  return (Tensor<1,dim>(*this) += p);
+};
+
+
+
+template <int dim>
+inline
+Tensor<1,dim> Tensor<1,dim>::operator - (const Tensor<1,dim> &p) const {
+  return (Tensor<1,dim>(*this) -= p);
+};
+
+
+
 template <int dim>
 inline
 void Tensor<1,dim>::clear () {
diff --git a/deal.II/base/source/function.cc b/deal.II/base/source/function.cc
index a88413d677..3b7a77494b 100644
--- a/deal.II/base/source/function.cc
+++ b/deal.II/base/source/function.cc
@@ -38,7 +38,7 @@ void Function<dim>::value_list (const vector<Point<dim> > &points,
 
 
 template <int dim>
-Point<dim> Function<dim>::gradient (const Point<dim> &) const {
+Tensor<1,dim> Function<dim>::gradient (const Point<dim> &) const {
   Assert (false, ExcPureFunctionCalled());
   return Point<dim>();
 };
@@ -47,7 +47,7 @@ Point<dim> Function<dim>::gradient (const Point<dim> &) const {
 
 template <int dim>
 void Function<dim>::gradient_list (const vector<Point<dim> > &points,
-				   vector<Point<dim> >       &gradients) const {
+				   vector<Tensor<1,dim> >    &gradients) const {
   Assert (gradients.size() == points.size(),
 	  ExcVectorHasWrongSize(gradients.size(), points.size()));
 
@@ -104,19 +104,19 @@ void ZeroFunction<dim>::value_list (const vector<Point<dim> > &points,
 
 
 template <int dim>
-Point<dim> ZeroFunction<dim>::gradient (const Point<dim> &) const {
-  return Point<dim>();
+Tensor<1,dim> ZeroFunction<dim>::gradient (const Point<dim> &) const {
+  return Tensor<1,dim>();
 };
 
 
 
 template <int dim>
 void ZeroFunction<dim>::gradient_list (const vector<Point<dim> > &points,
-				       vector<Point<dim> >       &gradients) const {
+				       vector<Tensor<1,dim> >    &gradients) const {
   Assert (gradients.size() == points.size(),
 	  ExcVectorHasWrongSize(gradients.size(), points.size()));
 
-  fill_n (gradients.begin(), points.size(), Point<dim>());
+  gradients.clear ();
 };
 
 
-- 
2.39.5