From: Rick M. <rm...@sa...> - 2006-05-30 16:29:58
|
Is it possible to create arrays that run from, say -5:5, rather than 0:11? Analogous to the Fortran "allocate(A(-5:5))" command? I'm translating a F90 code to Python, and it would be easier to do this than to use a python dictionary. Thanks in advance. Rick Muller rm...@sa... |
From: Alan I. <ai...@am...> - 2006-05-30 17:25:23
|
On Tue, 30 May 2006, Rick Muller wrote: > Is it possible to create arrays that run from, say -5:5, rather than > 0:11? Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as N >>> x = N.arange(-5,6) >>> x array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]) >>> y=N.arange(11) >>> y-5 array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]) hth, Alan Isaac |
From: Rob H. <he...@ta...> - 2006-05-30 17:32:57
|
I believe Rick is talking about negative indices (possible in FORTRAN), in which case the answer is no. -Rob On May 30, 2006, at 11:28 AM, Rick Muller wrote: > Is it possible to create arrays that run from, say -5:5, rather > than 0:11? Analogous to the Fortran "allocate(A(-5:5))" command? > I'm translating a F90 code to Python, and it would be easier to do > this than to use a python dictionary. ----- Rob Hetland, Assistant Professor Dept of Oceanography, Texas A&M University p: 979-458-0096, f: 979-845-6331 e: he...@ta..., w: http://pong.tamu.edu |
From: Alan I. <ai...@am...> - 2006-05-30 17:42:20
|
> On May 30, 2006, at 11:28 AM, Rick Muller wrote: >> Is it possible to create arrays that run from, say -5:5, rather >> than 0:11? Analogous to the Fortran "allocate(A(-5:5))" command? >> I'm translating a F90 code to Python, and it would be easier to do >> this than to use a python dictionary. On Tue, 30 May 2006, Rob Hetland wrote: > I believe Rick is talking about negative indices (possible in > FORTRAN), in which case the answer is no. I see. Perhaps this is still relevant? (Or perhaps not.) >>> y=N.arange(11) >>> x=range(-5,6) >>> y[x] array([ 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5]) >>> hth, Alan Isaac |
From: Rob H. <he...@ta...> - 2006-05-30 17:54:05
|
Yes, brilliant. The answer is yes, but you need to modify the array to have it make sense; you need to fold the array over, so that the 'negative' indices reference data from the rear of the array.... I was thinking about having the first negative index first... I'm still not sure if this will be 'skating on dull blades with sharp knives' in converting fortran to numpy. More generally to the problem of code conversion, I think that a direct fortran -> numpy translation is not the best thing -- the numpy code should be vectorized. The array indexing problems will (mostly) go away when the fortran code is vectorized, and will result in much faster python code in the end as well. -r On May 30, 2006, at 12:42 PM, Alan Isaac wrote: >> On May 30, 2006, at 11:28 AM, Rick Muller wrote: >>> Is it possible to create arrays that run from, say -5:5, rather >>> than 0:11? Analogous to the Fortran "allocate(A(-5:5))" command? >>> I'm translating a F90 code to Python, and it would be easier to do >>> this than to use a python dictionary. > > > On Tue, 30 May 2006, Rob Hetland wrote: >> I believe Rick is talking about negative indices (possible in >> FORTRAN), in which case the answer is no. > > > > I see. > Perhaps this is still relevant? > (Or perhaps not.) > >>>> y=N.arange(11) >>>> x=range(-5,6) >>>> y[x] > array([ 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5]) >>>> > > hth, > Alan Isaac > > > > > > > ------------------------------------------------------- > All the advantages of Linux Managed Hosting--Without the Cost and > Risk! > Fully trained technicians. The highest number of Red Hat > certifications in > the hosting industry. Fanatical Support. Click to learn more > http://sel.as-us.falkag.net/sel? > cmd=lnk&kid=107521&bid=248729&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion ----- Rob Hetland, Assistant Professor Dept of Oceanography, Texas A&M University p: 979-458-0096, f: 979-845-6331 e: he...@ta..., w: http://pong.tamu.edu |
From: Rick M. <rm...@sa...> - 2006-05-30 18:36:39
|
Indeed I am. Thanks for the reply On May 30, 2006, at 11:32 AM, Rob Hetland wrote: > > I believe Rick is talking about negative indices (possible in > FORTRAN), in which case the answer is no. > > -Rob > > On May 30, 2006, at 11:28 AM, Rick Muller wrote: > >> Is it possible to create arrays that run from, say -5:5, rather >> than 0:11? Analogous to the Fortran "allocate(A(-5:5))" command? >> I'm translating a F90 code to Python, and it would be easier to do >> this than to use a python dictionary. > > ----- > Rob Hetland, Assistant Professor > Dept of Oceanography, Texas A&M University > p: 979-458-0096, f: 979-845-6331 > e: he...@ta..., w: http://pong.tamu.edu > > Rick Muller rm...@sa... |
From: David H. <dav...@gm...> - 2006-05-30 20:04:52
|
Just a thought: would it be possible to overload the array __getitem__ method ? I can do it with lists, but not with arrays... For instance, class fortarray(list): def __getitem__(self, index): return list.__getitem__(self, index+5) and >>> l = fortarray() >>> l.append(1) >>> l[-5] 1 There is certainly a more elegant way to define the class with the starting index as an argument, but I didn't look into it. For arrays, this doesn't work out of the box, but I'd surprised if there was no way to tweak it to do the same. Good luck David 2006/5/30, Rick Muller <rm...@sa...>: > > Indeed I am. Thanks for the reply > On May 30, 2006, at 11:32 AM, Rob Hetland wrote: > > > > > I believe Rick is talking about negative indices (possible in > > FORTRAN), in which case the answer is no. > > > > -Rob > > > > On May 30, 2006, at 11:28 AM, Rick Muller wrote: > > > >> Is it possible to create arrays that run from, say -5:5, rather > >> than 0:11? Analogous to the Fortran "allocate(A(-5:5))" command? > >> I'm translating a F90 code to Python, and it would be easier to do > >> this than to use a python dictionary. > > > > ----- > > Rob Hetland, Assistant Professor > > Dept of Oceanography, Texas A&M University > > p: 979-458-0096, f: 979-845-6331 > > e: he...@ta..., w: http://pong.tamu.edu > > > > > > Rick Muller > rm...@sa... > > > > > > > ------------------------------------------------------- > All the advantages of Linux Managed Hosting--Without the Cost and Risk! > Fully trained technicians. The highest number of Red Hat certifications in > the hosting industry. Fanatical Support. Click to learn more > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |
From: Robert K. <rob...@gm...> - 2006-05-30 20:17:39
|
David Huard wrote: > Just a thought: > would it be possible to overload the array __getitem__ method ? > > I can do it with lists, but not with arrays... > > For instance, > > class fortarray(list): > def __getitem__(self, index): > return list.__getitem__(self, index+5) > > and >>>> l = fortarray() >>>> l.append(1) >>>> l[-5] > 1 > > There is certainly a more elegant way to define the class with the > starting index as an argument, but I didn't look into it. For arrays, > this doesn't work out of the box, but I'd surprised if there was no way > to tweak it to do the same. One certainly could write a subclass of array that handles arbitrarily-based indices. On the other hand, writing a correct and consistent implementation would be very tricky. On the gripping hand, a quick hack might suffice if one only needed to use it locally, like inside a single function, and convert to and from real arrays at the boundaries. -- 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 |
From: Rick M. <rm...@sa...> - 2006-05-30 20:32:40
|
I certainly think that something along these lines would be possible. However, in the end I just decided to keep track of the indices using a Python dictionary, which means to access A[-3] I actually have to call A[index[-3]]. A little clunkier, but I was worried that the other solutions would be brittle in the long run. Thanks for all of the comments. On May 30, 2006, at 2:04 PM, David Huard wrote: > Just a thought: > would it be possible to overload the array __getitem__ method ? > > I can do it with lists, but not with arrays... > > For instance, > > class fortarray(list): > def __getitem__(self, index): > return list.__getitem__(self, index+5) > > and > >>> l = fortarray() > >>> l.append(1) > >>> l[-5] > 1 > > There is certainly a more elegant way to define the class with the > starting index as an argument, but I didn't look into it. For > arrays, this doesn't work out of the box, but I'd surprised if > there was no way to tweak it to do the same. > > Good luck > David > > 2006/5/30, Rick Muller <rm...@sa...>: > Indeed I am. Thanks for the reply > On May 30, 2006, at 11:32 AM, Rob Hetland wrote: > > > > > I believe Rick is talking about negative indices (possible in > > FORTRAN), in which case the answer is no. > > > > -Rob > > > > On May 30, 2006, at 11:28 AM, Rick Muller wrote: > > > >> Is it possible to create arrays that run from, say -5:5, rather > >> than 0:11? Analogous to the Fortran "allocate(A(-5:5))" command? > >> I'm translating a F90 code to Python, and it would be easier to do > >> this than to use a python dictionary. > > > > ----- > > Rob Hetland, Assistant Professor > > Dept of Oceanography, Texas A&M University > > p: 979-458-0096, f: 979-845-6331 > > e: he...@ta..., w: http://pong.tamu.edu > > > > > > Rick Muller > rm...@sa... > > > > > > > ------------------------------------------------------- > All the advantages of Linux Managed Hosting--Without the Cost and > Risk! > Fully trained technicians. The highest number of Red Hat > certifications in > the hosting industry. Fanatical Support. Click to learn more > http://sel.as-us.falkag.net/sel? > cmd=lnk&kid=107521&bid=248729&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > Rick Muller rm...@sa... |