473,471 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Improve an algorithme involving 6 loops

This is the basic concept of my code:

#include <iostream.h>
#include <math.h>
#include <stdlib.h>

int main() {
double Gtotal, L, size, distance, theRandom;
double Gi[ 50 ][ 50 ][ 50 ];
int xi, xf, yi, yf, zi, zf;

Gtotal = 0; size = 50; L = 2;

// here every slot in the matrix has the same
coordinates, to keep things simple
// this code wasn't used to test this particular
situation
xi=2; xf=1; yi=2; yf=1; zi=2; zf=1;

for(int x = 0; x < size; x++) {
for(int y = 0; y < size; y++) {
for(int z = 0; z < size; z++) {
theRandom = (1 + rand()% 6 );
if(theRandom <= 2) Gi[ x ][ y ][ z ] = 0;
else Gi[ x ][ y ][ z ] = x - y + z;
}}}

for(int l = 0; l < size; l++) {
for(int m = 0; m < size; m++) {
for(int n = 0; n < size; n++) {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
for(int k = 0; k < size; k++) {
if(Gi[ i ][ j ][ k ] != 0 || (i != l && j != m && k != n)) {
distance = sqrt((xi - xf)^2 + (yi - yf)^2 + (zi - zf)^2);
Gtotal = Gtotal + (((1/distance) * exp(-distance/L)) * Gi[ i ][ j ][
k ]);
}
}}}

// normally I would have another array
here instead of the same one
// I just wanted to keep things simple
Gi[ l ][ m ][ n ] = Gi[ l ][ m ][ n ] + Gtotal;
cout << Gi[ l ][ m ][ n ] << endl;
}}}

return 0;
}

Basic idea, I have a 3d array 50x50x50 that has some data in it.
I'm trying to make a new 3d array based on the first one where each
slot in the new array is generated from all 50x50x50 slots from the
first 3d array.

Now I know that
(1/distance) * exp(-distance/L)
will be redundant because the distance between two points in the array
will be redundant.

Try to imagine the 3d arrays being data in a cartesian graph where each
slot has cartesian coordinates.

Hopefully this is clear as to what I'm trying to do. I want to improve
the the runtime.

May 8 '06 #1
7 1574
dp******@gmail.com wrote:
This is the basic concept of my code: [snip code that seems like it's intended to explain something, but that
does not]

There's a good chance you are off topic anyway. We mostly do
language issues here. It's possible to tease us into "how to" things
if they are interesting, or if you pose them as "how do I do this in
C++?"
Basic idea, I have a 3d array 50x50x50 that has some data in it.
I'm trying to make a new 3d array based on the first one where each
slot in the new array is generated from all 50x50x50 slots from the
first 3d array.
Ok, that was not very clear at all. And you should consider the wisdom
of asking, in effect, "How do I do what this code does only better?"

What is the formula that is supposed to determine the destination
array from the origin array? What if it's not 50x50x50?
Now I know that
(1/distance) * exp(-distance/L)
will be redundant because the distance between two points in the array
will be redundant.

Try to imagine the 3d arrays being data in a cartesian graph where each
slot has cartesian coordinates.

Hopefully this is clear as to what I'm trying to do. I want to improve
the the runtime.


Nope. Not clear at all. Sorry.

If you could maybe explain in a way that does not involve "how can I
do what these six loops do?" In other words, don't use source code
as spec.
Socks

May 8 '06 #2
dp******@gmail.com wrote:
This is the basic concept of my code:

