#pragma once #include #include #include #include #include "state.h" #include "types.h" template struct orthogonal_t { bool is_reflection; T *x; }; template void init(orthogonal_t *ptr) { ptr->is_reflection = false; ptr->x = (T *)calloc(q * q, sizeof(T)); for (q_t i = 0; i < q; i++) { ptr->x[q * i + i] = (T)1; } } template orthogonal_t copy (orthogonal_t m) { orthogonal_t m_copy; m_copy.is_reflection = m.is_reflection; q_t size; if (m.is_reflection) { size = q; } else { size = q * q; } m_copy.x = (T *)calloc(size, sizeof(T)); for (q_t i = 0; i < size; i++) { m_copy.x[i] = m.x[i]; } return m_copy; } template void free_spin (orthogonal_t m) { free(m.x); } template vector_t act (orthogonal_t m, vector_t v) { vector_t v_rot; v_rot.x = (T *)calloc(q, sizeof(T)); if (m.is_reflection) { double prod = 0; for (q_t i = 0; i < q; i++) { prod += v.x[i] * m.x[i]; } for (q_t i = 0; i < q; i++) { v_rot.x[i] = v.x[i] - 2 * prod * m.x[i]; } } else { for (q_t i = 0; i < q; i++) { for (q_t j = 0; j < q; j++) { v_rot.x[i] += m.x[q * i + j] * v.x[j]; } } } return v_rot; } template orthogonal_t act (orthogonal_t m1, orthogonal_t m2) { orthogonal_t m2_rot; m2_rot.is_reflection = false; m2_rot.x = (T *)calloc(q * q, sizeof(T)); if (m1.is_reflection) { for (q_t i = 0; i < q; i++) { double akOki = 0; for (q_t k = 0; k < q; k++) { akOki += m1.x[k] * m2.x[q * k + i]; } for (q_t j = 0; j < q; j++) { m2_rot.x[q * j + i] = m2.x[q * j + i] - 2 * akOki * m1.x[j]; } } } else { for (q_t i = 0; i < q; i++) { for (q_t j = 0; j < q; j++) { for (q_t k = 0; k < q; k++) { m2_rot.x[i * q + j] += m1.x[i * q + j] * m2.x[j * q + k]; } } } } return m2_rot; } template vector_t act_inverse (orthogonal_t m, vector_t v) { if (m.is_reflection) { return act(m, v); // reflections are their own inverse } else { vector_t v_rot; v_rot.x = (T *)calloc(q, sizeof(T)); for (q_t i = 0; i < q; i++) { for (q_t j = 0; j < q; j++) { v_rot.x[i] += m.x[q * j + i] * v.x[j]; } } return v_rot; } } template orthogonal_t act_inverse (orthogonal_t m1, orthogonal_t m2) { if (m1.is_reflection) { return act(m1, m2); // reflections are their own inverse } else { orthogonal_t m2_rot; m2_rot.x = (T *)calloc(q * q, sizeof(T)); for (q_t i = 0; i < q; i++) { for (q_t j = 0; j < q; j++) { for (q_t k = 0; k < q; k++) { m2_rot.x[i * q + j] += m1.x[j * q + i] * m2.x[j * q + k]; } } } return m2_rot; } } template orthogonal_t generate_rotation_uniform (gsl_rng *r, vector_t v) { orthogonal_t ptr; ptr.is_reflection = true; ptr.x = (double *)calloc(q, sizeof(double)); double v2 = 0; for (q_t i = 0; i < q; i++) { ptr.x[i] = gsl_ran_ugaussian(r); v2 += ptr.x[i] * ptr.x[i]; } double mag_v = sqrt(v2); for (q_t i = 0; i < q; i++) { ptr.x[i] /= mag_v; } return ptr; } template orthogonal_t generate_rotation_perturbation (gsl_rng *r, vector_t v0, double epsilon, unsigned int n) { orthogonal_t m; m.is_reflection = true; m.x = (double *)malloc(q * sizeof(double)); vector_t v; if (n > 1) { unsigned int rotation = gsl_rng_uniform_int(r, n); v.x = (double *)malloc(q * sizeof(double)); double cosr = cos(2 * M_PI * rotation / (double)n / 2.0); double sinr = sin(2 * M_PI * rotation / (double)n / 2.0); v.x[0] = v0.x[0] * cosr - v0.x[1] * sinr; v.x[1] = v0.x[1] * cosr + v0.x[0] * sinr; for (q_t i = 2; i < q; i++) { v.x[i] = v0.x[i]; } } else { v.x = v0.x; } double m2 = 0; double m_dot_v = 0; for (q_t i = 0; i < q; i++) { m.x[i] = gsl_ran_ugaussian(r); m_dot_v += m.x[i] * v.x[i]; } double v2 = 0; double factor = epsilon * gsl_ran_ugaussian(r); for (q_t i = 0; i < q; i++) { m.x[i] = m.x[i] - m_dot_v * v.x[i] + factor * v.x[i]; v2 += pow(m.x[i], 2); } double mag_v = sqrt(v2); for (q_t i = 0; i < q; i++) { m.x[i] /= mag_v; } if (n > 1) { free(v.x); } return m; }