<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The ryg blog]]></provider_name><provider_url><![CDATA[https://fgiesen.wordpress.com]]></provider_url><author_name><![CDATA[fgiesen]]></author_name><author_url><![CDATA[https://fgiesen.wordpress.com/author/fgiesen/]]></author_url><title><![CDATA[Row major vs. column major, row vectors vs. column&nbsp;vectors]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>Row-major vs. column-major is just a storage order thing and doesn&#8217;t have anything to do with what kind of vectors you use. But graphics programmers tend to be exposed to either GL (which uses column-major storage and column vectors) or D3D (which used row-major storage and row vectors in its fixed function pipeline, and still uses that convention in its examples), so they think the two things are tied together somehow. They&#8217;re not. So let&#8217;s disentangle them! But before we start, a small apology: In this article I&#8217;ll be using both &#8220;formula&#8221; and &#8220;code&#8221; notation for variable names, which is a typographic faux pas but necessary because WordPress isn&#8217;t very good at lining up <img src="https://s0.wp.com/latex.php?latex=%5CLaTeX&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5CLaTeX&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5CLaTeX&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;LaTeX" class="latex" /> formulas with body text and I don&#8217;t want to make this unnecessarily hard to read.</p>
<h3>Row/column major</h3>
<p>There&#8217;s no disagreement about how plain arrays are stored in memory: Any programming language that supports a plain (not associative) array type just stores the elements sequentially. Once a language supports multidimensional arrays however, it needs to decide how to squeeze the 2D arrangement of data into a 1D arrangement in memory, typically as an 1D array. One classical use case for multidimensional arrays are matrices:</p>
<p>Given a Matrix <img src="https://s0.wp.com/latex.php?latex=A+%3D+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%26+a_%7B12%7D+%26+a_%7B13%7D+%5C%5C+a_%7B21%7D+%26+a_%7B22%7D+%26+a_%7B23%7D+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=A+%3D+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%26+a_%7B12%7D+%26+a_%7B13%7D+%5C%5C+a_%7B21%7D+%26+a_%7B22%7D+%26+a_%7B23%7D+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=A+%3D+%5Cbegin%7Bpmatrix%7D+a_%7B11%7D+%26+a_%7B12%7D+%26+a_%7B13%7D+%5C%5C+a_%7B21%7D+%26+a_%7B22%7D+%26+a_%7B23%7D+%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="A = &#92;begin{pmatrix} a_{11} &amp; a_{12} &amp; a_{13} &#92;&#92; a_{21} &amp; a_{22} &amp; a_{23} &#92;end{pmatrix}" class="latex" />, there&#8217;s two &#8220;canonical&#8221; ways to store it in memory (discounting fancier storage schemes such as tiled or Morton-order storage):</p>
<ul>
<li><em>Row-major</em> storage traverses the matrix by rows, then within each row enumerates the columns. So our <code>A</code> would be stored in memory as <code>a11, a12, a13, a21, a22, a23</code>. This matches reading order in English and most other Western languages. Row-major storage order for 2D arrays is used by C / C++, the Borland dialects of Pascal, and pretty much any language that was designed since the mid-1970s and supports multidimensional arrays directly. (Several newer languages such as Java don&#8217;t support 2D arrays directly at all, opting instead to represent them as an 1D array of references to 1D arrays). It&#8217;s also the dominant storage order for images. The position of the element at row <code>i</code>, column <code>j</code> in the underlying 1D array is computed as <code>i*stride + j</code>, where <code>stride</code> is the number of elements stored per row, usually the width of the 2D array, but it can also be larger.</li>
<li><em>Column-major</em> storage traverses the matrix by columns, then enumerates the rows within each column. <code>A</code> would be stored in memory as <code>a11, a21, a12, a22, a13, a23</code>. Column-major storage is used by FORTRAN and hence by BLAS and LAPACK, the bread and butter of high-performance numerical computation. It is also used by GL for matrices (due to something of a historical accident), even though GL is a C-based library and all other types of 2D or higher-dimensional data in GL (2D/3D textures etc.) use row-major.</li>
</ul>
<p>Let me reiterate: Both row and column major are about the way you layout 2D (3D, &#8230;) arrays in memory, which uses 1D addresses. In the example (and also in the following), I always write the row index first, followed by the column index, i.e. <img src="https://s0.wp.com/latex.php?latex=a_%7Bij%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=a_%7Bij%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=a_%7Bij%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="a_{ij}" class="latex" /> is the element of <code>A</code> in row <code>i</code> and column <code>j</code>, no matter which way it ends up being stored. By the same convention, I write &#8220;3&#215;4 matrix&#8221; to denote a matrix with 3 rows and 4 columns, independent of memory layout.</p>
<h3>Matrix multiplication</h3>
<p>The next ingredient we need is matrix multiplication. If <code>A</code> and <code>B</code> are a <img src="https://s0.wp.com/latex.php?latex=m+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=m+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=m+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="m &#92;times n" class="latex" /> and a <img src="https://s0.wp.com/latex.php?latex=n+%5Ctimes+p&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=n+%5Ctimes+p&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=n+%5Ctimes+p&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="n &#92;times p" class="latex" /> matrix, respectively, their product <code>C=AB</code> is a <img src="https://s0.wp.com/latex.php?latex=m+%5Ctimes+p&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=m+%5Ctimes+p&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=m+%5Ctimes+p&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="m &#92;times p" class="latex" /> matrix &#8211; note the middle dimension has to match between the two. The element in row <code>i</code> and column <code>j</code> of matrix <code>C</code> is computed as the dot product of the <code>i</code>-th row of <code>A</code> and the <code>j</code>-th column of <code>B</code>, or in formulas <img src="https://s0.wp.com/latex.php?latex=c_%7Bij%7D+%3D+%5Csum_%7Bk%3D1%7D%5En+a_%7Bik%7D+b_%7Bkj%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=c_%7Bij%7D+%3D+%5Csum_%7Bk%3D1%7D%5En+a_%7Bik%7D+b_%7Bkj%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=c_%7Bij%7D+%3D+%5Csum_%7Bk%3D1%7D%5En+a_%7Bik%7D+b_%7Bkj%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="c_{ij} = &#92;sum_{k=1}^n a_{ik} b_{kj}" class="latex" />. All this is standard stuff.</p>
<p>The important point to note here is that for a matrix product <code>AB</code> you <em>always</em> compute the dot product between rows of A and columns of B; there&#8217;s no disagreement here. This is how everyone does it, row-major layout or not.</p>
<h3>Row and column vectors</h3>
<p>So here&#8217;s where the confusion starts: a <code>n</code>-element vector can be interpreted as a matrix (a &#8220;typecast&#8221; if you will). Again, there&#8217;s two canonical ways to do this: either we stack the <code>n</code> numbers vertically into a <img src="https://s0.wp.com/latex.php?latex=n+%5Ctimes+1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=n+%5Ctimes+1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=n+%5Ctimes+1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="n &#92;times 1" class="latex" /> matrix, which gives a <em>column vector</em>, or we arrange them horizontally, producing a <img src="https://s0.wp.com/latex.php?latex=1+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=1+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=1+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="1 &#92;times n" class="latex" /> matrix or <em>row vector</em>. Note that this has <em>nothing</em> to do with the storage layout; in both row and column major orders, row and column vectors are simply stored as a <code>n</code>-element 1D array.</p>
<p>Here&#8217;s where the confusion starts: To transform a vector by a matrix, you first need to convert the vector to a matrix (i.e. choose whether it&#8217;s supposed to be a column or row vector this time), then multiply the two. In the usual graphics setting, we have a 4&#215;4 matrix <code>T</code> and a 4-vector <code>v</code>. Since the middle dimension in a matrix product must match, we can&#8217;t do this arbitrarily. If we write <code>v</code> as a 4&#215;1 matrix (column vector), we can compute <code>Tv</code> but not <code>vT</code>. If v is instead a 1&#215;4 matrix (row vector), only <code>vT</code> works and <code>Tv</code> leads to a &#8220;dimension mismatch&#8221;. For both column and row vectors, the result of that is again a column or row vector, respectively, which has repercussions: if we want to transform the result again by another matrix, again there&#8217;s only one place where we can legally put it. For column vectors, transforming a vector by <code>T</code> then <code>S</code> leads to the expression <img src="https://s0.wp.com/latex.php?latex=S%28Tv%29%3DSTv%3D%28ST%29v&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=S%28Tv%29%3DSTv%3D%28ST%29v&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=S%28Tv%29%3DSTv%3D%28ST%29v&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="S(Tv)=STv=(ST)v" class="latex" />, so the matrix that represents &#8220;first <code>T</code> then <code>S</code>&#8221; is given by the matrix <code>ST</code>. For row vectors, we get <img src="https://s0.wp.com/latex.php?latex=%28vT%29S%3DvTS%3Dv%28TS%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28vT%29S%3DvTS%3Dv%28TS%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28vT%29S%3DvTS%3Dv%28TS%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(vT)S=vTS=v(TS)" class="latex" />, so the matrix for the concatenation of the two is given by <code>TS</code>. Small difference, big repercussions: whether you choose row or column vectors influences how you concatenate transforms.</p>
<p>GL fixed function is column-major and uses column vectors, whereas D3D fixed function was row-major and used row vectors. This has led a lot of people to believe that your storage order must always match the way you interpret vectors. It doesn&#8217;t. Another myth is that matrix multiplication order depends on whether you&#8217;re using row-major or column-major. It doesn&#8217;t: matrices are always multiplied the same way, and what that product matrix means depends on what type of vector you use, not what kind of storage scheme.</p>
<p>One more point: say you have two <code>n</code>-element vectors, <code>u</code> and <code>v</code>. There&#8217;s two ways to multiply these two together as a matrix product (yes, you can multiply two vectors): if <code>u</code> is a row vector and <code>v</code> is a column vector, the product is a 1&#215;1 matrix, or just a single scalar, the dot product of the two vectors. This operation is the (matrix) inner product. If instead <code>u</code> is a column vector and <code>v</code> is a row vector, the result is a <img src="https://s0.wp.com/latex.php?latex=n+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=n+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=n+%5Ctimes+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="n &#92;times n" class="latex" /> matrix, or outer product (actually the outer product can also be computed when the dimensions of <code>u</code> and <code>v</code> don&#8217;t match, but I ignore that case here). The outer product isn&#8217;t as common as its more famous cousin, but it does crop up in several places in Linear Algebra and is worth knowing about. I bring these two examples up to illustrate that you can&#8217;t just hand-wave away the issue of vector orientation entirely;  you need both, and which one is which matters when the two interact.</p>
<p>With that cleared up, two comments: First, I recommend that you stick with whatever storage order is the default in your language/environment (principle of least surprise and all that), but it <em>really</em> doesn&#8217;t matter whether you pick row or column vectors &#8211; not in any deep sense, anyway. The difference between the two is purely one of notation (and convention). Second, there happens to be a very much dominant convention in maths, physics and really all the rest of the world except for Computer Graphics, and that convention is to use column vectors by default. For whatever reason, some of the very earliest CG texts chose to break with tradition and used row vectors, and ever since CG has been cursed with a completely unnecessary confusion about things such as matrix multiplication order. If you get to choose, I suggest you prefer column vectors. But whatever you do, make sure to document whatever you use in your code and <em>stick with it</em>, at least within individual components. In terms of understanding and debuggability, a single source file that uses two different vector math libraries with opposite conventions (or three &#8211; I&#8217;ve seen it happen) is a catastrophe. It&#8217;s a bug honeypot. Just say no, and fix it when you see it.</p>
]]></html></oembed>