// This is curvefit.cc
// defining the memberfunctions from curvefit.h

#include<math.h>
#include"curvefit.h"
#include<fstream.h>
#include<assert.h> // program stops if error
#include<string.h>
#include<strstream.h>

const int STRINGSIZE = 128;
const int FILENAMESIZE = 10;
	
void Data :: SaveData( Fit& fit) // Save the data and the fitted formula in a file
	{
	char s[STRINGSIZE];
	char filename[FILENAMESIZE];
	cout << endl;
	cout << endl;
	cout << "(max 6 characters in front of .dat)" << endl;
	cout << "Type filename *.dat : " << endl;
	cin >> filename;
	ofstream outfile(filename); 
	
	outfile << "# Data to be plotted:" << endl;
	for(int i = 0; i < Size(); i++ )
		{
		outfile << ptr_x[i] << " " << ptr_y[i] << endl;
		}
	Formula(fit, s);
	outfile << "# Fitted curve is " << s << endl;
	outfile.close();
	delete [] s;
	}

float*  Data :: X()		// Get pointer to x-coordinates
	{ return ptr_x; }
	
float* Data :: Y()		// Get pointer to y-coordinates
	{ return ptr_y; }
	
unsigned int Data :: Size()	// Get no of data points
	{ return size; }
	
void Data :: ReadData( istream& in)	// Read in the values (here I 
					// use ReadData(cin)
	{
	for ( int i = 0; i < Size(); i++ )
		{ in >> ptr_x[i] >> ptr_y[i]; } 	
	}
	
void Data :: SetZero(float* &ptr)	// To prevent memory leaks
	{
	if( ptr!= 0)
		ptr = 0;
	}


LinearData :: LinearData() 	// Constructor, doesn't do anything interesting
	{
	size = 0;
	ptr_x = 0;
	ptr_y = 0;
	}

LinearData :: LinearData( unsigned int n)	// constructor
	{ 
	size = n;
	ptr_x = new float[n];
	ptr_y = new float[n];
	assert((n>0) && ptr_x && ptr_y); // if n or ptr_x or ptr_y = 0
					// then error
	}
	
LinearData :: ~LinearData()
	{
	delete [] ptr_x;
	delete [] ptr_y;
	}

void LinearData :: Linearize(float* &newX, float* &newY, 
			 unsigned int& newSize) 
			 
	/* A conversion is not needed in this case but Fit still has
	to know what the fit data should be */
	 {
	 SetZero(newX);		// In case they are not properly initialized
	 SetZero(newY);
	 newSize = Size(); 		// set the "converted" values
	 newX = new float[newSize]; 	// equal to the old ones
	 newY = new float[newSize];
	 for ( int i = 0; i< Size(); i++)
		{
		  newX[i] = ptr_x[i]; 
		  newY[i] = ptr_y[i];
		}
	
	 }

// The Formula() functions all have the purpose of writing a fitted formula
// out to the datafile
void LinearData ::  Formula( Fit& fit, char s[] )
	{
        strstream sout(s, STRINGSIZE);
	switch(fit.KindOfFit())
		{
	case 1: {
		sout << "y= " << fit.Intersect() << " + x*" 
			<< fit.Slope() << '\0' << endl;
		break;
		}
	default:
		{
		cout << "Fit not found" << endl;
		break;
		}	
		}
        }
        
ExponentialData :: ExponentialData() {	// Constructor, doesn't do anything interesting
	size = 0;
	ptr_x = 0;
	ptr_y = 0;
	}

ExponentialData :: ExponentialData( unsigned int n) // constructor
	{
	size = n;
	ptr_x = new float[n];
	ptr_y = new float[n];
	}
	
ExponentialData :: ~ExponentialData()
	{
	delete [] ptr_x;
	delete [] ptr_y;
	}
	
void ExponentialData :: Linearize(float* &newX, float* &newY,
			 unsigned int& newSize) 
	 
	 /* here I need to manipulate the y-data to be able to use
	 StraightLineFit */
	 {
	 SetZero(newX);
	 SetZero(newY);
	 newSize = Size(); 
	 newX = new float[newSize];
	 newY = new float[newSize];
	 for ( int i = 0; i < Size(); i++ )
	 	{
	 	if ( ptr_y[i] <= 0 ) // if ln(y) not defined don't use
	 				// that data point
	 		{
	 		delete [] newX;
	 		delete [] newY;
	 		newSize--;
	 		newX = new float[newSize]; 
	 		newY = new float[newSize];
	 		}
	 	
	 	else		// if everything is OK
	 		{
	 		newX[i] = ptr_x[i];
	 		newY[i] = log( ptr_y[i] ); // log = ln
	 		}
	 	}
	 	
	 cout << endl;
	 cout << "No. of points used in fit: " << newSize << endl;
	 }
	 		
