1

I have two numpy arrays, with just the 3-dimensional coordinates of two molecules.

I need to implement the following equation, and I'm having problems in the subtraction of each coordinate of one of the arrays by the second, and then square it.

enter image description here

I have tried the following, but since I'm still learning I feel that I am making some major mistake. The simple code I use is:

a = [math.sqrt(1/3*((i[:,0]-j[:,0])**2) + ((i[:,1] - j[:,1])**2) + ((i[:,2]-j[:,2])**2) for i, j in zip(coordenates_2, coordenates_1))]
2
  • try np.sqrt(np.mean((arr1 - arr2) ** 2))) Commented Dec 6, 2020 at 18:41
  • math.sqrt only works with single values, not arrays. np.sqrt is best when using whole arrays. Commented Dec 6, 2020 at 20:04

1 Answer 1

1

It's numpy you can easily do it using the following example:

import numpy as np
x1 = np.random.randn(3,3,3)
x2 = np.random.randn(3,3,3)

res = np.sqrt(np.mean(np.power(x1-x2,2)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, sorry for the mess I made formulating the question. I have some questions about your answer, can I use np.square insted of ``` np.power``` and for my case would´t It be np.sum instead of np.mean. Finally I have used the following line, but I still cant get the exact results so perhaps Im making a mistake somewhere. np.sqrt((1/3)* np.sum(np.square(coordenadas_2-coordenadas_1)))
@JoseGraciaRodriguez can you share your data and expected result? It might be a small detail that is missed. And you can use square and sum it won’t matter

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.