Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-04-2005, 07:20 AM   #1
LQ Newbie
Registered: Mar 2004
Posts: 23

Rep: Reputation: 15
Programming maths in Python

I'm trying to write some Python code to perform some computational group theory tasks, the problem is that I'm really a mathematician rather than a programmer so I was wondering if any of you real programmers could help me out.

I have n functions, f_1, ..., f_n, and m words, u_1, ..., u_m. I want to perform each function on each word, which is easy enough:

for i in range(m):
	for j in range(n):
The problem is that now I want to perform f_j on each result in the 2D list1, and then again on these results, and so on an arbitrary number of times until I observe some property in the results and break out of the loops.

So each time I need to add another dimension to my list and hence, as far as I can see (which isn't, admittedly, very far), I'll need another for loop.

What's the best way of doing this without having to hand code a huge number of nested for loops?

Thanks in advance for any help!
Old 08-04-2005, 11:03 AM   #2
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
I'm not 100% sure what you need to do. However, as I understand it, you're performing some transformation on a n-dimensional matrix, and then that transformation on the resulting n+1-dimensional matrix?

If this is the case, placing each loop in a function would be ideal. You could use a recursive function to handle this. Recursive functions are similar to the fibonacci sequence.
def fib(n):
    """Returns the nth term of the fibonacci sequence."""
        return 1
    return fib(n-1) + fib(n-2)
Since I'm not sure exactly what your goal is, I hope this explanation makes things a little clearer. I'm really a programmer, rather than a mathematician (though CS majors do take quite a bit of math).
Old 08-05-2005, 06:01 AM   #3
LQ Newbie
Registered: Mar 2004
Posts: 23

Original Poster
Rep: Reputation: 15
Yeah, this is definitely the right sort of idea. I'm not sure how to implement it to do exactly what I want, so I'll probably be back pleading for more help in no time!
Old 08-05-2005, 09:48 AM   #4
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31

Maybe it'll be easier if you use dictionaries instead of lists.

n = ...
m = ...
... definition of your functions...

values = {} #will contain the matrices
values[0] = {} # The first matrix
for i in range(n):
    values[0][i] = {}
    for j in range(m):
        values[0][i][j] = initial_value??

# function to iterate
def calculate_next_values(values):
    l = len(values)
    values[l] = {}
    for i in range(n):
        values[l][i] = {}
        for j in range(m):  
            values[l][i][j] = f[i](values[l-1][i][j])
    return values

I haven't checked that this runs, but you probably got the idea.

Another thing: if you are planning to do A LOT of iterations, maybe Python is not the better choice...

Last edited by enemorales; 08-05-2005 at 09:49 AM.
Old 08-05-2005, 11:33 AM   #5
LQ Newbie
Registered: Mar 2004
Posts: 23

Original Poster
Rep: Reputation: 15
I guess in terms of readablility dictionaries would be better, I think I'm just lazyily choosing to use lists because I find them more intuitive.

I take your point about Python not being the right language, especially with these nested for loops meaning that the number of calculations increases exponentially, but the functions that are being performed are actually coded in C++ (not by me I might add, as I'm hopeless at C++!) and I'm expecting to not have more than about 5 layers of nesting in most cases, so I hope Python will be just about efficient enough to cope.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
help in python programming... dirty_bastard Programming 1 08-14-2005 03:00 PM
Python Parellel Port Programming pembo13 Programming 2 10-26-2004 06:43 PM
Programming in Python? Gerardoj Programming 1 09-25-2003 05:55 PM
books on python programming wirednb Programming 3 09-05-2003 03:20 PM
Programming with python Relfx Linux - Newbie 1 08-22-2001 09:16 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration