MiniTensor Version of the Day
Loading...
Searching...
No Matches
MiniTensor_Factorizations.h
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MiniTensor Package
4//
5// Copyright 2016 NTESS and the MiniTensor contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#if !defined(MiniTensor_Factorizations_h)
11#define MiniTensor_Factorizations_h
12
13// Matrix factorizations: eigen, SVD, polar, Cholesky, Givens.
14#include "MiniTensor_Inverse.h"
15#include "MiniTensor_Norms.h"
16
17namespace minitensor {
18
21
30template<typename T, Index N>
32void
33givens_left(T const & c, T const & s, Index i, Index k, Tensor<T, N> & A);
34
43template<typename T, Index N>
45void
46givens_right(T const & c, T const & s, Index i, Index k, Tensor<T, N> & A);
47
55template <typename T, Index N>
56std::pair<Vector<T, N>, Tensor<T, N>> sort_permutation(Vector<T, N> const &u);
57
62template <typename T, Index N>
63std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>> svd(Tensor<T, N> const &A);
64
74template<typename T, Index N>
78
84template <typename T, Index N>
85std::pair<Tensor<T, N>, Tensor<T, N>> polar_left(Tensor<T, N> const &A);
86
92template <typename T, Index N>
93std::pair<Tensor<T, N>, Tensor<T, N>> polar_right(Tensor<T, N> const &A);
94
100template <typename T, Index N>
101std::pair<Tensor<T, N>, Tensor<T, N>> polar_left_eig(Tensor<T, N> const &A);
102
108template <typename T, Index N>
109std::pair<Tensor<T, N>, Tensor<T, N>> polar_right_eig(Tensor<T, N> const &A);
110
116template <typename T, Index N>
117std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
119
120template <typename T, Index N>
121std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
123
129template <typename T, Index N>
130std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
132
140template <typename T>
141std::pair<T, T> schur_sym(const T f, const T g, const T h);
142
147template <typename T> std::pair<T, T> givens(T const &a, T const &b);
148
153template <typename T, Index N>
154std::pair<Tensor<T, N>, Tensor<T, N>> eig_sym(Tensor<T, N> const &A);
155
160template <typename T, Index N>
161std::pair<Tensor<T, N>, Tensor<T, N>> eig_spd(Tensor<T, N> const &A);
162
169template <typename T, Index N>
170std::pair<Tensor<T, N>, Tensor<T, N>> eig_spd_cos(Tensor<T, N> const &A);
171
179template <typename T, Index N>
180std::pair<Tensor<T, N>, bool> cholesky(Tensor<T, N> const &A);
181
185template <typename T, Index N> T cond(Tensor<T, N> const &A);
186
190template <typename T, Index N> T inv_cond(Tensor<T, N> const &A);
191
192//
193// Condition number.
194//
195template <typename T, Index N> T cond(Tensor<T, N> const &A) {
196 Index const
197 dimension = A.get_dimension();
198
199 Tensor<T, N> const S = std::get<1>(svd(A));
200
201 T const
202 k = S(0, 0) / S(dimension - 1, dimension - 1);
203
204 return k;
205}
206
207//
208// Reciprocal condition number.
209//
210template <typename T, Index N> T inv_cond(Tensor<T, N> const &A) {
211 Index const
212 dimension = A.get_dimension();
213
214 Tensor<T, N> const S = std::get<1>(svd(A));
215
216 T const
217 k = S(dimension - 1, dimension - 1) / S(0, 0);
218
219 return k;
220}
221
222//
223// Sort and index in descending order. Useful for ordering singular values
224// and eigenvalues and corresponding vectors in the respective decompositions.
225//
226template <typename T, Index N>
227std::pair<Vector<T, N>, Tensor<T, N>> sort_permutation(Vector<T, N> const &u) {
228
229 Index const
230 dimension = u.get_dimension();
231
232 std::vector <std::pair<T, Index>>
233 s(dimension);
234
235 for (Index i = 0; i < dimension; ++i) {
236 s[i].first = u(i);
237 s[i].second = i;
238 }
239
240 std::sort(s.begin(), s.end(), greater_than<std::pair<T, Index>>);
241
242 Vector<T, N> v(dimension);
243
245 P = zero<T, N>(dimension);
246
247 for (Index i = 0; i < dimension; ++i) {
248 v(i) = s[i].first;
249 P(s[i].second, i) = 1.0;
250 }
251
252 return std::make_pair(v, P);
253}
254
255//
256// Apply Givens-Jacobi rotation on the left in place.
257//
258template<typename T, Index N>
260void
261givens_left(T const & c, T const & s, Index i, Index k, Tensor<T, N> & A)
262{
263 Index const
264 dimension = A.get_dimension();
265
266 for (Index j = 0; j < dimension; ++j) {
267 T const t1 = A(i,j);
268 T const t2 = A(k,j);
269 A(i,j) = c * t1 - s * t2;
270 A(k,j) = s * t1 + c * t2;
271 }
272 return;
273}
274
275//
276// Apply Givens-Jacobi rotation on the right in place.
277//
278template<typename T, Index N>
280void
281givens_right(T const & c, T const & s, Index i, Index k, Tensor<T, N> & A)
282{
283 Index const
284 dimension = A.get_dimension();
285
286 for (Index j = 0; j < dimension; ++j) {
287 T const t1 = A(j,i);
288 T const t2 = A(j,k);
289 A(j,i) = c * t1 - s * t2;
290 A(j,k) = s * t1 + c * t2;
291 }
292 return;
293}
294
295namespace impl {
296
297//
298// Singular value decomposition (SVD) for 2x2
299// bidiagonal matrix. Used for general 2x2 SVD.
300// Adapted from LAPAPCK's DLASV2, Netlib's dlasv2.c
301// and LBNL computational crystallography toolbox
302// \param f, g, h where A = [f, g; 0, h]
303// \return \f$ A = USV^T\f$
304//
305template <typename T, Index N>
306std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>> svd_bidiagonal(T f, T g,
307 T h) {
308 T fa = std::abs(f);
309 T ga = std::abs(g);
310 T ha = std::abs(h);
311
312 T s0 = 0.0;
313 T s1 = 0.0;
314
315 T cu = 1.0;
316 T su = 0.0;
317 T cv = 1.0;
318 T sv = 0.0;
319
320 bool swap_diag = (ha > fa);
321
322 if (swap_diag == true) {
323 std::swap(fa, ha);
324 std::swap(f, h);
325 }
326
327 // diagonal matrix
328 if (ga == 0.0) {
329 s1 = ha;
330 s0 = fa;
331 } else if (ga > fa && fa / ga < machine_epsilon<T>()) {
332 // case of very large ga
333 s0 = ga;
334 s1 = ha > 1.0 ?
335 T(fa / (ga / ha)) :
336 T((fa / ga) * ha);
337 cu = 1.0;
338 su = h / g;
339 cv = f / g;
340 sv = 1.0;
341 } else {
342 // normal case
343 T d = fa - ha;
344 T l = d / fa; // l \in [0,1]
345 T m = g / f; // m \in (-1/macheps, 1/macheps)
346 T t = 2.0 - l; // t \in [1,2]
347 T mm = m * m;
348 T tt = t * t;
349 T s = std::sqrt(tt + mm); // s \in [1,1 + 1/macheps]
350 T r = l != 0.0 ?
351 T(std::sqrt(l * l + mm)) :
352 T(std::abs(m)); // r \in [0,1 + 1/macheps]
353 T a = 0.5 * (s + r); // a \in [1,1 + |m|]
354 s1 = ha / a;
355 s0 = fa * a;
356
357 // Compute singular vectors
358 T tau; // second assignment to T in DLASV2
359 if (mm != 0.0) {
360 tau = (m / (s + t) + m / (r + l)) * (1.0 + a);
361 } else {
362 // note that m is very tiny
363 tau = l == 0.0 ?
364 T(copysign(T(2.0), f) * copysign(T(1.0), g)) :
365 T(g / copysign(d, f) + m / t);
366 }
367 T lv = std::sqrt(tau * tau + 4.0); // second assignment to L in DLASV2
368 cv = 2.0 / lv;
369 sv = tau / lv;
370 cu = (cv + sv * m) / a;
371 su = (h / f) * sv / a;
372 }
373
374 // Fix signs of singular values in accordance to sign of singular vectors
375 s0 = copysign(s0, f);
376 s1 = copysign(s1, h);
377
378 if (swap_diag == true) {
379 std::swap(cu, sv);
380 std::swap(su, cv);
381 }
382
383 Tensor<T, N> U(cu, -su, su, cu);
384 Tensor<T, N> S(s0, 0.0, 0.0, s1);
385 Tensor<T, N> V(cv, -sv, sv, cv);
386
387 return std::make_tuple(U, S, V);
388}
389
390//
391// R^2 singular value decomposition (SVD)
392// \param A tensor
393// \return \f$ A = USV^T\f$
394//
395template <typename T, Index N>
396std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
397svd_2x2(Tensor<T, N> const &A) {
398 assert(A.get_dimension() == 2);
399
400 // First compute a givens rotation to eliminate 1,0 entry in tensor
401 T c = 1.0;
402 T s = 0.0;
403 std::tie(c, s) = givens(A(0, 0), A(1, 0));
404
405 Tensor<T, N>
406 R(c, -s, s, c);
407
408 Tensor<T, N>
409 B = R * A;
410
411 // B is bidiagonal. Use specialized algorithm to compute its SVD
412 Tensor<T, N>
413 X(2), S(2), V(2);
414
415 std::tie(X, S, V) = svd_bidiagonal<T, N>(B(0, 0), B(0, 1), B(1, 1));
416
417 // Complete general 2x2 SVD with givens rotation calculated above
418 Tensor<T, N>
419 U = transpose(R) * X;
420
421 return std::make_tuple(U, S, V);
422}
423
424//
425// R^N singular value decomposition (SVD)
426// \param A tensor
427// \return \f$ A = USV^T\f$
428//
429template <typename T, Index N>
430std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
431svd_NxN(Tensor<T, N> const &A) {
432 // Scale first
433 T const
434 norm_a = norm(A);
435
436 T const
437 scale = norm_a > 0.0 ? norm_a : T(1.0);
438
439 Tensor<T, N>
440 S = A / scale;
441
442 Index const
443 dimension = A.get_dimension();
444
445 Tensor<T, N>
446 U = identity<T, N>(dimension);
447
448 Tensor<T, N>
449 V = identity<T, N>(dimension);
450
451 T
452 off = norm_off_diagonal(S);
453
454 T const
455 tol = machine_epsilon<T>();
456
457 Index const
458 max_iter = 2048;
459
460 Index
461 num_iter = 0;
462
463 while (off > tol && num_iter < max_iter) {
464
465 // Find largest off-diagonal entry
466 Index
467 p = 0;
468
469 Index
470 q = 0;
471
472 std::tie(p, q) = arg_max_off_diagonal(S);
473
474 if (p > q) {
475 std::swap(p, q);
476 }
477
478 // Obtain left and right Givens rotations by using 2x2 SVD
479 Tensor <T, 2>
480 Spq(S(p,p), S(p,q), S(q,p), S(q,q));
481
482 Tensor <T, 2>
483 L(2), D(2), R(2);
484
485 std::tie(L, D, R) = svd_2x2(Spq);
486
487 T const &
488 cl = L(0,0);
489
490 T const &
491 sl = L(0,1);
492
493 T const &
494 cr = R(0,0);
495
496 T const &
497 sr = (sgn(R(0,1)) == sgn(R(1,0))) ? T(-R(0,1)) : T(R(0,1));
498
499 // Apply both Givens rotations to matrices
500 // that are converging to singular values and singular vectors
501 givens_left(cl, sl, p, q, S);
502 givens_right(cr, sr, p, q, S);
503
504 givens_right(cl, sl, p, q, U);
505 givens_left(cr, sr, p, q, V);
506
507 off = norm_off_diagonal(S);
508 num_iter++;
509 }
510
511 if (num_iter == max_iter) {
512 MT_WARNING("SVD iteration did not converge.");
513 }
514
515 // Fix signs for entries in the diagonal matrix S
516 // that are negative
517 for (Index i = 0; i < dimension; ++i) {
518 if (S(i,i) < 0.0) {
519 S(i,i) = -S(i,i);
520 for (Index j = 0; j < dimension; ++j) {
521 U(j,i) = -U(j,i);
522 }
523 }
524 }
525
526 Vector<T, N> s(dimension);
527 Tensor<T, N> P(dimension);
528
529 std::tie(s, P) = sort_permutation(diag(S));
530 S = scale * diag(s);
531
532 // V has accumulated the right rotations on its left, so the right singular
533 // vectors are the columns of transpose(V). The permutation reorders those
534 // columns, as it does the columns of U.
535 U = U * P;
536 V = transpose(V) * P;
537
538 return std::make_tuple(U, diag(diag(S)), V);
539}
540
541} // namespace impl
542
543//
544// R^N singular value decomposition (SVD)
545// \param A tensor
546// \return \f$ A = USV^T\f$
547//
548template <typename T, Index N>
549std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
550svd(Tensor<T, N> const &A) {
551 Index const
552 dimension = A.get_dimension();
553
555 U(dimension), S(dimension), V(dimension);
556
557 switch (dimension) {
558
559 default:
560 std::tie(U, S, V) = impl::svd_NxN(A);
561 break;
562
563 case 2:
564 std::tie(U, S, V) = impl::svd_2x2(A);
565 // svd_2x2 doubles as a building block inside svd_NxN's Jacobi sweep, so
566 // it returns the raw 2x2 factorization without the sign/order
567 // canonicalization that svd_NxN applies to its own result. Apply that
568 // canonicalization here, for the top-level 2D SVD only, so svd() returns
569 // the standard convention (nonnegative singular values in descending
570 // order) regardless of dimension. Folding a negative sign into the
571 // corresponding column of U preserves A = U S V^T.
572 for (Index i = 0; i < dimension; ++i) {
573 if (S(i, i) < 0.0) {
574 S(i, i) = -S(i, i);
575 for (Index j = 0; j < dimension; ++j) {
576 U(j, i) = -U(j, i);
577 }
578 }
579 }
580 // Sort descending. For 2x2 this is a single compare-and-swap of the two
581 // columns of U and V (and the two singular values), avoiding the heap
582 // allocation and matrix multiply that the general sort_permutation
583 // incurs.
584 if (S(0, 0) < S(1, 1)) {
585 std::swap(S(0, 0), S(1, 1));
586 std::swap(U(0, 0), U(0, 1));
587 std::swap(U(1, 0), U(1, 1));
588 std::swap(V(0, 0), V(0, 1));
589 std::swap(V(1, 0), V(1, 1));
590 }
591 break;
592
593 }
594
595 return std::make_tuple(U, S, V);
596}
597
598//
599// Project to O(N) (Orthogonal Group) using a Newton-type algorithm.
600// See Higham's Functions of Matrices p210 [2008]
601// \param A tensor (often a deformation-gradient-like tensor)
602// \return \f$ R = \argmin_Q \|A - Q\|\f$
603// This algorithm projects a given tensor in GL(N) to O(N).
604// The rotation/reflection obtained through this projection is
605// the orthogonal component of the real polar decomposition
606//
607template<typename T, Index N>
609Tensor<T, N>
611{
612 Index const
613 dimension = A.get_dimension();
614
615 bool
616 scale = true;
617
618 T const
619 tol_scale = 0.01;
620
621 T const tol_conv =
622 static_cast<Index>(Kokkos::sqrt(static_cast<double>(dimension))) *
624
626 X = A;
627
628 T
629 gamma = 2.0;
630
631 Index const
632 max_iter = 128;
633
634 Index
635 num_iter = 0;
636
637 while (num_iter < max_iter) {
638
640 Y = inverse(X);
641
642 T
643 mu = 1.0;
644
645 if (scale == true) {
646 mu = (norm_1(Y) * norm_infinity(Y)) / (norm_1(X) * norm_infinity(X));
647 mu = std::sqrt(std::sqrt(mu));
648 }
649
651 Z = 0.5 * (mu * X + transpose(Y) / mu);
652
654 D = Z - X;
655
656 T
657 delta = norm(D) / norm(Z);
658
659 if (scale == true && delta < tol_scale) {
660 scale = false;
661 }
662
663 bool
664 end_iter =
665 norm(D) <= std::sqrt(tol_conv) ||
666 (delta > 0.5 * gamma && scale == false);
667
668 X = Z;
669 gamma = delta;
670
671 if (end_iter == true) {
672 break;
673 }
674
675 num_iter++;
676
677 }
678
679 if (num_iter == max_iter) {
680 MT_WARNING("Polar iteration did not converge.");
681 }
682
683 return X;
684}
685
686//
687// R^N Left polar decomposition
688// \param A tensor (often a deformation-gradient-like tensor)
689// \return \f$ VR = A \f$ with \f$ R \in SO(N) \f$ and \f$ V \in SPD(N) \f$
690//
691template <typename T, Index N>
692std::pair<Tensor<T, N>, Tensor<T, N>> polar_left(Tensor<T, N> const &A) {
694 R = polar_rotation(A);
695
697 V = sym(A * transpose(R));
698
699 return std::make_pair(V, R);
700}
701
702//
703// R^N Right polar decomposition
704// \param A tensor (often a deformation-gradient-like tensor)
705// \return \f$ RU = A \f$ with \f$ R \in SO(N) \f$ and \f$ U \in SPD(N) \f$
706//
707template <typename T, Index N>
708std::pair<Tensor<T, N>, Tensor<T, N>> polar_right(Tensor<T, N> const &A) {
710 R = polar_rotation(A);
711
713 U = sym(transpose(R) * A);
714
715 return std::make_pair(R, U);
716}
717
718//
719// R^3 left polar decomposition with eigenvalue decomposition
720// \param F tensor (often a deformation-gradient-like tensor)
721// \return \f$ VR = F \f$ with \f$ R \in SO(3) \f$ and V SPD(3)
722//
723template <typename T, Index N>
724std::pair<Tensor<T, N>, Tensor<T, N>> polar_left_eig(Tensor<T, N> const &F) {
725 assert(F.get_dimension() == 3);
726
727 // set up return tensors
729 R(3);
730
732 V(3);
733
734 // temporary tensor used to compute R
736 Vinv(3);
737
738 // compute spd tensor
740 b = F * transpose(F);
741
742 // get eigenvalues/eigenvectors
744 eVal(3);
745
747 eVec(3);
748 std::tie(eVec, eVal) = eig_spd(b);
749
750 // compute sqrt() and inv(sqrt()) of eigenvalues
752 x = zero<T, N>(3);
753
754 for (Index i = 0; i < 3; ++i) {
755 if (eVal(i, i) < T(0)) {
756 MT_ERROR_EXIT("Non-SPD input: negative eigenvalue.");
757 }
758 }
759
760 x(0,0) = std::sqrt(eVal(0,0));
761 x(1,1) = std::sqrt(eVal(1,1));
762 x(2,2) = std::sqrt(eVal(2,2));
763
765 xi = zero<T, N>(3);
766
767 xi(0,0) = 1.0 / x(0,0);
768 xi(1,1) = 1.0 / x(1,1);
769 xi(2,2) = 1.0 / x(2,2);
770
771 // compute V, Vinv, and R
772 V = eVec * x * transpose(eVec);
773 Vinv = eVec * xi * transpose(eVec);
774 R = Vinv * F;
775 return std::make_pair(V, R);
776}
777
778//
779// R^3 right polar decomposition with eigenvalue decomposition
780// \param F tensor (often a deformation-gradient-like tensor)
781// \return \f$ RU = F \f$ with \f$ R \in SO(3) \f$ and U SPD(3)
782//
783template <typename T, Index N>
784std::pair<Tensor<T, N>, Tensor<T, N>> polar_right_eig(Tensor<T, N> const &F) {
785 Index const
786 dimension = F.get_dimension();
787
788 assert(dimension == 3);
789
791 R(dimension);
792
794 U(dimension);
795
796 // temporary tensor used to compute R
798 Uinv(dimension);
799
800 // compute spd tensor
802 C = transpose(F) * F;
803
804 // get eigenvalues/eigenvectors
806 eVal(dimension);
807
809 eVec(dimension);
810
811 std::tie(eVec, eVal) = eig_spd(C);
812
813 // compute sqrt() and inv(sqrt()) of eigenvalues
815 x = zero<T, N>(dimension);
816
817 for (Index i = 0; i < dimension; ++i) {
818 if (eVal(i, i) < T(0)) {
819 MT_ERROR_EXIT("Non-SPD input: negative eigenvalue.");
820 }
821 }
822
823 x(0,0) = std::sqrt(eVal(0,0));
824 x(1,1) = std::sqrt(eVal(1,1));
825 x(2,2) = std::sqrt(eVal(2,2));
826
828 xi = zero<T, N>(dimension);
829
830 xi(0,0) = 1.0 / x(0,0);
831 xi(1,1) = 1.0 / x(1,1);
832 xi(2,2) = 1.0 / x(2,2);
833
834 // compute U, Uinv, and R
835 U = eVec * x * transpose(eVec);
836 Uinv = eVec * xi * transpose(eVec);
837 R = F * Uinv;
838
839 return std::make_pair(R, U);
840}
841
842//
843// R^N left polar decomposition with matrix logarithm for V
844// \param F tensor (often a deformation-gradient-like tensor)
845// \return \f$ VR = F \f$ with \f$ R \in SO(N) \f$ and V SPD(N), and log V
846//
847template <typename T, Index N>
848std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
850 Index const
851 dimension = F.get_dimension();
852
854 X(dimension), S(dimension), Y(dimension);
855
856 std::tie(X, S, Y) = svd(F);
857
859 R = X * transpose(Y);
860
862 V = X * S * transpose(X);
863
865 s = S;
866
867 for (Index i = 0; i < dimension; ++i) {
868 s(i,i) = std::log(s(i,i));
869 }
870
872 v = X * s * transpose(X);
873
874 return std::make_tuple(V, R, v);
875}
876
881template <typename T, Index N>
882std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
884 Index const
885 dimension = F.get_dimension();
886
887 Tensor<T, N> const
888 b = dot_t(F, F);
889
891 V(dimension), D(dimension);
892
893 std::tie(V, D) = eig_sym(b);
894
896 DQ(dimension, Filler::ZEROS), DI(dimension, Filler::ZEROS), DL(dimension, Filler::ZEROS);
897
898 for (Index i = 0; i < dimension; ++i) {
899 if (D(i,i) < T(0)) {
900 MT_ERROR_EXIT("Non-SPD input: negative eigenvalue.");
901 }
902 DQ(i,i) = std::sqrt(D(i,i));
903 DI(i,i) = 1.0 / DQ(i,i);
904 DL(i,i) = std::log(DQ(i,i));
905 }
906
907 Tensor<T, N> const
908 R = dot(V, DI) * t_dot(V, F);
909
910 Tensor<T, N> const
911 X = V * dot_t(DQ, V);
912
913 Tensor<T, N> const
914 x = V * dot_t(DL, V);
915
916 return std::make_tuple(X, R, x);
917}
918
919//
920// R^N left polar decomposition with matrix logarithm for V
921// \param F tensor (often a deformation-gradient-like tensor)
922// \return \f$ VR = F \f$ with \f$ R \in SO(N) \f$ and V SPD(N), and log V
923//
924template <typename T, Index N>
925std::tuple<Tensor<T, N>, Tensor<T, N>, Tensor<T, N>>
927 Index const
928 dimension = F.get_dimension();
929
930 // set up return tensors
931 Tensor<T, N> R(dimension), V(dimension), v(dimension), Vinv(dimension);
932
933 // compute spd tensor
934 Tensor<T, N> b = F*transpose(F);
935
936 // get eigenvalues/eigenvectors
937 Tensor<T, N> eVal(dimension);
938 Tensor<T, N> eVec(dimension);
939 std::tie(eVec, eVal) = eig_spd_cos(b);
940
941 // compute sqrt() and inv(sqrt()) of eigenvalues
942 for (Index i = 0; i < 3; ++i) {
943 if (eVal(i,i) < T(0)) {
944 MT_ERROR_EXIT("Non-SPD input: negative eigenvalue.");
945 }
946 }
948 x(0,0) = std::sqrt(eVal(0,0));
949 x(1,1) = std::sqrt(eVal(1,1));
950 x(2,2) = std::sqrt(eVal(2,2));
951 Tensor<T, N> xi = zero<T, N>(3);
952 xi(0,0) = 1.0/x(0,0);
953 xi(1,1) = 1.0/x(1,1);
954 xi(2,2) = 1.0/x(2,2);
955 Tensor<T, N> lnx = zero<T, N>(3);
956 lnx(0,0) = std::log(x(0,0));
957 lnx(1,1) = std::log(x(1,1));
958 lnx(2,2) = std::log(x(2,2));
959 // compute V, Vinv, log(V)=v, and R
960 V = eVec*x*transpose(eVec);
961 Vinv = eVec*xi*transpose(eVec);
962 v = eVec*lnx*transpose(eVec);
963 R = Vinv*F;
964
965 return std::make_tuple(V, R, v);
966}
967
968//
969// Symmetric Schur algorithm for R^2.
970// \param \f$ A = [f, g; g, h] \in S(2) \f$
971// \return \f$ c, s \rightarrow [c, -s; s, c]\f diagonalizes A$
972//
973template <typename T>
974std::pair<T, T> schur_sym(T const f, T const g, T const h) {
975 T c = 1.0;
976 T s = 0.0;
977
978 if (g != 0.0) {
979 T t = (h - f) / (2.0 * g);
980
981 if (t >= 0.0) {
982 t = 1.0 / (std::sqrt(1.0 + t * t) + t);
983 } else {
984 t = -1.0 / (std::sqrt(1.0 + t * t) - t);
985 }
986 c = 1.0 / std::sqrt(1.0 + t * t);
987 s = t * c;
988 }
989
990 return std::make_pair(c, s);
991}
992
993//
994// Givens rotation. [c, -s; s, c] [a; b] = [r; 0]
995// \param a, b
996// \return c, s
997//
998template <typename T> std::pair<T, T> givens(T const &a, T const &b) {
999 T c = 1.0;
1000 T s = 0.0;
1001
1002 if (b != 0.0) {
1003 if (std::abs(b) > std::abs(a)) {
1004 T const t = - a / b;
1005 s = 1.0 / std::sqrt(1.0 + t * t);
1006 c = t * s;
1007 } else {
1008 T const t = - b / a;
1009 c = 1.0 / std::sqrt(1.0 + t * t);
1010 s = t * c;
1011 }
1012 }
1013
1014 return std::make_pair(c, s);
1015}
1016
1017namespace impl {
1018
1019//
1020// R^N eigenvalue decomposition for symmetric 2nd-order tensor
1021// \param A tensor
1022// \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1023// See algorithm 8.4.2 in Matrix Computations, Golub & Van Loan 1996
1024//
1025template <typename T, Index N>
1026std::pair<Tensor<T, N>, Tensor<T, N>> eig_sym_NxN(Tensor<T, N> const &A) {
1027 Tensor<T, N>
1028 D = sym(A);
1029
1030 Index const
1031 dimension = A.get_dimension();
1032
1033 Tensor<T, N>
1034 V = identity<T, N>(dimension);
1035
1036 T
1037 off = norm_off_diagonal(D);
1038
1039 T
1040 tol = machine_epsilon<T>() * norm(A);
1041
1042 // Estimate based on random generation and linear regression.
1043 // Golub & Van Loan p 429 expect ~ dimension * log(dimension)
1044 Index const
1045 max_iter = 5 * dimension * dimension / 2;
1046
1047 Index
1048 num_iter = 0;
1049
1050 while (off > tol && num_iter < max_iter) {
1051
1052 // Find largest off-diagonal entry
1053 Index
1054 p = 0;
1055
1056 Index
1057 q = 0;
1058
1059 std::tie(p, q) = arg_max_off_diagonal(D);
1060 if (p > q) {
1061 std::swap(p,q);
1062 }
1063
1064 // Obtain Givens rotations by using 2x2 symmetric Schur algorithm
1065 T const &
1066 f = D(p,p);
1067
1068 T const &
1069 g = D(p,q);
1070
1071 T const &
1072 h = D(q,q);
1073
1074 T
1075 c, s;
1076
1077 std::tie(c, s) = schur_sym(f, g, h);
1078
1079 // Apply Givens rotation to matrices
1080 // that are converging to eigenvalues and eigenvectors
1081 givens_left(c, s, p, q, D);
1082 givens_right(c, s, p, q, D);
1083
1084 givens_right(c, s, p, q, V);
1085
1086 off = norm_off_diagonal(D);
1087 num_iter++;
1088 }
1089
1090 Vector<T, N> d(dimension);
1091 Tensor<T, N> P(dimension);
1092
1093 std::tie(d, P) = sort_permutation(diag(D));
1094 D = diag(d);
1095 V = V * P;
1096
1097 return std::make_pair(V, D);
1098}
1099
1100//
1101// R^2 eigenvalue decomposition for symmetric 2nd-order tensor
1102// \param A tensor
1103// \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1104//
1105template <typename T, Index N>
1106std::pair<Tensor<T, N>, Tensor<T, N>> eig_sym_2x2(Tensor<T, N> const &A) {
1107 assert(A.get_dimension() == 2);
1108
1109 T const f = A(0,0);
1110 T const g = 0.5 * (A(0,1) + A(1,0));
1111 T const h = A(1,1);
1112
1113 //
1114 // Eigenvalues, based on LAPACK's dlae2
1115 //
1116 T const sum = f + h;
1117 T const dif = std::abs(f - h);
1118 T const g2 = std::abs(g + g);
1119
1120 T fhmax = f;
1121 T fhmin = h;
1122
1123 const bool swap_diag = std::abs(h) > std::abs(f);
1124
1125 if (swap_diag == true) {
1126 std::swap(fhmax, fhmin);
1127 }
1128
1129 T r = 0.0;
1130 if (dif > g2) {
1131 T const t = g2 / dif;
1132 r = dif * std::sqrt(1.0 + t * t);
1133 } else if (dif < g2) {
1134 T const t = dif / g2;
1135 r = g2 * std::sqrt(1.0 + t * t);
1136 } else {
1137 // dif == g2, including zero
1138 r = g2 * std::sqrt(2.0);
1139 }
1140
1141 T s0 = 0.0;
1142 T s1 = 0.0;
1143
1144 if (sum != 0.0) {
1145 s0 = 0.5 * (sum + copysign(r, sum));
1146 // Order of execution important.
1147 // To get fully accurate smaller eigenvalue,
1148 // next line needs to be executed in higher precision.
1149 s1 = (fhmax / s0) * fhmin - (g / s0) * g;
1150 } else {
1151 // s0 == s1, including zero
1152 s0 = 0.5 * r;
1153 s1 = -0.5 * r;
1154 }
1155
1156 //
1157 // Eigenvectors. schur_sym returns the Jacobi rotation J = [c, s; -s, c]
1158 // whose columns are the eigenvectors and which diagonalizes A as
1159 // A = J * diag(l0, l1) * transpose(J), with the eigenvalue attached to
1160 // column 0 of J being l0 = f - tan(theta) * g and to column 1 being
1161 // l1 = h + tan(theta) * g (tan(theta) = s / c).
1162 //
1163 // The previous implementation used transpose(J) as V and paired the
1164 // magnitude-ordered eigenvalues (s0, s1) to the columns through swap_diag, so
1165 // V * D * transpose(V) did not reconstruct A for general off-diagonal cases
1166 // (Trilinos issue #15389). Build V = J directly, and attach the accurate
1167 // (s0, s1) eigenvalues to their matching columns.
1168 //
1169 T
1170 c, s;
1171
1172 std::tie(c, s) = schur_sym(f, g, h);
1173
1174 Tensor<T, N>
1175 V(c, s, -s, c);
1176
1177 T const l0 = f - (s / c) * g;
1178
1179 T d0, d1;
1180 if (std::abs(l0 - s0) <= std::abs(l0 - s1)) {
1181 d0 = s0;
1182 d1 = s1;
1183 } else {
1184 d0 = s1;
1185 d1 = s0;
1186 }
1187
1188 //
1189 // Return eigenvalues in descending order, permuting the eigenvectors to
1190 // match, for consistency with the eig_sym_NxN path. For 2x2 this is a single
1191 // compare-and-swap; done inline to avoid the heap allocation and matrix
1192 // multiply of the general sort_permutation, as eig_sym is called per
1193 // integration point in 2D mechanics.
1194 //
1195 if (d0 < d1) {
1196 std::swap(d0, d1);
1197 std::swap(V(0, 0), V(0, 1));
1198 std::swap(V(1, 0), V(1, 1));
1199 }
1200
1201 Tensor<T, N>
1202 D(d0, 0.0, 0.0, d1);
1203
1204 return std::make_pair(V, D);
1205}
1206
1207} // namespace impl
1208
1209//
1210// R^N eigenvalue decomposition for symmetric 2nd-order tensor
1211// \param A tensor
1212// \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1213//
1214template <typename T, Index N>
1215std::pair<Tensor<T, N>, Tensor<T, N>> eig_sym(Tensor<T, N> const &A) {
1216 Index const
1217 dimension = A.get_dimension();
1218
1220 V(dimension), D(dimension);
1221
1222 switch (dimension) {
1223
1224 default:
1225 std::tie(V, D) = impl::eig_sym_NxN(A);
1226 break;
1227
1228 case 2:
1229 std::tie(V, D) = impl::eig_sym_2x2(A);
1230 break;
1231
1232 }
1233
1234 return std::make_pair(V, D);
1235}
1236
1237//
1238// R^N eigenvalue decomposition for SPD 2nd-order tensor
1239// \param A tensor
1240// \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1241//
1242template <typename T, Index N>
1243std::pair<Tensor<T, N>, Tensor<T, N>> eig_spd(Tensor<T, N> const &A) {
1244 return eig_sym(A);
1245}
1246
1247//
1248// R^3 eigenvalue decomposition for SPD 2nd-order tensor
1249// \param A tensor
1250// \return V eigenvectors, D eigenvalues in diagonal Matlab-style
1251//
1252template <typename T, Index N>
1253std::pair<Tensor<T, N>, Tensor<T, N>> eig_spd_cos(Tensor<T, N> const &A) {
1254 Index const
1255 dimension = A.get_dimension();
1256
1257 assert(dimension == 3);
1258
1259 // This algorithm comes from the journal article
1260 // Scherzinger and Dohrmann, CMAME 197 (2008) 4007-4015
1261
1262 // this algorithm will return the eigenvalues in D
1263 // and the eigenvectors in V
1265 D = zero<T, N>(dimension);
1266
1268 V = zero<T, N>(dimension);
1269
1270 // not sure if this is necessary...
1271 T
1272 pi = std::acos(-1);
1273
1274 // convenience operators
1275 Tensor<T, N> const
1276 I = identity<T, N>(dimension);
1277
1278 int
1279 ii[3][2] = { { 1, 2 }, { 2, 0 }, { 0, 1 } };
1280
1282 rm = zero<T, N>(dimension);
1283
1284 // scale the matrix to reduce the characteristic equation
1285 T
1286 trA = (1.0/3.0) * I1(A);
1287
1289 Ap(A - trA*I);
1290
1291 // compute other invariants
1292 T
1293 J2 = I2(Ap);
1294
1295 T
1296 J3 = det(Ap);
1297
1298 // deal with volumetric tensors
1299 if (-J2 <= 1.e-30)
1300 {
1301 D(0,0) = trA;
1302 D(1,1) = trA;
1303 D(2,2) = trA;
1304
1305 V(0,0) = 1.0;
1306 V(1,0) = 0.0;
1307 V(2,0) = 0.0;
1308
1309 V(0,1) = 0.0;
1310 V(1,1) = 1.0;
1311 V(2,1) = 0.0;
1312
1313 V(0,2) = 0.0;
1314 V(1,2) = 0.0;
1315 V(2,2) = 1.0;
1316 }
1317 else
1318 {
1319 // first things first, find the most dominant e-value
1320 // Need to solve cos(3 theta)=rhs for theta
1321 T
1322 t1 = 3.0 / -J2;
1323
1324 T
1325 rhs = (J3 / 2.0) * T(std::sqrt(t1 * t1 * t1));
1326
1327 T
1328 theta = pi / 2.0 * (1.0 - (rhs < 0 ? -1.0 : 1.0));
1329
1330 if (std::abs(rhs) <= 1.0) theta = std::acos(rhs);
1331
1332 T
1333 thetad3 = theta / 3.0;
1334
1335 if (thetad3 > pi / 6.0) thetad3 += 2.0 * pi / 3.0;
1336
1337 // most dominant e-value
1338 D(2,2) = 2.0 * std::cos(thetad3) * std::sqrt(-J2 / 3.0);
1339
1340 // now reduce the system
1342 R = Ap - D(2,2) * I;
1343
1344 // QR factorization with column pivoting
1345 Vector<T, N> a(dimension);
1346 a(0) = R(0,0)*R(0,0) + R(1,0)*R(1,0) + R(2,0)*R(2,0);
1347 a(1) = R(0,1)*R(0,1) + R(1,1)*R(1,1) + R(2,1)*R(2,1);
1348 a(2) = R(0,2)*R(0,2) + R(1,2)*R(1,2) + R(2,2)*R(2,2);
1349
1350 // find the most dominant column
1351 int k = 0;
1352 T max = a(0);
1353 if (a(1) > max)
1354 {
1355 k = 1;
1356 max = a(1);
1357 }
1358 if (a(2) > max)
1359 {
1360 k = 2;
1361 }
1362
1363 // normalize the most dominant column to get s1
1364 a(k) = std::sqrt(a(k));
1365 for (int i(0); i < dimension; ++i)
1366 R(i,k) /= a(k);
1367
1368 // dot products of dominant column with other two columns
1369 T d0 = 0.0;
1370 T d1 = 0.0;
1371 for (int i(0); i < dimension; ++i)
1372 {
1373 d0 += R(i,k) * R(i,ii[k][0]);
1374 d1 += R(i,k) * R(i,ii[k][1]);
1375 }
1376
1377 // projection
1378 for (int i(0); i < dimension; ++i)
1379 {
1380 R(i,ii[k][0]) -= d0 * R(i,k);
1381 R(i,ii[k][1]) -= d1 * R(i,k);
1382 }
1383
1384 // now finding next most dominant column
1385 a.clear();
1386 for (int i(0); i < dimension; ++i)
1387 {
1388 a(0) += R(i,ii[k][0]) * R(i,ii[k][0]);
1389 a(1) += R(i,ii[k][1]) * R(i,ii[k][1]);
1390 }
1391
1392 int p = 0;
1393 if (std::abs(a(1)) > std::abs(a(0))) p = 1;
1394
1395 // normalize next most dominant column to get s2
1396 a(p) = std::sqrt(a(p));
1397 int k2 = ii[k][p];
1398
1399 for (int i(0); i < dimension; ++i)
1400 R(i,k2) /= a(p);
1401
1402 // set first eigenvector as cross product of s1 and s2
1403 V(0,2) = R(1,k) * R(2,k2) - R(2,k) * R(1,k2);
1404 V(1,2) = R(2,k) * R(0,k2) - R(0,k) * R(2,k2);
1405 V(2,2) = R(0,k) * R(1,k2) - R(1,k) * R(0,k2);
1406
1407 // normalize
1408 T
1409 mag = std::sqrt(V(0,2) * V(0,2) + V(1,2) * V(1,2) + V(2,2) * V(2,2));
1410
1411 V(0,2) /= mag;
1412 V(1,2) /= mag;
1413 V(2,2) /= mag;
1414
1415 // now for the other two eigenvalues, extract vectors
1417 rk(R(0,k), R(1,k), R(2,k));
1418
1420 rk2(R(0,k2), R(1,k2), R(2,k2));
1421
1422 // compute projections
1424 ak = Ap * rk;
1425
1427 ak2 = Ap * rk2;
1428
1429 // set up reduced remainder matrix
1430 rm(0,0) = dot(rk,ak);
1431 rm(0,1) = dot(rk,ak2);
1432 rm(1,1) = dot(rk2,ak2);
1433
1434 // compute eigenvalues 2 and 3
1435 T
1436 b = 0.5 * (rm(0,0) - rm(1,1));
1437
1438 T
1439 fac = (b < 0 ? -1.0 : 1.0);
1440
1441 T
1442 arg = b * b + rm(0,1) * rm(0,1);
1443
1444 if (arg == 0)
1445 D(0,0) = rm(1,1) + b;
1446 else
1447 D(0,0) = rm(1,1) + b - fac * std::sqrt(b * b + rm(0,1) * rm(0,1));
1448
1449 D(1,1) = rm(0,0) + rm(1,1) - D(0,0);
1450
1451 // update reduced remainder matrix
1452 rm(0,0) -= D(0,0);
1453 rm(1,0) = rm(0,1);
1454 rm(1,1) -= D(0,0);
1455
1456 // again, find most dominant column
1457 a.clear();
1458 a(0) = rm(0,0) * rm(0,0) + rm(0,1) * rm(0,1);
1459 a(1) = rm(0,1) * rm(0,1) + rm(1,1) * rm(1,1);
1460
1461 int k3 = 0;
1462 if (a(1) > a(0)) k3 = 1;
1463 if (a(k3) == 0.0)
1464 {
1465 rm(0,k3) = 1.0;
1466 rm(1,k3) = 0.0;
1467 }
1468
1469 // set 2nd eigenvector via cross product
1470 V(0,0) = rm(0,k3) * rk2(0) - rm(1,k3) * rk(0);
1471 V(1,0) = rm(0,k3) * rk2(1) - rm(1,k3) * rk(1);
1472 V(2,0) = rm(0,k3) * rk2(2) - rm(1,k3) * rk(2);
1473
1474 // normalize
1475 mag = std::sqrt(V(0,0) * V(0,0) + V(1,0) * V(1,0) + V(2,0) * V(2,0));
1476 V(0,0) /= mag;
1477 V(1,0) /= mag;
1478 V(2,0) /= mag;
1479
1480 // set last eigenvector as cross product of other two
1481 V(0,1) = V(1,0) * V(2,2) - V(2,0) * V(1,2);
1482 V(1,1) = V(2,0) * V(0,2) - V(0,0) * V(2,2);
1483 V(2,1) = V(0,0) * V(1,2) - V(1,0) * V(0,2);
1484
1485 // normalize
1486 mag = std::sqrt(V(0,1) * V(0,1) + V(1,1) * V(1,1) + V(2,1) * V(2,1));
1487 V(0,1) /= mag;
1488 V(1,1) /= mag;
1489 V(2,1) /= mag;
1490
1491 // add back in the offset
1492 for (int i(0); i < dimension; ++i)
1493 D(i,i) += trA;
1494 }
1495
1496 return std::make_pair(V, D);
1497}
1498
1499//
1500// Cholesky decomposition, rank-1 update algorithm
1501// (Matrix Computations 3rd ed., Golub & Van Loan, p145)
1502// \param A assumed symmetric tensor
1503// \return G Cholesky factor A = GG^T
1504// \return completed (bool) algorithm ran to completion
1505//
1506template <typename T, Index N>
1507std::pair<Tensor<T, N>, bool> cholesky(Tensor<T, N> const &A) {
1509 G = sym(A);
1510
1511 Index const
1512 dimension = A.get_dimension();
1513
1514 for (Index k = 0; k < dimension; ++k) {
1515
1516 // Zeros above the diagonal
1517 for (Index j = k + 1; j < dimension; ++j) {
1518 G(k,j) = 0.0;
1519 }
1520
1521 T
1522 s = G(k,k);
1523
1524 if (s <= 0.0) {
1525 return std::make_pair(G, false);
1526 }
1527
1528 s = std::sqrt(s);
1529
1530 for (Index j = k + 1; j < dimension; ++j) {
1531 G(j,k) /= s;
1532 }
1533
1534 G(k,k) = s;
1535
1536 for (Index j = k + 1; j < dimension; ++j) {
1537 for (Index i = j; i < dimension; ++i) {
1538 G(i,j) -= G(i,k) * G(j,k);
1539 }
1540 }
1541
1542 }
1543
1544 return std::make_pair(G, true);
1545}
1546
1548} // namespace minitensor
1549
1550#endif // MiniTensor_Factorizations_h
#define MT_WARNING(...)
#define KOKKOS_INLINE_FUNCTION
#define MT_ERROR_EXIT(...)
KOKKOS_INLINE_FUNCTION void clear()
KOKKOS_INLINE_FUNCTION Tensor< T, N > const identity()
KOKKOS_INLINE_FUNCTION Tensor< T, N > const zero()
KOKKOS_INLINE_FUNCTION Index get_dimension() const
KOKKOS_INLINE_FUNCTION Index get_dimension() const
KOKKOS_INLINE_FUNCTION Matrix< typename Promote< S, T >::type, M, N > dot_t(Matrix< S, M, P > const &A, Matrix< T, N, P > const &B)
KOKKOS_INLINE_FUNCTION Tensor< T, N > diag(Vector< T, N > const &v)
KOKKOS_INLINE_FUNCTION void scale(TensorBase< R, SR > const &A, S const &s, TensorBase< T, ST > &B)
KOKKOS_INLINE_FUNCTION Vector< typename Promote< S, T >::type, M > dot(Matrix< T, M, N > const &A, Vector< S, N > const &u)
KOKKOS_INLINE_FUNCTION Matrix< typename Promote< S, T >::type, M, N > t_dot(Matrix< S, P, M > const &A, Matrix< T, P, N > const &B)
KOKKOS_INLINE_FUNCTION Matrix< T, M, N > transpose(Matrix< T, N, M > const &A)
KOKKOS_INLINE_FUNCTION Tensor< T, N > sym(Tensor< T, N > const &A)
std::pair< Tensor< T, N >, Tensor< T, N > > eig_spd(Tensor< T, N > const &A)
std::tuple< Tensor< T, N >, Tensor< T, N >, Tensor< T, N > > polar_left_logV_lame(Tensor< T, N > const &F)
std::pair< Tensor< T, N >, Tensor< T, N > > polar_left_eig(Tensor< T, N > const &A)
std::tuple< Tensor< T, N >, Tensor< T, N >, Tensor< T, N > > polar_left_logV(Tensor< T, N > const &F)
std::pair< T, T > givens(T const &a, T const &b)
std::pair< T, T > schur_sym(const T f, const T g, const T h)
std::pair< Tensor< T, N >, Tensor< T, N > > polar_right_eig(Tensor< T, N > const &A)
std::tuple< Tensor< T, N >, Tensor< T, N >, Tensor< T, N > > polar_left_logV_eig(Tensor< T, N > const &F)
T cond(Tensor< T, N > const &A)
std::pair< Tensor< T, N >, Tensor< T, N > > eig_sym(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION Tensor< T, N > polar_rotation(Tensor< T, N > const &A)
std::tuple< Tensor< T, N >, Tensor< T, N >, Tensor< T, N > > svd(Tensor< T, N > const &A)
std::pair< Tensor< T, N >, Tensor< T, N > > polar_right(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION void givens_right(T const &c, T const &s, Index i, Index k, Tensor< T, N > &A)
std::pair< Tensor< T, N >, Tensor< T, N > > polar_left(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION void givens_left(T const &c, T const &s, Index i, Index k, Tensor< T, N > &A)
std::pair< Tensor< T, N >, Tensor< T, N > > eig_spd_cos(Tensor< T, N > const &A)
T inv_cond(Tensor< T, N > const &A)
std::pair< Tensor< T, N >, bool > cholesky(Tensor< T, N > const &A)
std::pair< Vector< T, N >, Tensor< T, N > > sort_permutation(Vector< T, N > const &u)
KOKKOS_INLINE_FUNCTION Tensor< T, N > inverse(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T I1(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T norm(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T det(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T norm_off_diagonal(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T norm_1(Tensor< T, N > const &A)
std::pair< Index, Index > arg_max_off_diagonal(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T norm_infinity(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T I2(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION Sacado::ScalarType< T >::type machine_epsilon()
KOKKOS_INLINE_FUNCTION Sacado::ScalarType< T >::type tau()
KOKKOS_INLINE_FUNCTION int sgn(T const &s)
KOKKOS_INLINE_FUNCTION T abs(T const &a)
KOKKOS_INLINE_FUNCTION T max(T const &a, T const &b)
uint32_t Index
Indexing type.
KOKKOS_INLINE_FUNCTION T copysign(T const &a, T const &b)