Intrepid
Intrepid_MiniTensor_LinearAlgebra.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_LinearAlgebra_t_h)
43 #define Intrepid_MiniTensor_LinearAlgebra_t_h
44 
45 namespace Intrepid {
46 
47 //
48 // R^N 2nd-order tensor inverse
49 // Gauss-Jordan elimination. Warning: full pivoting
50 // for small tensors. Use Teuchos LAPACK interface for
51 // more efficient and robust techniques.
52 // \param A nonsingular tensor
53 // \return \f$ A^{-1} \f$
54 //
55 template<typename T>
56 Tensor<T>
57 inverse(Tensor<T> const & A)
58 {
59  Index const
60  N = A.get_dimension();
61 
62  Tensor<T> S = A;
63  Tensor<T> B = identity<T>(N);
64 
65  typedef std::set<Index> IndexSet;
66  typedef std::set<Index>::const_iterator IndexIter;
67 
68  IndexSet intact_rows;
69  IndexSet intact_cols;
70 
71  for (Index k = 0; k < N; ++k) {
72  intact_rows.insert(k);
73  intact_cols.insert(k);
74  }
75 
76  // Gauss-Jordan elimination with full pivoting
77  for (Index k = 0; k < N; ++k) {
78 
79  // Determine full pivot
80  T pivot = 0.0;
81 
82  IndexIter pivot_row_iter = intact_rows.begin();
83  IndexIter pivot_col_iter = intact_cols.begin();
84 
85  for (IndexIter rows_iter = intact_rows.begin();
86  rows_iter != intact_rows.end(); ++rows_iter) {
87 
88  for (IndexIter cols_iter = intact_cols.begin();
89  cols_iter != intact_cols.end(); ++cols_iter) {
90 
91  Index const row = *rows_iter;
92  Index const col = *cols_iter;
93  T const s = std::abs(S(row, col));
94 
95  if (s > pivot) {
96 
97  pivot_row_iter = rows_iter;
98  pivot_col_iter = cols_iter;
99 
100  pivot = s;
101 
102  }
103 
104  }
105 
106  }
107 
108  Index const pivot_row = *pivot_row_iter;
109  Index const pivot_col = *pivot_col_iter;
110 
111  // Gauss-Jordan elimination
112  T const t = S(pivot_row, pivot_col);
113 
114  if (t == 0.0) {
115  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
116  std::cerr << std::endl;
117  std::cerr << "Inverse of singular tensor.";
118  std::cerr << std::endl;
119  exit(1);
120  }
121 
122  for (Index j = 0; j < N; ++j) {
123  S(pivot_row, j) /= t;
124  B(pivot_row, j) /= t;
125  }
126 
127  for (Index i = 0; i < N; ++i) {
128  if (i == pivot_row) continue;
129 
130  T const c = S(i, pivot_col);
131 
132  for (Index j = 0; j < N; ++j) {
133  S(i, j) -= c * S(pivot_row, j);
134  B(i, j) -= c * B(pivot_row, j);
135  }
136  }
137 
138  // Eliminate current row and col from intact rows and cols
139  intact_rows.erase(pivot_row_iter);
140  intact_cols.erase(pivot_col_iter);
141 
142  }
143 
144  Tensor<T>
145  X = t_dot(S, B);
146 
147  return X;
148 }
149 
150 //
151 // R^N Subtensor
152 // \param A tensor
153 // \param i index
154 // \param j index
155 // \return Subtensor with i-row and j-col deleted.
156 //
157 template<typename T>
158 Tensor<T>
159 subtensor(Tensor<T> const & A, Index const i, Index const j)
160 {
161  Index const
162  N = A.get_dimension();
163 
164  assert(i < N);
165  assert(j < N);
166 
167  Tensor<T> B(N - 1);
168 
169  Index p = 0;
170  Index q = 0;
171  for (Index m = 0; m < N; ++m) {
172  if (m == i) continue;
173  for (Index n = 0; n < N; ++n) {
174  if (n == j) continue;
175  B(p, q) = A(m, n);
176  ++q;
177  }
178  ++p;
179  }
180 
181  return B;
182 }
183 
184 //
185 // Exponential map
186 //
187 template<typename T>
188 Tensor<T>
189 exp(Tensor<T> const & A)
190 {
191  return exp_pade(A);
192 }
193 
194 //
195 // R^N exponential map by Taylor series, radius of convergence is infinity
196 // \param A tensor
197 // \return \f$ \exp A \f$
198 //
199 template<typename T>
200 Tensor<T>
201 exp_taylor(Tensor<T> const & A)
202 {
203  Index const
204  max_iter = 128;
205 
206  T const
207  tol = machine_epsilon<T>();
208 
209  Index const
210  N = A.get_dimension();
211 
212  Tensor<T>
213  term = identity<T>(N);
214 
215  // Relative error taken wrt to the first term, which is I and norm = 1
216  T
217  relative_error = 1.0;
218 
219  Tensor<T>
220  B = term;
221 
222  Index
223  k = 0;
224 
225  while (relative_error > tol && k < max_iter) {
226  term = static_cast<T>(1.0 / (k + 1.0)) * term * A;
227  B = B + term;
228  relative_error = norm_1(term);
229  ++k;
230  }
231 
232  return B;
233 }
234 
235 namespace {
236 
237 //
238 // Scaling parameter theta for scaling and squaring exponential.
239 //
240 template<typename T>
241 T
242 scaling_squaring_theta(Index const order)
243 {
244  assert(order > 0 && order < 22);
245 
246  T const theta[] =
247  {
248  0.0e-0, 3.7e-8, 5.3e-4, 1.5e-2, 8.5e-2, 2.5e-1, 5.4e-1, 9.5e-1,
249  1.5e-0, 2.1e-0, 2.8e-0, 3.6e-0, 4.5e-0, 5.4e-0, 6.3e-0, 7.3e-0,
250  8.4e-0, 9,4e-0, 1.1e+1, 1.2e+1, 1.3e+1, 1.4e+1
251  };
252 
253  return theta[order];
254 }
255 
256 //
257 // Polynomial coefficients for Padé approximants.
258 //
259 template<typename T>
260 T
261 polynomial_coefficient(Index const order, Index const index)
262 {
263  assert(index <= order);
264 
265  T
266  c = 0.0;
267 
268  switch (order) {
269 
270  default:
271  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
272  std::cerr << std::endl;
273  std::cerr << "Wrong order in Pade' polynomial coefficient: ";
274  std::cerr << order << std::endl;
275  exit(1);
276  break;
277 
278  case 3:
279  {
280  T const
281  b[] = {120.0, 60.0, 12.0, 1.0};
282 
283  c = b[index];
284  }
285  break;
286 
287  case 5:
288  {
289  T const
290  b[] = {30240.0, 15120.0, 3360.0, 420.0, 30.0, 1.0};
291 
292  c = b[index];
293  }
294  break;
295 
296  case 7:
297  {
298  T const
299  b[] = {17297280.0, 8648640.0, 1995840.0, 277200.0, 25200.0, 1512.0,
300  56.0, 1.0};
301 
302  c = b[index];
303  }
304  break;
305 
306  case 9:
307  {
308  T const
309  b[] = {17643225600.0, 8821612800.0, 2075673600.0, 302702400.0,
310  30270240.0, 2162160.0, 110880.0, 3960.0, 90.0, 1.0};
311 
312  c = b[index];
313  }
314  break;
315 
316  case 13:
317  {
318  T const
319  b[] = {64764752532480000.0, 32382376266240000.0, 7771770303897600.0,
320  1187353796428800.0, 129060195264000.0, 10559470521600.0,
321  670442572800.0, 33522128640.0, 1323241920.0, 40840800.0,
322  960960.0, 16380.0, 182.0, 1.0};
323 
324  c = b[index];
325  }
326  break;
327 
328  }
329 
330  return c;
331 }
332 
333 //
334 // Padé approximant polynomial odd and even terms.
335 //
336 template<typename T>
337 std::pair<Tensor<T>, Tensor<T> >
338 pade_polynomial_terms(Tensor<T> const & A, Index const order)
339 {
340  Index const
341  N = A.get_dimension();
342 
343  Tensor<T>
344  B = identity<T>(N);
345 
346  Tensor<T>
347  U = polynomial_coefficient<Real>(order, 1) * B;
348 
349  Tensor<T>
350  V = polynomial_coefficient<Real>(order, 0) * B;
351 
352  Tensor<T> const
353  A2 = A * A;
354 
355  for (Index i = 3; i <= order; i += 2) {
356 
357  B = B * A2;
358 
359  Tensor<T> const
360  O = polynomial_coefficient<Real>(order, i) * B;
361 
362  Tensor<T> const
363  E = polynomial_coefficient<Real>(order, i - 1) * B;
364 
365  U += O;
366 
367  V += E;
368 
369  }
370 
371  U = A * U;
372 
373  return std::make_pair(U, V);
374 }
375 
376 //
377 // Compute a non-negative integer power of a tensor by binary manipulation.
378 //
379 template<typename T>
380 Tensor<T>
381 binary_powering(Tensor<T> const & A, Index const exponent)
382 {
383  if (exponent == 0) return eye<T>(A.get_dimension());
384 
385  Index const
386  rightmost_bit = 1;
387 
388  Index const
389  number_digits = std::numeric_limits<Index>::digits;
390 
391  Index const
392  leftmost_bit = rightmost_bit << (number_digits - 1);
393 
394  Index
395  t = 0;
396 
397  for (Index j = 0; j < number_digits; ++j) {
398 
399  if (((exponent << j) & leftmost_bit) != 0) {
400 
401  t = number_digits - j - 1;
402  break;
403 
404  }
405 
406  }
407 
408  Tensor<T>
409  P = A;
410 
411  Index
412  i = 0;
413 
414  Index
415  m = exponent;
416 
417  while ((m & rightmost_bit) == 0) {
418  P = P * P;
419  ++i;
420  m = m >> 1;
421  }
422 
423  Tensor<T>
424  X = P;
425 
426  for (Index j = i + 1; j <= t; ++j) {
427  P = P * P;
428 
429  if (((exponent >> j) & rightmost_bit) != 0) {
430  X = X * P;
431  }
432  }
433 
434  return X;
435 }
436 
437 } // anonymous namespace
438 
439 //
440 // Exponential map by squaring and scaling and Padé approximants.
441 // See algorithm 10.20 in Functions of Matrices, N.J. Higham, SIAM, 2008.
442 // \param A tensor
443 // \return \f$ \exp A \f$
444 //
445 template<typename T>
446 Tensor<T>
447 exp_pade(Tensor<T> const & A)
448 {
449  Index const
450  N = A.get_dimension();
451 
452  Index const
453  orders[] = {3, 5, 7, 9, 13};
454 
455  Index const
456  number_orders = 5;
457 
458  Index const
459  highest_order = orders[number_orders - 1];
460 
461  Tensor<T>
462  B;
463 
464  Real const
465  norm = Sacado::ScalarValue<T>::eval((norm_1(A)));
466 
467  for (Index i = 0; i < number_orders; ++i) {
468 
469  Index const
470  order = orders[i];
471 
472  Real const
473  theta = scaling_squaring_theta<Real>(order);
474 
475  if (order < highest_order && norm < theta) {
476 
477  Tensor<T>
478  U;
479 
480  Tensor<T>
481  V;
482 
483  boost::tie(U, V) = pade_polynomial_terms(A, order);
484 
485  B = inverse(V - U) * (U + V);
486 
487  break;
488 
489  } else if (order == highest_order) {
490 
491  Real const
492  theta_highest = scaling_squaring_theta<Real>(order);
493 
494  Index const
495  power_two = Index(std::ceil(log2(norm / theta_highest)));
496 
497  Real
498  scale = 1.0;
499 
500  for (Index j = 0; j < power_two; ++j) {
501  scale /= 2.0;
502  }
503 
504  Tensor<T> const
505  I = identity<T>(N);
506 
507  Tensor<T> const
508  A1 = scale * A;
509 
510  Tensor<T> const
511  A2 = A1 * A1;
512 
513  Tensor<T> const
514  A4 = A2 * A2;
515 
516  Tensor<T> const
517  A6 = A2 * A4;
518 
519  Real const b0 = polynomial_coefficient<Real>(order, 0);
520  Real const b1 = polynomial_coefficient<Real>(order, 1);
521  Real const b2 = polynomial_coefficient<Real>(order, 2);
522  Real const b3 = polynomial_coefficient<Real>(order, 3);
523  Real const b4 = polynomial_coefficient<Real>(order, 4);
524  Real const b5 = polynomial_coefficient<Real>(order, 5);
525  Real const b6 = polynomial_coefficient<Real>(order, 6);
526  Real const b7 = polynomial_coefficient<Real>(order, 7);
527  Real const b8 = polynomial_coefficient<Real>(order, 8);
528  Real const b9 = polynomial_coefficient<Real>(order, 9);
529  Real const b10 = polynomial_coefficient<Real>(order, 10);
530  Real const b11 = polynomial_coefficient<Real>(order, 11);
531  Real const b12 = polynomial_coefficient<Real>(order, 12);
532  Real const b13 = polynomial_coefficient<Real>(order, 13);
533 
534  Tensor<T> const
535  U = A1 * (
536  (A6 * (b13 * A6 + b11 * A4 + b9 * A2) +
537  b7 * A6 + b5 * A4 + b3 * A2 + b1 * I));
538 
539  Tensor<T> const
540  V = A6 * (b12 * A6 + b10 * A4 + b8 * A2) +
541  b6 * A6 + b4 * A4 + b2 * A2 + b0 * I;
542 
543  Tensor<T> const
544  R = inverse(V - U) * (U + V);
545 
546  Index const
547  exponent = (1U << power_two);
548 
549  B = binary_powering(R, exponent);
550 
551  }
552 
553  }
554 
555  return B;
556 }
557 
558 //
559 // Logarithmic map by Taylor series.
560 //
561 template<typename T>
562 Tensor<T>
563 log_taylor(Tensor<T> const & A)
564 {
565  Index const
566  max_iter = 128;
567 
568  T const
569  tol = machine_epsilon<T>();
570 
571  T const
572  norm_tensor = norm_1(A);
573 
574  Index const
575  N = A.get_dimension();
576 
577  Tensor<T> const
578  A_minus_I = A - identity<T>(N);
579 
580  Tensor<T>
581  term = A_minus_I;
582 
583  T
584  norm_term = norm_1(term);
585 
586  T
587  relative_error = norm_term / norm_tensor;
588 
589  Tensor<T>
590  B = term;
591 
592  Index
593  k = 1;
594 
595  while (relative_error > tol && k <= max_iter) {
596  term = static_cast<T>(- (k / (k + 1.0))) * term * A_minus_I;
597  B = B + term;
598  norm_term = norm_1(term);
599  relative_error = norm_term / norm_tensor;
600  ++k;
601  }
602 
603  return B;
604 }
605 
606 //
607 // Logarithmic map.
608 //
609 template<typename T>
610 Tensor<T>
611 log(Tensor<T> const & A)
612 {
613  return log_gregory(A);
614 }
615 
616 //
617 // Logarithmic map by Gregory series.
618 //
619 template<typename T>
620 Tensor<T>
621 log_gregory(Tensor<T> const & A)
622 {
623  Index const
624  max_iter = 128;
625 
626  T const
627  tol = machine_epsilon<T>();
628 
629  T const
630  norm_tensor = norm_1(A);
631 
632  Index const
633  N = A.get_dimension();
634 
635  Tensor<T> const
636  I_minus_A = identity<T>(N) - A;
637 
638  Tensor<T> const
639  I_plus_A = identity<T>(N) + A;
640 
641  Tensor<T>
642  term = I_minus_A * inverse(I_plus_A);
643 
644  T
645  norm_term = norm_1(term);
646 
647  T
648  relative_error = norm_term / norm_tensor;
649 
650  Tensor<T> const
651  C = term * term;
652 
653  Tensor<T>
654  B = term;
655 
656  Index
657  k = 1;
658 
659  while (relative_error > tol && k <= max_iter) {
660  term = static_cast<T>((2 * k - 1.0) / (2 * k + 1.0)) * term * C;
661  B = B + term;
662  norm_term = norm_1(term);
663  relative_error = norm_term / norm_tensor;
664  ++k;
665  }
666 
667  B = - 2.0 * B;
668 
669  return B;
670 }
671 
672 //
673 // Logarithmic map for symmetric tensor.
674 //
675 template<typename T>
676 Tensor<T>
677 log_sym(Tensor<T> const & A)
678 {
679  return log_eig_sym(A);
680 }
681 
682 //
683 // Logarithmic map for symmetric tensor using eigenvalue decomposition.
684 //
685 template<typename T>
686 Tensor<T>
687 log_eig_sym(Tensor<T> const & A)
688 {
689  Index const
690  N = A.get_dimension();
691 
692  Tensor<T>
693  V(N);
694 
695  Tensor<T>
696  D(N);
697 
698  boost::tie(V, D) = eig_sym(A);
699 
700  for (Index i = 0; i < N; ++i) {
701  D(i, i) = std::log(D(i, i));
702  }
703 
704  Tensor<T> const
705  B = dot_t(dot(V, D), V);
706 
707  return B;
708 }
709 
710 //
711 // R^N logarithmic map of a rotation. Not implemented yet.
712 // \param R with \f$ R \in SO(N) \f$
713 // \return \f$ r = \log R \f$ with \f$ r \in so(N) \f$
714 //
715 template<typename T>
716 Tensor<T>
717 log_rotation(Tensor<T> const & R)
718 {
719  Index const
720  N = R.get_dimension();
721 
722  //firewalls, make sure R \in SO(N)
723  assert(norm(dot_t(R,R) - eye<T>(N)) < 100.0 * machine_epsilon<T>());
724  assert(std::abs(det(R) - 1.0) < 100.0 * machine_epsilon<T>());
725 
726  // acos requires input between -1 and +1
727  T
728  cosine = 0.5 * (trace(R) - 1.0);
729 
730  if (cosine < -1.0) {
731  cosine = -1.0;
732  } else if(cosine > 1.0) {
733  cosine = 1.0;
734  }
735 
736  T
737  theta = std::acos(cosine);
738 
739  Tensor<T>
740  r(N);
741 
742  switch (N) {
743 
744  default:
745  std::cerr << "Logarithm of SO(N) N != 2,3 not implemented." << std::endl;
746  exit(1);
747  break;
748 
749  case 3:
750  if (theta == 0.0) {
751 
752  r = zero<T>(3);
753 
754  } else if (std::abs(cosine + 1.0) < 10.0 * machine_epsilon<T>()) {
755 
756  r = log_rotation_pi(R);
757 
758  } else {
759 
760  r = theta / std::sin(theta) * skew(R);
761 
762  }
763  break;
764 
765  case 2:
766  r(0,0) = 0.0;
767  r(0,1) = -theta;
768  r(1,0) = theta;
769  r(1,1) = 0.0;
770  break;
771 
772  }
773 
774  return r;
775 }
776 
777 // R^N Logarithmic map of a 180-degree rotation. Not implemented.
778 // \param R with \f$ R \in SO(N) \f$
779 // \return \f$ r = \log R \f$ with \f$ r \in so(N) \f$
780 //
781 template<typename T>
782 Tensor<T>
783 log_rotation_pi(Tensor<T> const & R)
784 {
785  Index const
786  N = R.get_dimension();
787 
788  T
789  cosine = 0.5*(trace(R) - 1.0);
790  // set firewall to make sure the rotation is indeed 180 degrees
791  assert(std::abs(cosine + 1.0) < 10.0 * machine_epsilon<T>());
792 
793  Tensor<T>
794  r(N);
795 
796  switch (N) {
797 
798  default:
799  std::cerr << "Logarithm of SO(N) N != 2,3 not implemented." << std::endl;
800  exit(1);
801  break;
802 
803  case 3:
804  {
805  // obtain U from R = LU
806  r = gaussian_elimination((R - identity<T>(3)));
807 
808  // backward substitution (for rotation exp(R) only)
809  T const tol = 10.0 * machine_epsilon<T>();
810 
811  Vector<T> normal(3);
812 
813  if (std::abs(r(2,2)) < tol){
814  normal(2) = 1.0;
815  } else {
816  normal(2) = 0.0;
817  }
818 
819  if (std::abs(r(1,1)) < tol){
820  normal(1) = 1.0;
821  } else {
822  normal(1) = -normal(2)*r(1,2)/r(1,1);
823  }
824 
825  if (std::abs(r(0,0)) < tol){
826  normal(0) = 1.0;
827  } else {
828  normal(0) = -normal(1)*r(0,1) - normal(2)*r(0,2)/r(0,0);
829  }
830 
831  normal = normal / norm(normal);
832 
833  r.fill(ZEROS);
834  r(0,1) = -normal(2);
835  r(0,2) = normal(1);
836  r(1,0) = normal(2);
837  r(1,2) = -normal(0);
838  r(2,0) = -normal(1);
839  r(2,1) = normal(0);
840 
841  T const pi = std::acos(-1.0);
842 
843  r = pi * r;
844  }
845  break;
846 
847  case 2:
848  {
849  T theta = std::acos(-1.0);
850 
851  if (R(0,0) > 0.0) {
852  theta = - theta;
853  }
854 
855  r(0,0) = 0.0;
856  r(0,1) = -theta;
857  r(1,0) = theta;
858  r(1,1) = 0.0;
859  }
860  break;
861 
862  }
863 
864  return r;
865 }
866 
867 // Gaussian Elimination with partial pivot
868 // \param matrix \f$ A \f$
869 // \return \f$ U \f$ where \f$ A = LU \f$
870 //
871 template<typename T>
872 Tensor<T>
873 gaussian_elimination(Tensor<T> const & A)
874 {
875  Index const
876  N = A.get_dimension();
877 
878  Tensor<T>
879  U = A;
880 
881  T const
882  tol = 10.0 * machine_epsilon<T>();
883 
884  Index i = 0;
885  Index j = 0;
886  Index i_max = 0;
887 
888  while ((i < N) && (j < N)) {
889  // find pivot in column j, starting in row i
890  i_max = i;
891  for (Index k = i + 1; k < N; ++k) {
892  if (std::abs(U(k,j)) > std::abs(U(i_max,j))) {
893  i_max = k;
894  }
895  }
896 
897  // Check if A(i_max,j) equal to or very close to 0
898  if (std::abs(U(i_max,j)) > tol){
899  // swap rows i and i_max and divide each entry in row i
900  // by U(i,j)
901  for (Index k = 0; k < N; ++k) {
902  std::swap(U(i,k), U(i_max,k));
903  }
904 
905  for (Index k = 0; k < N; ++k) {
906  U(i,k) = U(i,k) / U(i,j);
907  }
908 
909  for (Index l = i + 1; l < N; ++l) {
910  for (Index k = 0; k < N; ++k) {
911  U(l,k) = U(l,k) - U(l,i) * U(i,k) / U(i,i);
912  }
913  }
914  ++i;
915  }
916  ++j;
917  }
918 
919  return U;
920 }
921 
922 // Apply Givens-Jacobi rotation on the left in place.
923 // \param c and s for a rotation G in form [c, s; -s, c]
924 // \param A
925 //
926 template<typename T>
927 void
928 givens_left(T const & c, T const & s, Index i, Index k, Tensor<T> & A)
929 {
930  Index const
931  N = A.get_dimension();
932 
933  for (Index j = 0; j < N; ++j) {
934  T const t1 = A(i,j);
935  T const t2 = A(k,j);
936  A(i,j) = c * t1 - s * t2;
937  A(k,j) = s * t1 + c * t2;
938  }
939  return;
940 }
941 
942 // Apply Givens-Jacobi rotation on the right in place.
943 // \param A
944 // \param c and s for a rotation G in form [c, s; -s, c]
945 //
946 template<typename T>
947 void
948 givens_right(T const & c, T const & s, Index i, Index k, Tensor<T> & A)
949 {
950  Index const
951  N = A.get_dimension();
952 
953  for (Index j = 0; j < N; ++j) {
954  T const t1 = A(j,i);
955  T const t2 = A(j,k);
956  A(j,i) = c * t1 - s * t2;
957  A(j,k) = s * t1 + c * t2;
958  }
959  return;
960 }
961 
962 //
963 // R^N exponential map of a skew-symmetric tensor. Not implemented.
964 // \param r \f$ r \in so(N) \f$
965 // \return \f$ R = \exp R \f$ with \f$ R \in SO(N) \f$
966 //
967 template<typename T>
968 Tensor<T>
969 exp_skew_symmetric(Tensor<T> const & r)
970 {
971  // Check whether skew-symmetry holds
972  assert(norm(sym(r)) < machine_epsilon<T>());
973 
974  Index const
975  N = r.get_dimension();
976 
977  Tensor<T>
978  R = identity<T>(N);
979 
980  T
981  theta = 0.0;
982 
983  switch (N) {
984 
985  default:
986  std::cerr << "Exp of so(N) N != 2,3 not implemented." << std::endl;
987  exit(1);
988  break;
989 
990  case 3:
991  theta = std::sqrt(r(2,1)*r(2,1)+r(0,2)*r(0,2)+r(1,0)*r(1,0));
992 
993  //Check whether norm == 0. If so, return identity.
994  if (theta >= machine_epsilon<T>()) {
995  R += std::sin(theta) / theta * r +
996  (1.0 - std::cos(theta)) / (theta * theta) * r * r;
997  }
998  break;
999 
1000  case 2:
1001  theta = r(1,0);
1002 
1003  T
1004  c = std::cos(theta);
1005 
1006  T
1007  s = std::sin(theta);
1008 
1009  R(0,0) = c;
1010  R(0,1) = -s;
1011  R(1,0) = s;
1012  R(1,1) = c;
1013 
1014  break;
1015 
1016  }
1017 
1018  return R;
1019 }
1020 
1021 //
1022 // R^N off-diagonal norm. Useful for SVD and other algorithms
1023 // that rely on Jacobi-type procedures.
1024 // \param A
1025 // \return \f$ \sqrt(\sum_i \sum_{j, j\neq i} a_{ij}^2) \f$
1026 //
1027 template<typename T>
1028 T
1029 norm_off_diagonal(Tensor<T> const & A)
1030 {
1031  Index const
1032  N = A.get_dimension();
1033 
1034  T s = 0.0;
1035 
1036  switch (N) {
1037 
1038  default:
1039  for (Index i = 0; i < N; ++i) {
1040  for (Index j = 0; j < N; ++j) {
1041  if (i != j) s += A(i,j)*A(i,j);
1042  }
1043  }
1044  break;
1045 
1046  case 3:
1047  s = A(0,1)*A(0,1) + A(0,2)*A(0,2) + A(1,2)*A(1,2) +
1048  A(1,0)*A(1,0) + A(2,0)*A(2,0) + A(2,1)*A(2,1);
1049  break;
1050 
1051  case 2:
1052  s = A(0,1)*A(0,1) + A(1,0)*A(1,0);
1053  break;
1054 
1055  }
1056 
1057  return std::sqrt(s);
1058 }
1059 
1060 //
1061 // R^N arg max abs. Useful for inverse and other algorithms
1062 // that rely on Jacobi-type procedures.
1063 // \param A
1064 // \return \f$ (p,q) = arg max_{i,j} |a_{ij}| \f$
1065 //
1066 template<typename T>
1067 std::pair<Index, Index>
1068 arg_max_abs(Tensor<T> const & A)
1069 {
1070  Index p = 0;
1071  Index q = 0;
1072 
1073  T s = std::abs(A(p,q));
1074 
1075  Index const
1076  N = A.get_dimension();
1077 
1078  for (Index i = 0; i < N; ++i) {
1079  for (Index j = 0; j < N; ++j) {
1080  if (std::abs(A(i,j)) > s) {
1081  p = i;
1082  q = j;
1083  s = std::abs(A(i,j));
1084  }
1085  }
1086  }
1087 
1088  return std::make_pair(p,q);
1089 }
1090 
1091 //
1092 // R^N arg max off-diagonal. Useful for SVD and other algorithms
1093 // that rely on Jacobi-type procedures.
1094 // \param A
1095 // \return \f$ (p,q) = arg max_{i \neq j} |a_{ij}| \f$
1096 //
1097 template<typename T>
1098 std::pair<Index, Index>
1099 arg_max_off_diagonal(Tensor<T> const & A)
1100 {
1101  Index p = 0;
1102  Index q = 1;
1103 
1104  T s = std::abs(A(p,q));
1105 
1106  Index const
1107  N = A.get_dimension();
1108 
1109  for (Index i = 0; i < N; ++i) {
1110  for (Index j = 0; j < N; ++j) {
1111  if (i != j && std::abs(A(i,j)) > s) {
1112  p = i;
1113  q = j;
1114  s = std::abs(A(i,j));
1115  }
1116  }
1117  }
1118 
1119  return std::make_pair(p,q);
1120 }
1121 
1122 namespace {
1123 
1124 //
1125 // Singular value decomposition (SVD) for 2x2
1126 // bidiagonal matrix. Used for general 2x2 SVD.
1127 // Adapted from LAPAPCK's DLASV2, Netlib's dlasv2.c
1128 // and LBNL computational crystalography toolbox
1129 // \param f, g, h where A = [f, g; 0, h]
1130 // \return \f$ A = USV^T\f$
1131 //
1132 template<typename T>
1133 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1134 svd_bidiagonal(T f, T g, T h)
1135 {
1136  T fa = std::abs(f);
1137  T ga = std::abs(g);
1138  T ha = std::abs(h);
1139 
1140  T s0 = 0.0;
1141  T s1 = 0.0;
1142 
1143  T cu = 1.0;
1144  T su = 0.0;
1145  T cv = 1.0;
1146  T sv = 0.0;
1147 
1148  bool swap_diag = (ha > fa);
1149 
1150  if (swap_diag) {
1151  std::swap(fa, ha);
1152  std::swap(f, h);
1153  }
1154 
1155  if (ga == 0.0) {
1156  s1 = ha;
1157  s0 = fa;
1158  } else if (ga > fa && fa / ga < machine_epsilon<T>()) {
1159  // case of very large ga
1160  s0 = ga;
1161  s1 = ha > 1.0 ?
1162  T(fa / (ga / ha)) :
1163  T((fa / ga) * ha);
1164  cu = 1.0;
1165  su = h / g;
1166  cv = f / g;
1167  sv = 1.0;
1168  } else {
1169  // normal case
1170  T d = fa - ha;
1171  T l = d != fa ?
1172  T(d / fa) :
1173  T(1.0); // l \in [0,1]
1174  T m = g / f; // m \in (-1/macheps, 1/macheps)
1175  T t = 2.0 - l; // t \in [1,2]
1176  T mm = m * m;
1177  T tt = t * t;
1178  T s = std::sqrt(tt + mm); // s \in [1,1 + 1/macheps]
1179  T r = l != 0.0 ?
1180  T(std::sqrt(l * l + mm)) :
1181  T(std::abs(m)); // r \in [0,1 + 1/macheps]
1182  T a = 0.5 * (s + r); // a \in [1,1 + |m|]
1183  s1 = ha / a;
1184  s0 = fa * a;
1185 
1186  // Compute singular vectors
1187  T tau; // second assignment to T in DLASV2
1188  if (mm != 0.0) {
1189  tau = (m / (s + t) + m / (r + l)) * (1.0 + a);
1190  } else {
1191  // note that m is very tiny
1192  tau = l == 0.0 ?
1193  T(copysign(T(2.0), f) * copysign(T(1.0), g)) :
1194  T(g / copysign(d, f) + m / t);
1195  }
1196  T lv = std::sqrt(tau * tau + 4.0); // second assignment to L in DLASV2
1197  cv = 2.0 / lv;
1198  sv = tau / lv;
1199  cu = (cv + sv * m) / a;
1200  su = (h / f) * sv / a;
1201  }
1202 
1203  // Fix signs of singular values in accordance to sign of singular vectors
1204  s0 = copysign(s0, f);
1205  s1 = copysign(s1, h);
1206 
1207  if (swap_diag) {
1208  std::swap(cu, sv);
1209  std::swap(su, cv);
1210  }
1211 
1212  Tensor<T> U(cu, -su, su, cu);
1213 
1214  Tensor<T> S(s0, 0.0, 0.0, s1);
1215 
1216  Tensor<T> V(cv, -sv, sv, cv);
1217 
1218  return boost::make_tuple(U, S, V);
1219 }
1220 
1221 //
1222 // R^2 singular value decomposition (SVD)
1223 // \param A tensor
1224 // \return \f$ A = USV^T\f$
1225 //
1226 template<typename T>
1227 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1228 svd_2x2(Tensor<T> const & A)
1229 {
1230  assert(A.get_dimension() == 2);
1231 
1232  // First compute a givens rotation to eliminate 1,0 entry in tensor
1233  T c = 1.0;
1234  T s = 0.0;
1235  boost::tie(c, s) = givens(A(0,0), A(1,0));
1236 
1237  Tensor<T>
1238  R(c, -s, s, c);
1239 
1240  Tensor<T>
1241  B = R * A;
1242 
1243  // B is bidiagonal. Use specialized algorithm to compute its SVD
1244  Tensor<T>
1245  X(2), S(2), V(2);
1246 
1247  boost::tie(X, S, V) = svd_bidiagonal(B(0,0), B(0,1), B(1,1));
1248 
1249  // Complete general 2x2 SVD with givens rotation calculated above
1250  Tensor<T>
1251  U = transpose(R) * X;
1252 
1253  return boost::make_tuple(U, S, V);
1254 }
1255 
1256 //
1257 // R^N singular value decomposition (SVD)
1258 // \param A tensor
1259 // \return \f$ A = USV^T\f$
1260 //
1261 template<typename T>
1262 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1263 svd_NxN(Tensor<T> const & A)
1264 {
1265  Tensor<T>
1266  S = A;
1267 
1268  Index const
1269  N = A.get_dimension();
1270 
1271  Tensor<T>
1272  U = identity<T>(N);
1273 
1274  Tensor<T>
1275  V = identity<T>(N);
1276 
1277  T
1278  off = norm_off_diagonal(S);
1279 
1280  T const
1281  tol = machine_epsilon<T>() * norm(A);
1282 
1283  Index const
1284  max_iter = 128;
1285 
1286  Index
1287  num_iter = 0;
1288 
1289  while (off > tol && num_iter < max_iter) {
1290 
1291  // Find largest off-diagonal entry
1292  Index
1293  p = 0;
1294 
1295  Index
1296  q = 0;
1297 
1298  boost::tie(p,q) = arg_max_off_diagonal(S);
1299 
1300  if (p > q) {
1301  std::swap(p, q);
1302  }
1303 
1304  // Obtain left and right Givens rotations by using 2x2 SVD
1305  Tensor <T>
1306  Spq(S(p,p), S(p,q), S(q,p), S(q,q));
1307 
1308  Tensor <T>
1309  L(2), D(2), R(2);
1310 
1311  boost::tie(L, D, R) = svd_2x2(Spq);
1312 
1313  T const &
1314  cl = L(0,0);
1315 
1316  T const &
1317  sl = L(0,1);
1318 
1319  T const &
1320  cr = R(0,0);
1321 
1322  T const &
1323  sr = (sgn(R(0,1)) == sgn(R(1,0))) ? T(-R(0,1)) : T(R(0,1));
1324 
1325  // Apply both Givens rotations to matrices
1326  // that are converging to singular values and singular vectors
1327  givens_left(cl, sl, p, q, S);
1328  givens_right(cr, sr, p, q, S);
1329 
1330  givens_right(cl, sl, p, q, U);
1331  givens_left(cr, sr, p, q, V);
1332 
1333  off = norm_off_diagonal(S);
1334  num_iter++;
1335  }
1336 
1337  if (num_iter == max_iter) {
1338  std::cerr << "WARNING: SVD iteration did not converge." << std::endl;
1339  }
1340 
1341  // Fix signs for entries in the diagonal matrix S
1342  // that are negative
1343  for (Index i = 0; i < N; ++i) {
1344  if (S(i,i) < 0.0) {
1345  S(i,i) = -S(i,i);
1346  for (Index j = 0; j < N; ++j) {
1347  U(j,i) = -U(j,i);
1348  }
1349  }
1350  }
1351 
1352  Vector<T> s(N);
1353  Tensor<T> P(N);
1354 
1355  boost::tie(s, P) = sort_permutation(diag(S));
1356  S = diag(s);
1357  U = U * P;
1358  V = V * P;
1359 
1360  return boost::make_tuple(U, diag(diag(S)), transpose(V));
1361 }
1362 
1363 } // anonymous namespace
1364 
1365 //
1366 // R^N singular value decomposition (SVD)
1367 // \param A tensor
1368 // \return \f$ A = USV^T\f$
1369 //
1370 template<typename T>
1371 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1372 svd(Tensor<T> const & A)
1373 {
1374  Index const
1375  N = A.get_dimension();
1376 
1377  Tensor<T>
1378  U(N), S(N), V(N);
1379 
1380  switch (N) {
1381 
1382  default:
1383  boost::tie(U, S, V) = svd_NxN(A);
1384  break;
1385 
1386  case 2:
1387  boost::tie(U, S, V) = svd_2x2(A);
1388  break;
1389 
1390  }
1391 
1392  return boost::make_tuple(U, S, V);
1393 }
1394 
1395 //
1396 // Project to O(N) (Orthogonal Group) using a Newton-type algorithm.
1397 // See Higham's Functions of Matrices p210 [2008]
1398 // \param A tensor (often a deformation-gradient-like tensor)
1399 // \return \f$ R = \argmin_Q \|A - Q\|\f$
1400 // This algorithm projects a given tensor in GL(N) to O(N).
1401 // The rotation/reflection obtained through this projection is
1402 // the orthogonal component of the real polar decomposition
1403 //
1404 template<typename T>
1405 Tensor<T>
1406 polar_rotation(Tensor<T> const & A)
1407 {
1408  Index const
1409  N = A.get_dimension();
1410 
1411  bool
1412  scale = true;
1413 
1414  T const
1415  tol_scale = 0.01;
1416 
1417  T const
1418  tol_conv = std::sqrt(N) * machine_epsilon<T>();
1419 
1420  Tensor<T>
1421  X = A;
1422 
1423  T
1424  gamma = 2.0;
1425 
1426  Index const
1427  max_iter = 128;
1428 
1429  Index
1430  num_iter = 0;
1431 
1432  while (num_iter < max_iter) {
1433 
1434  Tensor<T>
1435  Y = inverse(X);
1436 
1437  T
1438  mu = 1.0;
1439 
1440  if (scale == true) {
1441  mu = (norm_1(Y) * norm_infinity(Y)) / (norm_1(X) * norm_infinity(X));
1442  mu = std::sqrt(std::sqrt(mu));
1443  }
1444 
1445  Tensor<T>
1446  Z = 0.5 * (mu * X + transpose(Y) / mu);
1447 
1448  Tensor<T>
1449  D = Z - X;
1450 
1451  T
1452  delta = norm(D) / norm(Z);
1453 
1454  if (scale == true && delta < tol_scale) {
1455  scale = false;
1456  }
1457 
1458  bool
1459  end_iter =
1460  norm(D) <= std::sqrt(tol_conv) ||
1461  (delta > 0.5 * gamma && scale == false);
1462 
1463  X = Z;
1464  gamma = delta;
1465 
1466  if (end_iter == true) {
1467  break;
1468  }
1469 
1470  num_iter++;
1471 
1472  }
1473 
1474  if (num_iter == max_iter) {
1475  std::cerr << "WARNING: Polar iteration did not converge." << std::endl;
1476  }
1477 
1478  return X;
1479 }
1480 
1481 //
1482 // R^N Left polar decomposition
1483 // \param A tensor (often a deformation-gradient-like tensor)
1484 // \return \f$ VR = A \f$ with \f$ R \in SO(N) \f$ and \f$ V \in SPD(N) \f$
1485 //
1486 template<typename T>
1487 std::pair<Tensor<T>, Tensor<T> >
1488 polar_left(Tensor<T> const & A)
1489 {
1490  Tensor<T>
1491  R = polar_rotation(A);
1492 
1493  Tensor<T>
1494  V = sym(A * transpose(R));
1495 
1496  return std::make_pair(V, R);
1497 }
1498 
1499 //
1500 // R^N Right polar decomposition
1501 // \param A tensor (often a deformation-gradient-like tensor)
1502 // \return \f$ RU = A \f$ with \f$ R \in SO(N) \f$ and \f$ U \in SPD(N) \f$
1503 //
1504 template<typename T>
1505 std::pair<Tensor<T>, Tensor<T> >
1506 polar_right(Tensor<T> const & A)
1507 {
1508  Tensor<T>
1509  R = polar_rotation(A);
1510 
1511  Tensor<T>
1512  U = sym(transpose(R) * A);
1513 
1514  return std::make_pair(R, U);
1515 }
1516 
1517 //
1518 // R^3 left polar decomposition with eigenvalue decomposition
1519 // \param F tensor (often a deformation-gradient-like tensor)
1520 // \return \f$ VR = F \f$ with \f$ R \in SO(3) \f$ and V SPD(3)
1521 //
1522 template<typename T>
1523 std::pair<Tensor<T>, Tensor<T> >
1524 polar_left_eig(Tensor<T> const & F)
1525 {
1526  assert(F.get_dimension() == 3);
1527 
1528  // set up return tensors
1529  Tensor<T>
1530  R(3);
1531 
1532  Tensor<T>
1533  V(3);
1534 
1535  // temporary tensor used to compute R
1536  Tensor<T>
1537  Vinv(3);
1538 
1539  // compute spd tensor
1540  Tensor<T>
1541  b = F * transpose(F);
1542 
1543  // get eigenvalues/eigenvectors
1544  Tensor<T>
1545  eVal(3);
1546 
1547  Tensor<T>
1548  eVec(3);
1549 
1550  boost::tie(eVec, eVal) = eig_spd(b);
1551 
1552  // compute sqrt() and inv(sqrt()) of eigenvalues
1553  Tensor<T>
1554  x = zero<T>(3);
1555 
1556  x(0,0) = std::sqrt(eVal(0,0));
1557  x(1,1) = std::sqrt(eVal(1,1));
1558  x(2,2) = std::sqrt(eVal(2,2));
1559 
1560  Tensor<T>
1561  xi = zero<T>(3);
1562 
1563  xi(0,0) = 1.0 / x(0,0);
1564  xi(1,1) = 1.0 / x(1,1);
1565  xi(2,2) = 1.0 / x(2,2);
1566 
1567  // compute V, Vinv, and R
1568  V = eVec * x * transpose(eVec);
1569  Vinv = eVec * xi * transpose(eVec);
1570  R = Vinv * F;
1571 
1572  return std::make_pair(V, R);
1573 }
1574 
1575 //
1576 // R^3 right polar decomposition with eigenvalue decomposition
1577 // \param F tensor (often a deformation-gradient-like tensor)
1578 // \return \f$ RU = F \f$ with \f$ R \in SO(3) \f$ and U SPD(3)
1579 //
1580 template<typename T>
1581 std::pair<Tensor<T>, Tensor<T> >
1582 polar_right_eig(Tensor<T> const & F)
1583 {
1584  assert(F.get_dimension() == 3);
1585 
1586  Tensor<T>
1587  R(3);
1588 
1589  Tensor<T>
1590  U(3);
1591 
1592  // temporary tensor used to compute R
1593  Tensor<T>
1594  Uinv(3);
1595 
1596  // compute spd tensor
1597  Tensor<T>
1598  C = transpose(F) * F;
1599 
1600  // get eigenvalues/eigenvectors
1601  Tensor<T>
1602  eVal(3);
1603 
1604  Tensor<T>
1605  eVec(3);
1606 
1607  boost::tie(eVec, eVal) = eig_spd(C);
1608 
1609  // compute sqrt() and inv(sqrt()) of eigenvalues
1610  Tensor<T>
1611  x = zero<T>(3);
1612 
1613  x(0,0) = std::sqrt(eVal(0,0));
1614  x(1,1) = std::sqrt(eVal(1,1));
1615  x(2,2) = std::sqrt(eVal(2,2));
1616 
1617  Tensor<T>
1618  xi = zero<T>(3);
1619 
1620  xi(0,0) = 1.0 / x(0,0);
1621  xi(1,1) = 1.0 / x(1,1);
1622  xi(2,2) = 1.0 / x(2,2);
1623 
1624  // compute U, Uinv, and R
1625  U = eVec * x * transpose(eVec);
1626  Uinv = eVec * xi * transpose(eVec);
1627  R = F * Uinv;
1628 
1629  return std::make_pair(R, U);
1630 }
1631 
1632 //
1633 // R^N left polar decomposition with matrix logarithm for V
1634 // \param F tensor (often a deformation-gradient-like tensor)
1635 // \return \f$ VR = F \f$ with \f$ R \in SO(N) \f$ and V SPD(N), and log V
1636 //
1637 template<typename T>
1638 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1639 polar_left_logV(Tensor<T> const & F)
1640 {
1641  Index const
1642  N = F.get_dimension();
1643 
1644  Tensor<T>
1645  X(N), S(N), Y(N);
1646 
1647  boost::tie(X, S, Y) = svd(F);
1648 
1649  Tensor<T>
1650  R = X * transpose(Y);
1651 
1652  Tensor<T>
1653  V = X * S * transpose(X);
1654 
1655  Tensor<T>
1656  s = S;
1657 
1658  for (Index i = 0; i < N; ++i) {
1659  s(i,i) = std::log(s(i,i));
1660  }
1661 
1662  Tensor<T>
1663  v = X * s * transpose(X);
1664 
1665  return boost::make_tuple(V, R, v);
1666 }
1667 
1668 //
1669 // R^N left polar decomposition with matrix logarithm for V
1670 // \param F tensor (often a deformation-gradient-like tensor)
1671 // \return \f$ VR = F \f$ with \f$ R \in SO(N) \f$ and V SPD(N), and log V
1672 //
1673 template<typename T>
1674 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1675 polar_left_logV_lame(Tensor<T> const & F)
1676 {
1677  Index const
1678  N = F.get_dimension();
1679 
1680  // set up return tensors
1681  Tensor<T> R(N), V(N), v(N), Vinv(N);
1682 
1683  // compute spd tensor
1684  Tensor<T> b = F*transpose(F);
1685 
1686  // get eigenvalues/eigenvectors
1687  Tensor<T> eVal(N);
1688  Tensor<T> eVec(N);
1689  boost::tie(eVec,eVal) = eig_spd_cos(b);
1690 
1691  // compute sqrt() and inv(sqrt()) of eigenvalues
1692  Tensor<T> x = zero<T>(3);
1693  x(0,0) = std::sqrt(eVal(0,0));
1694  x(1,1) = std::sqrt(eVal(1,1));
1695  x(2,2) = std::sqrt(eVal(2,2));
1696  Tensor<T> xi = zero<T>(3);
1697  xi(0,0) = 1.0/x(0,0);
1698  xi(1,1) = 1.0/x(1,1);
1699  xi(2,2) = 1.0/x(2,2);
1700  Tensor<T> lnx = zero<T>(3);
1701  lnx(0,0) = std::log(x(0,0));
1702  lnx(1,1) = std::log(x(1,1));
1703  lnx(2,2) = std::log(x(2,2));
1704 
1705  // compute V, Vinv, log(V)=v, and R
1706  V = eVec*x*transpose(eVec);
1707  Vinv = eVec*xi*transpose(eVec);
1708  v = eVec*lnx*transpose(eVec);
1709  R = Vinv*F;
1710 
1711  return boost::make_tuple(V,R,v);
1712 }
1713 
1714 //
1715 // R^N logarithmic map using BCH expansion (4 terms)
1716 // \param x tensor
1717 // \param y tensor
1718 // \return Baker-Campbell-Hausdorff series up to 4 terms
1719 //
1720 template<typename T>
1721 Tensor<T>
1722 bch(Tensor<T> const & x, Tensor<T> const & y)
1723 {
1724  return
1725  // first order term
1726  x + y
1727  +
1728  // second order term
1729  0.5*(x*y - y*x)
1730  +
1731  // third order term
1732  1.0/12.0 *
1733  (x*x*y - 2.0*x*y*x + x*y*y + y*x*x - 2.0*y*x*y + y*y*x)
1734  +
1735  // fourth order term
1736  1.0/24.0 *
1737  (x*x*y*y - 2.0*x*y*x*y + 2.0*y*x*y*x - y*y*x*x);
1738 }
1739 
1740 //
1741 // Symmetric Schur algorithm for R^2.
1742 // \param \f$ A = [f, g; g, h] \in S(2) \f$
1743 // \return \f$ c, s \rightarrow [c, -s; s, c]\f diagonalizes A$
1744 //
1745 template<typename T>
1746 std::pair<T, T>
1747 schur_sym(T const f, T const g, T const h)
1748 {
1749  T c = 1.0;
1750  T s = 0.0;
1751 
1752  if (g != 0.0) {
1753  T t = (h - f) / (2.0 * g);
1754 
1755  if (t >= 0.0) {
1756  t = 1.0 / (std::sqrt(1.0 + t * t) + t);
1757  } else {
1758  t = -1.0 / (std::sqrt(1.0 + t * t) - t);
1759  }
1760  c = 1.0 / std::sqrt(1.0 + t * t);
1761  s = t * c;
1762  }
1763 
1764  return std::make_pair(c, s);
1765 }
1766 
1767 //
1768 // Givens rotation. [c, -s; s, c] [a; b] = [r; 0]
1769 // \param a, b
1770 // \return c, s
1771 //
1772 template<typename T>
1773 std::pair<T, T>
1774 givens(T const & a, T const & b)
1775 {
1776  T c = 1.0;
1777  T s = 0.0;
1778 
1779  if (b != 0.0) {
1780  if (std::abs(b) > std::abs(a)) {
1781  T const t = - a / b;
1782  s = 1.0 / std::sqrt(1.0 + t * t);
1783  c = t * s;
1784  } else {
1785  T const t = - b / a;
1786  c = 1.0 / std::sqrt(1.0 + t * t);
1787  s = t * c;
1788  }
1789  }
1790 
1791  return std::make_pair(c, s);
1792 }
1793 
1794 namespace {
1795 
1796 //
1797 // R^N eigenvalue decomposition for symmetric 2nd-order tensor
1798 // \param A tensor
1799 // \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1800 //
1801 template<typename T>
1802 std::pair<Tensor<T>, Tensor<T> >
1803 eig_sym_NxN(Tensor<T> const & A)
1804 {
1805  Tensor<T>
1806  D = sym(A);
1807 
1808  Index const
1809  N = A.get_dimension();
1810 
1811  Tensor<T>
1812  V = identity<T>(N);
1813 
1814  T
1815  off = norm_off_diagonal(D);
1816 
1817  T const
1818  tol = machine_epsilon<T>() * norm(A);
1819 
1820  Index const
1821  max_iter = 128;
1822 
1823  Index
1824  num_iter = 0;
1825 
1826  while (off > tol && num_iter < max_iter) {
1827 
1828  // Find largest off-diagonal entry
1829  Index
1830  p = 0;
1831 
1832  Index
1833  q = 0;
1834 
1835  boost::tie(p,q) = arg_max_off_diagonal(D);
1836  if (p > q) {
1837  std::swap(p,q);
1838  }
1839 
1840  // Obtain Givens rotations by using 2x2 symmetric Schur algorithm
1841  T const &
1842  f = D(p,p);
1843 
1844  T const &
1845  g = D(p,q);
1846 
1847  T const &
1848  h = D(q,q);
1849 
1850  T
1851  c, s;
1852 
1853  boost::tie(c, s) = schur_sym(f, g, h);
1854 
1855  // Apply Givens rotation to matrices
1856  // that are converging to eigenvalues and eigenvectors
1857  givens_left(c, s, p, q, D);
1858  givens_right(c, s, p, q, D);
1859 
1860  givens_right(c, s, p, q, V);
1861 
1862  off = norm_off_diagonal(D);
1863  num_iter++;
1864  }
1865 
1866  if (num_iter == max_iter) {
1867  std::cerr << "WARNING: EIG iteration did not converge." << std::endl;
1868  }
1869 
1870  Vector<T> d(N);
1871  Tensor<T> P(N);
1872 
1873  boost::tie(d, P) = sort_permutation(diag(D));
1874  D = diag(d);
1875  V = V * P;
1876 
1877  return std::make_pair(V, D);
1878 }
1879 
1880 //
1881 // R^2 eigenvalue decomposition for symmetric 2nd-order tensor
1882 // \param A tensor
1883 // \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1884 //
1885 template<typename T>
1886 std::pair<Tensor<T>, Tensor<T> >
1887 eig_sym_2x2(Tensor<T> const & A)
1888 {
1889  assert(A.get_dimension() == 2);
1890 
1891  T const f = A(0,0);
1892  T const g = 0.5 * (A(0,1) + A(1,0));
1893  T const h = A(1,1);
1894 
1895  //
1896  // Eigenvalues, based on LAPACK's dlae2
1897  //
1898  T const sum = f + h;
1899  T const dif = std::abs(f - h);
1900  T const g2 = std::abs(g + g);
1901 
1902  T fhmax = f;
1903  T fhmin = h;
1904 
1905  const bool swap_diag = std::abs(h) > std::abs(f);
1906 
1907  if (swap_diag == true) {
1908  std::swap(fhmax, fhmin);
1909  }
1910 
1911  T r = 0.0;
1912  if (dif > g2) {
1913  T const t = g2 / dif;
1914  r = dif * std::sqrt(1.0 + t * t);
1915  } else if (dif < g2) {
1916  T const t = dif / g2;
1917  r = g2 * std::sqrt(1.0 + t * t);
1918  } else {
1919  // dif == g2, including zero
1920  r = g2 * std::sqrt(2.0);
1921  }
1922 
1923  T s0 = 0.0;
1924  T s1 = 0.0;
1925 
1926  if (sum != 0.0) {
1927  s0 = 0.5 * (sum + copysign(r, sum));
1928  // Order of execution important.
1929  // To get fully accurate smaller eigenvalue,
1930  // next line needs to be executed in higher precision.
1931  s1 = (fhmax / s0) * fhmin - (g / s0) * g;
1932  } else {
1933  // s0 == s1, including zero
1934  s0 = 0.5 * r;
1935  s1 = -0.5 * r;
1936  }
1937 
1938  Tensor<T>
1939  D(s0, 0.0, 0.0, s1);
1940 
1941  //
1942  // Eigenvectors
1943  //
1944  T
1945  c, s;
1946 
1947  boost::tie(c, s) = schur_sym(f, g, h);
1948 
1949  Tensor<T>
1950  V(c, -s, s, c);
1951 
1952  if (swap_diag == true) {
1953  // swap eigenvectors if eigenvalues were swapped
1954  std::swap(V(0,0), V(0,1));
1955  std::swap(V(1,0), V(1,1));
1956  }
1957 
1958  return std::make_pair(V, D);
1959 }
1960 
1961 } // anonymous namespace
1962 
1963 //
1964 // R^N eigenvalue decomposition for symmetric 2nd-order tensor
1965 // \param A tensor
1966 // \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1967 //
1968 template<typename T>
1969 std::pair<Tensor<T>, Tensor<T> >
1970 eig_sym(Tensor<T> const & A)
1971 {
1972  Index const
1973  N = A.get_dimension();
1974 
1975  Tensor<T>
1976  V(N), D(N);
1977 
1978  switch (N) {
1979 
1980  default:
1981  boost::tie(V, D) = eig_sym_NxN(A);
1982  break;
1983 
1984  case 2:
1985  boost::tie(V, D) = eig_sym_2x2(A);
1986  break;
1987 
1988  }
1989 
1990  return std::make_pair(V, D);
1991 }
1992 
1993 //
1994 // R^N eigenvalue decomposition for SPD 2nd-order tensor
1995 // \param A tensor
1996 // \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1997 //
1998 template<typename T>
1999 std::pair<Tensor<T>, Tensor<T> >
2000 eig_spd(Tensor<T> const & A)
2001 {
2002  return eig_sym(A);
2003 }
2004 
2005 //
2006 // R^3 eigenvalue decomposition for SPD 2nd-order tensor
2007 // \param A tensor
2008 // \return V eigenvectors, D eigenvalues in diagonal Matlab-style
2009 //
2010 template<typename T>
2011 std::pair<Tensor<T>, Tensor<T> >
2012 eig_spd_cos(Tensor<T> const & A)
2013 {
2014  assert(A.get_dimension() == 3);
2015 
2016  // This algorithm comes from the journal article
2017  // Scherzinger and Dohrmann, CMAME 197 (2008) 4007-4015
2018 
2019  // this algorithm will return the eigenvalues in D
2020  // and the eigenvectors in V
2021  Tensor<T>
2022  D = zero<T>(3);
2023 
2024  Tensor<T>
2025  V = zero<T>(3);
2026 
2027  // not sure if this is necessary...
2028  T
2029  pi = std::acos(-1);
2030 
2031  // convenience operators
2032  Tensor<T> const
2033  I = identity<T>(3);
2034 
2035  int
2036  ii[3][2] = { { 1, 2 }, { 2, 0 }, { 0, 1 } };
2037 
2038  Tensor<T>
2039  rm = zero<T>(3);
2040 
2041  // scale the matrix to reduce the characteristic equation
2042  T
2043  trA = (1.0/3.0) * I1(A);
2044 
2045  Tensor<T>
2046  Ap(A - trA*I);
2047 
2048  // compute other invariants
2049  T
2050  J2 = I2(Ap);
2051 
2052  T
2053  J3 = det(Ap);
2054 
2055  // deal with volumetric tensors
2056  if (-J2 <= 1.e-30)
2057  {
2058  D(0,0) = trA;
2059  D(1,1) = trA;
2060  D(2,2) = trA;
2061 
2062  V(0,0) = 1.0;
2063  V(1,0) = 0.0;
2064  V(2,0) = 0.0;
2065 
2066  V(0,1) = 0.0;
2067  V(1,1) = 1.0;
2068  V(2,1) = 0.0;
2069 
2070  V(0,2) = 0.0;
2071  V(1,2) = 0.0;
2072  V(2,2) = 1.0;
2073  }
2074  else
2075  {
2076  // first things first, find the most dominant e-value
2077  // Need to solve cos(3 theta)=rhs for theta
2078  T
2079  t1 = 3.0 / -J2;
2080 
2081  T
2082  rhs = (J3 / 2.0) * T(std::sqrt(t1 * t1 * t1));
2083 
2084  T
2085  theta = pi / 2.0 * (1.0 - (rhs < 0 ? -1.0 : 1.0));
2086 
2087  if (std::abs(rhs) <= 1.0) theta = std::acos(rhs);
2088 
2089  T
2090  thetad3 = theta / 3.0;
2091 
2092  if (thetad3 > pi / 6.0) thetad3 += 2.0 * pi / 3.0;
2093 
2094  // most dominant e-value
2095  D(2,2) = 2.0 * std::cos(thetad3) * std::sqrt(-J2 / 3.0);
2096 
2097  // now reduce the system
2098  Tensor<T>
2099  R = Ap - D(2,2) * I;
2100 
2101  // QR factorization with column pivoting
2102  Vector<T> a(3);
2103  a(0) = R(0,0)*R(0,0) + R(1,0)*R(1,0) + R(2,0)*R(2,0);
2104  a(1) = R(0,1)*R(0,1) + R(1,1)*R(1,1) + R(2,1)*R(2,1);
2105  a(2) = R(0,2)*R(0,2) + R(1,2)*R(1,2) + R(2,2)*R(2,2);
2106 
2107  // find the most dominant column
2108  int k = 0;
2109  T max = a(0);
2110  if (a(1) > max)
2111  {
2112  k = 1;
2113  max = a(1);
2114  }
2115  if (a(2) > max)
2116  {
2117  k = 2;
2118  }
2119 
2120  // normalize the most dominant column to get s1
2121  a(k) = std::sqrt(a(k));
2122  for (int i(0); i < 3; ++i)
2123  R(i,k) /= a(k);
2124 
2125  // dot products of dominant column with other two columns
2126  T d0 = 0.0;
2127  T d1 = 0.0;
2128  for (int i(0); i < 3; ++i)
2129  {
2130  d0 += R(i,k) * R(i,ii[k][0]);
2131  d1 += R(i,k) * R(i,ii[k][1]);
2132  }
2133 
2134  // projection
2135  for (int i(0); i < 3; ++i)
2136  {
2137  R(i,ii[k][0]) -= d0 * R(i,k);
2138  R(i,ii[k][1]) -= d1 * R(i,k);
2139  }
2140 
2141  // now finding next most dominant column
2142  a.clear();
2143  for (int i(0); i < 3; ++i)
2144  {
2145  a(0) += R(i,ii[k][0]) * R(i,ii[k][0]);
2146  a(1) += R(i,ii[k][1]) * R(i,ii[k][1]);
2147  }
2148 
2149  int p = 0;
2150  if (std::abs(a(1)) > std::abs(a(0))) p = 1;
2151 
2152  // normalize next most dominant column to get s2
2153  a(p) = std::sqrt(a(p));
2154  int k2 = ii[k][p];
2155 
2156  for (int i(0); i < 3; ++i)
2157  R(i,k2) /= a(p);
2158 
2159  // set first eigenvector as cross product of s1 and s2
2160  V(0,2) = R(1,k) * R(2,k2) - R(2,k) * R(1,k2);
2161  V(1,2) = R(2,k) * R(0,k2) - R(0,k) * R(2,k2);
2162  V(2,2) = R(0,k) * R(1,k2) - R(1,k) * R(0,k2);
2163 
2164  // normalize
2165  T
2166  mag = std::sqrt(V(0,2) * V(0,2) + V(1,2) * V(1,2) + V(2,2) * V(2,2));
2167 
2168  V(0,2) /= mag;
2169  V(1,2) /= mag;
2170  V(2,2) /= mag;
2171 
2172  // now for the other two eigenvalues, extract vectors
2173  Vector<T>
2174  rk(R(0,k), R(1,k), R(2,k));
2175 
2176  Vector<T>
2177  rk2(R(0,k2), R(1,k2), R(2,k2));
2178 
2179  // compute projections
2180  Vector<T>
2181  ak = Ap * rk;
2182 
2183  Vector<T>
2184  ak2 = Ap * rk2;
2185 
2186  // set up reduced remainder matrix
2187  rm(0,0) = dot(rk,ak);
2188  rm(0,1) = dot(rk,ak2);
2189  rm(1,1) = dot(rk2,ak2);
2190 
2191  // compute eigenvalues 2 and 3
2192  T
2193  b = 0.5 * (rm(0,0) - rm(1,1));
2194 
2195  T
2196  fac = (b < 0 ? -1.0 : 1.0);
2197 
2198  T
2199  arg = b * b + rm(0,1) * rm(0,1);
2200 
2201  if (arg == 0)
2202  D(0,0) = rm(1,1) + b;
2203  else
2204  D(0,0) = rm(1,1) + b - fac * std::sqrt(b * b + rm(0,1) * rm(0,1));
2205 
2206  D(1,1) = rm(0,0) + rm(1,1) - D(0,0);
2207 
2208  // update reduced remainder matrix
2209  rm(0,0) -= D(0,0);
2210  rm(1,0) = rm(0,1);
2211  rm(1,1) -= D(0,0);
2212 
2213  // again, find most dominant column
2214  a.clear();
2215  a(0) = rm(0,0) * rm(0,0) + rm(0,1) * rm(0,1);
2216  a(1) = rm(0,1) * rm(0,1) + rm(1,1) * rm(1,1);
2217 
2218  int k3 = 0;
2219  if (a(1) > a(0)) k3 = 1;
2220  if (a(k3) == 0.0)
2221  {
2222  rm(0,k3) = 1.0;
2223  rm(1,k3) = 0.0;
2224  }
2225 
2226  // set 2nd eigenvector via cross product
2227  V(0,0) = rm(0,k3) * rk2(0) - rm(1,k3) * rk(0);
2228  V(1,0) = rm(0,k3) * rk2(1) - rm(1,k3) * rk(1);
2229  V(2,0) = rm(0,k3) * rk2(2) - rm(1,k3) * rk(2);
2230 
2231  // normalize
2232  mag = std::sqrt(V(0,0) * V(0,0) + V(1,0) * V(1,0) + V(2,0) * V(2,0));
2233  V(0,0) /= mag;
2234  V(1,0) /= mag;
2235  V(2,0) /= mag;
2236 
2237  // set last eigenvector as cross product of other two
2238  V(0,1) = V(1,0) * V(2,2) - V(2,0) * V(1,2);
2239  V(1,1) = V(2,0) * V(0,2) - V(0,0) * V(2,2);
2240  V(2,1) = V(0,0) * V(1,2) - V(1,0) * V(0,2);
2241 
2242  // normalize
2243  mag = std::sqrt(V(0,1) * V(0,1) + V(1,1) * V(1,1) + V(2,1) * V(2,1));
2244  V(0,1) /= mag;
2245  V(1,1) /= mag;
2246  V(2,1) /= mag;
2247 
2248  // add back in the offset
2249  for (int i(0); i < 3; ++i)
2250  D(i,i) += trA;
2251  }
2252 
2253  return std::make_pair(V, D);
2254 }
2255 
2256 //
2257 // Cholesky decomposition, rank-1 update algorithm
2258 // (Matrix Computations 3rd ed., Golub & Van Loan, p145)
2259 // \param A assumed symmetric tensor
2260 // \return G Cholesky factor A = GG^T
2261 // \return completed (bool) algorithm ran to completion
2262 //
2263 template<typename T>
2264 std::pair<Tensor<T>, bool >
2265 cholesky(Tensor<T> const & A)
2266 {
2267  Tensor<T>
2268  G = sym(A);
2269 
2270  Index const
2271  N = A.get_dimension();
2272 
2273  for (Index k = 0; k < N; ++k) {
2274 
2275  // Zeros above the diagonal
2276  for (Index j = k + 1; j < N; ++j) {
2277  G(k,j) = 0.0;
2278  }
2279 
2280  T
2281  s = G(k,k);
2282 
2283  if (s <= 0.0) {
2284  return std::make_pair(G, false);
2285  }
2286 
2287  s = std::sqrt(s);
2288 
2289  for (Index j = k + 1; j < N; ++j) {
2290  G(j,k) /= s;
2291  }
2292 
2293  G(k,k) = s;
2294 
2295  for (Index j = k + 1; j < N; ++j) {
2296  for (Index i = j; i < N; ++i) {
2297  G(i,j) -= G(i,k) * G(j,k);
2298  }
2299  }
2300 
2301  }
2302 
2303  return std::make_pair(G, true);
2304 }
2305 
2306 } // namespace Intrepid
2307 
2308 #endif // Intrepid_MiniTensor_LinearAlgebra_t_h