]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Further work on the tensor classes.
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 6 Nov 1998 19:05:02 +0000 (19:05 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 6 Nov 1998 19:05:02 +0000 (19:05 +0000)
git-svn-id: https://svn.dealii.org/trunk@651 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/point.h
deal.II/base/include/base/tensor.h
deal.II/base/include/base/tensor_base.h
deal.II/base/source/log.cc

index a546fe6ca0bfcb23d4413da5bd4f64cac89dc10c..c8b1a3e2e10d49b25779c13d3fbbfedb14d6b92d 100644 (file)
@@ -43,6 +43,13 @@ class Point : public Tensor<1,dim> {
                                      * to zero.
                                      */
     explicit Point ();
+
+                                    /**
+                                     * Convert a tensor to a point. Since no
+                                     * additional data is inside a point,
+                                     * this is ok.
+                                     */
+    Point (const Tensor<1,dim> &);
     
                                     /**
                                      *  Constructor for one dimensional points. This
@@ -103,6 +110,11 @@ class Point : public Tensor<1,dim> {
                                      */
     Point<dim>   operator * (const double) const;
 
+                                    /**
+                                     *  Returns the scalar product of two vectors.
+                                     */
+    double       operator * (const Point<dim> &) const;
+
                                     /**
                                      *  Divide by a factor. If possible, use
                                      *  #operator /=# instead since this does not
@@ -110,32 +122,6 @@ class Point : public Tensor<1,dim> {
                                      */
     Point<dim>   operator / (const double) const;
 
-                                    /**
-                                     *  Add another vector, i.e. move this point by
-                                     *  the given offset.
-                                     */
-    Point<dim> & operator += (const Point<dim> &);
-                                    /**
-                                     *  Subtract another vector.
-                                     */
-    Point<dim> & operator -= (const Point<dim> &);
-
-                                    /**
-                                     *  Scale the vector by #factor#, i.e. multiply
-                                     *  all coordinates by #factor#.
-                                     */
-    Point<dim> & operator *= (const double &factor);
-
-                                    /**
-                                     *  Scale the vector by #1/factor#.
-                                     */
-    Point<dim> & operator /= (const double &factor);
-
-                                    /**
-                                     *  Returns the scalar product of two vectors.
-                                     */
-    double              operator * (const Point<dim> &) const;
-
                                     /**
                                      *  Returns the scalar product of this point
                                      *  vector with itself, i.e. the square, or
@@ -174,6 +160,12 @@ Point<dim>::Point () :
                Tensor<1,dim>() {};
 
 
+template <int dim>
+inline
+Point<dim>::Point (const Tensor<1,dim> &t) :
+               Tensor<1,dim>(t) {};
+
+
 
 template <>
 inline
@@ -246,71 +238,29 @@ Point<dim> Point<dim>::operator * (const double factor) const {
 
 template <int dim>
 inline
-Point<dim> operator * (const double factor, const Point<dim> &p) {
-  return p*factor;
-};
-
-
-
-template <int dim>
-inline
-Point<dim> Point<dim>::operator / (const double factor) const {
-  return (Point<dim>(*this) /= factor);
-};
-
-
-  
-template <int dim>
-inline
-Point<dim> & Point<dim>::operator += (const Point<dim> &p) {
-  for (unsigned int i=0; i<dim; ++i)
-    values[i] += p.values[i];
-  return *this;
-};
-
-
-
-template <int dim>
-inline
-Point<dim> & Point<dim>::operator -= (const Point<dim> &p) {
-  for (unsigned int i=0; i<dim; ++i)
-    values[i] -= p.values[i];
-  return *this;
-};
-
-
-
-template <int dim>
-inline
-Point<dim> & Point<dim>::operator *= (const double &s) {
-  for (unsigned int i=0; i<dim; ++i)
-    values[i] *= s;
-  return *this;
+double Point<dim>::operator * (const Point<dim> &p) const {
+                                  // simply pass down
+  return Tensor<1,dim>::operator * (p);
 };
 
 
 
 template <int dim>
 inline
-Point<dim> & Point<dim>::operator /= (const double &s) {
-  for (unsigned int i=0; i<dim; ++i)
-    values[i] /= s;
-  return *this;
+Point<dim> operator * (const double factor, const Point<dim> &p) {
+  return p*factor;
 };
 
 
 
 template <int dim>
 inline
-double Point<dim>::operator * (const Point<dim> &p) const {
-  double q=0;
-  for (unsigned int i=0; i<dim; ++i)
-    q += values[i] * p.values[i];
-  return q;
+Point<dim> Point<dim>::operator / (const double factor) const {
+  return (Point<dim>(*this) /= factor);
 };
 
 
-
+  
 template <int dim>
 inline
 double Point<dim>::square () const {
index 47951acbb37d5d9bc9c7d7ac56f24a3fb6a25b8e..de90959cdf98176e53a0a786c4badd896dfe9e59 100644 (file)
@@ -66,15 +66,37 @@ class Tensor {
     Tensor & operator = (const Tensor<rank_,dim> &);
 
                                     /**
-                                     *  Test for equality of two points.
+                                     *  Test for equality of two tensors.
                                      */
     bool operator == (const Tensor<rank_,dim> &) const;
 
                                     /**
-                                     *  Test for inequality of two points.
+                                     *  Test for inequality of two tensors.
                                      */
     bool operator != (const Tensor<rank_,dim> &) const;
 
+                                    /**
+                                     *  Add another vector, i.e. move this tensor by
+                                     *  the given offset.
+                                     */
+    Tensor<rank_,dim> & operator += (const Tensor<rank_,dim> &);
+    
+                                    /**
+                                     *  Subtract another vector.
+                                     */
+    Tensor<rank_,dim> & operator -= (const Tensor<rank_,dim> &);
+
+                                    /**
+                                     *  Scale the vector by #factor#, i.e. multiply
+                                     *  all coordinates by #factor#.
+                                     */
+    Tensor<rank_,dim> & operator *= (const double &factor);
+
+                                    /**
+                                     *  Scale the vector by #1/factor#.
+                                     */
+    Tensor<rank_,dim> & operator /= (const double &factor);
+
                                     /**
                                      * Reset all values to zero.
                                      */
@@ -126,7 +148,7 @@ template <int rank_, int dim>
 inline
 Tensor<rank_,dim> & Tensor<rank_,dim>::operator = (const Tensor<rank_,dim> &t) {
   for (unsigned int i=0; i<dim; ++i)
-    values[i] = t.values[i];
+    subtensor[i] = t.subtensor[i];
   return *this;
 };
 
@@ -136,7 +158,7 @@ template <int rank_, int dim>
 inline
 bool Tensor<rank_,dim>::operator == (const Tensor<rank_,dim> &p) const {
   for (unsigned int i=0; i<dim; ++i)
-    if (values[i] != p.values[i]) return false;
+    if (subtensor[i] != p.subtensor[i]) return false;
   return true;
 };
 
@@ -150,6 +172,46 @@ bool Tensor<rank_,dim>::operator != (const Tensor<rank_,dim> &p) const {
 
 
 
+template <int rank_, int dim>
+inline
+Tensor<rank_,dim> & Tensor<rank_,dim>::operator += (const Tensor<rank_,dim> &p) {
+  for (unsigned int i=0; i<dim; ++i)
+    subtensor[i] += p.subtensor[i];
+  return *this;
+};
+
+
+
+template <int rank_, int dim>
+inline
+Tensor<rank_,dim> & Tensor<rank_,dim>::operator -= (const Tensor<rank_,dim> &p) {
+  for (unsigned int i=0; i<dim; ++i)
+    subtensor[i] -= p.subtensor[i];
+  return *this;
+};
+
+
+
+template <int rank_, int dim>
+inline
+Tensor<rank_,dim> & Tensor<rank_,dim>::operator *= (const double &s) {
+  for (unsigned int i=0; i<dim; ++i)
+    subtensor[i] *= s;
+  return *this;
+};
+
+
+
+template <int rank_, int dim>
+inline
+Tensor<rank_,dim> & Tensor<rank_,dim>::operator /= (const double &s) {
+  for (unsigned int i=0; i<dim; ++i)
+    subtensor[i] /= s;
+  return *this;
+};
+
+
+
 template <int rank_, int dim>
 inline
 void Tensor<rank_,dim>::clear () {
@@ -165,6 +227,29 @@ void Tensor<rank_,dim>::clear () {
 
 
 
+/*  Exception class. This is certainly not the best possible place for its
+    declaration, but at present, local classes to any of Tensor<> can't
+    be properly accessed (haven't investigated why). If anyone has a better
+    idea, realize it!
+*/
+DeclException1 (ExcInvalidTensorIndex,
+               int,
+               << "Invalid tensor index " << arg1);
+
+
+
+template <int dim>
+inline
+void contract (Tensor<1,dim>       &dest,
+              const Tensor<2,dim> &src1,
+              const Tensor<1,dim> &src2) {
+  dest.clear ();
+  for (unsigned int i=0; i<dim; ++i)
+    for (unsigned int j=0; j<dim; ++j)
+      dest[i] += src1[i][j] * src2[j];
+};
+
+
 
 template <int dim>
 inline
@@ -180,6 +265,100 @@ void contract (Tensor<2,dim>       &dest,
 
 
 
+template <int dim>
+inline
+void contract (Tensor<2,dim>       &dest,
+              const Tensor<2,dim> &src1,   const unsigned int index1,
+              const Tensor<2,dim> &src2,   const unsigned int index2) {
+  dest.clear ();
+
+  switch (index1)
+    {
+      case 1:
+           switch (index2)
+             {
+               case 1:
+                     for (unsigned int i=0; i<dim; ++i)
+                       for (unsigned int j=0; j<dim; ++j)
+                         for (unsigned int k=0; k<dim; ++k)
+                           dest[i][j] += src1[k][i] * src2[k][j];
+                     break;
+               case 2:
+                     for (unsigned int i=0; i<dim; ++i)
+                       for (unsigned int j=0; j<dim; ++j)
+                         for (unsigned int k=0; k<dim; ++k)
+                           dest[i][j] += src1[k][i] * src2[j][k];
+                     break;
+
+               default:
+                     Assert (false, ExcInvalidTensorIndex (index2));
+             };
+           break;
+      case 2:
+           switch (index2)
+             {
+               case 1:
+                     for (unsigned int i=0; i<dim; ++i)
+                       for (unsigned int j=0; j<dim; ++j)
+                         for (unsigned int k=0; k<dim; ++k)
+                           dest[i][j] += src1[i][k] * src2[k][j];
+                     break;
+               case 2:
+                     for (unsigned int i=0; i<dim; ++i)
+                       for (unsigned int j=0; j<dim; ++j)
+                         for (unsigned int k=0; k<dim; ++k)
+                           dest[i][j] += src1[i][k] * src2[j][k];
+                     break;
+
+               default:
+                     Assert (false, ExcInvalidTensorIndex (index2));
+             };
+           break;
+
+      default:
+           Assert (false, ExcInvalidTensorIndex (index1));
+    };
+};
+
+
+
+template <int dim>
+inline
+void contract (Tensor<2,dim>       &dest,
+              const Tensor<3,dim> &src1,   const unsigned int index1,
+              const Tensor<1,dim> &src2) {
+  dest.clear ();
+
+  switch (index1)
+    {
+      case 1:
+           for (unsigned int i=0; i<dim; ++i)
+             for (unsigned int j=0; j<dim; ++j)
+               for (unsigned int k=0; k<dim; ++k)
+                 dest[i][j] += src1[k][i][j] * src2[k];
+           break;
+
+      case 2:
+           for (unsigned int i=0; i<dim; ++i)
+             for (unsigned int j=0; j<dim; ++j)
+               for (unsigned int k=0; k<dim; ++k)
+                 dest[i][j] += src1[i][k][j] * src2[k];
+           break;
+
+      case 3:
+           for (unsigned int i=0; i<dim; ++i)
+             for (unsigned int j=0; j<dim; ++j)
+               for (unsigned int k=0; k<dim; ++k)
+                 dest[i][j] += src1[i][j][k] * src2[k];
+           break;
+
+      default:
+           Assert (false, ExcInvalidTensorIndex (index1));
+    };
+};
+
+
+
 template <int dim>
 inline
 void contract (Tensor<3,dim>       &dest,
@@ -224,7 +403,14 @@ void contract (Tensor<4,dim>       &dest,
            dest[i][j][k][l] += src1[i][j][m] * src2[m][k][l];
 };
 
-    
+
+
+inline
+double determinant (const Tensor<2,2> &t) {
+  return ((t[0][0] * t[1][1]) -
+         (t[1][0] * t[0][1]));
+};
+
     
     
     
index 2c775d119de4db2fb80a04896fb35585490f05cd..077c722c53217d39cd6fa250cfc6576cf4659423 100644 (file)
@@ -102,6 +102,32 @@ class Tensor<1,dim> {
                                      */
     bool operator != (const Tensor<1,dim> &) const;
 
+                                    /**
+                                     *  Add another vector, i.e. move this point by
+                                     *  the given offset.
+                                     */
+    Tensor<1,dim> & operator += (const Tensor<1,dim> &);
+                                    /**
+                                     *  Subtract another vector.
+                                     */
+    Tensor<1,dim> & operator -= (const Tensor<1,dim> &);
+
+                                    /**
+                                     *  Scale the vector by #factor#, i.e. multiply
+                                     *  all coordinates by #factor#.
+                                     */
+    Tensor<1,dim> & operator *= (const double &factor);
+
+                                    /**
+                                     *  Scale the vector by #1/factor#.
+                                     */
+    Tensor<1,dim> & operator /= (const double &factor);
+
+                                    /**
+                                     *  Returns the scalar product of two vectors.
+                                     */
+    double              operator * (const Tensor<1,dim> &) const;
+
                                     /**
                                      * Reset all values to zero.
                                      */
@@ -208,6 +234,57 @@ bool 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) {
+  for (unsigned int i=0; i<dim; ++i)
+    values[i] += p.values[i];
+  return *this;
+};
+
+
+
+template <int dim>
+inline
+Tensor<1,dim> & Tensor<1,dim>::operator -= (const Tensor<1,dim> &p) {
+  for (unsigned int i=0; i<dim; ++i)
+    values[i] -= p.values[i];
+  return *this;
+};
+
+
+
+template <int dim>
+inline
+Tensor<1,dim> & Tensor<1,dim>::operator *= (const double &s) {
+  for (unsigned int i=0; i<dim; ++i)
+    values[i] *= s;
+  return *this;
+};
+
+
+
+template <int dim>
+inline
+Tensor<1,dim> & Tensor<1,dim>::operator /= (const double &s) {
+  for (unsigned int i=0; i<dim; ++i)
+    values[i] /= s;
+  return *this;
+};
+
+
+
+template <int dim>
+inline
+double Tensor<1,dim>::operator * (const Tensor<1,dim> &p) const {
+  double q=0;
+  for (unsigned int i=0; i<dim; ++i)
+    q += values[i] * p.values[i];
+  return q;
+};
+
+
+
 template <int dim>
 inline
 void Tensor<1,dim>::clear () {
index 6707f31f2bcb671fc7bd36cd88fe5c512664c5ac..520acdd938ad4ce1434da79413312071afc0a427 100644 (file)
@@ -3,9 +3,12 @@
 #include <base/logstream.h>
 #include <base/jobidentifier.h>
 
+
+
 LogStream deallog;
 
 
+
 LogStream::LogStream()
                : std(&cerr), file(0), was_endl(true),
                  std_depth(10000), file_depth(10000)

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.