# Why is (N dot N) different for magnitude than for X, Y, Z components?

• I
Summary:
I have data in terms of X, Y, and Z components. There is also a "Total" value for each set of X, Y, Z data. I see that SQRT(X^2 + Y^2 + Z^2) = Total. So I think "Total" is a vector magnitude. Now I have to use the data in a calculation: (the equation is "N dot N") but can't figure out the correct way to do it. If I (in Python) square "Total," the answer is different than if I do: X^2 + Y^2 + Z^2. Is it obvious to anyone whether I should use "Total" or "X^2 + Y^2 + Z^2"?
I have to perform a calculation on my data. Here is an example of data from just one time step (data from other time steps would appear as additional rows).

 X Y Z Total 2 2 1 3

Total = SQRT(X2 + Y2 + Z2).

The calculation I have to do is: (N • N), where "N" is an average.

I tried doing this in Python and realized I don't understand what I have to do. The result is not the same when I use "Total" as when I use the components. For example,

np.average(X)**2 + np.average(Y)**2 + np.average(Z)**2

is not the same as

np.average(Total)**2

Can anyone help explain which is the most likely way to do it? The only information I have makes it unclear if "N" in the equation is "Total" (the vector magnitude) or (np.average(X)**2 + np.average(Y)**2 + np.average(Y)**2) and I have no idea if there's a way to tell from what I have asked. I'm not a mathematician.

Last edited:

Gaussian97
Homework Helper
Well, I think it is clear that is not the same the magnitude of the average, that the average of the magnitude. Since taking the magnitude of a vector is not a linear operation.
As for whether you must compute one or the other, I don't think any of us can help you... Both quantities are physically interesting and no one here will have a better idea of what you were asked to do than you...

Well, I think it is clear that is not the same the magnitude of the average, that the average of the magnitude. Since taking the magnitude of a vector is not a linear operation.
As for whether you must compute one or the other, I don't think any of us can help you... Both quantities are physically interesting and no one here will have a better idea of what you were asked to do than you...
Thank you, Gaussian. It's not homework. I'm old. I'm using an old paper to calculate something I need for another calculation and there's no more information than what I posted here.

I was hoping someone who is familiar with vector calculus might know of a relationship between vector magnitude and the components that might make it make sense somehow! Sounds like I'm stuck though unless I can track down someone else who has used the equation and has provided better parameter definitions.

I don't understand your first sentence, sorry. Do you mean my definition of the magnitude is incorrect?

Gaussian97
Homework Helper

The magnitude of the average gives you information about how the system moves as a whole. While the average of the magnitude gives you information about how the system spreads in all directions, even when there is not a general movement in a certain direction.

I don't understand your first sentence, sorry. Do you mean my definition of the magnitude is incorrect?
No, what I'm saying is that the definition (the one you have is correct) is not linear, so the magnitude of a sum is not the sum of magnitudes.

bumblebee77
jedishrfu
Mentor
The definition of ##\vec A \cdot \vec B = |A||B|cos(\theta)## where ##\theta## is the angle between the two vectors.

In your case, ##\vec N \cdot \vec N = |N|^2 ## since ##\theta## is zero and ##cos(0)## is one.

In xyz coordinates, we can use the Pythagorean formula to compute |N| and from that ##|N|^2## : ##x^2 + y^2 + z^2##

bumblebee77
Office_Shredder
Staff Emeritus
Gold Member
np.average(X)**2 + np.average(Y)**2 + np.average(Y)**2

is not the same as

np.average(Total)**2

Just checking, that has two Ys and no Z's in the first part, is that your entire problem?

bumblebee77
Thank you, Gaussian97, that is interesting.
Just checking, that has two Ys and no Z's in the first part, is that your entire problem?
Thank you for pointing that out. That's a typo. It is "Z" in my code. I will edit the question.

The definition of ##\vec A \cdot \vec B = |A||B|cos(\theta)## where ##\theta## is the angle between the two vectors.

In your case, ##\vec N \cdot \vec N = |N|^2 ## since ##\theta## is zero and ##cos(0)## is one.

In xyz coordinates, we can use the Pythagorean formula to compute |N| and from that ##|N|^2## : ##x^2 + y^2 + z^2##
Thank you, jedishrfu, that is interesting. I am still not getting it though. Would anyone happen to have time to elaborate?

