-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathieu.h
More file actions
357 lines (314 loc) · 11.3 KB
/
Copy pathmathieu.h
File metadata and controls
357 lines (314 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#pragma once
#include "error.h"
#include "mathieu/matrix_utils.h"
#include "mathieu/make_matrix.h"
#include "mathieu/mathieu_coeffs.h"
#include "mathieu/mathieu_eigs.h"
#include "mathieu/besseljyd.h"
#include "mathieu/mathieu_fcns.h"
/*
*
* This is part of the Mathieu function suite -- a reimplementation
* of the Mathieu functions for Scipy. This file #includes all the
* fcn impls in the mathieu/ subdirectory, and provides translation
* from the Scipy call to the calling signature I implemented in my
* reimplementation.
*
* Stuart Brorson, Summer 2025.
*
*/
namespace xsf {
/* Characteristic values */
//-------------------------------------------------------------
/**
* Mathieu characteristic values (eigenvalues) for even parity functions.
*
* Even parity characteristic values a.
*
* @param m Eigenvalue order. Must be positive integer less than 500.
* @param q Mathieu parameter q. Real number.
* @return Mathieu eigenvalue a.
*/
template <typename T>
T cem_cva(T m, T q) {
// This returns the even Mathieu characteristic value (eigenvalue) a.
// Check for invalid Mathieu order.
if ((m < 0) || (m != floor(m))) {
set_error("mathieu_a", SF_ERROR_DOMAIN, NULL);
return std::numeric_limits<T>::quiet_NaN();
}
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double da;
int retcode = xsf::mathieu::mathieu_a(im, dq, &da);
if (retcode != SF_ERROR_OK) {
set_error("mathieu_a", SF_ERROR_NO_RESULT, NULL);
return std::numeric_limits<T>::quiet_NaN();
}
// Now cast back.
T a = static_cast<T>(da);
return a;
}
//-------------------------------------------------------------
template <typename T>
/**
* Mathieu characteristic values (eigenvalues) b for odd functions.
*
*
* Odd parity characteristic values b.
*
* @param m Eigenvalue order. Must be positive integer less than 500.
* @param q Mathieu parameter q. Real number.
* @return Mathieu eigenvalue b.
*/
T sem_cva(T m, T q) {
// This returns the odd Mathieu characteristic value (eigenvalue) b.
// Check for invalid Mathieu order.
if ((m < 1) || (m != floor(m))) {
set_error("mathieu_b", SF_ERROR_DOMAIN, NULL);
return std::numeric_limits<T>::quiet_NaN();
}
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double db;
int retcode = xsf::mathieu::mathieu_b(im, dq, &db);
if (retcode != SF_ERROR_OK) {
set_error("mathieu_b", SF_ERROR_NO_RESULT, NULL);
return std::numeric_limits<T>::quiet_NaN();
}
// Now cast back.
T b = static_cast<T>(db);
return b;
}
//---------------------------------------------------------------
/* Mathieu functions */
/**
* Even parity Mathieu angular function ce(m, q, x)
*
* This implementation of ce follows the definitions on the
* DLMF, https://dlmf.nist.gov/28
*
* @param m Function order. Must be positive integer less than 500.
* @param q Parameter q. Real number.
* @param x Angular coordinate x (radians). Real number.
* @param csf Value of function. Real number.
* @param csd Value of derivative w.r.t. x. Real number.
*/
template <typename T>
void cem(T m, T q, T x, T &csf, T &csd) {
if ((m < 0) || (m != floor(m))) {
csf = std::numeric_limits<T>::quiet_NaN();
csd = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_ce", SF_ERROR_DOMAIN, NULL);
} else {
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double dx = static_cast<double>(x);
double dcsf;
double dcsd;
// Call fcn and cast back.
int retcode = xsf::mathieu::mathieu_ce(im, dq, dx, &dcsf, &dcsd);
if (retcode != SF_ERROR_OK) {
csf = std::numeric_limits<T>::quiet_NaN();
csd = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_ce", (sf_error_t) retcode, NULL);
} else {
csf = static_cast<T>(dcsf);
csd = static_cast<T>(dcsd);
}
}
}
//---------------------------------------------------------------
/**
* Odd parity Mathieu angular function se(m, q, x)
*
* This implementation of ce follows the definitions on the
* DLMF, https://dlmf.nist.gov/28
*
* @param m Function order. Must be positive integer less than 500.
* @param q Parameter q. Real number.
* @param x Angular coordinate x (radians). Real number.
* @param ssf Value of function. Real number.
* @param ssd Value of derivative w.r.t. x. Real number
*/
template <typename T>
void sem(T m, T q, T x, T &ssf, T &ssd) {
if ((m < 1) || (m != floor(m))) {
ssf = std::numeric_limits<T>::quiet_NaN();
ssd = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_sem", SF_ERROR_DOMAIN, NULL);
} else {
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double dx = static_cast<double>(x);
double dssf;
double dssd;
// Call fcn and cast back.
int retcode = xsf::mathieu::mathieu_se(im, dq, dx, &dssf, &dssd);
if (retcode != SF_ERROR_OK) {
ssf = std::numeric_limits<T>::quiet_NaN();
ssd = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_sem", (sf_error_t) retcode, NULL);
} else {
ssf = static_cast<T>(dssf);
ssd = static_cast<T>(dssd);
}
}
}
//---------------------------------------------------------------
/**
* Even parity modified (radial) Mathieu function of first kind Mc1(m, q, x)
*
* This implementation of ce follows the definitions on the
* DLMF, https://dlmf.nist.gov/28
*
* @param m Function order. Must be positive integer less than 500.
* @param q Parameter q. Positive real number
* @param x Radial coordinate x. Positive real number.
* @param f1r Value of function. Real number.
* @param d1r Value of derivative w.r.t. x. Real number
*/
template <typename T>
void mcm1(T m, T q, T x, T &f1r, T &d1r) {
if ((m < 0) || (m != floor(m))) {
f1r = std::numeric_limits<T>::quiet_NaN();
d1r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_mcm1", SF_ERROR_DOMAIN, NULL);
} else {
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double dx = static_cast<double>(x);
double df1r;
double dd1r;
// Call fcn and cast back.
int retcode = xsf::mathieu::mathieu_modmc1(im, dq, dx, &df1r, &dd1r);
if (retcode != SF_ERROR_OK) {
f1r = std::numeric_limits<T>::quiet_NaN();
d1r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_mcm1", (sf_error_t) retcode, NULL);
} else {
f1r = static_cast<T>(df1r);
d1r = static_cast<T>(dd1r);
}
}
}
//---------------------------------------------------------------
/**
* Odd parity modified (radial) Mathieu function of first kind Ms1(m, q, x)
*
* This implementation of ce follows the definitions on the
* DLMF, https://dlmf.nist.gov/28
*
* @param m Function order. Must be positive integer less than 500.
* @param q Parameter q. Positive real number
* @param x Radial coordinate x. Positive real number.
* @param f1r Value of function. Real number.
* @param d1r Value of derivative w.r.t. x. Real number
*/
template <typename T>
void msm1(T m, T q, T x, T &f1r, T &d1r) {
if ((m < 1) || (m != floor(m))) {
f1r = std::numeric_limits<T>::quiet_NaN();
d1r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_msm1", SF_ERROR_DOMAIN, NULL);
} else {
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double dx = static_cast<double>(x);
double df1r;
double dd1r;
// Call fcn and cast back.
int retcode = xsf::mathieu::mathieu_modms1(im, dq, dx, &df1r, &dd1r);
if (retcode != SF_ERROR_OK) {
f1r = std::numeric_limits<T>::quiet_NaN();
d1r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_msm1", (sf_error_t) retcode, NULL);
} else {
f1r = static_cast<T>(df1r);
d1r = static_cast<T>(dd1r);
}
}
}
//---------------------------------------------------------------
/**
* Even parity modified (radial) Mathieu function of second kind Mc2(m, q, x)
*
* This implementation of ce follows the definitions on the
* DLMF, https://dlmf.nist.gov/28
*
* @param m Function order. Must be positive integer less than 500.
* @param q Parameter q. Positive real number
* @param x Radial coordinate x. Positive real number.
* @param f2r Value of function. Real number.
* @param d2r Value of derivative w.r.t. x. Real number
*/
template <typename T>
void mcm2(T m, T q, T x, T &f2r, T &d2r) {
if ((m < 0) || (m != floor(m))) {
f2r = std::numeric_limits<T>::quiet_NaN();
d2r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_mcm2", SF_ERROR_DOMAIN, NULL);
} else {
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double dx = static_cast<double>(x);
double df2r;
double dd2r;
// Call fcn and cast back.
int retcode = xsf::mathieu::mathieu_modmc2(im, dq, dx, &df2r, &dd2r);
if (retcode != SF_ERROR_OK) {
f2r = std::numeric_limits<T>::quiet_NaN();
d2r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_mcm2", (sf_error_t) retcode, NULL);
} else {
f2r = static_cast<T>(df2r);
d2r = static_cast<T>(dd2r);
}
}
}
//---------------------------------------------------------------
/**
* Odd parity modified (radial) Mathieu function of second kind Ms2(m, q, x)
*
* This implementation of ce follows the definitions on the
* DLMF, https://dlmf.nist.gov/28
*
* @param m Function order. Must be positive integer less than 500.
* @param q Parameter q. Positive real number
* @param x Radial coordinate x. Positive real number.
* @param f2r Value of function. Real number.
* @param d2r Value of derivative w.r.t. x. Real number
*/
template <typename T>
void msm2(T m, T q, T x, T &f2r, T &d2r) {
if ((m < 1) || (m != floor(m))) {
f2r = std::numeric_limits<T>::quiet_NaN();
d2r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_msm2", SF_ERROR_DOMAIN, NULL);
} else {
// Must cast to correct types prior to fcn call.
int im = static_cast<int>(m);
double dq = static_cast<double>(q);
double dx = static_cast<double>(x);
double df2r;
double dd2r;
// Call fcn and cast back.
int retcode = xsf::mathieu::mathieu_modms2(im, dq, dx, &df2r, &dd2r);
if (retcode != SF_ERROR_OK) {
f2r = std::numeric_limits<T>::quiet_NaN();
d2r = std::numeric_limits<T>::quiet_NaN();
set_error("mathieu_msm2", (sf_error_t) retcode, NULL);
} else {
f2r = static_cast<T>(df2r);
d2r = static_cast<T>(dd2r);
}
}
}
} // namespace xsf