#include "numerical_solving_c.h"
#include "math.h"

// a,b,c,d are input vectors
// e,f are vectors used internally
// sol is the output
void tridiagonalSolving_c(const std::vector<double> &a, const std::vector<double> &b, const std::vector<double> &c, const std::vector<double> &d,
                          std::vector<double> &e, std::vector<double> &f, std::vector<double> &sol) {
  int n = b.size();
  //Forward steps
  double e_prev = 0.0;
  double f_prev = 0.0;
  for(int i=0;i<n;i++) {
    e[i] = c[i]/(b[i] - a[i]*e_prev);
    f[i] = (d[i] - a[i]*f_prev)/(b[i] - a[i]*e_prev);
    // Rcout<<i<< " "<< e[i]<< " "<< f[i]<<"\n";
    e_prev = e[i];
    f_prev = f[i];
  }
  //Backward steps
  sol[n-1] = f[n-1];
  for(int i = (n - 2);i>=0;i--) {
    sol[i] = f[i] - e[i]*sol[i + 1];
  }
}


double normal_cdf(double x, double mean, double stddev) {
  return 0.5 * (1 + std::erf((x - mean) / (stddev * std::sqrt(2.0))));
}
