Intrepid
Intrepid_MiniTensor_LinearAlgebra.i.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_LinearAlgebra_i_h)
43 #define Intrepid_MiniTensor_LinearAlgebra_i_h
44 
45 namespace Intrepid {
46 
47 //
48 // R^N tensor Frobenius norm
49 // \return \f$ \sqrt{A:A} \f$
50 //
51 template<typename T>
52 inline
53 T
54 norm(Tensor<T> const & A)
55 {
56  Index const
57  N = A.get_dimension();
58 
59  T s = 0.0;
60 
61  switch (N) {
62 
63  default:
64  s = dotdot(A, A);
65  break;
66 
67  case 3:
68  s+= A(0,0)*A(0,0) + A(0,1)*A(0,1) + A(0,2)*A(0,2);
69  s+= A(1,0)*A(1,0) + A(1,1)*A(1,1) + A(1,2)*A(1,2);
70  s+= A(2,0)*A(2,0) + A(2,1)*A(2,1) + A(2,2)*A(2,2);
71  break;
72 
73  case 2:
74  s+= A(0,0)*A(0,0) + A(0,1)*A(0,1);
75  s+= A(1,0)*A(1,0) + A(1,1)*A(1,1);
76  break;
77 
78  }
79 
80  return std::sqrt(s);
81 }
82 
83 //
84 // R^N tensor 1-norm
85 // \return \f$ \max_{j \in {0,\cdots,N}}\Sigma_{i=0}^N |A_{ij}| \f$
86 //
87 template<typename T>
88 inline
89 T
90 norm_1(Tensor<T> const & A)
91 {
92  Index const
93  N = A.get_dimension();
94 
95  Vector<T> v(N);
96 
97  T s = 0.0;
98 
99  switch (N) {
100 
101  default:
102 
103  for (Index i = 0; i < N; ++i) {
104  T t = 0.0;
105  for (Index j = 0; j < N; ++j) {
106  t += std::abs(A(j, i));
107  }
108  v(i) = t;
109  }
110 
111  for (Index i = 0; i < N; ++i) {
112  s = std::max(s, v(i));
113  }
114  break;
115 
116  case 3:
117  v(0) = std::abs(A(0,0)) + std::abs(A(1,0)) + std::abs(A(2,0));
118  v(1) = std::abs(A(0,1)) + std::abs(A(1,1)) + std::abs(A(2,1));
119  v(2) = std::abs(A(0,2)) + std::abs(A(1,2)) + std::abs(A(2,2));
120 
121  s = std::max(std::max(v(0),v(1)),v(2));
122  break;
123 
124  case 2:
125  v(0) = std::abs(A(0,0)) + std::abs(A(1,0));
126  v(1) = std::abs(A(0,1)) + std::abs(A(1,1));
127 
128  s = std::max(v(0),v(1));
129  break;
130 
131  }
132 
133  return s;
134 }
135 
136 //
137 // R^N tensor infinity-norm
138 // \return \f$ \max_{i \in {0,\cdots,N}}\Sigma_{j=0}^N |A_{ij}| \f$
139 //
140 template<typename T>
141 inline
142 T
143 norm_infinity(Tensor<T> const & A)
144 {
145  Index const
146  N = A.get_dimension();
147 
148  Vector<T> v(N);
149 
150  T s = 0.0;
151 
152  switch (N) {
153 
154  default:
155  for (Index i = 0; i < N; ++i) {
156  T t = 0.0;
157  for (Index j = 0; j < N; ++j) {
158  t += std::abs(A(i, j));
159  }
160  v(i) = t;
161  }
162 
163  for (Index i = 0; i < N; ++i) {
164  s = std::max(s, v(i));
165  }
166  break;
167 
168  case 3:
169  v(0) = std::abs(A(0,0)) + std::abs(A(0,1)) + std::abs(A(0,2));
170  v(1) = std::abs(A(1,0)) + std::abs(A(1,1)) + std::abs(A(1,2));
171  v(2) = std::abs(A(2,0)) + std::abs(A(2,1)) + std::abs(A(2,2));
172 
173  s = std::max(std::max(v(0),v(1)),v(2));
174  break;
175 
176  case 2:
177  v(0) = std::abs(A(0,0)) + std::abs(A(0,1));
178  v(1) = std::abs(A(1,0)) + std::abs(A(1,1));
179 
180  s = std::max(v(0),v(1));
181  break;
182 
183  }
184 
185  return s;
186 }
187 
188 //
189 // Swap row. Exchange rows i and j in place
190 // \param A tensor
191 // \param i index
192 // \param j index
193 //
194 template<typename T>
195 void
196 swap_row(Tensor<T> & A, Index const i, Index const j)
197 {
198  Index const
199  N = A.get_dimension();
200 
201  if (i != j) {
202  for (Index k = 0; k < N; ++k) {
203  std::swap(A(i, k), A(j, k));
204  }
205  }
206  return;
207 }
208 
209 //
210 // Swap column. Exchange columns i and j in place
211 // \param A tensor
212 // \param i index
213 // \param j index
214 //
215 template<typename T>
216 void
217 swap_col(Tensor<T> & A, Index const i, Index const j)
218 {
219  Index const
220  N = A.get_dimension();
221 
222  if (i != j) {
223  for (Index k = 0; k < N; ++k) {
224  std::swap(A(k, i), A(k, j));
225  }
226  }
227  return;
228 }
229 
230 //
231 // R^N determinant
232 // Laplace expansion. Warning: no pivoting.
233 // Casual use only. Use Teuchos LAPACK interface for
234 // more efficient and robust techniques.
235 // \param A tensor
236 // \return \f$ \det A \f$
237 //
238 template<typename T>
239 inline
240 T
241 det(Tensor<T> const & A)
242 {
243  Index const
244  N = A.get_dimension();
245 
246  T s = 0.0;
247 
248  switch (N) {
249 
250  default:
251  {
252  int sign = 1;
253  for (Index i = 0; i < N; ++i) {
254  const T d = det(subtensor(A, i, 1));
255  s += sign * d * A(i, 1);
256  sign *= -1;
257  }
258  }
259  break;
260 
261  case 3:
262  s =
263  -A(0,2)*A(1,1)*A(2,0) + A(0,1)*A(1,2)*A(2,0) +
264  A(0,2)*A(1,0)*A(2,1) - A(0,0)*A(1,2)*A(2,1) -
265  A(0,1)*A(1,0)*A(2,2) + A(0,0)*A(1,1)*A(2,2);
266  break;
267 
268  case 2:
269  s = A(0,0) * A(1,1) - A(1,0) * A(0,1);
270  break;
271 
272  }
273 
274  return s;
275 }
276 
277 //
278 // R^N trace
279 // \param A tensor
280 // \return \f$ A:I \f$
281 //
282 template<typename T>
283 inline
284 T
285 trace(Tensor<T> const & A)
286 {
287  Index const
288  N = A.get_dimension();
289 
290  T s = 0.0;
291 
292  switch (N) {
293 
294  default:
295  for (Index i = 0; i < N; ++i) {
296  s += A(i,i);
297  }
298  break;
299 
300  case 3:
301  s = A(0,0) + A(1,1) + A(2,2);
302  break;
303 
304  case 2:
305  s = A(0,0) + A(1,1);
306  break;
307 
308  }
309 
310  return s;
311 }
312 
313 //
314 // R^N first invariant, trace
315 // \param A tensor
316 // \return \f$ I_A = A:I \f$
317 //
318 template<typename T>
319 inline
320 T
321 I1(Tensor<T> const & A)
322 {
323  return trace(A);
324 }
325 
326 //
327 // R^N second invariant
328 // \param A tensor
329 // \return \f$ II_A = \frac{1}{2}((I_A)^2-I_{A^2}) \f$
330 //
331 template<typename T>
332 inline
333 T
334 I2(Tensor<T> const & A)
335 {
336  Index const
337  N = A.get_dimension();
338 
339  T
340  s = 0.0;
341 
342  const T
343  trA = trace(A);
344 
345  switch (N) {
346 
347  default:
348  s = 0.5 * (trA * trA - trace(A * A));
349  break;
350 
351  case 3:
352  s = 0.5 * (trA*trA - A(0,0)*A(0,0) - A(1,1)*A(1,1) - A(2,2)*A(2,2)) -
353  A(0,1)*A(1,0) - A(0,2)*A(2,0) - A(1,2)*A(2,1);
354  break;
355 
356  case 2:
357  s = 0.5 * (trA * trA - trace(A * A));
358  break;
359 
360  }
361 
362  return s;
363 }
364 
365 //
366 // R^N third invariant
367 // \param A tensor
368 // \return \f$ III_A = \det A \f$
369 //
370 template<typename T>
371 inline
372 T
373 I3(Tensor<T> const & A)
374 {
375  return det(A);
376 }
377 
378 } // namespace Intrepid
379 
380 #endif // Intrepid_MiniTensor_LinearAlgebra_i_h