[SciPy-User] a new enigma for a matlab user

midel midel at sfr.fr
Mon May 23 08:21:45 EDT 2011


Wow, thanks everybody for all your answers, it is perfectly clear for me now !

The road from matlab to python is full of funny obstacles :)

midel


========================================

Message du : 23/05/2011
De : "midel " <midel at sfr.fr>
A : "scipy-user" <scipy-user at scipy.org>
Copie à : 
Sujet : Re: [SciPy-User] a new enigma for a matlab user


 Thanks ! ok, so :

variable1=variable2;

creates a pointer, not an independant and new variable ?

midel

========================================

Message du : 23/05/2011
De : "Robin " <robince at gmail.com>
A : "midel" <midel at sfr.fr>, "SciPy Users List" <scipy-user at scipy.org>
Copie à : 
Sujet : Re: [SciPy-User] a new enigma for a matlab user


 
In Python you can think of everything as an object and the 'variables'
are text labels that point to an object.

> Ve=Uo;
Here you are setting the "name" Ve to point to the same object the Uo
points to. This means both of these reference the same variable. So
when you make a change to one it is changing the same array object and
you see the changes in the other.

If you don't want that, you can either explicitly copy:
Ve = Uo.copy()
or create a seperate matrix.

However, you might be confused when the same thing doesn't happen with integers:
a = 1
b = a
a = 2
# b doesn't change
This is explained by keeping in mind the name idea... there is a '1'
object and a '2' object... after b=a both names are pointing to 1
object, when you do a=2 you are rebinding a to point to the 2 object.
This is different from changing the underlying object, which you do
when you manipulate a slice of a numpy array.

Hope this helps a bit,

Cheers

Robin

On Mon, May 23, 2011 at 2:05 PM, midel  wrote:
> Hi everybody,
>
> Today I face a new mystery (for me) which seems to be linked to a
> fundamental difference between matlab and python langage...
>
> The principle of my code (its beginning) is quite simple. I create an "x"
> vector (dimension nx) and then 2 other vectors Efini_ord and Ehini which are
> gaussian functions of x.
>
> Then I want to store these vectors in two matrices Uo and Ve (dimension nx,
> 20). I do this because I will modifie the vectors 19 times and i want to
> store every step.
>
> Here is my code :
>
> import numpy as np
> import scipy as sp
>
> import matplotlib.pylab as plt
> from numpy.fft import fft,ifft
>
>
> #Fonction pour creer une gaussienne
> def supergaussnorm(x,n):
>     sga=np.exp(-x**(2*n));
>     return sga
>
> # fenetre transverse
> RES=6;
> mx=10; #taille de la fenetre
> nx=2**RES; # nombre de points
> x=np.linspace(-mx/2,mx/2,nx); #vecteur transverse
>
>
> # two different profiles
> Efini_ord=1*supergaussnorm(x,1);
> Ehini=0*(supergaussnorm(x,1));
>
> # kind of "saving data" matrices Uo and Ve
> nET=20;
> Uo=np.zeros((nx,nET))+np.zeros((nx,nET))*1j;
> Ve=Uo;
>
>
> # first iteration of data saving
> Uo[:,0]=Efini_ord;
> Ve[:,0]=Ehini; #line to be commented or not, which seems to have absolutely
> no relation with Uo
>
> # plot plot
> plt.plot(Uo[:,0],'r.')
> plt.plot(Efini_ord)
> plt.show()
>
> If I run this, Uo[:,0] IS NOT Efini_ord, but a vector of zeros...
>
> If I #comment the line :
>
> #Ve[:,0]=Ehini; #line to be commented or not, which seems to have absolutely
> no relation with Uo
>
> Now Uo[:,0] IS Efini_ord ! Everything works as if what I do with Ve has an
> influence on Uo. It is confirmed by the fact that if I create Ve by :
>
> Ve=np.zeros((nx,nET))+np.zeros((nx,nET))*1j;
>
> instead of
>
> Ve=Uo;
>
> the problem also disappears !
>
> I was used with matlab to "clone" variables as in Ve=Uo;
>
> but it looks like such writing is totally wrong in python...
>
> Can somebody explain ?
>
> Thanks !
>
> Midel
>
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>


 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110523/2fb416b4/attachment.html>


More information about the SciPy-User mailing list