1. So the Pythagorean formula says the same thing my original question said, that "Total" is the square root of the sum of squares of the X, Y, Z components:

Pythagorean formula:
##|N|^2## : ##x^2 + y^2 + z^2##

My code ("**2" means "squared"):
Total**2 = X**2 + Y**2 + Z**2

2. The problem is that I get a different answer for (N • N) when I do this (np.average means "take the mean of"):
i. np.average(Total)**2

compared to this:
ii. np.average(X)**2 + np.average(Y)**2 + np.average(Z)**2

Using (i) gives leads to an answer that's reasonable. Using (ii) doesn't but I don't understand why. If anyone can see what I'm missing, I would be grateful to know.

jedishrfu
Mentor
You are conflating two notions here:
- computing an average vector
- length of an average vector

You must compute them in that order any other order will yield incorrect results.

Office_Shredder
Staff Emeritus
Gold Member
To put it another way, consider the two vectors (1,0,0) and (-1,0,0). np.average(X)=np.average(Y)=np.average(Z)=0, but np.average(total)=1.

It's probably worth meditating for a bit on this example.

bumblebee77 and jedishrfu
To put it another way, consider the two vectors (1,0,0) and (-1,0,0). np.average(X)=np.average(Y)=np.average(Z)=0, but np.average(total)=1.

It's probably worth meditating for a bit on this example.

Thank you, Office_Shredder (and jedishrfu), this is very helpful indeed. I understand the difference between the two methods you have provided in your example,
(1,0,0) (-1,0,0).

1. np.average(X)=np.average(Y)=np.average(Z)=0 because
Average = sum/count = 1+(-1)/2 = 0/2 = 0

However, I think this is different from what my equation describes:
(N•N), where N is an average of each column in my table. If I understand correctly, this should be:
np.average(X**2)=np.average(Y**2)=np.average(Z**2)=0

and I think it would end up being:
First square: 1*(-1) + 0*0 + 0*0 = 1
Then take the average: 1

2. Then in second part of your example, np.average(total)=1 because
Total = 1*(-1) + 0*0 + 0*0 = 1

I apologize because I know I'm being very slow, but I am still not certain I am getting this. From my update to your first method, it looks like both examples should lead to the same result. But they don't in my Python calculation.

I have thought about this so long and the result is so important for what I'm trying to do that I'm second guessing myself and would like to make sure not to proceed based on something that's wrong. If you see anywhere I'm getting something wrong, I'd appreciate it a lot if you would let me know. Thank you both for your patience!

Last edited:
Gaussian97
Homework Helper
Thank you, Office_Shredder (and jedishrfu), this is very helpful indeed. I understand the difference between the two methods now based on your example,
(1,0,0) (-1,0,0):

1. np.average(X)=np.average(Y)=np.average(Z)=0 because
Average = sum/count = 1+(-1)/2 = 0/2 = 0

2. np.average(total)=1 because
Total = 1*(-1) + 0*0 + 0*0 = 1

I apologize because I know I'm being very slow, but I am still not certain which one is the right approach in my situation. If we know that Total = SQRT(X**2 + Y**2 + Z**2), then how should I calculate (N•N), where N is an average? It's method (2), right?

I have thought about this so long and the result is so important for what I'm trying to do that I'm second guessing myself and would like to make sure not to proceed based on something that's wrong. Thank you both for your patience!
As I told you at the beginning, if you don't tell us what is ##N## there's absolutely no way to tell you which way is correct, both approaches are correct.
It's like saying, "I need to know what is the result of ##x+1##, if I put ##x=0## I get 1 as a result, but if I put ##x=1## then I get 2, what is the correct one?"
If ##\vec{N}## is the average of all the points, i.e. $$\vec{N}=\text{average}\{\vec{r}\}$$ then you must first take the average and then compute the dot product (i.e, the magnitude) of the vector ##\vec{N}##. But without a precise definition of ##N## there's no way no one can help you to know which way is the correct one.

