Intrepid
Intrepid_MiniTensor_Tensor.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_Tensor_i_h)
43 #define Intrepid_MiniTensor_Tensor_i_h
44 
45 namespace Intrepid {
46 
47 namespace {
48 
49 template<typename S>
50 bool
51 greater_than(S const & a, S const & b)
52 {
53  return a.first > b.first;
54 }
55 
56 } // anonymous namespace
57 
58 //
59 // Sort and index in descending order. Useful for ordering singular values
60 // and eigenvalues and corresponding vectors in the respective decompositions.
61 //
62 template<typename T>
63 std::pair<Vector<T>, Tensor<T> >
64 sort_permutation(Vector<T> const & u)
65 {
66 
67  Index const
68  N = u.get_dimension();
69 
70  std::vector<std::pair<T, Index > >
71  s(N);
72 
73  for (Index i = 0; i < N; ++i) {
74  s[i].first = u(i);
75  s[i].second = i;
76  }
77 
78  std::sort(s.begin(), s.end(), greater_than< std::pair<T, Index > > );
79 
80  Vector<T> v(N);
81 
82  Tensor<T>
83  P = zero<T>(N);
84 
85  for (Index i = 0; i < N; ++i) {
86  v(i) = s[i].first;
87  P(s[i].second, i) = 1.0;
88  }
89 
90  return std::make_pair(v, P);
91 
92 }
93 
94 //
95 // Fill components with value.
96 //
97 template<typename T>
98 inline
99 void
100 Tensor<T>::fill(ComponentValue value)
101 {
102  TensorBase<T>::fill(value);
103  return;
104 }
105 
106 //
107 // Fill components from array defined by pointer.
108 //
109 template<typename T>
110 inline
111 void
112 Tensor<T>::fill(T const * data_ptr, ComponentOrder const component_order)
113 {
114  assert(data_ptr != NULL);
115 
116  Tensor<T> &
117  self = (*this);
118 
119  Index const
120  dimension = self.get_dimension();
121 
122  switch (dimension) {
123 
124  default:
125  TensorBase<T>::fill(data_ptr);
126  break;
127 
128  case 3:
129 
130  switch (component_order) {
131 
132  case CANONICAL:
133  TensorBase<T>::fill(data_ptr);
134  break;
135 
136  case SIERRA_FULL:
137 
138  // 0 1 2 3 4 5 6 7 8
139  // XX YY ZZ XY YZ ZX YX ZY XZ
140  // 0 4 8 1 5 6 3 7 2
141  self[0] = data_ptr[0];
142  self[4] = data_ptr[1];
143  self[8] = data_ptr[2];
144 
145  self[1] = data_ptr[3];
146  self[5] = data_ptr[4];
147  self[6] = data_ptr[5];
148 
149  self[3] = data_ptr[6];
150  self[7] = data_ptr[7];
151  self[2] = data_ptr[8];
152  break;
153 
154  case SIERRA_SYMMETRIC:
155  self[0] = data_ptr[0];
156  self[4] = data_ptr[1];
157  self[8] = data_ptr[2];
158 
159  self[1] = data_ptr[3];
160  self[5] = data_ptr[4];
161  self[6] = data_ptr[5];
162 
163  self[3] = data_ptr[3];
164  self[7] = data_ptr[4];
165  self[2] = data_ptr[5];
166  break;
167 
168  default:
169  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
170  std::cerr << std::endl;
171  std::cerr << "Unknown component order.";
172  std::cerr << std::endl;
173  exit(1);
174  break;
175 
176  }
177 
178  break;
179  }
180 
181  return;
182 }
183 
184 //
185 // Default constructor
186 //
187 template<typename T>
188 inline
191 {
192  return;
193 }
194 
195 //
196 // Constructor that initializes to NaNs
197 //
198 template<typename T>
199 inline
200 Tensor<T>::Tensor(Index const dimension) :
201 TensorBase<T>::TensorBase(dimension, order)
202 {
203  return;
204 }
205 
209 template<typename T>
210 inline
211 Tensor<T>::Tensor(Index const dimension, ComponentValue value) :
212 TensorBase<T>::TensorBase(dimension, order, value)
213 {
214  return;
215 }
216 
217 //
218 // Create tensor from a scalar
219 //
220 template<typename T>
221 inline
222 Tensor<T>::Tensor(Index const dimension, T const & s) :
223 TensorBase<T>::TensorBase(dimension, order, s)
224 {
225  return;
226 }
227 
228 //
229 // Create tensor specifying components
230 // \param s00, s01, ... components in the R^2 canonical basis
231 //
232 template<typename T>
233 inline
235  T const & s00, T const & s01,
236  T const & s10, T const & s11)
237 {
238  Tensor<T> &
239  self = (*this);
240 
241  self.set_dimension(2);
242 
243  self[0] = s00;
244  self[1] = s01;
245 
246  self[2] = s10;
247  self[3] = s11;
248 
249  return;
250 }
251 
252 //
253 // Create tensor specifying components
254 // \param s00, s01, ... components in the R^3 canonical basis
255 //
256 template<typename T>
257 inline
259  T const & s00, T const & s01, T const & s02,
260  T const & s10, T const & s11, T const & s12,
261  T const & s20, T const & s21, T const & s22)
262 {
263  Tensor<T> &
264  self = (*this);
265 
266  self.set_dimension(3);
267 
268  self[0] = s00;
269  self[1] = s01;
270  self[2] = s02;
271 
272  self[3] = s10;
273  self[4] = s11;
274  self[5] = s12;
275 
276  self[6] = s20;
277  self[7] = s21;
278  self[8] = s22;
279 
280  return;
281 }
282 
283 //
284 // Create tensor from array
285 //
286 template<typename T>
287 inline
288 Tensor<T>::Tensor(Index const dimension, T const * data_ptr) :
289 TensorBase<T>::TensorBase(dimension, order, data_ptr)
290 {
291  return;
292 }
293 
294 //
295 // Create tensor from array with component order
296 //
297 template<typename T>
298 inline
300  Index const dimension,
301  T const * data_ptr,
302  ComponentOrder const component_order)
303 {
304  assert(data_ptr != NULL);
305 
306  Tensor<T> &
307  self = (*this);
308 
309  self.set_dimension(dimension);
310 
311  fill(data_ptr, component_order);
312 
313  return;
314 }
315 
316 //
317 // Copy constructor
318 //
319 template<typename T>
320 inline
322 TensorBase<T>::TensorBase(A)
323 {
324  return;
325 }
326 
327 //
328 // 2nd-order tensor from 4th-order tensor
329 //
330 template<typename T>
331 inline
333 {
334  Index const
335  dimension_4th = A.get_dimension();
336 
337  Index const
338  dimension_2nd = dimension_4th * dimension_4th;
339 
340  Tensor<T> &
341  self = (*this);
342 
343  self.set_dimension(dimension_2nd);
344 
345  Index const
346  number_components = dimension_2nd * dimension_2nd;
347 
348  for (Index i = 0; i < number_components; ++i) {
349  self[i] = A[i];
350  }
351 
352  return;
353 }
354 
355 //
356 // Simple destructor
357 //
358 template<typename T>
359 inline
361 {
362  return;
363 }
364 
365 //
366 // Indexing for constant tensor
367 //
368 template<typename T>
369 inline
370 T const &
371 Tensor<T>::operator()(Index const i, Index const j) const
372 {
373  Tensor<T> const &
374  self = (*this);
375 
376  Index const
377  N = self.get_dimension();
378 
379  assert(i < N);
380  assert(j < N);
381 
382  return self[i * N + j];
383 }
384 
385 //
386 //Tensor indexing
387 //
388 template<typename T>
389 inline
390 T &
391 Tensor<T>::operator()(Index const i, Index const j)
392 {
393  Tensor<T> &
394  self = (*this);
395 
396  Index const
397  N = self.get_dimension();
398 
399  assert(i < N);
400  assert(j < N);
401 
402  return self[i * N + j];
403 }
404 
405 //
406 // Tensor addition
407 //
408 template<typename S, typename T>
409 inline
411 operator+(Tensor<S> const & A, Tensor<T> const & B)
412 {
414  C;
415 
416  add(A, B, C);
417 
418  return C;
419 }
420 
421 //
422 // Tensor subtraction
423 //
424 template<typename S, typename T>
425 inline
426 Tensor<typename Promote<S, T>::type>
427 operator-(Tensor<S> const & A, Tensor<T> const & B)
428 {
429  Tensor<typename Promote<S, T>::type>
430  C;
431 
432  subtract(A, B, C);
433 
434  return C;
435 }
436 
437 //
438 // Tensor minus
439 //
440 template<typename T>
441 inline
442 Tensor<T>
443 operator-(Tensor<T> const & A)
444 {
445  Tensor<T>
446  B;
447 
448  minus(A, B);
449 
450  return B;
451 }
452 
453 //
454 // Tensor equality
455 //
456 template<typename T>
457 inline
458 bool
459 operator==(Tensor<T> const & A, Tensor<T> const & B)
460 {
461  return equal(A, B);
462 }
463 
464 //
465 // Tensor inequality
466 //
467 template<typename T>
468 inline
469 bool
470 operator!=(Tensor<T> const & A, Tensor<T> const & B)
471 {
472  return not_equal(A, B);
473 }
474 
475 //
476 // Scalar tensor product
477 //
478 template<typename S, typename T>
479 inline
480 typename lazy_disable_if< order_1234<S>, apply_tensor< Promote<S,T> > >::type
481 operator*(S const & s, Tensor<T> const & A)
482 {
483  Tensor<typename Promote<S, T>::type>
484  B;
485 
486  scale(A, s, B);
487 
488  return B;
489 }
490 
491 //
492 // Tensor scalar product
493 //
494 template<typename S, typename T>
495 inline
496 typename lazy_disable_if< order_1234<S>, apply_tensor< Promote<S,T> > >::type
497 operator*(Tensor<T> const & A, S const & s)
498 {
499  Tensor<typename Promote<S, T>::type>
500  B;
501 
502  scale(A, s, B);
503 
504  return B;
505 }
506 
507 //
508 // Tensor scalar division
509 //
510 template<typename S, typename T>
511 inline
512 Tensor<typename Promote<S, T>::type>
513 operator/(Tensor<T> const & A, S const & s)
514 {
515  Tensor<typename Promote<S, T>::type>
516  B;
517 
518  divide(A, s, B);
519 
520  return B;
521 }
522 
523 //
524 // Tensor vector product v = A u
525 //
526 template<typename S, typename T>
527 inline
528 Vector<typename Promote<S, T>::type>
529 operator*(Tensor<T> const & A, Vector<S> const & u)
530 {
531  return dot(A, u);
532 }
533 
534 //
535 // Vector tensor product v = u A
536 //
537 template<typename S, typename T>
538 inline
539 Vector<typename Promote<S, T>::type>
540 operator*(Vector<S> const & u, Tensor<T> const & A)
541 {
542  return dot(u, A);
543 }
544 
545 //
546 // Tensor dot product C = A B
547 //
548 template<typename S, typename T>
549 inline
550 Tensor<typename Promote<S, T>::type>
551 operator*(Tensor<S> const & A, Tensor<T> const & B)
552 {
553  return dot(A, B);
554 }
555 
556 //
557 // Extract a row as a vector
558 //
559 template<typename T>
560 Vector<T>
561 row(Tensor<T> const & A, Index const i)
562 {
563  Index const
564  N = A.get_dimension();
565 
566  Vector<T>
567  v(N);
568 
569  switch (N) {
570  default:
571  for (Index j = 0; j < N; ++j) {
572  v(j) = A(i,j);
573  }
574  break;
575 
576  case 2:
577  v(0) = A(i,0);
578  v(1) = A(i,1);
579  break;
580 
581  case 3:
582  v(0) = A(i,0);
583  v(1) = A(i,1);
584  v(2) = A(i,2);
585  break;
586  }
587 
588  return v;
589 }
590 
591 //
592 // Extract a column as a vector
593 //
594 template<typename T>
595 Vector<T>
596 col(Tensor<T> const & A, Index const j)
597 {
598  Index const
599  N = A.get_dimension();
600 
601  Vector<T>
602  v(N);
603 
604  switch (N) {
605  default:
606  for (Index i = 0; i < N; ++i) {
607  v(i) = A(i,j);
608  }
609  break;
610 
611  case 2:
612  v(0) = A(0,j);
613  v(1) = A(1,j);
614  break;
615 
616  case 3:
617  v(0) = A(0,j);
618  v(1) = A(1,j);
619  v(2) = A(2,j);
620  break;
621  }
622 
623  return v;
624 }
625 
626 
627 //
628 // R^N tensor vector product v = A u
629 // \param A tensor
630 // \param u vector
631 // \return \f$ A u \f$
632 //
633 template<typename S, typename T>
634 inline
635 Vector<typename Promote<S, T>::type>
636 dot(Tensor<T> const & A, Vector<S> const & u)
637 {
638  Index const
639  N = A.get_dimension();
640 
641  assert(u.get_dimension() == N);
642 
643  Vector<typename Promote<S, T>::type>
644  v(N);
645 
646  switch (N) {
647 
648  default:
649  for (Index i = 0; i < N; ++i) {
650 
651  typename Promote<S, T>::type
652  s = 0.0;
653 
654  for (Index p = 0; p < N; ++p) {
655  s += A(i, p) * u(p);
656  }
657  v(i) = s;
658  }
659  break;
660 
661  case 3:
662  v(0) = A(0,0)*u(0) + A(0,1)*u(1) + A(0,2)*u(2);
663  v(1) = A(1,0)*u(0) + A(1,1)*u(1) + A(1,2)*u(2);
664  v(2) = A(2,0)*u(0) + A(2,1)*u(1) + A(2,2)*u(2);
665  break;
666 
667  case 2:
668  v(0) = A(0,0)*u(0) + A(0,1)*u(1);
669  v(1) = A(1,0)*u(0) + A(1,1)*u(1);
670  break;
671 
672  }
673 
674  return v;
675 }
676 
677 //
678 // R^N vector tensor product v = u A
679 // \param A tensor
680 // \param u vector
681 // \return \f$ u A = A^T u \f$
682 //
683 template<typename S, typename T>
684 inline
685 Vector<typename Promote<S, T>::type>
686 dot(Vector<S> const & u, Tensor<T> const & A)
687 {
688  Index const
689  N = A.get_dimension();
690 
691  assert(u.get_dimension() == N);
692 
693  Vector<typename Promote<S, T>::type>
694  v(N);
695 
696  switch (N) {
697 
698  default:
699  for (Index i = 0; i < N; ++i) {
700 
701  typename Promote<S, T>::type
702  s = 0.0;
703 
704  for (Index p = 0; p < N; ++p) {
705  s += A(p, i) * u(p);
706  }
707  v(i) = s;
708  }
709  break;
710 
711  case 3:
712  v(0) = A(0,0)*u(0) + A(1,0)*u(1) + A(2,0)*u(2);
713  v(1) = A(0,1)*u(0) + A(1,1)*u(1) + A(2,1)*u(2);
714  v(2) = A(0,2)*u(0) + A(1,2)*u(1) + A(2,2)*u(2);
715  break;
716 
717  case 2:
718  v(0) = A(0,0)*u(0) + A(1,0)*u(1);
719  v(1) = A(0,1)*u(0) + A(1,1)*u(1);
720  break;
721 
722  }
723 
724  return v;
725 }
726 
727 //
728 // R^N tensor tensor product C = A B
729 // \param A tensor
730 // \param B tensor
731 // \return a tensor \f$ A \cdot B \f$
732 //
733 template<typename S, typename T>
734 inline
735 Tensor<typename Promote<S, T>::type>
736 dot(Tensor<S> const & A, Tensor<T> const & B)
737 {
738  Index const
739  N = A.get_dimension();
740 
741  assert(B.get_dimension() == N);
742 
743  Tensor<typename Promote<S, T>::type>
744  C(N);
745 
746  switch (N) {
747 
748  default:
749  for (Index i = 0; i < N; ++i) {
750  for (Index j = 0; j < N; ++j) {
751 
752  typename Promote<S, T>::type
753  s = 0.0;
754 
755  for (Index p = 0; p < N; ++p) {
756  s += A(i, p) * B(p, j);
757  }
758  C(i, j) = s;
759  }
760  }
761  break;
762 
763  case 3:
764  C(0,0) = A(0,0)*B(0,0) + A(0,1)*B(1,0) + A(0,2)*B(2,0);
765  C(0,1) = A(0,0)*B(0,1) + A(0,1)*B(1,1) + A(0,2)*B(2,1);
766  C(0,2) = A(0,0)*B(0,2) + A(0,1)*B(1,2) + A(0,2)*B(2,2);
767 
768  C(1,0) = A(1,0)*B(0,0) + A(1,1)*B(1,0) + A(1,2)*B(2,0);
769  C(1,1) = A(1,0)*B(0,1) + A(1,1)*B(1,1) + A(1,2)*B(2,1);
770  C(1,2) = A(1,0)*B(0,2) + A(1,1)*B(1,2) + A(1,2)*B(2,2);
771 
772  C(2,0) = A(2,0)*B(0,0) + A(2,1)*B(1,0) + A(2,2)*B(2,0);
773  C(2,1) = A(2,0)*B(0,1) + A(2,1)*B(1,1) + A(2,2)*B(2,1);
774  C(2,2) = A(2,0)*B(0,2) + A(2,1)*B(1,2) + A(2,2)*B(2,2);
775  break;
776 
777  case 2:
778  C(0,0) = A(0,0)*B(0,0) + A(0,1)*B(1,0);
779  C(0,1) = A(0,0)*B(0,1) + A(0,1)*B(1,1);
780 
781  C(1,0) = A(1,0)*B(0,0) + A(1,1)*B(1,0);
782  C(1,1) = A(1,0)*B(0,1) + A(1,1)*B(1,1);
783  break;
784 
785  }
786 
787  return C;
788 }
789 
790 //
791 // R^N tensor tensor product C = A^T B
792 // \param A tensor
793 // \param B tensor
794 // \return a tensor \f$ A^T \cdot B \f$
795 //
796 template<typename S, typename T>
797 inline
798 Tensor<typename Promote<S, T>::type>
799 t_dot(Tensor<S> const & A, Tensor<T> const & B)
800 {
801  Index const
802  N = A.get_dimension();
803 
804  assert(B.get_dimension() == N);
805 
806  Tensor<typename Promote<S, T>::type>
807  C(N);
808 
809  switch (N) {
810 
811  default:
812  for (Index i = 0; i < N; ++i) {
813  for (Index j = 0; j < N; ++j) {
814 
815  typename Promote<S, T>::type
816  s = 0.0;
817 
818  for (Index p = 0; p < N; ++p) {
819  s += A(p, i) * B(p, j);
820  }
821  C(i, j) = s;
822  }
823  }
824  break;
825 
826  case 3:
827  C(0,0) = A(0,0)*B(0,0) + A(1,0)*B(1,0) + A(2,0)*B(2,0);
828  C(0,1) = A(0,0)*B(0,1) + A(1,0)*B(1,1) + A(2,0)*B(2,1);
829  C(0,2) = A(0,0)*B(0,2) + A(1,0)*B(1,2) + A(2,0)*B(2,2);
830 
831  C(1,0) = A(0,1)*B(0,0) + A(1,1)*B(1,0) + A(2,1)*B(2,0);
832  C(1,1) = A(0,1)*B(0,1) + A(1,1)*B(1,1) + A(2,1)*B(2,1);
833  C(1,2) = A(0,1)*B(0,2) + A(1,1)*B(1,2) + A(2,1)*B(2,2);
834 
835  C(2,0) = A(0,2)*B(0,0) + A(1,2)*B(1,0) + A(2,2)*B(2,0);
836  C(2,1) = A(0,2)*B(0,1) + A(1,2)*B(1,1) + A(2,2)*B(2,1);
837  C(2,2) = A(0,2)*B(0,2) + A(1,2)*B(1,2) + A(2,2)*B(2,2);
838  break;
839 
840  case 2:
841  C(0,0) = A(0,0)*B(0,0) + A(1,0)*B(1,0);
842  C(0,1) = A(0,0)*B(0,1) + A(1,0)*B(1,1);
843 
844  C(1,0) = A(0,1)*B(0,0) + A(1,1)*B(1,0);
845  C(1,1) = A(0,1)*B(0,1) + A(1,1)*B(1,1);
846  break;
847 
848  }
849 
850  return C;
851 }
852 
853 //
854 // R^N tensor tensor product C = A B^T
855 // \param A tensor
856 // \param B tensor
857 // \return a tensor \f$ A \cdot B^T \f$
858 //
859 template<typename S, typename T>
860 inline
861 Tensor<typename Promote<S, T>::type>
862 dot_t(Tensor<S> const & A, Tensor<T> const & B)
863 {
864  Index const
865  N = A.get_dimension();
866 
867  assert(B.get_dimension() == N);
868 
869  Tensor<typename Promote<S, T>::type>
870  C(N);
871 
872  switch (N) {
873 
874  default:
875  for (Index i = 0; i < N; ++i) {
876  for (Index j = 0; j < N; ++j) {
877 
878  typename Promote<S, T>::type
879  s = 0.0;
880 
881  for (Index p = 0; p < N; ++p) {
882  s += A(i, p) * B(j, p);
883  }
884  C(i, j) = s;
885  }
886  }
887  break;
888 
889  case 3:
890  C(0,0) = A(0,0)*B(0,0) + A(0,1)*B(0,1) + A(0,2)*B(0,2);
891  C(0,1) = A(0,0)*B(1,0) + A(0,1)*B(1,1) + A(0,2)*B(1,2);
892  C(0,2) = A(0,0)*B(2,0) + A(0,1)*B(2,1) + A(0,2)*B(2,2);
893 
894  C(1,0) = A(1,0)*B(0,0) + A(1,1)*B(0,1) + A(1,2)*B(0,2);
895  C(1,1) = A(1,0)*B(1,0) + A(1,1)*B(1,1) + A(1,2)*B(1,2);
896  C(1,2) = A(1,0)*B(2,0) + A(1,1)*B(2,1) + A(1,2)*B(2,2);
897 
898  C(2,0) = A(2,0)*B(0,0) + A(2,1)*B(0,1) + A(2,2)*B(0,2);
899  C(2,1) = A(2,0)*B(1,0) + A(2,1)*B(1,1) + A(2,2)*B(1,2);
900  C(2,2) = A(2,0)*B(2,0) + A(2,1)*B(2,1) + A(2,2)*B(2,2);
901  break;
902 
903  case 2:
904  C(0,0) = A(0,0)*B(0,0) + A(0,1)*B(0,1);
905  C(0,1) = A(0,0)*B(1,0) + A(0,1)*B(1,1);
906 
907  C(1,0) = A(1,0)*B(0,0) + A(1,1)*B(0,1);
908  C(1,1) = A(1,0)*B(1,0) + A(1,1)*B(1,1);
909  break;
910 
911  }
912 
913  return C;
914 }
915 
916 //
917 // R^N tensor tensor product C = A^T B^T
918 // \param A tensor
919 // \param B tensor
920 // \return a tensor \f$ A^T \cdot B^T \f$
921 //
922 template<typename S, typename T>
923 inline
924 Tensor<typename Promote<S, T>::type>
925 t_dot_t(Tensor<S> const & A, Tensor<T> const & B)
926 {
927  Index const
928  N = A.get_dimension();
929 
930  assert(B.get_dimension() == N);
931 
932  Tensor<typename Promote<S, T>::type>
933  C(N);
934 
935  switch (N) {
936 
937  default:
938  for (Index i = 0; i < N; ++i) {
939  for (Index j = 0; j < N; ++j) {
940 
941  typename Promote<S, T>::type
942  s = 0.0;
943 
944  for (Index p = 0; p < N; ++p) {
945  s += A(p, i) * B(j, p);
946  }
947  C(i, j) = s;
948  }
949  }
950  break;
951 
952  case 3:
953  C(0,0) = A(0,0)*B(0,0) + A(1,0)*B(0,1) + A(2,0)*B(0,2);
954  C(0,1) = A(0,0)*B(1,0) + A(1,0)*B(1,1) + A(2,0)*B(1,2);
955  C(0,2) = A(0,0)*B(2,0) + A(1,0)*B(2,1) + A(2,0)*B(2,2);
956 
957  C(1,0) = A(0,1)*B(0,0) + A(1,1)*B(0,1) + A(2,1)*B(0,2);
958  C(1,1) = A(0,1)*B(1,0) + A(1,1)*B(1,1) + A(2,1)*B(1,2);
959  C(1,2) = A(0,1)*B(2,0) + A(1,1)*B(2,1) + A(2,1)*B(2,2);
960 
961  C(2,0) = A(0,2)*B(0,0) + A(1,2)*B(0,1) + A(2,2)*B(0,2);
962  C(2,1) = A(0,2)*B(1,0) + A(1,2)*B(1,1) + A(2,2)*B(1,2);
963  C(2,2) = A(0,2)*B(2,0) + A(1,2)*B(2,1) + A(2,2)*B(2,2);
964  break;
965 
966  case 2:
967  C(0,0) = A(0,0)*B(0,0) + A(1,0)*B(0,1);
968  C(0,1) = A(0,0)*B(1,0) + A(1,0)*B(1,1);
969 
970  C(1,0) = A(0,1)*B(0,0) + A(1,1)*B(0,1);
971  C(1,1) = A(0,1)*B(1,0) + A(1,1)*B(1,1);
972  break;
973 
974  }
975 
976  return C;
977 }
978 
979 //
980 // R^N tensor tensor double dot product (contraction)
981 // \param A tensor
982 // \param B tensor
983 // \return a scalar \f$ A : B \f$
984 //
985 template<typename S, typename T>
986 inline
987 typename Promote<S, T>::type
988 dotdot(Tensor<S> const & A, Tensor<T> const & B)
989 {
990  Index const
991  N = A.get_dimension();
992 
993  assert(B.get_dimension() == N);
994 
995  typename Promote<S, T>::type
996  s = 0.0;
997 
998  switch (N) {
999 
1000  default:
1001  for (Index p = 0; p < N; ++p) {
1002  for (Index q = 0; q < N; ++q) {
1003  s += A(p, q) * B(p, q);
1004  }
1005  }
1006  break;
1007 
1008  case 3:
1009  s+= A(0,0)*B(0,0) + A(0,1)*B(0,1) + A(0,2)*B(0,2);
1010  s+= A(1,0)*B(1,0) + A(1,1)*B(1,1) + A(1,2)*B(1,2);
1011  s+= A(2,0)*B(2,0) + A(2,1)*B(2,1) + A(2,2)*B(2,2);
1012  break;
1013 
1014  case 2:
1015  s+= A(0,0)*B(0,0) + A(0,1)*B(0,1);
1016  s+= A(1,0)*B(1,0) + A(1,1)*B(1,1);
1017  break;
1018 
1019  }
1020 
1021  return s;
1022 }
1023 
1024 //
1025 // R^N dyad
1026 // \param u vector
1027 // \param v vector
1028 // \return \f$ u \otimes v \f$
1029 //
1030 template<typename S, typename T>
1031 inline
1032 Tensor<typename Promote<S, T>::type>
1033 dyad(Vector<S> const & u, Vector<T> const & v)
1034 {
1035  Index const
1036  N = u.get_dimension();
1037 
1038  assert(v.get_dimension() == N);
1039 
1040  Tensor<typename Promote<S, T>::type>
1041  A(N);
1042 
1043  switch (N) {
1044 
1045  default:
1046  for (Index i = 0; i < N; ++i) {
1047 
1048  typename Promote<S, T>::type const
1049  s = u(i);
1050 
1051  for (Index j = 0; j < N; ++j) {
1052  A(i, j) = s * v(j);
1053  }
1054  }
1055  break;
1056 
1057  case 3:
1058  A(0,0) = u(0) * v(0);
1059  A(0,1) = u(0) * v(1);
1060  A(0,2) = u(0) * v(2);
1061 
1062  A(1,0) = u(1) * v(0);
1063  A(1,1) = u(1) * v(1);
1064  A(1,2) = u(1) * v(2);
1065 
1066  A(2,0) = u(2) * v(0);
1067  A(2,1) = u(2) * v(1);
1068  A(2,2) = u(2) * v(2);
1069  break;
1070 
1071  case 2:
1072  A(0,0) = u(0) * v(0);
1073  A(0,1) = u(0) * v(1);
1074 
1075  A(1,0) = u(1) * v(0);
1076  A(1,1) = u(1) * v(1);
1077  break;
1078 
1079  }
1080 
1081  return A;
1082 }
1083 
1084 //
1085 // R^N bun operator, just for Jay
1086 // \param u vector
1087 // \param v vector
1088 // \return \f$ u \otimes v \f$
1089 //
1090 template<typename S, typename T>
1091 inline
1092 Tensor<typename Promote<S, T>::type>
1093 bun(Vector<S> const & u, Vector<T> const & v)
1094 {
1095  return dyad(u, v);
1096 }
1097 
1098 //
1099 // R^N tensor product
1100 // \param u vector
1101 // \param v vector
1102 // \return \f$ u \otimes v \f$
1103 //
1104 template<typename S, typename T>
1105 inline
1106 Tensor<typename Promote<S, T>::type>
1107 tensor(Vector<S> const & u, Vector<T> const & v)
1108 {
1109  return dyad(u, v);
1110 }
1111 
1112 //
1113 // R^N diagonal tensor from vector
1114 // \param v vector
1115 // \return A = diag(v)
1116 //
1117 template<typename T>
1118 Tensor<T>
1119 diag(Vector<T> const & v)
1120 {
1121  Index const
1122  N = v.get_dimension();
1123 
1124  Tensor<T>
1125  A = zero<T>(N);
1126 
1127  switch (N) {
1128 
1129  default:
1130  for (Index i = 0; i < N; ++i) {
1131  A(i, i) = v(i);
1132  }
1133  break;
1134 
1135  case 3:
1136  A(0,0) = v(0);
1137  A(1,1) = v(1);
1138  A(2,2) = v(2);
1139  break;
1140 
1141  case 2:
1142  A(0,0) = v(0);
1143  A(1,1) = v(1);
1144  break;
1145 
1146  }
1147 
1148  return A;
1149 }
1150 
1151 //
1152 // R^N diagonal of tensor in a vector
1153 // \param A tensor
1154 // \return v = diag(A)
1155 //
1156 template<typename T>
1157 Vector<T>
1158 diag(Tensor<T> const & A)
1159 {
1160  Index const
1161  N = A.get_dimension();
1162 
1163  Vector<T>
1164  v(N);
1165 
1166  switch (N) {
1167 
1168  default:
1169  for (Index i = 0; i < N; ++i) {
1170  v(i) = A(i, i);
1171  }
1172  break;
1173 
1174  case 3:
1175  v(0) = A(0,0);
1176  v(1) = A(1,1);
1177  v(2) = A(2,2);
1178  break;
1179 
1180  case 2:
1181  v(0) = A(0,0);
1182  v(1) = A(1,1);
1183  break;
1184 
1185  }
1186 
1187  return v;
1188 }
1189 
1190 //
1191 // Zero 2nd-order tensor
1192 // All components are zero
1193 //
1194 template<typename T>
1195 inline
1196 Tensor<T> const
1197 zero(Index const N)
1198 {
1199  return Tensor<T>(N, ZEROS);
1200 }
1201 
1202 //
1203 // R^N 2nd-order identity tensor
1204 //
1205 template<typename T>
1206 inline
1207 Tensor<T> const
1208 identity(Index const N)
1209 {
1210  Tensor<T>
1211  A(N, ZEROS);
1212 
1213  switch (N) {
1214 
1215  default:
1216  for (Index i = 0; i < N; ++i) {
1217  A(i, i) = 1.0;
1218  }
1219  break;
1220 
1221  case 3:
1222  A(0,0) = 1.0;
1223  A(1,1) = 1.0;
1224  A(2,2) = 1.0;
1225  break;
1226 
1227  case 2:
1228  A(0,0) = 1.0;
1229  A(1,1) = 1.0;
1230  break;
1231 
1232  }
1233 
1234  return A;
1235 }
1236 
1237 //
1238 // R^N 2nd-order identity tensor, à la Matlab
1239 //
1240 template<typename T>
1241 inline
1242 Tensor<T> const
1243 eye(Index const N)
1244 {
1245  return identity<T>(N);
1246 }
1247 
1248 //
1249 // R^N 2nd-order tensor transpose
1250 //
1251 template<typename T>
1252 inline
1253 Tensor<T>
1254 transpose(Tensor<T> const & A)
1255 {
1256  Index const
1257  N = A.get_dimension();
1258 
1259  Tensor<T>
1260  B = A;
1261 
1262  switch (N) {
1263 
1264  default:
1265  for (Index i = 0; i < N; ++i) {
1266  for (Index j = i + 1; j < N; ++j) {
1267  std::swap(B(i, j), B(j, i));
1268  }
1269  }
1270  break;
1271 
1272  case 3:
1273  std::swap(B(0,1), B(1,0));
1274  std::swap(B(0,2), B(2,0));
1275 
1276  std::swap(B(1,2), B(2,1));
1277 
1278  break;
1279 
1280  case 2:
1281  std::swap(B(0,1), B(1,0));
1282 
1283  break;
1284 
1285  }
1286 
1287  return B;
1288 }
1289 
1290 //
1291 // R^N symmetric part of 2nd-order tensor
1292 // \return \f$ \frac{1}{2}(A + A^T) \f$
1293 //
1294 template<typename T>
1295 inline
1296 Tensor<T>
1297 sym(Tensor<T> const & A)
1298 {
1299  Index const
1300  N = A.get_dimension();
1301 
1302  Tensor<T>
1303  B(N);
1304 
1305  switch (N) {
1306 
1307  default:
1308  B = 0.5 * (A + transpose(A));
1309  break;
1310 
1311  case 3:
1312  {
1313  T const & s00 = A(0,0);
1314  T const & s11 = A(1,1);
1315  T const & s22 = A(2,2);
1316 
1317  T const & s01 = 0.5 * (A(0,1) + A(1,0));
1318  T const & s02 = 0.5 * (A(0,2) + A(2,0));
1319  T const & s12 = 0.5 * (A(1,2) + A(2,1));
1320 
1321  B(0,0) = s00;
1322  B(0,1) = s01;
1323  B(0,2) = s02;
1324 
1325  B(1,0) = s01;
1326  B(1,1) = s11;
1327  B(1,2) = s12;
1328 
1329  B(2,0) = s02;
1330  B(2,1) = s12;
1331  B(2,2) = s22;
1332  }
1333  break;
1334 
1335  case 2:
1336  {
1337  T const & s00 = A(0,0);
1338  T const & s11 = A(1,1);
1339 
1340  T const & s01 = 0.5 * (A(0,1) + A(1,0));
1341 
1342  B(0,0) = s00;
1343  B(0,1) = s01;
1344 
1345  B(1,0) = s01;
1346  B(1,1) = s11;
1347  }
1348  break;
1349 
1350  }
1351 
1352  return B;
1353 }
1354 
1355 //
1356 // R^N skew symmetric part of 2nd-order tensor
1357 // \return \f$ \frac{1}{2}(A - A^T) \f$
1358 //
1359 template<typename T>
1360 inline
1361 Tensor<T>
1362 skew(Tensor<T> const & A)
1363 {
1364  Index const
1365  N = A.get_dimension();
1366 
1367  Tensor<T>
1368  B(N);
1369 
1370  switch (N) {
1371 
1372  default:
1373  B = 0.5 * (A - transpose(A));
1374  break;
1375 
1376  case 3:
1377  {
1378  T const & s01 = 0.5*(A(0,1)-A(1,0));
1379  T const & s02 = 0.5*(A(0,2)-A(2,0));
1380  T const & s12 = 0.5*(A(1,2)-A(2,1));
1381 
1382  B(0,0) = 0.0;
1383  B(0,1) = s01;
1384  B(0,2) = s02;
1385 
1386  B(1,0) = -s01;
1387  B(1,1) = 0.0;
1388  B(1,2) = s12;
1389 
1390  B(2,0) = -s02;
1391  B(2,1) = -s12;
1392  B(2,2) = 0.0;
1393  }
1394  break;
1395 
1396  case 2:
1397  {
1398  T const & s01 = 0.5*(A(0,1)-A(1,0));
1399 
1400  B(0,0) = 0.0;
1401  B(0,1) = s01;
1402 
1403  B(1,0) = -s01;
1404  B(1,1) = 0.0;
1405  }
1406  break;
1407 
1408  }
1409 
1410  return B;
1411 }
1412 
1413 //
1414 // R^N skew symmetric 2nd-order tensor from vector, undefined
1415 // for N!=3.
1416 // \param u vector
1417 //
1418 template<typename T>
1419 inline
1420 Tensor<T>
1421 skew(Vector<T> const & u)
1422 {
1423  Index const
1424  N = u.get_dimension();
1425 
1426  Tensor<T>
1427  A(N);
1428 
1429  switch (N) {
1430 
1431  case 3:
1432  A(0,0) = 0.0;
1433  A(0,1) = -u(2);
1434  A(0,2) = u(1);
1435 
1436  A(1,0) = u(2);
1437  A(1,1) = 0.0;
1438  A(1,2) = -u(0);
1439 
1440  A(2,0) = -u(1);
1441  A(2,1) = u(0);
1442  A(2,2) = 0.0;
1443  break;
1444 
1445  default:
1446  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
1447  std::cerr << std::endl;
1448  std::cerr << "Skew from vector undefined for R^" << N;
1449  std::cerr << std::endl;
1450  exit(1);
1451  break;
1452 
1453  }
1454 
1455  return A;
1456 }
1457 
1458 } // namespace Intrepid
1459 
1460 #endif // Intrepid_MiniTensor_Tensor_i_h
void set_dimension(Index const dimension)
void fill(ComponentValue value)
void fill(ComponentValue value)
T const & operator()(Index const i, Index const j) const