42 #if !defined(Intrepid_MiniTensor_LinearAlgebra_t_h) 43 #define Intrepid_MiniTensor_LinearAlgebra_t_h 57 inverse(Tensor<T>
const & A)
60 N = A.get_dimension();
63 Tensor<T> B = identity<T>(N);
65 typedef std::set<Index> IndexSet;
66 typedef std::set<Index>::const_iterator IndexIter;
71 for (Index k = 0; k < N; ++k) {
72 intact_rows.insert(k);
73 intact_cols.insert(k);
77 for (Index k = 0; k < N; ++k) {
82 IndexIter pivot_row_iter = intact_rows.begin();
83 IndexIter pivot_col_iter = intact_cols.begin();
85 for (IndexIter rows_iter = intact_rows.begin();
86 rows_iter != intact_rows.end(); ++rows_iter) {
88 for (IndexIter cols_iter = intact_cols.begin();
89 cols_iter != intact_cols.end(); ++cols_iter) {
91 Index
const row = *rows_iter;
92 Index
const col = *cols_iter;
93 T
const s = std::abs(S(row, col));
97 pivot_row_iter = rows_iter;
98 pivot_col_iter = cols_iter;
108 Index
const pivot_row = *pivot_row_iter;
109 Index
const pivot_col = *pivot_col_iter;
112 T
const t = S(pivot_row, pivot_col);
115 std::cerr <<
"ERROR: " << __PRETTY_FUNCTION__;
116 std::cerr << std::endl;
117 std::cerr <<
"Inverse of singular tensor.";
118 std::cerr << std::endl;
122 for (Index j = 0; j < N; ++j) {
123 S(pivot_row, j) /= t;
124 B(pivot_row, j) /= t;
127 for (Index i = 0; i < N; ++i) {
128 if (i == pivot_row)
continue;
130 T
const c = S(i, pivot_col);
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);
139 intact_rows.erase(pivot_row_iter);
140 intact_cols.erase(pivot_col_iter);
159 subtensor(Tensor<T>
const & A, Index
const i, Index
const j)
162 N = A.get_dimension();
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;
189 exp(Tensor<T>
const & A)
201 exp_taylor(Tensor<T>
const & A)
207 tol = machine_epsilon<T>();
210 N = A.get_dimension();
213 term = identity<T>(N);
217 relative_error = 1.0;
225 while (relative_error > tol && k < max_iter) {
226 term =
static_cast<T
>(1.0 / (k + 1.0)) * term * A;
228 relative_error = norm_1(term);
242 scaling_squaring_theta(Index
const order)
244 assert(order > 0 && order < 22);
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
261 polynomial_coefficient(Index
const order, Index
const index)
263 assert(index <= order);
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;
281 b[] = {120.0, 60.0, 12.0, 1.0};
290 b[] = {30240.0, 15120.0, 3360.0, 420.0, 30.0, 1.0};
299 b[] = {17297280.0, 8648640.0, 1995840.0, 277200.0, 25200.0, 1512.0,
309 b[] = {17643225600.0, 8821612800.0, 2075673600.0, 302702400.0,
310 30270240.0, 2162160.0, 110880.0, 3960.0, 90.0, 1.0};
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};
337 std::pair<Tensor<T>, Tensor<T> >
338 pade_polynomial_terms(Tensor<T>
const & A, Index
const order)
341 N = A.get_dimension();
347 U = polynomial_coefficient<Real>(order, 1) * B;
350 V = polynomial_coefficient<Real>(order, 0) * B;
355 for (Index i = 3; i <= order; i += 2) {
360 O = polynomial_coefficient<Real>(order, i) * B;
363 E = polynomial_coefficient<Real>(order, i - 1) * B;
373 return std::make_pair(U, V);
381 binary_powering(Tensor<T>
const & A, Index
const exponent)
383 if (exponent == 0)
return eye<T>(A.get_dimension());
389 number_digits = std::numeric_limits<Index>::digits;
392 leftmost_bit = rightmost_bit << (number_digits - 1);
397 for (Index j = 0; j < number_digits; ++j) {
399 if (((exponent << j) & leftmost_bit) != 0) {
401 t = number_digits - j - 1;
417 while ((m & rightmost_bit) == 0) {
426 for (Index j = i + 1; j <= t; ++j) {
429 if (((exponent >> j) & rightmost_bit) != 0) {
447 exp_pade(Tensor<T>
const & A)
450 N = A.get_dimension();
453 orders[] = {3, 5, 7, 9, 13};
459 highest_order = orders[number_orders - 1];
465 norm = Sacado::ScalarValue<T>::eval((norm_1(A)));
467 for (Index i = 0; i < number_orders; ++i) {
473 theta = scaling_squaring_theta<Real>(order);
475 if (order < highest_order && norm < theta) {
483 boost::tie(U, V) = pade_polynomial_terms(A, order);
485 B = inverse(V - U) * (U + V);
489 }
else if (order == highest_order) {
492 theta_highest = scaling_squaring_theta<Real>(order);
495 power_two = Index(std::ceil(log2(norm / theta_highest)));
500 for (Index j = 0; j < power_two; ++j) {
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);
536 (A6 * (b13 * A6 + b11 * A4 + b9 * A2) +
537 b7 * A6 + b5 * A4 + b3 * A2 + b1 * I));
540 V = A6 * (b12 * A6 + b10 * A4 + b8 * A2) +
541 b6 * A6 + b4 * A4 + b2 * A2 + b0 * I;
544 R = inverse(V - U) * (U + V);
547 exponent = (1U << power_two);
549 B = binary_powering(R, exponent);
563 log_taylor(Tensor<T>
const & A)
569 tol = machine_epsilon<T>();
572 norm_tensor = norm_1(A);
575 N = A.get_dimension();
578 A_minus_I = A - identity<T>(N);
584 norm_term = norm_1(term);
587 relative_error = norm_term / norm_tensor;
595 while (relative_error > tol && k <= max_iter) {
596 term =
static_cast<T
>(- (k / (k + 1.0))) * term * A_minus_I;
598 norm_term = norm_1(term);
599 relative_error = norm_term / norm_tensor;
611 log(Tensor<T>
const & A)
613 return log_gregory(A);
621 log_gregory(Tensor<T>
const & A)
627 tol = machine_epsilon<T>();
630 norm_tensor = norm_1(A);
633 N = A.get_dimension();
636 I_minus_A = identity<T>(N) - A;
639 I_plus_A = identity<T>(N) + A;
642 term = I_minus_A * inverse(I_plus_A);
645 norm_term = norm_1(term);
648 relative_error = norm_term / norm_tensor;
659 while (relative_error > tol && k <= max_iter) {
660 term =
static_cast<T
>((2 * k - 1.0) / (2 * k + 1.0)) * term * C;
662 norm_term = norm_1(term);
663 relative_error = norm_term / norm_tensor;
677 log_sym(Tensor<T>
const & A)
679 return log_eig_sym(A);
687 log_eig_sym(Tensor<T>
const & A)
690 N = A.get_dimension();
698 boost::tie(V, D) = eig_sym(A);
700 for (Index i = 0; i < N; ++i) {
701 D(i, i) = std::log(D(i, i));
705 B = dot_t(dot(V, D), V);
717 log_rotation(Tensor<T>
const & R)
720 N = R.get_dimension();
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>());
728 cosine = 0.5 * (trace(R) - 1.0);
732 }
else if(cosine > 1.0) {
737 theta = std::acos(cosine);
745 std::cerr <<
"Logarithm of SO(N) N != 2,3 not implemented." << std::endl;
754 }
else if (std::abs(cosine + 1.0) < 10.0 * machine_epsilon<T>()) {
756 r = log_rotation_pi(R);
760 r = theta / std::sin(theta) * skew(R);
783 log_rotation_pi(Tensor<T>
const & R)
786 N = R.get_dimension();
789 cosine = 0.5*(trace(R) - 1.0);
791 assert(std::abs(cosine + 1.0) < 10.0 * machine_epsilon<T>());
799 std::cerr <<
"Logarithm of SO(N) N != 2,3 not implemented." << std::endl;
806 r = gaussian_elimination((R - identity<T>(3)));
809 T
const tol = 10.0 * machine_epsilon<T>();
813 if (std::abs(r(2,2)) < tol){
819 if (std::abs(r(1,1)) < tol){
822 normal(1) = -normal(2)*r(1,2)/r(1,1);
825 if (std::abs(r(0,0)) < tol){
828 normal(0) = -normal(1)*r(0,1) - normal(2)*r(0,2)/r(0,0);
831 normal = normal / norm(normal);
841 T
const pi = std::acos(-1.0);
849 T theta = std::acos(-1.0);
873 gaussian_elimination(Tensor<T>
const & A)
876 N = A.get_dimension();
882 tol = 10.0 * machine_epsilon<T>();
888 while ((i < N) && (j < N)) {
891 for (Index k = i + 1; k < N; ++k) {
892 if (std::abs(U(k,j)) > std::abs(U(i_max,j))) {
898 if (std::abs(U(i_max,j)) > tol){
901 for (Index k = 0; k < N; ++k) {
902 std::swap(U(i,k), U(i_max,k));
905 for (Index k = 0; k < N; ++k) {
906 U(i,k) = U(i,k) / U(i,j);
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);
928 givens_left(T
const & c, T
const & s, Index i, Index k, Tensor<T> & A)
931 N = A.get_dimension();
933 for (Index j = 0; j < N; ++j) {
936 A(i,j) = c * t1 - s * t2;
937 A(k,j) = s * t1 + c * t2;
948 givens_right(T
const & c, T
const & s, Index i, Index k, Tensor<T> & A)
951 N = A.get_dimension();
953 for (Index j = 0; j < N; ++j) {
956 A(j,i) = c * t1 - s * t2;
957 A(j,k) = s * t1 + c * t2;
969 exp_skew_symmetric(Tensor<T>
const & r)
972 assert(norm(sym(r)) < machine_epsilon<T>());
975 N = r.get_dimension();
986 std::cerr <<
"Exp of so(N) N != 2,3 not implemented." << std::endl;
991 theta = std::sqrt(r(2,1)*r(2,1)+r(0,2)*r(0,2)+r(1,0)*r(1,0));
994 if (theta >= machine_epsilon<T>()) {
995 R += std::sin(theta) / theta * r +
996 (1.0 - std::cos(theta)) / (theta * theta) * r * r;
1004 c = std::cos(theta);
1007 s = std::sin(theta);
1027 template<
typename T>
1029 norm_off_diagonal(Tensor<T>
const & A)
1032 N = A.get_dimension();
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);
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);
1052 s = A(0,1)*A(0,1) + A(1,0)*A(1,0);
1057 return std::sqrt(s);
1066 template<
typename T>
1067 std::pair<Index, Index>
1068 arg_max_abs(Tensor<T>
const & A)
1073 T s = std::abs(A(p,q));
1076 N = A.get_dimension();
1078 for (Index i = 0; i < N; ++i) {
1079 for (Index j = 0; j < N; ++j) {
1080 if (std::abs(A(i,j)) > s) {
1083 s = std::abs(A(i,j));
1088 return std::make_pair(p,q);
1097 template<
typename T>
1098 std::pair<Index, Index>
1099 arg_max_off_diagonal(Tensor<T>
const & A)
1104 T s = std::abs(A(p,q));
1107 N = A.get_dimension();
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) {
1114 s = std::abs(A(i,j));
1119 return std::make_pair(p,q);
1132 template<
typename T>
1133 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1134 svd_bidiagonal(T f, T g, T h)
1148 bool swap_diag = (ha > fa);
1158 }
else if (ga > fa && fa / ga < machine_epsilon<T>()) {
1178 T s = std::sqrt(tt + mm);
1180 T(std::sqrt(l * l + mm)) :
1182 T a = 0.5 * (s + r);
1189 tau = (m / (s + t) + m / (r + l)) * (1.0 + a);
1193 T(copysign(T(2.0), f) * copysign(T(1.0), g)) :
1194 T(g / copysign(d, f) + m / t);
1196 T lv = std::sqrt(tau * tau + 4.0);
1199 cu = (cv + sv * m) / a;
1200 su = (h / f) * sv / a;
1204 s0 = copysign(s0, f);
1205 s1 = copysign(s1, h);
1212 Tensor<T> U(cu, -su, su, cu);
1214 Tensor<T> S(s0, 0.0, 0.0, s1);
1216 Tensor<T> V(cv, -sv, sv, cv);
1218 return boost::make_tuple(U, S, V);
1226 template<
typename T>
1227 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1228 svd_2x2(Tensor<T>
const & A)
1230 assert(A.get_dimension() == 2);
1235 boost::tie(c, s) = givens(A(0,0), A(1,0));
1247 boost::tie(X, S, V) = svd_bidiagonal(B(0,0), B(0,1), B(1,1));
1251 U = transpose(R) * X;
1253 return boost::make_tuple(U, S, V);
1261 template<
typename T>
1262 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1263 svd_NxN(Tensor<T>
const & A)
1269 N = A.get_dimension();
1278 off = norm_off_diagonal(S);
1281 tol = machine_epsilon<T>() * norm(A);
1289 while (off > tol && num_iter < max_iter) {
1298 boost::tie(p,q) = arg_max_off_diagonal(S);
1306 Spq(S(p,p), S(p,q), S(q,p), S(q,q));
1311 boost::tie(L, D, R) = svd_2x2(Spq);
1323 sr = (sgn(R(0,1)) == sgn(R(1,0))) ? T(-R(0,1)) : T(R(0,1));
1327 givens_left(cl, sl, p, q, S);
1328 givens_right(cr, sr, p, q, S);
1330 givens_right(cl, sl, p, q, U);
1331 givens_left(cr, sr, p, q, V);
1333 off = norm_off_diagonal(S);
1337 if (num_iter == max_iter) {
1338 std::cerr <<
"WARNING: SVD iteration did not converge." << std::endl;
1343 for (Index i = 0; i < N; ++i) {
1346 for (Index j = 0; j < N; ++j) {
1355 boost::tie(s, P) = sort_permutation(diag(S));
1360 return boost::make_tuple(U, diag(diag(S)), transpose(V));
1370 template<
typename T>
1371 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1372 svd(Tensor<T>
const & A)
1375 N = A.get_dimension();
1383 boost::tie(U, S, V) = svd_NxN(A);
1387 boost::tie(U, S, V) = svd_2x2(A);
1392 return boost::make_tuple(U, S, V);
1404 template<
typename T>
1406 polar_rotation(Tensor<T>
const & A)
1409 N = A.get_dimension();
1418 tol_conv = std::sqrt(N) * machine_epsilon<T>();
1432 while (num_iter < max_iter) {
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));
1446 Z = 0.5 * (mu * X + transpose(Y) / mu);
1452 delta = norm(D) / norm(Z);
1454 if (scale ==
true && delta < tol_scale) {
1460 norm(D) <= std::sqrt(tol_conv) ||
1461 (delta > 0.5 * gamma && scale ==
false);
1466 if (end_iter ==
true) {
1474 if (num_iter == max_iter) {
1475 std::cerr <<
"WARNING: Polar iteration did not converge." << std::endl;
1486 template<
typename T>
1487 std::pair<Tensor<T>, Tensor<T> >
1488 polar_left(Tensor<T>
const & A)
1491 R = polar_rotation(A);
1494 V = sym(A * transpose(R));
1496 return std::make_pair(V, R);
1504 template<
typename T>
1505 std::pair<Tensor<T>, Tensor<T> >
1506 polar_right(Tensor<T>
const & A)
1509 R = polar_rotation(A);
1512 U = sym(transpose(R) * A);
1514 return std::make_pair(R, U);
1522 template<
typename T>
1523 std::pair<Tensor<T>, Tensor<T> >
1524 polar_left_eig(Tensor<T>
const & F)
1526 assert(F.get_dimension() == 3);
1541 b = F * transpose(F);
1550 boost::tie(eVec, eVal) = eig_spd(b);
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));
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);
1568 V = eVec * x * transpose(eVec);
1569 Vinv = eVec * xi * transpose(eVec);
1572 return std::make_pair(V, R);
1580 template<
typename T>
1581 std::pair<Tensor<T>, Tensor<T> >
1582 polar_right_eig(Tensor<T>
const & F)
1584 assert(F.get_dimension() == 3);
1598 C = transpose(F) * F;
1607 boost::tie(eVec, eVal) = eig_spd(C);
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));
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);
1625 U = eVec * x * transpose(eVec);
1626 Uinv = eVec * xi * transpose(eVec);
1629 return std::make_pair(R, U);
1637 template<
typename T>
1638 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1639 polar_left_logV(Tensor<T>
const & F)
1642 N = F.get_dimension();
1647 boost::tie(X, S, Y) = svd(F);
1650 R = X * transpose(Y);
1653 V = X * S * transpose(X);
1658 for (Index i = 0; i < N; ++i) {
1659 s(i,i) = std::log(s(i,i));
1663 v = X * s * transpose(X);
1665 return boost::make_tuple(V, R, v);
1673 template<
typename T>
1674 boost::tuple<Tensor<T>, Tensor<T>, Tensor<T> >
1675 polar_left_logV_lame(Tensor<T>
const & F)
1678 N = F.get_dimension();
1681 Tensor<T> R(N), V(N), v(N), Vinv(N);
1684 Tensor<T> b = F*transpose(F);
1689 boost::tie(eVec,eVal) = eig_spd_cos(b);
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));
1706 V = eVec*x*transpose(eVec);
1707 Vinv = eVec*xi*transpose(eVec);
1708 v = eVec*lnx*transpose(eVec);
1711 return boost::make_tuple(V,R,v);
1720 template<
typename T>
1722 bch(Tensor<T>
const & x, Tensor<T>
const & y)
1733 (x*x*y - 2.0*x*y*x + x*y*y + y*x*x - 2.0*y*x*y + y*y*x)
1737 (x*x*y*y - 2.0*x*y*x*y + 2.0*y*x*y*x - y*y*x*x);
1745 template<
typename T>
1747 schur_sym(T
const f, T
const g, T
const h)
1753 T t = (h - f) / (2.0 * g);
1756 t = 1.0 / (std::sqrt(1.0 + t * t) + t);
1758 t = -1.0 / (std::sqrt(1.0 + t * t) - t);
1760 c = 1.0 / std::sqrt(1.0 + t * t);
1764 return std::make_pair(c, s);
1772 template<
typename T>
1774 givens(T
const & a, T
const & b)
1780 if (std::abs(b) > std::abs(a)) {
1781 T
const t = - a / b;
1782 s = 1.0 / std::sqrt(1.0 + t * t);
1785 T
const t = - b / a;
1786 c = 1.0 / std::sqrt(1.0 + t * t);
1791 return std::make_pair(c, s);
1801 template<
typename T>
1802 std::pair<Tensor<T>, Tensor<T> >
1803 eig_sym_NxN(Tensor<T>
const & A)
1809 N = A.get_dimension();
1815 off = norm_off_diagonal(D);
1818 tol = machine_epsilon<T>() * norm(A);
1826 while (off > tol && num_iter < max_iter) {
1835 boost::tie(p,q) = arg_max_off_diagonal(D);
1853 boost::tie(c, s) = schur_sym(f, g, h);
1857 givens_left(c, s, p, q, D);
1858 givens_right(c, s, p, q, D);
1860 givens_right(c, s, p, q, V);
1862 off = norm_off_diagonal(D);
1866 if (num_iter == max_iter) {
1867 std::cerr <<
"WARNING: EIG iteration did not converge." << std::endl;
1873 boost::tie(d, P) = sort_permutation(diag(D));
1877 return std::make_pair(V, D);
1885 template<
typename T>
1886 std::pair<Tensor<T>, Tensor<T> >
1887 eig_sym_2x2(Tensor<T>
const & A)
1889 assert(A.get_dimension() == 2);
1892 T
const g = 0.5 * (A(0,1) + A(1,0));
1898 T
const sum = f + h;
1899 T
const dif = std::abs(f - h);
1900 T
const g2 = std::abs(g + g);
1905 const bool swap_diag = std::abs(h) > std::abs(f);
1907 if (swap_diag ==
true) {
1908 std::swap(fhmax, fhmin);
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);
1920 r = g2 * std::sqrt(2.0);
1927 s0 = 0.5 * (sum + copysign(r, sum));
1931 s1 = (fhmax / s0) * fhmin - (g / s0) * g;
1939 D(s0, 0.0, 0.0, s1);
1947 boost::tie(c, s) = schur_sym(f, g, h);
1952 if (swap_diag ==
true) {
1954 std::swap(V(0,0), V(0,1));
1955 std::swap(V(1,0), V(1,1));
1958 return std::make_pair(V, D);
1968 template<
typename T>
1969 std::pair<Tensor<T>, Tensor<T> >
1970 eig_sym(Tensor<T>
const & A)
1973 N = A.get_dimension();
1981 boost::tie(V, D) = eig_sym_NxN(A);
1985 boost::tie(V, D) = eig_sym_2x2(A);
1990 return std::make_pair(V, D);
1998 template<
typename T>
1999 std::pair<Tensor<T>, Tensor<T> >
2000 eig_spd(Tensor<T>
const & A)
2010 template<
typename T>
2011 std::pair<Tensor<T>, Tensor<T> >
2012 eig_spd_cos(Tensor<T>
const & A)
2014 assert(A.get_dimension() == 3);
2036 ii[3][2] = { { 1, 2 }, { 2, 0 }, { 0, 1 } };
2043 trA = (1.0/3.0) * I1(A);
2082 rhs = (J3 / 2.0) * T(std::sqrt(t1 * t1 * t1));
2085 theta = pi / 2.0 * (1.0 - (rhs < 0 ? -1.0 : 1.0));
2087 if (std::abs(rhs) <= 1.0) theta = std::acos(rhs);
2090 thetad3 = theta / 3.0;
2092 if (thetad3 > pi / 6.0) thetad3 += 2.0 * pi / 3.0;
2095 D(2,2) = 2.0 * std::cos(thetad3) * std::sqrt(-J2 / 3.0);
2099 R = Ap - D(2,2) * I;
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);
2121 a(k) = std::sqrt(a(k));
2122 for (
int i(0); i < 3; ++i)
2128 for (
int i(0); i < 3; ++i)
2130 d0 += R(i,k) * R(i,ii[k][0]);
2131 d1 += R(i,k) * R(i,ii[k][1]);
2135 for (
int i(0); i < 3; ++i)
2137 R(i,ii[k][0]) -= d0 * R(i,k);
2138 R(i,ii[k][1]) -= d1 * R(i,k);
2143 for (
int i(0); i < 3; ++i)
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]);
2150 if (std::abs(a(1)) > std::abs(a(0))) p = 1;
2153 a(p) = std::sqrt(a(p));
2156 for (
int i(0); i < 3; ++i)
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);
2166 mag = std::sqrt(V(0,2) * V(0,2) + V(1,2) * V(1,2) + V(2,2) * V(2,2));
2174 rk(R(0,k), R(1,k), R(2,k));
2177 rk2(R(0,k2), R(1,k2), R(2,k2));
2187 rm(0,0) = dot(rk,ak);
2188 rm(0,1) = dot(rk,ak2);
2189 rm(1,1) = dot(rk2,ak2);
2193 b = 0.5 * (rm(0,0) - rm(1,1));
2196 fac = (b < 0 ? -1.0 : 1.0);
2199 arg = b * b + rm(0,1) * rm(0,1);
2202 D(0,0) = rm(1,1) + b;
2204 D(0,0) = rm(1,1) + b - fac * std::sqrt(b * b + rm(0,1) * rm(0,1));
2206 D(1,1) = rm(0,0) + rm(1,1) - D(0,0);
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);
2219 if (a(1) > a(0)) k3 = 1;
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);
2232 mag = std::sqrt(V(0,0) * V(0,0) + V(1,0) * V(1,0) + V(2,0) * V(2,0));
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);
2243 mag = std::sqrt(V(0,1) * V(0,1) + V(1,1) * V(1,1) + V(2,1) * V(2,1));
2249 for (
int i(0); i < 3; ++i)
2253 return std::make_pair(V, D);
2263 template<
typename T>
2264 std::pair<Tensor<T>,
bool >
2265 cholesky(Tensor<T>
const & A)
2271 N = A.get_dimension();
2273 for (Index k = 0; k < N; ++k) {
2276 for (Index j = k + 1; j < N; ++j) {
2284 return std::make_pair(G,
false);
2289 for (Index j = k + 1; j < N; ++j) {
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);
2303 return std::make_pair(G,
true);
2308 #endif // Intrepid_MiniTensor_LinearAlgebra_t_h