I have been caught out writing a simple function that manipulates the elements of a matrix.
I have distilled the problem into the code below.
I find that 'sum' works with a Numeric array but fails with a Matrix. Am I doing something wrong? If not, what should I do to patch it?
(I am working under Windows with Numpy 23.0)
Here is the code:
# 'a' is a two dimensional array. The # function simply places the sum of # the first column in a[0,0] def sum(a): N = a.shape[0] sum = 0 for i in range(N): sum += a[i,0]
# This is the criticial line a[0,0] = sum
return a[0,0]
#------------------------ if(__name__ == '__main__'):
import Numeric import Matrix
a = Numeric.ones( (9,1) ) print sum( a ) # Ok print sum( Matrix.Matrix( a ) ) # Fails
This is a bug in Matrix.py which has been fixed.
Here is the relevant section of code to replace the current
__setitem__ command in the Matrix class with:
def __setitem__(self, index, value): value = asarray(value, self._typecode) self.array[index] = squeeze(value)
Good luck,
Travis O.
I find that 'sum' works with a Numeric array but fails with a Matrix. Am I doing something wrong? If not, what should I do to patch it?