0

I have this piece of code:

import numpy as np

A=np.matrix('3 340 30; 5 2 23; 02 0 1')
(m,m)=A.shape;
P=np.eye(m);
a=np.copy(A)
print(A)
print(a)
print(P*a) 
print(P*A)

When I print "A" and "a" they both show the same matrix on the screen. However, the result of the multiplication with matrix P are totally different.

print(P*A) shows [[   3.  340.   30.]
              [   5.    2.   23.]
              [   2.    0.    1.]]


print(P*a) shows  [[ 3.  0.  0.]
              [ 0.  2.  0.]
              [ 0.  0.  1.]]

Can someone explain what is going on?

Thanks.

Eman
  • 165
  • 1
  • 1
  • 8
  • The types of `a` and `A` are different, the former is an `ndarray` and the latter is a matrix, thus the `*` operator a has different meaning. If you want them to be the same type you can use `deepcopy` instead. – José Sánchez Dec 17 '16 at 22:35
  • 1
    @JoséSánchez Thanks for the reply, I thought it would automatically match the types. – Eman Dec 17 '16 at 22:39
  • Check `A.copy()`. The method may preserve the matrix subclass better. `np.matrix` is an old feature that's mainly of value to wayward MATLAB users. – hpaulj Dec 17 '16 at 22:42
  • Also relevant - http://stackoverflow.com/questions/33791386 – Divakar Dec 17 '16 at 22:48

0 Answers0