void  ExponentialData :: Formula( Fit &fit, char s[]) 
	{
	strstream sout(s, STRINGSIZE);
	switch(fit.KindOfFit())
		{
	case 1: {
		sout << "y= " << exp(fit.Intersect()) << 
			"* exp(x*" << fit.Slope() << " )" << '\0' << endl;
		break;
		}
	default:
		{
		cout << "Fit not found" << endl;
		break;
		}	
		}
	
	}
	
	
CosineData :: CosineData() {	// Constructor, doesn't do anything interesting
	size = 0;
	ptr_x = 0;
	ptr_y = 0;
	}
	
CosineData :: CosineData( unsigned int n )	// constructor
	{
	size = n;
	ptr_x = new float[n];
	ptr_y = new float[n];
	}

CosineData :: ~CosineData()
	{
	delete [] ptr_x;
	delete [] ptr_y;
	}
	
void CosineData :: Linearize(float* &newX, float* &newY,
			 unsigned int& newSize) 
	 
	 /* again a conversion is needed to use StraightLineFit
	 It will need lny and ln(cosx)) as input */
	 {
	 SetZero(newX);
	 SetZero(newY);
	 newSize = Size();
	 newX = new float[newSize]; 
	 newY = new float[newSize];
	 for ( int i = 0; i < Size(); i++ )
	 	{
	 	
	 	// if ln not defined throw away the point
	 	if ( (ptr_y[i] == 0) || ( cos(ptr_x[i]) == 0 ) )
	 		{
	 		delete [] newX;
	 		delete [] newY;
	 		newSize--;
	 		newX = new float[newSize];
	 		newY = new float[newSize];
	 		}
	 	
	 	else  	// if no problem
	 		{
	 		newX[i] = log( abs( cos(ptr_x[i])) );
	 		newY[i] = log( abs( ptr_y[i] ) );
	 		}
	 	}
	 cout << endl;	
	 cout << "No. of points used in fit: " << newSize << endl;	
	 }
	 
	 
void CosineData :: Formula( Fit &fit, char s[]) 
	{
	strstream sout(s, STRINGSIZE);
	switch(fit.KindOfFit())
		{
	case 1: { 
		sout << "y= " << exp(fit.Intersect()) << 
		"* (cos(x))^( " << fit.Slope() << " )" << '\0' << endl; 
		break;
		}
	default:
		{
		cout << "Fit not found" << endl;
		break;
		}
		}
		
	}



Fit :: ~Fit()	// destructor
	{
	delete [] convertedX;
	delete [] convertedY;
	}
	
int Fit :: KindOfFit() { return 0; }
	

StraightLineFit :: StraightLineFit() { slope = 0; intersect = 0; }

int StraightLineFit :: KindOfFit()
	{
	return 1;
	}
	
	
StraightLineFit :: StraightLineFit( Data &fitInput )   // constructor,
					// creates the fit
	{
	// convert data
	fitInput.Linearize( convertedX, convertedY, convertedSize );
	
	// initialize and calculate slope, intersection etc.
	float sumXsq = 0, sumX=0, sumY=0, sumXY = 0; 
	for( int i = 0; i< convertedSize; i++ )
		{ 
		sumXsq +=  convertedX[i] * convertedX[i];
		sumX += convertedX[i];
		sumY += convertedY[i];
		sumXY += convertedX[i] * convertedY[i];
		}
	float delta = convertedSize * sumXsq - ( sumX * sumX );
	intersect = ( sumXsq * sumY - sumX * sumXY ) / delta;
	slope = ( convertedSize * sumXY - sumX * sumY ) / delta;
	deltaSlope = sqrt( abs(sumXsq / delta) );
	deltaIntc = sqrt( abs(convertedSize / delta) );
	}
	
float StraightLineFit :: Slope() { return slope; }

float StraightLineFit :: Intersect() { return intersect; }
	
float StraightLineFit :: DeltaSlope() { return deltaSlope; }

float StraightLineFit :: DeltaIntc() { return deltaIntc; }


float abs(float argument)
	{
	float returnValue;
	returnValue = ( argument < 0.0 ) ? -argument : argument;
	return returnValue;
	}


