21 #ifndef GENERAL_MATRIX_3X3_IMPL_H 22 #define GENERAL_MATRIX_3X3_IMPL_H 24 #ifndef SHEAF_DLL_SPEC_H 25 #include "SheafSystem/sheaf_dll_spec.h" 28 #ifndef ASSERT_CONTRACT_H 29 #include "SheafSystem/assert_contract.h" 32 #ifndef ERROR_MESSAGE_H 33 #include "SheafSystem/error_message.h" 36 #ifndef ANTISYMMETRIC_MATRIX_3X3_H 37 #include "SheafSystem/antisymmetric_matrix_3x3.h" 40 #ifndef GENERAL_MATRIX_1x3_H 41 #include "SheafSystem/general_matrix_1x3.h" 44 #ifndef GENERAL_MATRIX_3X1_H 45 #include "SheafSystem/general_matrix_3x1.h" 48 #ifndef GENERAL_MATRIX_3X2_H 49 #include "SheafSystem/general_matrix_3x2.h" 52 #ifndef GENERAL_MATRIX_3X3_H 53 #include "SheafSystem/general_matrix_3x3.h" 56 #ifndef SYMMETRIC_MATRIX_3X3_H 57 #include "SheafSystem/symmetric_matrix_3x3.h" 61 #include "SheafSystem/std_sstream.h" 65 #include "SheafSystem/std_cmath.h" 68 #ifndef STD_ALGORITHM_H 69 #include "SheafSystem/std_algorithm.h" 75 using namespace sheaf;
83 general_matrix_3x3<T>::
90 T* lcomps =
const_cast<T*
>(components);
103 template <
typename T>
111 T* lcomps =
const_cast<T*
>(components);
124 template <
typename T>
132 T* lcomps =
const_cast<T*
>(components);
145 template <
typename T>
162 template <
typename T>
179 template <
typename T>
196 template <
typename T>
203 require(xrow >= 0 && xrow < number_of_rows());
207 T* result = &components[row_index(xrow)];
218 template <
typename T>
225 require(xrow >= 0 && xrow < number_of_rows());
229 const T* result = &components[row_index(xrow)];
241 template <
typename T>
248 require(xrow >= 0 && xrow < number_of_rows());
254 int lindex = row_index(xrow);
268 template <
typename T>
275 require(xcolumn >= 0 && xcolumn < number_of_columns());
281 int lindex = xcolumn;
296 template <
typename T>
306 T* result = components;
318 template <
typename T>
320 operator
const T* ()
const 328 const T* result = components;
340 template <
typename T>
347 require(xrow >= 0 && xrow < number_of_rows());
356 ensure(result == number_of_columns()*xrow);
365 template <
typename T>
388 T c00 = a11*a22 - a12*a21;
389 T c01 = a12*a20 - a10*a22;
390 T c02 = a10*a21 - a11*a20;
392 T c10 = a02*a21 - a01*a22;
393 T c11 = a00*a22 - a02*a20;
394 T c12 = a01*a20 - a00*a21;
396 T c20 = a01*a12 - a02*a11;
397 T c21 = a02*a10 - a00*a12;
398 T c22 = a00*a11 - a01*a10;
420 template <
typename T>
440 template <
typename T>
449 for(
int i=0; i<d(); ++i)
451 components[i] = xvalue;
456 ensure_for_all(i, 0, d(), components[i] == xvalue);
462 template <
typename T>
483 T c00 = a11*a22 - a12*a21;
484 T c01 = a12*a20 - a10*a22;
485 T c02 = a10*a21 - a11*a20;
487 xresult = a00*c00 + a01*c01 + a02*c02;
496 template <
typename T>
517 template <
typename T>
556 T c = a00*(a11 + a22) + a11*a22 - a01*a10 - a02*a20 - a12*a21;
557 T b = -(a00 + a11 + a22);
565 T discriminant = b*b - 4.0*c;
567 if(discriminant < 0.0)
569 post_fatal_error_message(
"Matrix has 2 complex roots.");
572 T sqrt_discriminant =
sqrt(discriminant);
575 lambda_1 = 0.5*(-b+sqrt_discriminant);;
576 lambda_2 = 0.5*(-b-sqrt_discriminant);
580 T q = (3.0*c - b*b)/9.0;
581 T r = (b*(9.0*c - 2.0*b*b) - 27.0*d) / 54.0;
583 T discriminant = q*q*q + r*r;
586 if(discriminant > 0.0)
590 post_fatal_error_message(
"Matrix has 2 complex roots.");
592 else if(discriminant== 0.0)
596 T powr = (r < 0) ? -
pow(-r, 1.0/3.0) :
pow(r, 1.0/3.0);
597 lambda_0 = (2.0*powr) - b3;
598 lambda_1 = (-powr) - b3;
607 static const T L_PI =
acos(T(-1.0));
613 lambda_0 = t2*
cos(t1/3.0) - b3;
614 lambda_1 = t2*
cos((t1 + 2.0*L_PI)/3.0) - b3;
615 lambda_2 = t2*
cos((t1 + 4.0*L_PI)/3.0) - b3;
625 T tmp[3] = {lambda_0, lambda_1, lambda_2};
627 std::sort(&tmp[0], &tmp[3]);
629 xresult[0][0] = tmp[0];
630 xresult[1][1] = tmp[1];
631 xresult[2][2] = tmp[2];
638 ensure(unexecutable(
"for_all i, 0, 2, xresult[i][i] == real number"));
645 template <
typename T>
655 diagonalization(result);
660 ensure(unexecutable(
"for_all i, 0, 2, result[i][i] == real number"));
668 template <
typename T>
694 template <
typename T>
717 template <
typename T>
740 T c00 = a11*a22 - a12*a21;
741 T c01 = a12*a20 - a10*a22;
742 T c02 = a10*a21 - a11*a20;
746 if(determinant == 0.0)
748 post_fatal_error_message(
"No inverse; determinant is zero.");
751 T c10 = a02*a21 - a01*a22;
752 T c11 = a00*a22 - a02*a20;
753 T c12 = a01*a20 - a00*a21;
755 T c20 = a01*a12 - a02*a11;
756 T c21 = a02*a10 - a00*a12;
757 T c22 = a00*a11 - a01*a10;
777 template <
typename T>
797 template <
typename T>
808 bool result = (lm[0][0]==0.0) && (lm[1][1]==0.0) && (lm[2][2]==0.0) &&
809 (lm[1][0]==-lm[0][1]) &&
810 (lm[2][0]==-lm[0][2]) &&
811 (lm[2][1]==-lm[1][2]);
822 template <
typename T>
834 (lm[0][1]==0.0) && (lm[0][2]==0.0) && (lm[1][0]==0.0) &&
835 (lm[1][2]==0.0) && (lm[2][0]==0.0) && (lm[2][1]==0.0);
846 template <
typename T>
857 bool result = is_diagonal() &&
858 (lm[0][0]==1.0) && (lm[1][1]==1.0) && (lm[2][2]==1.0);
870 template <
typename T>
895 template <
typename T>
906 bool result = (lm[1][0]==lm[0][1]) &&
907 (lm[2][0]==lm[0][2]) &&
908 (lm[2][1]==lm[1][2]);
919 template <
typename T>
928 for(
int i=0; i<d(); ++i)
930 xresult.
components[i] = xscalar*components[i];
944 template <
typename T>
964 template <
typename T>
978 int nra = number_of_rows();
979 int nca = number_of_columns();
982 for(
int i=0; i<nra; ++i)
984 for(
int j=0; j<ncb; ++j)
988 for(
int k=0; k<nca; ++k)
990 sum += lm[i][k]*xother[k][j];
1004 template <
typename T>
1024 template <
typename T>
1038 int nra = number_of_rows();
1039 int nca = number_of_columns();
1042 for(
int i=0; i<nra; ++i)
1044 for(
int j=0; j<ncb; ++j)
1048 for(
int k=0; k<nca; ++k)
1050 sum += lm[i][k]*xother[k][j];
1053 xresult[i][j] = sum;
1064 template <
typename T>
1084 template <
typename T>
1098 int nra = number_of_rows();
1099 int nca = number_of_columns();
1102 for(
int i=0; i<nra; ++i)
1104 for(
int j=0; j<ncb; ++j)
1108 for(
int k=0; k<nca; ++k)
1110 sum += lm[i][k]*xother[k][j];
1113 xresult[i][j] = sum;
1124 template <
typename T>
1145 template <
typename T>
1158 xresult = lm[0][0] + lm[1][1] + lm[2][2];
1162 ensure(xresult == lm[0][0] + lm[1][1] + lm[2][2]);
1168 template <
typename T>
1189 template <
typename T>
1200 int nrows = number_of_rows();
1201 int ncols = number_of_columns();
1203 for(
int i=0; i<nrows; ++i)
1205 for(
int j=0; j<ncols; ++j)
1207 xresult[i][j] = lm[j][i];
1218 template <
typename T>
1238 template <
typename T>
1251 int n = number_of_rows();
1253 for(
int i=0; i<n-1; ++i)
1255 for(
int j=i+1; j<n; ++j)
1257 xresult[i][j] = lm[i][j] - lm[j][i];
1267 template <
typename T>
1277 antisymmetric_part(result);
1288 template <
typename T>
1301 int n = number_of_rows();
1303 for(
int i=0; i<n; ++i)
1305 for(
int j=i; j<n; ++j)
1307 xresult[i][j] = lm[i][j] + lm[j][i];
1317 template <
typename T>
1327 symmetric_part(result);
1341 #ifndef DOXYGEN_SKIP_IMPLEMENTATIONS 1344 template <
typename T>
1345 std::ostream& operator<<(std::ostream& xos, const general_matrix_3x3<T>& xm)
1352 int ncols = xm.number_of_columns();
1354 for(
int i=0; i<nrows; ++i)
1356 for(
int j=0; j<ncols; ++j)
1358 xos <<
" " << xm[i][j];
1370 #endif // ifndef DOXYGEN_SKIP_IMPLEMENTATIONS 1377 #endif // GENERAL_MATRIX_3X3_IMPL_H SHEAF_DLL_SPEC void sqrt(const sec_at0 &x0, sec_at0 &xresult, bool xauto_access)
Compute sqrt of x0 (sqrt(x0)) (pre-allocated version).
T trace() const
The trace of the matrix (auto-allocated).
bool is_symmetric() const
True if this matrix is symmetric.
static int number_of_columns()
The number of columns.
static int number_of_columns()
The number of columns.
symmetric_matrix_3x3< T > symmetric_part() const
The symmetric part of a this matrix (auto-allocated).
antisymmetric_matrix_3x3< T > antisymmetric_part() const
The antisymmetric part of a this matrix (auto-allocated).
Antisymmetric matrix with 3 rows and 3 columns.
general_matrix_3x3< T > identity() const
The identity matrix (auto-allocated).
general_matrix_1x3< T > row(int xrow) const
A 1x3 matrix containing the elements or row xrow.
bool is_antisymmetric() const
True if this matrix is antisymmetric.
general_matrix_3x3< T > diagonalization() const
The diagonalization of the matrix (auto-allocated).
bool is_identity() const
True if this is an identity matrix.
static int d()
Dimension of the underlying elements.
General matrix with 3 rows and 2 columns.
static int number_of_rows()
The number of rows.
void assign(const T &xvalue)
Assign all elements of this matrix to the value xvalue.
bool is_positive_definite() const
True if this matrix is positive definite.
void trace(const S0 &x0, SR &xresult, bool xauto_access)
General matrix with 3 rows and 1 column.
T determinant() const
The determinant of the matrix (auto-allocated).
void multiply(const T &xscalar, general_matrix_3x3< T > &xresult) const
This matrix multiplied by a scalar (pre-allocated).
general_matrix_3x3< T > transpose() const
The transpose of the matrix (auto-allocated).
bool is_positive_definite() const
True if this matrix is positive definite.
bool is_diagonal() const
True if this matrix is diagonal.
int row_index(int xrow) const
Index for row xrow in the linear storage array.
Row dofs type for class gl3.
T components[3]
Linear storage array.
static int number_of_columns()
The number of columns.
SHEAF_DLL_SPEC void cos(const sec_at0 &x0, sec_at0 &xresult, bool xauto_access)
Compute cos of x0 (cos(x0)) (pre-allocated version).
void determinant(const S0 &x0, SR &xresult, bool xauto_access)
Row dofs type for class jcb_e33.
general_matrix_3x3< T > inverse() const
The inverse of the matrix (auto-allocated).
T * operator[](int xrow)
Pointer to the first element in row xrow of this matrix. Facilitates accessing elements via matrix[i]...
SHEAF_DLL_SPEC void acos(const sec_at0 &x0, sec_at0 &xresult, bool xauto_access)
Compute acos of x0 (acos(x0)) (pre-allocated version).
General matrix with 3 rows and 3 columns.
General matrix with 1 row and 3 columns.
Namespace for the sheaves component of the sheaf system.
general_matrix_3x1< T > column(int xcolumn) const
A 3x1 matrix containing the elements or column xcolumn.
T components[3]
Linear storage array.
static int number_of_rows()
The number of rows.
SHEAF_DLL_SPEC void multiply(const vd &x0, const vd_value_type &x1, vd &xresult, bool xauto_access)
Vector x0 multiplied by scalar x1 (pre-allocated version for persistent types).
Namespace for the fiber_bundles component of the sheaf system.
SHEAF_DLL_SPEC void pow(const sec_at0 &x0, const vd_value_type &xexponent, sec_at0 &xresult, bool xauto_access)
Compute x0 to power xexponent (pow(x0, xexponent)) (pre-allocated version).
SHEAF_DLL_SPEC void inverse(const gl2_lite &xlite, gl2_lite &xresult)
Inverse (pre-allocated version for volatile type).
general_matrix_3x3< T > adjoint() const
The adjoint of the matrix (auto-allocated).
T components[9]
Linear storage array.
A tensor of degree 2 over an abstract vector space (persistent version).
Symmetric matrix with 3 rows and 3 columns.