473,763 Members | 5,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom data type in a matrix.

Gaz
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.

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

Apr 23 '06 #2
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/...mpy-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__(s elf, **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
ro*********@gma il.com

"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

Apr 23 '06 #3
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_ty pe = "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
Apr 23 '06 #4
Gaz
And how im supposed to assign data to a specific hex?

Apr 23 '06 #5
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
Apr 23 '06 #6
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
Apr 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2029
by: Blake T. Garretson | last post by:
I'm having some issues with decimal.Decimal objects playing nice with custom data types. I have my own matrix and rational classes which implement __add__ and __radd__. They know what to do with Decimal objects and react appropriately. The problem is that they only work with Decimals if the custom type is on the left (and therefore __add__ gets called), but NOT if the Decimal is on the left. The Decimal immediately throws the usual...
3
2496
by: Tim Wesson | last post by:
Does anyone know if it is possible to choose between the below templates (or similar ones) according to the promotion rules for types T and U? Thanks, Tim Wesson.
1
1969
by: Phil Price | last post by:
Hi there, I'm developing a shape recognition application for the tablet PC for a) fun b) university project. Currently I'm working on the learning stage using neural networks, and have to store a load of learning data (a 25 by 25 matrix) each shape group has a number of user drawn shapes, then the application will create variations of these shapes (by moving nodes and drawing lines into the matrix between nodes, after normalization). So...
3
1523
by: Daniel L Elliott | last post by:
Hello, I want to have a class which can contain vectors of many different types (int, double, Complex, etc). Is it possible to have a generic vector inside a non-template class? Thank you, Dan Elliott
9
1955
by: Ben R. | last post by:
Hi guys, I've got a DB table of timecards with these fields in the table: ID (Int) UserID (Int) DateWorked (DateTime) HoursWorkedOnThatDate (Double) I'd like to display a grid, with Monday - Sunday across the top (in columns)
2
1738
by: Marco Biagioni | last post by:
After i've tried to update a vb 6.0 project to vb.net, using visual studio utility,i can't read correctly data bytes from a .bmp file to insert them in a matrix to operate on. Using vb 6.0 the code was based on Get function: GET #1, PIXELSTART, PHOTO.MATRIX where PHOTO is a structure data type, with a member MATRIX previously defined in this way:
2
2803
by: Marco Biagioni | last post by:
After i've tried to update a vb 6.0 project to vb.net, using visual studio utility,i can't read correctly data bytes from a .bmp file to insert them in a matrix to operate on. Using vb 6.0 the code was based on Get function: GET #1, PIXELSTART, PHOTO.MATRIX where PHOTO is a structure data type, with a member MATRIX previously defined in this way:
3
1438
by: mosi | last post by:
Python matrices are usually defined with numpy scipy array or similar. e.g. I would like to have easier way of defining matrices, for example: Any ideas how could this be done? The ";" sign is reserved, the "" is used for lists.
5
2802
by: =?Utf-8?B?TWFuanJlZSBHYXJn?= | last post by:
Hi, I developed a custom Matrix class derived from custom VC++ doubles Array class derived from a RealArray which is defined as: typedef CArray<double,doubleRealArray; Now I need to convert this custom Matrix class to standard doubles Matrix (VC++) as I am passing it to some function that understands only the standard class.
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.