Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Programming maths in Python
 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

 08-04-2005, 07:20 AM #1 DiagonalArg LQ Newbie   Registered: Mar 2004 Posts: 23 Rep: 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!
 08-04-2005, 11:03 AM #2 Matir Moderator   Registered: Nov 2004 Location: San Jose, CA Distribution: Ubuntu Posts: 8,507 Rep: 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).
 08-05-2005, 06:01 AM #3 DiagonalArg LQ Newbie   Registered: Mar 2004 Posts: 23 Original Poster Rep: 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!
 08-05-2005, 09:48 AM #4 enemorales Member   Registered: Jul 2004 Location: Santiago, Chile Distribution: Ubuntu Posts: 410 Rep: 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 09:49 AM.
 08-05-2005, 11:33 AM #5 DiagonalArg LQ Newbie   Registered: Mar 2004 Posts: 23 Original Poster Rep: 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.