![](https://secure.gravatar.com/avatar/b99d9997fdf5e315e20d79752f9910de.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/49df8cd4b1b6056c727778925f86147a.jpg?s=120&d=mm&r=g)
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.
![](https://secure.gravatar.com/avatar/49df8cd4b1b6056c727778925f86147a.jpg?s=120&d=mm&r=g)
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.
participants (2)
-
Blair Hall
-
Travis Oliphant