3

I'm having trouble in a loop. I have a bunch of points (a 5-D space) saved in an array

Coords=[]
Coords.append(zip(x,y,z,t,w))
Coords=np.array(Coords,float)

so that I can call all coords of 1 particle by, for example

Coords[0,0]=[0.0.0.0.1.]
Coords[0,1]=[0.1,0.,0.,0.,0.9]
Coords[0,1,0]=0.1

Now, I need to calculate some properties for every particle, so I create a dicionary, where for every key(i.e. every particle) I compute somwthing

A={}
Aa=np.arange(0.0,1.0+0.001,0.001)
for s in range(len(Coords[0])):
    A[s]=[]
    for a in Aa:
        if Coords[0,s,2]>=a and np.sqrt(Coords[0,s,0]*Coords[0,s,4])>=a:
            A[s].append(a)

Here I get the proper dictionary, so I'm calling the varaibles Coords[0,s,0] and Coords[0,s,4] properly, there is no problem.

Now, this is where I have problems. I need to compute another property for every particle for every value in A, therefore I create a dictionary of dictionaries.

L={}
for s in range(len(Coords[0])):
    L[s]={}
    for a in A[s]:
        L[s][a]=[]
        for i in Aa:
            if (Coords[0,s,0]-i)*(Coords[0,s,4]-i)-a**2==0:
                L[s][a].append(i)

Now I have a problem. The variables Coords are not called properly, there are missig values.

For example, the Coords[0,2,0]=0.1 and Coords[0,2,4]=0.6 should produce two values in the list: 0.1 and 0.6 (for a=0). However, in the list only appears the value 0.1, like the variable Coords[0,2,4]=0.6 doesn't exist.

However, if I write by hand the if condition like (for a=0)

if (0.1-i)*(0.6-i)-a**2==0

then I get the proper values.

Does anyone know why this is happening? Is it because I have dictionaries inside dictionaries?

Thanks.

1
  • I don't think you use dictionaries anywhere in your code. Commented Jan 15, 2018 at 0:03

1 Answer 1

1

In your second condition:

(Coords[0,s,0]-i)*(Coords[0,s,4]-i)-a**2==0:

Try using a tolerance for your comparison, something like:

abs((Coords[0,s,0]-i)*(Coords[0,s,4]-i)-a**2) < 10**-10

There's a more detailed description here: Rounding errors with floats in Python using Numpy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.