 Welcome to this session in the course of computer programming. In this session we are going to look at some more applications of matrices. We had seen last time that we can use matrices to represent a system of n simultaneous linear equations and we had even written a program to implement the Gaussian elimination method to solve such a system. Today we shall look at two more applications of matrices, one multiplying different matrices and second analyzing magic squares. We will see what both of these things are. First you will remember that matrix multiplication is actually a slightly involved process. Here is an example of a 3 by 4 matrix A which is to be multiplied by another matrix B which is 4 by 2. These matrices are called multiplication compatible matrices when the number of columns in the first matrix is equal to the number of rows in the second matrix. The resultant matrix peculiarly has the same number of rows as the number of rows of the first matrix and the same number of columns as the number of columns in the second matrix. So, if this is how the matrix A looks like, this is how the matrix B looks like, then this is how the matrix C will be. Consider the multiplication operation. The multiplication operation is involved in that every element of the C matrix is actually calculated using several elements of A and several elements of B. Consider for example, the calculation of the first row 0th column element. This is the first row, this is the 0th column element. If I want to calculate the value of this, I will actually have to consider the first row of matrix A and 0th column of matrix B. Let us look at this. This is the row that I will need to consider and this is the column that I need to consider in order to get this value. How do we obtain this? The way you obtain this is you take this A 1 0 multiplied by B 0 0, then you add to it A 1 1 multiplied by B 1 0, then you add to it A 1 2 and multiplied by B 2 0 and finally, you take A 1 3 and multiplied by B 3 0. In short, C 1 0 is nothing but the sub total of this element into this, this element into this, this element into this and this element into this. This we will have to do for each and every element of C. So, notice that by the matrix multiplication operation is involved, logically it is very simple. If I want to calculate the i th row j th column element of C, I need to multiply all elements of i th row of A with the corresponding elements of j th row j th column of B and find the sum that will give me this element. Having kept myself with this, I can now formulate the matrix multiplication problem. If I have an m by n matrix A, an n by p matrix B, then the matrix product C equal to A B will be an m by p matrix and this is nothing but summation over k equal to 0 to n minus 1 A i k multiplied by B k j. This summation will give you the value of C i j. Obviously, one needs to calculate this entire summation for each possible value of i n j, which will actually be m rows and p column matrix. Having understood this, it is a simple matter to write a program. This is a small segment of the program, which implements the main logic of matrix multiplication. The entire program is available as matmult dot c p p on the side. So, I presume that I have read the matrices A and B. Having read the matrices, I set up an iteration to vary i from 0 to m minus 1. Within that, I set up a nested iteration to vary j from 0 to p minus 1. Notice that this iteration will complete all the calculations for m by p elements of the resultant matrix. The resultant matrix C is initialized. That i comma j th element is initialized to 0 and now I have to accumulate in this the sum, which we just saw to be calculated. So, the sigma k equal to 0 to n minus 1 is calculated by this dual loop, where C i j is augmented by a i k into b k j. Once this loop is over, the same thing is to be done for other values of j and for all values of i. When I complete these three nested iterations, I will simply have the resultant matrix and I can output the result matrix C. Notice how simple the program is. We now look at magic squares. Magic square is an interesting square. It is an n by n square in its basic form, which contains unique positive integers. The positive integers are arranged such that the sum of elements of every row and every column is same. Here is an example, 8, 1, 6, 3, 5, 7, 4, 9, 2. Let us look at the sums. 3 plus 5 plus 7 is 15. 8 plus 1 plus 6 is 15. 6 plus 7 plus 2 is also 15. Additionally, there is diagonal. 6 plus 5 plus 4 is also 15. The other diagonal, 8 plus 5 plus 2 is also 15. Very interesting matrix, isn't it? These matrices were known to Chinese. They call them lo shu square. A 3 by 3 magic square was well known to Indians since Vedic time. This is a very ancient observation of such matrix. Today, we simply attempt to solve a problem. We say, if somebody gives me an n by n matrix, which has n rows and n columns, it will have two diagonals. Of course, we want to test whether such a given matrix is a magic square or not. And how do we test it? Well, the algorithm is very clear from the properties of the magic square. First, find out what should be the sum. We are not commented on this, but it so happens that for normal magic square of the size n by n, the sum of each row column and diagonal is n into n star n plus 1 divided by 2. So, if any sum is not equal to this, then this square is not a magic square. So, we calculate sums of rows and columns, calculate the sums of two diagonals, compare each with this value. If it is same, good, the square is a magic square, otherwise not. We will need arrays of size n to store these sums. Observe that I have an n by n matrix. So, number of rows are also n. So, that means I will have a vector of n elements to store row sums and another vector or another array of n elements to store column sums. I will calculate these sums and check if these sums are equal to the required sums. Let us look at this program magic dot c p p. In main, I have a square matrix 20 by 20. I have n as the size i j as the index variable and sum as a variable that I will use for temporarily storing some values. As we decided, we will have an array of 20 elements to store row sums, another array to store column sums. There are only two diagonals independent of the size of the array. Therefore, two simple variables d 1 sum and d 2 sum are good enough. These will store the sums of the two diagonals. I first read the value of n. Next, I read the values of all the elements of the matrix. So, I run a nested iteration for i equal to 0 to n minus 1 and j equal to 0 to n minus 1 and input values i comma j th element of the square. Having that this value, I now initialize my row sum and column sum arrays to 0. They must initially be 0 because I will be accumulating the values in the i th row and values in the j th column or i th column etcetera into these. Let us look at the next part of the program. I calculate the sum for this being n into n magic square. If this input square has to be an n by n magic square, then the sum must be equal to this. This was the purpose of declaring the variable sum. Now, I will find the row sums and check against the required sum. So, here is the way I find the row sum. For i equal to 0 to n minus 1, for j equal to 0 to n minus 1, r sum i plus equal to square i j. Remember that this is nothing but r sum i equal to r sum i plus square i j. What am I doing? I am taking all the elements of i th row and adding them into the i th element of r sum. When I complete this, I would have calculated that sum of the i th row. I compare it. If it does not compare properly, then I simply say it is not a magic square. I need not calculate the remaining sums at all. I can simply return with an error code which incidentally I have used to be 1. If however, the sum is same, I will go back and calculate the same row sum for another row i equal to 1, i equal to 2, etcetera. This part completely checks whether row sums are as required or not. In exactly the same fashion, I can find the column sum and check against the required sum. Here, I vary j from 0 to n minus 1 and for each value of j, I vary i from 0 to n minus 1. I now calculate c sum j plus equal to square i j j. This will calculate the sum of the j th column of the matrix and if this sum is not equal to sum, I again get out. But if it is, then I do the same thing for all the column. Remember that there is no sanctity of putting j here and i here. The question is whether I am iterating over all the columns or not and as long as I appropriately choose these, I can choose i or j or any other variable in any fashion that we want. In fact, one of the important things to learn in handling matrices is to properly handle nested iterations involving a variable varying rows away another variable varying columns. So, we conclude that by this time, if everything is all right, the rows and columns add up to the required number. But wait, we still have to look at the diagonals. To calculate the sums of two diagonals is very easy. You see square i comma i gives me an element on one diagonal for different values of i. Square j n minus j minus 1 will give me the other diagonal. You can verify that the use of such an index j for the row and n minus j minus 1 for the column will actually give you an index which is like this which will cover this area. The first array i equal to 0 to n minus 1 0 0 1 1 2 2 3 3 will cover this diagonal, whereas this will cover the other diagonal. Having found both the sums, I simply check whether each of the sum is same as the given sum or not or required sum or not. If it is not a magic square, I get out same thing with d 2 sum. If everything is all right, if I have reached this point, then the square is indeed a magic square and I declare that the given matrix is a magic square. So, interesting example, we now learnt about the magic squares and we also saw how to manipulate matrices in order to find their row sums, column sums, diagonal sums, etcetera, etcetera. We also wrote a program in this session for multiplying matrices, a simple and elegant program, but which can do lot of computations even for a 100 by 100 matrix. These programs as I mentioned are available in the files matmult.cpp and magic.cpp. Please download and compile these programs and run them with your own input. Thank you very much.