genMatrixTrace
in GenMath.lib

Matrix Functions        Function List by Category        Alphabetical Function List

Sample Code

int genMatrixTrace(
                                    float *Matrix,        // pointer to nxn Matrix array
                                    int     dimen,           // dimension of Matrix
                                    float *trace)           // pointer to store trace

This function computes the trace of an nxn matrix:

trace = sum {a(i,i)}

There is no array bounds checking performed within the function; the user must ensure that matrices A, B and C are of proper size.  Specifically, all three should be nxn arrays.

The return code is errno, defined in the C standard library (math.h).  This is an integer value
for math errors.

 
Sample Application:
/////////// splMTrc.c for genMatrixTrace Library Function ////////////
//								    //
//	For this sample, a 3x3 matrix is used.  		    //
//								    //
//////////////////////////////////////////////////////////////////////

//////////////////////////// INCLUDES ////////////////////////////////

#include "stdio.h"
#include "genMath.h"

////////////////////////////// MAIN //////////////////////////////////
void main()
{

	//local declarations
	//integers
	int retcode;		// return code for call to genMatrixTrace

	//floats
	float a[3][3];		// matrix A
	float c;		// trace

	//character strings
	char  ch;		//input string to exit

	//intialize the A and B matrices
	a[0][0] = (float)1.234;
	a[0][1] = (float)-.678;
	a[0][2] = (float)10.3;
	a[1][0] = (float)14.004;
	a[1][1] = (float)5.332;
	a[1][2] = (float)0.004;
	a[2][0] = (float)-23.400;
	a[2][1] = (float)0.067;
	a[2][2] = (float)-3.882;

	//compute the trace
	retcode = genMatrixTrace(a,3,c);
	
	//display the result
	
	printf("%s%f%s", "Trace = ",c,"\n\n");

	
	//display closing message and wait for <enter> before exiting
	printf("Press <Enter> to end");
	scanf("%c", &ch);

} // End Main

/////////////////////////////// End splMTrc.c ///////////////////////