[PYTHON MATRIX-SIG] Re: max/min bug

janko hauser jhauser@ifm.uni-kiel.de
Tue, 28 Jan 1997 10:13:34 +0100


I think the MLab-functions are only a first try. I think some of them
could be more generally. And I have problems with the typecodes. If I
compute the mean of an integer array, the result is an integer, but
this is not always the mean. On the other hand, should functions
change the typecode?

I have made more general min and max functions. I thinks this concept
can be applied to many of the functions in MLab.py. I see one problem
with the definition of axis, because at the moment 

axis=index_of_axis_in_shape_tuple

Is it wrong to think of 

axis=number_of_dimension - 1 ??

Anyway, here are my attempts: 
(If there are mistakes, please shout loud!)

def max(m,axis=0):
      if axis == None:
   return maximum.reduce(ravel(m))
      else:
   return maximum.reduce(m,axis)

def min(m,axis=0):
      if axis == None:
   return minimum.reduce(ravel(m))
      else:
   return minimum.reduce(m,axis)

def mean(m,axis=0):
      if axis == None:
   return add.reduce(ravel(m))/(multiply.reduce(m.shape)*1.)
      else:
   return add.reduce(m,axis)/(m.shape[axis]*1.)

def diff(m,axis=0):
   l_sl=[slice(None,None,None)]*len(m.shape)
   u_sl=l_sl[:]
   l_sl[axis]=slice(1,None,1)
   u_sl[axis]=slice(None,-1,1)

   return m[l_sl]-m[u_sl]

def ndiff(m,n=1,axis=0):
   l_sl=[slice(None,None,None)]*len(m.shape)
   u_sl=l_sl[:]
   l_sl[axis]=slice(1,None,1)
   u_sl[axis]=slice(None,-1,1)
    
     if n >= 1:
   return ndiff(m[l_sl]-m[u_sl],n-1,axis)
     else:
   return m[l_sl]-m[u_sl]

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

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