Re: [Numpy-discussion] Overloading sqrt(5.5)*myvector
On Dec 26, 2007 3:49 AM, Gary Ruben [EMAIL PROTECTED] wrote: Sorry this isn't an answer, just noise, but for those here who don't know, Bruce is the chief maintainer of the vpython project. I have found vpython aka the visual module to be a highly attractive and useful module for teaching physics. It would be great if someone with Boost experience would try to help him out. I wouldn't want him to get falsely disillusioned with this list as I for one have been looking forward to a fully numpy-compatible version of vpython. I think the problem is that few of us are that familiar with boost/python. I have used it myself, but only for interfacing C++ classes or accessing Numpy arrays through their buffer interface. I always avoided the Numeric machinery because it looked clumsy and inefficient to me, I didn't want an embedded version of Numeric (Numarray, Numpy), I wanted speed. Anyway, I think numpy.float64 hides a normal C double, so the slowdown probably comes from the boost/python machinery. I don't think boost/python was ever updated to use Numpy in particular and the Numpy data type may be throwing it through an interpretive loop. The speed of double vs float arithmetic on Intel hardware is pretty much a wash, so should not show much difference otherwise. You can get a float32 result from sqrt In [2]: type(sqrt(float32(5.5))) Out[2]: type 'numpy.float32' But I suspect it will make little difference, probably what is needed is the corresponding C/Python data type. I think that information is available somewhere, but am not sure of the details. Someone else can probably help you there (Travis?) Chuck snip ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
Le Mercredi 26 Décembre 2007 21:22, Mathew Yeates a écrit : Hi I've been looking at fromfunction and itertools but I'm flummoxed. I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] It's obvious when the number of lists is not arbitrary. But what if thats not known until runtime? Mathew In the 'Reference Manual' of Python there is an example. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion -- René Bastian http://www.musiques-rb.org http://pythoneon.musiques-rb.org http://divergences.be édition du 15.11.07 : musique ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
On Dec 26, 2007 12:22 PM, Mathew Yeates [EMAIL PROTECTED] wrote: I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] It's obvious when the number of lists is not arbitrary. But what if thats not known until runtime? Would this work? Make a function that takes two inputs (a list of lists and a list) and returns a list of lists that contains all possible combinations. Iterate through all lists by calling the function with the output of the previous call (a list of lists) and the next list. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
Hi -- I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] Try this: In [25]: [(x, y) for x in r1 for y in r2] Out[25]: [('dog', 1), ('dog', 2), ('cat', 1), ('cat', 2)] Cheers, Stuart Brorson Interactive Supercomputing, inc. 135 Beaver Street | Waltham | MA | 02452 | USA http://www.interactivesupercomputing.com/ ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
Which reference manual? René Bastian wrote: Le Mercredi 26 Décembre 2007 21:22, Mathew Yeates a écrit : Hi I've been looking at fromfunction and itertools but I'm flummoxed. I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] It's obvious when the number of lists is not arbitrary. But what if thats not known until runtime? Mathew In the 'Reference Manual' of Python there is an example. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
yes, I came up with this and may use it. Seems like it would be insanely slow but my problem is small enough that it might be okay. Thanks Keith Goodman wrote: On Dec 26, 2007 12:22 PM, Mathew Yeates [EMAIL PROTECTED] wrote: I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] It's obvious when the number of lists is not arbitrary. But what if thats not known until runtime? Would this work? Make a function that takes two inputs (a list of lists and a list) and returns a list of lists that contains all possible combinations. Iterate through all lists by calling the function with the output of the previous call (a list of lists) and the next list. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
On Dec 26, 2007 2:30 PM, Charles R Harris [EMAIL PROTECTED] wrote: On Dec 26, 2007 1:45 PM, Keith Goodman [EMAIL PROTECTED] wrote: On Dec 26, 2007 12:22 PM, Mathew Yeates [EMAIL PROTECTED] wrote: I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] It's obvious when the number of lists is not arbitrary. But what if thats not known until runtime? Would this work? Make a function that takes two inputs (a list of lists and a list) and returns a list of lists that contains all possible combinations. Iterate through all lists by calling the function with the output of the previous call (a list of lists) and the next list. Yeah, you can do it with recursion, but I don't think it would be quite as efficient. An example of the explicit approach, define the following generator: def count(listoflists) : counter = [i[0] for i in listoflists] Make that counter = [0 for i in listoflists]. That bug slipped in going from [0]*len(listoflists). Chuck ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
On Dec 26, 2007 1:45 PM, Keith Goodman [EMAIL PROTECTED] wrote: On Dec 26, 2007 12:22 PM, Mathew Yeates [EMAIL PROTECTED] wrote: I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] It's obvious when the number of lists is not arbitrary. But what if thats not known until runtime? Would this work? Make a function that takes two inputs (a list of lists and a list) and returns a list of lists that contains all possible combinations. Iterate through all lists by calling the function with the output of the previous call (a list of lists) and the next list. Yeah, you can do it with recursion, but I don't think it would be quite as efficient. An example of the explicit approach, define the following generator: def count(listoflists) : counter = [i[0] for i in listoflists] maxdigit = [len(i) - 1 for i in listoflists] yield counter while True : i = 0; while i len(counter) and counter[i] == maxdigit[i] : counter[i] = 0 i += 1 if i len(counter) : counter[i] += 1 yield counter else : return In [30]: a = ['dog', 'cat', 'horse'] In [31]: b = ['red', 'black'] In [32]: c = [a,b] In [33]: for i in count.count(c) : print [c[j][i[j]] for j in range(len(c))] : ['dog', 'red'] ['cat', 'red'] ['horse', 'red'] ['dog', 'black'] ['cat', 'black'] ['horse', 'black'] ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
Thanks Chuck. Charles R Harris wrote: On Dec 26, 2007 2:30 PM, Charles R Harris [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: On Dec 26, 2007 1:45 PM, Keith Goodman [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: On Dec 26, 2007 12:22 PM, Mathew Yeates [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: I have an arbitrary number of lists. I want to form all possible combinations from all lists. So if r1=[dog,cat] r2=[1,2] I want to return [[dog,1],[dog,2],[cat,1],[cat,2]] It's obvious when the number of lists is not arbitrary. But what if thats not known until runtime? Would this work? Make a function that takes two inputs (a list of lists and a list) and returns a list of lists that contains all possible combinations. Iterate through all lists by calling the function with the output of the previous call (a list of lists) and the next list. Yeah, you can do it with recursion, but I don't think it would be quite as efficient. An example of the explicit approach, define the following generator: def count(listoflists) : counter = [i[0] for i in listoflists] Make that counter = [0 for i in listoflists]. That bug slipped in going from [0]*len(listoflists). Chuck ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
Here's a baroque way to do it using generated code: def cg_combinations(seqs): n = len(seqs) chunks = [def f(%s): % ', '.join('s%s' % i for i in range(n))] for i in reversed(range(n)): chunks.append( * (n -i) + for x%s in s%s: % (i, i)) chunks.append( * n + yield ( + ', '.join('x%s' % i for i in range(n)) + ')') code = '\n'.join(chunks) exec code return f(*seqs) It should be reasonably fast, if non-obvious. I've included a version with some timing and testing against Chucks version below. Enjoy. (Also, it can be simplified slightly, but I wanted to generate in the same order as Chuck for comparison purposes). == def count(seqs) : counter = [0 for i in seqs] maxdigit = [len(i) - 1 for i in seqs] yield counter while True : i = 0; while i len(counter) and counter[i] = maxdigit[i] : counter[i] = 0 i += 1 if i len(counter) : counter[i] += 1 yield counter else : return def count_combinations(seqs): for c in count(seqs): yield tuple(s[i] for (s, i) in zip(seqs, c)) def cg_combinations(seqs): n = len(seqs) chunks = [def f(%s): % ', '.join('s%s' % i for i in range(n))] for i in reversed(range(n)): chunks.append( * (n -i) + for x%s in s%s: % (i, i)) chunks.append( * n + yield ( + ', '.join('x%s' % i for i in range(n)) + ')') code = '\n'.join(chunks) exec code return f(*seqs) a = abcde*10 b = range(99) c = [x**2 for x in range(33)] seqs = [a, b, c] if __name__ == __main__: assert list(count_combinations(seqs)) == list(cg_combinations(seqs)) import timeit test = timeit.Timer('list(count_combinations(seqs))', 'from __main__ import count_combinations, seqs') t1 = test.timeit(number=10) test = timeit.Timer('list(cg_combinations(seqs))', 'from __main__ import cg_combinations, seqs') t2 = test.timeit(number=10) print t1, t2 ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] how do I list all combinations
Hi all, I use numpy's own ndindex() for tasks like these: In: numpy.ndindex? Type: type Base Class: type 'type' String Form:class 'numpy.lib.index_tricks.ndindex' Namespace: Interactive File: /Library/Frameworks/Python.framework/Versions/2.4/ lib/python2.4/site-packages/numpy/lib/index_tricks.py Docstring: Pass in a sequence of integers corresponding to the number of dimensions in the counter. This iterator will then return an N-dimensional counter. Example: for index in ndindex(3,2,1): ... print index (0, 0, 0) (0, 1, 0) (1, 0, 0) (1, 1, 0) (2, 0, 0) (2, 1, 0) Here's a combination function using numpy.ndindex: def combinations(*lists): lens = [len(l) for l in lists] for indices in numpy.ndindex(*lens): yield [l[i] for l, i in zip(lists, indices)] r1=[dog,cat] r2=[1,2] list(combinations(r1, r2)) Out: [['dog', 1], ['dog', 2], ['cat', 1], ['cat', 2]] Or you could use 'map' and 'operator.getitem', which might be faster(?): def combinations(*lists): lens = [len(l) for l in lists] for indices in numpy.ndindex(*lens): yield map(operator.getitem, lists, indices) In the python cookbook, there are numerous similar recipes for generating permutations, combinations, and the like. Zach Pincus Program in Biomedical Informatics and Department of Biochemistry Stanford University School of Medicine On Dec 26, 2007, at 5:48 PM, Timothy Hochberg wrote: Here's a baroque way to do it using generated code: def cg_combinations(seqs): n = len(seqs) chunks = [def f(%s): % ', '.join('s%s' % i for i in range (n))] for i in reversed(range(n)): chunks.append( * (n -i) + for x%s in s%s: % (i, i)) chunks.append( * n + yield ( + ', '.join('x%s' % i for i in range(n)) + ')') code = '\n'.join(chunks) exec code return f(*seqs) It should be reasonably fast, if non-obvious. I've included a version with some timing and testing against Chucks version below. Enjoy. (Also, it can be simplified slightly, but I wanted to generate in the same order as Chuck for comparison purposes). == def count(seqs) : counter = [0 for i in seqs] maxdigit = [len(i) - 1 for i in seqs] yield counter while True : i = 0; while i len(counter) and counter[i] = maxdigit[i] : counter[i] = 0 i += 1 if i len(counter) : counter[i] += 1 yield counter else : return def count_combinations(seqs): for c in count(seqs): yield tuple(s[i] for (s, i) in zip(seqs, c)) def cg_combinations(seqs): n = len(seqs) chunks = [def f(%s): % ', '.join('s%s' % i for i in range (n))] for i in reversed(range(n)): chunks.append( * (n -i) + for x%s in s%s: % (i, i)) chunks.append( * n + yield ( + ', '.join('x%s' % i for i in range(n)) + ')') code = '\n'.join(chunks) exec code return f(*seqs) a = abcde*10 b = range(99) c = [x**2 for x in range(33)] seqs = [a, b, c] if __name__ == __main__: assert list(count_combinations(seqs)) == list (cg_combinations(seqs)) import timeit test = timeit.Timer('list(count_combinations(seqs))', 'from __main__ import count_combinations, seqs') t1 = test.timeit(number=10) test = timeit.Timer('list(cg_combinations(seqs))', 'from __main__ import cg_combinations, seqs') t2 = test.timeit(number=10) print t1, t2 ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Overloading sqrt(5.5)*myvector
Gary Ruben wrote: Sorry this isn't an answer, just noise, but for those here who don't know, Bruce is the chief maintainer of the vpython project. I have found vpython aka the visual module to be a highly attractive and useful module for teaching physics. It would be great if someone with Boost experience would try to help him out. I wouldn't want him to get falsely disillusioned with this list as I for one have been looking forward to a fully numpy-compatible version of vpython. Please keep in mind that for many of us, this is the holiday season and we are on vacation. While I'm happy to check the list and give answers that are at the front of my head, deeper answers that require exploration or experimentation are beyond my available time. I'm sure others are in a similar situation. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion