Hi,
I've got some c++ code(which i don't have much experience in).
I'm trying to write the same code again in python but i'm not having much luck.

The c++ code creates an array of x,y co-ordinates:

for (double ix = 0; ix < nlat; ix++)
{
    for (double iy = 0; iy < nlat; iy++)
    {
        ipart = ipart+1
        xy[0][ipart]=ix*s;
        xy[1][ipart]=iy*s;
    }   
}

I've tried the folliwing python code but am having no luck

xy=(1,1)
xy=list(xy)


for x in range(0,nlat):

    for y in range(0,nlat):
        ipart=ipart+1
        xy[0][ipart] =y*s
        xy[1][ipart] =y*s

can anyone see where I'm going wrong? I've also tried removing the line converting from a tuple to a list but no joy.
Thanks in advance

Recommended Answers

All 11 Replies

I don't what you want exactly but it's better to use Scipy array for that, but if you like list, you have to append.

from scipy import zeros
xy = zeros((nlat,nlat)) #initialisation of the array
for x in range(0,nlat):
    for y in range(0,nlat):
        ipart=ipart+1
        xy[0][ipart] =y*s
        xy[1][ipart] =y*s

it should work

could you post there error you are getting?

oh and instead of

xy=(1,1)
xy=list(xy)

You can just go

xy = [1,1]

And then it is a list already

In [1]: xy = [1,1]
In [3]: print xy
[1, 1]
In [4]: xy[0][1]
---------------------------------------------------------------------------
<type 'exceptions.TypeError'> Traceback (most recent call last)

/var/autofs/misc/home/faniry/<ipython console> in <module>()

<type 'exceptions.TypeError'>: 'int' object is unsubscriptable

In [5]:

You can't index a list that's the problem. To put a new element in a list we have to use append.

I tried the scipy solution and i get the following error:

Traceback (most recent call last):
File "program.py", line 26, in <module>
xy[0][ipart] =y*s
IndexError: index out of bounds

incidently, the ipart is there purely as a way of indexing the particles. Later on in the program they are moved but they must be checked against other particles for example

chosenparticle=int(numberofparticles+(random()-0.1)
#the index is defined later#
for i in range(0,numberofparticles-1)
    index=(i+chosenparticle)%(numberofparticles+1)

thus the x and y values of the particle chosen and the ones not chosen can be compared

#eg for x co-ordinate
print  xy[0][chosenparticle] #should print just the one chosen
print  xy[0][index] # should print all but the chosen particles x

I'm just stuck on how to create the initial conditions to vary the parameters.

or perhaps there is a more python friendly way to get the same effect?

I think that this doesn't make any sens

chosenparticle=int(numberofparticles+(random()-0.1)

In fact, random() is a random number in (0,1), so random()-0.1 is still in (0,1)

so, int(numberofparticles+(random()-0.1)) = int(numberofparticles) ???????

You should fixe it first.

The above error that you get is because, your ipart increase until ipart+1
so, you may possibly take range(0,nlat-1) instead of range(0,nlat)

May be, it helps you, 'scipy.arange' can loop through float, example

for i in scipy.arange(0,5.3,0.01)#from 0 to 5.3 with a step size of 0.01

Regards
Faniry

sorry, i made a mistake,
it should have been

chosenparticle=int(numberofparticles*random())

but the values from 0,nlat are integers, since nlat is just the number of particles along the edges of the square.

I'm lost with your particle suff but this is the last version of your question

from scipy import zeros
xy = zeros((nlat,nlat)) #initialisation of the array
for x in range(0,nlat-1):
    for y in range(0,nlat-1):
        ipart=ipart+1             
        xy[0][ipart] =y*s
        xy[1][ipart] =y*s

Regards
Faniry

One nice way to create multidimensional arrays is to use the dictionary container, where the keys are the index tuples of the array ...

# create a 2D-array using a dictionary with index tuples as keys
# initialize values to zero
cols = 3
rows = 4
d = dict([((x,y), 0) for x in range(cols) for y in range(rows)])

# Python3 has dictionary comprehension to simplify this
#d = {(x,y):0 for x in range(cols) for y in range(rows)} 

# sorts the key index tuples
for ix in sorted(d):
    print( "ix=%s val=%s" % (ix, d[ix]) )

print('-'*20)

# change some values, basically assign to index
d[0, 1] = 5
d[0, 2] = 7
# or ...
ix = (0, 0)
d[ix] = 9
# load part of the array ...
for col in range(1, cols):
    for row in range(rows):
        # load with a fixed or calculated value
        d[col, row] = 33

# now show the result sorted by index tuple
for ix in sorted(d):
    print( "ix=%s val=%s" % (ix, d[ix]) )
    
print('-'*20)

# get a specific value from a given index
print(d[0, 0])  # 9
# or ... 
ix = (0, 0)
print(d[ix])  # 9
# simpler ...
my_ix = 0, 0
print(d[my_ix])  # 9

"""
my result -->
ix=(0, 0) val=0
ix=(0, 1) val=0
ix=(0, 2) val=0
ix=(0, 3) val=0
ix=(1, 0) val=0
ix=(1, 1) val=0
ix=(1, 2) val=0
ix=(1, 3) val=0
ix=(2, 0) val=0
ix=(2, 1) val=0
ix=(2, 2) val=0
ix=(2, 3) val=0
--------------------
ix=(0, 0) val=9
ix=(0, 1) val=5
ix=(0, 2) val=7
ix=(0, 3) val=0
ix=(1, 0) val=33
ix=(1, 1) val=33
ix=(1, 2) val=33
ix=(1, 3) val=33
ix=(2, 0) val=33
ix=(2, 1) val=33
ix=(2, 2) val=33
ix=(2, 3) val=33
--------------------
9
9
9

"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.