472,365 Members | 1,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,365 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 1466


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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.