From: wolf Date: Fri, 6 Nov 1998 12:07:32 +0000 (+0000) Subject: Make the tensor class more complete. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0dc9bfec64bc59de71b8bdac3fd865e9b57c24a3;p=dealii-svn.git Make the tensor class more complete. git-svn-id: https://svn.dealii.org/trunk@637 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/tensor.h b/deal.II/base/include/base/tensor.h index df5882797c..47951acbb3 100644 --- a/deal.II/base/include/base/tensor.h +++ b/deal.II/base/include/base/tensor.h @@ -9,21 +9,38 @@ +/** + * Provide a general tensor class with an arbitrary rank, i.e. with + * an arbitrary number of indices. The Tensor class provides an + * indexing operator and a bit of infrastructure, but most + * functionality is recursively handed down to tensors of rank 1 or + * put into external templated functions, e.g. the #contract# family. + * + * Using this tensor class for objects of rank to has advantages over + * matrices in many cases since the dimension is known to the compiler + * as well as the location of the data. It is therefore possible to + * produce far more efficient code than for matrices with + * runtime-dependant dimension. + */ template class Tensor { public: /** - * Provide a way to get the dimension of - * an object without explicit knowledge - * of it's data type. Implementation is - * this way instead of providing a function - * #dimension()# because now it is possible - * to get the dimension at compile time - * without the expansion and preevaluation - * of an inlined function; the compiler may - * therefore produce more efficient code - * and you may use this value to declare - * other data types. + * Provide a way to get the + * dimension of an object without + * explicit knowledge of it's + * data type. Implementation is + * this way instead of providing + * a function #dimension()# + * because now it is possible to + * get the dimension at compile + * time without the expansion and + * preevaluation of an inlined + * function; the compiler may + * therefore produce more + * efficient code and you may use + * this value to declare other + * data types. */ static const unsigned int dimension = dim; @@ -43,11 +60,26 @@ class Tensor { */ const Tensor &operator [] (const unsigned int i) const; + /** + * Assignment operator. + */ + Tensor & operator = (const Tensor &); + + /** + * Test for equality of two points. + */ + bool operator == (const Tensor &) const; + + /** + * Test for inequality of two points. + */ + bool operator != (const Tensor &) const; + /** * Reset all values to zero. */ void clear (); - + /** * Exception @@ -60,19 +92,18 @@ class Tensor { * Array of tensors holding the * subelements. */ - Tensor subtensor[dim]; + Tensor subtensor[dim]; }; - /*--------------------------- Inline functions -----------------------------*/ -template +template inline -Tensor & -Tensor::operator[] (const unsigned int i) { +Tensor & +Tensor::operator[] (const unsigned int i) { Assert (i::operator[] (const unsigned int i) { -template +template inline -const Tensor & -Tensor::operator[] (const unsigned int i) const { +const Tensor & +Tensor::operator[] (const unsigned int i) const { Assert (i::operator[] (const unsigned int i) const { -template +template +inline +Tensor & Tensor::operator = (const Tensor &t) { + for (unsigned int i=0; i +inline +bool Tensor::operator == (const Tensor &p) const { + for (unsigned int i=0; i +inline +bool Tensor::operator != (const Tensor &p) const { + return !((*this) == p); +}; + + + +template inline -void Tensor::clear () { +void Tensor::clear () { for (unsigned int i=0; i +#include @@ -20,22 +21,33 @@ template class Tensor; - +/** + * This class is a specialized version of the #Tensor# class. + * It handles tensors with one index, i.e. vectors, of fixed dimension + * and offers the functionality needed for tensors of higher rank. + * + * In many cases, you will want to use the more specialized #Point# class + * which acts as a tensor of rank one but has more functionality. + */ template class Tensor<1,dim> { public: /** - * Provide a way to get the dimension of - * an object without explicit knowledge - * of it's data type. Implementation is - * this way instead of providing a function - * #dimension()# because now it is possible - * to get the dimension at compile time - * without the expansion and preevaluation - * of an inlined function; the compiler may - * therefore produce more efficient code - * and you may use this value to declare - * other data types. + * Provide a way to get the + * dimension of an object without + * explicit knowledge of it's + * data type. Implementation is + * this way instead of providing + * a function #dimension()# + * because now it is possible to + * get the dimension at compile + * time without the expansion and + * preevaluation of an inlined + * function; the compiler may + * therefore produce more + * efficient code and you may use + * this value to declare other + * data types. */ static const unsigned int dimension = dim; @@ -76,23 +88,25 @@ class Tensor<1,dim> { double & operator [] (const unsigned int index); /** - * Reset all values to zero. + * Assignment operator. */ - void clear (); - + Tensor<1,dim> & operator = (const Tensor<1,dim> &); + /** - * Exception + * Test for equality of two points. + */ + bool operator == (const Tensor<1,dim> &) const; + + /** + * Test for inequality of two points. */ - DeclException1 (ExcDimTooSmall, - int, - << "Given dimensions must be >= 1, but was " << arg1); + bool operator != (const Tensor<1,dim> &) const; /** - * Exception + * Reset all values to zero. */ - DeclException1 (ExcInvalidIndex, - int, - << "Invalid index " << arg1); + void clear (); + protected: /** * Stores the values in a simple array. @@ -103,6 +117,28 @@ class Tensor<1,dim> { +DeclException1 (ExcDimTooSmall, + int, + << "Given dimensions must be >= 1, but was " << arg1); +DeclException1 (ExcInvalidIndex, + int, + << "Invalid index " << arg1); + + + + + /** + * Prints the values of this point in the + * form #x1 x2 x3 etc#. + */ +template +ostream & operator << (ostream &out, const Tensor<1,dim> &p); + + + + + + /*------------------------------- Inline functions: Tensor ---------------------------*/ @@ -144,6 +180,34 @@ double & Tensor<1,dim>::operator [] (const unsigned int index) { +template +inline +Tensor<1,dim> & Tensor<1,dim>::operator = (const Tensor<1,dim> &p) { + for (unsigned int i=0; i +inline +bool Tensor<1,dim>::operator == (const Tensor<1,dim> &p) const { + for (unsigned int i=0; i +inline +bool Tensor<1,dim>::operator != (const Tensor<1,dim> &p) const { + return !((*this) == p); +}; + + + template inline void Tensor<1,dim>::clear () { @@ -154,6 +218,38 @@ void Tensor<1,dim>::clear () { + + +template +inline +ostream & operator << (ostream &out, const Tensor<1,dim> &p) { + for (unsigned int i=0; i +inline +ostream & operator << (ostream &out, const Tensor<1,1> &p) { + out << p[0]; + + if (!out) + throw GlobalExcIO (); + + return out; +}; + + + + + + /*---------------------------- tensor_base.h ---------------------------*/ /* end of #ifndef __tensor_base_H */ #endif