LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-04-2005, 06:20 AM   #1
DiagonalArg
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:

Code:
list1=[]
for i in range(m):
	list1.append([])
	for j in range(n):
		list1.append(f[j](u[i]))
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, 10:03 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.
Code:
def fib(n):
    """Returns the nth term of the fibonacci sequence."""
    if(n<=2)
        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, 05:01 AM   #3
DiagonalArg
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, 08:48 AM   #4
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Hi,

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

Code:
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 08:49 AM.
 
Old 08-05-2005, 10:33 AM   #5
DiagonalArg
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.
 
  


Reply


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 02:00 PM
Python Parellel Port Programming pembo13 Programming 2 10-26-2004 05:43 PM
Programming in Python? Gerardoj Programming 1 09-25-2003 04:55 PM
books on python programming wirednb Programming 3 09-05-2003 02:20 PM
Programming with python Relfx Linux - Newbie 1 08-22-2001 08:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 PM.

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