genMatrixAdd
in GenMath.lib
Matrix Functions Function List by Category Alphabetical Function List
int genMatrixAdd(
float *MatrixA,
// pointer to matrix A array
float *MatrixB,
// pointer to matrix B array
int dimM,
// m dimension, rows
int dimN,
// n dimension, columns
float *MatrixSum) // pointer
to store product Matrix
This function adds two mxn matrices:
c(i,j) = a(i,j) + b(i,j)
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 arrays with m first dimension(rows),
n second dimension (columns).
The
return code is errno, defined in the C standard library (math.h). This is
an integer value
for math errors.
Sample Application:
/////////// splMtAdd.c for genMatrixAdd Library Function ///////////// // // // For this sample, 3x3 matrices are used. // // // ////////////////////////////////////////////////////////////////////// //////////////////////////// INCLUDES //////////////////////////////// #include "stdio.h" #include "genMath.h" ////////////////////////////// MAIN ////////////////////////////////// void main() { //local declarations //integers int i; // i dimension counter for result int j; // j dimension counter for result display int retcode; // return code for call to genMatrixAdd //floats float a[3][3]; // matrix A float b[3][3]; // matrix B float c[3][3]; // resultant matrix, C //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; b[0][0] = (float)4.559; b[0][1] = (float)3.24; b[0][2] = (float)0.3422;
b[1][0] = (float)-6.669; b[1][1] = (float)0.076; b[1][2] = (float)13.010;
b[2][0] = (float)2.002; b[2][1] = (float)1.009; b[2][2] = (float)-9.007; //add the matrices retcode = genMatrixAdd(a,b,3,3,c); //display the result printf("%s", "Resultant Matrix Elements: \n\n"); for(i=0;i<3;++i){ for(j=0; j < 3; ++j){
printf("%s%d%s%d%s%f%s", "c[",i,"][",j,"] = ", c[i],"\n"); } } //display closing message and wait for <enter> before exiting printf("\nPress <Enter> to end"); scanf("%c", &ch); } // End Main /////////////////////////////// End splMtAdd.c ///////////////////////