Daniel T. a écrit :
> In article < .com>,
> wrote:
>
> > thanks mlimber for the input
> >
> > I tried to explain that xi, xf, yi, yf, zi, and zf do change for each
> > slot of Gi.
> >
> > Lots of the things you said were relavant. I bunched up the code when I
> > was writing it to better see the loops but I do space my code normally.
> >
> > Anyways, I figured out a way to do this. Thanks for your time.
>
> Please, share it with us...
Actually, I'm right back at square one now. Even though the runtime has
greatly improved, it's still too long. The variable 'size' is set to 20
in the code but ideally it should be set to 50.
This is the code I came up with:
#include <iostream.h>
#include <math.h>
#include <algorithm>
#include <time.h>
int main() {
const int size = 20;
const double L = 2;
double distance, theRandom, Gtotal, temp, dif;
int i_index, m_index, x, y, z;
time_t start,end;
void sortValues(int&, int&, int&);
double *Gi;
double *Gf;
double *theMatrix;
Gi = new double[size*size*size];
Gf = new double[size*size*size];
theMatrix = new double[size*size*size];
for(x = 0; x < size*size*size; x++) {
theMatrix[ x ] = 0;
theRandom = (1 + rand()% 6 );
if(theRandom <= 2)
Gi[ x ] = 0;
else
Gi[ x ] = x * theRandom;
}
time (&start);
//Algorithm starts here
for(int l = 0; l < size; l++) {
for(int m = 0; m < size; m++) {
for(int n = 0; n < size; n++) {
Gtotal = 0;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
for(int k = 0; k < size; k++) {
i_index = i + j * size + k * size * size;
if(Gi[ i_index ] != 0 ||
(i != l && j != m && k != n)) {
x = abs(i-l);
y = abs(j-m);
z = abs(k-n);
sortValues(x, y, z);
m_index = x + y * size + z * size * size;
if(theMatrix[ m_index ] != 0) {
temp = theMatrix[ m_index ];
} else {
distance = sqrt(pow(x,2) + pow(y,2) + pow(z,2));
theMatrix[ m_index ] =
(1/distance) * exp(-distance/L);
}
Gtotal += (theMatrix[ m_index ] * Gi[ i_index ]);
}
}}}
if(Gtotal != 0)
Gf[ l + (m * size) + (n * size * size) ] += Gtotal;
}}}
//Algorithm ends here
time (&end);
dif = difftime (end,start);
cout << "Delta t : " << dif << endl;
return 0;
}
void sortValues(int &x, int &y, int &z) {
int n;
if(x >= y) {
n = x;
x = y;
y = n;
}
if(x >= z) {
n = x;
x = z;
z = n;
}
if(y >= z) {
n = y;
y = z;
z = n;
}
}
-----------------------------------------------------------
Anyways, I haven't found a better way of doing this. I was suggested to
consider using a Sparse Matrix but I have no idea how to make a 3d one.
All I'm trying to do is reduce the amount of time it takes to compute
this.