Hacker News new | ask | show | jobs
by maple3142 2086 days ago
Matrix, not array.

    >>> import numpy as np
    >>> np.mat(((1,0),(0,1)))**2
    matrix([[1, 0],
            [0, 1]])
1 comments

That's a particularly confusing example to choose, because matrix squaring produces the same result as array (element-wise) squaring:

  >>> np.array([[1, 0], [0, 1]])**2
  array([[1, 0],
         [0, 1]])
Here:

  >>> np.array([[1, 2], [3, 4]])**2
  array([[ 1,  4],
         [ 9, 16]])
  >>> np.mat([[1, 2], [3, 4]])**2
  matrix([[ 7, 10],
          [15, 22]])
The real problem with the

  np.array("asdgh")**2
example is squaring doesn't make sense for character data.

Matrix vs. array squaring has other issues too, as matrix squaring only works with square matrices:

  >>> np.mat([1, 2])**2
  [...]
  LinAlgError: Last 2 dimensions of the array must be square
  >>> np.array([1, 2])**2
  array([1, 4])