Previous: 15.1.1 Matrix Operations: Transforms
Up: 15.1 Matrices
Previous Page: 15.1.1 Matrix Operations: Transforms
Next Page: 15.2 Complex Numbers
Other common manipulations involving matrices require addition of two matrices, multiplication of two matrices, and inversion of matrices. In this section, we will implement matrix addition and matrix multiplication algorithms. Addition of two matrices may arise when two sets of equations relate the same set of variables. For example, consider the matrix equations:
Corresponding equations of the two sets can be added together to obtain a
combined single set:
where, in matrix terms,
Vectors are special cases of rectangular matrices having rows and
column. We will therefore implement a single function that sums two rectangular
matrices. The sum of matrices
and
generates a new matrix, say
. If the
elements of matrix
are a[i][j],
and those of
are b[i][j], then the sum
matrix,
with elements c[i][j], is determined as follows:
c[i][j] = a[i][j] + b[i][j]The implementation of matrix addition is easy, and the code is shown in Figure 15.5.
Multiplication of two matrices and
results when combining two
transformations, i.e. where a vector being transformed by
a matrix
is itself the
result of a transformation by a matrix
.
Consider the following two sets of equations:
Since, by the second equation, equals
,
we can substitute
for
in
the first equation:
Or, the combined equation results in:
The product of matrices and
generates a matrix
. If the number of rows and columns
of
are given by
and
,
and those of B are given by
and
, then, the
number of elements of
represents the number of
columns of
and the number of rows of
,
i.e.
.
Also,
must have the same number of rows
as
and the same number of
columns as
.
That is, the number of rows and columns of
must be
and
. It turns out
that each c[i][j] is a result of a scalar product of
row
of matrix
and column
of matrix
.
Let the
row of
and the
column of
be:
a[i][0] a[i][1] ... a[i][c1 - 1]
b[0][j] b[1][j] ... b[r2 - 1][j]
then, the scalar product, c[i][j], is given by:
a[i][0] * b[0][j] + a[i][1] * b[1][j] + ... + a[i][c1-1] * b[r2-1][j]With this algorithm, the sum is easily implemented as a cumulative sum, initialized to zero. Each pass through the loop adds one product term, a[i][k] * b[k][j], for
c[i][j] = 0;
for (k = 0; k < c1; k++)
c[i][j] += a[i][k] * b[k][j];
Such a loop is repeated for all appropriate
We can now write a simple example that uses the matrix functions
defined above as shown in Figure 15.7. The
program adds and multiplies matrices. To keep the program simple, we assume
square matrices. The program first reads in the size of square matrices, and
then reads in the two matrices. These matrices are added and multiplied, and
the resultant matrices are printed.
A sample session is shown below:
Another important matrix operation is the inversion of a square matrix. An
inverse matrix has the property:
where
is the inverse matrix
and
is a unit matrix with unit diagonal
elements and zero elements elsewhere. The unit matrix has the property:
If
, it follows that
Thus, given the inverse matrix,
the solution to the matrix equation for any is
easily obtained.
Inversion of a matrix is somewhat more complex. An inverse of a matrix can be
obtained by the Gauss-Jordan method - a modified version of the Gauss
elimination method discussed in Chapter .
A good reference [1], for matrix
computational methods as well as other numeric methods, is given at the end of
this chapter.