Linear Algebra and Its Applications, exercise 1.4.9

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)

and

   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

b_i = \sum_{j=1}^{j=n} a_{ij} x_j

for a given i in the inner loop. Since i is constant for a_{ij} 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.

This entry was posted in linear algebra. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s