As I told you at the beginning, if you don't tell us what is ##N## there's absolutely no way to tell you which way is correct, both approaches are correct.
It's like saying, "I need to know what is the result of ##x+1##, if I put ##x=0## I get 1 as a result, but if I put ##x=1## then I get 2, what is the correct one?"
If ##\vec{N}## is the average of all the points, i.e. $$\vec{N}=\text{average}\{\vec{r}\}$$ then you must first take the average and then compute the dot product (i.e, the magnitude) of the vector ##\vec{N}##. But without a precise definition of ##N## there's no way no one can help you to know which way is the correct one.

Gaussian97, I edited what you quoted although I don't think it affects what you have written. I suspect that we do know enough about N to solve this problem but I'm too inexperienced to figure it out.

What I know about N is that it's provided in bold, so it is a vector. I also know that the components (X, Y, Z) are represented by my data table columns. I know that Total is the square root of the sum of squares of the components.

I don't know if it's OK to use Total in "(N)•(N)" (here I have used () to denote "average" so that "(N)•(N)" means "average N dotted with average N"). Using Total gives me a different result than using (X)•(X) + (Y)•(Y) + (Z)•(Z).

This is the second term in the expression for "variance" if that helps.

Gaussian97
Homework Helper
Well, if ##\vec{N}## is a vector, then ##\vec{N}\cdot \vec{N}## is the norm of that vector and therefore computing the average of the magnitudes is clearly not what we want.
Also, the variance of a vector quantity can be computed as
$$V[\vec{r}] = <r^2>-<\vec{r}>^2$$
So indeed the variance is really the difference between the two number you compute. If you say that you want the second term, i.e. ##<\vec{r}>^2## then definitively you must first take the average over the positions, and then compute the norm of the resulting vector.

bumblebee77
Well, if ##\vec{N}## is a vector, then ##\vec{N}\cdot \vec{N}## is the norm of that vector and therefore computing the average of the magnitudes is clearly not what we want.
Also, the variance of a vector quantity can be computed as
$$V[\vec{r}] = <r^2>-<\vec{r}>^2$$
So indeed the variance is really the difference between the two number you compute. If you say that you want the second term, i.e. ##<\vec{r}>^2## then definitively you must first take the average over the positions, and then compute the norm of the resulting vector.
Thank you, Gaussian. To make sure I'm clear (not certain that I am):

I need to compute:
$$V[\vec{r}] = <r^2>-<\vec{r}>^2$$
I am OK with the first term. It's the second term I need help with.

1. Do I understand you correctly that I cannot use this method to calculate the first term? (This is the method that gives me a reasonable answer when I use the result in another calculation.)
np.average(Total)**2

because "Total" is a magnitude as in:
Total = SQRT(X^2 + Y^2 + Z^2) = ##\vec{N}\cdot \vec{N}##

2. Do I understand correctly that you're saying I should instead calculate
##<\vec{r}>^2##

by first averaging each column in my table (X then Y then Z). Then doing:
X*X + Y*Y + Z*Z?

