Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 8th, 2001, 06:37 PM
Bystander Bystander is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 0 Bystander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old August 9th, 2001, 09:08 AM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > 2-Dimensional array math


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT