[Numpy-discussion] Changing a matrix element into a scalar

Alan G Isaac alan.isaac@gmail....
Tue Aug 3 13:44:07 CDT 2010


On 8/3/2010 1:29 PM, Joshua Holbrook wrote:
> What's the
> advantage of the matrix datatype?


As it turns out, that's a controversial question.  ;-)

One answer: pedagogy (for those used to matrices).

A related answer: succinctness and readability of *some* code.

         >>> a,b,c = np.array([[1,2,3]]), np.array([[4],[5],[6]]), np.arange(9).reshape((3,3))
         >>> d = np.dot(b,np.dot(a,c))   # matrix multiplication
         >>> d2 = b.dot(a.dot(c))        # NumPy 1.5+
         >>> d5 = d.dot(d).dot(d).dot(d).dot(d)  #matrix exponentiation
         >>> d[0,0] = 0
         >>> di = np.linalg.inv(d)       #matix inverse
         >>> A,B,C = np.mat('1 2 3'), np.mat('4;5;6'), np.mat(np.arange(9).reshape((3,3)))
         >>> D = B * (A * C)             #matrix multiplication
         >>> D5 = D**5                   #matrix exponentiation
         >>> D[0,0] = 0
         >>> DI = D.I                    #matix inverse

fwiw,
Alan Isaac



More information about the NumPy-Discussion mailing list