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;
830 mfem_error(
"dsyevr_Eigensystem(...): inf/nan values in W");
834 mfem_error(
"dsyevr_Eigensystem(...): inf/nan values in Z");
837 for (IL = 0; IL < N; IL++)
838 for (IU = 0; IU <= IL; IU++)
841 for (M = 0; M < N; M++)
843 VL += Z[M+IL*N] * Z[M+IU*N];
859 cerr <<
"dsyevr_Eigensystem(...):"
860 <<
" Z^t Z - I deviation = " << VU
861 <<
"\n W[max] = " << W[N-1] <<
", W[min] = "
862 << W[0] <<
", N = " << N << endl;
868 cerr <<
"dsyevr_Eigensystem(...):"
869 <<
" Z^t Z - I deviation = " << VU
870 <<
"\n W[max] = " << W[N-1] <<
", W[min] = "
871 << W[0] <<
", N = " << N << endl;
875 mfem_error(
"dsyevr_Eigensystem(...): ERROR: ...");
878 for (IL = 0; IL < N; IL++)
879 for (IU = 0; IU < N; IU++)
882 for (M = 0; M < N; M++)
884 VL += Z[IL+M*N] * W[M] * Z[IU+M*N];
886 VL = fabs(VL-data[IL+N*IU]);
894 cerr <<
"dsyevr_Eigensystem(...):"
895 <<
" max matrix deviation = " << VU
896 <<
"\n W[max] = " << W[N-1] <<
", W[min] = "
897 << W[0] <<
", N = " << N << endl;
901 mfem_error(
"dsyevr_Eigensystem(...): ERROR: ...");
916 #ifdef MFEM_USE_LAPACK
944 double *data = a.
Data();
945 for (
int i = 0; i < hw; i++)
950 dsyev_(&JOBZ, &UPLO, &N, A, &LDA, W, &QWORK, &LWORK, &INFO);
953 WORK =
new double[LWORK];
955 dsyev_(&JOBZ, &UPLO, &N, A, &LDA, W, WORK, &LWORK, &INFO);
959 cerr <<
"dsyev_Eigensystem: DSYEV error code: " << INFO << endl;
964 if (evect == NULL) {
delete [] A; }
969 void DenseMatrix::Eigensystem(Vector &ev, DenseMatrix *evect)
971 #ifdef MFEM_USE_LAPACK
986 #ifdef MFEM_USE_LAPACK
992 double *a = copy_of_this.data;
1002 dgesvd_(&jobu, &jobvt, &m, &n, a, &m,
1003 s, u, &m, vt, &n, &qwork, &lwork, &info);
1005 lwork = (int) qwork;
1006 work =
new double[lwork];
1008 dgesvd_(&jobu, &jobvt, &m, &n, a, &m,
1009 s, u, &m, vt, &n, work, &lwork, &info);
1014 cerr <<
"DenseMatrix::SingularValues : info = " << info << endl;
1029 for (
int i=0; i < sv.
Size(); ++i)
1038 static const double sqrt_1_eps = sqrt(1./numeric_limits<double>::epsilon());
1045 double t, zeta = (d2 - d1)/(2*d12);
1046 if (fabs(zeta) < sqrt_1_eps)
1048 t = d12*copysign(1./(fabs(zeta) + sqrt(1. + zeta*zeta)), zeta);
1052 t = d12*copysign(0.5/fabs(zeta), zeta);
1060 double &c,
double &s)
1070 double t, zeta = (d2 - d1)/(2*d12);
1071 if (fabs(zeta) < sqrt_1_eps)
1073 t = copysign(1./(fabs(zeta) + sqrt(1. + zeta*zeta)), zeta);
1077 t = copysign(0.5/fabs(zeta), zeta);
1080 c = sqrt(1./(1. + t*t));
1089 const double &x1,
const double &x2,
const double &x3,
1090 double &n1,
double &n2,
double &n3)
1098 t = sqrt(1./(t + r*r));
1099 n1 = copysign(t, x1);
1106 double &n1,
double &n2,
double &n3)
1110 if (fabs(x1) >= fabs(x2))
1112 if (fabs(x1) >= fabs(x3))
1125 else if (fabs(x2) >= fabs(x3))
1135 double &d1,
double &d12,
double &d21,
double &d2)
1146 double n1 = fabs(d1) + fabs(d21);
1147 double n2 = fabs(d2) + fabs(d12);
1149 bool swap_columns = (n2 > n1);
1161 if (fabs(d1) > fabs(d21))
1169 if (fabs(d1) < fabs(d21))
1181 if (fabs(d12) > fabs(d2))
1194 if (fabs(d12) < fabs(d2))
1207 n1 = hypot(d1, d21);
1213 mu = copysign(n1, d1);
1214 n1 = -d21*(d21/(d1 + mu));
1218 if (fabs(n1) <= fabs(d21))
1222 mu = (2./(1. + n1*n1))*(n1*d12 + d2);
1230 mu = (2./(1. + n2*n2))*(d12 + n2*d2);
1252 n2 = 1./(1. + fabs(mu));
1254 if (fabs(d1) <= n2*fabs(d2))
1276 double &d1,
double &d2,
double &d3,
double &c12,
double &c13,
double &c23,
1277 double &c21,
double &c31,
double &c32)
1280 double mu, n1, n2, n3, s1, s2, s3;
1282 s1 = hypot(c21, c31);
1289 mu = copysign(n1, d1);
1290 n1 = -s1*(s1/(d1 + mu));
1295 if (fabs(n1) >= fabs(c21))
1297 if (fabs(n1) >= fabs(c31))
1302 mu = 2./(1. + s2*s2 + s3*s3);
1303 n2 = mu*(c12 + s2*d2 + s3*c32);
1304 n3 = mu*(c13 + s2*c23 + s3*d3);
1314 else if (fabs(c21) >= fabs(c31))
1319 mu = 2./(1. + s1*s1 + s3*s3);
1320 n2 = mu*(s1*c12 + d2 + s3*c32);
1321 n3 = mu*(s1*c13 + c23 + s3*d3);
1333 mu = 2./(1. + s1*s1 + s2*s2);
1334 n2 = mu*(s1*c12 + s2*d2 + c32);
1335 n3 = mu*(s1*c13 + s2*c23 + d3);
1370 d1 = -(c12*d2 + c13*d3)/d1;
1381 const double &d12,
const double &d13,
const double &d23,
1382 double &d1,
double &d2,
double &d3)
1395 double c12 = d12, c13 = d13, c23 = d23;
1396 double c21, c31, c32;
1400 c32 = fabs(d1) + fabs(c12) + fabs(c13);
1401 c31 = fabs(d2) + fabs(c12) + fabs(c23);
1402 c21 = fabs(d3) + fabs(c13) + fabs(c23);
1407 col = (c32 >= c31) ? 1 : 2;
1411 col = (c31 >= c21) ? 2 : 3;
1443 if (fabs(d1) <= fabs(c13))
1445 row = (fabs(d1) <= fabs(c12)) ? 1 : 2;
1449 row = (fabs(c12) <= fabs(c13)) ? 2 : 3;
1454 if (fabs(d1) >= fabs(c13))
1456 row = (fabs(d1) >= fabs(c12)) ? 1 : 2;
1460 row = (fabs(c12) >= fabs(c13)) ? 2 : 3;
1510 double &d1,
double &d2,
double &d3,
double &d12,
double &d13,
double &d23,
1511 double &z1,
double &z2,
double &z3,
double &v1,
double &v2,
double &v3,
1531 double s, w1, w2, w3;
1537 if (fabs(z1) <= fabs(z3))
1539 k = (fabs(z1) <= fabs(z2)) ? 1 : 2;
1543 k = (fabs(z2) <= fabs(z3)) ? 2 : 3;
1549 if (fabs(z1) >= fabs(z3))
1551 k = (fabs(z1) >= fabs(z2)) ? 1 : 2;
1555 k = (fabs(z2) >= fabs(z3)) ? 2 : 3;
1582 g = copysign(1., z1);
1583 v1 = -s*(s/(z1 + g));
1586 if (fabs(z2) > g) { g = fabs(z2); }
1587 if (fabs(z3) > g) { g = fabs(z3); }
1591 g = 2./(v1*v1 + v2*v2 + v3*v3);
1596 w1 = g*( d1*v1 + d12*v2 + d13*v3);
1597 w2 = g*(d12*v1 + d2*v2 + d23*v3);
1598 w3 = g*(d13*v1 + d23*v2 + d3*v3);
1600 s = (g/2)*(v1*w1 + v2*w2 + v3*w3);
1607 d23 -= v2*w3 + v3*w2;
1612 s = d12 - v1*w2 - v2*w1;
1613 s = d13 - v1*w3 - v3*w1;
1635 mult = frexp(d_max, &d_exp);
1636 if (d_exp == numeric_limits<double>::max_exponent)
1638 mult *= numeric_limits<double>::radix;
1653 "The matrix must be square and sized 1, 2, or 3 to compute the singular values."
1654 <<
" Height() = " <<
Height()
1655 <<
", Width() = " <<
Width());
1658 const double *d = data;
1666 double d0, d1, d2, d3;
1673 double d_max = fabs(d0);
1674 if (d_max < fabs(d1)) { d_max = fabs(d1); }
1675 if (d_max < fabs(d2)) { d_max = fabs(d2); }
1676 if (d_max < fabs(d3)) { d_max = fabs(d3); }
1690 double t = 0.5*((d0+d2)*(d0-d2)+(d1-d3)*(d1+d3));
1692 double s = d0*d2 + d1*d3;
1693 s = sqrt(0.5*(d0*d0 + d1*d1 + d2*d2 + d3*d3) + sqrt(t*t + s*s));
1698 t = fabs(d0*d3 - d1*d2) / s;
1715 double d0, d1, d2, d3, d4, d5, d6, d7, d8;
1716 d0 = d[0]; d3 = d[3]; d6 = d[6];
1717 d1 = d[1]; d4 = d[4]; d7 = d[7];
1718 d2 = d[2]; d5 = d[5]; d8 = d[8];
1721 double d_max = fabs(d0);
1722 if (d_max < fabs(d1)) { d_max = fabs(d1); }
1723 if (d_max < fabs(d2)) { d_max = fabs(d2); }
1724 if (d_max < fabs(d3)) { d_max = fabs(d3); }
1725 if (d_max < fabs(d4)) { d_max = fabs(d4); }
1726 if (d_max < fabs(d5)) { d_max = fabs(d5); }
1727 if (d_max < fabs(d6)) { d_max = fabs(d6); }
1728 if (d_max < fabs(d7)) { d_max = fabs(d7); }
1729 if (d_max < fabs(d8)) { d_max = fabs(d8); }
1734 d0 /= mult; d1 /= mult; d2 /= mult;
1735 d3 /= mult; d4 /= mult; d5 /= mult;
1736 d6 /= mult; d7 /= mult; d8 /= mult;
1738 double b11 = d0*d0 + d1*d1 + d2*d2;
1739 double b12 = d0*d3 + d1*d4 + d2*d5;
1740 double b13 = d0*d6 + d1*d7 + d2*d8;
1741 double b22 = d3*d3 + d4*d4 + d5*d5;
1742 double b23 = d3*d6 + d4*d7 + d5*d8;
1743 double b33 = d6*d6 + d7*d7 + d8*d8;
1762 double aa = (b11 + b22 + b33)/3;
1768 double b11_b22 = ((d0-d3)*(d0+d3)+(d1-d4)*(d1+d4)+(d2-d5)*(d2+d5));
1769 double b22_b33 = ((d3-d6)*(d3+d6)+(d4-d7)*(d4+d7)+(d5-d8)*(d5+d8));
1770 double b33_b11 = ((d6-d0)*(d6+d0)+(d7-d1)*(d7+d1)+(d8-d2)*(d8+d2));
1771 c1 = (b11_b22 - b33_b11)/3;
1772 c2 = (b22_b33 - b11_b22)/3;
1773 c3 = (b33_b11 - b22_b33)/3;
1776 Q = (2*(b12*b12 + b13*b13 + b23*b23) + c1*c1 + c2*c2 + c3*c3)/6;
1777 R = (c1*(b23*b23 - c2*c3)+ b12*(b12*c3 - 2*b13*b23) +b13*b13*c2)/2;
1810 double sqrtQ = sqrt(Q);
1811 double sqrtQ3 = Q*sqrtQ;
1816 if (fabs(R) >= sqrtQ3)
1838 aa -= 2*sqrtQ*cos(acos(R)/3);
1842 aa -= 2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
1846 aa -= 2*sqrtQ*cos((acos(R) - 2.0*M_PI)/3);
1853 r = -2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
1862 r = -2*sqrtQ*cos(acos(R)/3);
1909 double v1, v2, v3, g;
1910 Reduce3S(mode, b11, b22, b33, b12, b13, b23,
1911 c1, c2, c3, v1, v2, v3, g);
1922 aa = std::min(std::min(b11, b22), b33);
1928 aa = (b22 <= b33) ? b22 : std::max(b11, b33);
1932 aa = (b11 <= b33) ? b11 : std::max(b33, b22);
1937 aa = std::max(std::max(b11, b22), b33);
1943 return sqrt(fabs(aa))*mult;
1957 const double *d = data;
1997 double d_max = fabs(d11);
1998 if (d_max < fabs(d22)) { d_max = fabs(d22); }
1999 if (d_max < fabs(d33)) { d_max = fabs(d33); }
2000 if (d_max < fabs(d12)) { d_max = fabs(d12); }
2001 if (d_max < fabs(d13)) { d_max = fabs(d13); }
2002 if (d_max < fabs(d23)) { d_max = fabs(d23); }
2007 d11 /= mult; d22 /= mult; d33 /= mult;
2008 d12 /= mult; d13 /= mult; d23 /= mult;
2010 double aa = (d11 + d22 + d33)/3;
2011 double c1 = d11 - aa;
2012 double c2 = d22 - aa;
2013 double c3 = d33 - aa;
2017 Q = (2*(d12*d12 + d13*d13 + d23*d23) + c1*c1 + c2*c2 + c3*c3)/6;
2018 R = (c1*(d23*d23 - c2*c3)+ d12*(d12*c3 - 2*d13*d23) + d13*d13*c2)/2;
2022 lambda[0] = lambda[1] = lambda[2] = aa;
2023 vec[0] = 1.; vec[3] = 0.; vec[6] = 0.;
2024 vec[1] = 0.; vec[4] = 1.; vec[7] = 0.;
2025 vec[2] = 0.; vec[5] = 0.; vec[8] = 1.;
2029 double sqrtQ = sqrt(Q);
2030 double sqrtQ3 = Q*sqrtQ;
2034 if (fabs(R) >= sqrtQ3)
2053 r = -2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
2057 r = -2*sqrtQ*cos(acos(R)/3);
2085 lambda[0] = lambda[1] = lambda[2] = aa;
2086 vec[0] = 1.; vec[3] = 0.; vec[6] = 0.;
2087 vec[1] = 0.; vec[4] = 1.; vec[7] = 0.;
2088 vec[2] = 0.; vec[5] = 0.; vec[8] = 1.;
2102 double v1, v2, v3, g;
2103 int k =
Reduce3S(mode, d11, d22, d33, d12, d13, d23,
2104 c1, c2, c3, v1, v2, v3, g);
2115 double *vec_1, *vec_2, *vec_3;
2120 lambda[0] = d11; vec_1 = vec;
2121 lambda[1] = d22; vec_2 = vec + 3;
2122 lambda[2] = d33; vec_3 = vec + 6;
2124 else if (d11 <= d33)
2126 lambda[0] = d11; vec_1 = vec;
2127 lambda[1] = d33; vec_3 = vec + 3;
2128 lambda[2] = d22; vec_2 = vec + 6;
2132 lambda[0] = d33; vec_3 = vec;
2133 lambda[1] = d11; vec_1 = vec + 3;
2134 lambda[2] = d22; vec_2 = vec + 6;
2141 lambda[0] = d22; vec_2 = vec;
2142 lambda[1] = d11; vec_1 = vec + 3;
2143 lambda[2] = d33; vec_3 = vec + 6;
2145 else if (d22 <= d33)
2147 lambda[0] = d22; vec_2 = vec;
2148 lambda[1] = d33; vec_3 = vec + 3;
2149 lambda[2] = d11; vec_1 = vec + 6;
2153 lambda[0] = d33; vec_3 = vec;
2154 lambda[1] = d22; vec_2 = vec + 3;
2155 lambda[2] = d11; vec_1 = vec + 6;
2162 d22 = g*(v2*c - v3*s);
2163 d33 = g*(v2*s + v3*c);
2164 vec_2[0] = - v1*d22; vec_3[0] = - v1*d33;
2165 vec_2[1] = c - v2*d22; vec_3[1] = s - v2*d33;
2166 vec_2[2] = -s - v3*d22; vec_3[2] = c - v3*d33;
2170 Swap(vec_2[0], vec_2[1]);
2171 Swap(vec_3[0], vec_3[1]);
2175 Swap(vec_2[0], vec_2[2]);
2176 Swap(vec_3[0], vec_3[2]);
2193 double* rp = data + r;
2196 for (
int i = 0; i < n; i++)
2208 double *cp = data + c * m;
2211 for (
int i = 0; i < m; i++)
2225 for (
int i = 0; i <
height; ++i)
2227 d(i) = (*this)(i,i);
2241 for (
int j = 0; j <
width; ++j)
2242 for (
int i = 0; i <
height; ++i)
2244 l(i) += fabs((*
this)(i,j));
2251 for (
int i = 0; i <
height; i++)
2254 for (
int j = 0; j <
width; j++)
2267 for (i = 0; i < N; i++)
2271 for (i = 0; i < n; i++)
2282 for (i = 0; i < N; i++)
2286 for (i = 0; i < n; i++)
2288 data[i*(n+1)] = diag[i];
2299 for (i = 0; i <
Height(); i++)
2300 for (j = i+1; j <
Width(); j++)
2303 (*this)(i,j) = (*
this)(j,i);
2318 for (
int i = 0; i <
Height(); i++)
2319 for (
int j = 0; j <
Width(); j++)
2321 (*this)(i,j) = A(j,i);
2330 mfem_error(
"DenseMatrix::Symmetrize() : not a square matrix!");
2334 for (
int i = 0; i <
Height(); i++)
2335 for (
int j = 0; j < i; j++)
2337 double a = 0.5 * ((*this)(i,j) + (*
this)(j,i));
2338 (*this)(j,i) = (*
this)(i,j) = a;
2344 for (
int i = 0; i <
Height(); i++)
2347 for (
int j = 0; j <
Width(); j++)
2350 (*this)(i, j) = 0.0;
2370 for (
int i = 0; i < n; i++)
2373 double x = (*this)(i,0);
2374 double y = (*this)(i,1);
2387 for (
int i = 0; i < n; i++)
2390 double x = (*this)(i,0);
2391 double y = (*this)(i,1);
2392 double z = (*this)(i,2);
2428 double *ddata = div.
GetData();
2430 for (
int i = 0; i < n; i++)
2440 for (
int j = 0; j <
Width(); j++)
2441 for (
int i = row1; i <= row2; i++)
2443 (*this)(i-row1,j) = A(i,j);
2451 for (
int j = col1; j <= col2; j++)
2452 for (
int i = 0; i <
Height(); i++)
2454 (*this)(i,j-col1) = A(i,j);
2464 for (j = 0; j < n; j++)
2465 for (i = 0; i < m; i++)
2467 (*this)(i,j) = A(Aro+i,Aco+j);
2476 for (j = 0; j < A.
Width(); j++)
2477 for (i = 0; i < A.
Height(); i++)
2479 (*this)(row_offset+i,col_offset+j) = *(v++);
2488 for (i = 0; i < A.
Width(); i++)
2489 for (j = 0; j < A.
Height(); j++)
2491 (*this)(row_offset+i,col_offset+j) = *(v++);
2496 int row_offset,
int col_offset)
2500 MFEM_VERIFY(row_offset+m <= this->
Height() && col_offset+n <= this->
Width(),
2501 "this DenseMatrix is too small to accomodate the submatrix. "
2502 <<
"row_offset = " << row_offset
2504 <<
", this->Height() = " << this->
Height()
2505 <<
", col_offset = " << col_offset
2507 <<
", this->Width() = " << this->
Width()
2509 MFEM_VERIFY(Aro+m <= A.
Height() && Aco+n <= A.
Width(),
2510 "The A DenseMatrix is too small to accomodate the submatrix. "
2513 <<
", A.Height() = " << A.
Height()
2514 <<
", Aco = " << Aco
2516 <<
", A.Width() = " << A.
Width()
2519 for (j = 0; j < n; j++)
2520 for (i = 0; i < m; i++)
2522 (*this)(row_offset+i,col_offset+j) = A(Aro+i,Aco+j);
2530 for (i = 0; i < n; i++)
2531 for (j = i+1; j < n; j++)
2532 (*
this)(row_offset+i,col_offset+j) =
2533 (*
this)(row_offset+j,col_offset+i) = 0.0;
2535 for (i = 0; i < n; i++)
2537 (*this)(row_offset+i,col_offset+i) = c;
2546 for (i = 0; i < n; i++)
2547 for (j = i+1; j < n; j++)
2548 (*
this)(row_offset+i,col_offset+j) =
2549 (*
this)(row_offset+j,col_offset+i) = 0.0;
2551 for (i = 0; i < n; i++)
2553 (*this)(row_offset+i,col_offset+i) = diag[i];
2567 if (co+aw >
Width() || ro+ah > h)
2573 p = data + ro + co * h;
2576 for (
int c = 0; c < aw; c++)
2578 for (
int r = 0; r < ah; r++)
2597 if (co+aw >
Width() || ro+ah > h)
2603 p = data + ro + co * h;
2606 for (
int c = 0; c < aw; c++)
2608 for (
int r = 0; r < ah; r++)
2620 double *vdata = v.
GetData() + offset;
2622 for (i = 0; i < n; i++)
2624 vdata[i] += data[i];
2631 const double *vdata = v.
GetData() + offset;
2633 for (i = 0; i < n; i++)
2646 mfem_error(
"DenseMatrix::AdjustDofDirection(...)");
2651 for (
int i = 0; i < n-1; i++)
2653 int s = (dof[i] < 0) ? (-1) : (1);
2654 for (
int j = i+1; j < n; j++)
2656 int t = (dof[j] < 0) ? (-s) : (s);
2659 (*this)(i,j) = -(*
this)(i,j);
2660 (*this)(j,i) = -(*
this)(j,i);
2668 for (
int j = 0; j <
Width(); j++)
2670 (*this)(row, j) = value;
2676 for (
int i = 0; i <
Height(); i++)
2678 (*this)(i, col) = value;
2684 for (
int j = 0; j <
Width(); j++)
2686 (*this)(r, j) = row[j];
2692 for (
int i = 0; i <
Height(); i++)
2694 (*this)(i, c) = col[i];
2700 for (
int col = 0; col <
Width(); col++)
2702 for (
int row = 0; row <
Height(); row++)
2704 if (std::abs(
operator()(row,col)) <= eps)
2715 ios::fmtflags old_flags = out.flags();
2717 out << setiosflags(ios::scientific | ios::showpos);
2718 for (
int i = 0; i <
height; i++)
2720 out <<
"[row " << i <<
"]\n";
2721 for (
int j = 0; j <
width; j++)
2723 out << (*this)(i,j);
2724 if (j+1 == width || (j+1) % width_ == 0)
2735 out.flags(old_flags);
2741 ios::fmtflags old_flags = out.flags();
2743 out << setiosflags(ios::scientific | ios::showpos);
2744 for (
int i = 0; i <
height; i++)
2746 for (
int j = 0; j <
width; j++)
2748 out << (*this)(i,j);
2754 out.flags(old_flags);
2760 ios::fmtflags old_flags = out.flags();
2762 out << setiosflags(ios::scientific | ios::showpos);
2763 for (
int j = 0; j <
width; j++)
2765 out <<
"[col " << j <<
"]\n";
2766 for (
int i = 0; i <
height; i++)
2768 out << (*this)(i,j);
2769 if (i+1 == height || (i+1) % width_ == 0)
2780 out.flags(old_flags);
2789 for (
int i = 0; i <
width; i++)
2793 cout <<
"size = " << width <<
", i_max = " << C.
MaxMaxNorm()
2794 <<
", cond_F = " <<
FNorm()*copy.FNorm() << endl;
2810 for (
int j = 0; j < C.
Width(); j++)
2811 for (
int i = 0; i < C.
Height(); i++)
2813 C(i,j) = A(i,j) + alpha * B(i,j);
2820 for (
int j = 0; j < C.
Width(); j++)
2821 for (
int i = 0; i < C.
Height(); i++)
2823 C(i,j) = alpha * A(i,j) + beta * B(i,j);
2828 #ifdef MFEM_USE_LAPACK
2830 dgemm_(
char *,
char *,
int *,
int *,
int *,
double *,
double *,
2831 int *,
double *,
int *,
double *,
double *,
int *);
2837 b.
Width() == c.
Height(),
"incompatible dimensions");
2839 #ifdef MFEM_USE_LAPACK
2840 static char transa =
'N', transb =
'N';
2841 static double alpha = 1.0, beta = 0.0;
2844 dgemm_(&transa, &transb, &m, &n, &k, &alpha, b.
Data(), &m,
2845 c.
Data(), &k, &beta, a.
Data(), &m);
2847 const int ah = a.
Height();
2848 const int aw = a.
Width();
2849 const int bw = b.
Width();
2850 double *ad = a.
Data();
2851 const double *bd = b.
Data();
2852 const double *cd = c.
Data();
2853 for (
int i = 0; i < ah*aw; i++)
2857 for (
int j = 0; j < aw; j++)
2859 for (
int k = 0; k < bw; k++)
2861 for (
int i = 0; i < ah; i++)
2863 ad[i+j*ah] += bd[i+k*ah] * cd[k+j*bw];
2873 b.
Width() == c.
Height(),
"incompatible dimensions");
2875 #ifdef MFEM_USE_LAPACK
2876 static char transa =
'N', transb =
'N';
2877 static double alpha = 1.0, beta = 1.0;
2880 dgemm_(&transa, &transb, &m, &n, &k, &alpha, b.
Data(), &m,
2881 c.
Data(), &k, &beta, a.
Data(), &m);
2883 const int ah = a.
Height();
2884 const int aw = a.
Width();
2885 const int bw = b.
Width();
2886 double *ad = a.
Data();
2887 const double *bd = b.
Data();
2888 const double *cd = c.
Data();
2889 for (
int j = 0; j < aw; j++)
2891 for (
int k = 0; k < bw; k++)
2893 for (
int i = 0; i < ah; i++)
2895 ad[i+j*ah] += bd[i+k*ah] * cd[k+j*bw];
2917 const double *d = a.
Data();
2918 double *ad = adja.
Data();
2933 e = d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
2934 g = d[3]*d[3] + d[4]*d[4] + d[5]*d[5];
2935 f = d[0]*d[3] + d[1]*d[4] + d[2]*d[5];
2937 ad[0] = d[0]*g - d[3]*f;
2938 ad[1] = d[3]*e - d[0]*f;
2939 ad[2] = d[1]*g - d[4]*f;
2940 ad[3] = d[4]*e - d[1]*f;
2941 ad[4] = d[2]*g - d[5]*f;
2942 ad[5] = d[5]*e - d[2]*f;
2951 else if (a.
Width() == 2)
2954 adja(0,1) = -a(0,1);
2955 adja(1,0) = -a(1,0);
2960 adja(0,0) = a(1,1)*a(2,2)-a(1,2)*a(2,1);
2961 adja(0,1) = a(0,2)*a(2,1)-a(0,1)*a(2,2);
2962 adja(0,2) = a(0,1)*a(1,2)-a(0,2)*a(1,1);
2964 adja(1,0) = a(1,2)*a(2,0)-a(1,0)*a(2,2);
2965 adja(1,1) = a(0,0)*a(2,2)-a(0,2)*a(2,0);
2966 adja(1,2) = a(0,2)*a(1,0)-a(0,0)*a(1,2);
2968 adja(2,0) = a(1,0)*a(2,1)-a(1,1)*a(2,0);
2969 adja(2,1) = a(0,1)*a(2,0)-a(0,0)*a(2,1);
2970 adja(2,2) = a(0,0)*a(1,1)-a(0,1)*a(1,0);
2987 else if (a.
Width() == 2)
2989 adjat(0,0) = a(1,1);
2990 adjat(1,0) = -a(0,1);
2991 adjat(0,1) = -a(1,0);
2992 adjat(1,1) = a(0,0);
2996 adjat(0,0) = a(1,1)*a(2,2)-a(1,2)*a(2,1);
2997 adjat(1,0) = a(0,2)*a(2,1)-a(0,1)*a(2,2);
2998 adjat(2,0) = a(0,1)*a(1,2)-a(0,2)*a(1,1);
3000 adjat(0,1) = a(1,2)*a(2,0)-a(1,0)*a(2,2);
3001 adjat(1,1) = a(0,0)*a(2,2)-a(0,2)*a(2,0);
3002 adjat(2,1) = a(0,2)*a(1,0)-a(0,0)*a(1,2);
3004 adjat(0,2) = a(1,0)*a(2,1)-a(1,1)*a(2,0);
3005 adjat(1,2) = a(0,1)*a(2,0)-a(0,0)*a(2,1);
3006 adjat(2,2) = a(0,0)*a(1,1)-a(0,1)*a(1,0);
3013 MFEM_ASSERT(inva.
Height() == a.
Width(),
"incorrect dimensions");
3014 MFEM_ASSERT(inva.
Width() == a.
Height(),
"incorrect dimensions");
3020 const double *d = a.
Data();
3021 double *
id = inva.
Data();
3024 t = 1.0 / (d[0]*d[0] + d[1]*d[1]);
3032 t = 1.0 / (d[0]*d[0] + d[1]*d[1] + d[2]*d[2]);
3040 e = d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
3041 g = d[3]*d[3] + d[4]*d[4] + d[5]*d[5];
3042 f = d[0]*d[3] + d[1]*d[4] + d[2]*d[5];
3043 t = 1.0 / (e*g - f*f);
3044 e *= t; g *= t; f *= t;
3046 id[0] = d[0]*g - d[3]*f;
3047 id[1] = d[3]*e - d[0]*f;
3048 id[2] = d[1]*g - d[4]*f;
3049 id[3] = d[4]*e - d[1]*f;
3050 id[4] = d[2]*g - d[5]*f;
3051 id[5] = d[5]*e - d[2]*f;
3059 MFEM_ASSERT(std::abs(t) > 1.0e-14 * pow(a.
FNorm()/a.
Width(), a.
Width()),
3060 "singular matrix!");
3072 inva(0,0) = a(1,1) * t ;
3073 inva(0,1) = -a(0,1) * t ;
3074 inva(1,0) = -a(1,0) * t ;
3075 inva(1,1) = a(0,0) * t ;
3078 inva(0,0) = (a(1,1)*a(2,2)-a(1,2)*a(2,1))*t;
3079 inva(0,1) = (a(0,2)*a(2,1)-a(0,1)*a(2,2))*t;
3080 inva(0,2) = (a(0,1)*a(1,2)-a(0,2)*a(1,1))*t;
3082 inva(1,0) = (a(1,2)*a(2,0)-a(1,0)*a(2,2))*t;
3083 inva(1,1) = (a(0,0)*a(2,2)-a(0,2)*a(2,0))*t;
3084 inva(1,2) = (a(0,2)*a(1,0)-a(0,0)*a(1,2))*t;
3086 inva(2,0) = (a(1,0)*a(2,1)-a(1,1)*a(2,0))*t;
3087 inva(2,1) = (a(0,1)*a(2,0)-a(0,0)*a(2,1))*t;
3088 inva(2,2) = (a(0,0)*a(1,1)-a(0,1)*a(1,0))*t;
3103 double t = 1. / a.
Det() ;
3108 inva(0,0) = 1.0 / a(0,0);
3111 inva(0,0) = a(1,1) * t ;
3112 inva(1,0) = -a(0,1) * t ;
3113 inva(0,1) = -a(1,0) * t ;
3114 inva(1,1) = a(0,0) * t ;
3117 inva(0,0) = (a(1,1)*a(2,2)-a(1,2)*a(2,1))*t;
3118 inva(1,0) = (a(0,2)*a(2,1)-a(0,1)*a(2,2))*t;
3119 inva(2,0) = (a(0,1)*a(1,2)-a(0,2)*a(1,1))*t;
3121 inva(0,1) = (a(1,2)*a(2,0)-a(1,0)*a(2,2))*t;
3122 inva(1,1) = (a(0,0)*a(2,2)-a(0,2)*a(2,0))*t;
3123 inva(2,1) = (a(0,2)*a(1,0)-a(0,0)*a(1,2))*t;
3125 inva(0,2) = (a(1,0)*a(2,1)-a(1,1)*a(2,0))*t;
3126 inva(1,2) = (a(0,1)*a(2,0)-a(0,0)*a(2,1))*t;
3127 inva(2,2) = (a(0,0)*a(1,1)-a(0,1)*a(1,0))*t;
3137 "Matrix must be 3x2 or 2x1, "
3138 <<
"and the Vector must be sized with the rows. "
3139 <<
" J.Height() = " << J.
Height()
3140 <<
", J.Width() = " << J.
Width()
3141 <<
", n.Size() = " << n.
Size()
3144 const double *d = J.
Data();
3152 n(0) = d[1]*d[5] - d[2]*d[4];
3153 n(1) = d[2]*d[3] - d[0]*d[5];
3154 n(2) = d[0]*d[4] - d[1]*d[3];
3160 for (
int i = 0; i < a.
Height(); i++)
3161 for (
int j = 0; j <= i; j++)
3164 for (
int k = 0; k < a.
Width(); k++)
3166 temp += a(i,k) * a(j,k);
3168 aat(j,i) = aat(i,j) = temp;
3174 for (
int i = 0; i < A.
Height(); i++)
3176 for (
int j = 0; j < i; j++)
3179 for (
int k = 0; k < A.
Width(); k++)
3181 t += D(k) * A(i, k) * A(j, k);
3189 for (
int i = 0; i < A.
Height(); i++)
3192 for (
int k = 0; k < A.
Width(); k++)
3194 t += D(k) * A(i, k) * A(i, k);
3202 for (
int i = 0; i < A.
Height(); i++)
3204 for (
int j = 0; j <= i; j++)
3207 for (
int k = 0; k < A.
Width(); k++)
3209 t += D(k) * A(i, k) * A(j, k);
3211 ADAt(j, i) = ADAt(i, j) = t;
3226 #ifdef MFEM_USE_LAPACK
3227 static char transa =
'N', transb =
'T';
3228 static double alpha = 1.0, beta = 0.0;
3231 dgemm_(&transa, &transb, &m, &n, &k, &alpha, A.
Data(), &m,
3232 B.
Data(), &n, &beta, ABt.
Data(), &m);
3234 const int ah = A.
Height();
3235 const int bh = B.
Height();
3236 const int aw = A.
Width();
3237 const double *ad = A.
Data();
3238 const double *bd = B.
Data();
3239 double *cd = ABt.
Data();
3241 for (
int i = 0, s = ah*bh; i < s; i++)
3245 for (
int k = 0; k < aw; k++)
3248 for (
int j = 0; j < bh; j++)
3250 const double bjk = bd[j];
3251 for (
int i = 0; i < ah; i++)
3253 cp[i] += ad[i] * bjk;
3261 const int ah = A.
Height();
3262 const int bh = B.
Height();
3263 const int aw = A.
Width();
3264 const double *ad = A.
Data();
3265 const double *bd = B.
Data();
3266 double *cd = ABt.
Data();
3268 for (
int j = 0; j < bh; j++)
3269 for (
int i = 0; i < ah; i++)
3272 const double *ap = ad + i;
3273 const double *bp = bd + j;
3274 for (
int k = 0; k < aw; k++)
3286 for (i = 0; i < A.
Height(); i++)
3287 for (j = 0; j < B.
Height(); j++)
3290 for (k = 0; k < A.
Width(); k++)
3292 d += A(i, k) * B(j, k);
3310 const int ah = A.
Height();
3311 const int bh = B.
Height();
3312 const int aw = A.
Width();
3313 const double *ad = A.
Data();
3314 const double *bd = B.
Data();
3315 const double *dd = D.
GetData();
3316 double *cd = ADBt.
Data();
3318 for (
int i = 0, s = ah*bh; i < s; i++)
3322 for (
int k = 0; k < aw; k++)
3325 for (
int j = 0; j < bh; j++)
3327 const double dk_bjk = dd[k] * bd[j];
3328 for (
int i = 0; i < ah; i++)
3330 cp[i] += ad[i] * dk_bjk;
3349 #ifdef MFEM_USE_LAPACK
3350 static char transa =
'N', transb =
'T';
3351 static double alpha = 1.0, beta = 1.0;
3354 dgemm_(&transa, &transb, &m, &n, &k, &alpha, A.
Data(), &m,
3355 B.
Data(), &n, &beta, ABt.
Data(), &m);
3357 const int ah = A.
Height();
3358 const int bh = B.
Height();
3359 const int aw = A.
Width();
3360 const double *ad = A.
Data();
3361 const double *bd = B.
Data();
3362 double *cd = ABt.
Data();
3364 for (
int k = 0; k < aw; k++)
3367 for (
int j = 0; j < bh; j++)
3369 const double bjk = bd[j];
3370 for (
int i = 0; i < ah; i++)
3372 cp[i] += ad[i] * bjk;
3383 for (i = 0; i < A.
Height(); i++)
3384 for (j = 0; j < B.
Height(); j++)
3387 for (k = 0; k < A.
Width(); k++)
3389 d += A(i, k) * B(j, k);
3407 const int ah = A.
Height();
3408 const int bh = B.
Height();
3409 const int aw = A.
Width();
3410 const double *ad = A.
Data();
3411 const double *bd = B.
Data();
3412 const double *dd = D.
GetData();
3413 double *cd = ADBt.
Data();
3415 for (
int k = 0; k < aw; k++)
3418 for (
int j = 0; j < bh; j++)
3420 const double dk_bjk = dd[k] * bd[j];
3421 for (
int i = 0; i < ah; i++)
3423 cp[i] += ad[i] * dk_bjk;
3443 #ifdef MFEM_USE_LAPACK
3444 static char transa =
'N', transb =
'T';
3446 static double beta = 1.0;
3449 dgemm_(&transa, &transb, &m, &n, &k, &alpha, A.
Data(), &m,
3450 B.
Data(), &n, &beta, ABt.
Data(), &m);
3452 const int ah = A.
Height();
3453 const int bh = B.
Height();
3454 const int aw = A.
Width();
3455 const double *ad = A.
Data();
3456 const double *bd = B.
Data();
3457 double *cd = ABt.
Data();
3459 for (
int k = 0; k < aw; k++)
3462 for (
int j = 0; j < bh; j++)
3464 const double bjk = a * bd[j];
3465 for (
int i = 0; i < ah; i++)
3467 cp[i] += ad[i] * bjk;
3478 for (i = 0; i < A.
Height(); i++)
3479 for (j = 0; j < B.
Height(); j++)
3482 for (k = 0; k < A.
Width(); k++)
3484 d += A(i, k) * B(j, k);
3501 #ifdef MFEM_USE_LAPACK
3502 static char transa =
'T', transb =
'N';
3503 static double alpha = 1.0, beta = 0.0;
3506 dgemm_(&transa, &transb, &m, &n, &k, &alpha, A.
Data(), &k,
3507 B.
Data(), &k, &beta, AtB.
Data(), &m);
3509 const int ah = A.
Height();
3510 const int aw = A.
Width();
3511 const int bw = B.
Width();
3512 const double *ad = A.
Data();
3513 const double *bd = B.
Data();
3514 double *cd = AtB.
Data();
3516 for (
int j = 0; j < bw; j++)
3518 const double *ap = ad;
3519 for (
int i = 0; i < aw; i++)
3522 for (
int k = 0; k < ah; k++)
3535 for (i = 0; i < A.
Width(); i++)
3536 for (j = 0; j < B.
Width(); j++)
3539 for (k = 0; k < A.
Height(); k++)
3541 d += A(k, i) * B(k, j);
3552 for (
int i = 0; i < A.
Height(); i++)
3554 for (
int j = 0; j < i; j++)
3557 for (
int k = 0; k < A.
Width(); k++)
3559 d += A(i,k) * A(j,k);
3561 AAt(i, j) += (d *= a);
3565 for (
int k = 0; k < A.
Width(); k++)
3567 d += A(i,k) * A(i,k);
3575 for (
int i = 0; i < A.
Height(); i++)
3576 for (
int j = 0; j <= i; j++)
3579 for (
int k = 0; k < A.
Width(); k++)
3581 d += A(i,k) * A(j,k);
3583 AAt(i, j) = AAt(j, i) = a * d;
3589 for (
int i = 0; i < v.
Size(); i++)
3590 for (
int j = 0; j <= i; j++)
3592 vvt(i,j) = vvt(j,i) = v(i) * v(j);
3608 for (i = 0; i < v.
Size(); i++)
3611 for (j = 0; j < w.
Size(); j++)
3613 VWt(i, j) = vi * w(j);
3629 for (
int i = 0; i < m; i++)
3632 for (
int j = 0; j < n; j++)
3634 VWt(i, j) += vi * w(j);
3651 for (
int j = 0; j < n; j++)
3653 const double awj = a * w(j);
3654 for (
int i = 0; i < m; i++)
3656 VWt(i, j) += v(i) * awj;
3672 for (
int i = 0; i < n; i++)
3674 double avi = a * v(i);
3675 for (
int j = 0; j < i; j++)
3677 double avivj = avi * v(j);
3681 VVt(i, i) += avi * v(i);
3688 #ifdef MFEM_USE_LAPACK
3691 MFEM_VERIFY(!info,
"LAPACK: error in DGETRF");
3695 for (
int i = 0; i < m; i++)
3700 double a = std::abs(data[piv+i*m]);
3701 for (
int j = i+1; j < m; j++)
3703 const double b = std::abs(data[j+i*m]);
3714 for (
int j = 0; j < m; j++)
3716 Swap<double>(data[i+j*m], data[piv+j*m]);
3720 MFEM_ASSERT(data[i+i*m] != 0.0,
"division by zero");
3721 const double a_ii_inv = 1.0/data[i+i*m];
3722 for (
int j = i+1; j < m; j++)
3724 data[j+i*m] *= a_ii_inv;
3726 for (
int k = i+1; k < m; k++)
3728 const double a_ik = data[i+k*m];
3729 for (
int j = i+1; j < m; j++)
3731 data[j+k*m] -= a_ik * data[j+i*m];
3743 for (
int k = 0; k < n; k++)
3746 for (
int i = 0; i < m; i++)
3748 double x_i = x[i] * data[i+i*m];
3749 for (
int j = i+1; j < m; j++)
3751 x_i += x[j] * data[i+j*m];
3756 for (
int i = m-1; i >= 0; i--)
3759 for (
int j = 0; j < i; j++)
3761 x_i += x[j] * data[i+j*m];
3766 for (
int i = m-1; i >= 0; i--)
3768 Swap<double>(x[i], x[ipiv[i]-
ipiv_base]);
3779 for (
int k = 0; k < n; k++)
3782 for (
int i = 0; i < m; i++)
3784 Swap<double>(x[i], x[ipiv[i]-
ipiv_base]);
3787 for (
int j = 0; j < m; j++)
3789 const double x_j = x[j];
3790 for (
int i = j+1; i < m; i++)
3792 x[i] -= data[i+j*m] * x_j;
3804 for (
int k = 0; k < n; k++)
3806 for (
int j = m-1; j >= 0; j--)
3808 const double x_j = ( x[j] /= data[j+j*m] );
3809 for (
int i = 0; i < j; i++)
3811 x[i] -= data[i+j*m] * x_j;
3820 #ifdef MFEM_USE_LAPACK
3823 if (m > 0 && n > 0) {
dgetrs_(&trans, &m, &n,
data, &m,
ipiv, X, &m, &info); }
3824 MFEM_VERIFY(!info,
"LAPACK: error in DGETRS");
3839 for (
int k = 0; k < m; k++)
3841 const double minus_x_k = -( x[k] = 1.0/data[k+k*m] );
3842 for (
int i = 0; i < k; i++)
3844 x[i] = data[i+k*m] * minus_x_k;
3846 for (
int j = k-1; j >= 0; j--)
3848 const double x_j = ( x[j] /= data[j+j*m] );
3849 for (
int i = 0; i < j; i++)
3851 x[i] -= data[i+j*m] * x_j;
3859 for (
int j = 0; j < k; j++)
3861 const double minus_L_kj = -data[k+j*m];
3862 for (
int i = 0; i <= j; i++)
3864 X[i+j*m] += X[i+k*m] * minus_L_kj;
3866 for (
int i = j+1; i < m; i++)
3868 X[i+j*m] = X[i+k*m] * minus_L_kj;
3872 for (
int k = m-2; k >= 0; k--)
3874 for (
int j = 0; j < k; j++)
3876 const double L_kj = data[k+j*m];
3877 for (
int i = 0; i < m; i++)
3879 X[i+j*m] -= X[i+k*m] * L_kj;
3884 for (
int k = m-1; k >= 0; k--)
3889 for (
int i = 0; i < m; i++)
3891 Swap<double>(X[i+k*m], X[i+piv_k*m]);
3898 const double *X1,
double *X2)
3901 for (
int k = 0; k < r; k++)
3903 for (
int j = 0; j < m; j++)
3905 const double x1_jk = X1[j+k*m];
3906 for (
int i = 0; i < n; i++)
3908 X2[i+k*n] -= A21[i+j*n] * x1_jk;
3915 int m,
int n,
double *A12,
double *A21,
double *A22)
const
3921 for (
int j = 0; j < m; j++)
3923 const double u_jj_inv = 1.0/data[j+j*m];
3924 for (
int i = 0; i < n; i++)
3926 A21[i+j*n] *= u_jj_inv;
3928 for (
int k = j+1; k < m; k++)
3930 const double u_jk = data[j+k*m];
3931 for (
int i = 0; i < n; i++)
3933 A21[i+k*n] -= A21[i+j*n] * u_jk;
3938 SubMult(m, n, n, A21, A12, A22);
3942 double *B1,
double *B2)
const
3947 SubMult(m, n, r, L21, B1, B2);
3951 const double *X2,
double *Y1)
const
3954 SubMult(n, m, r, U12, X2, Y1);
3963 MFEM_ASSERT(
height ==
width,
"not a square matrix");
3973 MFEM_ASSERT(
height ==
width,
"not a square matrix");
3981 MFEM_ASSERT(a,
"DenseMatrix is not given");
3982 const double *adata = a->data;
3985 lu.
data[i] = adata[i];
3992 MFEM_VERIFY(mat.
height == mat.
width,
"DenseMatrix is not square!");
4008 MFEM_VERIFY(p != NULL,
"Operator is not a DenseMatrix!");
4028 for (
int i = 0; i <
width; i++)
4032 cout <<
"size = " << width <<
", i_max = " << C.
MaxMaxNorm() << endl;
4050 #ifdef MFEM_USE_LAPACK
4056 &qwork, &lwork, &info);
4058 lwork = (int) qwork;
4059 work =
new double[lwork];
4066 if (mat.
Width() != n)
4068 mfem_error(
"DenseMatrixEigensystem::Eval()");
4072 #ifdef MFEM_USE_LAPACK
4075 work, &lwork, &info);
4079 cerr <<
"DenseMatrixEigensystem::Eval(): DSYEV error code: "
4084 mfem_error(
"DenseMatrixEigensystem::Eval(): Compiled without LAPACK");
4090 #ifdef MFEM_USE_LAPACK
4110 void DenseMatrixSVD::Init()
4112 #ifdef MFEM_USE_LAPACK
4121 NULL, &n, &qwork, &lwork, &info);
4123 lwork = (int) qwork;
4124 work =
new double[lwork];
4126 mfem_error(
"DenseMatrixSVD::Init(): Compiled without LAPACK");
4139 #ifdef MFEM_USE_LAPACK
4141 NULL, &n, work, &lwork, &info);
4145 cerr <<
"DenseMatrixSVD::Eval() : info = " << info << endl;
4149 mfem_error(
"DenseMatrixSVD::Eval(): Compiled without LAPACK");
4155 #ifdef MFEM_USE_LAPACK
4165 const int *I = elem_dof.
GetI(), *J = elem_dof.
GetJ(), *dofs;
4166 double *d_col = tdata, *yp = y, x_col;
4167 const double *xp = x;
4171 for (
int i = 0; i < ne; i++)
4174 for (
int col = 0; col < n; col++)
4176 x_col = xp[dofs[col]];
4177 for (
int row = 0; row < n; row++)
4179 yp[dofs[row]] += x_col*d_col[row];
4188 for (
int i = 0; i < ne; i++)
4191 x_col = xp[dofs[0]];
4192 for (
int row = 0; row < n; row++)
4194 ye(row) = x_col*d_col[row];
4197 for (
int col = 1; col < n; col++)
4199 x_col = xp[dofs[col]];
4200 for (
int row = 0; row < n; row++)
4202 ye(row) += x_col*d_col[row];
4206 for (
int row = 0; row < n; row++)
4208 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))
int CheckFinite(const double *v, const int n)
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 to size s.
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 AddMult_a_ABt(double a, const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt)
ABt += a * A * B^t.
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 MultADBt(const DenseMatrix &A, const Vector &D, const DenseMatrix &B, DenseMatrix &ADBt)
ADBt = A D B^t, where D is diagonal.
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 the matrix data array.
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)
Set the Vector data and size.
DenseMatrix & operator-=(DenseMatrix &m)
void MultADAt(const DenseMatrix &A, const Vector &D, DenseMatrix &ADAt)
ADAt = A D A^t, where D is diagonal.
int height
Dimension of the output / number of rows in the matrix.
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.
void AddMultADBt(const DenseMatrix &A, const Vector &D, const DenseMatrix &B, DenseMatrix &ADBt)
ADBt = A D B^t, where D is diagonal.
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
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.
int width
Dimension of the input / number of columns in the matrix.