# Caveman Normalization

This topic is 5384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm sure I'm about to be heavily laughed at, but here goes anyways. I'm trying to understand why a square root is needed to normalize a vector. When I normalize using the square root of the sum of sqaures, I get values that are unequal to 1. They are close to 1, but always off a bit. Is a unit 3-dimensional vector's length not quite the sum of the absolute axes? In other words, is abs(x) + abs(y) + abs(z) the length? If not, what is happening with sqrt(x * x + y * y + z * z) that makes the length correct? I did a test with a brute force negative check:
FLOAT x=56.0f, y=40.0f, z=-20.0f;
UINT loops = 0x00FFFFFF;
while(loops--)
{
res = 1.0f / ( ( (x < 0.0f) ? -x : x ) + ( (y < 0.0f) ? -y : y ) + ( (z < 0.0f) ? -z : z ) );
//	res = 1.0f / ( abs(x) + abs(y) + abs(z) );
//	res = 1.0f / sqrtf((x * x) + (y * y) + (z * z));

x *= res;
y *= res;
z *= res;
}


The sqrtf method takes about 5 seconds to execute this, and the caveman (likely incorrect) method takes less than 1 second. Using the abs() functions instead of conditional checks takes around 13 seconds [lol]. The sum of absolute axes is always exactly 1.0 with the negative checks. The compiler may also be doing something that speeds up the negative checks. Hopefully not. Can anyone explain what is incredibly stupid about what I'm doing here? I'm sure there are several things [smile] Thanks! [Edited by - Jiia on October 16, 2004 10:40:11 PM]

##### Share on other sites
I'm sure you are after the Euclidean length, which is indeed

euc = sqrt(x*x+y*y+z*z);

"In other words, is abs(x) + abs(y) + abs(z) the length?"
Not the Euclidean length.

Note:

sqrt(x*x+y*y+z*z) does not equal abs(x)+abs(y)+abs(z)

"When I normalize using the square root of the sum of sqaures, I get values that are unequal to 1..."
When you find the length of the normalised vector it should be equal to one.

Not quite sure what your problem is...?

##### Share on other sites
Quote:
 Original post by KuladusNot quite sure what your problem is...?

You mean I'm an idiot / nutcase and there is little help for me? [lol]

I didn't really have a problem, other than being confused. The idea of a unit vector was throwing me off. I'm thinking unit = 1. But it's one distance point, not that the sum is equal to 1.

So the actual distance from vector(0,0,0) to vector(5,5,5) is not 15, it's around 8.5 or so, and my sum normalization version is way off. Actually, I must be having a really bad day to think this was right.

Thanks for trying to make sense of my half-unbaked idea [smile]

##### Share on other sites
Not completely half-baked :)

What you were describing is the L1 vector norm (whereas the Euclidean distance is the L2 norm.)

##### Share on other sites
Quote:
 Original post by KuladusNot completely half-baked :)

[lol] Perhaps a quarter-baked?

Quote:
 What you were describing is the L1 vector norm (whereas the Euclidean distance is the L2 norm.)

Only by coincidence. I really had no idea what I was babbling about [smile] Are there actually uses for this L1 norm? Using this as a direction would push objects more slowly in diagonal directions, right?

Thanks again.

##### Share on other sites
Why sqrt(x*x+y*y+z*z) is a length of vector in Euclidean space :

***********************************
abs(x) + abs(y) + abs(z)
is, IIRC, called Manhattan distance.
In 2D it's
 0 * **   *  *****X,Y

it's number of stars in such ascii path.(note that number of stars doesn't depend to path as long as you don't go back.)
It's not equal to sqrt(x*x+y*y+z*z)

And as about making it be unit-length. If you normalize using /(abs(x) + abs(y) + abs(z)) you make vector be placed on surface of "unit octohedron", surface where (abs(x) + abs(y) + abs(z))=1
Using max(abs(x) , abs(y) , abs(z)) you make it be placed on surface of unit cube, surface where max(abs(x) , abs(y) , abs(z))=1

Using sqrt(x*x+y*y+z*z) , on unit sphere, surface where sqrt(x*x+y*y+z*z)=1

(this =1 will only work for formules that works like normalization - if you normalize once, or normalize twice,result is the same)

**********

if in some space(euclidean or not) you have some norm of vector given by L(x,y,z) , and L(k*x,k*y,k*z)=k*L(x,y,z) , normalization
can be done by nvector=vector*(1/L(vector)) , so we have
L(nvector)=L(vector*(1/L(vector)))=L(vector)*(1/L(vector))=1

and if L(something)=1 , something is said to be unit-length... err, unit-norm, normalized.

##### Share on other sites
Why is that a stupid question? It's just knowledge. You have it or you haven't, nothing wrong about that.

To put it simply, you know vector coordinates relates to an orthogonal frame (right angle), where one axis goes up, one goes right.

So, when you trace a segment from the origin to a point (x, y), you can see it forms a right angle triangle. Pythagoras' theorem states that the square length of the hypothenuse (the segment, or the side of the triangle opposite the right angle), is equal to the sum of the square lengths of the two other sides of the triangle.

C2 = A2 + B2

C is the length of the segment (which is the hypothenuse), and A and B are the coordinates of the vector along the axes of the frame.

in vector terms, L is the vector, (x, y) it's coordinates.

L2 = x2 + y2

so |L| = sqrt(x2 + y2)

Pythagorean Theorem

##### Share on other sites
jeez 44 different proofs of the theorem!! Some very exotic :)

##### Share on other sites
and this is quite funny.

Disproof of the Pythagoras' Theorem

Which is your solution :)

##### Share on other sites
yours is called the Manhattan distance. I think you can guess why :)

• 21
• 13
• 9
• 17
• 13