Robert,
Transforming your matrix to a list before computation isn't very  
efficient. If you do need some extra parameters in your __init__ to be  
compatible with other functions such as asmatrix, well, just add them,  
or use a coverall **kwargs
def __init__(self, instruments, **kwargs)
No guarantee it'll work all the time.

Otherwise, please have a look at:
http://docs.scipy.org/doc/numpy/user/basics.subclassing.html
and the other link at the top of that page. In your case, I'd try to  
put the initialization in the __array_finalize__.



On Dec 10, 2008, at 7:15 AM, <[EMAIL PROTECTED]> <[EMAIL PROTECTED] 
 > wrote:

> Hello,
>
> I'm using numpy-1.1.1 for Python 2.3.  I'm trying to create a class  
> that acts just like the numpy.matrix class with my own added methods  
> and attributes.  I want to pass my class a list of custom  
> "instrument" objects and do some math based on these objects to set  
> the matrix.  To this end I've done the following:
>
> from numpy import matrix
>
> class rcMatrix(matrix):
>    def __init__(self,instruments):
>        """Do some calculations and set the values of the matrix."""
>        self[0,0] = 100 # Just an example
>        self[0,1] = 100 # The real init method
>        self[1,0] = 200 # Does some math based on the input objects
>        self[1,1] = 300 #
>    def __new__(cls,instruments):
>        """When creating a new instance begin by creating an NxN  
> matrix of
>        zeroes."""
>        len_ = len(instruments)
>        return matrix.__new__(cls,[[0.0]*len_]*len_)
>
> It works great and I can, for example, multiply two of my custom  
> matrices seamlessly.  I can also get the transpose.  However, when I  
> try to get the inverse I get an error:
>
>> rcm = rcMatrix(['instrument1','instrument2'])
>> print rcm
> [[ 100.  100.]
> [ 200.  300.]]
>> print rcm.T
> [[ 100.  200.]
> [ 100.  300.]]
>> print [5,10] * rcm
> [[ 2500.  3500.]]
>> print rcm.I
> Traceback (most recent call last):
>  File "[Standard]/deleteme", line 29, in ?
>  File "C:\Python23\Lib\site-packages\numpy\core\defmatrix.py", line  
> 492, in getI
>    return asmatrix(func(self))
>  File "C:\Python23\Lib\site-packages\numpy\core\defmatrix.py", line  
> 52, in asmatrix
>    return matrix(data, dtype=dtype, copy=False)
> TypeError: __init__() got an unexpected keyword argument 'dtype'
>
>
>
> I've had to overwrite the getI function in order for things to work  
> out:
>
>    def getI(self): return matrix(self.tolist()).I
>    I = property(getI, None, doc="inverse")
>
> Is this the correct way to achieve my goals?
>
> Please let me know if anything is unclear.
>
> Thanks,
>
> Robert Conde
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to