<s>
In	O
computing	O
,	O
row-major	B-Data_Structure
order	I-Data_Structure
and	O
column-major	B-Data_Structure
order	I-Data_Structure
are	O
methods	O
for	O
storing	O
multidimensional	O
arrays	O
in	O
linear	O
storage	O
such	O
as	O
random	B-Architecture
access	I-Architecture
memory	I-Architecture
.	O
</s>
<s>
In	O
row-major	B-Data_Structure
order	I-Data_Structure
,	O
the	O
consecutive	O
elements	O
of	O
a	O
row	O
reside	O
next	O
to	O
each	O
other	O
,	O
whereas	O
the	O
same	O
holds	O
true	O
for	O
consecutive	O
elements	O
of	O
a	O
column	O
in	O
column-major	B-Data_Structure
order	I-Data_Structure
.	O
</s>
<s>
a	O
matrix	B-Architecture
,	O
the	O
orders	O
can	O
be	O
generalized	O
to	O
arrays	O
of	O
any	O
dimension	O
by	O
noting	O
that	O
the	O
terms	O
row-major	B-Data_Structure
and	O
column-major	O
are	O
equivalent	O
to	O
lexicographic	O
and	O
colexicographic	O
orders	O
,	O
respectively	O
.	O
</s>
<s>
This	O
is	O
primarily	O
due	O
to	O
CPU	B-General_Concept
caching	I-General_Concept
which	O
exploits	O
spatial	B-General_Concept
locality	I-General_Concept
of	I-General_Concept
reference	I-General_Concept
.	O
</s>
<s>
In	O
addition	O
,	O
contiguous	O
access	O
makes	O
it	O
possible	O
to	O
use	O
SIMD	B-Device
instructions	O
that	O
operate	O
on	O
vectors	O
of	O
data	O
.	O
</s>
<s>
In	O
some	O
media	O
such	O
as	O
magnetic-tape	B-Architecture
data	I-Architecture
storage	I-Architecture
,	O
accessing	O
sequentially	O
is	O
orders	O
of	O
magnitude	O
faster	O
than	O
nonsequential	O
access	O
.	O
</s>
<s>
The	O
terms	O
row-major	B-Data_Structure
and	O
column-major	O
stem	O
from	O
the	O
terminology	O
related	O
to	O
ordering	O
objects	O
.	O
</s>
<s>
For	O
matrices	O
in	O
mathematical	O
notation	O
,	O
the	O
first	O
index	O
indicates	O
the	O
row	O
,	O
and	O
the	O
second	O
indicates	O
the	O
column	O
,	O
e.g.	O
,	O
given	O
a	O
matrix	B-Architecture
,	O
is	O
in	O
its	O
first	O
row	O
and	O
second	O
column	O
.	O
</s>
<s>
The	O
choice	O
of	O
how	O
to	O
group	O
and	O
order	O
the	O
indices	O
,	O
either	O
by	O
row-major	B-Data_Structure
or	O
column-major	O
methods	O
,	O
is	O
thus	O
a	O
matter	O
of	O
convention	O
.	O
</s>
<s>
Row-major	B-Data_Structure
grouping	O
starts	O
from	O
the	O
leftmost	O
index	O
and	O
column-major	O
from	O
the	O
rightmost	O
index	O
,	O
leading	O
to	O
lexicographic	O
and	O
colexicographic	O
(	O
or	O
colex	O
)	O
orders	O
,	O
respectively	O
.	O
</s>
<s>
In	O
C	B-Language
,	O
multidimensional	O
arrays	O
are	O
stored	O
in	O
row-major	B-Data_Structure
order	I-Data_Structure
,	O
and	O
the	O
array	O
indexes	O
are	O
written	O
row-first	O
(	O
lexicographical	O
access	O
order	O
)	O
:	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
in	O
Fortran	B-Application
,	O
arrays	O
are	O
stored	O
in	O
column-major	B-Data_Structure
order	I-Data_Structure
,	O
while	O
the	O
array	O
indexes	O
are	O
still	O
written	O
row-first	O
(	O
colexicographical	O
access	O
order	O
)	O
:	O
</s>
<s>
Note	O
how	O
the	O
use	O
of	O
A[i][j]	O
with	O
multi-step	O
indexing	O
as	O
in	O
C	B-Language
,	O
as	O
opposed	O
to	O
a	O
neutral	O
notation	O
like	O
A(i,j )	O
as	O
in	O
Fortran	B-Application
,	O
almost	O
inevitably	O
implies	O
row-major	B-Data_Structure
order	I-Data_Structure
for	O
syntactic	O
reasons	O
,	O
so	O
to	O
speak	O
,	O
because	O
it	O
can	O
be	O
rewritten	O
as	O
(	O
A[i]	O
)	O
[j],	O
and	O
the	O
A[i]	O
row	O
part	O
can	O
even	O
be	O
assigned	O
to	O
an	O
intermediate	O
variable	O
that	O
is	O
then	O
indexed	O
in	O
a	O
separate	O
expression	O
.	O
</s>
<s>
(	O
No	O
other	O
implications	O
should	O
be	O
assumed	O
,	O
e.g.	O
,	O
Fortran	B-Application
is	O
not	O
column-major	O
simply	O
because	O
of	O
its	O
notation	O
,	O
and	O
even	O
the	O
above	O
implication	O
could	O
intentionally	O
be	O
circumvented	O
in	O
a	O
new	O
language	O
.	O
)	O
</s>
<s>
To	O
use	O
column-major	B-Data_Structure
order	I-Data_Structure
in	O
a	O
row-major	B-Data_Structure
environment	O
,	O
or	O
vice	O
versa	O
,	O
for	O
whatever	O
reason	O
,	O
one	O
workaround	O
is	O
to	O
assign	O
non-conventional	O
roles	O
to	O
the	O
indexes	O
(	O
using	O
the	O
first	O
index	O
for	O
the	O
column	O
and	O
the	O
second	O
index	O
for	O
the	O
row	O
)	O
,	O
and	O
another	O
is	O
to	O
bypass	O
language	O
syntax	O
by	O
explicitly	O
computing	O
positions	O
in	O
a	O
one-dimensional	O
array	O
.	O
</s>
<s>
Of	O
course	O
,	O
deviating	O
from	O
convention	O
probably	O
incurs	O
a	O
cost	O
that	O
increases	O
with	O
the	O
degree	O
of	O
necessary	O
interaction	O
with	O
conventional	O
language	O
features	O
and	O
other	O
code	O
,	O
not	O
only	O
in	O
the	O
form	O
of	O
increased	O
vulnerability	O
to	O
mistakes	O
(	O
forgetting	O
to	O
also	O
invert	O
matrix	B-Architecture
multiplication	O
order	O
,	O
reverting	O
to	O
convention	O
during	O
code	O
maintenance	O
,	O
etc	O
.	O
</s>
<s>
Running	O
the	O
loop	O
row-wise	O
is	O
preferred	O
in	O
row-major	B-Data_Structure
languages	O
like	O
C	B-Language
and	O
vice	O
versa	O
for	O
column-major	O
languages	O
.	O
</s>
<s>
Programming	O
languages	O
or	O
their	O
standard	O
libraries	O
that	O
support	O
multi-dimensional	O
arrays	O
typically	O
have	O
a	O
native	O
row-major	B-Data_Structure
or	O
column-major	O
storage	O
order	O
for	O
these	O
arrays	O
.	O
</s>
<s>
Row-major	B-Data_Structure
order	I-Data_Structure
is	O
used	O
in	O
C/C	O
++	O
/Objective	O
-C	O
(	O
for	O
C-style	O
arrays	O
)	O
,	O
PL/I	B-Language
,	O
Pascal	B-Application
,	O
Speakeasy	B-General_Concept
,	O
and	O
SAS	B-Language
.	O
</s>
<s>
Column-major	B-Data_Structure
order	I-Data_Structure
is	O
used	O
in	O
Fortran	B-Application
,	O
MATLAB	B-Language
,	O
GNU	B-Language
Octave	I-Language
,	O
Julia	B-Application
,	O
S	B-Language
,	O
S-PLUS	B-Application
,	O
R	B-Language
,	O
Scilab	B-Application
,	O
Yorick	B-Language
,	O
and	O
Rasdaman	B-Application
.	O
</s>
<s>
A	O
typical	O
alternative	O
for	O
dense	O
array	O
storage	O
is	O
to	O
use	O
Iliffe	B-Data_Structure
vectors	I-Data_Structure
,	O
which	O
typically	O
store	O
pointers	O
to	O
elements	O
in	O
the	O
same	O
row	O
contiguously	O
(	O
like	O
row-major	B-Data_Structure
order	I-Data_Structure
)	O
,	O
but	O
not	O
the	O
rows	O
themselves	O
.	O
</s>
<s>
They	O
are	O
used	O
in	O
(	O
ordered	O
by	O
age	O
)	O
:	O
Java	B-Language
,	O
C#	B-Application
/CLI/.Net	O
,	O
Scala	B-Application
,	O
and	O
Swift	B-Application
.	O
</s>
<s>
Even	O
less	O
dense	O
is	O
to	O
use	O
lists	O
of	O
lists	O
,	O
e.g.	O
,	O
in	O
Python	B-Language
,	O
and	O
in	O
the	O
Wolfram	B-Language
Language	I-Language
of	O
Wolfram	B-Application
Mathematica	I-Application
.	O
</s>
<s>
An	O
alternative	O
approach	O
uses	O
tables	O
of	O
tables	O
,	O
e.g.	O
,	O
in	O
Lua	B-Language
.	O
</s>
<s>
Support	O
for	O
multi-dimensional	O
arrays	O
may	O
also	O
be	O
provided	O
by	O
external	O
libraries	O
,	O
which	O
may	O
even	O
support	O
arbitrary	O
orderings	O
,	O
where	O
each	O
dimension	O
has	O
a	O
stride	B-Data_Structure
value	O
,	O
and	O
row-major	B-Data_Structure
or	O
column-major	O
are	O
just	O
two	O
possible	O
resulting	O
interpretations	O
.	O
</s>
<s>
Row-major	B-Data_Structure
order	I-Data_Structure
is	O
the	O
default	O
in	O
NumPy	B-Application
(	O
for	O
Python	B-Language
)	O
.	O
</s>
<s>
Column-major	B-Data_Structure
order	I-Data_Structure
is	O
the	O
default	O
in	O
Eigen	B-Language
and	O
(	O
both	O
for	O
C++	B-Language
)	O
.	O
</s>
<s>
A	O
special	O
case	O
would	O
be	O
OpenGL	B-Application
(	O
and	O
OpenGL	B-Application
ES	I-Application
)	O
for	O
graphics	O
processing	O
.	O
</s>
<s>
Since	O
"	O
recent	O
mathematical	O
treatments	O
of	O
linear	O
algebra	O
and	O
related	O
fields	O
invariably	O
treat	O
vectors	O
as	O
columns	O
,	O
"	O
designer	O
Mark	O
Segal	O
decided	O
to	O
substitute	O
this	O
for	O
the	O
convention	O
in	O
predecessor	O
IRIS	B-Library
GL	I-Library
,	O
which	O
was	O
to	O
write	O
vectors	O
as	O
rows	O
;	O
for	O
compatibility	O
,	O
transformation	O
matrices	O
would	O
still	O
be	O
stored	O
in	O
vector-major	O
(=	O
row-major	B-Data_Structure
)	O
rather	O
than	O
coordinate-major	O
(=	O
column-major	O
)	O
order	O
,	O
and	O
he	O
then	O
used	O
the	O
trick	O
"[to]	O
say	O
that	O
matrices	O
in	O
OpenGL	B-Application
are	O
stored	O
in	O
column-major	B-Data_Structure
order	I-Data_Structure
"	O
.	O
</s>
<s>
This	O
was	O
really	O
only	O
relevant	O
for	O
presentation	O
,	O
because	O
matrix	B-Architecture
multiplication	O
was	O
stack-based	O
and	O
could	O
still	O
be	O
interpreted	O
as	O
post-multiplication	O
,	O
but	O
,	O
worse	O
,	O
reality	O
leaked	O
through	O
the	O
C-based	O
API	B-General_Concept
because	O
individual	O
elements	O
would	O
be	O
accessed	O
as	O
M[vector][coordinate]	O
or	O
,	O
effectively	O
,	O
M[column][row],	O
which	O
unfortunately	O
muddled	O
the	O
convention	O
that	O
the	O
designer	O
sought	O
to	O
adopt	O
,	O
and	O
this	O
was	O
even	O
preserved	O
in	O
the	O
OpenGL	B-Language
Shading	I-Language
Language	I-Language
that	O
was	O
later	O
added	O
(	O
although	O
this	O
also	O
makes	O
it	O
possible	O
to	O
access	O
coordinates	O
by	O
name	O
instead	O
,	O
e.g.	O
,	O
M[vector].y	O
)	O
.	O
</s>
<s>
As	O
a	O
result	O
,	O
many	O
developers	O
will	O
now	O
simply	O
declare	O
that	O
having	O
the	O
column	O
as	O
the	O
first	O
index	O
is	O
the	O
definition	O
of	O
column-major	O
,	O
even	O
though	O
this	O
is	O
clearly	O
not	O
the	O
case	O
with	O
a	O
real	O
column-major	O
language	O
like	O
Fortran	B-Application
.	O
</s>
<s>
Torch	B-Algorithm
(	O
for	O
Lua	B-Language
)	O
changed	O
from	O
column-major	O
to	O
row-major	B-Data_Structure
default	O
order	O
.	O
</s>
<s>
As	O
exchanging	O
the	O
indices	O
of	O
an	O
array	O
is	O
the	O
essence	O
of	O
array	O
transposition	O
,	O
an	O
array	O
stored	O
as	O
row-major	B-Data_Structure
but	O
read	O
as	O
column-major	O
(	O
or	O
vice	O
versa	O
)	O
will	O
appear	O
transposed	O
(	O
as	O
long	O
as	O
the	O
matrix	B-Architecture
is	O
square	O
)	O
.	O
</s>
<s>
As	O
actually	O
performing	O
this	O
rearrangement	B-Algorithm
in	I-Algorithm
memory	I-Algorithm
is	O
typically	O
an	O
expensive	O
operation	O
,	O
some	O
systems	O
provide	O
options	O
to	O
specify	O
individual	O
matrices	O
as	O
being	O
stored	O
transposed	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
Basic	B-Application
Linear	I-Application
Algebra	I-Application
Subprograms	I-Application
functions	O
are	O
passed	O
flags	O
indicating	O
which	O
arrays	O
are	O
transposed	O
.	O
</s>
<s>
For	O
a	O
d-dimensional	O
array	O
with	O
dimensions	O
Nk	O
(	O
k	O
=	O
1	O
...	O
d	O
)	O
,	O
a	O
given	O
element	O
of	O
this	O
array	O
is	O
specified	O
by	O
a	O
tuple	B-Application
of	O
d	O
(	O
zero-based	O
)	O
indices	O
.	O
</s>
<s>
In	O
row-major	B-Data_Structure
order	I-Data_Structure
,	O
the	O
last	O
dimension	O
is	O
contiguous	O
,	O
so	O
that	O
the	O
memory-offset	O
of	O
this	O
element	O
is	O
given	O
by	O
:	O
</s>
<s>
In	O
column-major	B-Data_Structure
order	I-Data_Structure
,	O
the	O
first	O
dimension	O
is	O
contiguous	O
,	O
so	O
that	O
the	O
memory-offset	O
of	O
this	O
element	O
is	O
given	O
by	O
:	O
</s>
<s>
For	O
a	O
given	O
order	O
,	O
the	O
stride	B-Data_Structure
in	O
dimension	O
k	O
is	O
given	O
by	O
the	O
multiplication	O
value	O
in	O
parentheses	O
before	O
index	O
nk	O
in	O
the	O
right-hand	O
side	O
summations	O
above	O
.	O
</s>
<s>
possible	O
orders	O
for	O
a	O
given	O
array	O
,	O
one	O
for	O
each	O
permutation	B-Algorithm
of	O
dimensions	O
(	O
with	O
row-major	B-Data_Structure
and	O
column-order	O
just	O
2	O
special	O
cases	O
)	O
,	O
although	O
the	O
lists	O
of	O
stride	B-Data_Structure
values	O
are	O
not	O
necessarily	O
permutations	B-Algorithm
of	O
each	O
other	O
,	O
e.g.	O
,	O
in	O
the	O
2-by-3	O
example	O
above	O
,	O
the	O
strides	O
are	O
(	O
3	O
,	O
1	O
)	O
for	O
row-major	B-Data_Structure
and	O
(	O
1	O
,	O
2	O
)	O
for	O
column-major	O
.	O
</s>
