473,408 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Subclassing numarray's arrays

I'd like to subclass numarray's array. I'd like to add some methods
and override others like __init__. I don't know how to do this and
haven't found help searching the manual or the web, can someone help?

For example imagine I just want to do something as simple as making a
subclass "NewClass" with the __init__ method overridden so that the
behaviour would be:
a = NewClass(2)
print a

[[2 2]
[2 2]]

How could that be done?

Thanks in advance, miz.
Jul 18 '05 #1
3 1537


Mizrandir wrote:
I'd like to subclass numarray's array. I'd like to add some methods
and override others like __init__. I don't know how to do this and
haven't found help searching the manual or the web, can someone help?

For example imagine I just want to do something as simple as making a
subclass "NewClass" with the __init__ method overridden so that the
behaviour would be:

a = NewClass(2)
print a


[[2 2]
[2 2]]

How could that be done?

Thanks in advance, miz.

Miz,

Numarray wasn't designed with subclassing in mind, but there are
workarounds for most problems.

You might try something like:
import numarray.numarraycore as _num
class NewClass(_num.NumArray):
def __init__(self, n, a):
''' n provides the length of each dimension,
a is the constant value to be plugged.
'''
arr= _num.array(sequence= n * n * [a], shape= (n, n))
self.__setstate__(arr.__getstate__())

def __repr__(self):
" Return printable representation of instance."
className= self.__class__.__name__
className= className.zfill(5).replace('0', ' ')
arr= self.copy()
arr.__class__= _num.NumArray
rep= className + _num.NumArray.__repr__(arr)[5:]
return rep

def __str__(self):
" Return a pretty printed string of the instance."
stri= self.copy()
stri.__class__= _num.NumArray
return _num.NumArray.__str__(stri)
if __name__ == '__main__':
a= NewClass(2, 2)
print a
b= NewClass(3, 4)
print `b`

I hope that this helps.

Colin W.

Jul 18 '05 #2
First of all, thanks for helping.
Numarray wasn't designed with subclassing in mind, but there are
workarounds for most problems.
On numarray's website there is a paper by the Numarray authors saying:
"... The new approach allows arrays to be subclassed as well as ...",
so I thought it should be something easy to do.

Anyway, I hadn't read anything about numarraycore, is it explained
anywhere. For example, I don't understand what the statement
"self.__setstate__(arr.__getstate__())" does.

You might try something like:
import numarray.numarraycore as _num
class NewClass(_num.NumArray):
def __init__(self, n, a):
''' n provides the length of each dimension,
a is the constant value to be plugged.
'''
arr= _num.array(sequence= n * n * [a], shape= (n, n))
self.__setstate__(arr.__getstate__())

def __repr__(self):
" Return printable representation of instance."
className= self.__class__.__name__
className= className.zfill(5).replace('0', ' ')
arr= self.copy()
arr.__class__= _num.NumArray
rep= className + _num.NumArray.__repr__(arr)[5:]
return rep

def __str__(self):
" Return a pretty printed string of the instance."
stri= self.copy()
stri.__class__= _num.NumArray
return _num.NumArray.__str__(stri)
if __name__ == '__main__':
a= NewClass(2, 2)
print a
b= NewClass(3, 4)
print `b`


miz.
Jul 18 '05 #3


Mizrandir wrote:
First of all, thanks for helping.

Numarray wasn't designed with subclassing in mind, but there are
workarounds for most problems.

On numarray's website there is a paper by the Numarray authors saying:
"... The new approach allows arrays to be subclassed as well as ...",
so I thought it should be something easy to do.

I don't remember that, but it is not as dificult as it looks. Just try
out the code below, nothing could be simpler than the __init__ method
below.

The __str__ and __repr__ are just tools which can be used with any subclass.
Anyway, I hadn't read anything about numarraycore, is it explained
anywhere. For example, I don't understand what the statement
"self.__setstate__(arr.__getstate__())" does.
NumArray creates a number of attributes to provide information about the
shape of an array, the data type of its elements and to point to the
memory buffer containing the binary data etc. The expression above just
transfers the values from arr (which is an instance of NumArray) to
self (which is an instance of NewClass).
You might try something like:
import numarray.numarraycore as _num
class NewClass(_num.NumArray):
def __init__(self, n, a):
''' n provides the length of each dimension,
a is the constant value to be plugged.
'''
arr= _num.array(sequence= n * n * [a], shape= (n, n))
self.__setstate__(arr.__getstate__())

def __repr__(self):
" Return printable representation of instance."
className= self.__class__.__name__
className= className.zfill(5).replace('0', ' ')
arr= self.copy()
arr.__class__= _num.NumArray
rep= className + _num.NumArray.__repr__(arr)[5:]
return rep

def __str__(self):
" Return a pretty printed string of the instance."
stri= self.copy()
stri.__class__= _num.NumArray
return _num.NumArray.__str__(stri)
if __name__ == '__main__':
a= NewClass(2, 2)
print a
b= NewClass(3, 4)
print `b`

miz.

Please let me know if I can provide any further information. numarray
is a good tool, but there is a small hurdle if one wishes to subclass.

Colin W.

Jul 18 '05 #4

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

Similar topics

2
by: Tim Rowe | last post by:
Does numarray-0.5.win32-py2.2.exe work with Python 2.3? If not, is there a version that will? Tia, Tim
8
by: Russell E. Owen | last post by:
I'm writing a C extension for numarray and am puzzled about the idiom for error handling. The documentation seems to say one should always decref an array after calling NA_InputArray, etc., to...
11
by: grv | last post by:
So it is supposed to be very fast to have an array of say 5 million integers stored in a binary file and do a = numarray.fromfile('filename', (2, 2, 2)) numarray.add(a, 9, a) but how is that...
4
by: Ben Champion | last post by:
I have installed python 2.3.3 on my windows machine have have ran several programs succesfully. I have also installed numarray 1.1 for my version of python and am now trying to create arrays using...
1
by: Chris P. | last post by:
Hi. I have a very simple task to perform and I'm having a hard time doing it. Given an array called 'x' (created using the numarray library), is there a single command that rounds each of its...
5
by: George Sakkis | last post by:
What's the fastest and most elegant equivalent of zip() in Numeric/Numarray between two equally sized 1D arrays ? That is, how to combine two (N,)-shaped arrays to one (N,2) (or (2,N)) shaped ? I...
6
by: Matt Feinstein | last post by:
Is there an optimal way to apply a function to the elements of a two-d array? What I'd like to do is define some function: def plone(x): return x+1 and then apply it elementwise to a 2-D...
3
by: PL | last post by:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for example, add 1 to each element) and pass it back to Python. With these building blocks I will be able to figure out all the...
0
by: andrewfelch | last post by:
Below is the code to/from Boolean arrays and Unsigned integers. On my Pentium 4, functions such as "bitwise_and" are 32 times faster when run on 32-bit integers instead of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
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...

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.