#include <iostream.h>
#include <math.h>
#include <stdlib.h>
The first header is deprecated. User <iostream> (and "using std
namespace;"?). The latter two should usually be replaced with <cmath>
and <cstdlib>.
int main() {
double Gtotal, L, size, distance, theRandom;
double Gi[ 50 ][ 50 ][ 50 ];
int xi, xf, yi, yf, zi, zf;
Don't declare variables until you can initialize them, and then make
those that don't need to change const and create the variables in as
small of a scope as possible. You might also consider using a container
instead of an array (see
http://www.parashift.com/c++-faq-lit....html#faq-34.1 and
http://www.parashift.com/c++-faq-lit...html#faq-13.10
and
http://www.parashift.com/c++-faq-lit...tml#faq-13.11).

Gtotal = 0; size = 50; L = 2;
Avoid putting more than one executable statement per line, and
initialize variables when you declare them.

// here every slot in the matrix has the same
coordinates, to keep things simple
// this code wasn't used to test this particular
situation
xi=2; xf=1; yi=2; yf=1; zi=2; zf=1;

for(int x = 0; x < size; x++) {
for(int y = 0; y < size; y++) {
for(int z = 0; z < size; z++) {
theRandom = (1 + rand()% 6 );
if(theRandom <= 2) Gi[ x ][ y ][ z ] = 0;
else Gi[ x ][ y ][ z ] = x - y + z;
}}}

for(int l = 0; l < size; l++) {
for(int m = 0; m < size; m++) {
for(int n = 0; n < size; n++) {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
for(int k = 0; k < size; k++) {
if(Gi[ i ][ j ][ k ] != 0 || (i != l && j != m && k != n)) {
distance = sqrt((xi - xf)^2 + (yi - yf)^2 + (zi - zf)^2);
First, I don't think you meant to use the xor operator (^) here. You
probably meant to use std::pow(). Second, since xi, xf, yi, yf, zi, and
zf don't change, calculate this outside the loop and make it a const.
Gtotal = Gtotal + (((1/distance) * exp(-distance/L)) * Gi[ i ][ j ][
k ]);
This is more concise and efficient:

Gtotal += distanceConst * Gi[i][j][k];

where the constant distanceConst is computed outside the loop.
}
}}}

// normally I would have another array
here instead of the same one
// I just wanted to keep things simple
Gi[ l ][ m ][ n ] = Gi[ l ][ m ][ n ] + Gtotal;
cout << Gi[ l ][ m ][ n ] << endl;
Don't use std::endl, which inserts a cr-lf and then flushes the buffer.
Instead, use an ordinary '\n'.
}}}

return 0;
}

Basic idea, I have a 3d array 50x50x50 that has some data in it.
I'm trying to make a new 3d array based on the first one where each
slot in the new array is generated from all 50x50x50 slots from the
first 3d array.

Now I know that
(1/distance) * exp(-distance/L)
will be redundant because the distance between two points in the array
will be redundant.

Try to imagine the 3d arrays being data in a cartesian graph where each
slot has cartesian coordinates.

Hopefully this is clear as to what I'm trying to do. I want to improve
the the runtime.


Cheers! --M

May 8 '06 #3
In article <11**********************@u72g2000cwu.googlegroups .com>,
dp******@gmail.com wrote:
Basic idea, I have a 3d array 50x50x50 that has some data in it.
I'm trying to make a new 3d array based on the first one where each
slot in the new array is generated from all 50x50x50 slots from the
first 3d array.
You have a 125000 element array and you want to update the all the
values. That's going to take some time no matter how you slice it O(n).

The best way to speed it up would be lazy initialization. Only calculate
the value of a slot when some other part of the code actually needs it.
The whole program will probably not be any faster, but it will probably
feel faster to the user.

Now I know that
(1/distance) * exp(-distance/L)
will be redundant because the distance between two points in the array
will be redundant.

Try to imagine the 3d arrays being data in a cartesian graph where each
slot has cartesian coordinates.

Hopefully this is clear as to what I'm trying to do. I want to improve
the the runtime.


It is clear what your are doing, not what you are trying to do. :-)
May 8 '06 #4
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.

May 8 '06 #5
In article <11**********************@i39g2000cwa.googlegroups .com>,
dp******@gmail.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...
May 8 '06 #6

Daniel T. a écrit :
In article <11**********************@i39g2000cwa.googlegroups .com>,
dp******@gmail.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.

May 10 '06 #7
In article <11**********************@j33g2000cwa.googlegroups .com>,
dp******@gmail.com wrote:
Daniel T. a écrit :
In article <11**********************@i39g2000cwa.googlegroups .com>,
dp******@gmail.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.

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.


A sparse matrix is simple:

class tuple {
tuple( int a, int b, int c ):x(a),y(b),z(c) { }
int x;
int y;
int z;
};

bool operator<( tuple a, tuple b ) {
return a.x < b.x ||
a.x == b.x && a.y < b.y ||
a.x == b.x && a.y == b.y && a.z < b.z;
}

typedef map< tuple, double > SparceMatrix;

Access it as follows:

SparceMatrix m;

m[ tuple( 5, 2, 6 ) ] = 234.8673;
May 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
23
by: philipl | last post by:
hi, I have some code here which basically look for within a string, the occurance of any 3 consectative characters which are the same. so AAA bbb etc would be reported by this function. I later...
4
by: comp.lang.tcl | last post by:
I wrote this PHP function in the hopes that it would properly use a TCL proc I wrote about 4 years ago: if (!function_exists('proper_case')) { /** * Ths function will convert a string into a...
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
11
by: Paul H | last post by:
Suppose I have a table called tblPeople and I want a field to illustrate whether each person prefers cats or dogs. I could do it one of three ways. 1. A plain text field Create a text field in...
75
by: At_sea_with_C | last post by:
Hello all, I have written an ascending sort routine for floats. This seems to do the job, but for elements over 10,000, it gets awfully slow. A lot of useless comparisions with previously sorted...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.