* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download Linear Algebra
Rotation matrix wikipedia , lookup
Linear least squares (mathematics) wikipedia , lookup
Principal component analysis wikipedia , lookup
Jordan normal form wikipedia , lookup
Determinant wikipedia , lookup
Eigenvalues and eigenvectors wikipedia , lookup
Matrix (mathematics) wikipedia , lookup
Perron–Frobenius theorem wikipedia , lookup
Four-vector wikipedia , lookup
Singular-value decomposition wikipedia , lookup
Orthogonal matrix wikipedia , lookup
Non-negative matrix factorization wikipedia , lookup
Ordinary least squares wikipedia , lookup
System of linear equations wikipedia , lookup
Cayley–Hamilton theorem wikipedia , lookup
Matrix calculus wikipedia , lookup
6.3 Programming Details
double â âA
163
=â double â A[0 . . .3]
A[0]
A[0][0] A[0][1] A[0][2] A[0][3]
A[1]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2]
A[2][0] A[2][1] A[2][2] A[2][3]
A[3]
A[3][0] A[3][1] A[3][2] A[3][3]
Fig. 6.2 Conceptual representation of the allocation of a matrix in C++.
First of all, when we think of an n à n matrix in Fortran and C++, we typically would
have a mental picture of a two-dimensional block of stored numbers. The computer stores
them however as sequential strings of numbers. The latter could be stored as row-major
order or column-major order. What do we mean by that? Recalling that for our matrix elements ai j , i refers to rows and j to columns, we could store a matrix in the sequence
a11 a12 . . . a1n a21 a22 . . . a2n . . . ann if it is row-major order (we go along a given row i and pick up all
column elements j) or it could be stored in column-major order a11 a21 . . . an1 a12 a22 . . . an2 . . . ann .
Fortran stores matrices in the latter way, i.e., by column-major, while C++ stores them
by row-major. It is crucial to keep this in mind when we are dealing with matrices, because
if we were to organize the matrix elements in the wrong way, important properties like the
transpose of a real matrix or the inverse can be wrong, and obviously yield wrong physics.
Fortran subscripts begin typically with 1, although it is no problem in starting with zero, while
C++ starts with 0 for the first element. This means that A(1, 1) in Fortran is equivalent to
A[0][0] in C++. Moreover, since the sequential storage in memory means that nearby matrix
elements are close to each other in the memory locations (and thereby easier to fetch) ,
operations involving e.g., additions of matrices may take more time if we do not respect the
given ordering.
To see this, consider the following coding of matrix addition in C++ and Fortran. We have
n à n matrices A, B and C and we wish to evaluate A = B + C according to Eq. (6.2). In C++
this would be coded like
for(i=0 ; i < n ; i++) {
for(j=0 ; j < n ; j++) {
a[i][j]=b[i][j]+c[i][j]
}
}