164 6 Linear Algebra
while in Fortran we would have
DOj=1, n
DOi=1, n
a(i,j)=b(i,j)+c(i,j)
ENDDO
ENDDO
Fig. 6.3 shows how a 3 × 3 matrixAis stored in both row-major and column-major ways.
a 11 a 12 a 13
a 21 a 22 a 23
a 31 a 32 a 33
⇐= =⇒
a 11
a 12
a 13
a 21
a 22
a 23
a 31
a 32
a 33
a 11
a 21
a 31
a 12
a 22
a 32
a 13
a 23
a 33
Fig. 6.3Row-major storage of a matrix to the left (C++ way) and column-major to the right (Fortran way).
Interchanging the order ofiand jcan lead to a considerable enhancement in process
time. In Fortran we write the above statements in a much simpler waya=b+c. However, the
addition still involves∼n^2 operations. Matrix multiplication or taking the inverse requires
∼n^3 operations. The matrix multiplication of Eq. (6.4) of two matricesA=BCcould then take
the following form in C++
for(i=0 ; i < n ; i++){
for(j=0 ; j < n ; j++){