From f797cd9bf83da1d5a1570aae1450c894dc6fcc4a Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Tue, 1 Sep 2015 22:18:22 -0500 Subject: [PATCH] Tensor: Finish interface cleanup --- include/deal.II/base/tensor.h | 152 ++++++++++++++++++----------- include/deal.II/base/tensor_base.h | 21 ++-- 2 files changed, 110 insertions(+), 63 deletions(-) diff --git a/include/deal.II/base/tensor.h b/include/deal.II/base/tensor.h index 10cc8b9bbc..32eaae6b8b 100644 --- a/include/deal.II/base/tensor.h +++ b/include/deal.II/base/tensor.h @@ -81,7 +81,7 @@ public: /** * Publish the rank of this tensor to the outside world. */ - static const unsigned int rank = rank_; + static const unsigned int rank = rank_; /** * Number of independent components of a tensor of current rank. This is dim @@ -112,17 +112,24 @@ public: typedef typename Tensor::array_type array_type[dim]; /** - * Constructor. Initialize all entries to zero. + * Constructor. Initialize all entries to zero if + * initialize==true; this is the default behaviour. */ - Tensor (); + explicit + Tensor (const bool initialize = true); /** - * Copy constructor, where the data is copied from a C-style array. + * Copy constructor. + */ + Tensor (const Tensor &initializer); + + /** + * Constructor, where the data is copied from a C-style array. */ Tensor (const array_type &initializer); /** - * Copy constructor from tensors with different underlying scalar type. This + * Constructor from tensors with different underlying scalar type. This * obviously requires that the @p OtherNumber type is convertible to @p * Number. */ @@ -130,14 +137,16 @@ public: Tensor (const Tensor &initializer); /** - * Conversion operator from tensor of tensors. + * Constructor that converts from a "tensor of tensors". */ - Tensor (const Tensor<1,dim,Tensor > &initializer); + template + Tensor (const Tensor<1,dim,Tensor > &initializer); /** * Conversion operator to tensor of tensors. */ - operator Tensor<1,dim,Tensor > () const; + template + operator Tensor<1,dim,Tensor > () const; /** * Read-Write access operator. @@ -152,15 +161,15 @@ public: /** * Read access using TableIndices indices */ - Number operator [](const TableIndices &indices) const; + Number operator [] (const TableIndices &indices) const; /** * Read and write access using TableIndices indices */ - Number &operator [](const TableIndices &indices); + Number &operator [] (const TableIndices &indices); /** - * Assignment operator. + * Copy assignment operator. */ Tensor &operator = (const Tensor &rhs); @@ -177,39 +186,49 @@ public: * exactly it means to assign a scalar value to a tensor, zero is the only * value allowed for d, allowing the intuitive notation * t=0 to reset all elements of the tensor to zero. + * + * @relates EnableIfScalar */ - Tensor &operator = (const Number d); + template ::type> + Tensor &operator = (const OtherNumber d); /** * Test for equality of two tensors. */ - bool operator == (const Tensor &) const; + template + bool operator == (const Tensor &) const; /** * Test for inequality of two tensors. */ - bool operator != (const Tensor &) const; + template + bool operator != (const Tensor &) const; /** * Add another tensor. */ - Tensor &operator += (const Tensor &); + template + Tensor &operator += (const Tensor &); /** * Subtract another tensor. */ - Tensor &operator -= (const Tensor &); + template + Tensor &operator -= (const Tensor &); /** * Scale the tensor by factor, i.e. multiply all components by * factor. */ - Tensor &operator *= (const Number factor); + template + Tensor &operator *= (const OtherNumber factor); /** * Scale the vector by 1/factor. */ - Tensor &operator /= (const Number factor); + template + Tensor &operator /= (const OtherNumber factor); /** * Unary minus operator. Negate all entries of a tensor. @@ -238,8 +257,8 @@ public: * vector. As usual in C++, the rightmost index of the tensor marches * fastest. */ - template - void unroll (Vector &result) const; + template + void unroll (Vector &result) const; /** * Returns an unrolled index in the range [0,dim^rank-1] for the element of @@ -256,8 +275,6 @@ public: static TableIndices unrolled_to_component_indices(const unsigned int i); - - /** * Reset all values to zero. * @@ -303,8 +320,8 @@ private: /** * Help function for unroll. */ - template - void unroll_recursion(Vector &result, + template + void unroll_recursion(Vector &result, unsigned int &start_index) const; // make the following class a @@ -327,11 +344,24 @@ private: template inline -Tensor::Tensor () +Tensor::Tensor (const bool initialize) +{ + if (initialize) + // need to create an object Number() to initialize to zero to avoid + // confusion with Tensor::operator=(scalar) when using something like + // Tensor<1,dim,Tensor<1,dim,Number> >. + for (unsigned int i=0; i!=dim; ++i) + subtensor[i] = Tensor(); +} + + + +template +inline +Tensor::Tensor (const Tensor &initializer) { -// default constructor. not specifying an initializer list calls -// the default constructor of the subobjects, which initialize them -// selves. therefore, the tensor is set to zero this way + for (unsigned int i=0; i!=dim; ++i) + subtensor[i] = initializer[i]; } @@ -341,26 +371,39 @@ inline Tensor::Tensor (const array_type &initializer) { for (unsigned int i=0; i(initializer[i]); + subtensor[i] = initializer[i]; +} + + + +template +template +inline +Tensor::Tensor (const Tensor &initializer) +{ + for (unsigned int i=0; i!=dim; ++i) + subtensor[i] = initializer[i]; } template +template inline Tensor::Tensor -(const Tensor<1,dim,Tensor > &tensor_in) +(const Tensor<1,dim,Tensor > &initializer) { for (unsigned int i=0; i +template inline Tensor::operator -Tensor<1,dim,Tensor > () const +Tensor<1,dim,Tensor > () const { return Tensor<1,dim,Tensor > (subtensor); } @@ -448,45 +491,40 @@ Tensor::operator = (const Tensor &t) template -template -Tensor::Tensor (const Tensor &t) -{ - *this = t; -} - - - -template +template inline Tensor & -Tensor::operator = (const Number d) +Tensor::operator = (const OtherNumber d) { - Assert (d==Number(0), ExcMessage ("Only assignment with zero is allowed")); + Assert (d == OtherNumber(), ExcMessage ("Only assignment with zero is allowed")); (void) d; for (unsigned int i=0; i +template inline bool -Tensor::operator == (const Tensor &p) const +Tensor::operator == (const Tensor &p) const { for (unsigned int i=0; i +template inline bool -Tensor::operator != (const Tensor &p) const +Tensor::operator != (const Tensor &p) const { return !((*this) == p); } @@ -494,9 +532,10 @@ Tensor::operator != (const Tensor &p) const template +template inline Tensor & -Tensor::operator += (const Tensor &p) +Tensor::operator += (const Tensor &p) { for (unsigned int i=0; i::operator += (const Tensor &p) template +template inline Tensor & -Tensor::operator -= (const Tensor &p) +Tensor::operator -= (const Tensor &p) { for (unsigned int i=0; i::operator -= (const Tensor &p) template +template inline Tensor & -Tensor::operator *= (const Number s) +Tensor::operator *= (const OtherNumber s) { for (unsigned int i=0; i::operator *= (const Number s) template +template inline Tensor & -Tensor::operator /= (const Number s) +Tensor::operator /= (const OtherNumber s) { for (unsigned int i=0; i::norm_square () const template -template +template inline void -Tensor::unroll (Vector &result) const +Tensor::unroll (Vector &result) const { AssertDimension (result.size(),(Utilities::fixed_power(dim))); @@ -595,10 +637,10 @@ Tensor::unroll (Vector &result) const template -template +template inline void -Tensor::unroll_recursion (Vector &result, +Tensor::unroll_recursion (Vector &result, unsigned int &index) const { for (unsigned int i=0; i class Tensor<1,dim,Number>; * as argument. * * @ingroup geomprimitives - * @author Wolfgang Bangerth, Matthias Maier, 2009, 2015 + * @author Wolfgang Bangerth, 2009, Matthias Maier, 2015 */ template class Tensor<0,dim,Number> @@ -116,6 +116,13 @@ public: */ typedef typename numbers::NumberTraits::real_type real_type; + /** + * Declare an array type which can be used to initialize an object of this + * type statically. In case of a a tensor of rank 0 this is just a scalar + * number type + */ + typedef Number array_type; + /** * Constructor. Set to zero. */ @@ -305,7 +312,7 @@ private: * as argument. * * @ingroup geomprimitives - * @author Wolfgang Bangerth, 1998-2005 + * @author Wolfgang Bangerth, 1998-2005, Matthias Maier, 2015 */ template class Tensor<1,dim,Number> @@ -365,14 +372,14 @@ public: Tensor (const bool initialize = true); /** - * Copy constructor, where the data is copied from a C-style array. + * Copy constructor. */ - Tensor (const array_type &initializer); + Tensor (const Tensor<1,dim,Number> &initializer); /** - * Copy constructor. + * Copy constructor, where the data is copied from a C-style array. */ - Tensor (const Tensor<1,dim,Number> &initializer); + Tensor (const array_type &initializer); /** * Copy constructor from tensors with different underlying scalar type. This @@ -1492,7 +1499,6 @@ operator- (const Tensor &p, const Tensor * sum_j src1[j] src2[j]. * * @relates Tensor - * @author Guido Kanschat, 2000 */ template inline @@ -1522,7 +1528,6 @@ contract (const Tensor<1,dim,Number> &src1, * contraction. * * @relates Tensor - * @author Wolfgang Bangerth, 2005 */ template inline -- 2.39.5