Intrepid
Intrepid_MiniTensor_Tensor3.t.h
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions: Alejandro Mota (amota@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #if !defined(Intrepid_MiniTensor_Tensor3_t_h)
43 #define Intrepid_MiniTensor_Tensor3_t_h
44 
45 namespace Intrepid {
46 
47 //
48 // \return \f$ B = A \cdot u := B_{ij} = A_{ijp} u_p \f$
49 //
50 template<typename S, typename T>
51 Tensor<typename Promote<S, T>::type>
52 dot(Tensor3<T> const & A, Vector<S> const & u)
53 {
54  Index const
55  N = A.get_dimension();
56 
57  assert(u.get_dimension() == N);
58 
59  Tensor<typename Promote<S, T>::type>
60  B(N);
61 
62  for (Index i = 0; i < N; ++i) {
63  for (Index j = 0; j < N; ++j) {
64 
65  typename Promote<S, T>::type
66  s = 0.0;
67 
68  for (Index p = 0; p < N; ++p) {
69  s += A(i,j,p) * u(p);
70  }
71  B(i,j) = s;
72  }
73  }
74 
75  return B;
76 }
77 
78 //
79 // \return \f$ B = u \cdot A := B_{ij} = u_p A{pij} \f$
80 //
81 template<typename S, typename T>
82 Tensor<typename Promote<S, T>::type>
83 dot(Vector<S> const & u, Tensor3<T> const & A)
84 {
85  Index const
86  N = A.get_dimension();
87 
88  assert(u.get_dimension() == N);
89 
90  Tensor<typename Promote<S, T>::type>
91  B(N);
92 
93  for (Index i = 0; i < N; ++i) {
94  for (Index j = 0; j < N; ++j) {
95 
96  typename Promote<S, T>::type
97  s = 0.0;
98 
99  for (Index p = 0; p < N; ++p) {
100  s += u(p) * A(p,i,j);
101  }
102  B(i,j) = s;
103  }
104  }
105 
106  return B;
107 }
108 
109 
110 //
111 // \return \f$ B = A \cdot u := B_{ij} = A_{ipj} u_p \f$
112 //
113 template<typename S, typename T>
114 Tensor<typename Promote<S, T>::type>
115 dot2(Tensor3<T> const & A, Vector<S> const & u)
116 {
117  Index const
118  N = A.get_dimension();
119 
120  assert(u.get_dimension() == N);
121 
122  Tensor<typename Promote<S, T>::type>
123  B(N);
124 
125  for (Index i = 0; i < N; ++i) {
126  for (Index j = 0; j < N; ++j) {
127 
128  typename Promote<S, T>::type
129  s = 0.0;
130 
131  for (Index p = 0; p < N; ++p) {
132  s += A(i,p,j) * u(p);
133  }
134  B(i,j) = s;
135  }
136  }
137 
138  return B;
139 }
140 
141 //
142 // \return \f$ B = u \cdot A := B_{ij} = u_p A_{ipj} \f$
143 //
144 template<typename S, typename T>
145 Tensor<typename Promote<S, T>::type>
146 dot2(Vector<S> const & u, Tensor3<T> const & A)
147 {
148  return dot2(A, u);
149 }
150 
154 template<typename S, typename T>
155 Tensor3<typename Promote<S, T>::type>
156 dot(Tensor3<T> const & A, Tensor<S> const & B)
157 {
158  Index const
159  N = A.get_dimension();
160 
161  assert(B.get_dimension() == N);
162 
163  Tensor3<typename Promote<S, T>::type>
164  C(N);
165 
166  for (Index i = 0; i < N; ++i) {
167  for (Index k = 0; k < N; ++k) {
168  for (Index j = 0; j < N; ++j) {
169 
170  typename Promote<S, T>::type
171  s = 0.0;
172 
173  for (Index p = 0; p < N; ++p) {
174  s += A(i,j,p) * B(p,k);
175  }
176  C(i,j,k) = s;
177  }
178  }
179  }
180 
181  return C;
182 }
183 
187 template<typename S, typename T>
188 Tensor3<typename Promote<S, T>::type>
189 dot(Tensor<S> const & A, Tensor3<T> const & B)
190 {
191  Index const
192  N = A.get_dimension();
193 
194  assert(B.get_dimension() == N);
195 
196  Tensor3<typename Promote<S, T>::type>
197  C(N);
198 
199  for (Index i = 0; i < N; ++i) {
200  for (Index k = 0; k < N; ++k) {
201  for (Index j = 0; j < N; ++j) {
202 
203  typename Promote<S, T>::type
204  s = 0.0;
205 
206  for (Index p = 0; p < N; ++p) {
207  s += A(i,p) * B(p,j,k);
208  }
209  C(i,j,k) = s;
210  }
211  }
212  }
213 
214  return C;
215 }
216 
220 template<typename S, typename T>
221 Tensor3<typename Promote<S, T>::type>
222 dot2(Tensor3<T> const & A, Tensor<S> const & B)
223 {
224  Index const
225  N = A.get_dimension();
226 
227  assert(B.get_dimension() == N);
228 
229  Tensor3<typename Promote<S, T>::type>
230  C(N);
231 
232  for (Index i = 0; i < N; ++i) {
233  for (Index k = 0; k < N; ++k) {
234  for (Index j = 0; j < N; ++j) {
235 
236  typename Promote<S, T>::type
237  s = 0.0;
238 
239  for (Index p = 0; p < N; ++p) {
240  s += A(i,p,j) * B(p,k);
241  }
242  C(i,j,k) = s;
243  }
244  }
245  }
246 
247  return C;
248 }
249 
250 
254 template<typename S, typename T>
255 Tensor3<typename Promote<S, T>::type>
256 dot2(Tensor<S> const & A, Tensor3<T> const & B)
257 {
258  Index const
259  N = A.get_dimension();
260 
261  assert(B.get_dimension() == N);
262 
263  Tensor3<typename Promote<S, T>::type>
264  C(N);
265 
266  for (Index i = 0; i < N; ++i) {
267  for (Index k = 0; k < N; ++k) {
268  for (Index j = 0; j < N; ++j) {
269 
270  typename Promote<S, T>::type
271  s = 0.0;
272 
273  for (Index p = 0; p < N; ++p) {
274  s += A(i,p) * B(j,p,k);
275  }
276  C(i,j,k) = s;
277  }
278  }
279  }
280 
281  return C;
282 }
283 
284 
285 //
286 // 3rd-order tensor input
287 // \param A 3rd-order tensor
288 // \param is input stream
289 // \return is input stream
290 //
291 template<typename T>
292 std::istream &
293 operator>>(std::istream & is, Tensor3<T> & A)
294 {
295  Index const
296  N = A.get_dimension();
297 
298  for (Index i = 0; i < N; ++i) {
299  for (Index j = 0; j < N; ++j) {
300  for (Index k = 0; k < N; ++k) {
301  is >> A(i,j,k);
302  }
303  }
304  }
305 
306  return is;
307 }
308 
309 //
310 // 3rd-order tensor output
311 // \param A 3rd-order tensor
312 // \param os output stream
313 // \return os output stream
314 //
315 template<typename T>
316 std::ostream &
317 operator<<(std::ostream & os, Tensor3<T> const & A)
318 {
319  Index const
320  N = A.get_dimension();
321 
322  if (N == 0) {
323  return os;
324  }
325 
326  for (Index i = 0; i < N; ++i) {
327 
328  for (Index j = 0; j < N; ++j) {
329 
330  os << std::scientific << A(i,j,0);
331 
332  for (Index k = 1; k < N; ++k) {
333  os << std::scientific << "," << A(i,j,k);
334  }
335 
336  os << std::endl;
337 
338  }
339 
340  os << std::endl;
341  os << std::endl;
342 
343  }
344 
345  return os;
346 }
347 
348 } // namespace Intrepid
349 
350 #endif // Intrepid_MiniTensor_Tensor3_t_h