1.3.1 General Principles

advertisement
1.3 Implementing Matrix Operations in Programming Languages
1.3.1 General Principles
We saw in sections 1.1.2 and 1.1.3 how to solve a system of linear equations Ax = b in Mathematica and
MATLAB. Once the coefficient matrix A and the vector b on the right side have been set up, it was only one
line:
x
=
LinearSolve[A, b]
in Mathematica
x
=
A\b
in MATLAB
This is typical in that many matrix operations can be done easily in either Mathematica or MATLAB. However,
if you are working in C you will probably either have to locate a prewritten function (probably in some library of
mathematical functions) or write a program to do the operation. Even if you are working in Mathematica or
MATLAB there will be times when there is no builtin function to do what you want to do. For this reason, we
are interested in some of the details of implementing various matrix operations in a programming language.
This section has a discussion of some of this.
Creating vectors and matrices. The first thing is how one creates a vector or matrix. In sections 1.1.2 and
1.1.3 we saw how to do that in Mathematica and MATLAB, so we look at how this is done in C. Actually C
supports several different ways to work with vectors and matrices, but we shall look at the most primitive of
these, namely the builtin array data type.
To create an array in C one must first provide a declaration for it. In the declaration one must give the
maximum number of elements the array will hold as well as the type of numbers the array will hold, i.e. arbitrary
real numbers or just integers. To create a one dimensional array b that can hold a vector of up to 7 real numbers
and a two dimensional array a that can hold up to a 77 matrix of real numbers one gives the declarations
float
b[7];
float
a[7][7]
In C, array subscripts start at zero, so the above declaration provides for an array with the elements
b[0], b[1], b[2], b[3], b[4], b[5] and b[6]. Often when we work with vectors in mathematics we assume the
subscripts start at one. Thus in going from mathematics to C we have a choice, either don't use the element of
the C array with subscript zero or shift all the subscripts by one when translating an algorithm into C. I prefer
the first choice, so I will generally not use the element of an array with subscript zero.
One can initialize an array when one declares it as follows.
1.3 - 1
float
b[7] = {0, 12, 2, 4, 15, 5, 7};
float a[7][7] = { {0,
{0,
0,
4, -1,
{0, -1,
{0,
0,
0,
0,
0},
0, -1,
0,
0};
0, -1,
0};
4, -1,
0, -1,
{0, -1,
0,
0,
{0,
0, -1,
{0,
0,
4,
0,
0,
4, -1, -1};
0, -1,
0, -1,
0, -1};
4, -1};
0, -1,
4};
Vector addition. Generally one works with arrays (or vectors or matrices) in the same manner in most
computer languages. The only difference in small details. For example, we would refer to bi by
b[[i]]
in Mathematica
b(i)
in MATLAB
b[i]
in C
So when we write an algorithm which is meant to be implemented in a computer language we will give it in a
form which is convenient for us and leave the some of the details of translating it into some programming
language to the reader. For example, suppose x and y are vectors with n components and we want to calculate
the sum z = x + y. Then an algorithm to do this is
for j = 1 to n

zj = xj + yj
In Mathematica this would be translated as
For[j = 1, j  n, j = j + 1,
z[[j]] = x[[j]] + y[[j]]
]
In MATLAB this would be translated as
for j = 1:n
z(j) = x(j) + y(j)
end
In C this would be translated as
for (j = 1, j<= n, j = j+1)
z[j] = x[j] + y[j];
end
1.3 - 2
Multiplication of two vectors. Suppose a is a row vector and x is a column vector, each with n components.
Then an algorithm to compute s = ax is
s=0
for j = 1 to n

s = s + ajxj
Multiplication of a matrix and a vector. Suppose A is an mn matrix and x is a column vector with n
components. Then an algorithm to compute y = Ax is
for j = 1 to m
|
yj = 0
|
for k = 1 to n


yj = yj – ajkxk
Multiplication of two matrices. Suppose A is an mn matrix and and B is an np matrix. Then an algorithm
to compute C = AB is
for j = 1 to m
|
for t = 1 to p
|
|
cjp = 0
|
|
for k = 1 to n


|
cjp = cjp – ajkbkp
The following section has a C program to illustrate the implementation of some of these operations in C.
1.3 - 3
Download