#include <stdlib.h>
#include <math.h>
#include <fstream.h>
#include <iostream.h>
#include <strstream.h>
#include "nrutil.h"
#include "nrutil.c"
#include "sort.c"
#include "kstwo.c"
#include "probks.c"
#define EPS1 0.001
#define EPS2 1.0e-8

//------>START PROGRAM HERE
int main(){
  //THIS PROGRAM IS FOR PROBLEM 3 of HOMEWORK 2 NLD
  
  //WE CONSTRUCT TWO SETS OF LOGISTIC MAP OUTPUT
  //ONE IS THE BASE SET
  
  //THE OTHER IS THE EMPIRICAL SET

  //WE THEN DO A NEAREST NEIGHBOR FORWARD ITERATION 
  //COMPARISON;  THEN WE HISTOGRAM DIFFERENCES

  //------>PREP THE COMPUTER FOR PRINTING TO FILE
  ofstream outf("log.dat");
  //The output file will be pro1.dat
  //ifstream inf("randomlist.dat");

  //***SHADOW PROGRAMMING,
  //***ignore the stuff with astericks
  ofstream outf2("test1.dat"); //************
  ofstream outf3("test2.dat"); //************
  ofstream outf4("test3.dat"); //************
  //*****************************************


  //------>DECLARE VARIABLES HERE
  int NUM=10000;//as requested
  double diff,r;
  double STORAGE[NUM];
  double WORK[NUM];
  double DIFF[NUM];
  double H[200];
  int MARK[NUM];
  int flag=0;
  double difff,different;
  int marker;
  int k=0;
  int ii=0;
  char buffer[1900];
  double x,x_n;
  //------>ACTION
  double lambda=4.0;
  x=0.38;
  for (int i=0; i<NUM; i++){
    STORAGE[i] = lambda*x*(1-x);
    x=STORAGE[i];}
  
  x=0.2;
  flag = -1;
  for(int i=0; i<NUM*5; i++){
    r=lambda*x*(1-x);
    flag=flag+1; 
    if(flag>=8){WORK[k]=r;k++;}if(flag==9){flag=-1;}
    x=r;
    
  } 

  for(int i=0; i<NUM-1; i++){
  difff=100;
    for(int j=0;j<NUM;j++){
      different=fabs(WORK[i]-STORAGE[j]);
      if(different<difff){difff=different; MARK[i]=j;}
    }}
  //***printf("%i \n",marker);}
  //MARK gives us a list of the nearest match that corresponds
  //with the ith entry of WORK
  
  //Now we can project and find the next iteration in each, and 
  //get the difference thereof
  int m;
  for(int i=0; i<NUM-1;i++){
    m = MARK[i];
    //***printf("%i \n",m);
    DIFF[i]=WORK[i+1]-STORAGE[m+1] +1;
   }
  //Finally, we want to bin these numbers.  
  //The differences should range from 0 to +/- 1
  //We'll do a binning of 0.01
  //This is 200 bins total
  //We multiply the differences by 100
  //and round to the lowest integer (1.9 --> 190; 1.96-->1.9)
  //int jj;
  //for(jj=0; jj<NUM-1; jj++){    
  //for(int i=0; i<200; i++){
  //  if(DIFF[jj]==i){H[i]=H[i]+1;}
  //}}
  //

  for(int i=0; i<200;i++){
   H[i]=0;}

  for (int j = 0; j<200;j++){
  for (int i = 0; i<NUM; i++){
    if (DIFF[i]<=(0.01 * (j+1))) if (DIFF[i]>=(0.01*(j))) {H[j] = H[j]+1;}}}
    
  for(int i=0; i<200;i++){
  outf<<(1.0*i)/100.0 -1.0<<"\t"<<H[i]<<"\n";
   }

  for(int j=0; j<NUM;j++){
    outf2<<STORAGE[j]<<"\n";
    outf3<<WORK[j]<<"\n";
    outf4<<DIFF[j]<<"\n";
  }

  double H2[200];
  H2[0]=0.0;
  for(int i=1; i<200;i++){H2[i]=H2[i-1]+1.0*H[i];}
 
  double H3[200]; H3[0]=0.0;
  for(int i=1; i<200;i++){H3[i]=H3[i-1]+20;if(i==99){H3[i]=H3[i-1]+3000;}if(i==100){H3[i]=H3[i-1]+3000;}}

  ofstream outf5("add1.dat");
  ofstream outf6("add2.dat");
  for(int i=0; i<200;i++){
  outf5<<H2[i]<<"\n";
  outf6<<H3[i]<<"\n";}
  //This crap comes from Numerical Recipes
  double d=0.0;
  double pro;
  int j1=1, j2=1;
  double d1,d2,en1,en2,en,fn1=0.0,fn2=0.0,dt;
    	en1=200;
	en2=200;
        double	n1=200;
	double n2=200;

	while (j1 <= 200 && j2 <= 200) {
		if ((d1=H2[j1]) <= (d2=H3[j2])) fn1=j1++/en1;
		if (d2 <= d1) fn2=j2++/en2;
		if ((dt=fabs(fn2-fn1)) > d) d=dt;
	}

	printf("D IS %f \n",d);
   en=sqrt(en1*en2/(en1+en2));
   double alam=(en+0.12+0.11/en)*(d); 
   double probs=3.14;
   	int j;
	double a2,fac=2.0,sum=0.0,term,termbf=0.0;

	a2 = -2.0*alam*alam; printf("lambda is %f \n",a2);
	for (j=1;j<=100;j++) {
		term=fac*exp(a2*j*j);
		sum += term; //printf("%f \n",term);
		fac = -fac;
		termbf=fabs(term);
	}


	//kstwo(H2,200,H3,200,d,pro);
  printf("Eta %f ",sum);
}  

