#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <time.h>
#include "Exception.h"
#include "MathP.h"
#include "TimeP.h"
#include "Chebyshev.h"

int main(int argc, char **argv) {

  if (argc != 2) {
    fprintf(stderr, "usage: %s nCoeffs\n", argv[1]);
    return -1;
  }

  double lower = 0.;
  double upper = 100.;
  int nCoeffs = atoi(argv[1]);

  Chebyshev power(lower, upper, nCoeffs, power);

  int nSlices = 100000;
  double xIncrement = (upper - lower) / nSlices;
  int j;

  double a, b, c;
  struct timespec startTime;
  struct timespec endTime;
  double execTime;

  fprintf(stderr, "Compute using library: ");
  double x = 0.;
  clock_gettime(CLOCK_REALTIME, &startTime);
  for (j = 0; j < nSlices; j++, x += xIncrement) {
    a = sin(x);
    b = cos(x);
    c = tan(x);
  }
  clock_gettime(CLOCK_REALTIME, &endTime);
  execTime = Time::seconds(&endTime) - Time::seconds(&startTime);
  fprintf(stderr, "done. t = %.0f msec\n", execTime * 1000);

  fprintf(stderr, "Compute using Chebyshev: ");
  x = 0.;
  clock_gettime(CLOCK_REALTIME, &startTime);
  for (j = 0; j < nSlices; j++, x += xIncrement) {
    a = sine->evaluate(x);
    b = cosine->evaluate(x);
    c = tangent->evaluate(x);
  }
  clock_gettime(CLOCK_REALTIME, &endTime);
  execTime = Time::seconds(&endTime) - Time::seconds(&startTime);
  fprintf(stderr, "done. t = %.0f msec\n", execTime * 1000);

  delete sine;
  delete cosine;
  delete tangent;

  return 0;
}
