|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
2-Dimensional array math
Hi,
I'm working on an script that adds the values of one 2D array to the values of another 2D array, at a specific location. It's essentially basic matrix math. The main problem I'm having is code efficiency, since the way I do it now takes eons, and for whatever reason, sometimes crashes mid-way... To illustrate, say the base matrix is: Code:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 And the overlay matrix is: Code:
1 2 1 2 3 2 1 2 1 And I need to add the second matrix to the first one at X3/Y2, the outcome would be: Code:
0 0 0 0 0 0 0 1 2 1 0 0 2 3 2 0 0 1 2 1 Usually, the base array is around 500x500, and the overlayed arrays (there can be several different arrays implemented on the same base one) range from 10x10 to 150x150. One cycle can overlay 1000's of arrays at a time, so you can imagine that speed and stability are a problem. Here's how I do it now (I entered some pseudo checks instead of the real ones, which can be a bit long): Code:
for ($cyclerY=0;$cyclerY<=$Base_Array_Height;$cyclerY++){
for ($cyclerX=0;$cyclerX<=$Base_Array_Width;$cyclerX++){
$Value_Hold = &Checking_Something_About_This_Location($cyclerX, $cyclerY);
if($Value_Hold == $Some_Check){
for ($cyclerYB=0;$cyclerYB<=$Overlay_Array_Height;$cyclerYB++){
for ($cyclerXB=0;$cyclerXB<=$Overlay_Array_Width;$cyclerXB++){
my $PowerP = $Value_Hold/$Some_Multiplier;
my $pinpointX = $cyclerX+$cyclerXB;
my $pinpointY = $cyclerY+$cyclerYB;
if($pinpointX < 0){$pinpointX = 0;}
if($pinpointY < 0){$pinpointY = 0;}
$Base_Array[$pinpointY][$pinpointX] += $PowerP*$Overlay_Array[$cyclerYB][$cyclerXB];
}
}
}
}
}
There are a lot of other operations in the script, but this specific part takes the longest to process. Mainly, my 2 questions are: A) Could anyone suggest a faster/better way of going about this? B) Should I just take that part and compile it in C? (I really would rather not, because I have to make frequent changes, but I understand that it's much faster at executing long loops of basic math) Thanks in advance. |
|
#2
|
||||
|
||||
|
hi
suppose base matrix is 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 and over lay is 2 2 2 2 2 2 2 2 2 so if it is added at x1,y1 ( assuming indices starting from 0) u want simple addition which should gave result like x >>> $array[0] 1 1 1 1 $array[1]1 3 3 3 $array[2]1 3 3 3 $array[3]1 3 3 3 i am just asking to make that u want this only, btw in ur code u have used way too many for loops which are not needed , i think. if yes then u can ask for position from which to overlay, $position_x = 1; $position_y = 1; #assuming u want to overlay from these indices of base array. for($over_index_x=0;$over_index_x<$over_length_x;$over_index_x++) { for($over_index_y=0;$over_index_y<$over_length_y;$over_index_y++) { $base_array[$position_x+$over_index_x][$position_y + $over_index_y] += $over_array[$over_index_x][$over_index_y]; } } here i have not put up the extra code. just the thing u want to do. i mean the very basic overlapping. u have also used some multiplier i have included that, but once u get the code, u can make the changes as u like. i have not tested this, but i hope u get the idea. also i would say that dont traverse the base array first, i mean in the outer most for loop , traverse over lap array, coz that would be small. in ur code u r traversing whole base array unnecessarily. hope this helps, jd
__________________
_____________________________ d.k.jariwala (JD) ~ simple thought, simple act ~ I blog @ http://jdk.phpkid.org |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > 2-Dimensional array math |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|