Welcome to Dream.In.Code
Getting Help is Easy!

Join 117,614 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,985 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



numpy array allocations

 
Reply to this topicStart new topic

numpy array allocations, start with x = y, then change the value of y, should x change too?

ramz
post 17 Jun, 2008 - 01:28 PM
Post #1


New D.I.C Head

*
Joined: 8 Jun, 2007
Posts: 10


My Contributions


I have come across something strange with numpy arrays in Python, and was wondering if anyone had an explanation. First let's start with something simple, which works as you'd expect:
CODE
>>> x = 6
>>> y = x
>>> y =3
>>> x
6
>>>


Now, the question: under what situations would the value of x become 3?

See the examples below to see what I mean. I've highlighted the lines that are changing with comments before and after.
CODE
from numpy import zeros, array, sqrt, loadtxt
rec_east = array(loadtxt("file.txt"), float)
# also load some other arrays:
# source_east, source_north, and rec_north
dist_x = zeros(rec_east.shape, float)
#### whatsthat.gifwhatsthat.gif ####
dist_y = dist_x
#### whatsthat.gifwhatsthat.gif ####
for j in range (0, 1):
   for k in range(0, 1):
       dist_x[j,k] = (rec_east[j,k] + source_east[j])/2
       tval = (rec_east[j,k] + source_east[j])/2
       #### whatsthat.gifwhatsthat.gif  ####
       #dist_y[j,k] = (rec_north[j,k] + source_north[j])/2
       #### whatsthat.gifwhatsthat.gif ####
   print tval, dist_x[j,k]
# output is:
# 323717.265 323717.265

This is what is expected: tval and dist_x are the same

Now, if the dist_y line is uncommented,
CODE
### ...
dist_x = zeros(rec_east.shape, float)
#### whatsthat.gifwhatsthat.gif ####
dist_y = dist_x
#### whatsthat.gifwhatsthat.gif ####
for j in range (0, 1):
   for k in range(0, 1):
       dist_x[j,k] = (rec_east[j,k] + source_east[j])/2
       tval = (rec_east[j,k] + source_east[j])/2
       #### whatsthat.gifwhatsthat.gif  ####
       dist_y[j,k] = (rec_north[j,k] + source_north[j])/2
       #### whatsthat.gifwhatsthat.gif ####
   print tval, dist_x[j,k]
# output is:
#323717.265 4464864.285

Not that the value of dist_x has changed. (Though not shown, it has changed to the value of dist_y[j,k].)

However, if the declaration of dist_y is changed, then the code works:
CODE
### ...
dist_x = zeros(rec_east.shape, float)
#### whatsthat.gifwhatsthat.gif  ####
dist_y = zeros(rec_east.shape, float)
#### whatsthat.gifwhatsthat.gif ####
for j in range (0, 1):
   for k in range(0, 1):
       dist_x[j,k] = (rec_east[j,k] + source_east[j])/2
       tval = (rec_east[j,k] + source_east[j])/2
       #### whatsthat.gifwhatsthat.gif  ####
       dist_y[j,k] = (rec_north[j,k] + source_north[j])/2
       #### whatsthat.gifwhatsthat.gif ####
   print tval, dist_x[j,k]
# output is:
# 323717.265 323717.265


While I have figured out how to circumvent the problem, I don't really get why it was happening in the first place. Is there any reason why I should have expected this?

This post has been edited by ramz: 17 Jun, 2008 - 01:32 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 11:57PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month