tags:

views:

714

answers:

3

Hi,

I have a python ndarray temp in some code I'm reading that suffers this:

x = temp**2

Is this the dot square (ie, equivalent to m.*m) or the matrix square (ie m must be a square matrix)? In particular, I'd like to know whether I can get rid of the transpose in this code:

temp = num.transpose(whatever)
num.sum(temp**2,axis=1))

and turn it into this:

num.sum(whatever**2,axis=0)

That will save me at least 0.1ms, and is clearly worth my time.
Thanks! The ** operator is ungooglable and I know nothing! a

+3  A: 

It's just the square of each element.

from numpy import *
a = arange(4).reshape((2,2))
print a**2

prints

[[0 1]
 [4 9]]
tom10
Woot, thanks. Fifteeeeenherewecome.
Alex
You're welcome. (I signed back into point out the probably obvious note, that if you're ndarray are >2 dimensions, I don't think the transposing, axis swapping thing will work.)
tom10
I can see where this might be confusing. Without knowing Python, and understanding that for real (and complex) numbers squaring means "multiply a number by itself", it would have been reasonable to assume that it meant "multiply a matrix by itself" for matricies. This means that the matrix has equal numbers of rows and columns, of course.
duffymo
+5  A: 

** is the raise-to-power operator in Python, so x**2 means "x squared" in Python -- including numpy. Such operations in numpy always apply element by element, so x**2 squares each element of array x (whatever number of dimensions) just like, say, x*2 would double each element, or x+2 would increment each element by two (in each case, x proper is unaffected -- the result is a new temporary array of the same shape as x!).

Edit: as @kaizer.ze points out, while what I wrote holds for numpy.array objects, it doesn't apply to numpy.matrix objects, where multiplication means matrix multiplication rather than element by element operation like for array (and similarly for raising to power) -- indeed, that's the key difference between the two types. As the Scipy tutorial puts it, for example:

When we use numpy.array or numpy.matrix there is a difference. A*x will be in the latter case matrix product, not elementwise product as with array.

i.e., as the numpy reference puts it:

A matrix is a specialized 2-d array that retains its 2-d nature through operations. It has certain special operators, such as * (matrix multiplication) and ** (matrix power).

Alex Martelli
Well it is sadly not so simple, as I answered; the differing behaviors of `array` and `matrix` can confuse this, and operators such as `*` and `**` change meaning! (If A * B is matrix multiplication whith A, B matrix, A**2 has to be matrix exponentiation of course.)
kaizer.se
Yes, there's a difference between matrix and array -- though `**` is of course still the raise-to-power operation, operations on a matrix apply to "the matrix", on an array to "the elements". Good point, let me edit to clarify.
Alex Martelli
+2  A: 

You should read NumPy for Matlab Users. The elementwise power operation is mentioned there, and you can also see that in numpy, some operators apply differently to array and matrix.

>>> from numpy import *
>>> a = arange(4).reshape((2,2))
>>> print a**2
[[0 1]
 [4 9]]
>>> print matrix(a)**2
[[ 2  3]
 [ 6 11]]
kaizer.se