-1

I don't want any for loop and was wondering if there is a function I can use.

Jobs
  • 3,087
  • 6
  • 22
  • 49

3 Answers3

6

If A was the numpy array, I would just type in A*A.

K. Tom
  • 61
  • 5
5

As K. Tom suggested you can do A * A you can also do A ** 2

import numpy as np

array = np.array([1,2,3])

print array * array #[1 4 9]
print array ** 2    #[1 4 9]
John
  • 12,723
  • 7
  • 48
  • 99
2

You could use np.square or np.power:

l =  [[1,2,3], [2,3,4]]

In [5]: np.power(l, 2)
Out[5]:
array([[ 1,  4,  9],
       [ 4,  9, 16]], dtype=int32)

In [6]: np.square(l)
Out[6]:
array([[ 1,  4,  9],
       [ 4,  9, 16]], dtype=int32)
Anton Protopopov
  • 27,206
  • 10
  • 83
  • 90