[Numpy-discussion] Having difficulty with Matrix.py

Blair Hall b.hall at irl.cri.nz
Thu Jun 5 16:23:07 EDT 2003

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

More information about the NumPy-Discussion mailing list