/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * ODEsystem_2F_Gaussian_cg.c
 *
 * Code generation for function 'ODEsystem_2F_Gaussian_cg'
 *
 */

/* Include files */
#include "ODEsystem_2F_Gaussian_cg.h"
#include "rt_nonfinite.h"
#include "mwmathutil.h"

/* Function Definitions */
void ODEsystem_2F_Gaussian_cg(const real_T coeff[6], real_T uu, real_T r,
                              real_T zeta, real_T dcoeff[6])
{
  real_T b_dcoeff_tmp;
  real_T b_dcoeff_tmp_tmp;
  real_T c_dcoeff_tmp;
  real_T c_dcoeff_tmp_tmp;
  real_T d_dcoeff_tmp;
  real_T dcoeff_tmp;
  real_T dcoeff_tmp_tmp;
  real_T e_dcoeff_tmp;
  real_T f_dcoeff_tmp;
  real_T g_dcoeff_tmp;
  real_T h_dcoeff_tmp;
  /*  System of ODE that solves for the coefficients of the MGF/CF function */
  /* ==========================================================================
   */
  /*  INPUTS: dt     ---> time step (used by the matlab ODE solver) */
  /*          coeffv ---> value of the coefficient */
  /*          x      ---> vector with the model (2-factor) parameters */
  /*          uu     ---> point where to compute the MGF(uu) */
  /*          rf     ---> risk-free rate */
  /*          d      ---> dividend yield */
  /*  */
  /*  OUTPUT: dcoeff ---> MGF coefficients (real and imaginary part): */
  /*                      alpha_real */
  /*                      alpha_img   */
  /*                      beta1_real   */
  /*                      beta1_img */
  /*                      beta2_real */
  /*                      beta2_img */
  /*                        */
  /* --------------------------------------------------------------------------
   */
  /*  Nicola Fusari (2015): */
  /*  You can distribute and use the code. Please cite the author if you use */
  /*  the code in in your research. */
  /* --------------------------------------------------------------------------
   */
  /*  Parameters assignement: */
  /* ------------------------ */
  /*  Compute the correlated jumps compensator: */
  /* --------------------------------------------------------------------------
   */
  /*  alpha */
  dcoeff_tmp = 0.0 * (0.00564001 * uu - 0.0501);
  b_dcoeff_tmp = uu * uu;
  dcoeff_tmp_tmp = muDoubleScalarExp(-0.0501 * uu + 0.002820005 * b_dcoeff_tmp);
  c_dcoeff_tmp = dcoeff_tmp_tmp * 1.008;
  d_dcoeff_tmp = muDoubleScalarCos(dcoeff_tmp);
  e_dcoeff_tmp = (coeff[2] * 0.093 - 1.0) + 0.0 * uu;
  dcoeff_tmp = muDoubleScalarSin(dcoeff_tmp);
  b_dcoeff_tmp_tmp = coeff[3] * coeff[3];
  c_dcoeff_tmp_tmp = coeff[2] * coeff[2];
  f_dcoeff_tmp =
      0.093 *
          ((((c_dcoeff_tmp_tmp * 0.093 + 0.093 * b_dcoeff_tmp_tmp) - 0.0 * uu) +
            0.0 * b_dcoeff_tmp) +
           2.0 * coeff[2] * (0.0 * uu - 1.0)) +
      1.0;
  g_dcoeff_tmp = -e_dcoeff_tmp * d_dcoeff_tmp - 0.093 * coeff[3] * dcoeff_tmp;
  h_dcoeff_tmp = coeff[4] * 0.0 * 0.0;
  dcoeff[0] =
      ((((-zeta * uu + r * uu) - 1.008 * (-0.04617970471511712 * uu + 1.0)) +
        coeff[2] * 4.032 * 0.0144) +
       h_dcoeff_tmp) +
      c_dcoeff_tmp * g_dcoeff_tmp / f_dcoeff_tmp;
  dcoeff_tmp = 0.093 * coeff[3] * d_dcoeff_tmp - e_dcoeff_tmp * dcoeff_tmp;
  dcoeff[1] = ((-((zeta - 0.046549142352838059) - r) * 0.0 +
                coeff[3] * 4.032 * 0.0144) +
               coeff[5] * 0.0 * 0.0) +
              c_dcoeff_tmp * dcoeff_tmp / f_dcoeff_tmp;
  /*  beta1 */
  c_dcoeff_tmp = 2.0 * coeff[2] * -0.48 * 0.2 - 1.0;
  d_dcoeff_tmp = 2.0 * dcoeff_tmp_tmp * 0.0;
  e_dcoeff_tmp = d_dcoeff_tmp * g_dcoeff_tmp / f_dcoeff_tmp;
  dcoeff[2] =
      0.5 *
      ((((((-2.0 * coeff[2] * 4.032 - b_dcoeff_tmp_tmp * 0.040000000000000008) +
           c_dcoeff_tmp_tmp * 0.040000000000000008) -
          2.0 * coeff[3] * -0.48 * 0.2 * 0.0) +
         c_dcoeff_tmp * uu) +
        b_dcoeff_tmp) +
       e_dcoeff_tmp);
  dcoeff[3] =
      0.5 * ((0.0 * (c_dcoeff_tmp + 2.0 * uu) +
              2.0 * coeff[3] * (0.2 * (coeff[2] * 0.2 + -0.48 * uu) - 4.032)) +
             d_dcoeff_tmp * dcoeff_tmp / f_dcoeff_tmp);
  /*  beta2 */
  dcoeff[4] = 0.5 * ((((((-2.0 * coeff[4] * 0.0 - coeff[5] * coeff[5] * 0.0) +
                         coeff[4] * coeff[4] * 0.0) -
                        2.0 * coeff[5] * 0.0 * 0.0 * 0.0) +
                       (2.0 * coeff[4] * 0.0 * 0.0 - 1.0) * uu) +
                      b_dcoeff_tmp) +
                     e_dcoeff_tmp);
  dcoeff[5] = (0.0 * ((h_dcoeff_tmp - 0.5) + uu) +
               coeff[5] * (0.0 * (coeff[4] * 0.0 + 0.0 * uu))) +
              dcoeff_tmp_tmp * 0.0 * dcoeff_tmp / f_dcoeff_tmp;
}