3. I thought <\vec{r}>^2$$(which is what I need) = ##\vec{N}\cdot \vec{N}##. Is that not true because in ##\vec{N}\cdot \vec{N}##, the Ns are not averaged? Last edited: Office_Shredder Staff Emeritus Science Advisor Gold Member I think you will get equality if you do np.average(total**2) instead of np.average(total)**2 I think you will get equality if you do np.average(total**2) instead of np.average(total)**2 Office_Shredder, thank you. I'm not sure if you saw my reply to Gaussian97 above. What I'm trying to do here is use the data in my table to calculate the second term in:$$V[\vec{r}] = <r^2>-<\vec{r}>^2$$I may be wrong, but I'm not sure if np.average(total**2) would work. 1. First of all, I'm not sure if it's OK to use "Total" (the square root of the sum of squares of X, Y, Z), which I think is the vector "norm" or if r is X + Y + Z. 2. Also, I think it would be similar to calculating the first term, which first involves squaring "r" and then taking the average of that result. I apologize for my slowness on this. I think my main problem is that I don't understand if r in$$V[\vec{r}] = <r^2>-<\vec{r}>^2$$is "Total" or if it is X + Y + Z. Gaussian97 Homework Helper I apologize for my slowness on this. I think my main problem is that I don't understand if r in$$V[\vec{r}] = <r^2>-<\vec{r}>^2$$is "Total" or if it is X + Y + Z. ##\vec{r}## is the position vector, and ##r## is the magnitude of ##\vec{r}##. In other words, the first term is first to compute the magnitude of each vector and then average the results, while the second is to average all the positions and then compute the magnitude of the resulting vector. bumblebee77 ##\vec{r}## is the position vector, and ##r## is the magnitude of ##\vec{r}##. In other words, the first term is first to compute the magnitude of each vector and then average the results, while the second is to average all the positions and then compute the magnitude of the resulting vector. Aha. So it sounds like the notation (the little arrow over r in ##\vec{r}## in the second term) is what solves my problem. So do I understand correctly that I cannot do this to calculate the second term: ##\vec{r}## = np.average(total**2) Instead I have to do this? ##\vec{r}## = <X*X > + <Y*Y> + <Z*Z> (where <> means "average) ##\vec{r}## =np.average(X**2)=np.average(Y**2)=np.average(Z**2) (in Python) Gaussian97 Homework Helper Aha. So it sounds like the notation (the little arrow over r in ##\vec{r}## in the second term) is what solves my problem. So do I understand correctly that I cannot do this to calculate the second term: ##\vec{r}## = np.average(total**2) No, of course, this is precisely the first term and what we discussed at the beginning that of course, the average of the magnitude is not the magnitude of the average Instead I have to do this? ##\vec{r}## = <X*X > + <Y*Y> + <Z*Z> (where <> means "average) ##\vec{r}## =np.average(X**2)=np.average(Y**2)=np.average(Z**2) (in Python) The first equation is clearly wrong, in the LHS you have a vector, in the RHS you have a number. The second I don't really understand what you mean, but sure enough, all those quantities will be different. You must first take the average of the positions, this results in a single vector, and then compute the magnitude of that vector using the Pythagorean theorem. bumblebee77 No, of course, this is precisely the first term and what we discussed at the beginning that of course, the average of the magnitude is not the magnitude of the average The first equation is clearly wrong, in the LHS you have a vector, in the RHS you have a number. The second I don't really understand what you mean, but sure enough, all those quantities will be different. You must first take the average of the positions, this results in a single vector, and then compute the magnitude of that vector using the Pythagorean theorem. Thank you for your patience, Gaussian. I'm clear now that in the first term, it is correct to use np.average(total**2) (where "total" is the norm). Here's the part I may not understand--how to calculate the second term, where you say this: "You must first take the average of the positions, this results in a single vector, and then compute the magnitude of that vector using the Pythagorean theorem." Is this what I need to do? - First calculate the average of X, the average of Y, and the average of Z. I will then have three numbers (a vector). - Then compute the magnitude/norm of that vector by doing this: SQRT(X^2 + Y^2 + Z^2)? Gaussian97 Homework Helper Yes, that's exactly what you need to do to compute the second term bumblebee77 Gaussian97, thank you so much. I can't tell you how grateful I am to be on the way to understanding this at last and that you and the other commenters have been so generous with your time. Wish I could buy you all a beverage. I tried to use the solutions we discussed and unfortunately, it looks like I'm still doing something wrong because the second term is very large compared to the first term and my result from my subsequent calculation is not reasonable. I'll continue to go over what you said in case I'm still misunderstanding something. I need to calculate Term 1 and Term 2 from my table of data at the top of this thread:$$V[\vec{r}] = <r^2>-<\vec{r}>^2

Here's how I did it in Python:
Term1 = np.average(total**2) # "total" is SQRT(X^2 + Y^2 + Z^2)

Term2:
Term2x = np.mean(X) # First calculate average of X, Y, and Z data (columns) from table.
Term2y = np.mean(Y)
Term2z = np.mean(Z)

Term2 = np.sqrt(Term2x**2 + Term2y**2 + Term2z**2)
# Then square the averages and take the square root.

I think what I need to do is not use a square root in the second term and do it like this:
Term2 = Term2x**2 + Term2y**2 + Term2z**2

I realized that I need to modify the equation I'm using this result in and the result I'm getting is now reasonable with my update (in red) to Term2. Whew. I really need to review my vector calculus. Thanks again, everyone.

Last edited: