Last Ray of Hope Home of Kaluriel Hargrove

10Apr/11Off

iOS Vector and Matrix Math: Part 2

Normalise Vector

Normalises a vector

const int kVectorCount = 3;
float vectorIn[kVectorCount] = { 1.5f, 2.5f, 3.2f };
float vectorOut[kVectorCount];
 
float unitaryNorm = cblas_snrm2( kVectorCount, &vectorIn[0], 1 );
if( unitaryNorm > FLT_EPSILON )
{
	//
	unitaryNorm = 1.0f / unitaryNorm;
 
	//
	for( int i = 0; i < kVectorCount; ++i )
	{
		vectorOut[i] = vectorIn[i] * unitaryNorm;
	}
}
float cblas_snrm2( const int inCount, const float * inInputVector, const int inInputStride )
{
	register float v = 0.0f;
 
	for( int idx = 0; idx < inCount; ++idx )
	{
		//
		v += powf( *inInputVector, 2.0f );
 
		//
		inInputVector += inInputStride;
	}
 
	return sqrtf( v );
}
Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.