25#ifndef __ROTATION_HPP__
26#define __ROTATION_HPP__
38 const double twopi = 6.2831853071795864769;
39 double phi = std::atan2(sinp, cosp);
52 auto cost = std::cos(theta__ / 2);
53 auto sint = std::sin(theta__ / 2);
55 rotm(0, 0) = std::complex<double>(cost, -u__[2] * sint);
56 rotm(1, 1) = std::complex<double>(cost, u__[2] * sint);
57 rotm(0, 1) = std::complex<double>(-u__[1] * sint, -u__[0] * sint);
58 rotm(1, 0) = std::complex<double>( u__[1] * sint, -u__[0] * sint);
72 double det = R__.
det() > 0 ? 1.0 : -1.0;
81 double w = std::sqrt(std::max(0.0, 1.0 + mat(0, 0) + mat(1, 1) + mat(2, 2))) / 2.0;
82 double x = std::sqrt(std::max(0.0, 1.0 + mat(0, 0) - mat(1, 1) - mat(2, 2))) / 2.0;
83 double y = std::sqrt(std::max(0.0, 1.0 - mat(0, 0) + mat(1, 1) - mat(2, 2))) / 2.0;
84 double z = std::sqrt(std::max(0.0, 1.0 - mat(0, 0) - mat(1, 1) + mat(2, 2))) / 2.0;
86 x = std::copysign(x, mat(2, 1) - mat(1, 2));
87 y = std::copysign(y, mat(0, 2) - mat(2, 0));
88 z = std::copysign(z, mat(1, 0) - mat(0, 1));
90 su2mat(0, 0) = std::complex<double>(w, -z);
91 su2mat(1, 1) = std::complex<double>(w, z);
92 su2mat(0, 1) = std::complex<double>(-y, -x);
93 su2mat(1, 0) = std::complex<double>(y, -x);
104 R__ = R__ * R__.
det();
105 u[0] = R__(2, 1) - R__(1, 2);
106 u[1] = R__(0, 2) - R__(2, 0);
107 u[2] = R__(1, 0) - R__(0, 1);
109 double sint = u.
length() / 2.0;
110 double cost = (R__(0, 0) + R__(1, 1) + R__(2, 2) - 1) / 2.0;
115 if (std::abs(theta) < 1e-12) {
117 }
else if (std::abs(theta -
pi) < 1e-12) {
124 if (R__(0, 0) >= R__(1, 1) && R__(0, 0) >= R__(2, 2)) {
125 u[0] = std::sqrt(std::abs(R__(0, 0) + 1) / 2);
126 u[1] = (R__(0, 1) + R__(1, 0)) / 4 / u[0];
127 u[2] = (R__(0, 2) + R__(2, 0)) / 4 / u[0];
128 }
else if (R__(1, 1) >= R__(0, 0) && R__(1, 1) >= R__(2, 2)) {
129 u[1] = std::sqrt(std::abs(R__(1, 1) + 1) / 2);
130 u[0] = (R__(1, 0) + R__(0, 1)) / 4 / u[1];
131 u[2] = (R__(1, 2) + R__(2, 1)) / 4 / u[1];
133 u[2] = std::sqrt(std::abs(R__(2, 2) + 1) / 2);
134 u[0] = (R__(2, 0) + R__(0, 2)) / 4 / u[2];
135 u[1] = (R__(2, 1) + R__(1, 2)) / 4 / u[2];
138 u = u * (1.0 / u.
length());
141 return std::pair<r3::vector<double>,
double>(u, theta);
176 double alpha = euler_angles__[0];
177 double beta = euler_angles__[1];
178 double gamma = euler_angles__[2];
181 rm(0, 0) = std::cos(alpha) * std::cos(beta) * std::cos(gamma) - std::sin(alpha) * std::sin(gamma);
182 rm(0, 1) = -std::cos(gamma) * std::sin(alpha) - std::cos(alpha) * std::cos(beta) * std::sin(gamma);
183 rm(0, 2) = std::cos(alpha) * std::sin(beta);
184 rm(1, 0) = std::cos(beta) * std::cos(gamma) * std::sin(alpha) + std::cos(alpha) * std::sin(gamma);
185 rm(1, 1) = std::cos(alpha) * std::cos(gamma) - std::cos(beta) * std::sin(alpha) * std::sin(gamma);
186 rm(1, 2) = std::sin(alpha) * std::sin(beta);
187 rm(2, 0) = -std::cos(gamma) * std::sin(beta);
188 rm(2, 1) = std::sin(beta) * std::sin(gamma);
189 rm(2, 2) = std::cos(beta);
200 if (std::abs(rot__.
det() - 1) > 1e-10) {
202 s <<
"determinant of rotation matrix is " << rot__.
det();
207 for (
int i = 0; i < 3; i++) {
208 for (
int j = 0; j < 3; j++) {
209 if (std::abs(rot__(i, j) - rit(i, j)) > tolerance__) {
211 s <<
"rotation matrix is not unitary" << std::endl
212 <<
"initial symmetry matrix:" << std::endl
213 << rot__ << std::endl
214 <<
"inverse transpose matrix:" << std::endl
221 if (std::abs(rot__(2, 2) - 1.0) < 1e-10) {
223 }
else if (std::abs(rot__(2, 2) + 1.0) < 1e-10) {
227 double beta = std::acos(rot__(2, 2));
228 angles[0] =
phi_by_sin_cos(rot__(1, 2) / std::sin(beta), rot__(0, 2) / std::sin(beta));
230 angles[2] =
phi_by_sin_cos(rot__(2, 1) / std::sin(beta), -rot__(2, 0) / std::sin(beta));
235 for (
int i = 0; i < 3; i++) {
236 for (
int j = 0; j < 3; j++) {
237 if (std::abs(rot__(i, j) - rm1(i, j)) > tolerance__) {
239 s <<
"matrices don't match" << std::endl
240 <<
"initial symmetry matrix: " << std::endl
241 << rot__ << std::endl
242 <<
"euler angles : " << angles[0] /
pi <<
" " << angles[1] /
pi <<
" " << angles[2] /
pi << std::endl
243 <<
"computed symmetry matrix : " << std::endl
T det() const
Return determinant of a matrix.
double length() const
Return vector length (L2 norm).
Multidimensional array with the column-major (Fortran) order.
void zero(memory_t mem__, size_t idx0__, size_t n__)
Zero n elements starting from idx0.
Memory management functions and classes.
auto transpose(matrix< T > src)
Return transpose of the matrix.
auto inverse(matrix< int > src)
Return inverse of the integer matrix.
Namespace of the SIRIUS library.
auto euler_angles(r3::matrix< double > const &rot__, double tolerance__)
Compute Euler angles corresponding to the proper rotation matrix.
auto rotation_matrix_su2(std::array< double, 3 > u__, double theta__)
Generate SU(2) rotation matrix from the axes and angle.
auto rot_mtrx_cart(r3::vector< double > euler_angles__)
Generate rotation matrix from three Euler angles.
double phi_by_sin_cos(double sinp, double cosp)
Return angle phi in the range [0, 2Pi) by its values of sin(phi) and cos(phi).
auto axis_angle(r3::matrix< double > R__)
Get axis and angle from rotation matrix.
Simple classes and functions to work with vectors and matrices of the R^3 space.
Eror and warning handling during run-time execution.