Row major or column major matrices?

Row major or column major matrices?

Post by Eric Hain » Sun, 24 Jan 1993 02:42:48



I feel vaguely like I'm in a twilight zone episode.  You know, like the ones
where some guy wakes up and finds that all the lettering is backwards and
everyone is left-handed.

Someone was telling me that OpenGL allows only post-concatenation of matrices.
This seemed bass-ackwards, and after a few minutes reconfirming in my slow
turtle brain that you'd normally want pre-concatenation of matrices, I read
over the OpenGL manual page carefully.  Aha, they store matrices in
column-major form:

        a0   a4   a8   a12      a typical       1  0  0 -2
        a1   a5   a9   a13      translation     0  1  0  5
        a2   a6   a10  a14      matrix -->   0  0  1  3
        a3   a7   a11  a15                      0  0  0  1

Me, I learned on my pappy's knee (well, actually, through Newman & Sproull) to
use row major matrices.  Up until today using column major matrices never
crossed my mind.  Anywell, ideas like "we only post-concatenate column major
matrices" translates into "we only pre-concatenate row major matrices".
Lovely, a new way to confuse computer graphics people (as if left vs.
right-handed coordinate systems weren't bad enough).  This is not a slam of
OpenGL - Phigs uses column major matrices, too!

It's admittedly a minor point, and really just a notational difference - the
matrices are just the same, of course.  As long as you get the matrix elements
in the right order it doesn't matter to the computer.  It's just writing it
down on paper and presenting it is where the difference appears.

Just out of curiosity, I looked at a few computer graphics books and whatnot
and how they presented 3D matrices.

Row Major users:

        Foley & VD (1st edition), Newman & Sproull, Graphics Gems series,
        Rogers books, Watt, Burger & Gillies, the Thalmanns, HP Starbase

Column Major users:

        Foley & VD & F & H (2nd edition), OpenGL, Phigs (and PEX, I assume)

So Foley et al is the only book I could find that uses column-major matrices.
Weirder yet, the first edition of Foley & VD uses the row-major form, but then
the second edition goes to column-major form!  Evidently this book's second
edition made quite an impression (or maybe just reflects some new teaching
methods present at the time), since both OpenGL and PEX adopted it, in
contrast with the rest of the research and academic community who (seem to)
use row-major matrices.

If you made it this far, take a minute and hit that "r" key and let me know
which form (if either) you prefer, where you learned it, and any other
insights you might have.  I'm interested to see if there's any correlation
with school, or age, or whatever.  I'll summarize to the net.

Thanks, and not that I'm biased or anything, but * and death to all
column-major users,

Eric Haines

 
 
 

Row major or column major matrices?

Post by Jeff Bu » Sun, 24 Jan 1993 09:09:30


I have no strong preference for either, but it would make my life
much esier if real-world systems could *choose one and stick with it*!
I recently had days of unnecessary skullsweat grafting OpenGL
evaluator onto a utility library that used the opposite ordering.

On a related note...
I learned most of my 3d graphics in left-handed coordinates. When
z increases into the screen, the perspective transformation falls
out trivially from an extra 1 in the viewing matrix.
(F&VD comes to mind as an example). Yet PEX insists on right-handed
coordinates, and PEX is the nearest thing to an existing standard in
the industrial world. (Arguably brain-dead, but there's no use in
getting into that.) Anyway, more nearly universal consistency would be appreciated here too.

Flame off...

--
        dr memory


 
 
 

Row major or column major matrices?

Post by Dave Ciemiewi » Tue, 26 Jan 1993 08:09:19


|> I have no strong preference for either, but it would make my life
|> much esier if real-world systems could *choose one and stick with it*!
|> I recently had days of unnecessary skullsweat grafting OpenGL
|> evaluator onto a utility library that used the opposite ordering.
|>
|> On a related note...
|> I learned most of my 3d graphics in left-handed coordinates. When
|> z increases into the screen, the perspective transformation falls
|> out trivially from an extra 1 in the viewing matrix.
|> (F&VD comes to mind as an example). Yet PEX insists on right-handed
|> coordinates, and PEX is the nearest thing to an existing standard in
|> the industrial world. (Arguably brain-dead, but there's no use in
|> getting into that.) Anyway, more nearly universal consistency would be appreciated here too.
|>
|> Flame off...

There is a really nice reason to make the coordinates use a right-handed
coordinate system: the vector algebra taught in physics and engineering
assumes that world is "right-handed", if I remember things correctly.
I seem to remember the torque vector following a right-handed rule, as
one example.

If the choices made match the "physical" world rather than some implementation
constraint, it makes things much easier for the scientists and engineers to
do their work with the model presented.

--

    __   * __   _  __  ___                      
   /  \ / / /  / \/  \/   \     He was a man like any other man, however, not
  /    /  \/  /  /\  /    /     quite like any other man.
  \___/\__/\_/  /_/ / \__/      
                   *            

 
 
 

Row major or column major matrices?

Post by Ron Levi » Tue, 26 Jan 1993 07:55:47



>I feel vaguely like I'm in a twilight zone episode.  You know, like the ones
>where some guy wakes up and finds that all the lettering is backwards and
>everyone is left-handed.

>Someone was telling me that OpenGL allows only post-concatenation of matrices.
>This seemed bass-ackwards, and after a few minutes reconfirming in my slow
>turtle brain that you'd normally want pre-concatenation of matrices, I read
>over the OpenGL manual page carefully.  Aha, they store matrices in
>column-major form:

>    a0   a4   a8   a12      a typical       1  0  0 -2
>    a1   a5   a9   a13      translation     0  1  0  5
>    a2   a6   a10  a14      matrix -->   0  0  1  3
>    a3   a7   a11  a15                      0  0  0  1

>Me, I learned on my pappy's knee (well, actually, through Newman & Sproull) to
>use row major matrices.  Up until today using column major matrices never
>crossed my mind.  Anywell, ideas like "we only post-concatenate column major
>matrices" translates into "we only pre-concatenate row major matrices".
>Lovely, a new way to confuse computer graphics people (as if left vs.
>right-handed coordinate systems weren't bad enough).  This is not a slam of
>OpenGL - Phigs uses column major matrices, too!

>It's admittedly a minor point, and really just a notational difference - the
>matrices are just the same, of course.  As long as you get the matrix elements
>in the right order it doesn't matter to the computer.  It's just writing it
>down on paper and presenting it is where the difference appears.

>Just out of curiosity, I looked at a few computer graphics books and whatnot
>and how they presented 3D matrices.

>Row Major users:

>    Foley & VD (1st edition), Newman & Sproull, Graphics Gems series,
>    Rogers books, Watt, Burger & Gillies, the Thalmanns, HP Starbase

>Column Major users:

>    Foley & VD & F & H (2nd edition), OpenGL, Phigs (and PEX, I assume)

>So Foley et al is the only book I could find that uses column-major matrices.
>Weirder yet, the first edition of Foley & VD uses the row-major form, but then
>the second edition goes to column-major form!  Evidently this book's second
>edition made quite an impression (or maybe just reflects some new teaching
>methods present at the time), since both OpenGL and PEX adopted it, in
>contrast with the rest of the research and academic community who (seem to)
>use row-major matrices.

>If you made it this far, take a minute and hit that "r" key and let me know
>which form (if either) you prefer, where you learned it, and any other
>insights you might have.  I'm interested to see if there's any correlation
>with school, or age, or whatever.  I'll summarize to the net.

>Thanks, and not that I'm biased or anything, but * and death to all
>column-major users,

>Eric Haines

Eric, I believe that your note is confusing two different
conventional distinctions.  

"Row-major" and "column-major", as I have understood the terms for
years, refer to how the matrices are stored in memory, not to how they
are used to represent transformations. That is, the terms distinguish
between the Fortran convention, in which the first subscript varies
most rapidly as memory address increases, and the C convention, in
which the second subscript varies most rapidly as memory address
increases.  Although your note refers to how matrices are stored, I
think you are really concerned with quite a different distinction,
namely, whether the matrix represents a linear transformation as a
left multiplier of column vectors or as a right multiplier of row
vectors.  Mathematicians sometimes distinguish these two conventions
as the "pre-fix" and "post-fix" conventions.

Mathematicians have long been split into two camps, between pre-fix
and post-fix preferences.  Geometers and analysts have tended more to
the pre-fix convention--thus you learned in calculus that f(x) denotes
the value of a function f for a domain element x. Then, if you compose
two functions, (gf)(x) means g(f(x)), that is, first apply f then
apply g.  This corresponds to interpreting a matrix as a left
multiplier of column vectors.

But some schools of abstract algebraists logicians, and some authors
of computer graphics books, have insisted on the post-fix (x)f or xf.  
I always work in the pre-fix convention when I have a choice, but this
is only because in the past most of my math professors and books have
used it and so it is a little more natural to me.  I especially prefer
it in computer graphics, which is inspired more by geometry than by
abstract algebra.

But the two conventions are completely equivalent, and one should have
no difficulty in using either.  Of course it is important to know
which convention is used by any particular system with which you are
working.  And of course, the matrices may be stored in row-major or
column-major order for either convention of writing functional
expressions; that distinction tends to depend on the programming
language.

Ron

 
 
 

Row major or column major matrices?

Post by Laurence James Edwar » Tue, 26 Jan 1993 10:34:50


|> I feel vaguely like I'm in a twilight zone episode.  You know, like the ones
|> where some guy wakes up and finds that all the lettering is backwards and
|> everyone is left-handed.
|>
|> Someone was telling me that OpenGL allows only post-concatenation of matrices
|> [...]
|> Me, I learned on my pappy's knee (well, actually, through Newman & Sproull) to
|> use row major matrices.  Up until today using column major matrices never
|> crossed my mind.  Anywell, ideas like "we only post-concatenate column major
|> matrices" translates into "we only pre-concatenate row major matrices".
|> Lovely, a new way to confuse computer graphics people (as if left vs.
|> right-handed coordinate systems weren't bad enough).  This is not a slam of
|> OpenGL - Phigs uses column major matrices, too!
|> [...]

It's even more surprising since the current version of GL normally
pre-concatenates. I can imagine some hard to find bugs when people port old GL
code in which they generate their own matrices from scratch. I'm not too upset
though, cause column vectors and post-concatenation is what I learned as an
engineer, and so it seems more natural to me. Note, that pre-concatenation
with column vectors or post-concatenation with row vectors is useful. In this
one instance I prefer PHIGS, which has a direct mechanism for doing both pre- &
post-concatenation of matrices ... you can post-concatenate in GL but only
indirectly and inefficiently.

According to the GL manual, GL uses row vectors and pre-multiplication for
purely historical reasons related to the efficiency of early hardware.

Larry Edwards

 
 
 

Row major or column major matrices?

Post by George Kyriaz » Tue, 26 Jan 1993 10:43:43



>I feel vaguely like I'm in a twilight zone episode.  You know, like the ones
>where some guy wakes up and finds that all the lettering is backwards and
>everyone is left-handed.

I feel like I am in the same episode whenever I talk about matrix concatenation
and (even worse) PHIGS-like view specification (view mapping and view
orientation).

Quote:>Someone was telling me that OpenGL allows only post-concatenation of matrices.
>This seemed bass-ackwards, and after a few minutes reconfirming in my slow
>turtle brain that you'd normally want pre-concatenation of matrices, I read
>over the OpenGL manual page carefully.  Aha, they store matrices in
>column-major form:

>    a0   a4   a8   a12      a typical       1  0  0 -2
>    a1   a5   a9   a13      translation     0  1  0  5
>    a2   a6   a10  a14      matrix -->   0  0  1  3
>    a3   a7   a11  a15                      0  0  0  1

Hmm...  I would feel confortable if your matrix was numbered like:

        a00     a01     a02     a03
        a10     a11     a12     a13
        a20     a21     a22     a23
        a30     a31     a32     a33

Using this numbering, you don't really care if it's row-major and column-major.
Well, knowing the way C implements things, you'll end up with row-major
finally, but who cares...

My point is that using row vectors, your X translation will end up at a30,
while using column vectors it will end up at a03.  You still have one 0 and
one 3.  With your numbering you have a 3 instead of a 12, which is a bigger
difference.  

Row-vectors vs. column vectors really mess you up sometimes though:

"wait!  This is not a perspective divide!  It's a translation!!"

or even worse

"Why on earth IBM GL on the GTO restricts the translation vector to be
a multiple of the 3rd column?  Wait!  That's the perspective calculations!
Duh!"

Quote:>If you made it this far, take a minute and hit that "r" key and let me know
>which form (if either) you prefer, where you learned it, and any other
>insights you might have.  I'm interested to see if there's any correlation
>with school, or age, or whatever.  I'll summarize to the net.

Well, I wish I remember how I got started into column vectors, but they
always made more sense to me, from algebra's point of view, anyway.
ie. ABx (where x is a vector, and A & B matrices) makes more sense to me
than xBA.  Even in cases that I have to use matrices that correspond to
row-vectors, I do it my own way and transpose in the end.  Ok, so it's
slower..  So, sue me!

Also, I think that if you are using your main processor to do matrix-vector
multiplication, using column vectors is faster (please beat me on the head
if I am wrong).  I bet that's why OpenGL switched to column vectors...
Plain old GL used the hardware to do the multiplications, so it's just a
DMA transfer to graphics pipe, while OpenGL has to provide the same
functionality, no matter what the hardware is.  So, in order to make the
software mat-vec faster, they use col vectors.

Additionally, if you work with C++ and you use operator overloading,
you have more flexibility in the implementation if the matrix-vector
multiplication is owned by the matrix class instead of the vector class..  
(which is the case with col vectors).

Hope that gives some insight.

  --george
--
----------------------------------------------------------------------------
George Kyriazis       | Visual Technologies Program, Rensselaer Design

----------------------------------------------------------------------------

 
 
 

Row major or column major matrices?

Post by Tom Koo » Wed, 27 Jan 1993 01:14:00


Hi All!

Friday January 22 1993, Eric Haines writes to All:

[about column major/row major matrices]

 > If you made it this far, take a minute and hit that "r" key and let me
know
 > which form (if either) you prefer, where you learned it, and any other
 > insights you might have.  I'm interested to see if there's any correlation
 > with school, or age, or whatever.  I'll summarize to the net.

I prefer the column major... Mainly because I learned matrices from Foley, van
Dam, Feiner and Hughes (ie 2nd edtion) ...

CU>Tom

 
 
 

Row major or column major matrices?

Post by Laurence James Edwar » Wed, 27 Jan 1993 10:53:41




|> |> I feel vaguely like I'm in a twilight zone episode.  You know, like the ones
|> |> where some guy wakes up and finds that all the lettering is backwards and
|> |> everyone is left-handed.
|> |>
|> |> Someone was telling me that OpenGL allows only post-concatenation of matrices
|> [...]
|>
|> It's even more surprising since the current version of GL normally
|> pre-concatenates. I can imagine some hard to find bugs when people port old GL
|> code in which they generate their own matrices from scratch.

Knowledgeable sources inside SGI inform me that the OpenGL spec's are just
written using column vectors and post-concatenation because of the notational
preferences of the author(s) ... the actual hardware will still be doing
pre-concatenations. Actually, this is clear from what Eric said. As Eric
indicated, in the OpenGL spec they show matrices stored in column major order,
whereas in reality C stores matrices in row major order, so effectively what
they've done in the diagrams is transpose the matrices ... hmmm I can think of
less confusing ways to write a spec.

Larry Edwards

 
 
 

Row major or column major matrices?

Post by John Scott Peter XXXI » Wed, 27 Jan 1993 09:22:10


Well, I eagerly await your agent of *....

Actually, I have no clear preference, and I'll explain why.  In all my
education (math and physics, B.S Physics UCLA 1986), only row-major matrices
were used, so I naturally think of that as the standard.  However, I have an
idea as to why so many people are switching to column-major notation.  It's
so that matrices can be post-multiplied instead of pre-multiplied!  Post-
multiplication notation accords better with the compound operators of C and
C++.  If you're writing a matrix class in C++, and you want to multiply one
matrix by another, it seems natural to define the operator `*=' as the
multiplication operator, such that:

    A *= B means A = A * B.

I was writing such a class for a simple 3-D graphics program, and I quickly
realised that the most common multiplication I wanted to do was PRE-
multiplication.  I therefore said `Screw convention', and defined `*=' so
that

    A *= B means A = B * A.

So, my solution was to change the standard C relationship between `*=' and
`*'.  Apparently, others decided it was better to change the very operation
of matrix multiplication to imply a column-major order, so that `A *= B'
would still mean `A = A*B'.

--
John Scott Peter XXXIII  // "Instantiating new solutions in the emerging arena

 
 
 

Row major or column major matrices?

Post by Christopher James Philli » Wed, 27 Jan 1993 11:36:39



Quote:>On a related note...
>I learned most of my 3d graphics in left-handed coordinates. When
>z increases into the screen, the perspective transformation falls
>out trivially from an extra 1 in the viewing matrix.
>(F&VD comes to mind as an example). Yet PEX insists on right-handed
>coordinates, and PEX is the nearest thing to an existing standard in
>the industrial world. (Arguably brain-dead, but there's no use in
>getting into that.) Anyway, more nearly universal consistency would be appreciated here too.

Well I learned most of my _2d_ graphics with x increasing to the right, and
y increasing down, so z increasing into the screen gives me a RHS
immediately :)

Christopher Jam.

 
 
 

Row major or column major matrices?

Post by PROF D. Rogers (EAS F » Fri, 29 Jan 1993 03:36:02



!Well, I eagerly await your agent of *....
!
!Actually, I have no clear preference, and I'll explain why.  In all my
!education (math and physics, B.S Physics UCLA 1986), only row-major matrices
!were used, so I naturally think of that as the standard.  However, I have an
!idea as to why so many people are switching to column-major notation.  It's
!so that matrices can be post-multiplied instead of pre-multiplied!  Post-
!multiplication notation accords better with the compound operators of C and
!C++.  If you're writing a matrix class in C++, and you want to multiply one
!matrix by another, it seems natural to define the operator `*=' as the
!multiplication operator, such that:
!
!    A *= B means A = A * B.
!
!I was writing such a class for a simple 3-D graphics program, and I quickly
!realised that the most common multiplication I wanted to do was PRE-
!multiplication.  I therefore said `Screw convention', and defined `*=' so
!that
!
!    A *= B means A = B * A.
!
!So, my solution was to change the standard C relationship between `*=' and
!`*'.  Apparently, others decided it was better to change the very operation
!of matrix multiplication to imply a column-major order, so that `A *= B'
!would still mean `A = A*B'.

To heck with C and C+ and Fortran etc. You'll should really look
at how ANSI BASIC (yes that * word) handles matrices. How about
like a mathematician would? Yup that's the way they do it AND
it is all built into the language! Amazing. When are these modern
langauage designers going to get with it.

Dave Rogers

 
 
 

Row major or column major matrices?

Post by PROF D. Rogers (EAS F » Fri, 29 Jan 1993 03:39:34




!
!!On a related note...
!!I learned most of my 3d graphics in left-handed coordinates. When
!!z increases into the screen, the perspective transformation falls
!!out trivially from an extra 1 in the viewing matrix.
!!(F&VD comes to mind as an example). Yet PEX insists on right-handed
!!coordinates, and PEX is the nearest thing to an existing standard in
!!the industrial world. (Arguably brain-dead, but there's no use in
!!getting into that.) Anyway, more nearly universal consistency would be appreciated here too.
!
!Well I learned most of my _2d_ graphics with x increasing to the right, and
!y increasing down, so z increasing into the screen gives me a RHS
!immediately :)

Well Chris that's because you guys are all upside down! :) :)

On the other hand I sure enjoyed it when I lived `downunder'.

Dave Rogers