19 #include "../general/table.hpp"
26 #if defined(_MSC_VER) && (_MSC_VER < 1800)
28 #define copysign _copysign
47 data =
new double[hw];
49 for (
int i = 0; i < hw; i++)
63 MFEM_ASSERT(s >= 0,
"invalid DenseMatrix size: " << s);
67 data =
new double[capacity]();
77 MFEM_ASSERT(m >= 0 && n >= 0,
78 "invalid DenseMatrix size: " << m <<
" x " << n);
82 data =
new double[capacity]();
91 :
Matrix(mat.width, mat.height)
96 data =
new double[capacity];
98 for (
int i = 0; i <
height; i++)
99 for (
int j = 0; j <
width; j++)
101 (*this)(i,j) = mat(j,i);
112 MFEM_ASSERT(h >= 0 && w >= 0,
113 "invalid DenseMatrix size: " << h <<
" x " << w);
121 if (hw > std::abs(capacity))
128 data =
new double[hw]();
146 for (
int row = 0; row <
height; row++)
152 double *d_col = data;
154 for (
int row = 0; row <
height; row++)
156 y[row] = x_col*d_col[row];
159 for (
int col = 1; col <
width; col++)
162 for (
int row = 0; row <
height; row++)
164 y[row] += x_col*d_col[row];
173 "incompatible dimensions");
175 Mult((
const double *)x, (
double *)y);
181 "incompatible dimensions");
185 for (
int i = 0; i < hw; i++)
187 a += data[i] * m.data[i];
195 double *d_col = data;
196 for (
int col = 0; col <
width; col++)
199 for (
int row = 0; row <
height; row++)
201 y_col += x[row]*d_col[row];
211 "incompatible dimensions");
219 "incompatible dimensions");
221 const double *xp = x;
222 double *d_col = data, *yp = y;
223 for (
int col = 0; col <
width; col++)
225 double x_col = xp[col];
226 for (
int row = 0; row <
height; row++)
228 yp[row] += x_col*d_col[row];
237 "incompatible dimensions");
239 const double *xp = x;
240 double *d_col = data, *yp = y;
241 for (
int col = 0; col <
width; col++)
243 double x_col = a*xp[col];
244 for (
int row = 0; row <
height; row++)
246 yp[row] += x_col*d_col[row];
256 "incompatible dimensions");
258 double *d_col = data;
259 for (
int col = 0; col <
width; col++)
262 for (
int row = 0; row <
height; row++)
264 y_col += x[row]*d_col[row];
275 for (
int i = 0; i <
height; i++)
278 for (
int j = 0; j <
width; j++)
280 Axi += (*this)(i,j) * x[j];
291 double * it_data = data;
292 for (
int j = 0; j <
width; ++j)
293 for (
int i = 0; i <
height; ++i)
295 *(it_data++) *= s(i);
302 double * it_data = data;
303 for (
int j = 0; j <
width; ++j)
304 for (
int i = 0; i <
height; ++i)
306 *(it_data++) /= s(i);
314 double * it_data = data;
315 for (
int j = 0; j <
width; ++j)
318 for (
int i = 0; i <
height; ++i)
329 double * it_data = data;
330 for (
int j = 0; j <
width; ++j)
333 for (
int i = 0; i <
height; ++i)
348 double * ss =
new double[
width];
351 for (
double * end_s = it_s +
width; it_s != end_s; ++it_s)
353 *(it_ss++) = sqrt(*it_s);
356 double * it_data = data;
357 for (
int j = 0; j <
width; ++j)
358 for (
int i = 0; i <
height; ++i)
360 *(it_data++) *= ss[i]*ss[j];
374 double * ss =
new double[
width];
377 for (
double * end_s = it_s +
width; it_s != end_s; ++it_s)
379 *(it_ss++) = 1./sqrt(*it_s);
382 double * it_data = data;
383 for (
int j = 0; j <
width; ++j)
384 for (
int i = 0; i <
height; ++i)
386 *(it_data++) *= ss[i]*ss[j];
397 mfem_error(
"DenseMatrix::Trace() : not a square matrix!");
403 for (
int i = 0; i <
width; i++)
419 "The matrix must be square and sized 1, 2, or 3 to compute the determinate."
420 <<
" Height() = " <<
Height()
421 <<
", Width() = " <<
Width());
429 return data[0] * data[3] - data[1] * data[2];
433 const double *d = data;
435 d[0] * (d[4] * d[8] - d[5] * d[7]) +
436 d[3] * (d[2] * d[7] - d[1] * d[8]) +
437 d[6] * (d[1] * d[5] - d[2] * d[4]);
452 return sqrt(data[0] * data[0] + data[1] * data[1]);
456 return sqrt(data[0] * data[0] + data[1] * data[1] + data[2] * data[2]);
460 const double *d = data;
461 double E = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
462 double G = d[3] * d[3] + d[4] * d[4] + d[5] * d[5];
463 double F = d[0] * d[3] + d[1] * d[4] + d[2] * d[5];
464 return sqrt(E * G - F * F);
472 for (
int j = 0; j <
Width(); j++)
473 for (
int i = 0; i <
Height(); i++)
475 (*this)(i,j) += c * A(i,j);
482 for (
int i = 0; i < s; i++)
492 for (
int i = 0; i < s; i++)
504 for (
int i = 0; i < hw; i++)
515 "incompatible matrix sizes.");
517 for (
int j = 0; j <
width; j++)
518 for (
int i = 0; i <
height; i++)
520 (*this)(i, j) += m(i, j);
528 for (
int j = 0; j <
width; j++)
529 for (
int i = 0; i <
height; i++)
531 (*this)(i, j) -= m(i, j);
540 for (
int i = 0; i < s; i++)
550 for (
int i = 0; i < hw; i++)
556 #ifdef MFEM_USE_LAPACK
558 dgetrf_(
int *,
int *,
double *,
int *,
int *,
int *);
560 dgetrs_(
char *,
int *,
int *,
double *,
int *,
int *,
double *,
int *,
int *);
562 dgetri_(
int *N,
double *A,
int *LDA,
int *IPIV,
double *WORK,
563 int *LWORK,
int *INFO);
575 #ifdef MFEM_USE_LAPACK
576 int *ipiv =
new int[
width];
585 mfem_error(
"DenseMatrix::Invert() : Error in DGETRF");
591 work =
new double[lwork];
597 mfem_error(
"DenseMatrix::Invert() : Error in DGETRI");
603 int c, i, j, n =
Width();
607 for (c = 0; c < n; c++)
609 a = fabs((*
this)(c, c));
611 for (j = c + 1; j < n; j++)
613 b = fabs((*
this)(j, c));
622 mfem_error(
"DenseMatrix::Invert() : singular matrix");
625 for (j = 0; j < n; j++)
627 Swap<double>((*this)(c, j), (*
this)(i, j));
630 a = (*this)(c, c) = 1.0 / (*
this)(c, c);
631 for (j = 0; j < c; j++)
635 for (j++; j < n; j++)
639 for (i = 0; i < c; i++)
641 (*this)(i, c) = a * (b = -(*
this)(i, c));
642 for (j = 0; j < c; j++)
644 (*this)(i, j) += b * (*
this)(c, j);
646 for (j++; j < n; j++)
648 (*this)(i, j) += b * (*
this)(c, j);
651 for (i++; i < n; i++)
653 (*this)(i, c) = a * (b = -(*
this)(i, c));
654 for (j = 0; j < c; j++)
656 (*this)(i, j) += b * (*
this)(c, j);
658 for (j++; j < n; j++)
660 (*this)(i, j) += b * (*
this)(c, j);
665 for (c = n - 1; c >= 0; c--)
668 for (i = 0; i < n; i++)
670 Swap<double>((*this)(i, c), (*
this)(i, j));
678 for (
int j = 0; j <
Width(); j++)
681 for (
int i = 0; i <
Height(); i++)
683 v[j] += (*this)(i,j)*(*
this)(i,j);
692 const double *d = data;
693 double norm = 0.0, abs_entry;
695 for (
int i = 0; i < hw; i++)
697 abs_entry = fabs(d[i]);
698 if (norm < abs_entry)
710 double max_norm = 0.0, entry, fnorm2;
712 for (i = 0; i < hw; i++)
714 entry = fabs(data[i]);
715 if (entry > max_norm)
727 for (i = 0; i < hw; i++)
729 entry = data[i] / max_norm;
730 fnorm2 += entry * entry;
733 return max_norm * sqrt(fnorm2);
736 #ifdef MFEM_USE_LAPACK
738 dsyevr_(
char *JOBZ,
char *RANGE,
char *UPLO,
int *N,
double *A,
int *LDA,
739 double *VL,
double *VU,
int *IL,
int *IU,
double *ABSTOL,
int *M,
740 double *W,
double *Z,
int *LDZ,
int *ISUPPZ,
double *WORK,
int *LWORK,
741 int *IWORK,
int *LIWORK,
int *INFO);
743 dsyev_(
char *JOBZ,
char *UPLO,
int *N,
double *A,
int *LDA,
double *W,
744 double *WORK,
int *LWORK,
int *INFO);
746 dgesvd_(
char *JOBU,
char *JOBVT,
int *M,
int *N,
double *A,
int *LDA,
747 double *S,
double *U,
int *LDU,
double *VT,
int *LDVT,
double *WORK,
748 int *LWORK,
int *INFO);
754 #ifdef MFEM_USE_LAPACK
762 double *A =
new double[N*N];
773 int *ISUPPZ =
new int[2*N];
792 double *data = a.
Data();
794 for (
int i = 0; i < hw; i++)
799 dsyevr_( &JOBZ, &RANGE, &UPLO, &N, A, &LDA, &VL, &VU, &IL, &IU,
800 &ABSTOL, &M, W, Z, &LDZ, ISUPPZ, &QWORK, &LWORK,
801 &QIWORK, &LIWORK, &INFO );
806 WORK =
new double[LWORK];
807 IWORK =
new int[LIWORK];
809 dsyevr_( &JOBZ, &RANGE, &UPLO, &N, A, &LDA, &VL, &VU, &IL, &IU,
810 &ABSTOL, &M, W, Z, &LDZ, ISUPPZ, WORK, &LWORK,
811 IWORK, &LIWORK, &INFO );
815 cerr <<
"dsyevr_Eigensystem(...): DSYEVR error code: "
823 cerr <<
"dsyevr_Eigensystem(...):\n"
824 <<
" DSYEVR did not find all eigenvalues "
825 << M <<
"/" << N << endl;
828 for (IL = 0; IL < N; IL++)
831 mfem_error(
"dsyevr_Eigensystem(...): !finite value in W");
833 for (IL = 0; IL < N*N; IL++)
836 mfem_error(
"dsyevr_Eigensystem(...): !finite value in Z");
839 for (IL = 0; IL < N; IL++)
840 for (IU = 0; IU <= IL; IU++)
843 for (M = 0; M < N; M++)
845 VL += Z[M+IL*N] * Z[M+IU*N];
861 cerr <<
"dsyevr_Eigensystem(...):"
862 <<
" Z^t Z - I deviation = " << VU
863 <<
"\n W[max] = " << W[N-1] <<
", W[min] = "
864 << W[0] <<
", N = " << N << endl;
870 cerr <<
"dsyevr_Eigensystem(...):"
871 <<
" Z^t Z - I deviation = " << VU
872 <<
"\n W[max] = " << W[N-1] <<
", W[min] = "
873 << W[0] <<
", N = " << N << endl;
877 mfem_error(
"dsyevr_Eigensystem(...): ERROR: ...");
880 for (IL = 0; IL < N; IL++)
881 for (IU = 0; IU < N; IU++)
884 for (M = 0; M < N; M++)
886 VL += Z[IL+M*N] * W[M] * Z[IU+M*N];
888 VL = fabs(VL-data[IL+N*IU]);
896 cerr <<
"dsyevr_Eigensystem(...):"
897 <<
" max matrix deviation = " << VU
898 <<
"\n W[max] = " << W[N-1] <<
", W[min] = "
899 << W[0] <<
", N = " << N << endl;
903 mfem_error(
"dsyevr_Eigensystem(...): ERROR: ...");
918 #ifdef MFEM_USE_LAPACK
946 double *data = a.
Data();
947 for (
int i = 0; i < hw; i++)
952 dsyev_(&JOBZ, &UPLO, &N, A, &LDA, W, &QWORK, &LWORK, &INFO);
955 WORK =
new double[LWORK];
957 dsyev_(&JOBZ, &UPLO, &N, A, &LDA, W, WORK, &LWORK, &INFO);
961 cerr <<
"dsyev_Eigensystem: DSYEV error code: " << INFO << endl;
966 if (evect == NULL) {
delete [] A; }
971 void DenseMatrix::Eigensystem(Vector &ev, DenseMatrix *evect)
973 #ifdef MFEM_USE_LAPACK
988 #ifdef MFEM_USE_LAPACK
994 double *a = copy_of_this.data;
1004 dgesvd_(&jobu, &jobvt, &m, &n, a, &m,
1005 s, u, &m, vt, &n, &qwork, &lwork, &info);
1007 lwork = (int) qwork;
1008 work =
new double[lwork];
1010 dgesvd_(&jobu, &jobvt, &m, &n, a, &m,
1011 s, u, &m, vt, &n, work, &lwork, &info);
1016 cerr <<
"DenseMatrix::SingularValues : info = " << info << endl;
1031 for (
int i=0; i < sv.
Size(); ++i)
1040 static const double sqrt_1_eps = sqrt(1./numeric_limits<double>::epsilon());
1047 double t, zeta = (d2 - d1)/(2*d12);
1048 if (fabs(zeta) < sqrt_1_eps)
1050 t = d12*copysign(1./(fabs(zeta) + sqrt(1. + zeta*zeta)), zeta);
1054 t = d12*copysign(0.5/fabs(zeta), zeta);
1062 double &c,
double &s)
1072 double t, zeta = (d2 - d1)/(2*d12);
1073 if (fabs(zeta) < sqrt_1_eps)
1075 t = copysign(1./(fabs(zeta) + sqrt(1. + zeta*zeta)), zeta);
1079 t = copysign(0.5/fabs(zeta), zeta);
1082 c = sqrt(1./(1. + t*t));
1091 const double &x1,
const double &x2,
const double &x3,
1092 double &n1,
double &n2,
double &n3)
1100 t = sqrt(1./(t + r*r));
1101 n1 = copysign(t, x1);
1108 double &n1,
double &n2,
double &n3)
1112 if (fabs(x1) >= fabs(x2))
1114 if (fabs(x1) >= fabs(x3))
1127 else if (fabs(x2) >= fabs(x3))
1137 double &d1,
double &d12,
double &d21,
double &d2)
1148 double n1 = fabs(d1) + fabs(d21);
1149 double n2 = fabs(d2) + fabs(d12);
1151 bool swap_columns = (n2 > n1);
1163 if (fabs(d1) > fabs(d21))
1171 if (fabs(d1) < fabs(d21))
1183 if (fabs(d12) > fabs(d2))
1196 if (fabs(d12) < fabs(d2))
1209 n1 = hypot(d1, d21);
1215 mu = copysign(n1, d1);
1216 n1 = -d21*(d21/(d1 + mu));
1220 if (fabs(n1) <= fabs(d21))
1224 mu = (2./(1. + n1*n1))*(n1*d12 + d2);
1232 mu = (2./(1. + n2*n2))*(d12 + n2*d2);
1254 n2 = 1./(1. + fabs(mu));
1256 if (fabs(d1) <= n2*fabs(d2))
1278 double &d1,
double &d2,
double &d3,
double &c12,
double &c13,
double &c23,
1279 double &c21,
double &c31,
double &c32)
1282 double mu, n1, n2, n3, s1, s2, s3;
1284 s1 = hypot(c21, c31);
1291 mu = copysign(n1, d1);
1292 n1 = -s1*(s1/(d1 + mu));
1297 if (fabs(n1) >= fabs(c21))
1299 if (fabs(n1) >= fabs(c31))
1304 mu = 2./(1. + s2*s2 + s3*s3);
1305 n2 = mu*(c12 + s2*d2 + s3*c32);
1306 n3 = mu*(c13 + s2*c23 + s3*d3);
1316 else if (fabs(c21) >= fabs(c31))
1321 mu = 2./(1. + s1*s1 + s3*s3);
1322 n2 = mu*(s1*c12 + d2 + s3*c32);
1323 n3 = mu*(s1*c13 + c23 + s3*d3);
1335 mu = 2./(1. + s1*s1 + s2*s2);
1336 n2 = mu*(s1*c12 + s2*d2 + c32);
1337 n3 = mu*(s1*c13 + s2*c23 + d3);
1372 d1 = -(c12*d2 + c13*d3)/d1;
1383 const double &d12,
const double &d13,
const double &d23,
1384 double &d1,
double &d2,
double &d3)
1397 double c12 = d12, c13 = d13, c23 = d23;
1398 double c21, c31, c32;
1402 c32 = fabs(d1) + fabs(c12) + fabs(c13);
1403 c31 = fabs(d2) + fabs(c12) + fabs(c23);
1404 c21 = fabs(d3) + fabs(c13) + fabs(c23);
1409 col = (c32 >= c31) ? 1 : 2;
1413 col = (c31 >= c21) ? 2 : 3;
1445 if (fabs(d1) <= fabs(c13))
1447 row = (fabs(d1) <= fabs(c12)) ? 1 : 2;
1451 row = (fabs(c12) <= fabs(c13)) ? 2 : 3;
1456 if (fabs(d1) >= fabs(c13))
1458 row = (fabs(d1) >= fabs(c12)) ? 1 : 2;
1462 row = (fabs(c12) >= fabs(c13)) ? 2 : 3;
1512 double &d1,
double &d2,
double &d3,
double &d12,
double &d13,
double &d23,
1513 double &z1,
double &z2,
double &z3,
double &v1,
double &v2,
double &v3,
1533 double s, w1, w2, w3;
1539 if (fabs(z1) <= fabs(z3))
1541 k = (fabs(z1) <= fabs(z2)) ? 1 : 2;
1545 k = (fabs(z2) <= fabs(z3)) ? 2 : 3;
1551 if (fabs(z1) >= fabs(z3))
1553 k = (fabs(z1) >= fabs(z2)) ? 1 : 2;
1557 k = (fabs(z2) >= fabs(z3)) ? 2 : 3;
1584 g = copysign(1., z1);
1585 v1 = -s*(s/(z1 + g));
1588 if (fabs(z2) > g) { g = fabs(z2); }
1589 if (fabs(z3) > g) { g = fabs(z3); }
1593 g = 2./(v1*v1 + v2*v2 + v3*v3);
1598 w1 = g*( d1*v1 + d12*v2 + d13*v3);
1599 w2 = g*(d12*v1 + d2*v2 + d23*v3);
1600 w3 = g*(d13*v1 + d23*v2 + d3*v3);
1602 s = (g/2)*(v1*w1 + v2*w2 + v3*w3);
1609 d23 -= v2*w3 + v3*w2;
1614 s = d12 - v1*w2 - v2*w1;
1615 s = d13 - v1*w3 - v3*w1;
1637 mult = frexp(d_max, &d_exp);
1638 if (d_exp == numeric_limits<double>::max_exponent)
1640 mult *= numeric_limits<double>::radix;
1655 "The matrix must be square and sized 1, 2, or 3 to compute the singular values."
1656 <<
" Height() = " <<
Height()
1657 <<
", Width() = " <<
Width());
1660 const double *d = data;
1668 double d0, d1, d2, d3;
1675 double d_max = fabs(d0);
1676 if (d_max < fabs(d1)) { d_max = fabs(d1); }
1677 if (d_max < fabs(d2)) { d_max = fabs(d2); }
1678 if (d_max < fabs(d3)) { d_max = fabs(d3); }
1692 double t = 0.5*((d0+d2)*(d0-d2)+(d1-d3)*(d1+d3));
1694 double s = d0*d2 + d1*d3;
1695 s = sqrt(0.5*(d0*d0 + d1*d1 + d2*d2 + d3*d3) + sqrt(t*t + s*s));
1700 t = fabs(d0*d3 - d1*d2) / s;
1717 double d0, d1, d2, d3, d4, d5, d6, d7, d8;
1718 d0 = d[0]; d3 = d[3]; d6 = d[6];
1719 d1 = d[1]; d4 = d[4]; d7 = d[7];
1720 d2 = d[2]; d5 = d[5]; d8 = d[8];
1723 double d_max = fabs(d0);
1724 if (d_max < fabs(d1)) { d_max = fabs(d1); }
1725 if (d_max < fabs(d2)) { d_max = fabs(d2); }
1726 if (d_max < fabs(d3)) { d_max = fabs(d3); }
1727 if (d_max < fabs(d4)) { d_max = fabs(d4); }
1728 if (d_max < fabs(d5)) { d_max = fabs(d5); }
1729 if (d_max < fabs(d6)) { d_max = fabs(d6); }
1730 if (d_max < fabs(d7)) { d_max = fabs(d7); }
1731 if (d_max < fabs(d8)) { d_max = fabs(d8); }
1736 d0 /= mult; d1 /= mult; d2 /= mult;
1737 d3 /= mult; d4 /= mult; d5 /= mult;
1738 d6 /= mult; d7 /= mult; d8 /= mult;
1740 double b11 = d0*d0 + d1*d1 + d2*d2;
1741 double b12 = d0*d3 + d1*d4 + d2*d5;
1742 double b13 = d0*d6 + d1*d7 + d2*d8;
1743 double b22 = d3*d3 + d4*d4 + d5*d5;
1744 double b23 = d3*d6 + d4*d7 + d5*d8;
1745 double b33 = d6*d6 + d7*d7 + d8*d8;
1764 double aa = (b11 + b22 + b33)/3;
1770 double b11_b22 = ((d0-d3)*(d0+d3)+(d1-d4)*(d1+d4)+(d2-d5)*(d2+d5));
1771 double b22_b33 = ((d3-d6)*(d3+d6)+(d4-d7)*(d4+d7)+(d5-d8)*(d5+d8));
1772 double b33_b11 = ((d6-d0)*(d6+d0)+(d7-d1)*(d7+d1)+(d8-d2)*(d8+d2));
1773 c1 = (b11_b22 - b33_b11)/3;
1774 c2 = (b22_b33 - b11_b22)/3;
1775 c3 = (b33_b11 - b22_b33)/3;
1778 Q = (2*(b12*b12 + b13*b13 + b23*b23) + c1*c1 + c2*c2 + c3*c3)/6;
1779 R = (c1*(b23*b23 - c2*c3)+ b12*(b12*c3 - 2*b13*b23) +b13*b13*c2)/2;
1812 double sqrtQ = sqrt(Q);
1813 double sqrtQ3 = Q*sqrtQ;
1818 if (fabs(R) >= sqrtQ3)
1840 aa -= 2*sqrtQ*cos(acos(R)/3);
1844 aa -= 2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
1848 aa -= 2*sqrtQ*cos((acos(R) - 2.0*M_PI)/3);
1855 r = -2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
1864 r = -2*sqrtQ*cos(acos(R)/3);
1911 double v1, v2, v3, g;
1912 Reduce3S(mode, b11, b22, b33, b12, b13, b23,
1913 c1, c2, c3, v1, v2, v3, g);
1924 aa = std::min(std::min(b11, b22), b33);
1930 aa = (b22 <= b33) ? b22 : std::max(b11, b33);
1934 aa = (b11 <= b33) ? b11 : std::max(b33, b22);
1939 aa = std::max(std::max(b11, b22), b33);
1945 return sqrt(fabs(aa))*mult;
1959 const double *d = data;
1999 double d_max = fabs(d11);
2000 if (d_max < fabs(d22)) { d_max = fabs(d22); }
2001 if (d_max < fabs(d33)) { d_max = fabs(d33); }
2002 if (d_max < fabs(d12)) { d_max = fabs(d12); }
2003 if (d_max < fabs(d13)) { d_max = fabs(d13); }
2004 if (d_max < fabs(d23)) { d_max = fabs(d23); }
2009 d11 /= mult; d22 /= mult; d33 /= mult;
2010 d12 /= mult; d13 /= mult; d23 /= mult;
2012 double aa = (d11 + d22 + d33)/3;
2013 double c1 = d11 - aa;
2014 double c2 = d22 - aa;
2015 double c3 = d33 - aa;
2019 Q = (2*(d12*d12 + d13*d13 + d23*d23) + c1*c1 + c2*c2 + c3*c3)/6;
2020 R = (c1*(d23*d23 - c2*c3)+ d12*(d12*c3 - 2*d13*d23) + d13*d13*c2)/2;
2024 lambda[0] = lambda[1] = lambda[2] = aa;
2025 vec[0] = 1.; vec[3] = 0.; vec[6] = 0.;
2026 vec[1] = 0.; vec[4] = 1.; vec[7] = 0.;
2027 vec[2] = 0.; vec[5] = 0.; vec[8] = 1.;
2031 double sqrtQ = sqrt(Q);
2032 double sqrtQ3 = Q*sqrtQ;
2036 if (fabs(R) >= sqrtQ3)
2055 r = -2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
2059 r = -2*sqrtQ*cos(acos(R)/3);
2087 lambda[0] = lambda[1] = lambda[2] = aa;
2088 vec[0] = 1.; vec[3] = 0.; vec[6] = 0.;
2089 vec[1] = 0.; vec[4] = 1.; vec[7] = 0.;
2090 vec[2] = 0.; vec[5] = 0.; vec[8] = 1.;
2104 double v1, v2, v3, g;
2105 int k =
Reduce3S(mode, d11, d22, d33, d12, d13, d23,
2106 c1, c2, c3, v1, v2, v3, g);
2117 double *vec_1, *vec_2, *vec_3;
2122 lambda[0] = d11; vec_1 = vec;
2123 lambda[1] = d22; vec_2 = vec + 3;
2124 lambda[2] = d33; vec_3 = vec + 6;
2126 else if (d11 <= d33)
2128 lambda[0] = d11; vec_1 = vec;
2129 lambda[1] = d33; vec_3 = vec + 3;
2130 lambda[2] = d22; vec_2 = vec + 6;
2134 lambda[0] = d33; vec_3 = vec;
2135 lambda[1] = d11; vec_1 = vec + 3;
2136 lambda[2] = d22; vec_2 = vec + 6;
2143 lambda[0] = d22; vec_2 = vec;
2144 lambda[1] = d11; vec_1 = vec + 3;
2145 lambda[2] = d33; vec_3 = vec + 6;
2147 else if (d22 <= d33)
2149 lambda[0] = d22; vec_2 = vec;
2150 lambda[1] = d33; vec_3 = vec + 3;
2151 lambda[2] = d11; vec_1 = vec + 6;
2155 lambda[0] = d33; vec_3 = vec;
2156 lambda[1] = d22; vec_2 = vec + 3;
2157 lambda[2] = d11; vec_1 = vec + 6;
2164 d22 = g*(v2*c - v3*s);
2165 d33 = g*(v2*s + v3*c);
2166 vec_2[0] = - v1*d22; vec_3[0] = - v1*d33;
2167 vec_2[1] = c - v2*d22; vec_3[1] = s - v2*d33;
2168 vec_2[2] = -s - v3*d22; vec_3[2] = c - v3*d33;
2172 Swap(vec_2[0], vec_2[1]);
2173 Swap(vec_3[0], vec_3[1]);
2177 Swap(vec_2[0], vec_2[2]);
2178 Swap(vec_3[0], vec_3[2]);
2195 double* rp = data + r;
2198 for (
int i = 0; i < n; i++)
2210 double *cp = data + c * m;
2213 for (
int i = 0; i < m; i++)
2227 for (
int i = 0; i <
height; ++i)
2229 d(i) = (*this)(i,i);
2243 for (
int j = 0; j <
width; ++j)
2244 for (
int i = 0; i <
height; ++i)
2246 l(i) += fabs((*
this)(i,j));
2253 for (
int i = 0; i <
height; i++)
2256 for (
int j = 0; j <
width; j++)
2269 for (i = 0; i < N; i++)
2273 for (i = 0; i < n; i++)
2284 for (i = 0; i < N; i++)
2288 for (i = 0; i < n; i++)
2290 data[i*(n+1)] = diag[i];
2301 for (i = 0; i <
Height(); i++)
2302 for (j = i+1; j <
Width(); j++)
2305 (*this)(i,j) = (*
this)(j,i);
2320 for (
int i = 0; i <
Height(); i++)
2321 for (
int j = 0; j <
Width(); j++)
2323 (*this)(i,j) = A(j,i);
2332 mfem_error(
"DenseMatrix::Symmetrize() : not a square matrix!");
2336 for (
int i = 0; i <
Height(); i++)
2337 for (
int j = 0; j < i; j++)
2339 double a = 0.5 * ((*this)(i,j) + (*
this)(j,i));
2340 (*this)(j,i) = (*
this)(i,j) = a;
2346 for (
int i = 0; i <
Height(); i++)
2349 for (
int j = 0; j <
Width(); j++)
2352 (*this)(i, j) = 0.0;
2372 for (
int i = 0; i < n; i++)
2375 double x = (*this)(i,0);
2376 double y = (*this)(i,1);
2389 for (
int i = 0; i < n; i++)
2392 double x = (*this)(i,0);
2393 double y = (*this)(i,1);
2394 double z = (*this)(i,2);
2430 double *ddata = div.
GetData();
2432 for (
int i = 0; i < n; i++)
2442 for (
int j = 0; j <
Width(); j++)
2443 for (
int i = row1; i <= row2; i++)
2445 (*this)(i-row1,j) = A(i,j);
2453 for (
int j = col1; j <= col2; j++)
2454 for (
int i = 0; i <
Height(); i++)
2456 (*this)(i,j-col1) = A(i,j);
2466 for (j = 0; j < n; j++)
2467 for (i = 0; i < m; i++)
2469 (*this)(i,j) = A(Aro+i,Aco+j);
2478 for (j = 0; j < A.
Width(); j++)
2479 for (i = 0; i < A.
Height(); i++)
2481 (*this)(row_offset+i,col_offset+j) = *(v++);
2490 for (i = 0; i < A.
Width(); i++)
2491 for (j = 0; j < A.
Height(); j++)
2493 (*this)(row_offset+i,col_offset+j) = *(v++);
2498 int row_offset,
int col_offset)
2502 MFEM_VERIFY(row_offset+m <= this->
Height() && col_offset+n <= this->
Width(),
2503 "this DenseMatrix is too small to accomodate the submatrix. "
2504 <<
"row_offset = " << row_offset
2506 <<
", this->Height() = " << this->
Height()
2507 <<
", col_offset = " << col_offset
2509 <<
", this->Width() = " << this->
Width()
2511 MFEM_VERIFY(Aro+m <= A.
Height() && Aco+n <= A.
Width(),
2512 "The A DenseMatrix is too small to accomodate the submatrix. "
2515 <<
", A.Height() = " << A.
Height()
2516 <<
", Aco = " << Aco
2518 <<
", A.Width() = " << A.
Width()
2521 for (j = 0; j < n; j++)
2522 for (i = 0; i < m; i++)
2524 (*this)(row_offset+i,col_offset+j) = A(Aro+i,Aco+j);
2532 for (i = 0; i < n; i++)
2533 for (j = i+1; j < n; j++)
2534 (*
this)(row_offset+i,col_offset+j) =
2535 (*
this)(row_offset+j,col_offset+i) = 0.0;
2537 for (i = 0; i < n; i++)
2539 (*this)(row_offset+i,col_offset+i) = c;
2548 for (i = 0; i < n; i++)
2549 for (j = i+1; j < n; j++)
2550 (*
this)(row_offset+i,col_offset+j) =
2551 (*
this)(row_offset+j,col_offset+i) = 0.0;
2553 for (i = 0; i < n; i++)
2555 (*this)(row_offset+i,col_offset+i) = diag[i];
2569 if (co+aw >
Width() || ro+ah > h)
2575 p = data + ro + co * h;
2578 for (
int c = 0; c < aw; c++)
2580 for (
int r = 0; r < ah; r++)
2599 if (co+aw >
Width() || ro+ah > h)
2605 p = data + ro + co * h;
2608 for (
int c = 0; c < aw; c++)
2610 for (
int r = 0; r < ah; r++)
2622 double *vdata = v.
GetData() + offset;
2624 for (i = 0; i < n; i++)
2626 vdata[i] += data[i];
2633 const double *vdata = v.
GetData() + offset;
2635 for (i = 0; i < n; i++)
2648 mfem_error(
"DenseMatrix::AdjustDofDirection(...)");
2653 for (
int i = 0; i < n-1; i++)
2655 int s = (dof[i] < 0) ? (-1) : (1);
2656 for (
int j = i+1; j < n; j++)
2658 int t = (dof[j] < 0) ? (-s) : (s);
2661 (*this)(i,j) = -(*
this)(i,j);
2662 (*this)(j,i) = -(*
this)(j,i);
2670 for (
int j = 0; j <
Width(); j++)
2672 (*this)(row, j) = value;
2678 for (
int i = 0; i <
Height(); i++)
2680 (*this)(i, col) = value;
2686 for (
int j = 0; j <
Width(); j++)
2688 (*this)(r, j) = row[j];
2694 for (
int i = 0; i <
Height(); i++)
2696 (*this)(i, c) = col[i];
2702 for (
int col = 0; col <
Width(); col++)
2704 for (
int row = 0; row <
Height(); row++)
2706 if (std::abs(
operator()(row,col)) <= eps)
2717 ios::fmtflags old_flags = out.flags();
2719 out << setiosflags(ios::scientific | ios::showpos);
2720 for (
int i = 0; i <
height; i++)
2722 out <<
"[row " << i <<
"]\n";
2723 for (
int j = 0; j <
width; j++)
2725 out << (*this)(i,j);
2726 if (j+1 == width || (j+1) % width_ == 0)
2737 out.flags(old_flags);
2743 ios::fmtflags old_flags = out.flags();
2745 out << setiosflags(ios::scientific | ios::showpos);
2746 for (
int i = 0; i <
height; i++)
2748 for (
int j = 0; j <
width; j++)
2750 out << (*this)(i,j);
2756 out.flags(old_flags);
2762 ios::fmtflags old_flags = out.flags();
2764 out << setiosflags(ios::scientific | ios::showpos);
2765 for (
int j = 0; j <
width; j++)
2767 out <<
"[col " << j <<
"]\n";
2768 for (
int i = 0; i <
height; i++)
2770 out << (*this)(i,j);
2771 if (i+1 == height || (i+1) % width_ == 0)
2782 out.flags(old_flags);
2791 for (
int i = 0; i <
width; i++)
2795 cout <<
"size = " << width <<
", i_max = " << C.
MaxMaxNorm()
2796 <<
", cond_F = " <<
FNorm()*copy.FNorm() << endl;
2812 for (
int j = 0; j < C.
Width(); j++)
2813 for (
int i = 0; i < C.
Height(); i++)
2815 C(i,j) = A(i,j) + alpha * B(i,j);
2822 for (
int j = 0; j < C.
Width(); j++)
2823 for (
int i = 0; i < C.
Height(); i++)
2825 C(i,j) = alpha * A(i,j) + beta * B(i,j);
2830 #ifdef MFEM_USE_LAPACK
2832 dgemm_(
char *,
char *,
int *,
int *,
int *,
double *,
double *,
2833 int *,
double *,
int *,
double *,
double *,
int *);
2839 b.
Width() == c.
Height(),
"incompatible dimensions");
2841 #ifdef MFEM_USE_LAPACK
2842 static char transa =
'N', transb =
'N';
2843 static double alpha = 1.0, beta = 0.0;
2846 dgemm_(&transa, &transb, &m, &n, &k, &alpha, b.
Data(), &m,
2847 c.
Data(), &k, &beta, a.
Data(), &m);
2849 const int ah = a.
Height();
2850 const int aw = a.
Width();
2851 const int bw = b.
Width();
2852 double *ad = a.
Data();
2853 const double *bd = b.
Data();
2854 const double *cd = c.
Data();
2855 for (
int i = 0; i < ah*aw; i++)
2859 for (
int j = 0; j < aw; j++)
2861 for (
int k = 0; k < bw; k++)
2863 for (
int i = 0; i < ah; i++)
2865 ad[i+j*ah] += bd[i+k*ah] * cd[k+j*bw];
2875 b.
Width() == c.
Height(),
"incompatible dimensions");
2877 #ifdef MFEM_USE_LAPACK
2878 static char transa =
'N', transb =
'N';
2879 static double alpha = 1.0, beta = 1.0;
2882 dgemm_(&transa, &transb, &m, &n, &k, &alpha, b.
Data(), &m,
2883 c.
Data(), &k, &beta, a.
Data(), &m);
2885 const int ah = a.
Height();
2886 const int aw = a.
Width();
2887 const int bw = b.
Width();
2888 double *ad = a.
Data();
2889 const double *bd = b.
Data();
2890 const double *cd = c.
Data();
2891 for (
int j = 0; j < aw; j++)
2893 for (
int k = 0; k < bw; k++)
2895 for (
int i = 0; i < ah; i++)
2897 ad[i+j*ah] += bd[i+k*ah] * cd[k+j*bw];
2919 const double *d = a.
Data();
2920 double *ad = adja.
Data();
2935 e = d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
2936 g = d[3]*d[3] + d[4]*d[4] + d[5]*d[5];
2937 f = d[0]*d[3] + d[1]*d[4] + d[2]*d[5];
2939 ad[0] = d[0]*g - d[3]*f;
2940 ad[1] = d[3]*e - d[0]*f;
2941 ad[2] = d[1]*g - d[4]*f;
2942 ad[3] = d[4]*e - d[1]*f;
2943 ad[4] = d[2]*g - d[5]*f;
2944 ad[5] = d[5]*e - d[2]*f;
2953 else if (a.
Width() == 2)
2956 adja(0,1) = -a(0,1);
2957 adja(1,0) = -a(1,0);
2962 adja(0,0) = a(1,1)*a(2,2)-a(1,2)*a(2,1);
2963 adja(0,1) = a(0,2)*a(2,1)-a(0,1)*a(2,2);
2964 adja(0,2) = a(0,1)*a(1,2)-a(0,2)*a(1,1);
2966 adja(1,0) = a(1,2)*a(2,0)-a(1,0)*a(2,2);
2967 adja(1,1) = a(0,0)*a(2,2)-a(0,2)*a(2,0);
2968 adja(1,2) = a(0,2)*a(1,0)-a(0,0)*a(1,2);
2970 adja(2,0) = a(1,0)*a(2,1)-a(1,1)*a(2,0);
2971 adja(2,1) = a(0,1)*a(2,0)-a(0,0)*a(2,1);
2972 adja(2,2) = a(0,0)*a(1,1)-a(0,1)*a(1,0);
2989 else if (a.
Width() == 2)
2991 adjat(0,0) = a(1,1);
2992 adjat(1,0) = -a(0,1);
2993 adjat(0,1) = -a(1,0);
2994 adjat(1,1) = a(0,0);
2998 adjat(0,0) = a(1,1)*a(2,2)-a(1,2)*a(2,1);
2999 adjat(1,0) = a(0,2)*a(2,1)-a(0,1)*a(2,2);
3000 adjat(2,0) = a(0,1)*a(1,2)-a(0,2)*a(1,1);
3002 adjat(0,1) = a(1,2)*a(2,0)-a(1,0)*a(2,2);
3003 adjat(1,1) = a(0,0)*a(2,2)-a(0,2)*a(2,0);
3004 adjat(2,1) = a(0,2)*a(1,0)-a(0,0)*a(1,2);
3006 adjat(0,2) = a(1,0)*a(2,1)-a(1,1)*a(2,0);
3007 adjat(1,2) = a(0,1)*a(2,0)-a(0,0)*a(2,1);
3008 adjat(2,2) = a(0,0)*a(1,1)-a(0,1)*a(1,0);
3015 MFEM_ASSERT(inva.
Height() == a.
Width(),
"incorrect dimensions");
3016 MFEM_ASSERT(inva.
Width() == a.
Height(),
"incorrect dimensions");
3022 const double *d = a.
Data();
3023 double *
id = inva.
Data();
3026 t = 1.0 / (d[0]*d[0] + d[1]*d[1]);
3034 t = 1.0 / (d[0]*d[0] + d[1]*d[1] + d[2]*d[2]);
3042 e = d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
3043 g = d[3]*d[3] + d[4]*d[4] + d[5]*d[5];
3044 f = d[0]*d[3] + d[1]*d[4] + d[2]*d[5];
3045 t = 1.0 / (e*g - f*f);
3046 e *= t; g *= t; f *= t;
3048 id[0] = d[0]*g - d[3]*f;
3049 id[1] = d[3]*e - d[0]*f;
3050 id[2] = d[1]*g - d[4]*f;
3051 id[3] = d[4]*e - d[1]*f;
3052 id[4] = d[2]*g - d[5]*f;
3053 id[5] = d[5]*e - d[2]*f;
3061 MFEM_ASSERT(std::abs(t) > 1.0e-14 * pow(a.
FNorm()/a.
Width(), a.
Width()),
3062 "singular matrix!");
3074 inva(0,0) = a(1,1) * t ;
3075 inva(0,1) = -a(0,1) * t ;
3076 inva(1,0) = -a(1,0) * t ;
3077 inva(1,1) = a(0,0) * t ;
3080 inva(0,0) = (a(1,1)*a(2,2)-a(1,2)*a(2,1))*t;
3081 inva(0,1) = (a(0,2)*a(2,1)-a(0,1)*a(2,2))*t;
3082 inva(0,2) = (a(0,1)*a(1,2)-a(0,2)*a(1,1))*t;
3084 inva(1,0) = (a(1,2)*a(2,0)-a(1,0)*a(2,2))*t;
3085 inva(1,1) = (a(0,0)*a(2,2)-a(0,2)*a(2,0))*t;
3086 inva(1,2) = (a(0,2)*a(1,0)-a(0,0)*a(1,2))*t;
3088 inva(2,0) = (a(1,0)*a(2,1)-a(1,1)*a(2,0))*t;
3089 inva(2,1) = (a(0,1)*a(2,0)-a(0,0)*a(2,1))*t;
3090 inva(2,2) = (a(0,0)*a(1,1)-a(0,1)*a(1,0))*t;
3105 double t = 1. / a.
Det() ;
3110 inva(0,0) = 1.0 / a(0,0);
3113 inva(0,0) = a(1,1) * t ;
3114 inva(1,0) = -a(0,1) * t ;
3115 inva(0,1) = -a(1,0) * t ;
3116 inva(1,1) = a(0,0) * t ;
3119 inva(0,0) = (a(1,1)*a(2,2)-a(1,2)*a(2,1))*t;
3120 inva(1,0) = (a(0,2)*a(2,1)-a(0,1)*a(2,2))*t;
3121 inva(2,0) = (a(0,1)*a(1,2)-a(0,2)*a(1,1))*t;
3123 inva(0,1) = (a(1,2)*a(2,0)-a(1,0)*a(2,2))*t;
3124 inva(1,1) = (a(0,0)*a(2,2)-a(0,2)*a(2,0))*t;
3125 inva(2,1) = (a(0,2)*a(1,0)-a(0,0)*a(1,2))*t;
3127 inva(0,2) = (a(1,0)*a(2,1)-a(1,1)*a(2,0))*t;
3128 inva(1,2) = (a(0,1)*a(2,0)-a(0,0)*a(2,1))*t;
3129 inva(2,2) = (a(0,0)*a(1,1)-a(0,1)*a(1,0))*t;
3139 "Matrix must be 3x2 or 2x1, "
3140 <<
"and the Vector must be sized with the rows. "
3141 <<
" J.Height() = " << J.
Height()
3142 <<
", J.Width() = " << J.
Width()
3143 <<
", n.Size() = " << n.
Size()
3146 const double *d = J.
Data();
3154 n(0) = d[1]*d[5] - d[2]*d[4];
3155 n(1) = d[2]*d[3] - d[0]*d[5];
3156 n(2) = d[0]*d[4] - d[1]*d[3];
3162 for (
int i = 0; i < a.
Height(); i++)
3163 for (
int j = 0; j <= i; j++)
3166 for (
int k = 0; k < a.
Width(); k++)
3168 temp += a(i,k) * a(j,k);
3170 aat(j,i) = aat(i,j) = temp;
3176 for (
int i = 0; i < A.
Height(); i++)
3178 for (
int j = 0; j < i; j++)
3181 for (
int k = 0; k < A.
Width(); k++)
3183 t += D(k) * A(i, k) * A(j, k);
3191 for (
int i = 0; i < A.
Height(); i++)
3194 for (
int k = 0; k < A.
Width(); k++)
3196 t += D(k) * A(i, k) * A(i, k);
3204 for (
int i = 0; i < A.
Height(); i++)
3206 for (
int j = 0; j <= i; j++)
3209 for (
int k = 0; k < A.
Width(); k++)
3211 t += D(k) * A(i, k) * A(j, k);
3213 ADAt(j, i) = ADAt(i, j) = t;
3228 #ifdef MFEM_USE_LAPACK
3229 static char transa =
'N', transb =
'T';
3230 static double alpha = 1.0, beta = 0.0;
3233 dgemm_(&transa, &transb, &m, &n, &k, &alpha, A.
Data(), &m,
3234 B.
Data(), &n, &beta, ABt.
Data(), &m);
3236 const int ah = A.
Height();
3237 const int bh = B.
Height();
3238 const int aw = A.
Width();
3239 const double *ad = A.
Data();
3240 const double *bd = B.
Data();
3241 double *cd = ABt.
Data();
3243 for (
int i = 0, s = ah*bh; i < s; i++)
3247 for (
int k = 0; k < aw; k++)
3250 for (
int j = 0; j < bh; j++)
3252 const double bjk = bd[j];
3253 for (
int i = 0; i < ah; i++)
3255 cp[i] += ad[i] * bjk;
3263 const int ah = A.
Height();
3264 const int bh = B.
Height();
3265 const int aw = A.
Width();
3266 const double *ad = A.
Data();
3267 const double *bd = B.
Data();
3268 double *cd = ABt.
Data();
3270 for (
int j = 0; j < bh; j++)
3271 for (
int i = 0; i < ah; i++)
3274 const double *ap = ad + i;
3275 const double *bp = bd + j;
3276 for (
int k = 0; k < aw; k++)
3288 for (i = 0; i < A.
Height(); i++)
3289 for (j = 0; j < B.
Height(); j++)
3292 for (k = 0; k < A.
Width(); k++)
3294 d += A(i, k) * B(j, k);
3312 const int ah = A.
Height();
3313 const int bh = B.
Height();
3314 const int aw = A.
Width();
3315 const double *ad = A.
Data();
3316 const double *bd = B.
Data();
3317 const double *dd = D.
GetData();
3318 double *cd = ADBt.
Data();
3320 for (
int i = 0, s = ah*bh; i < s; i++)
3324 for (
int k = 0; k < aw; k++)
3327 for (
int j = 0; j < bh; j++)
3329 const double dk_bjk = dd[k] * bd[j];
3330 for (
int i = 0; i < ah; i++)
3332 cp[i] += ad[i] * dk_bjk;
3351 #ifdef MFEM_USE_LAPACK
3352 static char transa =
'N', transb =
'T';
3353 static double alpha = 1.0, beta = 1.0;
3356 dgemm_(&transa, &transb, &m, &n, &k, &alpha, A.
Data(), &m,
3357 B.
Data(), &n, &beta, ABt.
Data(), &m);
3359 const int ah = A.
Height();
3360 const int bh = B.
Height();
3361 const int aw = A.
Width();
3362 const double *ad = A.
Data();
3363 const double *bd = B.
Data();
3364 double *cd = ABt.
Data();
3366 for (
int k = 0; k < aw; k++)
3369 for (
int j = 0; j < bh; j++)
3371 const double bjk = bd[j];
3372 for (
int i = 0; i < ah; i++)
3374 cp[i] += ad[i] * bjk;
3385 for (i = 0; i < A.
Height(); i++)
3386 for (j = 0; j < B.
Height(); j++)
3389 for (k = 0; k < A.
Width(); k++)
3391 d += A(i, k) * B(j, k);
3408 #ifdef MFEM_USE_LAPACK
3409 static char transa =
'T', transb =
'N';
3410 static double alpha = 1.0, beta = 0.0;
3413 dgemm_(&transa, &transb, &m, &n, &k, &alpha, A.
Data(), &k,
3414 B.
Data(), &k, &beta, AtB.
Data(), &m);
3416 const int ah = A.
Height();
3417 const int aw = A.
Width();
3418 const int bw = B.
Width();
3419 const double *ad = A.
Data();
3420 const double *bd = B.
Data();
3421 double *cd = AtB.
Data();
3423 for (
int j = 0; j < bw; j++)
3425 const double *ap = ad;
3426 for (
int i = 0; i < aw; i++)
3429 for (
int k = 0; k < ah; k++)
3442 for (i = 0; i < A.
Width(); i++)
3443 for (j = 0; j < B.
Width(); j++)
3446 for (k = 0; k < A.
Height(); k++)
3448 d += A(k, i) * B(k, j);
3459 for (
int i = 0; i < A.
Height(); i++)
3461 for (
int j = 0; j < i; j++)
3464 for (
int k = 0; k < A.
Width(); k++)
3466 d += A(i,k) * A(j,k);
3468 AAt(i, j) += (d *= a);
3472 for (
int k = 0; k < A.
Width(); k++)
3474 d += A(i,k) * A(i,k);
3482 for (
int i = 0; i < A.
Height(); i++)
3483 for (
int j = 0; j <= i; j++)
3486 for (
int k = 0; k < A.
Width(); k++)
3488 d += A(i,k) * A(j,k);
3490 AAt(i, j) = AAt(j, i) = a * d;
3496 for (
int i = 0; i < v.
Size(); i++)
3497 for (
int j = 0; j <= i; j++)
3499 vvt(i,j) = vvt(j,i) = v(i) * v(j);
3515 for (i = 0; i < v.
Size(); i++)
3518 for (j = 0; j < w.
Size(); j++)
3520 VWt(i, j) = vi * w(j);
3536 for (
int i = 0; i < m; i++)
3539 for (
int j = 0; j < n; j++)
3541 VWt(i, j) += vi * w(j);
3558 for (
int i = 0; i < m; i++)
3560 double avi = a * v(i);
3561 for (
int j = 0; j < n; j++)
3563 VWt(i, j) += avi * w(j);
3579 for (
int i = 0; i < n; i++)
3581 double avi = a * v(i);
3582 for (
int j = 0; j < i; j++)
3584 double avivj = avi * v(j);
3588 VVt(i, i) += avi * v(i);
3595 #ifdef MFEM_USE_LAPACK
3598 MFEM_VERIFY(!info,
"LAPACK: error in DGETRF")
3602 for (
int i = 0; i < m; i++)
3607 double a = std::abs(data[piv+i*m]);
3608 for (
int j = i+1; j < m; j++)
3610 const double b = std::abs(data[j+i*m]);
3621 for (
int j = 0; j < m; j++)
3623 Swap<double>(data[i+j*m], data[piv+j*m]);
3627 MFEM_ASSERT(data[i+i*m] != 0.0,
"division by zero");
3628 const double a_ii_inv = 1.0/data[i+i*m];
3629 for (
int j = i+1; j < m; j++)
3631 data[j+i*m] *= a_ii_inv;
3633 for (
int k = i+1; k < m; k++)
3635 const double a_ik = data[i+k*m];
3636 for (
int j = i+1; j < m; j++)
3638 data[j+k*m] -= a_ik * data[j+i*m];
3650 for (
int k = 0; k < n; k++)
3653 for (
int i = 0; i < m; i++)
3655 double x_i = x[i] * data[i+i*m];
3656 for (
int j = i+1; j < m; j++)
3658 x_i += x[j] * data[i+j*m];
3663 for (
int i = m-1; i >= 0; i--)
3666 for (
int j = 0; j < i; j++)
3668 x_i += x[j] * data[i+j*m];
3673 for (
int i = m-1; i >= 0; i--)
3675 Swap<double>(x[i], x[ipiv[i]-
ipiv_base]);
3686 for (
int k = 0; k < n; k++)
3689 for (
int i = 0; i < m; i++)
3691 Swap<double>(x[i], x[ipiv[i]-
ipiv_base]);
3694 for (
int j = 0; j < m; j++)
3696 const double x_j = x[j];
3697 for (
int i = j+1; i < m; i++)
3699 x[i] -= data[i+j*m] * x_j;
3711 for (
int k = 0; k < n; k++)
3713 for (
int j = m-1; j >= 0; j--)
3715 const double x_j = ( x[j] /= data[j+j*m] );
3716 for (
int i = 0; i < j; i++)
3718 x[i] -= data[i+j*m] * x_j;
3727 #ifdef MFEM_USE_LAPACK
3731 MFEM_VERIFY(!info,
"LAPACK: error in DGETRS")
3746 for (
int k = 0; k < m; k++)
3748 const double minus_x_k = -( x[k] = 1.0/data[k+k*m] );
3749 for (
int i = 0; i < k; i++)
3751 x[i] = data[i+k*m] * minus_x_k;
3753 for (
int j = k-1; j >= 0; j--)
3755 const double x_j = ( x[j] /= data[j+j*m] );
3756 for (
int i = 0; i < j; i++)
3758 x[i] -= data[i+j*m] * x_j;
3766 for (
int j = 0; j < k; j++)
3768 const double minus_L_kj = -data[k+j*m];
3769 for (
int i = 0; i <= j; i++)
3771 X[i+j*m] += X[i+k*m] * minus_L_kj;
3773 for (
int i = j+1; i < m; i++)
3775 X[i+j*m] = X[i+k*m] * minus_L_kj;
3779 for (
int k = m-2; k >= 0; k--)
3781 for (
int j = 0; j < k; j++)
3783 const double L_kj = data[k+j*m];
3784 for (
int i = 0; i < m; i++)
3786 X[i+j*m] -= X[i+k*m] * L_kj;
3791 for (
int k = m-1; k >= 0; k--)
3796 for (
int i = 0; i < m; i++)
3798 Swap<double>(X[i+k*m], X[i+piv_k*m]);
3805 const double *X1,
double *X2)
3808 for (
int k = 0; k < r; k++)
3810 for (
int j = 0; j < m; j++)
3812 const double x1_jk = X1[j+k*m];
3813 for (
int i = 0; i < n; i++)
3815 X2[i+k*n] -= A21[i+j*n] * x1_jk;
3822 int m,
int n,
double *A12,
double *A21,
double *A22)
const
3828 for (
int j = 0; j < m; j++)
3830 const double u_jj_inv = 1.0/data[j+j*m];
3831 for (
int i = 0; i < n; i++)
3833 A21[i+j*n] *= u_jj_inv;
3835 for (
int k = j+1; k < m; k++)
3837 const double u_jk = data[j+k*m];
3838 for (
int i = 0; i < n; i++)
3840 A21[i+k*n] -= A21[i+j*n] * u_jk;
3845 SubMult(m, n, n, A21, A12, A22);
3849 double *B1,
double *B2)
const
3854 SubMult(m, n, r, L21, B1, B2);
3858 const double *X2,
double *Y1)
const
3861 SubMult(n, m, r, U12, X2, Y1);
3870 MFEM_ASSERT(
height ==
width,
"not a square matrix");
3880 MFEM_ASSERT(
height ==
width,
"not a square matrix");
3888 MFEM_ASSERT(a,
"DenseMatrix is not given");
3889 const double *adata = a->data;
3892 lu.
data[i] = adata[i];
3899 MFEM_VERIFY(mat.
height == mat.
width,
"DenseMatrix is not square!");
3915 MFEM_VERIFY(p != NULL,
"Operator is not a DenseMatrix!");
3935 for (
int i = 0; i <
width; i++)
3939 cout <<
"size = " << width <<
", i_max = " << C.
MaxMaxNorm() << endl;
3957 #ifdef MFEM_USE_LAPACK
3963 &qwork, &lwork, &info);
3965 lwork = (int) qwork;
3966 work =
new double[lwork];
3973 if (mat.
Width() != n)
3975 mfem_error(
"DenseMatrixEigensystem::Eval()");
3979 #ifdef MFEM_USE_LAPACK
3982 work, &lwork, &info);
3986 cerr <<
"DenseMatrixEigensystem::Eval(): DSYEV error code: "
3991 mfem_error(
"DenseMatrixEigensystem::Eval(): Compiled without LAPACK");
3997 #ifdef MFEM_USE_LAPACK
4017 void DenseMatrixSVD::Init()
4019 #ifdef MFEM_USE_LAPACK
4028 NULL, &n, &qwork, &lwork, &info);
4030 lwork = (int) qwork;
4031 work =
new double[lwork];
4033 mfem_error(
"DenseMatrixSVD::Init(): Compiled without LAPACK");
4046 #ifdef MFEM_USE_LAPACK
4048 NULL, &n, work, &lwork, &info);
4052 cerr <<
"DenseMatrixSVD::Eval() : info = " << info << endl;
4056 mfem_error(
"DenseMatrixSVD::Eval(): Compiled without LAPACK");
4062 #ifdef MFEM_USE_LAPACK
4072 const int *I = elem_dof.
GetI(), *J = elem_dof.
GetJ(), *dofs;
4073 double *d_col = tdata, *yp = y, x_col;
4074 const double *xp = x;
4078 for (
int i = 0; i < ne; i++)
4081 for (
int col = 0; col < n; col++)
4083 x_col = xp[dofs[col]];
4084 for (
int row = 0; row < n; row++)
4086 yp[dofs[row]] += x_col*d_col[row];
4095 for (
int i = 0; i < ne; i++)
4098 x_col = xp[dofs[0]];
4099 for (
int row = 0; row < n; row++)
4101 ye(row) = x_col*d_col[row];
4104 for (
int col = 1; col < n; col++)
4106 x_col = xp[dofs[col]];
4107 for (
int row = 0; row < n; row++)
4109 ye(row) += x_col*d_col[row];
4113 for (
int row = 0; row < n; row++)
4115 yp[dofs[row]] += ye(row);
virtual void PrintT(std::ostream &out=std::cout, int width_=4) const
Prints the transpose matrix to stream out.
void Symmetrize()
(*this) = 1/2 ((*this) + (*this)^t)
void dsyevr_Eigensystem(DenseMatrix &a, Vector &ev, DenseMatrix *evect)
void MultABt(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt)
Multiply a matrix A with the transpose of a matrix B: A*Bt.
int Size() const
Logical size of the array.
void SymmetricScaling(const Vector &s)
SymmetricScaling this = diag(sqrt(s)) * this * diag(sqrt(s))
void AddMultVWt(const Vector &v, const Vector &w, DenseMatrix &VWt)
VWt += v w^t.
DenseMatrix & operator*=(double c)
void GetDiag(Vector &d) const
Returns the diagonal of the matrix.
bool KernelVector2G(const int &mode, double &d1, double &d12, double &d21, double &d2)
void MultVWt(const Vector &v, const Vector &w, DenseMatrix &VWt)
DenseMatrix & operator+=(DenseMatrix &m)
void InvRightScaling(const Vector &s)
InvRightScaling: this = this * diag(1./s);.
void Eigenvalues2S(const double &d12, double &d1, double &d2)
void GetScalingFactor(const double &d_max, double &mult)
void SingularValues(Vector &sv) const
void dsyev_Eigensystem(DenseMatrix &a, Vector &ev, DenseMatrix *evect)
void SetSize(int s)
Resize the vector if the new size is different.
double Det() const
Calculates the determinant of the matrix (for 2x2 or 3x3 matrices)
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols.
void BlockFactor(int m, int n, double *A12, double *A21, double *A22) const
void BlockBackSolve(int m, int n, int r, const double *U12, const double *X2, double *Y1) const
double InnerProduct(const double *x, const double *y) const
Compute y^t A x.
void CalcAdjugate(const DenseMatrix &a, DenseMatrix &adja)
void AddMult(const Table &elem_dof, const Vector &x, Vector &y) const
void TestInversion()
Invert and print the numerical conditioning of the inversion.
Data type dense matrix using column-major storage.
int Size() const
Returns the size of the vector.
void CopyRows(const DenseMatrix &A, int row1, int row2)
Copy rows row1 through row2 from A to *this.
void Eval(DenseMatrix &M)
Abstract data type for matrix inverse.
void dgetrs_(char *, int *, int *, double *, int *, int *, double *, int *, int *)
void Factor()
Factor the current DenseMatrix, *a.
void GetInverseMatrix(int m, double *X) const
Assuming L.U = P.A factored data of size (m x m), compute X <- A^{-1}.
void CalcOrtho(const DenseMatrix &J, Vector &n)
DenseMatrix & operator=(double c)
Sets the matrix elements equal to constant c.
void vec_normalize3_aux(const double &x1, const double &x2, const double &x3, double &n1, double &n2, double &n3)
void dgesvd_(char *JOBU, char *JOBVT, int *M, int *N, double *A, int *LDA, double *S, double *U, int *LDU, double *VT, int *LDVT, double *WORK, int *LWORK, int *INFO)
void Mult_a_AAt(double a, const DenseMatrix &A, DenseMatrix &AAt)
AAt = a * A * A^t.
static void SubMult(int m, int n, int r, const double *A21, const double *X1, double *X2)
void dgetri_(int *N, double *A, int *LDA, int *IPIV, double *WORK, int *LWORK, int *INFO)
virtual void Mult(const Vector &x, Vector &y) const
Matrix vector multiplication with the inverse of dense matrix.
int KernelVector3G_aux(const int &mode, double &d1, double &d2, double &d3, double &c12, double &c13, double &c23, double &c21, double &c31, double &c32)
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
double & operator()(int i, int j)
Returns reference to a_{ij}.
void USolve(int m, int n, double *X) const
double FNorm() const
Compute the Frobenius norm of the matrix.
void MultTranspose(const double *x, double *y) const
Multiply a vector with the transpose matrix.
void CalcAdjugateTranspose(const DenseMatrix &a, DenseMatrix &adjat)
Calculate the transposed adjugate of a matrix (for NxN matrices, N=1,2,3)
friend class DenseMatrixInverse
void dgemm_(char *, char *, int *, int *, int *, double *, double *, int *, double *, int *, double *, double *, int *)
void AddMult(const DenseMatrix &b, const DenseMatrix &c, DenseMatrix &a)
Matrix matrix multiplication. A += B * C.
double operator*(const DenseMatrix &m) const
Matrix inner product: tr(A^t B)
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows.
void Add(const double c, const DenseMatrix &A)
Adds the matrix A multiplied by the number c to the matrix.
void AddMult_a_VWt(const double a, const Vector &v, const Vector &w, DenseMatrix &VWt)
VWt += a * v w^t.
void InvSymmetricScaling(const Vector &s)
InvSymmetricScaling this = diag(sqrt(1./s)) * this * diag(sqrt(1./s))
void BlockForwSolve(int m, int n, int r, const double *L21, double *B1, double *B2) const
DenseMatrixSVD(DenseMatrix &M)
Abstract data type matrix.
void Norm2(double *v) const
Take the 2-norm of the columns of A and store in v.
void Invert()
Replaces the current matrix with its inverse.
virtual ~DenseMatrixInverse()
Destroys dense inverse matrix.
void LSolve(int m, int n, double *X) const
void LeftScaling(const Vector &s)
LeftScaling this = diag(s) * this.
void CopyMNDiag(double c, int n, int row_offset, int col_offset)
Copy c on the diagonal of size n to *this at row_offset, col_offset.
void vec_normalize3(const double &x1, const double &x2, const double &x3, double &n1, double &n2, double &n3)
virtual void PrintMatlab(std::ostream &out=std::cout) const
~DenseMatrixEigensystem()
void AddMult_a_VVt(const double a, const Vector &v, DenseMatrix &VVt)
VVt += a * v v^t.
void Neg()
(*this) = -(*this)
virtual void Print(std::ostream &out=std::cout, int width_=4) const
Prints matrix to stream out.
virtual void SetOperator(const Operator &op)
Set/update the solver for the given operator.
void Solve(int m, int n, double *X) const
int KernelVector3S(const int &mode, const double &d12, const double &d13, const double &d23, double &d1, double &d2, double &d3)
void SetRow(int r, const Vector &row)
void Getl1Diag(Vector &l) const
Returns the l1 norm of the rows of the matrix v_i = sum_j |a_ij|.
void AddToVector(int offset, Vector &v) const
Add the matrix 'data' to the Vector 'v' at the given 'offset'.
void GetColumn(int c, Vector &col) const
void AddMult(const Vector &x, Vector &y) const
y += A.x
void Threshold(double eps)
Replace small entries, abs(a_ij) <= eps, with zero.
void CalcInverse(const DenseMatrix &a, DenseMatrix &inva)
void TestInversion()
Print the numerical conditioning of the inversion: ||A^{-1} A - I||.
void Eigensystem2S(const double &d12, double &d1, double &d2, double &c, double &s)
double MaxMaxNorm() const
Compute the norm ||A|| = max_{ij} |A_{ij}|.
void dsyev_(char *JOBZ, char *UPLO, int *N, double *A, int *LDA, double *W, double *WORK, int *LWORK, int *INFO)
double * Data() const
Returns vector of the elements.
void Swap(Array< T > &, Array< T > &)
void Transpose()
(*this) = (*this)^t
void MultVVt(const Vector &v, DenseMatrix &vvt)
Make a matrix from a vector V.Vt.
double Trace() const
Trace of a square matrix.
void AddMultABt(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt)
ABt += A * B^t.
void mfem_error(const char *msg)
int Reduce3S(const int &mode, double &d1, double &d2, double &d3, double &d12, double &d13, double &d23, double &z1, double &z2, double &z3, double &v1, double &v2, double &v3, double &g)
void MultAAt(const DenseMatrix &a, DenseMatrix &aat)
Calculate the matrix A.At.
void dgetrf_(int *, int *, double *, int *, int *, int *)
void AddMatrix(DenseMatrix &A, int ro, int co)
Perform (ro+i,co+j)+=A(i,j) for 0<=i<A.Height, 0<=j<A.Width.
void CalcInverseTranspose(const DenseMatrix &a, DenseMatrix &inva)
Calculate the inverse transpose of a matrix (for NxN matrices, N=1,2,3)
void SetDataAndSize(double *d, int s)
DenseMatrix & operator-=(DenseMatrix &m)
void MultADAt(const DenseMatrix &A, const Vector &D, DenseMatrix &ADAt)
ADAt = A D A^t, where D is diagonal.
void CopyCols(const DenseMatrix &A, int col1, int col2)
Copy columns col1 through col2 from A to *this.
virtual MatrixInverse * Inverse() const
Returns a pointer to the inverse matrix.
virtual ~DenseMatrix()
Destroys dense matrix.
void CopyMNt(const DenseMatrix &A, int row_offset, int col_offset)
Copy matrix A^t to the location in *this at row_offset, col_offset.
void Diag(double c, int n)
Creates n x n diagonal matrix with diagonal elements c.
void Mult(int m, int n, double *X) const
void MultADBt(const DenseMatrix &A, const Vector &D, const DenseMatrix &B, DenseMatrix &ADBt)
ADBt = A D B^t, where D is diagonal.
static const int ipiv_base
void GradToCurl(DenseMatrix &curl)
void dsyevr_(char *JOBZ, char *RANGE, char *UPLO, int *N, double *A, int *LDA, double *VL, double *VU, int *IL, int *IU, double *ABSTOL, int *M, double *W, double *Z, int *LDZ, int *ISUPPZ, double *WORK, int *LWORK, int *IWORK, int *LIWORK, int *INFO)
DenseMatrixInverse()
Default constructor.
double CalcSingularvalue(const int i) const
Return the i-th singular value (decreasing order) of NxN matrix, N=1,2,3.
void GetRowSums(Vector &l) const
Compute the row sums of the DenseMatrix.
void GetRow(int r, Vector &row)
void CalcEigenvalues(double *lambda, double *vec) const
DenseMatrixEigensystem(DenseMatrix &m)
int Rank(double tol) const
void AddMult_a(double a, const Vector &x, Vector &y) const
y += a * A.x
void RightScaling(const Vector &s)
RightScaling: this = this * diag(s);.
void MultAtB(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &AtB)
Multiply the transpose of a matrix A with a matrix B: At*B.
void Mult(const double *x, double *y) const
Matrix vector multiplication.
void AddMultTranspose_a(double a, const Vector &x, Vector &y) const
void AddMultADAt(const DenseMatrix &A, const Vector &D, DenseMatrix &ADAt)
ADAt += A D A^t, where D is diagonal.
void GetFromVector(int offset, const Vector &v)
Get the matrix 'data' from the Vector 'v' at the given 'offset'.
void CopyMN(const DenseMatrix &A, int m, int n, int Aro, int Aco)
Copy the m x n submatrix of A at row/col offsets Aro/Aco to *this.
void InvLeftScaling(const Vector &s)
InvLeftScaling this = diag(1./s) * this.
void SetCol(int c, const Vector &col)
void SetSize(int s)
Change the size of the DenseMatrix to s x s.
virtual double & Elem(int i, int j)
Returns reference to a_{ij}.
void AdjustDofDirection(Array< int > &dofs)
void GradToDiv(Vector &div)
void AddMult_a_AAt(double a, const DenseMatrix &A, DenseMatrix &AAt)
AAt += a * A * A^t.