From: Todd M. <jm...@st...> - 2004-03-15 15:52:41
|
Hi Faheem, In order to create an object array, do this: >>> import numarary.objects as obj >>> a = obj.fromlist(["t","u","w",1]) >>> a ObjectArray(["t","u","w",1) Unfortunately, the ability to apply arbitrary functions to all the elements of an object array has never been implemented. Nevertheless, the following might do: def apply(array, function, result=None): rank1 = (array.rank == 1) if result is None: result0 = array.copy() else: result0 = result for x in range(array.shape[0]): if rank1: result0[x] = function(array[x]) else: apply(array[x], function, result0[x]) return result0 Re-ordering the loop and the if statement results in faster but slightly more verbose code. Regards, Todd Miller ----- Original Message ----- From: "Faheem Mitha" <fa...@em...> To: <num...@li...> Sent: Sunday, March 14, 2004 12:20 PM Subject: [Numpy-discussion] Re: numarray.objects missing in documentation > On Sun, 14 Mar 2004 09:55:52 -0500, Todd Miller <jm...@st...> wrote: > > On Sun, 2004-03-14 at 00:26, Faheem Mitha wrote: > >> Dear People, > >> > >> I'm posting this to gmane.comp.python.numeric.general. I am not sure > >> what mailing list this corresponds to. I earlier tried to post this to > >> the mailing list mentioned in the numarray documentation > >> (nu...@li...), but it bounced. > >> > >> I don't see numarray.objects listed in > >> http://stsdas.stsci.edu/numarray/numarray-0.9.html/genindex.html > > > > The section on object arrays has yet to be written. What is it that you > > want to know? > > Thanks for the quick reply. I'm trying to make an array of python > objects as follows. Specifically, given a user defined class, I want > to creata an array of instances of that class. > > Consider the following class. > > class cell: > def setrow(self,row): > self.row = row > def setcol(self,col): > self.col = col > ... > > I create an "empty" cell. > > empcell = cell() > > Now I want to create an array of objects identical to empcell, and > later initialize them. > > I tried stuff along the lines of > > ****************************************************************** > import string > from numarray import * > > empcell = cell() > > base = [empcell] > > mat = fromlist(base,shape=(2,2)) > ****************************************************************** > > But the > > mat = fromlist(base,shape=(2,2)) > > line fails with > > AttributeError: cell instance has no attribute '__len__' > > Similar things fail with the same error message. No doubt I'm doing > something stupid, but would someone please enlighten me? > > I got the fromlist command from > > >>> help("numarray.objects") > > I was a bit disconcerted to find on trying to run the first example > from the help page that I got > > >>> a = fromlist(["t","u","w"]) > ... > TypeError: Expecting a python numeric type, got something else. > > What am I doing wrong? > > I'm running > > ii python 2.3.3-5 An interactive > high-level object-oriented language (default versio > ii python-numarray 0.8.1-1 An array > processing package modelled after Python-Numeric > ii python-numeric 23.1-1 Numerical > (matrix-oriented) Mathematics for Python > > on Debian sarge. > > Thanks in advance and sorry for the long-winded message. > > Faheem. > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: IBM Linux Tutorials > Free Linux tutorial presented by Daniel Robbins, President and CEO of > GenToo technologies. Learn everything from fundamentals to system > administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |