20 #ifndef JDFTX_CORE_MATRIX3_H 21 #define JDFTX_CORE_MATRIX3_H 25 template<
typename scalar=
double>
class matrix3 31 __hostanddev__ scalar& operator()(
int i,
int j) {
return m[i][j]; }
32 __hostanddev__
const scalar& operator()(
int i,
int j)
const {
return m[i][j]; }
36 __hostanddev__
void set_row(
int i,
const vector3<scalar>& v) {
for(
int j=0; j<3; j++) m[i][j] = v[j]; }
38 {
for(
int j=0; j<3; j++) { m[0][j] = v0[j]; m[1][j] = v1[j]; m[2][j] = v2[j]; }
40 __hostanddev__
void set_col(
int j,
const vector3<scalar>& v) {
for(
int i=0; i<3; i++) m[i][j] = v[i]; }
42 {
for(
int i=0; i<3; i++) { m[i][0] = v0[i]; m[i][1] = v1[i]; m[i][2] = v2[i]; }
46 explicit __hostanddev__
matrix3(scalar d0=0, scalar d1=0, scalar d2=0)
47 { m[0][0] = d0; m[1][1] = d1, m[2][2] = d2;
48 m[0][1] = m[0][2] = m[1][0] = m[1][2] = m[2][0] = m[2][1] = 0.0;
51 scalar m00, scalar m01, scalar m02,
52 scalar m10, scalar m11, scalar m12,
53 scalar m20, scalar m21, scalar m22 )
54 { m[0][0] = m00; m[0][1] = m01; m[0][2] = m02;
55 m[1][0] = m10; m[1][1] = m11; m[1][2] = m12;
56 m[2][0] = m20; m[2][1] = m21; m[2][2] = m22;
59 {
for(
int i=0; i<3; i++)
60 for(
int j=0; j<3; j++)
61 m[i][j] = scalar(n(i,j));
67 for(
int i=0; i<3; i++)
68 for(
int j=0; j<3; j++)
74 for(
int i=0; i<3; i++)
75 for(
int j=0; j<3; j++)
76 ret(i,j) = m[i][j] + n(i,j);
80 {
for(
int i=0; i<3; i++)
81 for(
int j=0; j<3; j++)
87 for(
int i=0; i<3; i++)
88 for(
int j=0; j<3; j++)
89 ret(i,j) = m[i][j] - n(i,j);
93 {
for(
int i=0; i<3; i++)
94 for(
int j=0; j<3; j++)
99 {
for(
int i=0; i<3; i++)
100 for(
int j=0; j<3; j++)
106 for(
int i=0; i<3; i++)
107 for(
int j=0; j<3; j++)
108 ret(i,j) = m[i][j] * s;
113 #define METRIC_LENGTH_SQUARED \ 114 return v[0]*v[0]*m[0][0] + v[1]*v[1]*m[1][1] + v[2]*v[2]*m[2][2] \ 115 + 2*(v[0]*v[1]*m[0][1] + v[0]*v[2]*m[0][2] + v[1]*v[2]*m[1][2]); 116 __hostanddev__
double metric_length_squared(
const vector3<double> &v)
const { METRIC_LENGTH_SQUARED }
117 __hostanddev__ scalar metric_length_squared(
const vector3<int> &v)
const { METRIC_LENGTH_SQUARED }
118 #undef METRIC_LENGTH_SQUARED 120 __hostanddev__
matrix3<scalar> operator/(scalar s)
const {
return (*
this) * (1.0/s); }
121 __hostanddev__
matrix3<scalar>& operator/(scalar s) {
return (*
this) *= (1.0/s); }
126 for(
int i=0; i<3; i++)
127 for(
int j=0; j<3; j++)
132 void print(FILE* fp,
const char *format,
bool brackets=
true)
const 133 {
for(
int i=0; i<3; i++)
134 {
if(brackets) fprintf(fp,
"[ ");
135 for(
int j=0; j<3; j++) fprintf(fp, format, m[i][j]);
136 if(brackets) fprintf(fp,
" ]\n");
else fprintf(fp,
"\n");
142 {
for(
int i=0; i<3; i++)
143 for(
int j=0; j<3; j++)
144 if(m[i][j] != n(i,j))
149 {
return ! (*
this == n);
158 for(
int i=0; i<3; i++)
159 for(
int j=0; j<3; j++)
160 m(i,j) = a[i] * b[j];
164 #define MUL_MAT_VEC(retType) \ 165 vector3<retType> ret; \ 166 for(int i=0; i<3; i++) \ 167 for(int j=0; j<3; j++) \ 168 ret[i] += m(i,j) * v[j]; \ 171 { MUL_MAT_VEC(scalar)
174 { MUL_MAT_VEC(scalar)
177 { MUL_MAT_VEC(scalar)
184 #define MUL_VEC_MAT(retType) \ 185 vector3<retType> ret; \ 186 for(int i=0; i<3; i++) \ 187 for(int j=0; j<3; j++) \ 188 ret[j] += v[i] * m(i,j); \ 191 { MUL_VEC_MAT(scalar)
194 { MUL_VEC_MAT(scalar)
197 { MUL_VEC_MAT(scalar)
204 #define MUL_MAT_MAT(retType) \ 205 matrix3<retType> ret; \ 206 for(int i=0; i<3; i++) \ 207 for(int j=0; j<3; j++) \ 208 for(int k=0; k<3; k++) \ 209 ret(i,j) += m(i,k) * n(k,j); \ 212 { MUL_MAT_MAT(scalar)
215 { MUL_MAT_MAT(scalar)
218 { MUL_MAT_MAT(scalar)
225 {
return (m = m * n); }
231 template<
typename scalar> __hostanddev__ scalar trace(
const matrix3<scalar> &m) {
return m(0,0)+m(1,1)+m(2,2); }
232 template<
typename scalar> __hostanddev__ scalar det(
const matrix3<scalar> &m) {
return box(m.row(0),m.row(1),m.row(2)); }
235 adj.set_cols(
cross(m.row(1),m.row(2)),
cross(m.row(2),m.row(0)),
cross(m.row(0),m.row(1)));
239 {
return (1./det(m)) * adjugate(m);
244 __hostanddev__
matrix3<> rotation(
double theta,
int axis)
245 {
double s, c; sincos(theta, &s, &c);
265 #endif //JDFTX_CORE_MATRIX3_H
ScalarField sqrt(const ScalarField &)
Elementwise square root (preserve input)
__hostanddev__ vector3< scalar > cross(const vector3< scalar > &a, const vector3< scalar > &b)
cross product
Definition: vector3.h:115
__hostanddev__ scalar box(const vector3< scalar > &a, const vector3< scalar > &b, const vector3< scalar > &c)
box product / triple product
Definition: vector3.h:123
ScalarField inv(const ScalarField &)
Elementwise reciprocal (preserve input)
Generic 3-vector.
Definition: vector3.h:33
__hostanddev__ matrix3< scalar > operator~() const
transpose
Definition: matrix3.h:124
double nrm2(const Tptr &X)
Definition: Operators.h:199