San Diego State University logo
ME 203

Module 5.1 - Algebraic Methods - Multi-Dimensional Arrays


All of the programs referred to here and in Module 5.2 can be decompressed from the tar file module5.tar.

And if the following begins to seem a little dry, try not to think of it as Multi-Dimensional Matrix Arithmetic, but instead as Hot Matrix-On-Matrix Action...

Vector-Matrix Multiplication
The product of a Vector V and a Matrix M produces a vector W, and requires that the number of elements in each row of M equal the number of elements in V. The new vector W will have the same number of coumns as M. Each element i of the product vector W is described by the following equation:

Fig 1 - Matrix/Vector multiplication formula at matrix position i

we implement this equation in the program mvp.c. Let's take a closer look at mvp.c:

  • Lines 1, 2 & 3 include the libraries we'll use; stdlib.h provides the function exit() so we can terminate the program gracefully if matrix and vector sizes don't match (Lines 18 thru 24) or the vector is longer than we'll allow for this program (Lines 26 thru 29).
  • Lines 6 thru 10 are our standard variable declarations; we have two different maximum sizes, nm & nv, for matrix and vector sizes, respectively.
    • One difference to our usual variable declarations is line 9:

           char name[100];

      This is something we haven't seen yet; an array of 100 characters. This array (Also known as a string; strings will be covered in detail in Module 10) will store the name of the file that the user types in to store the product vector result in. We size this character array to 100 to make sure that it's more than large enough to handle whatever name the user types in.
  • Notice also that we're using a slightly different conditional statement to make sure that the sizes match in lines 18 thru 24;
    • This is an if-then-else statement; if the sizes are not equal, then exit, else set n to the size of both matrix and vector.
    • This else additon to the conditional statement can come in handy in many occassions.
  • Once we have the matrix and vector size, n, we can now declare the vector, matrix and result variables (Lines 32 thru 34). Notice that the matrix uses n as as both the row size and column size; matrix/vector multiplication doesn't require this to be true.
  • Lines 37 to 39 implements the for loop that reads in the values from the vector file to vector, and lines 42 thru 46 requires a nested for loop to read in all of the values into the matrix mat from the file.
    • Once we've read in all the values into vector and mat from the files fv and fm, we don't need to access them anymore, so we close them in lines 47 & 48.
  • After initializing the result vector via the for loop in lines 51 to 53, we implement the matrix-vector multiplication formula shown above in Figure 1.
    • For future reference, one good heuristic for figuring out what programming construct to use for certain mathematical formulae is that a Summation sign translates very well into a C programming for loop.
    • The summation is implemented as a for loop, but the formula in Figure 1 only takes into account the value at position i of the result, so we'll need to nest this for loop into one that finds for all values of vector, and this leads to the nested for loop that implements the matrix-vector multiplication in lines 56 thru 60.
  • Lines 62 thru 65 query the user for the name of a file to place the vector result into, and define a file pointer with that name for writing in line 65.
  • We need only one for loop to output the result into this file in lines 68 to 70;
  • That's it; we close the output file and terminate the program in lines 71 and 72.

 

Matrix-Matrix Summation
Matrix-Matrix summation is a relatively simple arithmetic operation; each element in a matrix A is multiplied by it's counterpart in array B (obviously, then, the number of elements in each row of array A must equal the number in each row of B, and the number of elements in each column of A must match the number in B as well). The equation for each element in the resultant matrix C is:

Fig 2 - Matrix/Matrix summation for each element Ci,j

The implementation of this arithmetic operation is relatively straightforward as seen in the program mms.c. Let's briefly step through the code:

  • After the include files and variables are declared in lines 1 thru 12, we open the data files matrix1.dat and matrix2.dat (in the data subdirectory) for reading.
  • In lines 16 to 18, the program requests a name to send the result matrix to, and then opens that named file for writing.
  • Lines 21 and 22 scan in the first integer from both matrix files, which provides the size;
  • These matrix sizes must be equal for the summation to take place; the if-then-else statement in lines 23 to 28 exit the program if they are not equal, and sets n to the size if they are.
  • We use n to declare our three 2-dimensional matrices (2 to sum, 1 to contain the result) in lines 30 thru 32.
  • The nested for loops in 34 to 39 extract the matrix values from the files and into mat1 and mat2, then close the files in lines 40 and 41.
  • Lines 44 thru 48 implement the matrix-matrix summation specified in Figure 2 above via a straight-forward nested for loop, no more complex then the nested for loop that loads the matrices above in lines 34 thru 39.
  • The rest of the program is simply administrative;
    • 52 to 59 uses the nested for loop to output the results both to the screen and the output file specified earlier.
    • We close the output file after the loops in line 60, and terminate the program.

Matrix-Matrix Multiplication
In Module 4.2, we saw how to multply 2 one dimensional arrays; the dot product of 2 n-element vectors A & B was defined as

(A[0] * B[0]) + (A[1] * B[1]) + ... + (B[n-1] * B[n-1])

2 dimensional array multiplication builds on this. if we think of each row of array A as a vector, and a column of B as another vector, then we could take their dot product. We would take the ith row of A and multiply it by the ith column of B; to make this work, then, the number of elements in each row of A must match the number of elements in each column of B.

So if C is the matrix generated by the multiplication of A and B, then each element of C is defined by the the equation:

Fig 3 - Matrix-Matrix multiplication formula at result element Ci,j

where n is the number of elements in each row of A and each column of B.

We implement this expression in the program mmp.c, which we describe below.

You'll notice that mmp.c mirrors mvp.c very closely; the only real difference is the nested iteration that implements the multiplication. And if we notice that the vector implementation for mvp.c is like one row in a 2-dimensional matrix, then it becomes clear that the only real adaptation for our multiplication structure will be an extra for loop to cover each row in the matrix.

  • Lines 1 thru 12 are standard fare; we include library files and declare variables. Notice once again that we don't declare our matrices until we've read in their sizes from the files.
  • We assign files to the file pointers (mat1 & mat2 for reading, mato for writing), and exit gracefully if any of the files don't exist in the if-then statement in lines 15 to 21.
  • Lines 23 thru 31 read the first values (The matrix sizes) from mat1.dat and mat2.dat, and use an if-then-else to make sure that they're equal; if not, we exit in line 28.
  • The else portion of the above if-then-else sets n to the matrix size, which we use to define the matrices in lines 32 thru 34.
  • The nested for loops in lines 37 to 43 read the actual matrix values from the files and into our program matrices mat1 and mat2.
  • Lines 46 thru 52 implement the arithmetic expression in Figure 3 above.
    • Notice that this is a doubly-nested for loop, as we discussed; the nested for loop from mvp.c would be the same as one row of our n x n matrix, so we need an extra for loop to implement for the rest of the matrix.
  • The nested for loop of lines 55 to 60 output the contents of the result matrix into the file mato.dat, and
  • We close all arrays and terminate the program in lines 62 thru 65.

Continue to the Apply Section ->

Website by Joshua Bleier