I am new to this, I have a convolution program that takes in a file of data, convolves it and outputs another file. I am attaching the code here.
void convolute()
{
ifstream fin;
ofstream fout;
int count = 0;
double a=0,b=0;
string input_file_string = "maxclus_500000node_3M_5000ens_666.dat";
string output_file_string = "1convolute_"+input_file_string;
fin.open(input_file_string.c_str());
while(fin) //to know the size of array to initialize
{
fin>>a>>b;
count++;
}
fin.close();
double* c = NULL;
c = new double[count+1];
double* d = NULL;
d = new double[count+1];
for(int i=0;i<count+1;i++)
{
c[i] = 0;
d[i] = 0;
}
fin.open(input_file_string.c_str());
int n = 1;
while(fin) //takes in data
{
fin>>a>>b;
c[n] = a;
d[n] = b;
n++;
}
fin.close();
double* binom = NULL;
binom = new double[count];
double* summ = NULL;
summ = new double[count+1];
for(int i=0;i<count+1;i++) summ[i] = 0;
for(int i=0;i<count;i++) binom[i] = 0;
for(int j=1;j<count;++j) //main convolution of data takes place
{
int x,y;
double prob = j*1.0/(count-1);
binom[j] = 1;
for(int i=j+1;i<count;++i)
binom[i] = binom[i-1]*((count-1)-i+1)*1.0/i*prob/(1-prob);
for(int i=j-1;i>=0;--i)
binom[i] = binom[i+1]*(i+1)*1.0/((count-1)-i)*(1-prob)/prob;
double sum = 0;
for(int i=0;i<count;++i) sum += binom[i];
for(int i=0;i<count;++i) binom[i] /= sum;
sum = 0;
for(int i=1;i<count;++i) sum += d[i]*binom[i];
summ[j] = sum;
//fout<<c[j]<<'\t'<<sum<<endl;
if(j%1000==0)
cout<<count-1<<'\t'<<j<<endl;
}
cout<<"writing to file "<<endl;
fout.open(output_file_string.c_str());
for(int i=1;i<count;i++) fout<<c[i]<<'\t'<<summ[i]<<endl;
fout.close();
delete [] c;
c = NULL;
delete [] d;
d = NULL;
delete [] binom;
binom = NULL;
delete [] summ;
summ = NULL;
}
I want to know, what i can do to speed up the part where main convolution takes place, my data files are quite large, and it takes a great amount of time for it to finish. Need help.