12 #ifndef MFEM_LINALG_KERNELS_HPP 13 #define MFEM_LINALG_KERNELS_HPP 15 #include "../config/config.hpp" 16 #include "../general/backends.hpp" 17 #include "../general/globals.hpp" 43 for (
int i = 0; i <
dim; i++) { d += (x[i]-y[i])*(x[i]-y[i]); }
49 MFEM_HOST_DEVICE
inline void Diag(
const double c,
double *data)
52 for (
int i = 0; i < N; i++) { data[i] = 0.0; }
53 for (
int i = 0; i <
dim; i++) { data[i*(
dim+1)] = c; }
58 MFEM_HOST_DEVICE
inline void Subtract(
const double a,
59 const double *x,
const double *y,
62 for (
int i = 0; i <
dim; i++) { z[i] =
a * (x[i] - y[i]); }
67 MFEM_HOST_DEVICE
inline void AddMultVWt(
const double *v,
const double *w,
70 for (
int i = 0; i <
dim; i++)
72 const double vi = v[i];
73 for (
int j = 0; j <
dim; j++) { VWt[i*
dim+j] += vi * w[j]; }
77 template<
int H,
int W,
typename T>
78 MFEM_HOST_DEVICE
inline 79 void FNorm(
double &scale_factor,
double &scaled_fnorm2,
const T *data)
82 T max_norm = 0.0, entry, fnorm2;
84 for (i = 0; i < hw; i++)
86 entry = fabs(data[i]);
95 scale_factor = scaled_fnorm2 = 0.0;
100 for (i = 0; i < hw; i++)
102 entry = data[i] / max_norm;
103 fnorm2 += entry * entry;
106 scale_factor = max_norm;
107 scaled_fnorm2 = fnorm2;
111 template<
int H,
int W,
typename T>
112 MFEM_HOST_DEVICE
inline 116 kernels::FNorm<H,W>(
s, n2, data);
121 template<
int H,
int W,
typename T>
122 MFEM_HOST_DEVICE
inline 126 kernels::FNorm<H,W>(
s, n2, data);
132 MFEM_HOST_DEVICE
inline 133 double Norml2(
const int size,
const T *data)
135 if (0 == size) {
return 0.0; }
136 if (1 == size) {
return std::abs(data[0]); }
139 for (
int i = 0; i < size; i++)
143 const T absdata = fabs(data[i]);
144 if (scale <= absdata)
146 const T sqr_arg = scale / absdata;
147 sum = 1.0 + sum * (sqr_arg * sqr_arg);
151 const T sqr_arg = absdata / scale;
152 sum += (sqr_arg * sqr_arg);
155 return scale * sqrt(sum);
161 template<
typename TA,
typename TX,
typename TY>
162 MFEM_HOST_DEVICE
inline 163 void Mult(
const int height,
const int width,
const TA *data,
const TX *x, TY *y)
167 for (
int row = 0; row < height; row++)
173 const TA *d_col = data;
175 for (
int row = 0; row < height; row++)
177 y[row] = x_col*d_col[row];
180 for (
int col = 1; col < width; col++)
183 for (
int row = 0; row < height; row++)
185 y[row] += x_col*d_col[row];
194 template<
typename TA,
typename TX,
typename TY>
195 MFEM_HOST_DEVICE
inline 201 for (
int row = 0; row < width; row++)
208 for (
int i = 0; i < width; ++i)
211 for (
int j = 0; j < height; ++j)
213 val += x[j] * data[i * height + j];
222 MFEM_HOST_DEVICE
inline 225 for (
int i = 0; i < size; i++)
227 for (
int j = 0; j < i; j++)
229 const T
a = 0.5 * (data[i*size+j] + data[j*size+i]);
230 data[j*size+i] = data[i*size+j] =
a;
236 template<
int dim,
typename T>
237 MFEM_HOST_DEVICE
inline T
Det(
const T *data)
244 template<
int dim,
typename T>
245 MFEM_HOST_DEVICE
inline 249 const T det = TAdjDetHD<T>(layout_t(), data, layout_t(), inv_data);
250 TAssignHD<AssignOp::Mult>(layout_t(), inv_data,
static_cast<T
>(1.0)/det);
254 template<
int dim,
typename T>
255 MFEM_HOST_DEVICE
inline 259 TAdjugateHD<T>(layout_t(), data, layout_t(), adj_data);
264 template<
typename TALPHA,
typename TA,
typename TB,
typename TC>
265 MFEM_HOST_DEVICE
inline 266 void Add(
const int height,
const int width,
const TALPHA
alpha,
267 const TA *Adata,
const TB *Bdata, TC *Cdata)
269 for (
int j = 0; j < width; j++)
271 for (
int i = 0; i < height; i++)
273 const int n = i*width+j;
274 Cdata[n] = Adata[n] +
alpha * Bdata[n];
281 template<
typename TALPHA,
typename TBETA,
typename TA,
typename TB,
typename TC>
282 MFEM_HOST_DEVICE
inline 283 void Add(
const int height,
const int width,
284 const TALPHA
alpha,
const TA *Adata,
285 const TBETA
beta,
const TB *Bdata,
288 const int m = height * width;
289 for (
int i = 0; i < m; i++)
291 Cdata[i] =
alpha * Adata[i] +
beta * Bdata[i];
297 template<
typename TA,
typename TB>
298 MFEM_HOST_DEVICE
inline 299 void Add(
const int height,
const int width,
const TA *Adata, TB *Bdata)
301 const int m = height * width;
302 for (
int i = 0; i < m; i++)
304 Bdata[i] += Adata[i];
310 template<
typename TA,
typename TB>
311 MFEM_HOST_DEVICE
inline 312 void Add(
const int height,
const int width,
313 const double alpha,
const TA *Adata, TB *Bdata)
315 const int m = height * width;
316 for (
int i = 0; i < m; i++)
318 Bdata[i] +=
alpha * Adata[i];
324 template<
typename TA,
typename TB>
325 MFEM_HOST_DEVICE
inline 326 void Set(
const int height,
const int width,
327 const double alpha,
const TA *Adata, TB *Bdata)
329 const int m = height * width;
330 for (
int i = 0; i < m; i++)
332 Bdata[i] =
alpha * Adata[i];
339 template<
typename TA,
typename TB,
typename TC>
340 MFEM_HOST_DEVICE
inline 341 void Mult(
const int Aheight,
const int Awidth,
const int Bwidth,
342 const TB *Bdata,
const TC *Cdata, TA *Adata)
344 const int ah_x_aw = Aheight * Awidth;
345 for (
int i = 0; i < ah_x_aw; i++) { Adata[i] = 0.0; }
346 for (
int j = 0; j < Awidth; j++)
348 for (
int k = 0; k < Bwidth; k++)
350 for (
int i = 0; i < Aheight; i++)
352 Adata[i+j*Aheight] += Bdata[i+k*Aheight] * Cdata[k+j*Bwidth];
361 template<
typename TA,
typename TB,
typename TC>
362 MFEM_HOST_DEVICE
inline 363 void MultABt(
const int Aheight,
const int Awidth,
const int Bheight,
364 const TA *Adata,
const TB *Bdata, TC *ABtdata)
366 const int ah_x_bh = Aheight * Bheight;
367 for (
int i = 0; i < ah_x_bh; i++) { ABtdata[i] = 0.0; }
368 for (
int k = 0; k < Awidth; k++)
371 for (
int j = 0; j < Bheight; j++)
373 const double bjk = Bdata[j];
374 for (
int i = 0; i < Aheight; i++)
376 c[i] += Adata[i] * bjk;
388 template<
typename TA,
typename TB,
typename TC>
389 MFEM_HOST_DEVICE
inline 390 void MultAtB(
const int Aheight,
const int Awidth,
const int Bwidth,
391 const TA *Adata,
const TB *Bdata, TC *AtBdata)
394 for (
int i = 0; i < Bwidth; ++i)
396 for (
int j = 0; j < Awidth; ++j)
399 for (
int k = 0; k < Aheight; ++k)
401 val += Adata[j * Aheight + k] * Bdata[i * Aheight + k];
410 template<
int HEIGHT,
int WIDTH> MFEM_HOST_DEVICE
416 template<
int dim> MFEM_HOST_DEVICE
420 template<
int dim> MFEM_HOST_DEVICE
430 MFEM_HOST_DEVICE
static inline 441 MFEM_HOST_DEVICE
static inline 442 void Eigenvalues2S(
const double &d12,
double &d1,
double &d2)
444 const double sqrt_1_eps = sqrt(1./Epsilon);
449 const double zeta = (d2 - d1)/(2*d12);
450 if (fabs(zeta) < sqrt_1_eps)
452 t = d12*copysign(1./(fabs(zeta) + sqrt(1. + zeta*zeta)), zeta);
456 t = d12*copysign(0.5/fabs(zeta), zeta);
464 MFEM_HOST_DEVICE
static inline 465 void Eigensystem2S(
const double &d12,
double &d1,
double &d2,
466 double &c,
double &
s)
468 const double sqrt_1_eps = sqrt(1./Epsilon);
478 const double zeta = (d2 - d1)/(2*d12);
479 const double azeta = fabs(zeta);
480 if (azeta < sqrt_1_eps)
482 t = copysign(1./(azeta + sqrt(1. + zeta*zeta)), zeta);
486 t = copysign(0.5/azeta, zeta);
488 c = sqrt(1./(1. +
t*
t));
498 MFEM_HOST_DEVICE
static inline 499 void GetScalingFactor(
const double &d_max,
double &mult)
504 mult = frexp(d_max, &d_exp);
505 if (d_exp == std::numeric_limits<double>::max_exponent)
507 mult *= std::numeric_limits<double>::radix;
520 MFEM_HOST_DEVICE
static inline 521 bool KernelVector2G(
const int &mode,
522 double &d1,
double &d12,
double &d21,
double &d2)
533 double n1 = fabs(d1) + fabs(d21);
534 double n2 = fabs(d2) + fabs(d12);
536 bool swap_columns = (n2 > n1);
548 if (fabs(d1) > fabs(d21))
556 if (fabs(d1) < fabs(d21))
568 if (fabs(d12) > fabs(d2))
581 if (fabs(d12) < fabs(d2))
600 mu = copysign(n1, d1);
601 n1 = -d21*(d21/(d1 +
mu));
605 if (fabs(n1) <= fabs(d21))
609 mu = (2./(1. + n1*n1))*(n1*d12 + d2);
617 mu = (2./(1. + n2*n2))*(d12 + n2*d2);
639 n2 = 1./(1. + fabs(
mu));
641 if (fabs(d1) <= n2*fabs(d2))
662 MFEM_HOST_DEVICE
static inline 663 void Vec_normalize3_aux(
const double &x1,
const double &x2,
665 double &n1,
double &n2,
double &n3)
669 const double m = fabs(x1);
673 t = sqrt(1./(
t + r*r));
674 n1 = copysign(
t, x1);
681 MFEM_HOST_DEVICE
static inline 682 void Vec_normalize3(
const double &x1,
const double &x2,
const double &x3,
683 double &n1,
double &n2,
double &n3)
686 if (fabs(x1) >= fabs(x2))
688 if (fabs(x1) >= fabs(x3))
692 Vec_normalize3_aux(x1, x2, x3, n1, n2, n3);
701 else if (fabs(x2) >= fabs(x3))
703 Vec_normalize3_aux(x2, x1, x3, n2, n1, n3);
706 Vec_normalize3_aux(x3, x1, x2, n3, n1, n2);
710 MFEM_HOST_DEVICE
static inline 711 int KernelVector3G_aux(
const int &mode,
712 double &d1,
double &d2,
double &d3,
713 double &c12,
double &c13,
double &c23,
714 double &c21,
double &c31,
double &c32)
717 double mu, n1, n2, n3, s1, s2, s3;
719 s1 = hypot(c21, c31);
726 mu = copysign(n1, d1);
727 n1 = -s1*(s1/(d1 +
mu));
732 if (fabs(n1) >= fabs(c21))
734 if (fabs(n1) >= fabs(c31))
739 mu = 2./(1. + s2*s2 + s3*s3);
740 n2 =
mu*(c12 + s2*d2 + s3*c32);
741 n3 =
mu*(c13 + s2*c23 + s3*d3);
751 else if (fabs(c21) >= fabs(c31))
756 mu = 2./(1. + s1*s1 + s3*s3);
757 n2 =
mu*(s1*c12 + d2 + s3*c32);
758 n3 =
mu*(s1*c13 + c23 + s3*d3);
770 mu = 2./(1. + s1*s1 + s2*s2);
771 n2 =
mu*(s1*c12 + s2*d2 + c32);
772 n3 =
mu*(s1*c13 + s2*c23 + d3);
786 if (KernelVector2G(mode, d2, c23, c32, d3))
807 d1 = -(c12*d2 + c13*d3)/d1;
811 Vec_normalize3(d1, d2, d3, d1, d2, d3);
817 MFEM_HOST_DEVICE
static inline 818 int KernelVector3S(
const int &mode,
const double &d12,
819 const double &d13,
const double &d23,
820 double &d1,
double &d2,
double &d3)
833 double c12 = d12, c13 = d13, c23 = d23;
834 double c21, c31, c32;
838 c32 = fabs(d1) + fabs(c12) + fabs(c13);
839 c31 = fabs(d2) + fabs(c12) + fabs(c23);
840 c21 = fabs(d3) + fabs(c13) + fabs(c23);
845 col = (c32 >= c31) ? 1 : 2;
849 col = (c31 >= c21) ? 2 : 3;
881 if (fabs(d1) <= fabs(c13))
883 row = (fabs(d1) <= fabs(c12)) ? 1 : 2;
887 row = (fabs(c12) <= fabs(c13)) ? 2 : 3;
892 if (fabs(d1) >= fabs(c13))
894 row = (fabs(d1) >= fabs(c12)) ? 1 : 2;
898 row = (fabs(c12) >= fabs(c13)) ? 2 : 3;
929 row = KernelVector3G_aux(mode, d1, d2, d3, c12, c13, c23, c21, c31, c32);
945 MFEM_HOST_DEVICE
static inline 946 int Reduce3S(
const int &mode,
947 double &d1,
double &d2,
double &d3,
948 double &d12,
double &d13,
double &d23,
949 double &z1,
double &z2,
double &z3,
950 double &v1,
double &v2,
double &v3,
970 double s, w1, w2, w3;
976 if (fabs(z1) <= fabs(z3))
978 k = (fabs(z1) <= fabs(z2)) ? 1 : 2;
982 k = (fabs(z2) <= fabs(z3)) ? 2 : 3;
988 if (fabs(z1) >= fabs(z3))
990 k = (fabs(z1) >= fabs(z2)) ? 1 : 2;
994 k = (fabs(z2) >= fabs(z3)) ? 2 : 3;
1021 g = copysign(1., z1);
1022 v1 = -
s*(
s/(z1 + g));
1025 if (fabs(z2) > g) { g = fabs(z2); }
1026 if (fabs(z3) > g) { g = fabs(z3); }
1030 g = 2./(v1*v1 + v2*v2 + v3*v3);
1035 w1 = g*( d1*v1 + d12*v2 + d13*v3);
1036 w2 = g*(d12*v1 + d2*v2 + d23*v3);
1037 w3 = g*(d13*v1 + d23*v2 + d3*v3);
1039 s = (g/2)*(v1*w1 + v2*w2 + v3*w3);
1046 d23 -= v2*w3 + v3*w2;
1051 s = d12 - v1*w2 - v2*w1;
1052 s = d13 - v1*w3 - v3*w1;
1071 template<> MFEM_HOST_DEVICE
inline 1074 const double t = 1.0 / (d[0]*d[0] + d[1]*d[1]);
1075 left_inv[0] = d[0] *
t;
1076 left_inv[1] = d[1] *
t;
1079 template<> MFEM_HOST_DEVICE
inline 1082 const double t = 1.0 / (d[0]*d[0] + d[1]*d[1] + d[2]*d[2]);
1083 left_inv[0] = d[0] *
t;
1084 left_inv[1] = d[1] *
t;
1085 left_inv[2] = d[2] *
t;
1088 template<> MFEM_HOST_DEVICE
inline 1091 double e = d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
1092 double g = d[3]*d[3] + d[4]*d[4] + d[5]*d[5];
1093 double f = d[0]*d[3] + d[1]*d[4] + d[2]*d[5];
1094 const double t = 1.0 / (e*g -
f*
f);
1095 e *=
t; g *=
t;
f *=
t;
1097 left_inv[0] = d[0]*g - d[3]*
f;
1098 left_inv[1] = d[3]*e - d[0]*
f;
1099 left_inv[2] = d[1]*g - d[4]*
f;
1100 left_inv[3] = d[4]*e - d[1]*
f;
1101 left_inv[4] = d[2]*g - d[5]*
f;
1102 left_inv[5] = d[5]*e - d[2]*
f;
1110 template<> MFEM_HOST_DEVICE
inline 1113 double d0 = data[0];
1114 double d2 = data[2];
1115 double d3 = data[3];
1117 internal::Eigensystem2S(d2, d0, d3, c,
s);
1141 template<> MFEM_HOST_DEVICE
inline 1144 double d11 = data[0];
1145 double d12 = data[3];
1146 double d22 = data[4];
1147 double d13 = data[6];
1148 double d23 = data[7];
1149 double d33 = data[8];
1153 double d_max = fabs(d11);
1154 if (d_max < fabs(d22)) { d_max = fabs(d22); }
1155 if (d_max < fabs(d33)) { d_max = fabs(d33); }
1156 if (d_max < fabs(d12)) { d_max = fabs(d12); }
1157 if (d_max < fabs(d13)) { d_max = fabs(d13); }
1158 if (d_max < fabs(d23)) { d_max = fabs(d23); }
1160 internal::GetScalingFactor(d_max, mult);
1163 d11 /= mult; d22 /= mult; d33 /= mult;
1164 d12 /= mult; d13 /= mult; d23 /= mult;
1166 double aa = (d11 + d22 + d33)/3;
1167 double c1 = d11 - aa;
1168 double c2 = d22 - aa;
1169 double c3 = d33 - aa;
1173 Q = (2*(d12*d12 + d13*d13 + d23*d23) + c1*c1 + c2*c2 + c3*c3)/6;
1174 R = (c1*(d23*d23 - c2*c3)+ d12*(d12*c3 - 2*d13*d23) + d13*d13*c2)/2;
1178 lambda[0] = lambda[1] = lambda[2] = aa;
1179 vec[0] = 1.; vec[3] = 0.; vec[6] = 0.;
1180 vec[1] = 0.; vec[4] = 1.; vec[7] = 0.;
1181 vec[2] = 0.; vec[5] = 0.; vec[8] = 1.;
1185 double sqrtQ = sqrt(Q);
1186 double sqrtQ3 = Q*sqrtQ;
1190 if (fabs(R) >= sqrtQ3)
1209 r = -2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
1213 r = -2*sqrtQ*cos(acos(R)/3);
1237 switch (internal::KernelVector3S(mode, d12, d13, d23, c1, c2, c3))
1241 lambda[0] = lambda[1] = lambda[2] = aa;
1242 vec[0] = 1.; vec[3] = 0.; vec[6] = 0.;
1243 vec[1] = 0.; vec[4] = 1.; vec[7] = 0.;
1244 vec[2] = 0.; vec[5] = 0.; vec[8] = 1.;
1258 double v1, v2, v3, g;
1259 int k = internal::Reduce3S(mode, d11, d22, d33, d12, d13, d23,
1260 c1, c2, c3, v1, v2, v3, g);
1268 internal::Eigensystem2S(d23, d22, d33, c,
s);
1271 double *vec_1, *vec_2, *vec_3;
1276 lambda[0] = d11; vec_1 = vec;
1277 lambda[1] = d22; vec_2 = vec + 3;
1278 lambda[2] = d33; vec_3 = vec + 6;
1280 else if (d11 <= d33)
1282 lambda[0] = d11; vec_1 = vec;
1283 lambda[1] = d33; vec_3 = vec + 3;
1284 lambda[2] = d22; vec_2 = vec + 6;
1288 lambda[0] = d33; vec_3 = vec;
1289 lambda[1] = d11; vec_1 = vec + 3;
1290 lambda[2] = d22; vec_2 = vec + 6;
1297 lambda[0] = d22; vec_2 = vec;
1298 lambda[1] = d11; vec_1 = vec + 3;
1299 lambda[2] = d33; vec_3 = vec + 6;
1301 else if (d22 <= d33)
1303 lambda[0] = d22; vec_2 = vec;
1304 lambda[1] = d33; vec_3 = vec + 3;
1305 lambda[2] = d11; vec_1 = vec + 6;
1309 lambda[0] = d33; vec_3 = vec;
1310 lambda[1] = d22; vec_2 = vec + 3;
1311 lambda[2] = d11; vec_1 = vec + 6;
1318 d22 = g*(v2*c - v3*
s);
1319 d33 = g*(v2*
s + v3*c);
1320 vec_2[0] = - v1*d22; vec_3[0] = - v1*d33;
1321 vec_2[1] = c - v2*d22; vec_3[1] =
s - v2*d33;
1322 vec_2[2] = -
s - v3*d22; vec_3[2] = c - v3*d33;
1326 internal::Swap(vec_2[0], vec_2[1]);
1327 internal::Swap(vec_3[0], vec_3[1]);
1331 internal::Swap(vec_2[0], vec_2[2]);
1332 internal::Swap(vec_3[0], vec_3[2]);
1343 template<> MFEM_HOST_DEVICE
inline 1346 double d0, d1, d2, d3;
1354 double d_max = fabs(d0);
1355 if (d_max < fabs(d1)) { d_max = fabs(d1); }
1356 if (d_max < fabs(d2)) { d_max = fabs(d2); }
1357 if (d_max < fabs(d3)) { d_max = fabs(d3); }
1358 internal::GetScalingFactor(d_max, mult);
1366 double t = 0.5*((d0+d2)*(d0-d2)+(d1-d3)*(d1+d3));
1367 double s = d0*d2 + d1*d3;
1368 s = sqrt(0.5*(d0*d0 + d1*d1 + d2*d2 + d3*d3) + sqrt(
t*
t +
s*
s));
1374 t = fabs(d0*d3 - d1*d2) /
s;
1391 template<> MFEM_HOST_DEVICE
inline 1394 double d0, d1, d2, d3, d4, d5, d6, d7, d8;
1395 d0 = data[0]; d3 = data[3]; d6 = data[6];
1396 d1 = data[1]; d4 = data[4]; d7 = data[7];
1397 d2 = data[2]; d5 = data[5]; d8 = data[8];
1400 double d_max = fabs(d0);
1401 if (d_max < fabs(d1)) { d_max = fabs(d1); }
1402 if (d_max < fabs(d2)) { d_max = fabs(d2); }
1403 if (d_max < fabs(d3)) { d_max = fabs(d3); }
1404 if (d_max < fabs(d4)) { d_max = fabs(d4); }
1405 if (d_max < fabs(d5)) { d_max = fabs(d5); }
1406 if (d_max < fabs(d6)) { d_max = fabs(d6); }
1407 if (d_max < fabs(d7)) { d_max = fabs(d7); }
1408 if (d_max < fabs(d8)) { d_max = fabs(d8); }
1409 internal::GetScalingFactor(d_max, mult);
1412 d0 /= mult; d1 /= mult; d2 /= mult;
1413 d3 /= mult; d4 /= mult; d5 /= mult;
1414 d6 /= mult; d7 /= mult; d8 /= mult;
1416 double b11 = d0*d0 + d1*d1 + d2*d2;
1417 double b12 = d0*d3 + d1*d4 + d2*d5;
1418 double b13 = d0*d6 + d1*d7 + d2*d8;
1419 double b22 = d3*d3 + d4*d4 + d5*d5;
1420 double b23 = d3*d6 + d4*d7 + d5*d8;
1421 double b33 = d6*d6 + d7*d7 + d8*d8;
1440 double aa = (b11 + b22 + b33)/3;
1446 double b11_b22 = ((d0-d3)*(d0+d3)+(d1-d4)*(d1+d4)+(d2-d5)*(d2+d5));
1447 double b22_b33 = ((d3-d6)*(d3+d6)+(d4-d7)*(d4+d7)+(d5-d8)*(d5+d8));
1448 double b33_b11 = ((d6-d0)*(d6+d0)+(d7-d1)*(d7+d1)+(d8-d2)*(d8+d2));
1449 c1 = (b11_b22 - b33_b11)/3;
1450 c2 = (b22_b33 - b11_b22)/3;
1451 c3 = (b33_b11 - b22_b33)/3;
1454 Q = (2*(b12*b12 + b13*b13 + b23*b23) + c1*c1 + c2*c2 + c3*c3)/6;
1455 R = (c1*(b23*b23 - c2*c3)+ b12*(b12*c3 - 2*b13*b23) +b13*b13*c2)/2;
1488 double sqrtQ = sqrt(Q);
1489 double sqrtQ3 = Q*sqrtQ;
1494 if (fabs(R) >= sqrtQ3)
1516 aa -= 2*sqrtQ*cos(acos(R)/3);
1520 aa -= 2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
1524 aa -= 2*sqrtQ*cos((acos(R) - 2.0*M_PI)/3);
1531 r = -2*sqrtQ*cos((acos(R) + 2.0*M_PI)/3);
1540 r = -2*sqrtQ*cos(acos(R)/3);
1571 switch (internal::KernelVector3S(mode, b12, b13, b23, c1, c2, c3))
1587 double v1, v2, v3, g;
1588 internal::Reduce3S(mode, b11, b22, b33, b12, b13, b23,
1589 c1, c2, c3, v1, v2, v3, g);
1596 internal::Eigenvalues2S(b23, b22, b33);
1600 aa = fmin(fmin(b11, b22), b33);
1606 aa = (b22 <= b33) ? b22 : fmax(b11, b33);
1610 aa = (b11 <= b33) ? b11 : fmax(b33, b22);
1615 aa = fmax(fmax(b11, b22), b33);
1621 return sqrt(fabs(aa))*mult;
1633 inline void LUSolve(
const double *data,
const int m,
const int *ipiv,
1637 for (
int i = 0; i < m; i++)
1639 internal::Swap<double>(x[i], x[ipiv[i]]);
1643 for (
int j = 0; j < m; j++)
1645 const double x_j = x[j];
1646 for (
int i = j + 1; i < m; i++)
1648 x[i] -= data[i + j * m] * x_j;
1653 for (
int j = m - 1; j >= 0; j--)
1655 const double x_j = (x[j] /= data[j + j * m]);
1656 for (
int i = 0; i < j; i++)
1658 x[i] -= data[i + j * m] * x_j;
1667 #endif // MFEM_LINALG_KERNELS_HPP MFEM_HOST_DEVICE T Det(const T *data)
Compute the determinant of a square matrix of size dim with given data.
MFEM_HOST_DEVICE double DistanceSquared(const double *x, const double *y)
Compute the square of the Euclidean distance to another vector.
MFEM_HOST_DEVICE void FNorm(double &scale_factor, double &scaled_fnorm2, const T *data)
MFEM_HOST_DEVICE void MultABt(const int Aheight, const int Awidth, const int Bheight, const TA *Adata, const TB *Bdata, TC *ABtdata)
Multiply a matrix of size Aheight x Awidth and data Adata with the transpose of a matrix of size Bhei...
MFEM_HOST_DEVICE void Diag(const double c, double *data)
Creates n x n diagonal matrix with diagonal elements c.
MFEM_HOST_DEVICE void CalcLeftInverse< 3, 1 >(const double *d, double *left_inv)
MFEM_HOST_DEVICE void Add(const int height, const int width, const TALPHA alpha, const TA *Adata, const TB *Bdata, TC *Cdata)
Compute C = A + alpha*B, where the matrices A, B and C are of size height x width with data Adata...
MFEM_HOST_DEVICE void CalcLeftInverse< 2, 1 >(const double *d, double *left_inv)
MFEM_HOST_DEVICE void MultAtB(const int Aheight, const int Awidth, const int Bwidth, const TA *Adata, const TB *Bdata, TC *AtBdata)
Multiply the transpose of a matrix of size Aheight x Awidth and data Adata with a matrix of size Ahei...
MFEM_HOST_DEVICE double CalcSingularvalue< 3 >(const double *data, const int i)
Return the i'th singular value of the matrix of size 3 with given data.
std::function< double(const Vector &)> f(double mass_coeff)
MFEM_HOST_DEVICE void CalcEigenvalues(const double *data, double *lambda, double *vec)
MFEM_HOST_DEVICE void CalcEigenvalues< 2 >(const double *data, double *lambda, double *vec)
MFEM_HOST_DEVICE void CalcLeftInverse< 3, 2 >(const double *d, double *left_inv)
MFEM_HOST_DEVICE void LUSolve(const double *data, const int m, const int *ipiv, double *x)
Assuming L.U = P.A for a factored matrix (m x m),.
MFEM_HOST_DEVICE double Norml2(const int size, const T *data)
Returns the l2 norm of the Vector with given size and data.
MFEM_HOST_DEVICE void CalcAdjugate(const T *data, T *adj_data)
Return the adjugate of a matrix.
MFEM_HOST_DEVICE void Mult(const int height, const int width, const TA *data, const TX *x, TY *y)
Matrix vector multiplication: y = A x, where the matrix A is of size height x width with given data...
void Swap(Array< T > &, Array< T > &)
MFEM_HOST_DEVICE void MultTranspose(const int height, const int width, const TA *data, const TX *x, TY *y)
Matrix transpose vector multiplication: y = At x, where the matrix A is of size height x width with g...
MFEM_HOST_DEVICE double CalcSingularvalue(const double *data, const int i)
Return the i'th singular value of the matrix of size dim with given data.
MFEM_HOST_DEVICE double CalcSingularvalue< 2 >(const double *data, const int i)
Return the i'th singular value of the matrix of size 2 with given data.
MFEM_HOST_DEVICE void CalcEigenvalues< 3 >(const double *data, double *lambda, double *vec)
MFEM_HOST_DEVICE void CalcInverse(const T *data, T *inv_data)
Return the inverse of a matrix with given size and data into the matrix with data inv_data...
MFEM_HOST_DEVICE void Subtract(const double a, const double *x, const double *y, double *z)
Vector subtraction operation: z = a * (x - y)
MFEM_HOST_DEVICE void Set(const int height, const int width, const double alpha, const TA *Adata, TB *Bdata)
Compute B = alpha*A, where the matrices A and B are of size height x width with data Adata and Bdata...
MFEM_HOST_DEVICE void AddMultVWt(const double *v, const double *w, double *VWt)
Dense matrix operation: VWt += v w^t.
MFEM_HOST_DEVICE void Symmetrize(const int size, T *data)
Symmetrize a square matrix with given size and data: A -> (A+A^T)/2.
MFEM_HOST_DEVICE void CalcLeftInverse(const double *data, double *left_inv)
Given a matrix of size 2x1, 3x1, or 3x2, compute the left inverse.
MFEM_HOST_DEVICE double FNorm2(const T *data)
Compute the square of the Frobenius norm of the matrix.