<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Memory_layout_of_Numpy_matrices</id>
	<title>Memory layout of Numpy matrices - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Memory_layout_of_Numpy_matrices"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Memory_layout_of_Numpy_matrices&amp;action=history"/>
	<updated>2026-06-18T13:42:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Memory_layout_of_Numpy_matrices&amp;diff=381&amp;oldid=prev</id>
		<title>Davrot at 16:51, 17 October 2025</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Memory_layout_of_Numpy_matrices&amp;diff=381&amp;oldid=prev"/>
		<updated>2025-10-17T16:51:12Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:51, 17 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== The goal ==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;More information about the Numpy matrices and the memory structure behind it.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;More information about the Numpy matrices and the memory structure behind it.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wiki:diff:1.41:old-231:rev-381:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Memory_layout_of_Numpy_matrices&amp;diff=231&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot;== The goal == More information about the Numpy matrices and the memory structure behind it.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == Memory layouts​ (row-major vs column-major) == C order (or row-major)&lt;blockquote&gt;In row-major order, the last dimension is contiguous, so that the memory-offset of this element is given by:&lt;/blockquote&gt;&lt;math display=&quot;block&quot;&gt; n_{d}+N_{d}\cdot (n_{d-1}+N_{d-1}\cdot (n_{d-2}+N...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Memory_layout_of_Numpy_matrices&amp;diff=231&amp;oldid=prev"/>
		<updated>2025-10-17T13:35:28Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== The goal == More information about the Numpy matrices and the memory structure behind it.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == &lt;a href=&quot;https://en.wikipedia.org/wiki/Row-_and_column-major_order&quot; class=&quot;extiw&quot; title=&quot;wikipedia:Row- and column-major order&quot;&gt;Memory layouts​ (row-major vs column-major)&lt;/a&gt; == C order (or row-major)&amp;lt;blockquote&amp;gt;In row-major order, the last dimension is contiguous, so that the memory-offset of this element is given by:&amp;lt;/blockquote&amp;gt;&amp;lt;math display=&amp;quot;block&amp;quot;&amp;gt; n_{d}+N_{d}\cdot (n_{d-1}+N_{d-1}\cdot (n_{d-2}+N...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== The goal ==&lt;br /&gt;
More information about the Numpy matrices and the memory structure behind it.&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
== [[wikipedia:Row-_and_column-major_order|Memory layouts​ (row-major vs column-major)]] ==&lt;br /&gt;
C order (or row-major)&amp;lt;blockquote&amp;gt;In row-major order, the last dimension is contiguous, so that the memory-offset of this element is given by:&amp;lt;/blockquote&amp;gt;&amp;lt;math display=&amp;quot;block&amp;quot;&amp;gt; n_{d}+N_{d}\cdot (n_{d-1}+N_{d-1}\cdot (n_{d-2}+N_{d-2}\cdot (\cdots +N_{2}n_{1})\cdots )))=\sum _{k=1}^{d}\left(\prod _{\ell =k+1}^{d}N_{\ell }\right)n_{k} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fortran (or column-major)&amp;lt;blockquote&amp;gt;In column-major order, the first dimension is contiguous, so that the memory-offset of this element is given by:&amp;lt;/blockquote&amp;gt;&amp;lt;math display=&amp;quot;block&amp;quot;&amp;gt; n_{1}+N_{1}\cdot (n_{2}+N_{2}\cdot (n_{3}+N_{3}\cdot (\cdots +N_{d-1}n_{d})\cdots )))=\sum _{k=1}^{d}\left(\prod _{\ell =1}^{k-1}N_{\ell }\right)n_{k} &amp;lt;/math&amp;gt;&amp;lt;div class=&amp;quot;figure&amp;quot;&amp;gt;&lt;br /&gt;
[[File:2025-10-17 15-34.png]]&lt;br /&gt;
&amp;lt;/div&amp;gt;[[wikipedia:Row-_and_column-major_order#/media/File:Row_and_column_major_order.svg|Illustration of difference between row- and column-major ordering]] (by CMG Lee. CC BY-SA 4.0)&lt;br /&gt;
&lt;br /&gt;
== [https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flags.html numpy.ndarray.flags] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;ndarray.flags&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Information about the memory layout of the array.&amp;lt;/blockquote&amp;gt;Attributes:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|C_CONTIGUOUS (C)&lt;br /&gt;
|The data is in a single, C-style contiguous segment.&lt;br /&gt;
|-&lt;br /&gt;
|F_CONTIGUOUS (F)&lt;br /&gt;
|The data is in a single, Fortran-style contiguous segment.&lt;br /&gt;
|-&lt;br /&gt;
|OWNDATA (O)&lt;br /&gt;
|The array owns the memory it uses or borrows it from another object.&lt;br /&gt;
|-&lt;br /&gt;
|WRITEABLE (W)&lt;br /&gt;
|The data area can be written to. Setting this to False locks the data, making it read-only. A view (slice, etc.) inherits WRITEABLE from its base array at creation time, but a view of a writeable array may be subsequently locked while the base array remains writeable. (The opposite is not true, in that a view of a locked array may not be made writeable. However, currently, locking a base object does not lock any views that already reference it, so under that circumstance it is possible to alter the contents of a locked array via a previously created writeable view onto it.) Attempting to change a non-writeable array raises a RuntimeError exception.&lt;br /&gt;
|-&lt;br /&gt;
|ALIGNED (A)&lt;br /&gt;
|The data and all elements are aligned appropriately for the hardware.&lt;br /&gt;
|-&lt;br /&gt;
|WRITEBACKIFCOPY (X)&lt;br /&gt;
|This array is a copy of some other array. The C-API function PyArray_ResolveWritebackIfCopy must be called before deallocating to the base array will be updated with the contents of this array.&lt;br /&gt;
|-&lt;br /&gt;
|FNC&lt;br /&gt;
|F_CONTIGUOUS and not C_CONTIGUOUS.&lt;br /&gt;
|-&lt;br /&gt;
|FORC&lt;br /&gt;
|F_CONTIGUOUS or C_CONTIGUOUS (one-segment test).&lt;br /&gt;
|-&lt;br /&gt;
|BEHAVED (B)&lt;br /&gt;
|ALIGNED and WRITEABLE.&lt;br /&gt;
|-&lt;br /&gt;
|CARRAY (CA)&lt;br /&gt;
|BEHAVED and C_CONTIGUOUS.&lt;br /&gt;
|-&lt;br /&gt;
|FARRAY (FA)&lt;br /&gt;
|BEHAVED and F_CONTIGUOUS and not C_CONTIGUOUS.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== 1d ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
a = np.zeros((1, 2))&lt;br /&gt;
print(a.flags)&amp;lt;/syntaxhighlight&amp;gt;Output&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;  C_CONTIGUOUS : True&lt;br /&gt;
  F_CONTIGUOUS : True&lt;br /&gt;
  OWNDATA : True&lt;br /&gt;
  WRITEABLE : True&lt;br /&gt;
  ALIGNED : True&lt;br /&gt;
  WRITEBACKIFCOPY : False&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 2d ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
a = np.zeros((2, 2))&lt;br /&gt;
print(a.flags)&amp;lt;/syntaxhighlight&amp;gt;Output&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;  C_CONTIGUOUS : True&lt;br /&gt;
  F_CONTIGUOUS : False&lt;br /&gt;
  OWNDATA : True&lt;br /&gt;
  WRITEABLE : True&lt;br /&gt;
  ALIGNED : True&lt;br /&gt;
  WRITEBACKIFCOPY : False&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== C - contigousness ==&lt;br /&gt;
There are situations when you need a C_CONTIGUOUS matrix. Examples are PyBind11 and numba.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
a = np.arange(1, 10)&lt;br /&gt;
&lt;br /&gt;
print(a.flags[&amp;quot;C_CONTIGUOUS&amp;quot;])  # -&amp;gt; True&lt;br /&gt;
&lt;br /&gt;
print(a[::1].flags[&amp;quot;C_CONTIGUOUS&amp;quot;])  # -&amp;gt; True&lt;br /&gt;
print(a[::2].flags[&amp;quot;C_CONTIGUOUS&amp;quot;])  # -&amp;gt; False&lt;br /&gt;
&lt;br /&gt;
print(a[::2].copy().flags[&amp;quot;C_CONTIGUOUS&amp;quot;])  # -&amp;gt; True&amp;lt;/syntaxhighlight&amp;gt;&amp;#039;&amp;#039;&amp;#039;You may want to make a copy of B for PyBind11 and numba or…&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== [https://numpy.org/doc/stable/reference/generated/numpy.ascontiguousarray.html numpy.ascontiguousarray] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;numpy.ascontiguousarray(a, dtype=None, *, like=None)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Return a contiguous array (ndim &amp;amp;#x3E;= 1) in memory (C order).&amp;lt;/blockquote&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
a = np.arange(1, 10)&lt;br /&gt;
&lt;br /&gt;
print(a.flags[&amp;quot;C_CONTIGUOUS&amp;quot;])  # -&amp;gt; True&lt;br /&gt;
print(a[::2].flags[&amp;quot;C_CONTIGUOUS&amp;quot;])  # -&amp;gt; False&lt;br /&gt;
print(np.ascontiguousarray(a[::2]).flags[&amp;quot;C_CONTIGUOUS&amp;quot;])  # -&amp;gt; True&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>