You are on page 1of 3

9 Introduction

Given two matrices, it is very easy to calculated the product of these two matrices either
by hand or by using a calculator. But when we have a chain of matrices and we want
to calculate the product of these matrices, the task becomes challenging especially when
the order of the matrices is high. With this purpose we develop a FORTRAN program to
calculate the product of n matrices. The methodology is explained below:
Suppose we have a series of matrices,
A1(m _ n),A2(n _ p),A3(p _ q)_ _ _ An􀀀1(i _ k),An(k _ j)
where m _ n is the dimension of matrix A1, n _ p is the dimension of matrix A2 and so
on.
The process starts with the calculation of the product of the first two matrices.
C(m _ p) = A1(m _ n) _ A2(n _ p)
where m _ p is the order of matrix C.
Matrix C is further multiplied with the matrix A3 i.e.,
D(m _ q) = C(m _ p)_ A3(p _ q)
This process of chain multiplication continues until all the matrices are multiplied and
the final product will be a matrix Z of order m _ j, where m is the row of first matrix A1
and j is the column of the last matrix An. i.e.,
Z(m _ j) = A1(m _ n)_ A2(n _ p)_A3(p _ q)_ _ _ An􀀀1(i _ k)_An(k _ j)
The criteria for the multiplication of sequence of matrices is that the column of one matrix
must be equal to the row of the adjecent matrix.
i.e., if A (m _ n) and B (p _ q) are any two matrices, then the multiplication is possible if
and only if n = p.
24
10 Flowchart
The algorithm used for the chain multiplication of matrices is given below:
• Declaration of the various input parameters and functions used in the program
• Assigning the input and output files a name
• Open the input file
• Call the ReadInput function and read the input matrices and their dimensions
• Check the validity of the input parameters and values
• Open the output file else report an opening error
• Calculate the product of first two matrices and store the results in matrix C
• Deallocate matrices A and B
• Call MatrixMult function to calculate the product of further matrices by assigning
the third matrix to matrix A and the matrix C to matrix B. Calculating the product
of these two and storing it in matrix C
The flowchart for multiplication of n matrices is shown below.
25
Start
Declaration/
Initialization
Min. no.
of matrix
>= 2
Error reading matrices Stop
Dim’s of
matrix
valid?
Error message Stop
Allocate memory
to matrix A & B
i<=dimA
j<=dimB Deallocate A & B
C(i,j) = A(i,k)*B(k,j)
i=i+1, j=j+1
k from
3 to n
dimA(1)=dimC(1)
dimA(2)=dimC(2)
Write C(i,j) matrix
Deallocate C
Stop
yes
no
yes
no
yes
no
yes
no
26
11 Program Guide
This section will give an insight into the program to calculate the product of n matrices
(chain multiplication).
• Declaration of global parameters:
This section defines the different parameters used in the program under the main
module. Following are some of the global declarations made in this program:
– Channel numbers: These are the channel numbers assigned to the input and
output files ioin and ioout
– Arrays: Different arrays are defined to store the values of the matrices, Array
A, Array B and Array C
– Dimensions of the arrays: dimA, dimB and dimC
– Return codes: These are the integer values that each function returns to the
main module after having called in the program. iReadMat, iReadMatDim,
iListMat and iMatMult
– Read and Write files: MatInp.inp and MatOut.out, these files are defined
to read the input values from and write the final values into a file respectively.
• Reading Input File:
This section reads the values from the input file and and defines the different criteria
required for matrix multiplication.
– Minimum matrix requirement: This sections specifies the minimum number
of matrices required for multiplication. If the number of given matrices is less
than this value the program is terminated.
– Dimension of matrix: This section checks the dimensions of the given matrices.
If the dimensions are invalid the program is stopped.
– Matrix multiplication criteria: Given two matrices A and B with dimensions
mxn and pxq respectively, the multiplication of these two matrices is possible
only if n=p. The program checks this criteria for successive multiplication of
these matrices.
– Memory allocation: Once the criteria for matrix multiplication is checked, the
program then allocates memory to the arrays containing the matrix values.
• Calculate the product of matrices:
This section of the program performs the chain multiplication of matrices and returns
the final product in the form of matrix C.
– Call the matrix multiplication: Once the memory is allocated to the first two
matrices i.e., Matrix A and Matrix B, the program then calls the function
iMatMult to calculate the product.
27

You might also like