Is the result incorrect with Numpy in Python 3?

2

I was doing matrix exercises with numpy, but apparently it generated an error in the final answer:

As we can see the final result of Z*Z is:

[[9,4,1],
[1,0,1],
[1,4,9],]

But after a suspicion I compared the results by hand with Mathcad and the result is:

[[10,4,-2],[4,4,4],[-2,4,10]]

the code to check it is:

from numpy import array
X=array([[-1,0,1],[-1,0,1],[-1,0,1]])
Y=array([[-2,-2,-2],[0,0,0],[2,2,2]])
Z=X+Y
Z=Z*Z
print(Z)

Numpy generated an error or to calculate the product of matrices is another command?

    
asked by Robby 13.07.2016 в 06:13
source

1 answer

5

What you are doing is not a product of matrices. To make product matrices you have to use numpy.dot or dot method of a numpy array . In addition, since the release of version 3.5 of Python you can use the new operator @ to make product matrices.

import numpy as np

X = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
Y = np.array([[-2,-2,-2],[0,0,0],[2,2,2]])
Z = X + Y
Z = np.dot(Z, Z)
print(Z)

# alternativas
# Z = Z.dot(Z)
# Z = Z @ Z # a partir de Python 3.5

And the result of print is:

[[10  4 -2]
 [ 4  4  4]
 [-2  4 10]]
    
answered by 13.07.2016 / 08:28
source