I wrote a piece of python code. It’s a generalised summation function, it sums up arbitrary functions (simple ones alteast!), for an arbitrary number of summations. Inspired by Nikhil’s lisp code, and a “Can I do this in Python attitude!”
Here it is:
def list_inc(list_begin,list_end,list_cur): if list_cur ==[ ]: return list_cur if list_cur[-1] < list_end[-1]: list_cur[-1] = list_cur[-1] + 1 return list_cur list_cur= list_inc(list_begin[:-1],list_end[:-1],list_cur[:-1]) list_cur.append(list_begin[-1]) return list_cur def summation(function,list_begin,list_end): sum = 0 list_cur = list_begin while (list_cur != list_end): sum = sum + apply (function,list_cur) list_cur = list_inc (list_begin,list_end,list_cur) sum = sum + apply (function,list_end) return sum #Arbitrary function, ranges print summation(lambda x,y,z:x*y*z,[1,1,1],[2,3,5])
Btw, I think I might have to read up on how to format the code properly while using wordpress. I did use the < "code"> thing, but it's not very impressive. A < "pre"> tag might be good.

Post a Comment