Re: Pythonic way to iterate through multidimensional space?

2014-10-08 Thread Gelonida N
On 10/7/2014 1:01 PM, Ned Batchelder wrote: On 10/7/14 2:10 AM, Gelonida N wrote: Disadvantage of itertools.product() is, that it makes a copy in memory. Reason ist, that itertools also makes products of generators (meaning of objects, that one can't iterate several times through) There are

Re: Pythonic way to iterate through multidimensional space?

2014-10-07 Thread Gelonida N
On 8/6/2014 1:39 PM, Tim Chase wrote: On 2014-08-06 11:04, Gayathri J wrote: Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? I believe something like this was discussed a while

Re: Pythonic way to iterate through multidimensional space?

2014-10-07 Thread Ned Batchelder
On 10/7/14 2:10 AM, Gelonida N wrote: Disadvantage of itertools.product() is, that it makes a copy in memory. Reason ist, that itertools also makes products of generators (meaning of objects, that one can't iterate several times through) There are two use cases, that I occasionaly stumble

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Gayathri J
Dear Peter Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? any idea? ** *# -*- coding: utf-8 -*-* *import numpy as np*

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Chris Angelico
On Wed, Aug 6, 2014 at 3:34 PM, Gayathri J usethisid2...@gmail.com wrote: Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? any idea? Don't worry about what's faster. That almost

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Mark Lawrence
On 06/08/2014 06:34, Gayathri J wrote: Dear Peter Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? any idea? * * * * *#

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Peter Otten
Gayathri J wrote: Dear Peter Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be... I wouldn't have expected product() to be significantly faster, but neither did I expect it to be slower. or am i

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Wojciech Giel
You might check numpy it is really powerful tool for working with multi dimensional arrays: ex. a = arange(81).reshape(3,3,3,3) a array( 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19,

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Gayathri J
Dear Peter Yes the f[t] or f[:,:,:] might give a marginal increase, but then i need to do further operations using the indices, in which case this wouldnt help Dear Wojciech np.flat() works if u dont care about the indices and only the matrix/array values matter. but if the i,j,k matters,

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Peter Otten
Gayathri J wrote: Dear Peter Yes the f[t] or f[:,:,:] might give a marginal increase, The speedup compared itertools.product() is significant: $ python -m timeit -s 'from itertools import product; from numpy.random import rand; N = 100; a = rand(N, N, N); r = range(N)' 'for x in

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Tim Chase
On 2014-08-06 11:04, Gayathri J wrote: Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? I believe something like this was discussed a while ago and there was a faster-but-uglier

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Gayathri J
Dear Peter thanks . But thats what I was trying to say just taking them to zero by f[:,:,:] = 0.0 or using np.zeros is surely going to give me a time gain... but my example of using the itertools.product() and doing f[x] =0.0 is just to compare the looping timing with the traditional nested

Pythonic way to iterate through multidimensional space?

2014-08-05 Thread Frank Miles
I need to evaluate a complicated function over a multidimensional space as part of an optimization problem. This is a somewhat general problem in which the number of dimensions and the function being evaluated can vary from problem to problem. I've got a working version (with loads of

Re: Pythonic way to iterate through multidimensional space?

2014-08-05 Thread Peter Otten
Frank Miles wrote: I need to evaluate a complicated function over a multidimensional space as part of an optimization problem. This is a somewhat general problem in which the number of dimensions and the function being evaluated can vary from problem to problem. I've got a working version

Re: Pythonic way to iterate through multidimensional space?

2014-08-05 Thread Frank Miles
On Tue, 05 Aug 2014 20:06:05 +, Frank Miles wrote: I need to evaluate a complicated function over a multidimensional space as part of an optimization problem. This is a somewhat general problem in which the number of dimensions and the function being evaluated can vary from problem to