Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '16,': 0.03; 'indices': 0.07; '22,': 0.09; '34,': 0.09; '40,': 0.09; 'method,': 0.09; 'cheers': 0.12; '2],': 0.16; '37,': 0.16; '46,': 0.16; '49,': 0.16; '52,': 0.16; '55,': 0.16; '58,': 0.16; '61,': 0.16; '64,': 0.16; '67,': 0.16; '70,': 0.16; 'matters,': 0.16; 'numpy': 0.16; 'subject:skip:m 10': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'aug': 0.22; 'email addr:gmail.com>': 0.22; '>>>': 0.24; '31,': 0.24; 'simpler': 0.24; "haven't": 0.24; "i've": 0.25; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; '13,': 0.31; '25,': 0.31; 'dimensions': 0.31; 'loads': 0.31; 'url:python': 0.33; "i'd": 0.34; 'problem': 0.35; 'problem.': 0.35; 'tool': 0.35; 'something': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'url:listinfo': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'being': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'space': 0.40; 'url:mail': 0.40; 'further': 0.61; 'dear': 0.65; 'dont': 0.67; 'yes': 0.68; 'frank': 0.68; 'evaluate': 0.72; 'clearer': 0.84; 'dimensional': 0.84; 'subject:space': 0.84; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=uijkDkQSeqHQOUMdzPLKgItkzWyv8ryJoznrNQAwoPA=; b=d2bfk4/uFOaJ8XQrF2V2syYyDCdFouQDqf4MqRT1Ee/cgy4Vm2NiRvVAid7pdGRnv2 4wWxVp9CiZxbFRr+sYSzLATyRW56OWuE8sklz1T+Tvn+SQm+X4jtVBdPxujFcYRNyM/U hCJTZnzr9POfLBiNxeQFZgeSnYmsZZ4NOy/ntFzj8PhnGNj2VCeyvdI9J4pLed/aMd39 PUGt4MJheD4+ir7xM37IS8w2vsXfGWli8yivee4g6M1rDm/lmZDTIUJcRtb2NWo76wla lgOS2mA1gZcVJBSgj3xEaTYoKosTw6BThQr/S8OeafEKKTnFgFDNqO31rxgtfkooK/P9 XX8w== MIME-Version: 1.0 X-Received: by 10.194.9.228 with SMTP id d4mr14764100wjb.99.1407327223325; Wed, 06 Aug 2014 05:13:43 -0700 (PDT) In-Reply-To: <53E1E180.6070308@gmail.com> References: <53E1E180.6070308@gmail.com> Date: Wed, 6 Aug 2014 17:43:43 +0530 Subject: Re: Pythonic way to iterate through multidimensional space? From: Gayathri J To: python-list@python.org Content-Type: multipart/alternative; boundary=047d7b4508a83aa8d604fff4e7e0 X-Mailman-Approved-At: Wed, 06 Aug 2014 14:14:10 +0200 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 209 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407327251 news.xs4all.nl 2874 [2001:888:2000:d::a6]:36402 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75794 --047d7b4508a83aa8d604fff4e7e0 Content-Type: text/plain; charset=UTF-8 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 matters, flatten wouldnt work On Wed, Aug 6, 2014 at 1:34 PM, Wojciech Giel wrote: > 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, 20], > [21, 22, 23], > [24, 25, 26]]], > > > [[[27, 28, 29], > [30, 31, 32], > [33, 34, 35]], > > [[36, 37, 38], > [39, 40, 41], > [42, 43, 44]], > > [[45, 46, 47], > [48, 49, 50], > [51, 52, 53]]], > > > [[[54, 55, 56], > [57, 58, 59], > [60, 61, 62]], > > [[63, 64, 65], > [66, 67, 68], > [69, 70, 71]], > > [[72, 73, 74], > [75, 76, 77], > [78, 79, 80]]]]) > > >>> f = a.flat > >>> for i in f: > ... print(i) > 0 > 1 > 2 > .. > 98 > 99 > > cheers > Wojciech > > > > > On 05/08/14 21:06, 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 (with loads of conditionals, and it only works >> to #dimensions <= 10), but I'd like something simpler and clearer and >> less hard-coded. >> >> I've web-searched for some plausible method, but haven't found anything >> "nice". Any recommendations where I should look, or what technique should >> be used? >> >> TIA! >> > > -- > https://mail.python.org/mailman/listinfo/python-list > --047d7b4508a83aa8d604fff4e7e0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Dear Peter

Yes the f[t] or f[:,:,= :] might give =C2=A0a marginal increase, but then i need to do further oper= ations =C2=A0using 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 mat= ter.
but if the <i,j,k> matters, flatten wouldnt work=



On Wed, Aug 6, 2014 at 1:34 PM, Wojciech Giel <wojt= ekgiel@gmail.com> wrote:
You might check numpy it is really powerful = tool for working with multi dimensional arrays:

ex.
>>> a =3D arange(81).reshape(3,3,3,3)
>>> a

array([[[[ 0, =C2=A01, =C2=A02],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[ 3, =C2=A04, =C2=A05],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[ 6, =C2=A07, =C2=A08]],

=C2=A0 =C2=A0 =C2=A0 =C2=A0 [[ 9, 10, 11],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[12, 13, 14],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[15, 16, 17]],

=C2=A0 =C2=A0 =C2=A0 =C2=A0 [[18, 19, 20],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[21, 22, 23],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[24, 25, 26]]],


=C2=A0 =C2=A0 =C2=A0 =C2=A0[[[27, 28, 29],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[30, 31, 32],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[33, 34, 35]],

=C2=A0 =C2=A0 =C2=A0 =C2=A0 [[36, 37, 38],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[39, 40, 41],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[42, 43, 44]],

=C2=A0 =C2=A0 =C2=A0 =C2=A0 [[45, 46, 47],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[48, 49, 50],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[51, 52, 53]]],


=C2=A0 =C2=A0 =C2=A0 =C2=A0[[[54, 55, 56],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[57, 58, 59],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[60, 61, 62]],

=C2=A0 =C2=A0 =C2=A0 =C2=A0 [[63, 64, 65],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[66, 67, 68],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[69, 70, 71]],

=C2=A0 =C2=A0 =C2=A0 =C2=A0 [[72, 73, 74],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[75, 76, 77],
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[78, 79, 80]]]])

>>> f =3D a.flat
>>> for i in f:
... =C2=A0 =C2=A0print(i)
0
1
2
..
98
99

cheers
Wojciech




On 05/08/14 21:06, Frank Miles wrote:
I need to evaluate a complicated function over a multidimensional space
as part of an optimization problem. =C2=A0This is a somewhat general proble= m
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 conditionals, and it only wor= ks
to #dimensions <=3D 10), but I'd like something simpler and clearer = and
less hard-coded.

I've web-searched for some plausible method, but haven't found anyt= hing
"nice". =C2=A0Any recommendations where I should look, or what te= chnique should
be used?

TIA!

--
https://mail.python.org/mailman/listinfo/python-list

--047d7b4508a83aa8d604fff4e7e0--