#include "taylor_order_3.hpp"

#include <cmath>

#include "math_utils.hpp"

namespace astrodyn_core {
namespace geqoe {

namespace {

constexpr std::size_t STATE_DIM = 6;

inline std::size_t idx_yprop(std::size_t step, std::size_t comp) {
    return step * STATE_DIM + comp;
}

inline double derivatives_of_inverse_3(double a0, double a1, double a2) {
    double out = 0.0;
    const double in_a[3] = {a0, a1, a2};
    astrodyn_core::math::compute_derivatives_of_inverse(in_a, &out, 3, true);
    return out;
}

inline double derivatives_of_inverse_4(double a0, double a1, double a2, double a3) {
    double out = 0.0;
    const double in_a[4] = {a0, a1, a2, a3};
    astrodyn_core::math::compute_derivatives_of_inverse(in_a, &out, 4, true);
    return out;
}

inline double derivatives_of_inverse_wrt_param_3(double a0, double a1, double a2, double ad0, double ad1, double ad2) {
    double out = 0.0;
    const double in_a[3] = {a0, a1, a2};
    const double in_d[3] = {ad0, ad1, ad2};
    astrodyn_core::math::compute_derivatives_of_inverse_wrt_param(in_a, in_d, &out, 3, true);
    return out;
}

inline double derivatives_of_inverse_wrt_param_4(double a0, double a1, double a2, double a3, double ad0, double ad1, double ad2, double ad3) {
    double out = 0.0;
    const double in_a[4] = {a0, a1, a2, a3};
    const double in_d[4] = {ad0, ad1, ad2, ad3};
    astrodyn_core::math::compute_derivatives_of_inverse_wrt_param(in_a, in_d, &out, 4, true);
    return out;
}

inline double derivatives_of_product_3(double a0, double a1, double a2) {
    double out = 0.0;
    const double in_a[3] = {a0, a1, a2};
    astrodyn_core::math::compute_derivatives_of_product(in_a, &out, 3, true);
    return out;
}

inline double derivatives_of_product_4(double a0, double a1, double a2, double a3) {
    double out = 0.0;
    const double in_a[4] = {a0, a1, a2, a3};
    astrodyn_core::math::compute_derivatives_of_product(in_a, &out, 4, true);
    return out;
}

inline double derivatives_of_product_wrt_param_3(double a0, double a1, double a2, double ad0, double ad1, double ad2) {
    double out = 0.0;
    const double in_a[3] = {a0, a1, a2};
    const double in_d[3] = {ad0, ad1, ad2};
    astrodyn_core::math::compute_derivatives_of_product_wrt_param(in_a, in_d, &out, 3, true);
    return out;
}

inline double derivatives_of_product_wrt_param_4(double a0, double a1, double a2, double a3, double ad0, double ad1, double ad2, double ad3) {
    double out = 0.0;
    const double in_a[4] = {a0, a1, a2, a3};
    const double in_d[4] = {ad0, ad1, ad2, ad3};
    astrodyn_core::math::compute_derivatives_of_product_wrt_param(in_a, in_d, &out, 4, true);
    return out;
}

} // namespace

void compute_coefficients_3(
    const double* y0,
    const PropagationConstants& constants,
    Order1Coefficients& out1,
    Order1Intermediates& inter1,
    Order2Coefficients& out2,
    Order2Intermediates& inter2,
    Order3Coefficients& out,
    Order3Intermediates& inter
) {
    compute_coefficients_2(y0, constants, out1, inter1, out2, inter2);

    const double fibm1 = inter1.fibm1;
    const double hr3 = inter1.hr3;
    const double r3p = inter1.r3p;
    const double r3p2_Lr = inter2.r3p2_Lr;
    const double r3p2_nu = inter2.r3p2_nu;
    const double r3p2_p1 = inter2.r3p2_p1;
    const double r3p2_p2 = inter2.r3p2_p2;
    const double r3p2_q1 = inter2.r3p2_q1;
    const double r3p2_q2 = inter2.r3p2_q2;
    const double r3p_Lr = inter1.r3p_Lr;
    const double r3p_nu = inter1.r3p_nu;
    const double r3p_p1 = inter1.r3p_p1;
    const double r3p_p2 = inter1.r3p_p2;
    const double r3p_q1 = inter1.r3p_q1;
    const double r3p_q2 = inter1.r3p_q2;
    const double nu_0 = inter1.nu_0;
    const double q1_0 = inter1.q1_0;
    const double q2_0 = inter1.q2_0;
    const double p1_0 = inter1.p1_0;
    const double p2_0 = inter1.p2_0;
    const double mu_norm = inter1.mu_norm;
    const double A = inter1.A;
    const double a = inter1.a;
    const double a_nu = inter1.a_nu;
    const double r = inter1.r;
    const double rp = inter1.rp;
    const double r2 = inter1.r2;
    const double r3 = inter1.r3;
    const double X = inter1.X;
    const double Y = inter1.Y;
    const double Xp = inter2.Xp;
    const double Yp = inter2.Yp;
    const double cosL = inter1.cosL;
    const double sinL = inter1.sinL;
    const double alpha = inter1.alpha;
    const double beta = inter1.beta;
    const double fib = inter1.fib;
    const double fic = inter2.fic;
    const double fih = inter1.fih;
    const double fir = inter1.fir;
    const double fir2 = inter1.fir2;
    const double fir3 = inter1.fir3;
    const double fihr3 = inter2.fihr3;
    const double fiD = inter1.fiD;
    const double c = inter1.c;
    const double h = inter1.h;
    const double hr = inter1.hr;
    const double d = inter1.d;
    const double wh = inter1.wh;
    const double I = inter1.I;
    const double U = inter1.U;
    const double delta = inter1.delta;
    const double zg = inter1.zg;
    const double GAMMA_ = inter1.GAMMA_;
    const double xi1 = inter1.xi1;
    const double xi2 = inter1.xi2;
    const double fUz = inter1.fUz;
    const double qs = inter1.qs;
    const double C = inter1.C;
    const double D = inter1.D;
    const double rpn = inter1.rpn;
    const double p1p_0 = inter1.p1p_0;
    const double p2p_0 = inter1.p2p_0;
    const double q1p_0 = inter1.q1p_0;
    const double q2p_0 = inter1.q2p_0;
    const double p1pp_0 = inter2.p1pp_0;
    const double p2pp_0 = inter2.p2pp_0;
    const double q1pp_0 = inter2.q1pp_0;
    const double q2pp_0 = inter2.q2pp_0;
    const double rpp = inter2.rpp;
    const double cosLp = inter2.cosLp;
    const double sinLp = inter2.sinLp;
    const double hp = inter2.hp;
    const double dp = inter2.dp;
    const double whp = inter2.whp;
    const double Ip = inter2.Ip;
    const double Up = inter2.Up;
    const double bp = inter2.bp;
    const double cp = inter2.cp;
    const double alphap = inter2.alphap;
    const double deltap = inter2.deltap;
    const double zgp = inter2.zgp;
    const double fUzp = inter2.fUzp;
    const double GAMMAp_ = inter2.GAMMAp_;
    const double xi1p = inter2.xi1p;
    const double xi2p = inter2.xi2p;
    const double qsp = inter2.qsp;
    const double psp = inter2.psp;
    const double Cp = inter2.Cp;
    const double Dp = inter2.Dp;
    const double rpn_p = inter2.rpn_p;
    const double fiDp = inter2.fiDp;
    const double fibp = inter2.fibp;
    const double ficp = inter2.ficp;
    const double fihp = inter2.fihp;
    const double fialphap = inter2.fialphap;
    const double fibm1_p = inter2.fibm1_p;
    const double fihr3p = inter2.fihr3p;
    const double firp = inter1.firp;
    const double firp2 = inter2.firp2;
    const double fir2p = inter1.fir2p;
    const double fir2p2 = inter2.fir2p2;
    const double fir3p = inter1.fir3p;
    const double fir3p2 = inter2.fir3p2;
    const double f2rp = inter1.f2rp;
    const double f2rpp = inter2.f2rpp;
    const double f2cp = inter2.f2cp;
    const double r3p2 = inter2.r3p2;
    const double hr3p = inter2.hr3p;
    const double r_nu = inter1.r_nu;
    const double r_Lr = inter1.r_Lr;
    const double r_q1 = inter1.r_q1;
    const double r_q2 = inter1.r_q2;
    const double r_p1 = inter1.r_p1;
    const double r_p2 = inter1.r_p2;
    const double rp_nu = inter1.rp_nu;
    const double rp_Lr = inter1.rp_Lr;
    const double rp_q1 = inter1.rp_q1;
    const double rp_q2 = inter1.rp_q2;
    const double rp_p1 = inter1.rp_p1;
    const double rp_p2 = inter1.rp_p2;
    const double rpp_nu = inter2.rpp_nu;
    const double rpp_Lr = inter2.rpp_Lr;
    const double rpp_q1 = inter2.rpp_q1;
    const double rpp_q2 = inter2.rpp_q2;
    const double rpp_p1 = inter2.rpp_p1;
    const double rpp_p2 = inter2.rpp_p2;
    const double r2_nu = inter1.r2_nu;
    const double r2_Lr = inter1.r2_Lr;
    const double r2_q1 = inter1.r2_q1;
    const double r2_q2 = inter1.r2_q2;
    const double r2_p1 = inter1.r2_p1;
    const double r2_p2 = inter1.r2_p2;
    const double r3_nu = inter1.r3_nu;
    const double r3_Lr = inter1.r3_Lr;
    const double r3_q1 = inter1.r3_q1;
    const double r3_q2 = inter1.r3_q2;
    const double r3_p1 = inter1.r3_p1;
    const double r3_p2 = inter1.r3_p2;
    const double beta_nu = inter1.beta_nu;
    const double beta_Lr = inter1.beta_Lr;
    const double beta_q1 = inter1.beta_q1;
    const double beta_q2 = inter1.beta_q2;
    const double beta_p1 = inter1.beta_p1;
    const double beta_p2 = inter1.beta_p2;
    const double alpha_nu = inter1.alpha_nu;
    const double alpha_Lr = inter1.alpha_Lr;
    const double alpha_q1 = inter1.alpha_q1;
    const double alpha_q2 = inter1.alpha_q2;
    const double alpha_p1 = inter1.alpha_p1;
    const double alpha_p2 = inter1.alpha_p2;
    const double c_nu = inter1.c_nu;
    const double c_Lr = inter1.c_Lr;
    const double c_q1 = inter1.c_q1;
    const double c_q2 = inter1.c_q2;
    const double c_p1 = inter1.c_p1;
    const double c_p2 = inter1.c_p2;
    const double fic_nu = inter1.fic_nu;
    const double fic_Lr = inter1.fic_Lr;
    const double fic_q1 = inter1.fic_q1;
    const double fic_q2 = inter1.fic_q2;
    const double fic_p1 = inter1.fic_p1;
    const double fic_p2 = inter1.fic_p2;
    const double h_nu = inter1.h_nu;
    const double h_Lr = inter1.h_Lr;
    const double h_q1 = inter1.h_q1;
    const double h_q2 = inter1.h_q2;
    const double h_p1 = inter1.h_p1;
    const double h_p2 = inter1.h_p2;
    const double fih_nu = inter1.fih_nu;
    const double fih_Lr = inter1.fih_Lr;
    const double fih_q1 = inter1.fih_q1;
    const double fih_q2 = inter1.fih_q2;
    const double fih_p1 = inter1.fih_p1;
    const double fih_p2 = inter1.fih_p2;
    const double X_nu = inter1.X_nu;
    const double X_Lr = inter1.X_Lr;
    const double X_q1 = inter1.X_q1;
    const double X_q2 = inter1.X_q2;
    const double X_p1 = inter1.X_p1;
    const double X_p2 = inter1.X_p2;
    const double Y_nu = inter1.Y_nu;
    const double Y_Lr = inter1.Y_Lr;
    const double Y_q1 = inter1.Y_q1;
    const double Y_q2 = inter1.Y_q2;
    const double Y_p1 = inter1.Y_p1;
    const double Y_p2 = inter1.Y_p2;
    const double cosL_nu = inter1.cosL_nu;
    const double cosL_Lr = inter1.cosL_Lr;
    const double cosL_q1 = inter1.cosL_q1;
    const double cosL_q2 = inter1.cosL_q2;
    const double cosL_p1 = inter1.cosL_p1;
    const double cosL_p2 = inter1.cosL_p2;
    const double sinL_nu = inter1.sinL_nu;
    const double sinL_Lr = inter1.sinL_Lr;
    const double sinL_q1 = inter1.sinL_q1;
    const double sinL_q2 = inter1.sinL_q2;
    const double sinL_p1 = inter1.sinL_p1;
    const double sinL_p2 = inter1.sinL_p2;
    const double zg_nu = inter1.zg_nu;
    const double zg_Lr = inter1.zg_Lr;
    const double zg_q1 = inter1.zg_q1;
    const double zg_q2 = inter1.zg_q2;
    const double zg_p1 = inter1.zg_p1;
    const double zg_p2 = inter1.zg_p2;
    const double fUz_nu = inter1.fUz_nu;
    const double fUz_Lr = inter1.fUz_Lr;
    const double fUz_q1 = inter1.fUz_q1;
    const double fUz_q2 = inter1.fUz_q2;
    const double fUz_p1 = inter1.fUz_p1;
    const double fUz_p2 = inter1.fUz_p2;
    const double U_nu = inter1.U_nu;
    const double U_Lr = inter1.U_Lr;
    const double U_q1 = inter1.U_q1;
    const double U_q2 = inter1.U_q2;
    const double U_p1 = inter1.U_p1;
    const double U_p2 = inter1.U_p2;
    const double delta_nu = inter1.delta_nu;
    const double delta_Lr = inter1.delta_Lr;
    const double delta_q1 = inter1.delta_q1;
    const double delta_q2 = inter1.delta_q2;
    const double delta_p1 = inter1.delta_p1;
    const double delta_p2 = inter1.delta_p2;
    const double I_nu = inter1.I_nu;
    const double I_Lr = inter1.I_Lr;
    const double I_q1 = inter1.I_q1;
    const double I_q2 = inter1.I_q2;
    const double I_p1 = inter1.I_p1;
    const double I_p2 = inter1.I_p2;
    const double d_nu = inter1.d_nu;
    const double d_Lr = inter1.d_Lr;
    const double d_q1 = inter1.d_q1;
    const double d_q2 = inter1.d_q2;
    const double d_p1 = inter1.d_p1;
    const double d_p2 = inter1.d_p2;
    const double wh_nu = inter1.wh_nu;
    const double wh_Lr = inter1.wh_Lr;
    const double wh_q1 = inter1.wh_q1;
    const double wh_q2 = inter1.wh_q2;
    const double wh_p1 = inter1.wh_p1;
    const double wh_p2 = inter1.wh_p2;
    const double GAMMA_nu = inter1.GAMMA_nu;
    const double GAMMA_Lr = inter1.GAMMA_Lr;
    const double GAMMA_q1 = inter1.GAMMA_q1;
    const double GAMMA_q2 = inter1.GAMMA_q2;
    const double GAMMA_p1 = inter1.GAMMA_p1;
    const double GAMMA_p2 = inter1.GAMMA_p2;
    const double xi1_nu = inter1.xi1_nu;
    const double xi1_Lr = inter1.xi1_Lr;
    const double xi1_q1 = inter1.xi1_q1;
    const double xi1_q2 = inter1.xi1_q2;
    const double xi1_p1 = inter1.xi1_p1;
    const double xi1_p2 = inter1.xi1_p2;
    const double xi2_nu = inter1.xi2_nu;
    const double xi2_Lr = inter1.xi2_Lr;
    const double xi2_q1 = inter1.xi2_q1;
    const double xi2_q2 = inter1.xi2_q2;
    const double xi2_p1 = inter1.xi2_p1;
    const double xi2_p2 = inter1.xi2_p2;
    const double q1p_nu = inter1.q1p_nu;
    const double q1p_Lr = inter1.q1p_Lr;
    const double q1p_q1 = inter1.q1p_q1;
    const double q1p_q2 = inter1.q1p_q2;
    const double q1p_p1 = inter1.q1p_p1;
    const double q1p_p2 = inter1.q1p_p2;
    const double q2p_nu = inter1.q2p_nu;
    const double q2p_Lr = inter1.q2p_Lr;
    const double q2p_q1 = inter1.q2p_q1;
    const double q2p_q2 = inter1.q2p_q2;
    const double q2p_p1 = inter1.q2p_p1;
    const double q2p_p2 = inter1.q2p_p2;
    const double p1p_nu = inter1.p1p_nu;
    const double p1p_Lr = inter1.p1p_Lr;
    const double p1p_q1 = inter1.p1p_q1;
    const double p1p_q2 = inter1.p1p_q2;
    const double p1p_p1 = inter1.p1p_p1;
    const double p1p_p2 = inter1.p1p_p2;
    const double p2p_nu = inter1.p2p_nu;
    const double p2p_Lr = inter1.p2p_Lr;
    const double p2p_q1 = inter1.p2p_q1;
    const double p2p_q2 = inter1.p2p_q2;
    const double p2p_p1 = inter1.p2p_p1;
    const double p2p_p2 = inter1.p2p_p2;
    const double rpn_nu = inter1.rpn_nu;
    const double rpn_Lr = inter1.rpn_Lr;
    const double rpn_q1 = inter1.rpn_q1;
    const double rpn_q2 = inter1.rpn_q2;
    const double rpn_p1 = inter1.rpn_p1;
    const double rpn_p2 = inter1.rpn_p2;
    const double D_nu = inter1.D_nu;
    const double D_Lr = inter1.D_Lr;
    const double D_q1 = inter1.D_q1;
    const double D_q2 = inter1.D_q2;
    const double D_p1 = inter1.D_p1;
    const double D_p2 = inter1.D_p2;
    const double fiD_nu = inter1.fiD_nu;
    const double fiD_Lr = inter1.fiD_Lr;
    const double fiD_q1 = inter1.fiD_q1;
    const double fiD_q2 = inter1.fiD_q2;
    const double fiD_p1 = inter1.fiD_p1;
    const double fiD_p2 = inter1.fiD_p2;
    const double C_nu = inter1.C_nu;
    const double C_Lr = inter1.C_Lr;
    const double C_q1 = inter1.C_q1;
    const double C_q2 = inter1.C_q2;
    const double C_p1 = inter1.C_p1;
    const double C_p2 = inter1.C_p2;
    const double fir3_p_nu = inter1.fir3_p_nu;
    const double fir3_p_Lr = inter1.fir3_p_Lr;
    const double fir3_p_q1 = inter1.fir3_p_q1;
    const double fir3_p_q2 = inter1.fir3_p_q2;
    const double fir3_p_p1 = inter1.fir3_p_p1;
    const double fir3_p_p2 = inter1.fir3_p_p2;
    const double fir_nu = inter1.fir_nu;
    const double fir_Lr = inter1.fir_Lr;
    const double fir_q1 = inter1.fir_q1;
    const double fir_q2 = inter1.fir_q2;
    const double fir_p1 = inter1.fir_p1;
    const double fir_p2 = inter1.fir_p2;
    const double firp_nu = inter1.firp_nu;
    const double firp_Lr = inter1.firp_Lr;
    const double firp_q1 = inter1.firp_q1;
    const double firp_q2 = inter1.firp_q2;
    const double firp_p1 = inter1.firp_p1;
    const double firp_p2 = inter1.firp_p2;
    const double fir2_nu = inter1.fir2_nu;
    const double fir2_Lr = inter1.fir2_Lr;
    const double fir2_q1 = inter1.fir2_q1;
    const double fir2_q2 = inter1.fir2_q2;
    const double fir2_p1 = inter1.fir2_p1;
    const double fir2_p2 = inter1.fir2_p2;
    const double fir2p_nu = inter1.fir2p_nu;
    const double fir2p_Lr = inter1.fir2p_Lr;
    const double fir2p_q1 = inter1.fir2p_q1;
    const double fir2p_q2 = inter1.fir2p_q2;
    const double fir2p_p1 = inter1.fir2p_p1;
    const double fir2p_p2 = inter1.fir2p_p2;
    const double fir3_nu = inter1.fir3_nu;
    const double fir3_Lr = inter1.fir3_Lr;
    const double fir3_q1 = inter1.fir3_q1;
    const double fir3_q2 = inter1.fir3_q2;
    const double fir3_p1 = inter1.fir3_p1;
    const double fir3_p2 = inter1.fir3_p2;
    const double f2rp_nu = inter1.f2rp_nu;
    const double f2rp_Lr = inter1.f2rp_Lr;
    const double f2rp_q1 = inter1.f2rp_q1;
    const double f2rp_q2 = inter1.f2rp_q2;
    const double f2rp_p1 = inter1.f2rp_p1;
    const double f2rp_p2 = inter1.f2rp_p2;
    const double hr3_nu = inter1.hr3_nu;
    const double hr3_Lr = inter1.hr3_Lr;
    const double hr3_q1 = inter1.hr3_q1;
    const double hr3_q2 = inter1.hr3_q2;
    const double hr3_p1 = inter1.hr3_p1;
    const double hr3_p2 = inter1.hr3_p2;
    const double fihr3_nu = inter1.fihr3_nu;
    const double fihr3_Lr = inter1.fihr3_Lr;
    const double fihr3_q1 = inter1.fihr3_q1;
    const double fihr3_q2 = inter1.fihr3_q2;
    const double fihr3_p1 = inter1.fihr3_p1;
    const double fihr3_p2 = inter1.fihr3_p2;
    const double Xp_nu = inter2.Xp_nu;
    const double Xp_Lr = inter2.Xp_Lr;
    const double Xp_q1 = inter2.Xp_q1;
    const double Xp_q2 = inter2.Xp_q2;
    const double Xp_p1 = inter2.Xp_p1;
    const double Xp_p2 = inter2.Xp_p2;
    const double Yp_nu = inter2.Yp_nu;
    const double Yp_Lr = inter2.Yp_Lr;
    const double Yp_q1 = inter2.Yp_q1;
    const double Yp_q2 = inter2.Yp_q2;
    const double Yp_p1 = inter2.Yp_p1;
    const double Yp_p2 = inter2.Yp_p2;
    const double cosLp_nu = inter2.cosLp_nu;
    const double cosLp_Lr = inter2.cosLp_Lr;
    const double cosLp_q1 = inter2.cosLp_q1;
    const double cosLp_q2 = inter2.cosLp_q2;
    const double cosLp_p1 = inter2.cosLp_p1;
    const double cosLp_p2 = inter2.cosLp_p2;
    const double sinLp_nu = inter2.sinLp_nu;
    const double sinLp_Lr = inter2.sinLp_Lr;
    const double sinLp_q1 = inter2.sinLp_q1;
    const double sinLp_q2 = inter2.sinLp_q2;
    const double sinLp_p1 = inter2.sinLp_p1;
    const double sinLp_p2 = inter2.sinLp_p2;
    const double zgp_nu = inter2.zgp_nu;
    const double zgp_Lr = inter2.zgp_Lr;
    const double zgp_q1 = inter2.zgp_q1;
    const double zgp_q2 = inter2.zgp_q2;
    const double zgp_p1 = inter2.zgp_p1;
    const double zgp_p2 = inter2.zgp_p2;
    const double fUzp_nu = inter2.fUzp_nu;
    const double fUzp_Lr = inter2.fUzp_Lr;
    const double fUzp_q1 = inter2.fUzp_q1;
    const double fUzp_q2 = inter2.fUzp_q2;
    const double fUzp_p1 = inter2.fUzp_p1;
    const double fUzp_p2 = inter2.fUzp_p2;
    const double Up_nu = inter2.Up_nu;
    const double Up_Lr = inter2.Up_Lr;
    const double Up_q1 = inter2.Up_q1;
    const double Up_q2 = inter2.Up_q2;
    const double Up_p1 = inter2.Up_p1;
    const double Up_p2 = inter2.Up_p2;
    const double bp_nu = inter2.bp_nu;
    const double bp_Lr = inter2.bp_Lr;
    const double bp_q1 = inter2.bp_q1;
    const double bp_q2 = inter2.bp_q2;
    const double bp_p1 = inter2.bp_p1;
    const double bp_p2 = inter2.bp_p2;
    const double cp_nu = inter2.cp_nu;
    const double cp_Lr = inter2.cp_Lr;
    const double cp_q1 = inter2.cp_q1;
    const double cp_q2 = inter2.cp_q2;
    const double cp_p1 = inter2.cp_p1;
    const double cp_p2 = inter2.cp_p2;
    const double hp_nu = inter2.hp_nu;
    const double hp_Lr = inter2.hp_Lr;
    const double hp_q1 = inter2.hp_q1;
    const double hp_q2 = inter2.hp_q2;
    const double hp_p1 = inter2.hp_p1;
    const double hp_p2 = inter2.hp_p2;
    const double alphap_nu = inter2.alphap_nu;
    const double alphap_Lr = inter2.alphap_Lr;
    const double alphap_q1 = inter2.alphap_q1;
    const double alphap_q2 = inter2.alphap_q2;
    const double alphap_p1 = inter2.alphap_p1;
    const double alphap_p2 = inter2.alphap_p2;
    const double deltap_nu = inter2.deltap_nu;
    const double deltap_Lr = inter2.deltap_Lr;
    const double deltap_q1 = inter2.deltap_q1;
    const double deltap_q2 = inter2.deltap_q2;
    const double deltap_p1 = inter2.deltap_p1;
    const double deltap_p2 = inter2.deltap_p2;
    const double Ip_nu = inter2.Ip_nu;
    const double Ip_Lr = inter2.Ip_Lr;
    const double Ip_q1 = inter2.Ip_q1;
    const double Ip_q2 = inter2.Ip_q2;
    const double Ip_p1 = inter2.Ip_p1;
    const double Ip_p2 = inter2.Ip_p2;
    const double dp_nu = inter2.dp_nu;
    const double dp_Lr = inter2.dp_Lr;
    const double dp_q1 = inter2.dp_q1;
    const double dp_q2 = inter2.dp_q2;
    const double dp_p1 = inter2.dp_p1;
    const double dp_p2 = inter2.dp_p2;
    const double whp_nu = inter2.whp_nu;
    const double whp_Lr = inter2.whp_Lr;
    const double whp_q1 = inter2.whp_q1;
    const double whp_q2 = inter2.whp_q2;
    const double whp_p1 = inter2.whp_p1;
    const double whp_p2 = inter2.whp_p2;
    const double GAMMAp_nu = inter2.GAMMAp_nu;
    const double GAMMAp_Lr = inter2.GAMMAp_Lr;
    const double GAMMAp_q1 = inter2.GAMMAp_q1;
    const double GAMMAp_q2 = inter2.GAMMAp_q2;
    const double GAMMAp_p1 = inter2.GAMMAp_p1;
    const double GAMMAp_p2 = inter2.GAMMAp_p2;
    const double xi1p_nu = inter2.xi1p_nu;
    const double xi1p_Lr = inter2.xi1p_Lr;
    const double xi1p_q1 = inter2.xi1p_q1;
    const double xi1p_q2 = inter2.xi1p_q2;
    const double xi1p_p1 = inter2.xi1p_p1;
    const double xi1p_p2 = inter2.xi1p_p2;
    const double xi2p_nu = inter2.xi2p_nu;
    const double xi2p_Lr = inter2.xi2p_Lr;
    const double xi2p_q1 = inter2.xi2p_q1;
    const double xi2p_q2 = inter2.xi2p_q2;
    const double xi2p_p1 = inter2.xi2p_p1;
    const double xi2p_p2 = inter2.xi2p_p2;
    const double qsp_nu = inter2.qsp_nu;
    const double qsp_Lr = inter2.qsp_Lr;
    const double qsp_q1 = inter2.qsp_q1;
    const double qsp_q2 = inter2.qsp_q2;
    const double qsp_p1 = inter2.qsp_p1;
    const double qsp_p2 = inter2.qsp_p2;
    const double psp_nu = inter2.psp_nu;
    const double psp_Lr = inter2.psp_Lr;
    const double psp_q1 = inter2.psp_q1;
    const double psp_q2 = inter2.psp_q2;
    const double psp_p1 = inter2.psp_p1;
    const double psp_p2 = inter2.psp_p2;
    const double Cp_nu = inter2.Cp_nu;
    const double Cp_Lr = inter2.Cp_Lr;
    const double Cp_q1 = inter2.Cp_q1;
    const double Cp_q2 = inter2.Cp_q2;
    const double Cp_p1 = inter2.Cp_p1;
    const double Cp_p2 = inter2.Cp_p2;
    const double Dp_nu = inter2.Dp_nu;
    const double Dp_Lr = inter2.Dp_Lr;
    const double Dp_q1 = inter2.Dp_q1;
    const double Dp_q2 = inter2.Dp_q2;
    const double Dp_p1 = inter2.Dp_p1;
    const double Dp_p2 = inter2.Dp_p2;
    const double fiDp_nu = inter2.fiDp_nu;
    const double fiDp_Lr = inter2.fiDp_Lr;
    const double fiDp_q1 = inter2.fiDp_q1;
    const double fiDp_q2 = inter2.fiDp_q2;
    const double fiDp_p1 = inter2.fiDp_p1;
    const double fiDp_p2 = inter2.fiDp_p2;
    const double rpn_p_nu = inter2.rpn_p_nu;
    const double rpn_p_Lr = inter2.rpn_p_Lr;
    const double rpn_p_q1 = inter2.rpn_p_q1;
    const double rpn_p_q2 = inter2.rpn_p_q2;
    const double rpn_p_p1 = inter2.rpn_p_p1;
    const double rpn_p_p2 = inter2.rpn_p_p2;
    const double hr3p_nu = inter2.hr3p_nu;
    const double hr3p_Lr = inter2.hr3p_Lr;
    const double hr3p_q1 = inter2.hr3p_q1;
    const double hr3p_q2 = inter2.hr3p_q2;
    const double hr3p_p1 = inter2.hr3p_p1;
    const double hr3p_p2 = inter2.hr3p_p2;
    const double fihr3p_nu = inter2.fihr3p_nu;
    const double fihr3p_Lr = inter2.fihr3p_Lr;
    const double fihr3p_q1 = inter2.fihr3p_q1;
    const double fihr3p_q2 = inter2.fihr3p_q2;
    const double fihr3p_p1 = inter2.fihr3p_p1;
    const double fihr3p_p2 = inter2.fihr3p_p2;
    const double firp2_nu = inter2.firp2_nu;
    const double firp2_Lr = inter2.firp2_Lr;
    const double firp2_q1 = inter2.firp2_q1;
    const double firp2_q2 = inter2.firp2_q2;
    const double firp2_p1 = inter2.firp2_p1;
    const double firp2_p2 = inter2.firp2_p2;
    const double fir2p2_nu = inter2.fir2p2_nu;
    const double fir2p2_Lr = inter2.fir2p2_Lr;
    const double fir2p2_q1 = inter2.fir2p2_q1;
    const double fir2p2_q2 = inter2.fir2p2_q2;
    const double fir2p2_p1 = inter2.fir2p2_p1;
    const double fir2p2_p2 = inter2.fir2p2_p2;
    const double fir3p2_nu = inter2.fir3p2_nu;
    const double fir3p2_Lr = inter2.fir3p2_Lr;
    const double fir3p2_q1 = inter2.fir3p2_q1;
    const double fir3p2_q2 = inter2.fir3p2_q2;
    const double fir3p2_p1 = inter2.fir3p2_p1;
    const double fir3p2_p2 = inter2.fir3p2_p2;
    const double f2rpp_nu = inter2.f2rpp_nu;
    const double f2rpp_Lr = inter2.f2rpp_Lr;
    const double f2rpp_q1 = inter2.f2rpp_q1;
    const double f2rpp_q2 = inter2.f2rpp_q2;
    const double f2rpp_p1 = inter2.f2rpp_p1;
    const double f2rpp_p2 = inter2.f2rpp_p2;
    const double f2cp_nu = inter2.f2cp_nu;
    const double f2cp_Lr = inter2.f2cp_Lr;
    const double f2cp_q1 = inter2.f2cp_q1;
    const double f2cp_q2 = inter2.f2cp_q2;
    const double f2cp_p1 = inter2.f2cp_p1;
    const double f2cp_p2 = inter2.f2cp_p2;
    const double ficp_nu = inter2.ficp_nu;
    const double ficp_Lr = inter2.ficp_Lr;
    const double ficp_q1 = inter2.ficp_q1;
    const double ficp_q2 = inter2.ficp_q2;
    const double ficp_p1 = inter2.ficp_p1;
    const double ficp_p2 = inter2.ficp_p2;
    const double fihp_nu = inter2.fihp_nu;
    const double fihp_Lr = inter2.fihp_Lr;
    const double fihp_q1 = inter2.fihp_q1;
    const double fihp_q2 = inter2.fihp_q2;
    const double fihp_p1 = inter2.fihp_p1;
    const double fihp_p2 = inter2.fihp_p2;
    const double fialphap_nu = inter2.fialphap_nu;
    const double fialphap_Lr = inter2.fialphap_Lr;
    const double fialphap_q1 = inter2.fialphap_q1;
    const double fialphap_q2 = inter2.fialphap_q2;
    const double fialphap_p1 = inter2.fialphap_p1;
    const double fialphap_p2 = inter2.fialphap_p2;
    const double fib_nu = inter2.fib_nu;
    const double fib_Lr = inter2.fib_Lr;
    const double fib_q1 = inter2.fib_q1;
    const double fib_q2 = inter2.fib_q2;
    const double fib_p1 = inter2.fib_p1;
    const double fib_p2 = inter2.fib_p2;
    const double fibp_nu = inter2.fibp_nu;
    const double fibp_Lr = inter2.fibp_Lr;
    const double fibp_q1 = inter2.fibp_q1;
    const double fibp_q2 = inter2.fibp_q2;
    const double fibp_p1 = inter2.fibp_p1;
    const double fibp_p2 = inter2.fibp_p2;
    const double fibm1_p_nu = inter2.fibm1_p_nu;
    const double fibm1_p_Lr = inter2.fibm1_p_Lr;
    const double fibm1_p_q1 = inter2.fibm1_p_q1;
    const double fibm1_p_q2 = inter2.fibm1_p_q2;
    const double fibm1_p_p1 = inter2.fibm1_p_p1;
    const double fibm1_p_p2 = inter2.fibm1_p_p2;
    const double hr_nu = inter2.hr_nu;
    const double hr_Lr = inter2.hr_Lr;
    const double hr_q1 = inter2.hr_q1;
    const double hr_q2 = inter2.hr_q2;
    const double hr_p1 = inter2.hr_p1;
    const double hr_p2 = inter2.hr_p2;
    const double qs_nu = inter2.qs_nu;
    const double qs_Lr = inter2.qs_Lr;
    const double qs_q1 = inter2.qs_q1;
    const double qs_q2 = inter2.qs_q2;
    const double qs_p1 = inter2.qs_p1;
    const double qs_p2 = inter2.qs_p2;
    const double q1p2_nu = inter2.q1p2_nu;
    const double q1p2_Lr = inter2.q1p2_Lr;
    const double q1p2_q1 = inter2.q1p2_q1;
    const double q1p2_q2 = inter2.q1p2_q2;
    const double q1p2_p1 = inter2.q1p2_p1;
    const double q1p2_p2 = inter2.q1p2_p2;
    const double q2p2_nu = inter2.q2p2_nu;
    const double q2p2_Lr = inter2.q2p2_Lr;
    const double q2p2_q1 = inter2.q2p2_q1;
    const double q2p2_q2 = inter2.q2p2_q2;
    const double q2p2_p1 = inter2.q2p2_p1;
    const double q2p2_p2 = inter2.q2p2_p2;
    const double p1p2_nu = inter2.p1p2_nu;
    const double p1p2_Lr = inter2.p1p2_Lr;
    const double p1p2_q1 = inter2.p1p2_q1;
    const double p1p2_q2 = inter2.p1p2_q2;
    const double p1p2_p1 = inter2.p1p2_p1;
    const double p1p2_p2 = inter2.p1p2_p2;
    const double p2p2_nu = inter2.p2p2_nu;
    const double p2p2_Lr = inter2.p2p2_Lr;
    const double p2p2_q1 = inter2.p2p2_q1;
    const double p2p2_q2 = inter2.p2p2_q2;
    const double p2p2_p1 = inter2.p2p2_p1;
    const double p2p2_p2 = inter2.p2p2_p2;
    const double qsp2 = 2.0 * ((q1p_0 * q1p_0) + q1_0 * q1pp_0 + (q2p_0 * q2p_0) + q2_0 * q2pp_0);
    const double psp2 = 2.0 * ((p1p_0 * p1p_0) + p1_0 * p1pp_0 + (p2p_0 * p2p_0) + p2_0 * p2pp_0);
    const double qsp2_nu = 2.0 * (2.0 * q1p_0 * q1p_nu + q1_0 * q1p2_nu + 2.0 * q2p_0 * q2p_nu + q2_0 * q2p2_nu);
    const double qsp2_Lr = 2.0 * (2.0 * q1p_0 * q1p_Lr + q1_0 * q1p2_Lr + 2.0 * q2p_0 * q2p_Lr + q2_0 * q2p2_Lr);
    const double qsp2_q1 = 2.0 * (2.0 * q1p_0 * q1p_q1 + q1pp_0 + q1_0 * q1p2_q1 + 2.0 * q2p_0 * q2p_q1 + q2_0 * q2p2_q1);
    const double qsp2_q2 = 2.0 * (2.0 * q1p_0 * q1p_q2 + q1_0 * q1p2_q2 + 2.0 * q2p_0 * q2p_q2 + q2pp_0 + q2_0 * q2p2_q2);
    const double qsp2_p1 = 2.0 * (2.0 * q1p_0 * q1p_p1 + q1_0 * q1p2_p1 + 2.0 * q2p_0 * q2p_p1 + q2_0 * q2p2_p1);
    const double qsp2_p2 = 2.0 * (2.0 * q1p_0 * q1p_p2 + q1_0 * q1p2_p2 + 2.0 * q2p_0 * q2p_p2 + q2_0 * q2p2_p2);
    const double psp2_nu = 2.0 * (2.0 * p1p_0 * p1p_nu + p1_0 * p1p2_nu + 2.0 * p2p_0 * p2p_nu + p2_0 * p2p2_nu);
    const double psp2_Lr = 2.0 * (2.0 * p1p_0 * p1p_Lr + p1_0 * p1p2_Lr + 2.0 * p2p_0 * p2p_Lr + p2_0 * p2p2_Lr);
    const double psp2_q1 = 2.0 * (2.0 * p1p_0 * p1p_q1 + p1_0 * p1p2_q1 + 2.0 * p2p_0 * p2p_q1 + p2_0 * p2p2_q1);
    const double psp2_q2 = 2.0 * (2.0 * p1p_0 * p1p_q2 + p1_0 * p1p2_q2 + 2.0 * p2p_0 * p2p_q2 + p2_0 * p2p2_q2);
    const double psp2_p1 = 2.0 * (2.0 * p1p_0 * p1p_p1 + p1pp_0 + p1_0 * p1p2_p1 + 2.0 * p2p_0 * p2p_p1 + p2_0 * p2p2_p1);
    const double psp2_p2 = 2.0 * (2.0 * p1p_0 * p1p_p2 + p1_0 * p1p2_p2 + 2.0 * p2p_0 * p2p_p2 + p2pp_0 + p2_0 * p2p2_p2);
    const double hrp = hp * fir + h * firp;
    const double Xpp = rpp * cosL + rp * cosLp - hrp * sinL - hr * sinLp;
    const double Ypp = rpp * sinL + rp * sinLp + hrp * cosL + hr * cosLp;
    const double hrp_nu = hp_nu * fir + hp * fir_nu + h_nu * firp + h * firp_nu;
    const double hrp_Lr = hp_Lr * fir + hp * fir_Lr + h_Lr * firp + h * firp_Lr;
    const double hrp_q1 = hp_q1 * fir + hp * fir_q1 + h_q1 * firp + h * firp_q1;
    const double hrp_q2 = hp_q2 * fir + hp * fir_q2 + h_q2 * firp + h * firp_q2;
    const double hrp_p1 = hp_p1 * fir + hp * fir_p1 + h_p1 * firp + h * firp_p1;
    const double hrp_p2 = hp_p2 * fir + hp * fir_p2 + h_p2 * firp + h * firp_p2;
    const double Xpp_nu = rpp_nu * cosL + rpp * cosL_nu + rp_nu * cosLp + rp * cosLp_nu - hrp_nu * sinL - hrp * sinL_nu - hr_nu * sinLp - hr * sinLp_nu;
    const double Xpp_Lr = rpp_Lr * cosL + rpp * cosL_Lr + rp_Lr * cosLp + rp * cosLp_Lr - hrp_Lr * sinL - hrp * sinL_Lr - hr_Lr * sinLp - hr * sinLp_Lr;
    const double Xpp_q1 = rpp_q1 * cosL + rpp * cosL_q1 + rp_q1 * cosLp + rp * cosLp_q1 - hrp_q1 * sinL - hrp * sinL_q1 - hr_q1 * sinLp - hr * sinLp_q1;
    const double Xpp_q2 = rpp_q2 * cosL + rpp * cosL_q2 + rp_q2 * cosLp + rp * cosLp_q2 - hrp_q2 * sinL - hrp * sinL_q2 - hr_q2 * sinLp - hr * sinLp_q2;
    const double Xpp_p1 = rpp_p1 * cosL + rpp * cosL_p1 + rp_p1 * cosLp + rp * cosLp_p1 - hrp_p1 * sinL - hrp * sinL_p1 - hr_p1 * sinLp - hr * sinLp_p1;
    const double Xpp_p2 = rpp_p2 * cosL + rpp * cosL_p2 + rp_p2 * cosLp + rp * cosLp_p2 - hrp_p2 * sinL - hrp * sinL_p2 - hr_p2 * sinLp - hr * sinLp_p2;
    const double Ypp_nu = rpp_nu * sinL + rpp * sinL_nu + rp_nu * sinLp + rp * sinLp_nu + hrp_nu * cosL + hrp * cosL_nu + hr_nu * cosLp + hr * cosLp_nu;
    const double Ypp_Lr = rpp_Lr * sinL + rpp * sinL_Lr + rp_Lr * sinLp + rp * sinLp_Lr + hrp_Lr * cosL + hrp * cosL_Lr + hr_Lr * cosLp + hr * cosLp_Lr;
    const double Ypp_q1 = rpp_q1 * sinL + rpp * sinL_q1 + rp_q1 * sinLp + rp * sinLp_q1 + hrp_q1 * cosL + hrp * cosL_q1 + hr_q1 * cosLp + hr * cosLp_q1;
    const double Ypp_q2 = rpp_q2 * sinL + rpp * sinL_q2 + rp_q2 * sinLp + rp * sinLp_q2 + hrp_q2 * cosL + hrp * cosL_q2 + hr_q2 * cosLp + hr * cosLp_q2;
    const double Ypp_p1 = rpp_p1 * sinL + rpp * sinL_p1 + rp_p1 * sinLp + rp * sinLp_p1 + hrp_p1 * cosL + hrp * cosL_p1 + hr_p1 * cosLp + hr * cosLp_p1;
    const double Ypp_p2 = rpp_p2 * sinL + rpp * sinL_p2 + rp_p2 * sinLp + rp * sinLp_p2 + hrp_p2 * cosL + hrp * cosL_p2 + hr_p2 * cosLp + hr * cosLp_p2;
    const double Cpp = 2.0 * (Ypp * q2_0 + 2.0 * Yp * q2p_0 + Y * q2pp_0 - (Xpp * q1_0 + 2.0 * Xp * q1p_0 + X * q1pp_0));
    const double Dpp = rpp * qs + 2.0 * rp * qsp + r * qsp2;
    const double fiDp2 = derivatives_of_inverse_3(D, Dp, Dpp);
    const double zgpp = Cpp * fiD + 2.0 * Cp * fiDp + C * fiDp2;
    const double Cpp_nu = 2.0 * (Ypp_nu * q2_0 + 2.0 * (Yp_nu * q2p_0 + Yp * q2p_nu) + Y_nu * q2pp_0 + Y * q2p2_nu - (Xpp_nu * q1_0 + 2.0 * (Xp_nu * q1p_0 + Xp * q1p_nu) + X_nu * q1pp_0 + X * q1p2_nu));
    const double Cpp_Lr = 2.0 * (Ypp_Lr * q2_0 + 2.0 * (Yp_Lr * q2p_0 + Yp * q2p_Lr) + Y_Lr * q2pp_0 + Y * q2p2_Lr - (Xpp_Lr * q1_0 + 2.0 * (Xp_Lr * q1p_0 + Xp * q1p_Lr) + X_Lr * q1pp_0 + X * q1p2_Lr));
    const double Cpp_q1 = 2.0 * (Ypp_q1 * q2_0 + 2.0 * (Yp_q1 * q2p_0 + Yp * q2p_q1) + Y_q1 * q2pp_0 + Y * q2p2_q1 - (Xpp + Xpp_q1 * q1_0 + 2.0 * (Xp_q1 * q1p_0 + Xp * q1p_q1) + X_q1 * q1pp_0 + X * q1p2_q1));
    const double Cpp_q2 = 2.0 * (Ypp + Ypp_q2 * q2_0 + 2.0 * (Yp_q2 * q2p_0 + Yp * q2p_q2) + Y_q2 * q2pp_0 + Y * q2p2_q2 - (Xpp_q2 * q1_0 + 2.0 * (Xp_q2 * q1p_0 + Xp * q1p_q2) + X_q2 * q1pp_0 + X * q1p2_q2));
    const double Cpp_p1 = 2.0 * (Ypp_p1 * q2_0 + 2.0 * (Yp_p1 * q2p_0 + Yp * q2p_p1) + Y_p1 * q2pp_0 + Y * q2p2_p1 - (Xpp_p1 * q1_0 + 2.0 * (Xp_p1 * q1p_0 + Xp * q1p_p1) + X_p1 * q1pp_0 + X * q1p2_p1));
    const double Cpp_p2 = 2.0 * (Ypp_p2 * q2_0 + 2.0 * (Yp_p2 * q2p_0 + Yp * q2p_p2) + Y_p2 * q2pp_0 + Y * q2p2_p2 - (Xpp_p2 * q1_0 + 2.0 * (Xp_p2 * q1p_0 + Xp * q1p_p2) + X_p2 * q1pp_0 + X * q1p2_p2));
    const double Dpp_nu = rpp_nu * qs + rpp * qs_nu + 2.0 * (rp_nu * qsp + rp * qsp_nu) + r_nu * qsp2 + r * qsp2_nu;
    const double Dpp_Lr = rpp_Lr * qs + rpp * qs_Lr + 2.0 * (rp_Lr * qsp + rp * qsp_Lr) + r_Lr * qsp2 + r * qsp2_Lr;
    const double Dpp_q1 = rpp_q1 * qs + rpp * qs_q1 + 2.0 * (rp_q1 * qsp + rp * qsp_q1) + r_q1 * qsp2 + r * qsp2_q1;
    const double Dpp_q2 = rpp_q2 * qs + rpp * qs_q2 + 2.0 * (rp_q2 * qsp + rp * qsp_q2) + r_q2 * qsp2 + r * qsp2_q2;
    const double Dpp_p1 = rpp_p1 * qs + rpp * qs_p1 + 2.0 * (rp_p1 * qsp + rp * qsp_p1) + r_p1 * qsp2 + r * qsp2_p1;
    const double Dpp_p2 = rpp_p2 * qs + rpp * qs_p2 + 2.0 * (rp_p2 * qsp + rp * qsp_p2) + r_p2 * qsp2 + r * qsp2_p2;
    const double fiDp2_nu = derivatives_of_inverse_wrt_param_3(D, Dp, Dpp, D_nu, Dp_nu, Dpp_nu);
    const double fiDp2_Lr = derivatives_of_inverse_wrt_param_3(D, Dp, Dpp, D_Lr, Dp_Lr, Dpp_Lr);
    const double fiDp2_q1 = derivatives_of_inverse_wrt_param_3(D, Dp, Dpp, D_q1, Dp_q1, Dpp_q1);
    const double fiDp2_q2 = derivatives_of_inverse_wrt_param_3(D, Dp, Dpp, D_q2, Dp_q2, Dpp_q2);
    const double fiDp2_p1 = derivatives_of_inverse_wrt_param_3(D, Dp, Dpp, D_p1, Dp_p1, Dpp_p1);
    const double fiDp2_p2 = derivatives_of_inverse_wrt_param_3(D, Dp, Dpp, D_p2, Dp_p2, Dpp_p2);
    const double zgpp_nu = Cpp_nu * fiD + Cpp * fiD_nu + 2.0 * (Cp_nu * fiDp + Cp * fiDp_nu) + C_nu * fiDp2 + C * fiDp2_nu;
    const double zgpp_Lr = Cpp_Lr * fiD + Cpp * fiD_Lr + 2.0 * (Cp_Lr * fiDp + Cp * fiDp_Lr) + C_Lr * fiDp2 + C * fiDp2_Lr;
    const double zgpp_q1 = Cpp_q1 * fiD + Cpp * fiD_q1 + 2.0 * (Cp_q1 * fiDp + Cp * fiDp_q1) + C_q1 * fiDp2 + C * fiDp2_q1;
    const double zgpp_q2 = Cpp_q2 * fiD + Cpp * fiD_q2 + 2.0 * (Cp_q2 * fiDp + Cp * fiDp_q2) + C_q2 * fiDp2 + C * fiDp2_q2;
    const double zgpp_p1 = Cpp_p1 * fiD + Cpp * fiD_p1 + 2.0 * (Cp_p1 * fiDp + Cp * fiDp_p1) + C_p1 * fiDp2 + C * fiDp2_p1;
    const double zgpp_p2 = Cpp_p2 * fiD + Cpp * fiD_p2 + 2.0 * (Cp_p2 * fiDp + Cp * fiDp_p2) + C_p2 * fiDp2 + C * fiDp2_p2;
    const double fUzp2 = -6.0 * ((zgp * zgp) + zg * zgpp);
    const double Upp = -A * (fUzp2 * fir3 + 2.0 * fUzp * fir3p + fUz * fir3p2);
    const double fUzp2_nu = -6.0 * (2.0 * zgp * zgp_nu + zg_nu * zgpp + zg * zgpp_nu);
    const double fUzp2_Lr = -6.0 * (2.0 * zgp * zgp_Lr + zg_Lr * zgpp + zg * zgpp_Lr);
    const double fUzp2_q1 = -6.0 * (2.0 * zgp * zgp_q1 + zg_q1 * zgpp + zg * zgpp_q1);
    const double fUzp2_q2 = -6.0 * (2.0 * zgp * zgp_q2 + zg_q2 * zgpp + zg * zgpp_q2);
    const double fUzp2_p1 = -6.0 * (2.0 * zgp * zgp_p1 + zg_p1 * zgpp + zg * zgpp_p1);
    const double fUzp2_p2 = -6.0 * (2.0 * zgp * zgp_p2 + zg_p2 * zgpp + zg * zgpp_p2);
    const double Upp_nu = -A * (fUzp2_nu * fir3 + fUzp2 * fir3_nu + 2.0 * (fUzp_nu * fir3p + fUzp * fir3_p_nu) + fUz_nu * fir3p2 + fUz * fir3p2_nu);
    const double Upp_Lr = -A * (fUzp2_Lr * fir3 + fUzp2 * fir3_Lr + 2.0 * (fUzp_Lr * fir3p + fUzp * fir3_p_Lr) + fUz_Lr * fir3p2 + fUz * fir3p2_Lr);
    const double Upp_q1 = -A * (fUzp2_q1 * fir3 + fUzp2 * fir3_q1 + 2.0 * (fUzp_q1 * fir3p + fUzp * fir3_p_q1) + fUz_q1 * fir3p2 + fUz * fir3p2_q1);
    const double Upp_q2 = -A * (fUzp2_q2 * fir3 + fUzp2 * fir3_q2 + 2.0 * (fUzp_q2 * fir3p + fUzp * fir3_p_q2) + fUz_q2 * fir3p2 + fUz * fir3p2_q2);
    const double Upp_p1 = -A * (fUzp2_p1 * fir3 + fUzp2 * fir3_p1 + 2.0 * (fUzp_p1 * fir3p + fUzp * fir3_p_p1) + fUz_p1 * fir3p2 + fUz * fir3p2_p1);
    const double Upp_p2 = -A * (fUzp2_p2 * fir3 + fUzp2 * fir3_p2 + 2.0 * (fUzp_p2 * fir3p + fUzp * fir3_p_p2) + fUz_p2 * fir3p2 + fUz * fir3p2_p2);
    const double bpp = -1.0 / 2.0 * (psp2 * fib + psp * fibp);
    const double fibp2 = derivatives_of_inverse_3(beta, bp, bpp);
    const double bpp_nu = -1.0 / 2.0 * (psp2_nu * fib + psp2 * fib_nu + psp_nu * fibp + psp * fibp_nu);
    const double bpp_Lr = -1.0 / 2.0 * (psp2_Lr * fib + psp2 * fib_Lr + psp_Lr * fibp + psp * fibp_Lr);
    const double bpp_q1 = -1.0 / 2.0 * (psp2_q1 * fib + psp2 * fib_q1 + psp_q1 * fibp + psp * fibp_q1);
    const double bpp_q2 = -1.0 / 2.0 * (psp2_q2 * fib + psp2 * fib_q2 + psp_q2 * fibp + psp * fibp_q2);
    const double bpp_p1 = -1.0 / 2.0 * (psp2_p1 * fib + psp2 * fib_p1 + psp_p1 * fibp + psp * fibp_p1);
    const double bpp_p2 = -1.0 / 2.0 * (psp2_p2 * fib + psp2 * fib_p2 + psp_p2 * fibp + psp * fibp_p2);
    const double fibp2_nu = derivatives_of_inverse_wrt_param_3(beta, bp, bpp, beta_nu, bp_nu, bpp_nu);
    const double fibp2_Lr = derivatives_of_inverse_wrt_param_3(beta, bp, bpp, beta_Lr, bp_Lr, bpp_Lr);
    const double fibp2_q1 = derivatives_of_inverse_wrt_param_3(beta, bp, bpp, beta_q1, bp_q1, bpp_q1);
    const double fibp2_q2 = derivatives_of_inverse_wrt_param_3(beta, bp, bpp, beta_q2, bp_q2, bpp_q2);
    const double fibp2_p1 = derivatives_of_inverse_wrt_param_3(beta, bp, bpp, beta_p1, bp_p1, bpp_p1);
    const double fibp2_p2 = derivatives_of_inverse_wrt_param_3(beta, bp, bpp, beta_p2, bp_p2, bpp_p2);
    const double cpp = std::cbrt((mu_norm * mu_norm) / nu_0) * bpp;
    const double ficp2 = derivatives_of_inverse_3(c, cp, cpp);
    const double f2cp_p = derivatives_of_product_3(c, cp, cpp);
    const double cpp_nu = std::cbrt(mu_norm * mu_norm) * (bpp_nu * std::pow(nu_0, -1.0 / 3.0) + bpp * -1.0 / 3.0 * std::pow(nu_0, -4.0 / 3.0));
    const double cpp_Lr = std::cbrt(mu_norm * mu_norm) / std::cbrt(nu_0) * bpp_Lr;
    const double cpp_q1 = std::cbrt(mu_norm * mu_norm) / std::cbrt(nu_0) * bpp_q1;
    const double cpp_q2 = std::cbrt(mu_norm * mu_norm) / std::cbrt(nu_0) * bpp_q2;
    const double cpp_p1 = std::cbrt(mu_norm * mu_norm) / std::cbrt(nu_0) * bpp_p1;
    const double cpp_p2 = std::cbrt(mu_norm * mu_norm) / std::cbrt(nu_0) * bpp_p2;
    const double ficp2_nu = derivatives_of_inverse_wrt_param_3(c, cp, cpp, c_nu, cp_nu, cpp_nu);
    const double ficp2_Lr = derivatives_of_inverse_wrt_param_3(c, cp, cpp, c_Lr, cp_Lr, cpp_Lr);
    const double ficp2_q1 = derivatives_of_inverse_wrt_param_3(c, cp, cpp, c_q1, cp_q1, cpp_q1);
    const double ficp2_q2 = derivatives_of_inverse_wrt_param_3(c, cp, cpp, c_q2, cp_q2, cpp_q2);
    const double ficp2_p1 = derivatives_of_inverse_wrt_param_3(c, cp, cpp, c_p1, cp_p1, cpp_p1);
    const double ficp2_p2 = derivatives_of_inverse_wrt_param_3(c, cp, cpp, c_p2, cp_p2, cpp_p2);
    const double f2cp_p_nu = derivatives_of_product_wrt_param_3(c, cp, cpp, c_nu, cp_nu, cpp_nu);
    const double f2cp_p_Lr = derivatives_of_product_wrt_param_3(c, cp, cpp, c_Lr, cp_Lr, cpp_Lr);
    const double f2cp_p_q1 = derivatives_of_product_wrt_param_3(c, cp, cpp, c_q1, cp_q1, cpp_q1);
    const double f2cp_p_q2 = derivatives_of_product_wrt_param_3(c, cp, cpp, c_q2, cp_q2, cpp_q2);
    const double f2cp_p_p1 = derivatives_of_product_wrt_param_3(c, cp, cpp, c_p1, cp_p1, cpp_p1);
    const double f2cp_p_p2 = derivatives_of_product_wrt_param_3(c, cp, cpp, c_p2, cp_p2, cpp_p2);
    const double cosLp2 = Xpp * fir + 2.0 * Xp * firp + X * firp2;
    const double sinLp2 = Ypp * fir + 2.0 * Yp * firp + Y * firp2;
    const double cosLp2_nu = Xpp_nu * fir + Xpp * fir_nu + 2.0 * (Xp_nu * firp + Xp * firp_nu) + X_nu * firp2 + X * firp2_nu;
    const double cosLp2_Lr = Xpp_Lr * fir + Xpp * fir_Lr + 2.0 * (Xp_Lr * firp + Xp * firp_Lr) + X_Lr * firp2 + X * firp2_Lr;
    const double cosLp2_q1 = Xpp_q1 * fir + Xpp * fir_q1 + 2.0 * (Xp_q1 * firp + Xp * firp_q1) + X_q1 * firp2 + X * firp2_q1;
    const double cosLp2_q2 = Xpp_q2 * fir + Xpp * fir_q2 + 2.0 * (Xp_q2 * firp + Xp * firp_q2) + X_q2 * firp2 + X * firp2_q2;
    const double cosLp2_p1 = Xpp_p1 * fir + Xpp * fir_p1 + 2.0 * (Xp_p1 * firp + Xp * firp_p1) + X_p1 * firp2 + X * firp2_p1;
    const double cosLp2_p2 = Xpp_p2 * fir + Xpp * fir_p2 + 2.0 * (Xp_p2 * firp + Xp * firp_p2) + X_p2 * firp2 + X * firp2_p2;
    const double sinLp2_nu = Ypp_nu * fir + Ypp * fir_nu + 2.0 * (Yp_nu * firp + Yp * firp_nu) + Y_nu * firp2 + Y * firp2_nu;
    const double sinLp2_Lr = Ypp_Lr * fir + Ypp * fir_Lr + 2.0 * (Yp_Lr * firp + Yp * firp_Lr) + Y_Lr * firp2 + Y * firp2_Lr;
    const double sinLp2_q1 = Ypp_q1 * fir + Ypp * fir_q1 + 2.0 * (Yp_q1 * firp + Yp * firp_q1) + Y_q1 * firp2 + Y * firp2_q1;
    const double sinLp2_q2 = Ypp_q2 * fir + Ypp * fir_q2 + 2.0 * (Yp_q2 * firp + Yp * firp_q2) + Y_q2 * firp2 + Y * firp2_q2;
    const double sinLp2_p1 = Ypp_p1 * fir + Ypp * fir_p1 + 2.0 * (Yp_p1 * firp + Yp * firp_p1) + Y_p1 * firp2 + Y * firp2_p1;
    const double sinLp2_p2 = Ypp_p2 * fir + Ypp * fir_p2 + 2.0 * (Yp_p2 * firp + Yp * firp_p2) + Y_p2 * firp2 + Y * firp2_p2;
    const double rpnp2 = p2pp_0 * sinL + 2.0 * p2p_0 * sinLp + p2_0 * sinLp2 - (p1pp_0 * cosL + 2.0 * p1p_0 * cosLp + p1_0 * cosLp2);
    const double rp3 = mu_norm * (rpnp2 * fic + 2.0 * rpn_p * ficp + rpn * ficp2);
    const double firp3 = derivatives_of_inverse_4(r, rp, rpp, rp3);
    const double f2rpp2 = derivatives_of_product_4(r, rp, rpp, rp3);
    const double rpnp2_nu = p2p2_nu * sinL + p2pp_0 * sinL_nu + 2.0 * (p2p_nu * sinLp + p2p_0 * sinLp_nu) + p2_0 * sinLp2_nu - (p1p2_nu * cosL + p1pp_0 * cosL_nu + 2.0 * (p1p_nu * cosLp + p1p_0 * cosLp_nu) + p1_0 * cosLp2_nu);
    const double rpnp2_Lr = p2p2_Lr * sinL + p2pp_0 * sinL_Lr + 2.0 * (p2p_Lr * sinLp + p2p_0 * sinLp_Lr) + p2_0 * sinLp2_Lr - (p1p2_Lr * cosL + p1pp_0 * cosL_Lr + 2.0 * (p1p_Lr * cosLp + p1p_0 * cosLp_Lr) + p1_0 * cosLp2_Lr);
    const double rpnp2_q1 = p2p2_q1 * sinL + p2pp_0 * sinL_q1 + 2.0 * (p2p_q1 * sinLp + p2p_0 * sinLp_q1) + p2_0 * sinLp2_q1 - (p1p2_q1 * cosL + p1pp_0 * cosL_q1 + 2.0 * (p1p_q1 * cosLp + p1p_0 * cosLp_q1) + p1_0 * cosLp2_q1);
    const double rpnp2_q2 = p2p2_q2 * sinL + p2pp_0 * sinL_q2 + 2.0 * (p2p_q2 * sinLp + p2p_0 * sinLp_q2) + p2_0 * sinLp2_q2 - (p1p2_q2 * cosL + p1pp_0 * cosL_q2 + 2.0 * (p1p_q2 * cosLp + p1p_0 * cosLp_q2) + p1_0 * cosLp2_q2);
    const double rpnp2_p1 = p2p2_p1 * sinL + p2pp_0 * sinL_p1 + 2.0 * (p2p_p1 * sinLp + p2p_0 * sinLp_p1) + p2_0 * sinLp2_p1 - (p1p2_p1 * cosL + p1pp_0 * cosL_p1 + 2.0 * (p1p_p1 * cosLp + p1p_0 * cosLp_p1) + cosLp2 + p1_0 * cosLp2_p1);
    const double rpnp2_p2 = p2p2_p2 * sinL + p2pp_0 * sinL_p2 + 2.0 * (p2p_p2 * sinLp + p2p_0 * sinLp_p2) + sinLp2 + p2_0 * sinLp2_p2 - (p1p2_p2 * cosL + p1pp_0 * cosL_p2 + 2.0 * (p1p_p2 * cosLp + p1p_0 * cosLp_p2) + p1_0 * cosLp2_p2);
    const double rp3_nu = mu_norm * (rpnp2_nu * fic + rpnp2 * fic_nu + 2.0 * (rpn_p_nu * ficp + rpn_p * ficp_nu) + rpn_nu * ficp2 + rpn * ficp2_nu);
    const double rp3_Lr = mu_norm * (rpnp2_Lr * fic + rpnp2 * fic_Lr + 2.0 * (rpn_p_Lr * ficp + rpn_p * ficp_Lr) + rpn_Lr * ficp2 + rpn * ficp2_Lr);
    const double rp3_q1 = mu_norm * (rpnp2_q1 * fic + rpnp2 * fic_q1 + 2.0 * (rpn_p_q1 * ficp + rpn_p * ficp_q1) + rpn_q1 * ficp2 + rpn * ficp2_q1);
    const double rp3_q2 = mu_norm * (rpnp2_q2 * fic + rpnp2 * fic_q2 + 2.0 * (rpn_p_q2 * ficp + rpn_p * ficp_q2) + rpn_q2 * ficp2 + rpn * ficp2_q2);
    const double rp3_p1 = mu_norm * (rpnp2_p1 * fic + rpnp2 * fic_p1 + 2.0 * (rpn_p_p1 * ficp + rpn_p * ficp_p1) + rpn_p1 * ficp2 + rpn * ficp2_p1);
    const double rp3_p2 = mu_norm * (rpnp2_p2 * fic + rpnp2 * fic_p2 + 2.0 * (rpn_p_p2 * ficp + rpn_p * ficp_p2) + rpn_p2 * ficp2 + rpn * ficp2_p2);
    const double firp3_nu = derivatives_of_inverse_wrt_param_4(r, rp, rpp, rp3, r_nu, rp_nu, rpp_nu, rp3_nu);
    const double firp3_Lr = derivatives_of_inverse_wrt_param_4(r, rp, rpp, rp3, r_Lr, rp_Lr, rpp_Lr, rp3_Lr);
    const double firp3_q1 = derivatives_of_inverse_wrt_param_4(r, rp, rpp, rp3, r_q1, rp_q1, rpp_q1, rp3_q1);
    const double firp3_q2 = derivatives_of_inverse_wrt_param_4(r, rp, rpp, rp3, r_q2, rp_q2, rpp_q2, rp3_q2);
    const double firp3_p1 = derivatives_of_inverse_wrt_param_4(r, rp, rpp, rp3, r_p1, rp_p1, rpp_p1, rp3_p1);
    const double firp3_p2 = derivatives_of_inverse_wrt_param_4(r, rp, rpp, rp3, r_p2, rp_p2, rpp_p2, rp3_p2);
    const double f2rpp2_nu = derivatives_of_product_wrt_param_4(r, rp, rpp, rp3, r_nu, rp_nu, rpp_nu, rp3_nu);
    const double f2rpp2_Lr = derivatives_of_product_wrt_param_4(r, rp, rpp, rp3, r_Lr, rp_Lr, rpp_Lr, rp3_Lr);
    const double f2rpp2_q1 = derivatives_of_product_wrt_param_4(r, rp, rpp, rp3, r_q1, rp_q1, rpp_q1, rp3_q1);
    const double f2rpp2_q2 = derivatives_of_product_wrt_param_4(r, rp, rpp, rp3, r_q2, rp_q2, rpp_q2, rp3_q2);
    const double f2rpp2_p1 = derivatives_of_product_wrt_param_4(r, rp, rpp, rp3, r_p1, rp_p1, rpp_p1, rp3_p1);
    const double f2rpp2_p2 = derivatives_of_product_wrt_param_4(r, rp, rpp, rp3, r_p2, rp_p2, rpp_p2, rp3_p2);
    const double fir2p3 = derivatives_of_inverse_4(r2, 2.0 * f2rp, 2.0 * f2rpp, 2.0 * f2rpp2);
    const double r3p3 = 3.0 * (2.0 * (rp * rp * rp) + 6.0 * r * rp * rpp + r2 * rp3);
    const double fir3p3 = derivatives_of_inverse_4(r3, r3p, r3p2, r3p3);
    const double r3p3_nu = 3.0 * (6.0 * (rp * rp) * rp_nu + 6.0 * (r_nu * rp * rpp + r * (rp_nu * rpp + rp * rpp_nu)) + r2_nu * rp3 + r2 * rp3_nu);
    const double r3p3_Lr = 3.0 * (6.0 * (rp * rp) * rp_Lr + 6.0 * (r_Lr * rp * rpp + r * (rp_Lr * rpp + rp * rpp_Lr)) + r2_Lr * rp3 + r2 * rp3_Lr);
    const double r3p3_q1 = 3.0 * (6.0 * (rp * rp) * rp_q1 + 6.0 * (r_q1 * rp * rpp + r * (rp_q1 * rpp + rp * rpp_q1)) + r2_q1 * rp3 + r2 * rp3_q1);
    const double r3p3_q2 = 3.0 * (6.0 * (rp * rp) * rp_q2 + 6.0 * (r_q2 * rp * rpp + r * (rp_q2 * rpp + rp * rpp_q2)) + r2_q2 * rp3 + r2 * rp3_q2);
    const double r3p3_p1 = 3.0 * (6.0 * (rp * rp) * rp_p1 + 6.0 * (r_p1 * rp * rpp + r * (rp_p1 * rpp + rp * rpp_p1)) + r2_p1 * rp3 + r2 * rp3_p1);
    const double r3p3_p2 = 3.0 * (6.0 * (rp * rp) * rp_p2 + 6.0 * (r_p2 * rp * rpp + r * (rp_p2 * rpp + rp * rpp_p2)) + r2_p2 * rp3 + r2 * rp3_p2);
    const double fir3_p3_nu = derivatives_of_inverse_wrt_param_4(r3, r3p, r3p2, r3p3, r3_nu, r3p_nu, r3p2_nu, r3p3_nu);
    const double fir3_p3_Lr = derivatives_of_inverse_wrt_param_4(r3, r3p, r3p2, r3p3, r3_Lr, r3p_Lr, r3p2_Lr, r3p3_Lr);
    const double fir3_p3_q1 = derivatives_of_inverse_wrt_param_4(r3, r3p, r3p2, r3p3, r3_q1, r3p_q1, r3p2_q1, r3p3_q1);
    const double fir3_p3_q2 = derivatives_of_inverse_wrt_param_4(r3, r3p, r3p2, r3p3, r3_q2, r3p_q2, r3p2_q2, r3p3_q2);
    const double fir3_p3_p1 = derivatives_of_inverse_wrt_param_4(r3, r3p, r3p2, r3p3, r3_p1, r3p_p1, r3p2_p1, r3p3_p1);
    const double fir3_p3_p2 = derivatives_of_inverse_wrt_param_4(r3, r3p, r3p2, r3p3, r3_p2, r3p_p2, r3p2_p2, r3p3_p2);
    const double fir2p3_nu = derivatives_of_inverse_wrt_param_4(r2, 2.0 * f2rp, 2.0 * f2rpp, 2.0 * f2rpp2, r2_nu, 2.0 * f2rp_nu, 2.0 * f2rpp_nu, 2.0 * f2rpp2_nu);
    const double fir2p3_Lr = derivatives_of_inverse_wrt_param_4(r2, 2.0 * f2rp, 2.0 * f2rpp, 2.0 * f2rpp2, r2_Lr, 2.0 * f2rp_Lr, 2.0 * f2rpp_Lr, 2.0 * f2rpp2_Lr);
    const double fir2p3_q1 = derivatives_of_inverse_wrt_param_4(r2, 2.0 * f2rp, 2.0 * f2rpp, 2.0 * f2rpp2, r2_q1, 2.0 * f2rp_q1, 2.0 * f2rpp_q1, 2.0 * f2rpp2_q1);
    const double fir2p3_q2 = derivatives_of_inverse_wrt_param_4(r2, 2.0 * f2rp, 2.0 * f2rpp, 2.0 * f2rpp2, r2_q2, 2.0 * f2rp_q2, 2.0 * f2rpp_q2, 2.0 * f2rpp2_q2);
    const double fir2p3_p1 = derivatives_of_inverse_wrt_param_4(r2, 2.0 * f2rp, 2.0 * f2rpp, 2.0 * f2rpp2, r2_p1, 2.0 * f2rp_p1, 2.0 * f2rpp_p1, 2.0 * f2rpp2_p1);
    const double fir2p3_p2 = derivatives_of_inverse_wrt_param_4(r2, 2.0 * f2rp, 2.0 * f2rpp, 2.0 * f2rpp2, r2_p2, 2.0 * f2rp_p2, 2.0 * f2rpp_p2, 2.0 * f2rpp2_p2);
    const double hpp = (f2cp_p - 2.0 * U * f2rpp - 4.0 * f2rp * Up - r2 * Upp) * fih + (f2cp - 2.0 * f2rp * U - r2 * Up) * fihp;
    const double fihp2 = derivatives_of_inverse_3(h, hp, hpp);
    const double hpp_nu = (f2cp_p_nu - 2.0 * (U_nu * f2rpp + U * f2rpp_nu) - 4.0 * (f2rp_nu * Up + f2rp * Up_nu) - r2_nu * Upp - r2 * Upp_nu) * fih + (f2cp_p - 2.0 * U * f2rpp - 4.0 * f2rp * Up - r2 * Upp) * fih_nu + (f2cp_nu - 2.0 * (f2rp_nu * U + f2rp * U_nu) - r2_nu * Up - r2 * Up_nu) * fihp + (f2cp - 2.0 * f2rp * U - r2 * Up) * fihp_nu;
    const double hpp_Lr = (f2cp_p_Lr - 2.0 * (U_Lr * f2rpp + U * f2rpp_Lr) - 4.0 * (f2rp_Lr * Up + f2rp * Up_Lr) - r2_Lr * Upp - r2 * Upp_Lr) * fih + (f2cp_p - 2.0 * U * f2rpp - 4.0 * f2rp * Up - r2 * Upp) * fih_Lr + (f2cp_Lr - 2.0 * (f2rp_Lr * U + f2rp * U_Lr) - r2_Lr * Up - r2 * Up_Lr) * fihp + (f2cp - 2.0 * f2rp * U - r2 * Up) * fihp_Lr;
    const double hpp_q1 = (f2cp_p_q1 - 2.0 * (U_q1 * f2rpp + U * f2rpp_q1) - 4.0 * (f2rp_q1 * Up + f2rp * Up_q1) - r2_q1 * Upp - r2 * Upp_q1) * fih + (f2cp_p - 2.0 * U * f2rpp - 4.0 * f2rp * Up - r2 * Upp) * fih_q1 + (f2cp_q1 - 2.0 * (f2rp_q1 * U + f2rp * U_q1) - r2_q1 * Up - r2 * Up_q1) * fihp + (f2cp - 2.0 * f2rp * U - r2 * Up) * fihp_q1;
    const double hpp_q2 = (f2cp_p_q2 - 2.0 * (U_q2 * f2rpp + U * f2rpp_q2) - 4.0 * (f2rp_q2 * Up + f2rp * Up_q2) - r2_q2 * Upp - r2 * Upp_q2) * fih + (f2cp_p - 2.0 * U * f2rpp - 4.0 * f2rp * Up - r2 * Upp) * fih_q2 + (f2cp_q2 - 2.0 * (f2rp_q2 * U + f2rp * U_q2) - r2_q2 * Up - r2 * Up_q2) * fihp + (f2cp - 2.0 * f2rp * U - r2 * Up) * fihp_q2;
    const double hpp_p1 = (f2cp_p_p1 - 2.0 * (U_p1 * f2rpp + U * f2rpp_p1) - 4.0 * (f2rp_p1 * Up + f2rp * Up_p1) - r2_p1 * Upp - r2 * Upp_p1) * fih + (f2cp_p - 2.0 * U * f2rpp - 4.0 * f2rp * Up - r2 * Upp) * fih_p1 + (f2cp_p1 - 2.0 * (f2rp_p1 * U + f2rp * U_p1) - r2_p1 * Up - r2 * Up_p1) * fihp + (f2cp - 2.0 * f2rp * U - r2 * Up) * fihp_p1;
    const double hpp_p2 = (f2cp_p_p2 - 2.0 * (U_p2 * f2rpp + U * f2rpp_p2) - 4.0 * (f2rp_p2 * Up + f2rp * Up_p2) - r2_p2 * Upp - r2 * Upp_p2) * fih + (f2cp_p - 2.0 * U * f2rpp - 4.0 * f2rp * Up - r2 * Upp) * fih_p2 + (f2cp_p2 - 2.0 * (f2rp_p2 * U + f2rp * U_p2) - r2_p2 * Up - r2 * Up_p2) * fihp + (f2cp - 2.0 * f2rp * U - r2 * Up) * fihp_p2;
    const double fihp2_nu = derivatives_of_inverse_wrt_param_3(h, hp, hpp, h_nu, hp_nu, hpp_nu);
    const double fihp2_Lr = derivatives_of_inverse_wrt_param_3(h, hp, hpp, h_Lr, hp_Lr, hpp_Lr);
    const double fihp2_q1 = derivatives_of_inverse_wrt_param_3(h, hp, hpp, h_q1, hp_q1, hpp_q1);
    const double fihp2_q2 = derivatives_of_inverse_wrt_param_3(h, hp, hpp, h_q2, hp_q2, hpp_q2);
    const double fihp2_p1 = derivatives_of_inverse_wrt_param_3(h, hp, hpp, h_p1, hp_p1, hpp_p1);
    const double fihp2_p2 = derivatives_of_inverse_wrt_param_3(h, hp, hpp, h_p2, hp_p2, hpp_p2);
    const double fibm1p2 = derivatives_of_inverse_3(beta + 1.0, bp, bpp);
    const double fibm1_p2_nu = derivatives_of_inverse_wrt_param_3(beta + 1.0, bp, bpp, beta_nu, bp_nu, bpp_nu);
    const double fibm1_p2_Lr = derivatives_of_inverse_wrt_param_3(beta + 1.0, bp, bpp, beta_Lr, bp_Lr, bpp_Lr);
    const double fibm1_p2_q1 = derivatives_of_inverse_wrt_param_3(beta + 1.0, bp, bpp, beta_q1, bp_q1, bpp_q1);
    const double fibm1_p2_q2 = derivatives_of_inverse_wrt_param_3(beta + 1.0, bp, bpp, beta_q2, bp_q2, bpp_q2);
    const double fibm1_p2_p1 = derivatives_of_inverse_wrt_param_3(beta + 1.0, bp, bpp, beta_p1, bp_p1, bpp_p1);
    const double fibm1_p2_p2 = derivatives_of_inverse_wrt_param_3(beta + 1.0, bp, bpp, beta_p2, bp_p2, bpp_p2);
    const double alphap2 = fibm1p2;
    const double fialphap2 = derivatives_of_inverse_3(fibm1, alphap, alphap2);
    const double alphap2_nu = fibm1_p2_nu;
    const double alphap2_Lr = fibm1_p2_Lr;
    const double alphap2_q1 = fibm1_p2_q1;
    const double alphap2_q2 = fibm1_p2_q2;
    const double alphap2_p1 = fibm1_p2_p1;
    const double alphap2_p2 = fibm1_p2_p2;
    const double fialphap2_nu = derivatives_of_inverse_wrt_param_3(fibm1, alphap, alphap2, alpha_nu, alphap_nu, alphap2_nu);
    const double fialphap2_Lr = derivatives_of_inverse_wrt_param_3(fibm1, alphap, alphap2, alpha_Lr, alphap_Lr, alphap2_Lr);
    const double fialphap2_q1 = derivatives_of_inverse_wrt_param_3(fibm1, alphap, alphap2, alpha_q1, alphap_q1, alphap2_q1);
    const double fialphap2_q2 = derivatives_of_inverse_wrt_param_3(fibm1, alphap, alphap2, alpha_q2, alphap_q2, alphap2_q2);
    const double fialphap2_p1 = derivatives_of_inverse_wrt_param_3(fibm1, alphap, alphap2, alpha_p1, alphap_p1, alphap2_p1);
    const double fialphap2_p2 = derivatives_of_inverse_wrt_param_3(fibm1, alphap, alphap2, alpha_p2, alphap_p2, alphap2_p2);
    const double deltap2 = -qsp2;
    const double deltap2_nu = -qsp2_nu;
    const double deltap2_Lr = -qsp2_Lr;
    const double deltap2_q1 = -qsp2_q1;
    const double deltap2_q2 = -qsp2_q2;
    const double deltap2_p1 = -qsp2_p1;
    const double deltap2_p2 = -qsp2_p2;
    const double hr3p2 = hpp * r3 + 6.0 * hp * r2 * rp + 3.0 * h * (2.0 * r * (rp * rp) + r2 * rpp);
    const double fihr3p2 = derivatives_of_inverse_3(hr3, hr3p, hr3p2);
    const double hr3p2_nu = hpp_nu * r3 + hpp * r3_nu + 6.0 * (hp_nu * r2 * rp + hp * (r2_nu * rp + r2 * rp_nu)) + 3.0 * h_nu * (2.0 * r * (rp * rp) + r2 * rpp) + 3.0 * h * (2.0 * (r_nu * (rp * rp) + r * 2.0 * rp * rp_nu) + r2_nu * rpp + r2 * rpp_nu);
    const double hr3p2_Lr = hpp_Lr * r3 + hpp * r3_Lr + 6.0 * (hp_Lr * r2 * rp + hp * (r2_Lr * rp + r2 * rp_Lr)) + 3.0 * h_Lr * (2.0 * r * (rp * rp) + r2 * rpp) + 3.0 * h * (2.0 * (r_Lr * (rp * rp) + r * 2.0 * rp * rp_Lr) + r2_Lr * rpp + r2 * rpp_Lr);
    const double hr3p2_q1 = hpp_q1 * r3 + hpp * r3_q1 + 6.0 * (hp_q1 * r2 * rp + hp * (r2_q1 * rp + r2 * rp_q1)) + 3.0 * h_q1 * (2.0 * r * (rp * rp) + r2 * rpp) + 3.0 * h * (2.0 * (r_q1 * (rp * rp) + r * 2.0 * rp * rp_q1) + r2_q1 * rpp + r2 * rpp_q1);
    const double hr3p2_q2 = hpp_q2 * r3 + hpp * r3_q2 + 6.0 * (hp_q2 * r2 * rp + hp * (r2_q2 * rp + r2 * rp_q2)) + 3.0 * h_q2 * (2.0 * r * (rp * rp) + r2 * rpp) + 3.0 * h * (2.0 * (r_q2 * (rp * rp) + r * 2.0 * rp * rp_q2) + r2_q2 * rpp + r2 * rpp_q2);
    const double hr3p2_p1 = hpp_p1 * r3 + hpp * r3_p1 + 6.0 * (hp_p1 * r2 * rp + hp * (r2_p1 * rp + r2 * rp_p1)) + 3.0 * h_p1 * (2.0 * r * (rp * rp) + r2 * rpp) + 3.0 * h * (2.0 * (r_p1 * (rp * rp) + r * 2.0 * rp * rp_p1) + r2_p1 * rpp + r2 * rpp_p1);
    const double hr3p2_p2 = hpp_p2 * r3 + hpp * r3_p2 + 6.0 * (hp_p2 * r2 * rp + hp * (r2_p2 * rp + r2 * rp_p2)) + 3.0 * h_p2 * (2.0 * r * (rp * rp) + r2 * rpp) + 3.0 * h * (2.0 * (r_p2 * (rp * rp) + r * 2.0 * rp * rp_p2) + r2_p2 * rpp + r2 * rpp_p2);
    const double fihr3p2_nu = derivatives_of_inverse_wrt_param_3(hr3, hr3p, hr3p2, hr3_nu, hr3p_nu, hr3p2_nu);
    const double fihr3p2_Lr = derivatives_of_inverse_wrt_param_3(hr3, hr3p, hr3p2, hr3_Lr, hr3p_Lr, hr3p2_Lr);
    const double fihr3p2_q1 = derivatives_of_inverse_wrt_param_3(hr3, hr3p, hr3p2, hr3_q1, hr3p_q1, hr3p2_q1);
    const double fihr3p2_q2 = derivatives_of_inverse_wrt_param_3(hr3, hr3p, hr3p2, hr3_q2, hr3p_q2, hr3p2_q2);
    const double fihr3p2_p1 = derivatives_of_inverse_wrt_param_3(hr3, hr3p, hr3p2, hr3_p1, hr3p_p1, hr3p2_p1);
    const double fihr3p2_p2 = derivatives_of_inverse_wrt_param_3(hr3, hr3p, hr3p2, hr3_p2, hr3p_p2, hr3p2_p2);
    const double GAMMApp_ = fialphap2 + alphap2 * (1.0 - r / a) - 2.0 * alphap * rp / a - alpha * rpp / a;
    const double Ipp = 3.0 * A * ((zgpp * delta + 2.0 * zgp * deltap + zg * deltap2) * fihr3 + 2.0 * (zgp * delta + zg * deltap) * fihr3p + zg * delta * fihr3p2);
    const double dp2 = (hpp - cpp) * fir2 + 2.0 * (hp - cp) * fir2p + (h - c) * fir2p2;
    const double whp2 = Ipp * zg + 2.0 * Ip * zgp + I * zgpp;
    const double xi1p2 = Xpp / a + 2.0 * p2pp_0;
    const double xi2p2 = Ypp / a + 2.0 * p1pp_0;
    const double GAMMApp_nu = fialphap2_nu + alphap2_nu * (1.0 - r / a) + alphap2 * (-r_nu / a + r / (a * a) * a_nu) - 2.0 * alphap_nu * rp / a - 2.0 * alphap * (rp_nu / a - rp / (a * a) * a_nu) - alpha_nu * rpp / a - alpha * (rpp_nu / a - rpp / (a * a) * a_nu);
    const double GAMMApp_Lr = fialphap2_Lr + alphap2_Lr * (1.0 - r / a) - alphap2 * r_Lr / a - 2.0 * alphap_Lr * rp / a - 2.0 * alphap * rp_Lr / a - alpha_Lr * rpp / a - alpha * rpp_Lr / a;
    const double GAMMApp_q1 = fialphap2_q1 + alphap2_q1 * (1.0 - r / a) - alphap2 * r_q1 / a - 2.0 * alphap_q1 * rp / a - 2.0 * alphap * rp_q1 / a - alpha_q1 * rpp / a - alpha * rpp_q1 / a;
    const double GAMMApp_q2 = fialphap2_q2 + alphap2_q2 * (1.0 - r / a) - alphap2 * r_q2 / a - 2.0 * alphap_q2 * rp / a - 2.0 * alphap * rp_q2 / a - alpha_q2 * rpp / a - alpha * rpp_q2 / a;
    const double GAMMApp_p1 = fialphap2_p1 + alphap2_p1 * (1.0 - r / a) - alphap2 * r_p1 / a - 2.0 * alphap_p1 * rp / a - 2.0 * alphap * rp_p1 / a - alpha_p1 * rpp / a - alpha * rpp_p1 / a;
    const double GAMMApp_p2 = fialphap2_p2 + alphap2_p2 * (1.0 - r / a) - alphap2 * r_p2 / a - 2.0 * alphap_p2 * rp / a - 2.0 * alphap * rp_p2 / a - alpha_p2 * rpp / a - alpha * rpp_p2 / a;
    const double Ipp_nu = 3.0 * A * ((zgpp_nu * delta + zgpp * delta_nu + 2.0 * (zgp_nu * deltap + zgp * deltap_nu) + zg_nu * deltap2 + zg * deltap2_nu) * fihr3 + (zgpp * delta + 2.0 * zgp * deltap + zg * deltap2) * fihr3_nu + 2.0 * (zgp_nu * delta + zgp * delta_nu + zg_nu * deltap + zg * deltap_nu) * fihr3p + 2.0 * (zgp * delta + zg * deltap) * fihr3p_nu + (zg_nu * delta + zg * delta_nu) * fihr3p2 + zg * delta * fihr3p2_nu);
    const double Ipp_Lr = 3.0 * A * ((zgpp_Lr * delta + zgpp * delta_Lr + 2.0 * (zgp_Lr * deltap + zgp * deltap_Lr) + zg_Lr * deltap2 + zg * deltap2_Lr) * fihr3 + (zgpp * delta + 2.0 * zgp * deltap + zg * deltap2) * fihr3_Lr + 2.0 * (zgp_Lr * delta + zgp * delta_Lr + zg_Lr * deltap + zg * deltap_Lr) * fihr3p + 2.0 * (zgp * delta + zg * deltap) * fihr3p_Lr + (zg_Lr * delta + zg * delta_Lr) * fihr3p2 + zg * delta * fihr3p2_Lr);
    const double Ipp_q1 = 3.0 * A * ((zgpp_q1 * delta + zgpp * delta_q1 + 2.0 * (zgp_q1 * deltap + zgp * deltap_q1) + zg_q1 * deltap2 + zg * deltap2_q1) * fihr3 + (zgpp * delta + 2.0 * zgp * deltap + zg * deltap2) * fihr3_q1 + 2.0 * (zgp_q1 * delta + zgp * delta_q1 + zg_q1 * deltap + zg * deltap_q1) * fihr3p + 2.0 * (zgp * delta + zg * deltap) * fihr3p_q1 + (zg_q1 * delta + zg * delta_q1) * fihr3p2 + zg * delta * fihr3p2_q1);
    const double Ipp_q2 = 3.0 * A * ((zgpp_q2 * delta + zgpp * delta_q2 + 2.0 * (zgp_q2 * deltap + zgp * deltap_q2) + zg_q2 * deltap2 + zg * deltap2_q2) * fihr3 + (zgpp * delta + 2.0 * zgp * deltap + zg * deltap2) * fihr3_q2 + 2.0 * (zgp_q2 * delta + zgp * delta_q2 + zg_q2 * deltap + zg * deltap_q2) * fihr3p + 2.0 * (zgp * delta + zg * deltap) * fihr3p_q2 + (zg_q2 * delta + zg * delta_q2) * fihr3p2 + zg * delta * fihr3p2_q2);
    const double Ipp_p1 = 3.0 * A * ((zgpp_p1 * delta + zgpp * delta_p1 + 2.0 * (zgp_p1 * deltap + zgp * deltap_p1) + zg_p1 * deltap2 + zg * deltap2_p1) * fihr3 + (zgpp * delta + 2.0 * zgp * deltap + zg * deltap2) * fihr3_p1 + 2.0 * (zgp_p1 * delta + zgp * delta_p1 + zg_p1 * deltap + zg * deltap_p1) * fihr3p + 2.0 * (zgp * delta + zg * deltap) * fihr3p_p1 + (zg_p1 * delta + zg * delta_p1) * fihr3p2 + zg * delta * fihr3p2_p1);
    const double Ipp_p2 = 3.0 * A * ((zgpp_p2 * delta + zgpp * delta_p2 + 2.0 * (zgp_p2 * deltap + zgp * deltap_p2) + zg_p2 * deltap2 + zg * deltap2_p2) * fihr3 + (zgpp * delta + 2.0 * zgp * deltap + zg * deltap2) * fihr3_p2 + 2.0 * (zgp_p2 * delta + zgp * delta_p2 + zg_p2 * deltap + zg * deltap_p2) * fihr3p + 2.0 * (zgp * delta + zg * deltap) * fihr3p_p2 + (zg_p2 * delta + zg * delta_p2) * fihr3p2 + zg * delta * fihr3p2_p2);
    const double dp2_nu = (hpp_nu - cpp_nu) * fir2 + (hpp - cpp) * fir2_nu + 2.0 * (hp_nu - cp_nu) * fir2p + 2.0 * (hp - cp) * fir2p_nu + (h_nu - c_nu) * fir2p2 + (h - c) * fir2p2_nu;
    const double dp2_Lr = (hpp_Lr - cpp_Lr) * fir2 + (hpp - cpp) * fir2_Lr + 2.0 * (hp_Lr - cp_Lr) * fir2p + 2.0 * (hp - cp) * fir2p_Lr + (h_Lr - c_Lr) * fir2p2 + (h - c) * fir2p2_Lr;
    const double dp2_q1 = (hpp_q1 - cpp_q1) * fir2 + (hpp - cpp) * fir2_q1 + 2.0 * (hp_q1 - cp_q1) * fir2p + 2.0 * (hp - cp) * fir2p_q1 + (h_q1 - c_q1) * fir2p2 + (h - c) * fir2p2_q1;
    const double dp2_q2 = (hpp_q2 - cpp_q2) * fir2 + (hpp - cpp) * fir2_q2 + 2.0 * (hp_q2 - cp_q2) * fir2p + 2.0 * (hp - cp) * fir2p_q2 + (h_q2 - c_q2) * fir2p2 + (h - c) * fir2p2_q2;
    const double dp2_p1 = (hpp_p1 - cpp_p1) * fir2 + (hpp - cpp) * fir2_p1 + 2.0 * (hp_p1 - cp_p1) * fir2p + 2.0 * (hp - cp) * fir2p_p1 + (h_p1 - c_p1) * fir2p2 + (h - c) * fir2p2_p1;
    const double dp2_p2 = (hpp_p2 - cpp_p2) * fir2 + (hpp - cpp) * fir2_p2 + 2.0 * (hp_p2 - cp_p2) * fir2p + 2.0 * (hp - cp) * fir2p_p2 + (h_p2 - c_p2) * fir2p2 + (h - c) * fir2p2_p2;
    const double whp2_nu = Ipp_nu * zg + Ipp * zg_nu + 2.0 * Ip_nu * zgp + 2.0 * Ip * zgp_nu + I_nu * zgpp + I * zgpp_nu;
    const double whp2_Lr = Ipp_Lr * zg + Ipp * zg_Lr + 2.0 * Ip_Lr * zgp + 2.0 * Ip * zgp_Lr + I_Lr * zgpp + I * zgpp_Lr;
    const double whp2_q1 = Ipp_q1 * zg + Ipp * zg_q1 + 2.0 * Ip_q1 * zgp + 2.0 * Ip * zgp_q1 + I_q1 * zgpp + I * zgpp_q1;
    const double whp2_q2 = Ipp_q2 * zg + Ipp * zg_q2 + 2.0 * Ip_q2 * zgp + 2.0 * Ip * zgp_q2 + I_q2 * zgpp + I * zgpp_q2;
    const double whp2_p1 = Ipp_p1 * zg + Ipp * zg_p1 + 2.0 * Ip_p1 * zgp + 2.0 * Ip * zgp_p1 + I_p1 * zgpp + I * zgpp_p1;
    const double whp2_p2 = Ipp_p2 * zg + Ipp * zg_p2 + 2.0 * Ip_p2 * zgp + 2.0 * Ip * zgp_p2 + I_p2 * zgpp + I * zgpp_p2;
    const double xi1p2_nu = Xpp_nu / a - Xpp / (a * a) * a_nu + 2.0 * p2p2_nu;
    const double xi2p2_nu = Ypp_nu / a - Ypp / (a * a) * a_nu + 2.0 * p1p2_nu;
    const double xi1p2_Lr = Xpp_Lr / a + 2.0 * p2p2_Lr;
    const double xi2p2_Lr = Ypp_Lr / a + 2.0 * p1p2_Lr;
    const double xi1p2_q1 = Xpp_q1 / a + 2.0 * p2p2_q1;
    const double xi2p2_q1 = Ypp_q1 / a + 2.0 * p1p2_q1;
    const double xi1p2_q2 = Xpp_q2 / a + 2.0 * p2p2_q2;
    const double xi2p2_q2 = Ypp_q2 / a + 2.0 * p1p2_q2;
    const double xi1p2_p1 = Xpp_p1 / a + 2.0 * p2p2_p1;
    const double xi2p2_p1 = Ypp_p1 / a + 2.0 * p1p2_p1;
    const double xi1p2_p2 = Xpp_p2 / a + 2.0 * p2p2_p2;
    const double xi2p2_p2 = Ypp_p2 / a + 2.0 * p1p2_p2;
    const double p1ppp_0 = p2pp_0 * (d - wh) + 2.0 * p2p_0 * (dp - whp) + p2_0 * (dp2 - whp2) - U * (ficp2 * xi1 + 2.0 * ficp * xi1p + fic * xi1p2) - 2.0 * Up * (ficp * xi1 + fic * xi1p) - fic * xi1 * Upp;
    const double p2ppp_0 = p1pp_0 * (-d + wh) + 2.0 * p1p_0 * (-dp + whp) + p1_0 * (-dp2 + whp2) + U * (ficp2 * xi2 + 2.0 * ficp * xi2p + fic * xi2p2) + 2.0 * Up * (ficp * xi2 + fic * xi2p) + fic * xi2 * Upp;
    const double Lrppp_0 = dp2 - whp2 - U * (ficp2 * GAMMA_ + 2.0 * ficp * GAMMAp_ + fic * GAMMApp_) - 2.0 * Up * (ficp * GAMMA_ + fic * GAMMAp_) - Upp * fic * GAMMA_;
    const double q1ppp_0 = -Ipp * sinL - 2.0 * Ip * sinLp - I * sinLp2;
    const double q2ppp_0 = -Ipp * cosL - 2.0 * Ip * cosLp - I * cosLp2;
    out.map_components_col2[0] = 0.0;
    out.map_components_col2[1] = q1ppp_0;
    out.map_components_col2[2] = q2ppp_0;
    out.map_components_col2[3] = p1ppp_0;
    out.map_components_col2[4] = p2ppp_0;
    out.map_components_col2[5] = Lrppp_0;
    const double q1p3_nu = -Ipp_nu * sinL - Ipp * sinL_nu - 2.0 * Ip_nu * sinLp - 2.0 * Ip * sinLp_nu - I_nu * sinLp2 - I * sinLp2_nu;
    const double q1p3_Lr = -Ipp_Lr * sinL - Ipp * sinL_Lr - 2.0 * Ip_Lr * sinLp - 2.0 * Ip * sinLp_Lr - I_Lr * sinLp2 - I * sinLp2_Lr;
    const double q1p3_q1 = -Ipp_q1 * sinL - Ipp * sinL_q1 - 2.0 * Ip_q1 * sinLp - 2.0 * Ip * sinLp_q1 - I_q1 * sinLp2 - I * sinLp2_q1;
    const double q1p3_q2 = -Ipp_q2 * sinL - Ipp * sinL_q2 - 2.0 * Ip_q2 * sinLp - 2.0 * Ip * sinLp_q2 - I_q2 * sinLp2 - I * sinLp2_q2;
    const double q1p3_p1 = -Ipp_p1 * sinL - Ipp * sinL_p1 - 2.0 * Ip_p1 * sinLp - 2.0 * Ip * sinLp_p1 - I_p1 * sinLp2 - I * sinLp2_p1;
    const double q1p3_p2 = -Ipp_p2 * sinL - Ipp * sinL_p2 - 2.0 * Ip_p2 * sinLp - 2.0 * Ip * sinLp_p2 - I_p2 * sinLp2 - I * sinLp2_p2;
    const double q2p3_nu = -Ipp_nu * cosL - Ipp * cosL_nu - 2.0 * Ip_nu * cosLp - 2.0 * Ip * cosLp_nu - I_nu * cosLp2 - I * cosLp2_nu;
    const double q2p3_Lr = -Ipp_Lr * cosL - Ipp * cosL_Lr - 2.0 * Ip_Lr * cosLp - 2.0 * Ip * cosLp_Lr - I_Lr * cosLp2 - I * cosLp2_Lr;
    const double q2p3_q1 = -Ipp_q1 * cosL - Ipp * cosL_q1 - 2.0 * Ip_q1 * cosLp - 2.0 * Ip * cosLp_q1 - I_q1 * cosLp2 - I * cosLp2_q1;
    const double q2p3_q2 = -Ipp_q2 * cosL - Ipp * cosL_q2 - 2.0 * Ip_q2 * cosLp - 2.0 * Ip * cosLp_q2 - I_q2 * cosLp2 - I * cosLp2_q2;
    const double q2p3_p1 = -Ipp_p1 * cosL - Ipp * cosL_p1 - 2.0 * Ip_p1 * cosLp - 2.0 * Ip * cosLp_p1 - I_p1 * cosLp2 - I * cosLp2_p1;
    const double q2p3_p2 = -Ipp_p2 * cosL - Ipp * cosL_p2 - 2.0 * Ip_p2 * cosLp - 2.0 * Ip * cosLp_p2 - I_p2 * cosLp2 - I * cosLp2_p2;
    const double p1p3_nu = p2p2_nu * (d - wh) + p2pp_0 * (d_nu - wh_nu) + 2.0 * p2p_nu * (dp - whp) + 2.0 * p2p_0 * (dp_nu - whp_nu) + p2_0 * (dp2_nu - whp2_nu) - U * (ficp2_nu * xi1 + ficp2 * xi1_nu + 2.0 * ficp_nu * xi1p + 2.0 * ficp * xi1p_nu + fic_nu * xi1p2 + fic * xi1p2_nu) - U_nu * (ficp2 * xi1 + 2.0 * ficp * xi1p + fic * xi1p2) - 2.0 * Up * (ficp_nu * xi1 + ficp * xi1_nu + fic_nu * xi1p + fic * xi1p_nu) - 2.0 * Up_nu * (ficp * xi1 + fic * xi1p) - (fic_nu * xi1 + fic * xi1_nu) * Upp - fic * xi1 * Upp_nu;
    const double p1p3_Lr = p2p2_Lr * (d - wh) + p2pp_0 * (d_Lr - wh_Lr) + 2.0 * p2p_Lr * (dp - whp) + 2.0 * p2p_0 * (dp_Lr - whp_Lr) + p2_0 * (dp2_Lr - whp2_Lr) - U * (ficp2_Lr * xi1 + ficp2 * xi1_Lr + 2.0 * ficp_Lr * xi1p + 2.0 * ficp * xi1p_Lr + fic_Lr * xi1p2 + fic * xi1p2_Lr) - U_Lr * (ficp2 * xi1 + 2.0 * ficp * xi1p + fic * xi1p2) - 2.0 * Up * (ficp_Lr * xi1 + ficp * xi1_Lr + fic_Lr * xi1p + fic * xi1p_Lr) - 2.0 * Up_Lr * (ficp * xi1 + fic * xi1p) - (fic_Lr * xi1 + fic * xi1_Lr) * Upp - fic * xi1 * Upp_Lr;
    const double p1p3_q1 = p2p2_q1 * (d - wh) + p2pp_0 * (d_q1 - wh_q1) + 2.0 * p2p_q1 * (dp - whp) + 2.0 * p2p_0 * (dp_q1 - whp_q1) + p2_0 * (dp2_q1 - whp2_q1) - U * (ficp2_q1 * xi1 + ficp2 * xi1_q1 + 2.0 * ficp_q1 * xi1p + 2.0 * ficp * xi1p_q1 + fic_q1 * xi1p2 + fic * xi1p2_q1) - U_q1 * (ficp2 * xi1 + 2.0 * ficp * xi1p + fic * xi1p2) - 2.0 * Up * (ficp_q1 * xi1 + ficp * xi1_q1 + fic_q1 * xi1p + fic * xi1p_q1) - 2.0 * Up_q1 * (ficp * xi1 + fic * xi1p) - (fic_q1 * xi1 + fic * xi1_q1) * Upp - fic * xi1 * Upp_q1;
    const double p1p3_q2 = p2p2_q2 * (d - wh) + p2pp_0 * (d_q2 - wh_q2) + 2.0 * p2p_q2 * (dp - whp) + 2.0 * p2p_0 * (dp_q2 - whp_q2) + p2_0 * (dp2_q2 - whp2_q2) - U * (ficp2_q2 * xi1 + ficp2 * xi1_q2 + 2.0 * ficp_q2 * xi1p + 2.0 * ficp * xi1p_q2 + fic_q2 * xi1p2 + fic * xi1p2_q2) - U_q2 * (ficp2 * xi1 + 2.0 * ficp * xi1p + fic * xi1p2) - 2.0 * Up * (ficp_q2 * xi1 + ficp * xi1_q2 + fic_q2 * xi1p + fic * xi1p_q2) - 2.0 * Up_q2 * (ficp * xi1 + fic * xi1p) - (fic_q2 * xi1 + fic * xi1_q2) * Upp - fic * xi1 * Upp_q2;
    const double p1p3_p1 = p2p2_p1 * (d - wh) + p2pp_0 * (d_p1 - wh_p1) + 2.0 * p2p_p1 * (dp - whp) + 2.0 * p2p_0 * (dp_p1 - whp_p1) + p2_0 * (dp2_p1 - whp2_p1) - U * (ficp2_p1 * xi1 + ficp2 * xi1_p1 + 2.0 * ficp_p1 * xi1p + 2.0 * ficp * xi1p_p1 + fic_p1 * xi1p2 + fic * xi1p2_p1) - U_p1 * (ficp2 * xi1 + 2.0 * ficp * xi1p + fic * xi1p2) - 2.0 * Up * (ficp_p1 * xi1 + ficp * xi1_p1 + fic_p1 * xi1p + fic * xi1p_p1) - 2.0 * Up_p1 * (ficp * xi1 + fic * xi1p) - (fic_p1 * xi1 + fic * xi1_p1) * Upp - fic * xi1 * Upp_p1;
    const double p1p3_p2 = p2p2_p2 * (d - wh) + p2pp_0 * (d_p2 - wh_p2) + 2.0 * p2p_p2 * (dp - whp) + 2.0 * p2p_0 * (dp_p2 - whp_p2) + dp2 - whp2 + p2_0 * (dp2_p2 - whp2_p2) - U * (ficp2_p2 * xi1 + ficp2 * xi1_p2 + 2.0 * ficp_p2 * xi1p + 2.0 * ficp * xi1p_p2 + fic_p2 * xi1p2 + fic * xi1p2_p2) - U_p2 * (ficp2 * xi1 + 2.0 * ficp * xi1p + fic * xi1p2) - 2.0 * Up * (ficp_p2 * xi1 + ficp * xi1_p2 + fic_p2 * xi1p + fic * xi1p_p2) - 2.0 * Up_p2 * (ficp * xi1 + fic * xi1p) - (fic_p2 * xi1 + fic * xi1_p2) * Upp - fic * xi1 * Upp_p2;
    const double p2p3_nu = p1p2_nu * (-d + wh) + p1pp_0 * (-d_nu + wh_nu) + 2.0 * p1p_nu * (-dp + whp) + 2.0 * p1p_0 * (-dp_nu + whp_nu) + p1_0 * (-dp2_nu + whp2_nu) + U * (ficp2_nu * xi2 + ficp2 * xi2_nu + 2.0 * ficp_nu * xi2p + 2.0 * ficp * xi2p_nu + fic_nu * xi2p2 + fic * xi2p2_nu) + U_nu * (ficp2 * xi2 + 2.0 * ficp * xi2p + fic * xi2p2) + 2.0 * Up * (ficp_nu * xi2 + ficp * xi2_nu + fic_nu * xi2p + fic * xi2p_nu) + 2.0 * Up_nu * (ficp * xi2 + fic * xi2p) + (fic_nu * xi2 + fic * xi2_nu) * Upp + fic * xi2 * Upp_nu;
    const double p2p3_Lr = p1p2_Lr * (-d + wh) + p1pp_0 * (-d_Lr + wh_Lr) + 2.0 * p1p_Lr * (-dp + whp) + 2.0 * p1p_0 * (-dp_Lr + whp_Lr) + p1_0 * (-dp2_Lr + whp2_Lr) + U * (ficp2_Lr * xi2 + ficp2 * xi2_Lr + 2.0 * ficp_Lr * xi2p + 2.0 * ficp * xi2p_Lr + fic_Lr * xi2p2 + fic * xi2p2_Lr) + U_Lr * (ficp2 * xi2 + 2.0 * ficp * xi2p + fic * xi2p2) + 2.0 * Up * (ficp_Lr * xi2 + ficp * xi2_Lr + fic_Lr * xi2p + fic * xi2p_Lr) + 2.0 * Up_Lr * (ficp * xi2 + fic * xi2p) + (fic_Lr * xi2 + fic * xi2_Lr) * Upp + fic * xi2 * Upp_Lr;
    const double p2p3_q1 = p1p2_q1 * (-d + wh) + p1pp_0 * (-d_q1 + wh_q1) + 2.0 * p1p_q1 * (-dp + whp) + 2.0 * p1p_0 * (-dp_q1 + whp_q1) + p1_0 * (-dp2_q1 + whp2_q1) + U * (ficp2_q1 * xi2 + ficp2 * xi2_q1 + 2.0 * ficp_q1 * xi2p + 2.0 * ficp * xi2p_q1 + fic_q1 * xi2p2 + fic * xi2p2_q1) + U_q1 * (ficp2 * xi2 + 2.0 * ficp * xi2p + fic * xi2p2) + 2.0 * Up * (ficp_q1 * xi2 + ficp * xi2_q1 + fic_q1 * xi2p + fic * xi2p_q1) + 2.0 * Up_q1 * (ficp * xi2 + fic * xi2p) + (fic_q1 * xi2 + fic * xi2_q1) * Upp + fic * xi2 * Upp_q1;
    const double p2p3_q2 = p1p2_q2 * (-d + wh) + p1pp_0 * (-d_q2 + wh_q2) + 2.0 * p1p_q2 * (-dp + whp) + 2.0 * p1p_0 * (-dp_q2 + whp_q2) + p1_0 * (-dp2_q2 + whp2_q2) + U * (ficp2_q2 * xi2 + ficp2 * xi2_q2 + 2.0 * ficp_q2 * xi2p + 2.0 * ficp * xi2p_q2 + fic_q2 * xi2p2 + fic * xi2p2_q2) + U_q2 * (ficp2 * xi2 + 2.0 * ficp * xi2p + fic * xi2p2) + 2.0 * Up * (ficp_q2 * xi2 + ficp * xi2_q2 + fic_q2 * xi2p + fic * xi2p_q2) + 2.0 * Up_q2 * (ficp * xi2 + fic * xi2p) + (fic_q2 * xi2 + fic * xi2_q2) * Upp + fic * xi2 * Upp_q2;
    const double p2p3_p1 = p1p2_p1 * (-d + wh) + p1pp_0 * (-d_p1 + wh_p1) + 2.0 * p1p_p1 * (-dp + whp) + 2.0 * p1p_0 * (-dp_p1 + whp_p1) + -dp2 + whp2 + p1_0 * (-dp2_p1 + whp2_p1) + U * (ficp2_p1 * xi2 + ficp2 * xi2_p1 + 2.0 * ficp_p1 * xi2p + 2.0 * ficp * xi2p_p1 + fic_p1 * xi2p2 + fic * xi2p2_p1) + U_p1 * (ficp2 * xi2 + 2.0 * ficp * xi2p + fic * xi2p2) + 2.0 * Up * (ficp_p1 * xi2 + ficp * xi2_p1 + fic_p1 * xi2p + fic * xi2p_p1) + 2.0 * Up_p1 * (ficp * xi2 + fic * xi2p) + (fic_p1 * xi2 + fic * xi2_p1) * Upp + fic * xi2 * Upp_p1;
    const double p2p3_p2 = p1p2_p2 * (-d + wh) + p1pp_0 * (-d_p2 + wh_p2) + 2.0 * p1p_p2 * (-dp + whp) + 2.0 * p1p_0 * (-dp_p2 + whp_p2) + p1_0 * (-dp2_p2 + whp2_p2) + U * (ficp2_p2 * xi2 + ficp2 * xi2_p2 + 2.0 * ficp_p2 * xi2p + 2.0 * ficp * xi2p_p2 + fic_p2 * xi2p2 + fic * xi2p2_p2) + U_p2 * (ficp2 * xi2 + 2.0 * ficp * xi2p + fic * xi2p2) + 2.0 * Up * (ficp_p2 * xi2 + ficp * xi2_p2 + fic_p2 * xi2p + fic * xi2p_p2) + 2.0 * Up_p2 * (ficp * xi2 + fic * xi2p) + (fic_p2 * xi2 + fic * xi2_p2) * Upp + fic * xi2 * Upp_p2;
    const double Lrp3_nu = dp2_nu - whp2_nu - U * (ficp2_nu * GAMMA_ + ficp2 * GAMMA_nu + 2.0 * ficp_nu * GAMMAp_ + 2.0 * ficp * GAMMAp_nu + fic_nu * GAMMApp_ + fic * GAMMApp_nu) - U_nu * (ficp2 * GAMMA_ + 2.0 * ficp * GAMMAp_ + fic * GAMMApp_) - 2.0 * Up * (ficp_nu * GAMMA_ + ficp * GAMMA_nu + fic_nu * GAMMAp_ + fic * GAMMAp_nu) - 2.0 * Up_nu * (ficp * GAMMA_ + fic * GAMMAp_) - Upp_nu * fic * GAMMA_ - Upp * (fic * GAMMA_nu + fic_nu * GAMMA_);
    const double Lrp3_Lr = dp2_Lr - whp2_Lr - U * (ficp2_Lr * GAMMA_ + ficp2 * GAMMA_Lr + 2.0 * ficp_Lr * GAMMAp_ + 2.0 * ficp * GAMMAp_Lr + fic_Lr * GAMMApp_ + fic * GAMMApp_Lr) - U_Lr * (ficp2 * GAMMA_ + 2.0 * ficp * GAMMAp_ + fic * GAMMApp_) - 2.0 * Up * (ficp_Lr * GAMMA_ + ficp * GAMMA_Lr + fic_Lr * GAMMAp_ + fic * GAMMAp_Lr) - 2.0 * Up_Lr * (ficp * GAMMA_ + fic * GAMMAp_) - Upp_Lr * fic * GAMMA_ - Upp * (fic * GAMMA_Lr + fic_Lr * GAMMA_);
    const double Lrp3_q1 = dp2_q1 - whp2_q1 - U * (ficp2_q1 * GAMMA_ + ficp2 * GAMMA_q1 + 2.0 * ficp_q1 * GAMMAp_ + 2.0 * ficp * GAMMAp_q1 + fic_q1 * GAMMApp_ + fic * GAMMApp_q1) - U_q1 * (ficp2 * GAMMA_ + 2.0 * ficp * GAMMAp_ + fic * GAMMApp_) - 2.0 * Up * (ficp_q1 * GAMMA_ + ficp * GAMMA_q1 + fic_q1 * GAMMAp_ + fic * GAMMAp_q1) - 2.0 * Up_q1 * (ficp * GAMMA_ + fic * GAMMAp_) - Upp_q1 * fic * GAMMA_ - Upp * (fic * GAMMA_q1 + fic_q1 * GAMMA_);
    const double Lrp3_q2 = dp2_q2 - whp2_q2 - U * (ficp2_q2 * GAMMA_ + ficp2 * GAMMA_q2 + 2.0 * ficp_q2 * GAMMAp_ + 2.0 * ficp * GAMMAp_q2 + fic_q2 * GAMMApp_ + fic * GAMMApp_q2) - U_q2 * (ficp2 * GAMMA_ + 2.0 * ficp * GAMMAp_ + fic * GAMMApp_) - 2.0 * Up * (ficp_q2 * GAMMA_ + ficp * GAMMA_q2 + fic_q2 * GAMMAp_ + fic * GAMMAp_q2) - 2.0 * Up_q2 * (ficp * GAMMA_ + fic * GAMMAp_) - Upp_q2 * fic * GAMMA_ - Upp * (fic * GAMMA_q2 + fic_q2 * GAMMA_);
    const double Lrp3_p1 = dp2_p1 - whp2_p1 - U * (ficp2_p1 * GAMMA_ + ficp2 * GAMMA_p1 + 2.0 * ficp_p1 * GAMMAp_ + 2.0 * ficp * GAMMAp_p1 + fic_p1 * GAMMApp_ + fic * GAMMApp_p1) - U_p1 * (ficp2 * GAMMA_ + 2.0 * ficp * GAMMAp_ + fic * GAMMApp_) - 2.0 * Up * (ficp_p1 * GAMMA_ + ficp * GAMMA_p1 + fic_p1 * GAMMAp_ + fic * GAMMAp_p1) - 2.0 * Up_p1 * (ficp * GAMMA_ + fic * GAMMAp_) - Upp_p1 * fic * GAMMA_ - Upp * (fic * GAMMA_p1 + fic_p1 * GAMMA_);
    const double Lrp3_p2 = dp2_p2 - whp2_p2 - U * (ficp2_p2 * GAMMA_ + ficp2 * GAMMA_p2 + 2.0 * ficp_p2 * GAMMAp_ + 2.0 * ficp * GAMMAp_p2 + fic_p2 * GAMMApp_ + fic * GAMMApp_p2) - U_p2 * (ficp2 * GAMMA_ + 2.0 * ficp * GAMMAp_ + fic * GAMMApp_) - 2.0 * Up * (ficp_p2 * GAMMA_ + ficp * GAMMA_p2 + fic_p2 * GAMMAp_ + fic * GAMMAp_p2) - 2.0 * Up_p2 * (ficp * GAMMA_ + fic * GAMMAp_) - Upp_p2 * fic * GAMMA_ - Upp * (fic * GAMMA_p2 + fic_p2 * GAMMA_);
    inter.Xpp = Xpp;
    inter.Ypp = Ypp;
    inter.cosLp2 = cosLp2;
    inter.sinLp2 = sinLp2;
    inter.hpp = hpp;
    inter.dp2 = dp2;
    inter.whp2 = whp2;
    inter.Ipp = Ipp;
    inter.Upp = Upp;
    inter.bpp = bpp;
    inter.cpp = cpp;
    inter.alphap2 = alphap2;
    inter.deltap2 = deltap2;
    inter.zgpp = zgpp;
    inter.fUzp2 = fUzp2;
    inter.GAMMApp_ = GAMMApp_;
    inter.xi1p2 = xi1p2;
    inter.xi2p2 = xi2p2;
    inter.qsp2 = qsp2;
    inter.psp2 = psp2;
    inter.Cpp = Cpp;
    inter.Dpp = Dpp;
    inter.rpnp2 = rpnp2;
    inter.rp3 = rp3;
    inter.rp3_nu = rp3_nu;
    inter.rp3_Lr = rp3_Lr;
    inter.rp3_q1 = rp3_q1;
    inter.rp3_q2 = rp3_q2;
    inter.rp3_p1 = rp3_p1;
    inter.rp3_p2 = rp3_p2;
    inter.fiDp2 = fiDp2;
    inter.fibp2 = fibp2;
    inter.ficp2 = ficp2;
    inter.fihp2 = fihp2;
    inter.fialphap2 = fialphap2;
    inter.fibm1p2 = fibm1p2;
    inter.fihr3p2 = fihr3p2;
    inter.firp3 = firp3;
    inter.fir2p3 = fir2p3;
    inter.fir3p3 = fir3p3;
    inter.f2rpp2 = f2rpp2;
    inter.f2cp_p = f2cp_p;
    inter.r3p3 = r3p3;
    inter.hr3p2 = hr3p2;
    inter.hrp = hrp;
    inter.Xpp_nu = Xpp_nu;
    inter.Xpp_Lr = Xpp_Lr;
    inter.Xpp_q1 = Xpp_q1;
    inter.Xpp_q2 = Xpp_q2;
    inter.Xpp_p1 = Xpp_p1;
    inter.Xpp_p2 = Xpp_p2;
    inter.Ypp_nu = Ypp_nu;
    inter.Ypp_Lr = Ypp_Lr;
    inter.Ypp_q1 = Ypp_q1;
    inter.Ypp_q2 = Ypp_q2;
    inter.Ypp_p1 = Ypp_p1;
    inter.Ypp_p2 = Ypp_p2;
    inter.cosLp2_nu = cosLp2_nu;
    inter.cosLp2_Lr = cosLp2_Lr;
    inter.cosLp2_q1 = cosLp2_q1;
    inter.cosLp2_q2 = cosLp2_q2;
    inter.cosLp2_p1 = cosLp2_p1;
    inter.cosLp2_p2 = cosLp2_p2;
    inter.sinLp2_nu = sinLp2_nu;
    inter.sinLp2_Lr = sinLp2_Lr;
    inter.sinLp2_q1 = sinLp2_q1;
    inter.sinLp2_q2 = sinLp2_q2;
    inter.sinLp2_p1 = sinLp2_p1;
    inter.sinLp2_p2 = sinLp2_p2;
    inter.zgpp_nu = zgpp_nu;
    inter.zgpp_Lr = zgpp_Lr;
    inter.zgpp_q1 = zgpp_q1;
    inter.zgpp_q2 = zgpp_q2;
    inter.zgpp_p1 = zgpp_p1;
    inter.zgpp_p2 = zgpp_p2;
    inter.fUzp2_nu = fUzp2_nu;
    inter.fUzp2_Lr = fUzp2_Lr;
    inter.fUzp2_q1 = fUzp2_q1;
    inter.fUzp2_q2 = fUzp2_q2;
    inter.fUzp2_p1 = fUzp2_p1;
    inter.fUzp2_p2 = fUzp2_p2;
    inter.Upp_nu = Upp_nu;
    inter.Upp_Lr = Upp_Lr;
    inter.Upp_q1 = Upp_q1;
    inter.Upp_q2 = Upp_q2;
    inter.Upp_p1 = Upp_p1;
    inter.Upp_p2 = Upp_p2;
    inter.bpp_nu = bpp_nu;
    inter.bpp_Lr = bpp_Lr;
    inter.bpp_q1 = bpp_q1;
    inter.bpp_q2 = bpp_q2;
    inter.bpp_p1 = bpp_p1;
    inter.bpp_p2 = bpp_p2;
    inter.cpp_nu = cpp_nu;
    inter.cpp_Lr = cpp_Lr;
    inter.cpp_q1 = cpp_q1;
    inter.cpp_q2 = cpp_q2;
    inter.cpp_p1 = cpp_p1;
    inter.cpp_p2 = cpp_p2;
    inter.hpp_nu = hpp_nu;
    inter.hpp_Lr = hpp_Lr;
    inter.hpp_q1 = hpp_q1;
    inter.hpp_q2 = hpp_q2;
    inter.hpp_p1 = hpp_p1;
    inter.hpp_p2 = hpp_p2;
    inter.alphap2_nu = alphap2_nu;
    inter.alphap2_Lr = alphap2_Lr;
    inter.alphap2_q1 = alphap2_q1;
    inter.alphap2_q2 = alphap2_q2;
    inter.alphap2_p1 = alphap2_p1;
    inter.alphap2_p2 = alphap2_p2;
    inter.deltap2_nu = deltap2_nu;
    inter.deltap2_Lr = deltap2_Lr;
    inter.deltap2_q1 = deltap2_q1;
    inter.deltap2_q2 = deltap2_q2;
    inter.deltap2_p1 = deltap2_p1;
    inter.deltap2_p2 = deltap2_p2;
    inter.Ipp_nu = Ipp_nu;
    inter.Ipp_Lr = Ipp_Lr;
    inter.Ipp_q1 = Ipp_q1;
    inter.Ipp_q2 = Ipp_q2;
    inter.Ipp_p1 = Ipp_p1;
    inter.Ipp_p2 = Ipp_p2;
    inter.dp2_nu = dp2_nu;
    inter.dp2_Lr = dp2_Lr;
    inter.dp2_q1 = dp2_q1;
    inter.dp2_q2 = dp2_q2;
    inter.dp2_p1 = dp2_p1;
    inter.dp2_p2 = dp2_p2;
    inter.whp2_nu = whp2_nu;
    inter.whp2_Lr = whp2_Lr;
    inter.whp2_q1 = whp2_q1;
    inter.whp2_q2 = whp2_q2;
    inter.whp2_p1 = whp2_p1;
    inter.whp2_p2 = whp2_p2;
    inter.GAMMApp_nu = GAMMApp_nu;
    inter.GAMMApp_Lr = GAMMApp_Lr;
    inter.GAMMApp_q1 = GAMMApp_q1;
    inter.GAMMApp_q2 = GAMMApp_q2;
    inter.GAMMApp_p1 = GAMMApp_p1;
    inter.GAMMApp_p2 = GAMMApp_p2;
    inter.xi1p2_nu = xi1p2_nu;
    inter.xi1p2_Lr = xi1p2_Lr;
    inter.xi1p2_q1 = xi1p2_q1;
    inter.xi1p2_q2 = xi1p2_q2;
    inter.xi1p2_p1 = xi1p2_p1;
    inter.xi1p2_p2 = xi1p2_p2;
    inter.xi2p2_nu = xi2p2_nu;
    inter.xi2p2_Lr = xi2p2_Lr;
    inter.xi2p2_q1 = xi2p2_q1;
    inter.xi2p2_q2 = xi2p2_q2;
    inter.xi2p2_p1 = xi2p2_p1;
    inter.xi2p2_p2 = xi2p2_p2;
    inter.qsp2_nu = qsp2_nu;
    inter.qsp2_Lr = qsp2_Lr;
    inter.qsp2_q1 = qsp2_q1;
    inter.qsp2_q2 = qsp2_q2;
    inter.qsp2_p1 = qsp2_p1;
    inter.qsp2_p2 = qsp2_p2;
    inter.psp2_nu = psp2_nu;
    inter.psp2_Lr = psp2_Lr;
    inter.psp2_q1 = psp2_q1;
    inter.psp2_q2 = psp2_q2;
    inter.psp2_p1 = psp2_p1;
    inter.psp2_p2 = psp2_p2;
    inter.Cpp_nu = Cpp_nu;
    inter.Cpp_Lr = Cpp_Lr;
    inter.Cpp_q1 = Cpp_q1;
    inter.Cpp_q2 = Cpp_q2;
    inter.Cpp_p1 = Cpp_p1;
    inter.Cpp_p2 = Cpp_p2;
    inter.Dpp_nu = Dpp_nu;
    inter.Dpp_Lr = Dpp_Lr;
    inter.Dpp_q1 = Dpp_q1;
    inter.Dpp_q2 = Dpp_q2;
    inter.Dpp_p1 = Dpp_p1;
    inter.Dpp_p2 = Dpp_p2;
    inter.fiDp2_nu = fiDp2_nu;
    inter.fiDp2_Lr = fiDp2_Lr;
    inter.fiDp2_q1 = fiDp2_q1;
    inter.fiDp2_q2 = fiDp2_q2;
    inter.fiDp2_p1 = fiDp2_p1;
    inter.fiDp2_p2 = fiDp2_p2;
    inter.rpnp2_nu = rpnp2_nu;
    inter.rpnp2_Lr = rpnp2_Lr;
    inter.rpnp2_q1 = rpnp2_q1;
    inter.rpnp2_q2 = rpnp2_q2;
    inter.rpnp2_p1 = rpnp2_p1;
    inter.rpnp2_p2 = rpnp2_p2;
    inter.rpp_nu = rpp_nu;
    inter.rpp_Lr = rpp_Lr;
    inter.rpp_q1 = rpp_q1;
    inter.rpp_q2 = rpp_q2;
    inter.rpp_p1 = rpp_p1;
    inter.rpp_p2 = rpp_p2;
    inter.r3p3_nu = r3p3_nu;
    inter.r3p3_Lr = r3p3_Lr;
    inter.r3p3_q1 = r3p3_q1;
    inter.r3p3_q2 = r3p3_q2;
    inter.r3p3_p1 = r3p3_p1;
    inter.r3p3_p2 = r3p3_p2;
    inter.hr3p2_nu = hr3p2_nu;
    inter.hr3p2_Lr = hr3p2_Lr;
    inter.hr3p2_q1 = hr3p2_q1;
    inter.hr3p2_q2 = hr3p2_q2;
    inter.hr3p2_p1 = hr3p2_p1;
    inter.hr3p2_p2 = hr3p2_p2;
    inter.fihr3p2_nu = fihr3p2_nu;
    inter.fihr3p2_Lr = fihr3p2_Lr;
    inter.fihr3p2_q1 = fihr3p2_q1;
    inter.fihr3p2_q2 = fihr3p2_q2;
    inter.fihr3p2_p1 = fihr3p2_p1;
    inter.fihr3p2_p2 = fihr3p2_p2;
    inter.firp3_nu = firp3_nu;
    inter.firp3_Lr = firp3_Lr;
    inter.firp3_q1 = firp3_q1;
    inter.firp3_q2 = firp3_q2;
    inter.firp3_p1 = firp3_p1;
    inter.firp3_p2 = firp3_p2;
    inter.fir2p3_nu = fir2p3_nu;
    inter.fir2p3_Lr = fir2p3_Lr;
    inter.fir2p3_q1 = fir2p3_q1;
    inter.fir2p3_q2 = fir2p3_q2;
    inter.fir2p3_p1 = fir2p3_p1;
    inter.fir2p3_p2 = fir2p3_p2;
    inter.fir3_p3_nu = fir3_p3_nu;
    inter.fir3_p3_Lr = fir3_p3_Lr;
    inter.fir3_p3_q1 = fir3_p3_q1;
    inter.fir3_p3_q2 = fir3_p3_q2;
    inter.fir3_p3_p1 = fir3_p3_p1;
    inter.fir3_p3_p2 = fir3_p3_p2;
    inter.f2rpp2_nu = f2rpp2_nu;
    inter.f2rpp2_Lr = f2rpp2_Lr;
    inter.f2rpp2_q1 = f2rpp2_q1;
    inter.f2rpp2_q2 = f2rpp2_q2;
    inter.f2rpp2_p1 = f2rpp2_p1;
    inter.f2rpp2_p2 = f2rpp2_p2;
    inter.f2cp_p_nu = f2cp_p_nu;
    inter.f2cp_p_Lr = f2cp_p_Lr;
    inter.f2cp_p_q1 = f2cp_p_q1;
    inter.f2cp_p_q2 = f2cp_p_q2;
    inter.f2cp_p_p1 = f2cp_p_p1;
    inter.f2cp_p_p2 = f2cp_p_p2;
    inter.ficp2_nu = ficp2_nu;
    inter.ficp2_Lr = ficp2_Lr;
    inter.ficp2_q1 = ficp2_q1;
    inter.ficp2_q2 = ficp2_q2;
    inter.ficp2_p1 = ficp2_p1;
    inter.ficp2_p2 = ficp2_p2;
    inter.fihp2_nu = fihp2_nu;
    inter.fihp2_Lr = fihp2_Lr;
    inter.fihp2_q1 = fihp2_q1;
    inter.fihp2_q2 = fihp2_q2;
    inter.fihp2_p1 = fihp2_p1;
    inter.fihp2_p2 = fihp2_p2;
    inter.fialphap2_nu = fialphap2_nu;
    inter.fialphap2_Lr = fialphap2_Lr;
    inter.fialphap2_q1 = fialphap2_q1;
    inter.fialphap2_q2 = fialphap2_q2;
    inter.fialphap2_p1 = fialphap2_p1;
    inter.fialphap2_p2 = fialphap2_p2;
    inter.fibp2_nu = fibp2_nu;
    inter.fibp2_Lr = fibp2_Lr;
    inter.fibp2_q1 = fibp2_q1;
    inter.fibp2_q2 = fibp2_q2;
    inter.fibp2_p1 = fibp2_p1;
    inter.fibp2_p2 = fibp2_p2;
    inter.fibm1_p2_nu = fibm1_p2_nu;
    inter.fibm1_p2_Lr = fibm1_p2_Lr;
    inter.fibm1_p2_q1 = fibm1_p2_q1;
    inter.fibm1_p2_q2 = fibm1_p2_q2;
    inter.fibm1_p2_p1 = fibm1_p2_p1;
    inter.fibm1_p2_p2 = fibm1_p2_p2;
    inter.hrp_nu = hrp_nu;
    inter.hrp_Lr = hrp_Lr;
    inter.hrp_q1 = hrp_q1;
    inter.hrp_q2 = hrp_q2;
    inter.hrp_p1 = hrp_p1;
    inter.hrp_p2 = hrp_p2;
    inter.fic = fic;
    inter.q1p3_nu = q1p3_nu;
    inter.q1p3_Lr = q1p3_Lr;
    inter.q1p3_q1 = q1p3_q1;
    inter.q1p3_q2 = q1p3_q2;
    inter.q1p3_p1 = q1p3_p1;
    inter.q1p3_p2 = q1p3_p2;
    inter.q2p3_nu = q2p3_nu;
    inter.q2p3_Lr = q2p3_Lr;
    inter.q2p3_q1 = q2p3_q1;
    inter.q2p3_q2 = q2p3_q2;
    inter.q2p3_p1 = q2p3_p1;
    inter.q2p3_p2 = q2p3_p2;
    inter.p1p3_nu = p1p3_nu;
    inter.p1p3_Lr = p1p3_Lr;
    inter.p1p3_q1 = p1p3_q1;
    inter.p1p3_q2 = p1p3_q2;
    inter.p1p3_p1 = p1p3_p1;
    inter.p1p3_p2 = p1p3_p2;
    inter.p2p3_nu = p2p3_nu;
    inter.p2p3_Lr = p2p3_Lr;
    inter.p2p3_q1 = p2p3_q1;
    inter.p2p3_q2 = p2p3_q2;
    inter.p2p3_p1 = p2p3_p1;
    inter.p2p3_p2 = p2p3_p2;
    inter.Lrp3_nu = Lrp3_nu;
    inter.Lrp3_Lr = Lrp3_Lr;
    inter.Lrp3_q1 = Lrp3_q1;
    inter.Lrp3_q2 = Lrp3_q2;
    inter.Lrp3_p1 = Lrp3_p1;
    inter.Lrp3_p2 = Lrp3_p2;
    inter.p1ppp_0 = p1ppp_0;
    inter.p2ppp_0 = p2ppp_0;
    inter.Lrppp_0 = Lrppp_0;
    inter.q1ppp_0 = q1ppp_0;
    inter.q2ppp_0 = q2ppp_0;

    // Fill coefficients struct
    out.q1ppp_0 = q1ppp_0;
    out.q2ppp_0 = q2ppp_0;
    out.p1ppp_0 = p1ppp_0;
    out.p2ppp_0 = p2ppp_0;
    out.Lrppp_0 = Lrppp_0;
    out.q1p3_nu = q1p3_nu;
    out.q1p3_Lr = q1p3_Lr;
    out.q1p3_q1 = q1p3_q1;
    out.q1p3_q2 = q1p3_q2;
    out.q1p3_p1 = q1p3_p1;
    out.q1p3_p2 = q1p3_p2;
    out.q2p3_nu = q2p3_nu;
    out.q2p3_Lr = q2p3_Lr;
    out.q2p3_q1 = q2p3_q1;
    out.q2p3_q2 = q2p3_q2;
    out.q2p3_p1 = q2p3_p1;
    out.q2p3_p2 = q2p3_p2;
    out.p1p3_nu = p1p3_nu;
    out.p1p3_Lr = p1p3_Lr;
    out.p1p3_q1 = p1p3_q1;
    out.p1p3_q2 = p1p3_q2;
    out.p1p3_p1 = p1p3_p1;
    out.p1p3_p2 = p1p3_p2;
    out.p2p3_nu = p2p3_nu;
    out.p2p3_Lr = p2p3_Lr;
    out.p2p3_q1 = p2p3_q1;
    out.p2p3_q2 = p2p3_q2;
    out.p2p3_p1 = p2p3_p1;
    out.p2p3_p2 = p2p3_p2;
    out.Lrp3_nu = Lrp3_nu;
    out.Lrp3_Lr = Lrp3_Lr;
    out.Lrp3_q1 = Lrp3_q1;
    out.Lrp3_q2 = Lrp3_q2;
    out.Lrp3_p1 = Lrp3_p1;
    out.Lrp3_p2 = Lrp3_p2;
}

void evaluate_order_3(
    const Order3Coefficients& coeffs,
    const double* dt_norm,
    std::size_t M,
    double* y_prop,
    Order1EvaluationScratch& scratch
) {
    for (std::size_t i = 0; i < M; ++i) {
        const double dt = dt_norm[i];
        const double q1ppp_0 = coeffs.q1ppp_0;
        const double q2ppp_0 = coeffs.q2ppp_0;
        const double p1ppp_0 = coeffs.p1ppp_0;
        const double p2ppp_0 = coeffs.p2ppp_0;
        const double Lrppp_0 = coeffs.Lrppp_0;
        const double q1p3_nu = coeffs.q1p3_nu;
        const double q1p3_Lr = coeffs.q1p3_Lr;
        const double q1p3_q1 = coeffs.q1p3_q1;
        const double q1p3_q2 = coeffs.q1p3_q2;
        const double q1p3_p1 = coeffs.q1p3_p1;
        const double q1p3_p2 = coeffs.q1p3_p2;
        const double q2p3_nu = coeffs.q2p3_nu;
        const double q2p3_Lr = coeffs.q2p3_Lr;
        const double q2p3_q1 = coeffs.q2p3_q1;
        const double q2p3_q2 = coeffs.q2p3_q2;
        const double q2p3_p1 = coeffs.q2p3_p1;
        const double q2p3_p2 = coeffs.q2p3_p2;
        const double p1p3_nu = coeffs.p1p3_nu;
        const double p1p3_Lr = coeffs.p1p3_Lr;
        const double p1p3_q1 = coeffs.p1p3_q1;
        const double p1p3_q2 = coeffs.p1p3_q2;
        const double p1p3_p1 = coeffs.p1p3_p1;
        const double p1p3_p2 = coeffs.p1p3_p2;
        const double p2p3_nu = coeffs.p2p3_nu;
        const double p2p3_Lr = coeffs.p2p3_Lr;
        const double p2p3_q1 = coeffs.p2p3_q1;
        const double p2p3_q2 = coeffs.p2p3_q2;
        const double p2p3_p1 = coeffs.p2p3_p1;
        const double p2p3_p2 = coeffs.p2p3_p2;
        const double Lrp3_nu = coeffs.Lrp3_nu;
        const double Lrp3_Lr = coeffs.Lrp3_Lr;
        const double Lrp3_q1 = coeffs.Lrp3_q1;
        const double Lrp3_q2 = coeffs.Lrp3_q2;
        const double Lrp3_p1 = coeffs.Lrp3_p1;
        const double Lrp3_p2 = coeffs.Lrp3_p2;
        const double dt3 = (dt * dt * dt);
        y_prop[idx_yprop(i, 1)] += q1ppp_0 * dt3 / 6.0;
        y_prop[idx_yprop(i, 2)] += q2ppp_0 * dt3 / 6.0;
        y_prop[idx_yprop(i, 3)] += p1ppp_0 * dt3 / 6.0;
        y_prop[idx_yprop(i, 4)] += p2ppp_0 * dt3 / 6.0;
        y_prop[idx_yprop(i, 5)] += Lrppp_0 * dt3 / 6.0;
        scratch.Lr_nu[i] += Lrp3_nu * dt3 / 6.0;
        scratch.Lr_Lr[i] += Lrp3_Lr * dt3 / 6.0;
        scratch.Lr_q1[i] += Lrp3_q1 * dt3 / 6.0;
        scratch.Lr_q2[i] += Lrp3_q2 * dt3 / 6.0;
        scratch.Lr_p1[i] += Lrp3_p1 * dt3 / 6.0;
        scratch.Lr_p2[i] += Lrp3_p2 * dt3 / 6.0;
        scratch.q1_nu[i] += q1p3_nu * dt3 / 6.0;
        scratch.q1_Lr[i] += q1p3_Lr * dt3 / 6.0;
        scratch.q1_q1[i] += q1p3_q1 * dt3 / 6.0;
        scratch.q1_q2[i] += q1p3_q2 * dt3 / 6.0;
        scratch.q1_p1[i] += q1p3_p1 * dt3 / 6.0;
        scratch.q1_p2[i] += q1p3_p2 * dt3 / 6.0;
        scratch.q2_nu[i] += q2p3_nu * dt3 / 6.0;
        scratch.q2_Lr[i] += q2p3_Lr * dt3 / 6.0;
        scratch.q2_q1[i] += q2p3_q1 * dt3 / 6.0;
        scratch.q2_q2[i] += q2p3_q2 * dt3 / 6.0;
        scratch.q2_p1[i] += q2p3_p1 * dt3 / 6.0;
        scratch.q2_p2[i] += q2p3_p2 * dt3 / 6.0;
        scratch.p1_nu[i] += p1p3_nu * dt3 / 6.0;
        scratch.p1_Lr[i] += p1p3_Lr * dt3 / 6.0;
        scratch.p1_q1[i] += p1p3_q1 * dt3 / 6.0;
        scratch.p1_q2[i] += p1p3_q2 * dt3 / 6.0;
        scratch.p1_p1[i] += p1p3_p1 * dt3 / 6.0;
        scratch.p1_p2[i] += p1p3_p2 * dt3 / 6.0;
        scratch.p2_nu[i] += p2p3_nu * dt3 / 6.0;
        scratch.p2_Lr[i] += p2p3_Lr * dt3 / 6.0;
        scratch.p2_q1[i] += p2p3_q1 * dt3 / 6.0;
        scratch.p2_q2[i] += p2p3_q2 * dt3 / 6.0;
        scratch.p2_p1[i] += p2p3_p1 * dt3 / 6.0;
        scratch.p2_p2[i] += p2p3_p2 * dt3 / 6.0;
    }
}

} // namespace geqoe
} // namespace astrodyn_core
