Custom data type in a matrix.

Discussion in 'Python' started by Gaz, Apr 23, 2006.

  1. Gaz

    Gaz Guest

    Hi guys. I've been lookig for this in the numpy pdf manual, in this
    group and on google, but i could not get an answer...

    Is there a way to create a custom data type (eg: Name: string(30), Age:
    int(2), married: boolean, etc) and then use that custom data in a
    matrix? Actually, this is a two question question :p

    Im doing a simple hex based game and i need a way to store every hex
    property (color, owner,x, y, etc) in a matrix's "cell", representing
    each cell a hex.

    Thank you.
    Gaz, Apr 23, 2006
    #1
    1. Advertising

  2. Gaz

    Gaz Guest

    BTW, i tried the "classe Thinge(): pass" but does not qualify as "data
    type" for a numpy array.
    Gaz, Apr 23, 2006
    #2
    1. Advertising

  3. Gaz

    Robert Kern Guest

    Gaz wrote:
    > Hi guys. I've been lookig for this in the numpy pdf manual, in this
    > group and on google, but i could not get an answer...


    You will probably want to look or ask on the numpy list, too.

    https://lists.sourceforge.net/lists/listinfo/numpy-discussion

    > Is there a way to create a custom data type (eg: Name: string(30), Age:
    > int(2), married: boolean, etc) and then use that custom data in a
    > matrix? Actually, this is a two question question :p


    Yes. Use record arrays. They are discussed in section 8.5 of the _The Guide to
    NumPy_ if you have the book. There is another example of using record arrays on
    the SciPy wiki (although it is less focused on combining different data types
    than it is named column access):

    http://www.scipy.org/RecordArrays

    Here is an example:

    In [18]: from numpy import *

    In [19]: rec.fromrecords([['Robert', 25, False], ['Thomas', 53, True]],
    names='name,age,married', formats=['S30', int, bool])
    Out[19]:
    recarray([('Robert', 25, False), ('Thomas', 53, True)],
    dtype=[('name', '|S30'), ('age', '>i4'), ('married', '|b1')])

    In [21]: Out[19].name
    Out[21]:
    chararray([Robert, Thomas],
    dtype='|S30')

    In [22]: Out[19].age
    Out[22]: array([25, 53])

    In [23]: Out[19].married
    Out[23]: array([False, True], dtype=bool)

    You can also use object arrays if you need to implement classes and not just
    dumb, basic types:

    In [33]: class Hex(dict):
    ....: def __init__(self, **kwds):
    ....: dict.__init__(self, **kwds)
    ....: self.__dict__ = self
    ....:
    ....:

    In [34]: field = array([Hex(color=(0,0,0), owner='Player1', x=10, y=20,
    etc='Black hex owned by Player1'),
    ....: Hex(color=(1,1,1), owner='Player2', x=10, y=21,
    etc='White hex owned by Player2')], dtype=object)

    In [35]:

    In [35]: field
    Out[35]: array([{'y': 20, 'etc': 'Black hex owned by Player1', 'color': (0, 0,
    0), 'owner': 'Player1', 'x': 10}, {'y': 21, 'etc': 'White hex owned by Player2',
    'color': (1, 1, 1), 'owner': 'Player2', 'x': 10}], dtype=object)

    --
    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
    Robert Kern, Apr 23, 2006
    #3
  4. Gaz schrieb:
    > Hi guys. I've been lookig for this in the numpy pdf manual, in this
    > group and on google, but i could not get an answer...
    >
    > Is there a way to create a custom data type (eg: Name: string(30), Age:
    > int(2), married: boolean, etc) and then use that custom data in a
    > matrix? Actually, this is a two question question :p
    >
    > Im doing a simple hex based game and i need a way to store every hex
    > property (color, owner,x, y, etc) in a matrix's "cell", representing
    > each cell a hex.


    You don't want numpy - you want ordinary lists in lists. Consider this:

    class Hex(object):
    def __init__(self, x, y):
    self.x, self.y = x, y
    self.terrain_type = "unknown"


    map = [[Hex(x, y) for y in xrange(height)] for x in xrange(width)]


    There is no advantage of using numpy whatsoever - your fields aren't
    subject to mathematical operations or other transformations/selections.

    Diez
    Diez B. Roggisch, Apr 23, 2006
    #4
  5. Gaz

    Gaz Guest

    And how im supposed to assign data to a specific hex?
    Gaz, Apr 23, 2006
    #5
  6. Gaz schrieb:
    > And how im supposed to assign data to a specific hex?


    How would you have done it using numarray? Accessing a specific field is
    done using slicing:

    fields[x][y].property = value

    Diez
    Diez B. Roggisch, Apr 23, 2006
    #6
  7. Diez B. Roggisch schrieb:
    > Gaz schrieb:
    >> And how im supposed to assign data to a specific hex?

    >
    > How would you have done it using numarray? Accessing a specific field is
    > done using slicing:


    The term slicing is of course wrong here - it's called array index access.

    Diez
    Diez B. Roggisch, Apr 23, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,671
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    407
    Holgerson
    Oct 26, 2007
  3. Terry Reedy
    Replies:
    0
    Views:
    554
    Terry Reedy
    Apr 2, 2009
  4. Robert Kern
    Replies:
    0
    Views:
    593
    Robert Kern
    Apr 2, 2009
  5. DebbieD

    Data Grid or Data Matrix...

    DebbieD, Oct 26, 2003, in forum: ASP General
    Replies:
    4
    Views:
    125
    Bob Barrows
    Oct 26, 2003
Loading...

Share This Page