[Numpy-tickets] [NumPy] #380: broadcasting problem?: - differs from -= and + differs from +=
NumPy
numpy-tickets at scipy.net
Tue Nov 14 15:47:29 CST 2006
#380: broadcasting problem?: - differs from -= and + differs from +=
------------------------+---------------------------------------------------
Reporter: holistone | Owner: somebody
Type: defect | Status: closed
Priority: normal | Milestone:
Component: numpy.core | Version:
Severity: normal | Resolution: wontfix
Keywords: |
------------------------+---------------------------------------------------
Changes (by rkern):
* status: new => closed
* resolution: => wontfix
Old description:
> a = ones((3,2))[[BR]]
> print a-a[0][[BR]]
> [[ 0. 0.] [ 0. 0.] [ 0. 0.]][[BR]]
> a -= a[0][[BR]]
> print a[[BR]]
> [[ 0. 0.] [ 1. 1.] [ 1. 1.]][[BR]][[BR]]
>
> a = ones((3,2))[[BR]]
> a+a[0][[BR]]
> array([[ 2., 2.], [ 2., 2.], [ 2., 2.]])[[BR]]
> a += a[0][[BR]]
> a[[BR]]
> array([[ 2., 2.], [ 3., 3.], [ 3., 3.]])[[BR]]
> I expected the same result in both cases.
> Is this the intended behavior?
New description:
{{{
#!python
a = ones((3,2))
print a-a[0]
[[ 0. 0.] [ 0. 0.] [ 0. 0.]]
a -= a[0]
print a
[[ 0. 0.] [ 1. 1.] [ 1. 1.]]
a = ones((3,2))
a+a[0]
array([[ 2., 2.], [ 2., 2.], [ 2., 2.]])
a += a[0]
a
array([[ 2., 2.], [ 3., 3.], [ 3., 3.]])
}}}
I expected the same result in both cases.
Is this the intended behavior?
Comment:
This is expected. Indexing does not create a new object; it creates a view
into the original object. If you modify the original object, then the view
will also change. The augmented assignments modify the original object in
place where the simple + and - operators create an entirely new object to
hold the results. In the += case, something like the following is
happening under the covers:
{{{
#!python
a[0] = a[0] + a[0]
a[1] = a[1] + a[0]
a[2] = a[2] + a[0]
}}}
The RHS of the second and third lines are getting the modified values
a[0].
--
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/380#comment:1>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.
More information about the Numpy-tickets
mailing list