[Tutor] strange behavior of matrix**matrix
__peter__ at web.de
Sun Jul 17 11:17:16 EDT 2016
> I'm puzzling over some strange behavior of raising a matrix to a matrix
> If I create two matrices (containing integers):
> a = np.array()
> b = np.array([-1])
> the operation a**b produces:
> array(, dtype=int32)
> The result of division b/a is correct:
> If any one of the matrices is float (i.e. created with the use of
> dtype=float) the result is correct, i.e. a**b produces:
> array([ 0.5])
> If a and b are single integer 32 numbers, e.g. obtained with:
> a = np.int32(2)
> b = np.int32(-1)
> the same operation a**b produces:
> as expected.
> Perhaps it's somehow related to the 'old' integer division of Pythons 2.x.
> I wonder if it's a bug or a feature.
There are two division operators, / and //, so you can choose explicitly:
>>> numpy.array([4, 6]) / numpy.array([3, 2])
array([ 1.33333333, 3. ])
>>> numpy.array([4, 6]) // numpy.array([3, 2])
There is only one ** operator, so numpy has to pick a default.
> And if it's a feature, then what is it useful for?
It's probably rarely used with negative exponents, but you don't want numpy
to pick the result type by inspecting all entry values in the potentially
large result array. For scalars the overhead is less relevant.
> I'm on Python 3.5.1 64bit via Spyder 3.0.0dev, numpy 1.9.3, scipy 0.16.1
> (all installed from WinPython-64bit-184.108.40.206.exe), running on Windows 10.
More information about the Tutor