[PYTHON MATRIX-SIG] Something broken with __mul__?

janko hauser jhauser@ifm.uni-kiel.de
Fri, 18 Oct 1996 13:47:41 +0200


I try to build a class similar to UserArray, and have some
problems. Here is my attempt.

#! /home/caesar/u1/hauser/bin/python 
#
#from multiarray import array, zeros, arraytype
#from fast_umath import *
import ni
from Numeric.Core import *
from Numeric.UserArray  import UserArray
from fast_umath import *
import string

    
def _as_array(a):
    if hasattr(a, "array"):
	return a.array
    else:
	return a


class a_mesh:
    def __init__(self, data, dx, dz, typecode=None):
	# More testing later
	if shape(data) != shape(dx):
	    raise ValueError, 'Inconsistent dimensions'
	if typecode == None:
	    self.array = array(data)
	    self.gx= array(dx)
	    self.gz= array(dz)
	else:
	    self.array = array(data, typecode)
	    self.gx= array(dx, typecode)
	    self.gz= array(dz, typecode)

	self.shape = self.array.shape
	self.name = string.split(str(self.__class__))[1]

    def __repr__(self):
	return str(self.array)[:] 

    # Array as sequence
    def __len__(self): return len(self.array)
    def __getitem__(self, index): 
	return self._return_constructor(self.array[index],self.gx[index],self.gz[index])
    def __getslice__(self, i, j): 
	return self._return_constructor(self.array[i:j],self.gx[i:j],self.gz[i:j])

    def __setitem__(self, index, value): self.array[index] = _as_array(value)
    def __setslice__(self, i, j, value): self.array[i:j] = _as_array(value)

    def __abs__(self): return self._return_constructor(abs(self.array),self.gx,self.gz)
    def __neg__(self): return self._return_constructor(-self.array,self.gx,self.gz)
    def __mul__(self, other): 
	return self._return_constructor(_as_array(self)*_as_array(other),self.gx,self.gz)
    __rmul__ = __mul__

    def __less__(self,other): 
	print 'test'
 	tmp=_as_array(self).less(_as_array(other))
 	print tmp
 	return self._return_constructor(tmp,self.gx,self.gz)


a_mesh._return_constructor=a_mesh

#def less(dat,other):
#    if hasattr(dat,"array"):
#	return a_mesh(less(dat.array,other),dat.gx,dat.gz)
#    else:
#	return less(dat,other)


#############################################
# Test
#############################################

if __name__ == '__main__':

    data=array([[[1,2,3],[4,5,6],[7,8,9]],
	       [[1,2,3],[4,5,6],[7,8,9]],
	       [[1,2,3],[4,5,6],[7,8,9]]])
    dx=data
    dz=data
    data=sqrt(data)

    a=a_mesh(data,dx,dz,Float32)
    b=a[:,:,0:2]
    print b.shape, type(b)
    # print less(b,2.)  #this give an error, too
    c=UserArray([1,2,3,4,5])
    print c,c*4
    print b*3.
    print b*3

### Output ######
(3, 3, 2) <type 'instance'>
1 2 3 4 5  4  8 12 16 20
 3.          4.24264061
 6.          6.70820403
 7.93725371  8.48528123

 3.          4.24264061
 6.          6.70820403
 7.93725371  8.48528123

 3.          4.24264061
 6.          6.70820403
 7.93725371  8.48528123
Traceback (innermost last):
  File "<stdin>", line 91, in ?
  File "<stdin>", line 52, in __mul__
TypeError: cannot perform this operation on these types

If I change * to multiply in the definition, this is ok. But with the
definition of 'less' I have no success. And if less is really a
function and not a method, do I need a definition at all?

Thanks to Konrad Hinsen, to convince me, that I don't make a stupid
mistake :-).

__Janko


=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================