I am writing a predictor corrector numerical solver. I have written a working circular array to keep track of the previous values of my function.
#include <cmath>
// Circular array
// this is fixed sized container to hold the last n update of a function
// written by Keivan Moradi 2014
template <typename T>
class carray
{
public:
carray(int s)
{
size = exp2(ceil(log2(s)));
array = new T[size];
SizeNegOne = size-1;
head = SizeNegOne;
}
void initialize(T n)
{
for (head=0; head<size; head++)
array[head]=n;
head = SizeNegOne;
}
void update(T h)
{
// bitwise modulus:
// if "size" is in power of 2:
//(head+1) & SizeNegOne = (head+1) % size
// in contrast to modulus, this method is guaranteed to get positive values
head = (head+1) & SizeNegOne;
array[head]=h;
}
T operator[](int index)
{
// bitwise modulus:
// if "size" is in power of 2:
// (head + index) & SizeNegOne = (head + index) % size
// in contrast to modulus, this method is guaranteed to get positive values
return array[(head + index) & SizeNegOne];
}
~carray()
{
delete [] array;
}
protected:
private:
T *array;
int size, SizeNegOne, head;
};
The following code shows how this code is supposed to work:
int main()
{
carray<float> phi(3);
phi.initialize(-64);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(6.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(7.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(8.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(9.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(10.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
return 0;
}
now I want to nest this class inside a predictor class so that I can use it something like this:
int main()
{
predictor<float> phi(4);
phi.update(10);
phi.update(11);
phi.update(12);
phi.update(13);
std::cout<<phi.predict2ndOrder()<<std::endl;
}
This code shows my failing best attempt:
#include <cmath>
template <typename T>
class predictor
{
public:
predictor(int s)
{
size = s;
}
void update(T a)
{
f.update(a);
}
T predict2ndOrder()
{
return f[0] + (3/2*(f[0]-f[-1])-1/2*(f[-1]-f[-2]));
}
private:
int size;
carray<T> f(size);
class carray
{
public:
carray(int s)
{
size = exp2(ceil(log2(s)));
array = new T[size];
SizeNegOne = size-1;
head = SizeNegOne;
}
~carray()
{
delete [] array;
}
void initialize(T n)
{
for (head=0; head<size; head++)
array[head]=n;
head = SizeNegOne;
}
void update(T h)
{
head = (head+1) & SizeNegOne;
array[head]=h;
}
T operator[](int index)
{
return array[(head + index) & SizeNegOne];
}
private:
T *array;
int size, SizeNegOne, head;
};
};
Would you please let me know how to fix this? I am a new c++ programmer so take it easy on me. ;)