What does matrix**2 mean in python/numpy?

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.

Woot, thanks. Fifteeeeenherewecome. – Alex Aug 29 '09 at 2:08 1 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 Aug 29 '09 at 2:17 1 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 Aug 29 '09 at 2:23.

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).

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. ) – URL1 Aug 29 '09 at 15:22 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 Aug 29 '09 at 17:23.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions