Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
SpecialFunctions.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "math/Constants.hpp"
12#include "math/Precision.hpp"
13#include "math/SIMDConfig.hpp"
14#include <array>
15#include <cmath>
16
17namespace correlation::math {
18
27inline real_t factorial(int n) {
28 static const std::array<real_t, 21> FACT = {
29 static_cast<real_t>(1.0),
30 static_cast<real_t>(1.0),
31 static_cast<real_t>(2.0),
32 static_cast<real_t>(6.0),
33 static_cast<real_t>(24.0),
34 static_cast<real_t>(120.0),
35 static_cast<real_t>(720.0),
36 static_cast<real_t>(5040.0),
37 static_cast<real_t>(40320.0),
38 static_cast<real_t>(362880.0),
39 static_cast<real_t>(3628800.0),
40 static_cast<real_t>(39916800.0),
41 static_cast<real_t>(479001600.0),
42 static_cast<real_t>(6227020800.0),
43 static_cast<real_t>(87178291200.0),
44 static_cast<real_t>(1307674368000.0),
45 static_cast<real_t>(20922789888000.0),
46 static_cast<real_t>(355687428096000.0),
47 static_cast<real_t>(6402373705728000.0),
48 static_cast<real_t>(121645100408832000.0),
49 static_cast<real_t>(2432902008176640000.0),
50 };
51 if (n < 0) {
52 return static_cast<real_t>(0.0);
53 }
54 if (n <= 20) {
55 return FACT.at(static_cast<size_t>(n));
56 }
57 return static_cast<real_t>(std::tgamma(static_cast<double>(n) + 1.0));
58}
59
64 int degree;
65 int order;
66};
67
78inline real_t sphLegendre(LegendreParams params, real_t theta) {
79 const int degree = params.degree;
80 const int order = params.order;
81 if (order < 0 || order > degree) {
82 return static_cast<real_t>(0.0);
83 }
84
85 real_t const cos_theta = std::cos(theta);
86
87 // Compute P_m^m(x)
88 auto p_mm = static_cast<real_t>(1.0);
89 if (order > 0) {
90 real_t const somx2 =
91 std::sqrt((static_cast<real_t>(1.0) - cos_theta) * (static_cast<real_t>(1.0) + cos_theta));
92 auto fact = static_cast<real_t>(1.0);
93 for (int i = 1; i <= order; ++i) {
94 p_mm *= -fact * somx2;
95 fact += static_cast<real_t>(2.0);
96 }
97 }
98
99 real_t p_lm = p_mm;
100
101 if (degree != order) {
102 // Compute P_{m+1}^m(x)
103 real_t const p_mp1m = cos_theta * static_cast<real_t>(2 * order + 1) * p_mm;
104 p_lm = p_mp1m;
105
106 if (degree > order + 1) {
107 // Compute P_l^m(x) using recurrence relation
108 real_t p_ll_2 = p_mm; // P_{l-2}^m
109 real_t p_ll_1 = p_mp1m; // P_{l-1}^m
110
111 for (int ll = order + 2; ll <= degree; ++ll) {
112 p_lm = (cos_theta * static_cast<real_t>(2 * ll - 1) * p_ll_1 -
113 static_cast<real_t>(ll + order - 1) * p_ll_2) /
114 static_cast<real_t>(ll - order);
115 p_ll_2 = p_ll_1;
116 p_ll_1 = p_lm;
117 }
118 }
119 }
120
121 // Normalization factor
122 const auto norm = static_cast<real_t>(std::sqrt(
123 (static_cast<real_t>(2.0) * static_cast<real_t>(degree) + static_cast<real_t>(1.0)) /
124 (static_cast<real_t>(4.0) * static_cast<real_t>(PI)) * factorial(degree - order) /
125 factorial(degree + order)));
126
127 // Cancel Condon-Shortley phase to match std::sph_legendre
128 if (order % 2 != 0) {
129 p_lm = -p_lm;
130 }
131
132 return p_lm * norm;
133}
134
144inline void sphLegendreBatch(LegendreParams params, const real_t *CORRELATION_RESTRICT theta,
145 real_t *CORRELATION_RESTRICT results, size_t count) {
146 const int degree = params.degree;
147 const int order = params.order;
148 if (order < 0 || order > degree) {
149 for (size_t idx = 0; idx < count; ++idx) {
150 results[idx] = static_cast<real_t>(0.0);
151 }
152 return;
153 }
154
155#if defined(CORRELATION_SIMD_AVX512) && defined(CORRELATION_USE_DOUBLE)
156 auto norm = static_cast<real_t>(std::sqrt(
157 (static_cast<real_t>(2.0) * static_cast<real_t>(degree) + static_cast<real_t>(1.0)) /
158 (static_cast<real_t>(4.0) * static_cast<real_t>(PI)) * factorial(degree - order) /
159 factorial(degree + order)));
160 if (order % 2 != 0) {
161 norm = -norm;
162 }
163
164 size_t idx = 0;
165 const __m512d vnorm = _mm512_set1_pd(norm);
166 for (; idx + 8 <= count; idx += 8) {
167 std::array<double, 8> x_arr{};
168 std::array<double, 8> somx2_arr{};
169 for (size_t k = 0; k < 8; ++k) {
170 x_arr.at(k) = std::cos(theta[idx + k]);
171 somx2_arr.at(k) = std::sqrt((1.0 - x_arr.at(k)) * (1.0 + x_arr.at(k)));
172 }
173 __m512d v_x = _mm512_loadu_pd(x_arr.data());
174 __m512d v_somx2 = _mm512_loadu_pd(somx2_arr.data());
175
176 __m512d vp_mm = _mm512_set1_pd(1.0);
177 if (order > 0) {
178 double fact = 1.0;
179 for (int k = 1; k <= order; ++k) {
180 vp_mm = _mm512_mul_pd(vp_mm, _mm512_mul_pd(_mm512_set1_pd(-fact), v_somx2));
181 fact += 2.0;
182 }
183 }
184
185 __m512d vp_lm = vp_mm;
186 if (degree != order) {
187 __m512d vp_mp1m = _mm512_mul_pd(_mm512_mul_pd(v_x, _mm512_set1_pd(2.0 * order + 1.0)), vp_mm);
188 vp_lm = vp_mp1m;
189
190 if (degree > order + 1) {
191 __m512d vp_ll_2 = vp_mm;
192 __m512d vp_ll_1 = vp_mp1m;
193
194 for (int ll = order + 2; ll <= degree; ++ll) {
195 __m512d term1 =
196 _mm512_mul_pd(_mm512_mul_pd(v_x, _mm512_set1_pd(2.0 * ll - 1.0)), vp_ll_1);
197 __m512d term2 = _mm512_mul_pd(_mm512_set1_pd(ll + order - 1.0), vp_ll_2);
198 __m512d diff = _mm512_sub_pd(term1, term2);
199 vp_lm = _mm512_mul_pd(diff, _mm512_set1_pd(1.0 / (ll - order)));
200 vp_ll_2 = vp_ll_1;
201 vp_ll_1 = vp_lm;
202 }
203 }
204 }
205
206 __m512d vres = _mm512_mul_pd(vp_lm, vnorm);
207 _mm512_storeu_pd(results + idx, vres);
208 }
209 for (; idx < count; ++idx) {
210 results[idx] = sphLegendre(
211 {
212 .degree = degree,
213 .order = order,
214 },
215 theta[idx]);
216 }
217#elif defined(CORRELATION_SIMD_AVX2) && defined(CORRELATION_USE_DOUBLE)
218 auto norm = static_cast<real_t>(std::sqrt(
219 (static_cast<real_t>(2.0) * static_cast<real_t>(degree) + static_cast<real_t>(1.0)) /
220 (static_cast<real_t>(4.0) * static_cast<real_t>(PI)) * factorial(degree - order) /
221 factorial(degree + order)));
222 if (order % 2 != 0) {
223 norm = -norm;
224 }
225
226 size_t idx = 0;
227 const __m256d vnorm = _mm256_set1_pd(norm);
228 for (; idx + 4 <= count; idx += 4) {
229 std::array<double, 4> x_arr{};
230 std::array<double, 4> somx2_arr{};
231 for (size_t k = 0; k < 4; ++k) {
232 x_arr.at(k) = std::cos(theta[idx + k]);
233 somx2_arr.at(k) = std::sqrt((1.0 - x_arr.at(k)) * (1.0 + x_arr.at(k)));
234 }
235 __m256d v_x = _mm256_loadu_pd(x_arr.data());
236 __m256d v_somx2 = _mm256_loadu_pd(somx2_arr.data());
237
238 __m256d vp_mm = _mm256_set1_pd(1.0);
239 if (order > 0) {
240 double fact = 1.0;
241 for (int k = 1; k <= order; ++k) {
242 vp_mm = _mm256_mul_pd(vp_mm, _mm256_mul_pd(_mm256_set1_pd(-fact), v_somx2));
243 fact += 2.0;
244 }
245 }
246
247 __m256d vp_lm = vp_mm;
248 if (degree != order) {
249 __m256d vp_mp1m = _mm256_mul_pd(_mm256_mul_pd(v_x, _mm256_set1_pd(2.0 * order + 1.0)), vp_mm);
250 vp_lm = vp_mp1m;
251
252 if (degree > order + 1) {
253 __m256d vp_ll_2 = vp_mm;
254 __m256d vp_ll_1 = vp_mp1m;
255
256 for (int ll = order + 2; ll <= degree; ++ll) {
257 __m256d term1 =
258 _mm256_mul_pd(_mm256_mul_pd(v_x, _mm256_set1_pd(2.0 * ll - 1.0)), vp_ll_1);
259 __m256d term2 = _mm256_mul_pd(_mm256_set1_pd(ll + order - 1.0), vp_ll_2);
260 __m256d diff = _mm256_sub_pd(term1, term2);
261 vp_lm = _mm256_mul_pd(diff, _mm256_set1_pd(1.0 / (ll - order)));
262 vp_ll_2 = vp_ll_1;
263 vp_ll_1 = vp_lm;
264 }
265 }
266 }
267
268 __m256d vres = _mm256_mul_pd(vp_lm, vnorm);
269 _mm256_storeu_pd(results + idx, vres);
270 }
271 for (; idx < count; ++idx) {
272 results[idx] = sphLegendre(
273 {
274 .degree = degree,
275 .order = order,
276 },
277 theta[idx]);
278 }
279#else
280 for (size_t idx = 0; idx < count; ++idx) {
281 results[idx] = sphLegendre(
282 {
283 .degree = degree,
284 .order = order,
285 },
286 theta[idx]);
287 }
288#endif
289}
290
291} // namespace correlation::math
Mathematical and physical constants for the Correlation tool.
Floating point precision type definitions for Correlation.
Configuration macros for SIMD instruction sets and compiler specifics.
Defines universal mathematical and physical constants.
constexpr real_t PI
High-precision mathematical pi constant.
Definition Constants.hpp:27
constexpr T norm(const Vector3< T > &vec_a) noexcept
Computes the vector norm (length).
Definition LinearAlgebra.hpp:520
void sphLegendreBatch(LegendreParams params, const real_t *theta, real_t *results, size_t count)
Vectorized version of sphLegendre. Computes the polynomial for a range of angles.
Definition SpecialFunctions.hpp:144
real_t sphLegendre(LegendreParams params, real_t theta)
Computes the spherical associated Legendre polynomial corresponding to std::sph_legendre from C++17.
Definition SpecialFunctions.hpp:78
real_t factorial(int n)
Inline helper for factorials up to 20. Beyond 20, the values exceed the capacity of a 64-bit integer,...
Definition SpecialFunctions.hpp:27
double real_t
Definition Precision.hpp:18
Parameters for Legendre polynomial evaluation (degree and order).
Definition SpecialFunctions.hpp:63
int order
Definition SpecialFunctions.hpp:65
int degree
Definition SpecialFunctions.hpp:64