Last Ray of Hope Home of Kaluriel Hargrove

31Mar/11Off

iOS Vector and Matrix Math

Matrix Multiply

Multiply two matrices together

float matrix1[16] =
{
	1.f, 2.f, 3.f, 4.f,
	5.f, 6.f, 7.f, 8.f,
	9.f, 10.f, 11.f, 12.f,
	13.f, 14.f, 15.f, 16.f
};
float matrix2[16] =
{
	2.f, 3.f, 4.f, 5.f,
	6.f, 7.f, 8.f, 9.f,
	10.f, 11.f, 12.f, 13.f,
	14.f, 15.f, 16.f, 17.f
};
float outMatrix[16];
 
vDSP_mmul( &matrix1[0], 1, &matrix2[0], 1, &outMatrix[0], 1, 4, 4, 4 );

void vDSP_mmul( const float * inInput1Matrix, const int inInput1Stride,
		const float * inInput2Matrix, const int inInput2Stride,
		float * outOutputMatrix, const int inOutputStride,
		const unsigned int inInput1RowCount,
		const unsigned int inInput2ColumnCount,
		const unsigned int inInput1ColumnCountInput2RowCount )
{
	const unsigned int M = inInput1RowCount;
	const unsigned int N = inInput2ColumnCount;
	const unsigned int P = inInput1ColumnCountInput2RowCount;
	const int I = inInput1Stride;
	const int J = inInput2Stride;
	const int K = inOutputStride;
 
	for( int m = 0; m < M; ++m )
	{
		for( int n = 0; n < N; ++n )
		{
			register float v = 0.0f;
 
			for( int p = 0; p < P; ++p )
			{
				v += inInput1Matrix[( m * P + p ) * I] * inInput2Matrix[( p * N + n ) * J];
			}
 
			outOutputMatrix[( m * N + n ) * K] = v;
		}
	}
}

 

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.