473,434 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Suggestions for optimizing my code

Hello,

I am looking for suggestions for how I might optimize my
code (make it execute faster), and make it more streamlined/
elegent. But before describing my code, I want to state that
I am not a computer scientist (I am an atmospheric scientist),
and have but a rudimentary understanding of OO principles.

My problem is this: I want to find the maximum off-diagonal
element of a correlation matrix, and the -position- of that
element within the matrix. In case you are unfamiliar with
correlation matrices they are square and symmetric, and contain
the mutual correlations among data collected at different
points in space or time.

I write much Python code and absolutely love the language. To
do the task outlined above I twiddled around with the "argsort"
methods available in both Numeric and numarry. In the end, I
decided to accomplish this through the following algorithm.

<----

import MLab
import Numeric as N

def FindMax( R):
"""Find row (column) where max off-diagonal element
occurs in matrx [R].
"""
# First get elements in the lower triangle of [R],
# since the diagonal elements are uninformative and
# the upper triangle contains redundant information.
Y = MLab.tril(R)

offMax = -9999.
rowMax = 0
colMax = 0

for row in range(len(Y)):
for col in range(0,row):
if Y[row][col] > offMax:
offMax = Y[row][col]
rowMax = row
colMax = col

return (rowMax, colMax, offMax)

---->

Now, this algorithm will work sufficiently fast on "small" sized
matrices, but I worry that performance will not scale well when
the dimensions of the matrix grow "large" (say 1000-1500 elements
on a side, or larger).

So onto my question. Could someone please provide me with a suggestion
for making this code more efficient and elegant? Again, I have but
a rudimentary understanding of OO principles.
Thanks very much for your help,
Daran
da**********@PAMyahoo.com

Aug 5 '05 #1
2 1060
drife wrote:
[...]
for row in range(len(Y)):
for col in range(0,row):


In this case you should use 'xrange' instead 'range'.

w.
Aug 5 '05 #2
drife wrote:
Hello,

I am looking for suggestions for how I might optimize my
code (make it execute faster), and make it more streamlined/
elegent. But before describing my code, I want to state that
I am not a computer scientist (I am an atmospheric scientist),
and have but a rudimentary understanding of OO principles.

My problem is this: I want to find the maximum off-diagonal
element of a correlation matrix, and the -position- of that
element within the matrix. In case you are unfamiliar with
correlation matrices they are square and symmetric, and contain
the mutual correlations among data collected at different
points in space or time.


import MLab
import Numeric as N

def find_max(R):
U = MLab.triu(R)
n = U.shape[0]
# set the diagonal elements to 0.0, too
U.flat[::n+1] = 0.0
k = N.argmax(U.flat)
i, j = divmod(k, n)
return i, j, R[i,j]

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 5 '05 #3

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

Similar topics

6
by: A Future Computer Scientist | last post by:
A question: Is it really important to think about optimizing the native code or optimizing it for P Code? Or does the code you write make a difference?
14
by: Ian Richardson | last post by:
I'm writing a large Javascript application (uncompressed source around 400K) which is doing almost all the initialisation it needs to in a just-in-time manner. However, I have included an option...
4
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int...
3
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any...
2
by: Brian | last post by:
In particular, this question goes out to the Microsoft C++ development team. Back in June, Ronald Laeremans posted the following message. Has the optimizing compiler been included with the...
4
by: Flashman | last post by:
A little confusing with setting up optimizing options with 2003 .NET. Under the Optimization Tab. if you set to /O1 or /O2 is the program ignoring the settings for Inline Function expansion,...
24
by: Richard G. Riley | last post by:
Without resorting to asm chunks I'm working on a few small routines which manipulate bitmasks. I'm looking for any guidance on writing C in a manner which tilts the compilers hand in, if possible,...
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
6
by: peter_k | last post by:
Hi, Last time i'm interested in optimizing small c programs. On my studies we are sending the programs using the web interface to online judge. The person who will wrote the faster program get...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.