public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
* row major order versus column major order
@ 2000-07-20 14:25 E. Robert Tisdale
  0 siblings, 0 replies; 3+ messages in thread
From: E. Robert Tisdale @ 2000-07-20 14:25 UTC (permalink / raw)
  To: gsl-discuss

I wrote:

> The only difference between multidimensional Fortran and C arrays
> is the order in which the subscripts appear.
> Fortran multidimensional array subscripts appear in order
> from least to most significant and C multidimensional array subscripts
> appear in reverse order from most to least significant so that
>
>         A(i1, i2, ..., iN) == A[1+iN]...[1+i2][1+i1]

But I should have written

        A(1+i1, 1+i2, ..., 1+iN) == A[iN]...[i2][i1]

or

        A(i1, i2, ..., iN) == A[iN-1]...[i2-1][i1-1]

I can get it right if I try all the different combinations;-)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* row major order versus column major order
@ 2000-07-20  1:00 E. Robert Tisdale
  0 siblings, 0 replies; 3+ messages in thread
From: E. Robert Tisdale @ 2000-07-20  1:00 UTC (permalink / raw)
  To: gsl-discuss

The only difference between multidimensional Fortran and C arrays
is the order in which the subscripts appear.
Fortran multidimensional array subscripts appear in order
from least to most significant and C multidimensional array subscripts
appear in reverse order from most to least significant so that

        A(i1, i2, ..., iN) == A[1+iN]...[1+i2][1+i1]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* row major order versus column major order
@ 2000-07-19 23:11 E. Robert Tisdale
  0 siblings, 0 replies; 3+ messages in thread
From: E. Robert Tisdale @ 2000-07-19 23:11 UTC (permalink / raw)
  To: gsl-discuss

The only difference between multidimensional Fortran and C arrays
is the order in which the subscripts appear.
Fortran multidimensional array subscripts appear in order
from least to most significant and C multidimensional array subscripts
appear in reverse order from most to least significant so that

        A(i1, i2, ..., iN) == A[iN]...[i2][i1]

which means that multidimensional Fortran and C arrays
appear to be transposes of each other.  Both Fortran and C programmers
interpret one dimensional arrays of numbers as vector objects and
interpret two dimensional arrays of numbers as matrix objects and
both Fortran and C programmers interpret the first and second subscripts
of a two dimensional array as row and column indices respectively
so two dimensional Fortran and C arrays are said to be stored
in column major order and row major order respectively
even if both Fortran and C programs refer to exactly the same object.

Fortran programmers naturally tend to think of one dimensional arrays
as column vectors and two dimensional arrays as collections of column vectors.
C programmers naturally tend to think of one dimensional arrays
as row vectors and two dimensional arrays as collections of row vectors.
Fortran programs can call C vector and matrix functions and
C programs can call Fortran vector and matrix subroutines
but the operation may be interpreted differently.
Suppose, for example, that a Fortran subroutine implements
a matrix-vector multiplication

        Ax

where A is an m by n matrix and x is an n element column vector.
C programmers interpret the Fortran subroutine as an implementation
of a vector-matrix multiplication

        x^TA^T = vM

where v = x^T is an n element row vector and M = A^T is an n by m matrix
but both x and v reference the same vector object in memory
and both A and M reference the same matrix object in memory.

A Fortran subroutine which computes an LDU decomposition

        PA = L(DU)

appears to the C program as

        A^TP^T = (DU)^TL^T

which the C programmer might interpret as

        AP = (LD)U

Similarly, a C function which computes and LDU decomposition

        PA = L(DU)

appears to the Fortran programmer as

        AP = (LD)U

A really good linear algebra subroutine library
would support both decompositions
but Fortran favors the most natural implementation of AP = (LD)U
and C favors the most natural implementation of PA = L(DU)
because they minimize cache misses due to striding.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2000-07-20 14:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-20 14:25 row major order versus column major order E. Robert Tisdale
  -- strict thread matches above, loose matches on Subject: below --
2000-07-20  1:00 E. Robert Tisdale
2000-07-19 23:11 E. Robert Tisdale

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).