Numeric array referencing vs copying.

  • Thread starter Maarten van Reeuwijk
  • Start date
M

Maarten van Reeuwijk

Hello,

I recently started using Python for CFD postprocessing, and although I like
the language very much, it's memory management (copying vs referencing)
makes me a little paranoid sometimes. Could you clarify how this works in
Python?

- does the array constructor make a copy of the elements?
- is a[:] = b[:] different from a = b?

Some good examples are in the fragment below:

from Numeric import *

def el_changebase(vecfield, newbase):
""" Changes the base of the vectorfield. newbase is a list containing the
new basevectors(which are an 1D-array). vecfield is a list containing
the fields for the individual vector components fields. The lenght of
the vecfield and newbase lists must be identical. """

vf = vecfield
nb = newbase
nf = []

tmp = array(vf[0], vf[0].typecode(), savespace = 1)

for bcomp in range(len(newbase)):
tmp[:] = 0
for vcomp in range(len(newbase)):
tmp = tmp + nb[bcomp][vcomp] * vf[vcomp]

nf.append(array(tmp))

return nf

# sample run
u = arange(9)
u.shape = (3,3)
v = array(u)

print "before:\n", u, "\n", v

s2 = 0.5 * sqrt(2)

# rotate the coordinate system 45 degrees
u, v = el_changebase([u,v],[array((s2, s2)), array((s2, -s2))])

print "after:\n", u, "\n", v

TIA, Maarten
 
M

Maarten van Reeuwijk

Whoops, detected a small bug in the example code: u and v should be floats.

So
u = arange(9)

should be

u = arange(9.)

This makes the outcome of the script a little more convincing :)

===================================================================
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
 
J

John J. Lee

Maarten van Reeuwijk said:
I recently started using Python for CFD postprocessing, and although I like
the language very much, it's memory management (copying vs referencing)
makes me a little paranoid sometimes. Could you clarify how this works in
Python?

- does the array constructor make a copy of the elements?

You mean Numeric.array? Yes. Try it out with the interactive prompt:
a = arange(3.)
a array([ 0., 1., 2.])
b = array(a)
b array([ 0., 1., 2.])
id(a) 135849936
id(b) 135622560
a[0] = 5.
a array([ 5., 1., 2.])
b
array([ 0., 1., 2.])

- is a[:] = b[:] different from a = b?
[...]

Yes, completely. a = b means just the same as it means for any other
"assignment" in Python: it just binds (the object bound to the name b)
to the name a. It never means anything else in Python, happily :)
There's a FAQ item (or two) on this in the Python FAQ. It's very F'ly
A'd :)

Slicing a Numeric array *is* different to other Python sequences:
slicing a Numeric.array doesn't make a copy, slicing a list (or a
tuple, of course) does. I'm sure this is covered in the Numeric
manual, though it's a while since I read it. Carrying on from above:
c = a[:]
c array([ 5., 1., 2.])
c[0] = 0
c array([ 0., 1., 2.])
a
array([ 0., 1., 2.])


But, in contrast:
l = [0., 1., 2.]
l2 = l[:]
l2 [0.0, 1.0, 2.0]
l2[0] = 5
l2 [5, 1.0, 2.0]
l
[0.0, 1.0, 2.0]


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top