I want to write in python the equivalent to this code in Matlab,
zeta = zeros(1,5)
alpha=[1e5,1e3,1e5,1e7,1e3];
dz = 0.001;
for i=1:length(zeta)
zeta(i) = alpha(i)/(dz*dz);
end
EDIT: Thank for all answers, they are all very useful and are helping understanding how python works, and seems Matlab too; because I'm not getting the full potencial of array and matrix operation. My initial programming language is C.
Now, I'm trying to figure how to code in python cycles and array operations. If you can help. (zeta is from the previous code)
nl= 7;
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.3)
wz=zeros(1,nl); %layer width
temp=0; %auxiliary temp variable
for i=1:nl
wz(i)=l(1,i)*(nz-1)+temp;
temp=wz(1,i);
end
5 Answers 5
import numpy as np
alpha = np.array([1e5, 1e3, 1e5, 1e7, 1e3])
dz = 0.001
zeta = alpha / dz**2
8 Comments
zeta = np.zeros(5); zeta[:] = alpha / dz**2? That wouldn't be faster, would it?a = 1:5; a(100) = 1; will resize a to be a 1x100 array. Therefore you need to pre-allocate arrays before iterating thorough them. In python, if you index something beyond its bounds, you'll raise an IndexError.Translate you code to python:
alpha = [1e5,1e3,1e5,1e7,1e3]
dz = 0.001
zeta = [i/(dz**2) for i in alpha]
Comments
Try this, using an explicit for loop:
zeta = [0] * 5
alpha = [1e5, 1e3, 1e5, 1e7, 1e3]
dz = 0.001
for i in range(len(zeta)):
zeta[i] = alpha[i] / (dz*dz)
Or this, using list comprehensions (this one should be faster):
alpha = [1e5, 1e3, 1e5, 1e7, 1e3]
dz = 0.001
zeta = [a/(dz*dz) for a in alpha]
Notice that I'm not using NumPy, just pure Python.
1 Comment
Just to make sure everything is touched on, when you're using Matlab, you should always vectorize, and that wasn't done in the initial code. Therefore, I would recommend that:
zeta = zeros(1,5)
alpha=[1e5,1e3,1e5,1e7,1e3];
dz = 0.001;
for i=1:length(zeta)
zeta(i) = alpha(i)/(dz*dz);
end
Actually be written as:
alpha = [1e5,1e3,1e5,1e7,1e3];
dz = 0.001;
zeta = alpha/dz^2;
Then the code that you write in numpy follows much more naturally from what you've written in Matlab and likewise takes advantage of vectorization.
import numpy
alpha = numpy.array([1e5,1e3,1e5,1e7,1e3])
dz = 0.001
zeta = alpha/(dz*dz)
In both cases, there's no need to pre-allocate.
ADDITION AFTER EDIT: So I think there are still some errors in the first block. You had:
nl= 7;
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.3)
wz=zeros(1,nl); %layer width
temp=0; %auxiliary temp variable
for i=1:nl
wz(i)=l(1,i)*(nz-1)+temp;
temp=wz(1,i);
end
You never defined nz, so I am guessing you meant the scalar nl, which (I think) is supposed to be the length of l (or was this just a coincidence?). Just for the first part, I'm guessing this is what you would like to do:
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.3];
nl = length(l);
wz = cumsum(l*(nl-1));
This is of course the Matlab version, and the numpy version also follows naturally where:
l = numpy.array([0.3,0.1,0.2,0.1,0.1,0.1,0.3])
n1 = len(l)
wz = numpy.cumsum(l*(n1-1))
I would like to clarify that this was what you wanted for the first part before I take a stab at the second.
5 Comments
With numpy, as with matlab or similar languages, you actually don't want to use loops over indicies when you can use array-based operations, which are faster and clearer:
import numpy
zeta = numpy.zeros((1,5))
alpha = numpy.array([1e5,1e3,1e5,1e7,1e3])
dz = 0.001
zeta[0,:] = alpha/(dz*dz)
if you really wanted to loop over the indicies, you'd do this:
import numpy
zeta = numpy.zeros((1,5))
alpha = numpy.array([1e5,1e3,1e5,1e7,1e3])
dz = 0.001
for i in xrange(alpha.size):
... zeta[0,i] = alpha[i]/(dz*dz)
ipython, especiallyipython -pylab. I use it instead of matlab, and it works great (unless you need some fancy toolbox).