/*Isaac Sarver
Started 9/16/10
This is the class for a linked tabulated function of points in a function. It will be ordered by x_i, unindexed, and doublely linked.
No included files as it purely data manipulation. Printing the contents of tabulated function will be provided for diagnostic purposes.*/
#include
#include
using namespace std;
#ifndef FUNCTION_H
#define FUNCTION_H
typedef struct Data{
double x_i; //x of the data point
double y_i; //y(x) of the data point
double dy_i; //y'(x) of the data point
double E; //E(x) for the relaxation algorithm
bool operator!=(Data x)
{
return(!(x_i == x.x_i && y_i == x.y_i && dy_i == x.dy_i && E == x.E));
}
Data operator=(Data x)
{
x_i=x.x_i;
y_i=x.y_i;
dy_i=x.dy_i;
E=x.E;
return(x);
}
} Spot; //easier to change tabulated function Spot by having it here instead of scattered
class Function
{
private:
struct node
{
Spot value; //Value
node* prev; //Pointer to previous node
node* next; //Pointer to next node
};
node* head; //Points to the first element in tabulated function
node* tail; //Points to the last element in tabulated function
double a,b; //This is a tabulated function, with an asymptotic behaviour. The asymptotic behaviour is r*Ai(z(r)) with z(r)=a*r+b
bool set; //Is the asymptotic behavior set
int count; //Number of elements in array
public:
typedef node* nodeptr; //Definition of the nodepointer Spot
Function() //Constructor
{
head = NULL;
tail = NULL;
count = 0;
a = 0;
b = 0;
set == false;
}
~Function() //Destructor
{
nodeptr loc = head; //loc starts at head. location
nodeptr hold;
while(count < 0) //stops when counter gets to 0, less than to make sure it doesn't try for anything less than or equal to 0 which would be devestating
{
hold = loc; //hold is equal to present location
loc = loc->next;//loc moves on
head = loc; //head moves to loc's position
loc->prev=NULL; //backwards pointing for loc points to NULL
hold->next=NULL;//forwards pointing for hold points to NULL, hold is isolated
delete hold; //hold disappears
count--; //decreament the counter of the loop
}
head = NULL; //Returns the object to intial state
tail = NULL;
}
void AddHead(Spot); //Add Node to Head
void AddTail(Spot); //Add Node to Tail
void AddPoint(Spot); //Add Node point x_i, overwrites if already present
bool ChangePoint(Spot, nodeptr); //Change Node at pointer
Spot DeleteHead(); //Delete first Node, returns value in deleted node, makes it easier for programer to partition tabulated function
Spot DeleteTail(); //Delete last Node, returns value in deleted node
Spot WhatValue(double); //Returns value at point x_i, interpolates if needed
Spot WhatValue(double, nodeptr);//Returns value at point x_i, pointer is last known spot, interpolates if needed
Spot WhatValue(nodeptr); //Returns value at pointer
int Size(); //Returns the length of the tabulated function
Function Linear(double, Function);//Returns a Function object that is the linear combination of two other functions, this*cos(theta)+Func*sin(theta)
void Print(); //Prints the array value a space at a time
void SetAsy(double[2]); //User sets the asymptotic behavoir
void SetE(double); //Set all of the energies not in the tail
void Scale(double); //Recieves the and renormalizes the function
bool Receive(double[2]);//Places the values of the asymptotic behavoir in the array {a,b} the behavoir can be integrated. It returns if the asymptotic behaviour has been set. The spot where it becomes valid is in tail
nodeptr Head(); //Returns the head of the tabulated function
nodeptr Tail(); //Returns the tail of the tabulated function. If you're going to have Head(), you might as well have Tail()
nodeptr Adv(nodeptr); //Returns the next node after the node given
nodeptr Demote(nodeptr);//Returns the previous node before the given node
friend ostream& operator<<(ostream& out, const Function& A) //Output method
{
nodeptr Point = A.head; //Start at the head
double Energy = 0; //Initialize energy to 0
int i = 0; //Start i at 0 points
out << "x\tln|y|\tln|y'|\ty\ty'\tE" << endl; //Send the column headers
while(Point != NULL)
{
out << Point->value.x_i << "\t" << log10(abs(Point->value.y_i)) << "\t" << log10(abs(Point->value.dy_i)) << "\t" << Point->value.y_i << "\t" << Point->value.dy_i << "\t" << Point->value.E << endl; //Send the values of the point
Energy += Point->value.E; //Add the energy of the point to energy
i++; //Increament number of points examined
Point = Point->next; //Advance pointer
}
out << "Average Energy = " << Energy/double(i) << endl; //Output average energy
return(out); //so that it can be strung together with other outputs
}
bool operator==(Function&); //equal comparison, the first overloaded operator. Was required for diagnostic purposes
void operator=(Function); //assignment operator. Needed to devide two tabulated functions
};
#endif