- Home /
Squared vector divided by squared magnitude
That's almost certainly a laughably simple math question, but I don't have the vocabulary to Google it.
My understanding is that .normalized
basically divides a vector by its magnitude, in turn obtained with a costly square root calculation. The page on .sqrMagnitude
recommends avoiding the calculation altogether when possible, by using .sqrmagnitude
with the square of whatever you're comparing your magnitude with.
I was wondering if that method applies to vectors. Will I get a normalised vector by squaring it, then dividing it by its squared magnitude? Does .normalized
already do that?
Vector3.normalized exactly does that. Calculates the square root to get the magnitude and then divide.
Anyway, you sould not worry much between Vector3.sqr$$anonymous$$agnitude or Vector3.magnitude. Using .sqr$$anonymous$$agnitude or .normalized will only make a difference if you call them many many times every update.
Its components, I mean. As in
v = new Vector3 (v.x * v.x, v.y * v.y, v.z * v.z);
Or
v = Vector3.Scale (v, v);
Then, to be frank, you should say so (ie edit the question) rather than talking about "squaring a vector". $$anonymous$$aking up your own names for algebraic operations is not going to help you to get help!
But anyway, I'm afraid you're on a wild goose chase here. The "magnitude comparison" operation can be done without ever computing the magnitude, because you don't actually care what it is, all you care about it how it compares to some other value.
Normalisation is different. A normalised vector is the vector divided by its magnitude. Hence, you need the actual magnitude.
Answer by wibble82 · Dec 15, 2015 at 11:04 AM
Hey
The basic answer to the question:
I was wondering if that method applies to vectors. Will I get a normalised vector by squaring it, then dividing it by its squared magnitude? Does .normalized already do that?
Is no, you can't :)
The vector, v = new Vector3 (v.x v.x, v.y v.y, v.z * v.z) has no strong mathematical meaning - that's not how you multiply vectors together.
There are 2 ways to multiply vectors together:
The DOT PRODUCT, sometimes called an inner product, which gives a scalar (1 float) result. Vector3.Dot(a,b) = a.x*b.x + a.y*b.y + a.z*b.z. The squared magnitude of a vector is actually got by doing the dot product of a vector with itself. i.e. a.sqrMagnitude = a.x*a.x+a.y*a.y+a.z*a.z
The OUTER PRODUCT gives a 3x3 matrix, and is a more complex mathematical concept which I won't go into here, cos it's not too useful until you want to work out tensors or eigenvalues and stuff! :)
So:
a.sqrMagnitude = Vector3.Dot(a,a) = a.x*a.x+a.y*a.y+a.z*a.z
and
a.magntidue = Mathf.Sqrt(a.sqrMagnitude) = Mathf.Sqrt(a.x*a.x+a.y*a.y+a.z*a.z)
When people talk about avoiding the use of 'magnitude', they generally mean that if all you want to know is "is my vector less than / more than a given length", then you can do it more efficiently by looking at sqrMagnitude:
float maxlen = 10.0f;
if(a.magnitude < maxlen)
{
//something here
}
is exactly the same as
float maxlen = 10.0f;
if(a.sqrMagnitude < maxlen*maxlen)
{
//something here
}
Hope that clarifies it a bit :)
Ugh, I was hoping to never see the word eigenvalue ever again, thanks! ;) Great post though. Just don't mention vector spaces, and we'll be fine. hehe
Answer by Soraphis · Dec 14, 2015 at 12:14 PM
Vector(2, 0) -> normalized = Vector(1, 0)
Vector(a, b) -> SqrMagnitude = a*a + b*b (pythagoras)
Vector(a, b) -> Magnitude = Sqrt(a*a + b*b)
Vector(a, b) -> normalized = 1/Sqrt(a*a + b*b) * Vector(a, b) = Vector(a / Sqrt(a*a + b*b), b / Sqrt(a*a + b*b) )
if you want to check if a vectors length is ... say less then 10:
if( v.Magnitude < 10)
is the same as:
if( v.SqrMagnitude < 100)
So, can I weasel out of the sqrt call by squaring the vector's components, then divide that by its original squared magnitude?
No, you can't. You can easily see this by considering a vector such as (1,2).
Squaring both elements gives you (1,4) which is a different direction to (1,2), and dividing a vector by a scalar does not change its direction. Since all you're proposing doing with these 2 vectors is dividing each of them by a scalar, there's no way that that can result in identical vectors.
Just an extra note, unrelated to the question: the Vector.normalized property computes a temporary value and returns it while the Vector.Normalize() method modifies the vector, and returns the normalized value. So use Normalize() if you need to call .normalized more than once (or assign Vector.normalized to a new variable and use it).
Your answer
Follow this Question
Related Questions
Vector3 values always precalculated, or just in MonoDevelop? 1 Answer
force.normalized * magnitude 1 Answer
how to Break a vector3 line into half and Quater 2 Answers
Rotate object based on hit normal + matching camera direction 0 Answers
How to get vector X distance from point on Y angle? 4 Answers