2019-02-19 21:20:30 +02:00
|
|
|
/*! \file indexing.h
|
|
|
|
* \brief Various index conversion functions.
|
|
|
|
*/
|
2017-12-17 17:42:18 +02:00
|
|
|
#ifndef QPMS_INDEXING_H
|
|
|
|
#define QPMS_INDEXING_H
|
|
|
|
|
|
|
|
#include "qpms_types.h"
|
2018-01-28 11:18:06 +02:00
|
|
|
#include <math.h>
|
2017-12-17 17:42:18 +02:00
|
|
|
|
2019-07-15 12:56:56 +03:00
|
|
|
|
|
|
|
|
2017-12-17 17:42:18 +02:00
|
|
|
static inline qpms_y_t qpms_mn2y(qpms_m_t m, qpms_l_t n) {
|
2018-01-28 11:18:06 +02:00
|
|
|
return n * (n + 1) + m - 1;
|
2017-12-17 17:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline qpms_lm_t qpms_y2n(qpms_y_t y) {
|
|
|
|
//return (sqrt(5+y)-2)/2; // the cast will truncate the fractional part, which is what we want
|
|
|
|
return sqrt(y+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline qpms_m_t qpms_yn2m(qpms_y_t y, qpms_l_t n) {
|
|
|
|
return y-qpms_mn2y(0,n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void qpms_y2mn_p(qpms_y_t y, qpms_m_t *m, qpms_l_t *n){
|
|
|
|
*m=qpms_yn2m(y,*n=qpms_y2n(y));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline qpms_y_t qpms_lMax2nelem(qpms_l_t lmax){
|
|
|
|
return lmax * ((qpms_y_t)lmax + 2);
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:39:02 +03:00
|
|
|
// Scalar versions: they have a place for the 0, 0 term in the beginning
|
|
|
|
|
|
|
|
static inline qpms_y_t qpms_mn2y_sc(qpms_m_t m, qpms_l_t n) {
|
|
|
|
return n * (n + 1) + m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline qpms_lm_t qpms_y2n_sc(qpms_y_t y) {
|
|
|
|
//return (sqrt(5+y)-2)/2; // the cast will truncate the fractional part, which is what we want
|
|
|
|
return sqrt(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline qpms_m_t qpms_yn2m_sc(qpms_y_t y, qpms_l_t n) {
|
|
|
|
return y-qpms_mn2y_sc(0,n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void qpms_y2mn_sc_p(qpms_y_t y, qpms_m_t *m, qpms_l_t *n){
|
|
|
|
*m=qpms_yn2m_sc(y,*n=qpms_y2n_sc(y));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline qpms_y_t qpms_lMax2nelem_sc(qpms_l_t lmax){
|
|
|
|
return lmax * ((qpms_y_t)lmax + 2) + 1;
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:58:49 +02:00
|
|
|
// TODO maybe enable crashing / validity control by macro definitions...
|
|
|
|
|
2019-02-19 21:20:30 +02:00
|
|
|
/// Conversion from VSWF type, order and degree to universal index.
|
|
|
|
static inline qpms_uvswfi_t qpms_tmn2uvswfi(
|
|
|
|
qpms_vswf_type_t t, qpms_m_t m, qpms_l_t n) {
|
|
|
|
return t + 4 * qpms_mn2y_sc(m, n);
|
|
|
|
}
|
|
|
|
|
2019-07-15 12:56:56 +03:00
|
|
|
/// Constant denoting the l=0, m=0 longitudinal spherical vector wave.
|
|
|
|
static const qpms_uvswfi_t QPMS_UI_L00 = 0;
|
|
|
|
|
2019-02-19 21:20:30 +02:00
|
|
|
/// Conversion from universal VSWF index u to type, order and degree.
|
2019-02-19 22:56:53 +02:00
|
|
|
/** Returns a non-zero value if the u value is invalid. */
|
|
|
|
static inline qpms_errno_t qpms_uvswfi2tmn(qpms_uvswfi_t u,
|
2019-02-19 21:20:30 +02:00
|
|
|
qpms_vswf_type_t *t, qpms_m_t *m, qpms_l_t *n) {
|
|
|
|
*t = u & 3;
|
|
|
|
qpms_y_sc_t y_sc = u / 4;
|
2019-02-21 05:48:35 +02:00
|
|
|
qpms_y2mn_sc_p(y_sc, m, n);
|
2019-02-19 21:20:30 +02:00
|
|
|
// Test validity
|
2019-02-19 22:56:53 +02:00
|
|
|
if (*t == 3) return QPMS_ERROR; // VSWF type code invalid, TODO WARN
|
|
|
|
if (*t && !y_sc) return QPMS_ERROR; // l == 0 for transversal wave, TODO WARN
|
|
|
|
return QPMS_SUCCESS;
|
2019-02-19 21:20:30 +02:00
|
|
|
}
|
2018-09-05 15:39:02 +03:00
|
|
|
|
2019-06-23 22:04:00 +03:00
|
|
|
/// Conversion from universal VSWF index u to type and the traditional layout index (\a l > 0).
|
2019-07-15 12:56:56 +03:00
|
|
|
/** Does *not* allow for longitudinal waves. */
|
2019-03-09 09:36:09 +02:00
|
|
|
static inline qpms_errno_t qpms_uvswfi2ty(qpms_uvswfi_t u,
|
|
|
|
qpms_vswf_type_t *t, qpms_y_t *y) {
|
|
|
|
*t = u & 3;
|
|
|
|
*y = u / 4 - 1;
|
|
|
|
if (*t == 0 || *t == 3) return QPMS_ERROR;
|
|
|
|
if (*y < 0) return QPMS_ERROR;
|
|
|
|
return QPMS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:04:00 +03:00
|
|
|
/// Conversion from universal VSWF index u to type and the traditional (vector) layout index.
|
|
|
|
/** *Does* allow for longitudinal waves, but returns an error if l == 0.
|
|
|
|
* (The only legit VSWF with l = 0 is the longitudinal one; u == 0.)
|
|
|
|
*/
|
|
|
|
static inline qpms_errno_t qpms_uvswfi2ty_l(qpms_uvswfi_t u,
|
|
|
|
qpms_vswf_type_t *t, qpms_y_t *y) {
|
|
|
|
*t = u & 3;
|
|
|
|
*y = u / 4 - 1;
|
|
|
|
if (*t == 3) return QPMS_ERROR;
|
|
|
|
if (*y < 0) return QPMS_ERROR;
|
|
|
|
return QPMS_SUCCESS;
|
|
|
|
}
|
2019-03-09 09:36:09 +02:00
|
|
|
|
2019-02-21 05:48:35 +02:00
|
|
|
/// Extract degree \a m from an universal VSWF index \a u.
|
|
|
|
static inline qpms_m_t qpms_uvswfi2m(qpms_uvswfi_t u) {
|
|
|
|
qpms_vswf_type_t t; qpms_m_t m; qpms_l_t n;
|
|
|
|
qpms_uvswfi2tmn(u, &t,&m,&n);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-17 17:42:18 +02:00
|
|
|
#endif //QPMS_INDEXING_H
|