Strange behavior of operator *=

from numpy import * __version__ '1.5.1' a = eye(2, dtype='int') a *= 1.0 a ; a.dtype array([[1, 0], [0, 1]])
a = a * 1.0 a ; a.dtype array([[ 1., 0.], [ 0., 1.]])
Hi all, I have encountered the following strangeness : dtype('int64') dtype('float64') So, in this case "a *= x" is not equivalent to "a = a*x" ? François

Indeed, it is not. In the first case, you keep your original object and each (integer) element is multiplied by 1.0. In the second example, you are creating a temporary object a*x, and as x is a float and a an array of integers, the result will be an array of floats, which will be assigned to a. Matthieu 2011/4/5 François Steinmetz <francois.steinmetz@gmail.com>
Hi all,
from numpy import * __version__ '1.5.1' a = eye(2, dtype='int') a *= 1.0 a ; a.dtype array([[1, 0], [0, 1]])
a = a * 1.0 a ; a.dtype array([[ 1., 0.], [ 0., 1.]])
I have encountered the following strangeness : dtype('int64') dtype('float64')
So, in this case "a *= x" is not equivalent to "a = a*x" ?
François
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Information System Engineer, Ph.D. Blog: http://matt.eifelle.com LinkedIn: http://www.linkedin.com/in/matthieubrucher

Ok, thanks for the clarification ! François On Tue, Apr 5, 2011 at 11:55, Matthieu Brucher <matthieu.brucher@gmail.com>wrote:
Indeed, it is not. In the first case, you keep your original object and each (integer) element is multiplied by 1.0. In the second example, you are creating a temporary object a*x, and as x is a float and a an array of integers, the result will be an array of floats, which will be assigned to a.
Matthieu
2011/4/5 François Steinmetz <francois.steinmetz@gmail.com>
Hi all,
from numpy import * __version__ '1.5.1' a = eye(2, dtype='int') a *= 1.0 a ; a.dtype array([[1, 0], [0, 1]])
a = a * 1.0 a ; a.dtype array([[ 1., 0.], [ 0., 1.]])
I have encountered the following strangeness : dtype('int64') dtype('float64')
So, in this case "a *= x" is not equivalent to "a = a*x" ?
François
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Information System Engineer, Ph.D. Blog: http://matt.eifelle.com LinkedIn: http://www.linkedin.com/in/matthieubrucher
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

2011/4/5 Alan G Isaac <alan.isaac@gmail.com>:
On 4/5/2011 5:49 AM, François Steinmetz wrote:
>>> a = eye(2, dtype='int') >>> a *= 1.0 >>> a ; a.dtype array([[1, 0], [0, 1]]) dtype('int64')
This in-place (!) multiplication should not change the dtype of a. I suspect you did not exactly cut and paste...
import numpy a = numpy.ones(1, dtype='int') a.dtype
Guess he's on a 64 bit system. $ # 32 bit $ python2.6 Python 2.6.5 (r265:79063, Jul 18 2010, 12:14:53) [GCC 4.2.1 (Apple Inc. build 5659)] on darwin Type "help", "copyright", "credits" or "license" for more information. dtype('int32')
^D
import numpy a = numpy.ones(1, dtype='int') a.dtype
$ python2.6-64 Python 2.6.5 (r265:79063, Jul 18 2010, 12:14:53) [GCC 4.2.1 (Apple Inc. build 5659)] on darwin Type "help", "copyright", "credits" or "license" for more information. dtype('int64') This is with 1.4.1 (sorry for the missing update). Same behaviour with numpy-1.5.1 on py2.7. Friedrich
participants (4)
-
Alan G Isaac
-
François Steinmetz
-
Friedrich Romstedt
-
Matthieu Brucher