Using arrays in Python - problems.

A

attackwarningred

Dear All,
Hello! I've just started to use Python and its a lovely
language! I've previously programmed in Fortran 95 and have just began
to use numpy. I'm having a few problems with arrays in Python though and
wondered if someone could offer me some advice?
I wrote the following Fortran code to randomly generate numbers from
a log-normal distribution for use in a Monte Carlo model:

do n=1,shotcount
F(n)=G05DEF(F_mean,F_sd)
enddo

The array F(n) is dynamically allocated earlier on and is sized with
reference to shotcount, the number of iterations the model performs. The
problem is I can't get something like this to run in Python using numpy,
and for the size of the array to be sized dynamically with reference to
the variable shotcount. I acknowledge that my knowledge of Python is
still really basic (I only started learning it a few days ago) and I'm
trying to get out of the Fortran programming mindset but I'm stuck and
don't seem to be able to get any further. If anyone could help I'd be
really grateful. Thanks very much in advance.

Best wishes,
Gareth.
 
M

marek.rocki

attackwarningred napisa (a):
The array F(n) is dynamically allocated earlier on and is sized with
reference to shotcount, the number of iterations the model performs. The
problem is I can't get something like this to run in Python using numpy,
and for the size of the array to be sized dynamically with reference to
the variable shotcount. I acknowledge that my knowledge of Python is
still really basic (I only started learning it a few days ago) and I'm
trying to get out of the Fortran programming mindset but I'm stuck and
don't seem to be able to get any further. If anyone could help I'd be
really grateful. Thanks very much in advance.

Hello. If you want your array to be dynamically resized at every loop
iteration, that might be quite inefficient. How about initialising it
with a required size?

F = numpy.array([0]*shotcount)
for n in xrange(shotcount):
F[n] = random.lognormvariate(F_mean, F_sd)

Hope that helps,
Marek
 
R

Robert Kern

attackwarningred napisa (a):
The array F(n) is dynamically allocated earlier on and is sized with
reference to shotcount, the number of iterations the model performs. The
problem is I can't get something like this to run in Python using numpy,
and for the size of the array to be sized dynamically with reference to
the variable shotcount. I acknowledge that my knowledge of Python is
still really basic (I only started learning it a few days ago) and I'm
trying to get out of the Fortran programming mindset but I'm stuck and
don't seem to be able to get any further. If anyone could help I'd be
really grateful. Thanks very much in advance.

Hello. If you want your array to be dynamically resized at every loop
iteration, that might be quite inefficient. How about initialising it
with a required size?

F = numpy.array([0]*shotcount)

A more idiomatic version would be this:

F = numpy.empty((shotcount,), dtype=float)

attackwarningred, you might want to ask your numpy questions on the
numpy-discussion mailing list:

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
D

Duncan Smith

attackwarningred napisa (a):

The array F(n) is dynamically allocated earlier on and is sized with
reference to shotcount, the number of iterations the model performs. The
problem is I can't get something like this to run in Python using numpy,
and for the size of the array to be sized dynamically with reference to
the variable shotcount. I acknowledge that my knowledge of Python is
still really basic (I only started learning it a few days ago) and I'm
trying to get out of the Fortran programming mindset but I'm stuck and
don't seem to be able to get any further. If anyone could help I'd be
really grateful. Thanks very much in advance.


Hello. If you want your array to be dynamically resized at every loop
iteration, that might be quite inefficient. How about initialising it
with a required size?

F = numpy.array([0]*shotcount)
for n in xrange(shotcount):
F[n] = random.lognormvariate(F_mean, F_sd)

Hope that helps,
Marek

or,

F = numpy.random.lognormal(F_mean, F_sd, shotcount)

(assuming F_mean, F_sd are the parameters of the distribution, rather
than the actual mean and standard deviation).

Duncan
 
A

attackwarningred

Thanks very much to those who sent me a reply to my array problem! Its
now working brilliantly!

Best wishes,
Gareth.

--


(e-mail address removed)
(e-mail address removed)

665.9238429876 - Number of the Pentium Beast
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top