OpenWalnut  1.5.0dev
WTensor.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WTENSOR_H
26 #define WTENSOR_H
27 
28 #include <iostream>
29 #include <vector>
30 
31 #include "WTensorSym.h"
32 
33 // ############################# class WTensor<> #############################################
34 /**
35  * Implements a tensor that has the same number of components in every
36  * direction.
37  *
38  * \tparam order The order of the tensor.
39  * \tparam dim The dimension of the tensor, i.e. the number of components
40  * in each direction.
41  * \tparam Data_T The datatype of the components, double by default.
42  *
43  * \note The dimension may never be 0.
44  * \note The type Data_T may not throw exceptions on construction, destruction or
45  * during any assignment operator.
46  *
47  * Access to specific elements of the tensor can be achieved in 2 ways:
48  *
49  * - operator (), whose number of parameters matches the template parameter order.
50  * - operator [], whose parameter is either an array or a std::vector of appropriate size.
51  *
52  * \note The datatype of the array or std::vector can be any type castable to std::size_t.
53  * \note There is no bounds checking for the array version of operator [].
54  * \note Operator () is not supported for orders larger than 6.
55  *
56  * Examples:
57  *
58  * - Construct a tensor of order 2 and dimension 3 (i.e. a 3x3-Matrix):
59  *
60  * WTensor< 2, 3 > w;
61  *
62  * - Change Element (2,0) to 4.0:
63  *
64  * w( 2, 0 ) = 4.0;
65  *
66  * - Construct a 4D-vector:
67  *
68  * WTensor< 1, 4 > v;
69  *
70  * - Access v at position 2:
71  *
72  * v( 2 ) = ...;
73  *
74  * std::vector< int > i( 1, 2 );
75  * v[ i ] = ...;
76  */
77 template< std::size_t order, std::size_t dim, typename Data_T = double >
78 class WTensor : public WTensorFunc< WTensorBase, order, dim, Data_T >
79 {
80 public:
81  /**
82  * Standard constructor.
83  */
85 
86  /**
87  * Construct a Tensor from a symmetric tensor.
88  *
89  * \param t A symmetric tensor.
90  */
92 
93  /**
94  * Copy from a symmetric tensor.
95  *
96  * \param t A symmetric tensor.
97  *
98  * \return new tensor
99  */
101 };
102 
103 template< std::size_t order, std::size_t dim, typename Data_T >
105  : WTensorFunc< WTensorBase, order, dim, Data_T >()
106 {
107 }
108 
109 template< std::size_t order, std::size_t dim, typename Data_T >
111  : WTensorFunc< WTensorBase, order, dim, Data_T >()
112 {
113  WTensorBase< order, dim, Data_T >::operator = ( t );
114 }
115 
116 template< std::size_t order, std::size_t dim, typename Data_T >
118 {
119  WTensorBase< order, dim, Data_T >::operator = ( t );
120  return *this;
121 }
122 
123 // ######################## stream output operators #################################
124 
125 /**
126  * Write a tensor of order 0 to an output stream.
127  *
128  * \param o An output stream.
129  * \param t A WTensor.
130  *
131  * \return The output stream.
132  */
133 template< std::size_t dim, typename Data_T >
134 std::ostream& operator << ( std::ostream& o, WTensor< 0, dim, Data_T > const& t )
135 {
136  o << t() << std::endl;
137  return o;
138 }
139 
140 /**
141  * Write a tensor of order 1 to an output stream.
142  *
143  * \param o An output stream.
144  * \param t A WTensor.
145  *
146  * \return The output stream.
147  */
148 template< std::size_t dim, typename Data_T >
149 std::ostream& operator << ( std::ostream& o, WTensor< 1, dim, Data_T > const& t )
150 {
151  for( std::size_t k = 0; k < dim; ++k )
152  {
153  o << t( k ) << " ";
154  }
155  o << std::endl;
156  return o;
157 }
158 
159 /**
160  * Write a tensor of order 2 to an output stream.
161  *
162  * \param o An output stream.
163  * \param t A WTensor.
164  *
165  * \return The output stream.
166  */
167 template< std::size_t dim, typename Data_T >
168 std::ostream& operator << ( std::ostream& o, WTensor< 2, dim, Data_T > const& t )
169 {
170  for( std::size_t k = 0; k < dim; ++k )
171  {
172  for( std::size_t l = 0; l < dim; ++l )
173  {
174  o << t( k, l ) << " ";
175  }
176  o << std::endl;
177  }
178  return o;
179 }
180 
181 #endif // WTENSOR_H
Implements a symmetric tensor that has the same number of components in every direction.
Definition: WTensorSym.h:73
Implements a tensor that has the same number of components in every direction.
Definition: WTensor.h:79
WTensor()
Standard constructor.
Definition: WTensor.h:104
WTensor(WTensorSym< order, dim, Data_T > const &t)
Construct a Tensor from a symmetric tensor.
Definition: WTensor.h:110
WTensor const & operator=(WTensorSym< order, dim, Data_T > const &t)
Copy from a symmetric tensor.
Definition: WTensor.h:117