void b_ODEsystem_2F_Gaussian_cg(const real_T coeff[6], const creal_T uu,
                                real_T r, real_T zeta, real_T dcoeff[6])
{
  real_T a_tmp;
  real_T b_dcoeff_tmp;
  real_T b_dcoeff_tmp_tmp;
  real_T c_dcoeff_tmp;
  real_T c_dcoeff_tmp_tmp;
  real_T d_dcoeff_tmp;
  real_T d_dcoeff_tmp_tmp;
  real_T dcoeff_tmp;
  real_T dcoeff_tmp_tmp;
  real_T e_dcoeff_tmp;
  real_T f_dcoeff_tmp;
  real_T g_dcoeff_tmp;
  real_T h_dcoeff_tmp;
  /*  System of ODE that solves for the coefficients of the MGF/CF function */
  /* ==========================================================================
   */
  /*  INPUTS: dt     ---> time step (used by the matlab ODE solver) */
  /*          coeffv ---> value of the coefficient */
  /*          x      ---> vector with the model (2-factor) parameters */
  /*          uu     ---> point where to compute the MGF(uu) */
  /*          rf     ---> risk-free rate */
  /*          d      ---> dividend yield */
  /*  */
  /*  OUTPUT: dcoeff ---> MGF coefficients (real and imaginary part): */
  /*                      alpha_real */
  /*                      alpha_img   */
  /*                      beta1_real   */
  /*                      beta1_img */
  /*                      beta2_real */
  /*                      beta2_img */
  /*                        */
  /* --------------------------------------------------------------------------
   */
  /*  Nicola Fusari (2015): */
  /*  You can distribute and use the code. Please cite the author if you use */
  /*  the code in in your research. */
  /* --------------------------------------------------------------------------
   */
  /*  Parameters assignement: */
  /* ------------------------ */
  /*  Compute the correlated jumps compensator: */
  /* --------------------------------------------------------------------------
   */
  /*  alpha */
  a_tmp = coeff[3] + 0.0 * uu.im;
  dcoeff_tmp = uu.im * (0.00564001 * uu.re - 0.0501);
  b_dcoeff_tmp = uu.re * uu.re;
  dcoeff_tmp_tmp = uu.im * uu.im;
  b_dcoeff_tmp_tmp = muDoubleScalarExp(
      -0.0501 * uu.re + 0.002820005 * (-dcoeff_tmp_tmp + b_dcoeff_tmp));
  c_dcoeff_tmp = b_dcoeff_tmp_tmp * 1.008;
  d_dcoeff_tmp = 0.093 * a_tmp;
  e_dcoeff_tmp = muDoubleScalarCos(dcoeff_tmp);
  c_dcoeff_tmp_tmp = 0.0 * uu.re;
  f_dcoeff_tmp = (coeff[2] * 0.093 - 1.0) + c_dcoeff_tmp_tmp;
  dcoeff_tmp = muDoubleScalarSin(dcoeff_tmp);
  d_dcoeff_tmp_tmp = coeff[2] * coeff[2];
  a_tmp = 0.093 * ((((d_dcoeff_tmp_tmp * 0.093 + 0.093 * (a_tmp * a_tmp)) -
                     c_dcoeff_tmp_tmp) +
                    0.0 * b_dcoeff_tmp) +
                   2.0 * coeff[2] * (c_dcoeff_tmp_tmp - 1.0)) +
          1.0;
  g_dcoeff_tmp = -f_dcoeff_tmp * e_dcoeff_tmp - d_dcoeff_tmp * dcoeff_tmp;
  h_dcoeff_tmp = coeff[4] * 0.0 * 0.0;
  dcoeff[0] = ((((-zeta * uu.re + r * uu.re) -
                 1.008 * (-0.04617970471511712 * uu.re + 1.0)) +
                coeff[2] * 4.032 * 0.0144) +
               h_dcoeff_tmp) +
              c_dcoeff_tmp * g_dcoeff_tmp / a_tmp;
  dcoeff_tmp = d_dcoeff_tmp * e_dcoeff_tmp - f_dcoeff_tmp * dcoeff_tmp;
  dcoeff[1] = ((-((zeta - 0.046549142352838059) - r) * uu.im +
                coeff[3] * 4.032 * 0.0144) +
               coeff[5] * 0.0 * 0.0) +
              c_dcoeff_tmp * dcoeff_tmp / a_tmp;
  /*  beta1 */
  c_dcoeff_tmp = 2.0 * coeff[2] * -0.48 * 0.2 - 1.0;
  d_dcoeff_tmp = 2.0 * b_dcoeff_tmp_tmp * 0.0;
  e_dcoeff_tmp = d_dcoeff_tmp * g_dcoeff_tmp / a_tmp;
  dcoeff[2] = 0.5 * (((((((-2.0 * coeff[2] * 4.032 -
                           coeff[3] * coeff[3] * 0.040000000000000008) +
                          d_dcoeff_tmp_tmp * 0.040000000000000008) -
                         2.0 * coeff[3] * -0.48 * 0.2 * uu.im) -
                        dcoeff_tmp_tmp) +
                       c_dcoeff_tmp * uu.re) +
                      b_dcoeff_tmp) +
                     e_dcoeff_tmp);
  dcoeff[3] =
      0.5 *
      ((uu.im * (c_dcoeff_tmp + 2.0 * uu.re) +
        2.0 * coeff[3] * (0.2 * (coeff[2] * 0.2 + -0.48 * uu.re) - 4.032)) +
       d_dcoeff_tmp * dcoeff_tmp / a_tmp);
  /*  beta2 */
  dcoeff[4] = 0.5 * (((((((-2.0 * coeff[4] * 0.0 - coeff[5] * coeff[5] * 0.0) +
                          coeff[4] * coeff[4] * 0.0) -
                         2.0 * coeff[5] * 0.0 * 0.0 * uu.im) -
                        dcoeff_tmp_tmp) +
                       (2.0 * coeff[4] * 0.0 * 0.0 - 1.0) * uu.re) +
                      b_dcoeff_tmp) +
                     e_dcoeff_tmp);
  dcoeff[5] = (uu.im * ((h_dcoeff_tmp - 0.5) + uu.re) +
               coeff[5] * (0.0 * (coeff[4] * 0.0 + c_dcoeff_tmp_tmp))) +
              b_dcoeff_tmp_tmp * 0.0 * dcoeff_tmp / a_tmp;
}

/* End of code generation (ODEsystem_2F_Gaussian_cg.c) */
