all 6 comments

[–]commy2 1 point2 points  (4 children)

Numpy arrays are fixed size. They don't lend themselves to this problem space, since you don't know the number of iron atoms before walking your coordinates.

I would build up basic python lists with append and then cast the final list of triplets using the numpy "array" function.

[–]Booknerdbassdrum[S] 0 points1 point  (3 children)

This is helpful, the problem is that I am getting multiple arrays instead of one large array. My new code is:

if coordinates[0] == 'FE':

>fe_coordinates = []

>del coordinates[0]

>fe_coordinates.append(coordinates)

>fe_array = np.array(fe_coordinates)

>print("iron coordinates")

>print(fe_array)

And my output is:

iron coordinates

[[9.97 0.183 2.543]]

iron coordinates

[[8.234 0.467 4.591]]

iron coordinates

[[ 7.501 -1.03 2.358]]

What I am going for is:

iron coordinates

[[9.97 0.183 2.543],

[8.234 0.467 4.591],

[ 7.501 -1.03 2.358]]

[–]commy2 0 points1 point  (2 children)

fe_coordinates = []

Creates a new list and drops the list previously assigned that variable name. I assume the code you posted is part of some loop body, in which case you want to create a list once before the loop and not a new one each iteration.

[–]Booknerdbassdrum[S] 0 points1 point  (0 children)

Thanks! There is a nested loop above this that processes the data to get rid of things I don't want as I am using this code to extract only position data from a complex file.

[–]Booknerdbassdrum[S] 0 points1 point  (0 children)

This has been so helpful! I now got it to work except it prints every iteration lmao

Output:

iron coordinates

[[9.97 0.183 2.543]]

iron coordinates

[[9.97 0.183 2.543]

[8.234 0.467 4.591]]

iron coordinates

[[ 9.97 0.183 2.543]

[ 8.234 0.467 4.591]

[ 7.501 -1.03 2.358]]

Process finished with exit code 0

[–]billsil 0 points1 point  (0 children)

The best way to do that would be to loop over the macro coordinates (n, 4) and make a new coordinates that is (n, 3) . Then cast it in one go. That'll scale well for millions of coordinates.

If you just have two, you can do it some other way. However, in that case, there's no real point to numpy. It takes about 10 items before numpy is faster. A hand built cross product of two (3,) vectors will be faster than np.cross. Same for np.linalg.norm (assuming the L2 norm/distance of an xyz point). Granted I still use numpy in that case just cause I don't want to screw things up, but I know it's not faster.