Exercise 1.4.9. Given the following two examples of FORTRAN code
DO 10 I=1,N DO 10 J=1,N 10 B(I) = B(I) + A(I,J)*X(J)
DO 10 J=1,N DO 10 I=1,N 10 B(I) = B(I) + A(I,J)*X(J)
Do they multiply Ax by rows and columns?
Answer: The first code sample computes
for a given i in the inner loop. Since i is constant for in the inner loop, the sum is for all entries in row i of A. The outer loop iterates over i, so the multiplication is done by rows.
The second code sample holds j constant in the inner loop and iterates over the entries of A in column j. The outer loop iterates over j, so the second sample is multiplying by columns.
NOTE: This continues a series of posts containing worked out exercises from the (out of print) book Linear Algebra and Its Applications, Third Edition by Gilbert Strang.
If you find these posts useful I encourage you to also check out the more current Linear Algebra and Its Applications, Fourth Edition, Dr Strang’s introductory textbook Introduction to Linear Algebra, Fourth Edition and the accompanying free online course, and Dr Strang’s other books.