@@ -3,7 +3,6 @@ Anatomy of an array
33
44.. contents :: **Contents**
55 :local:
6-
76
87
98Introduction
@@ -13,7 +12,7 @@ As explained in the Preface_, you should have a basic experience with numpy to
1312read this book. If this is not the case, you'd better start with a beginner
1413tutorial before coming back here. Consequently I'll only give here a quick
1514reminder on the basic anatomy of numpy arrays, especially regarding the memory
16- layout, view and copy and the data type. They are critical notions to
15+ layout, view, copy and the data type. They are critical notions to
1716understand if you want your computation to benefit from numpy philosophy.
1817
1918Let's consider a simple example where we want to clear all the values from an
@@ -171,30 +170,34 @@ This array can be actually considered from different perspectives (i.e. layouts)
171170.. code::
172171 :class : output
173172
174- strides[1 ]
175- (=2 )
176- ┌─────────────────────┐
177-
178- ┌ ┌──────────┬──────────┐ ┐
179- │ │ 00000000 │ 00000000 │ │
180- │ ├──────────┼──────────┤ │
181- │ │ 00000000 │ 00000001 │ │ strides[0 ]
182- │ ├──────────┼──────────┤ │ (=2x3 )
183- │ │ 00000000 │ 00000010 │ │
184- │ ├──────────┼──────────┤ ┘
185- Z.nbytes │ │ 00000000 │ 00000011 │
186- (=3x3x2 ) │ ├──────────┼──────────┤
187- ┆ ┆ ┆ ┆
188- ┆ ┆ … ┆ … ┆
189- ┆ ┆ ┆ ┆
190- │ ├──────────┼──────────┤
191- │ │ 00000000 │ 00001000 │
192- └ └──────────┴──────────┘
193-
194- └─────────────────────┘
195- Z.itemsize
196- Z.dtype.itemsize
197- (=2 )
173+ strides[1 ]
174+ (=2 )
175+ ┌─────────────────────┐
176+
177+ ┌ ┌──────────┬──────────┐ ┐
178+ │ p+ 00 : │ 00000000 │ 00000000 │ │
179+ │ ├──────────┼──────────┤ │
180+ │ p+ 02 : │ 00000000 │ 00000001 │ │ strides[0 ]
181+ │ ├──────────┼──────────┤ │ (=2x3 )
182+ │ p+ 04 │ 00000000 │ 00000010 │ │
183+ │ ├──────────┼──────────┤ ┘
184+ │ p+ 06 │ 00000000 │ 00000011 │
185+ │ ├──────────┼──────────┤
186+ Z.nbytes │ p+ 08 : │ 00000000 │ 00000100 │
187+ (=3x3x2 ) │ ├──────────┼──────────┤
188+ │ p+ 10 : │ 00000000 │ 00000101 │
189+ │ ├──────────┼──────────┤
190+ │ p+ 12 : │ 00000000 │ 00000110 │
191+ │ ├──────────┼──────────┤
192+ │ p+ 14 : │ 00000000 │ 00000111 │
193+ │ ├──────────┼──────────┤
194+ │ p+ 16 : │ 00000000 │ 00001000 │
195+ └ └──────────┴──────────┘
196+
197+ └─────────────────────┘
198+ Z.itemsize
199+ Z.dtype.itemsize
200+ (=2 )
198201
199202
200203If we now take a slice of `Z` , the result is a view of the base array `Z` :
@@ -229,10 +232,11 @@ cannot be deduced anymore from the dtype and shape only:
229232 :class : output
230233
231234 ┌───┬╌╌╌┬───┬╌╌╌┬╌╌╌┬╌╌╌┬───┬╌╌╌┬───┐ ┌───┬───┬───┬───┐
232- │ 0 │ │ 2 │ │ │ │ 6 │ │ 8 │ → │ 0 │ 2 │ 6 │ 8 │
235+ │ 0 │ │ 2 │ ╎ ╎ │ 6 │ │ 8 │ → │ 0 │ 2 │ 6 │ 8 │
233236 └───┴╌╌╌┴───┴╌╌╌┴╌╌╌┴╌╌╌┴───┴╌╌╌┴───┘ └───┴───┴───┴───┘
234-
235- └───────────────────────────────────┘
237+ └─┬─┘ └─┬─┘ └─┬─┘ └─┬─┘
238+ └───┬───┘ └───┬───┘
239+ └───────────┬───────────┘
236240 Z.size
237241 (=4 )
238242
@@ -243,28 +247,30 @@ cannot be deduced anymore from the dtype and shape only:
243247.. code::
244248 :class : output
245249
246- ┌ ┌──────────┬──────────┐ ┐ ┐
247- │ │ 00000000 │ 00000000 │ │ │
248- │ ├──────────┼──────────┤ │ strides[1 ] │
249- │ ┆ ┆ ┆ │ (=4 ) │
250- │ ├──────────┼──────────┤ ┘ │ strides[0 ]
251- │ │ 00000000 │ 00000010 │ │ (=12 )
252- │ ├──────────┼──────────┤ │
253- Z.nbytes │ ┆ ┆ ┆ ┆
254- (n=8 ) ┆ ┆ … ┆ … ┆ ┆
255- ┆ ┆ ┆ ┆ ┆
256- │ ├──────────┼──────────┤ ┘
257- │ │ 00000000 │ 00001000 │
258- │ ├──────────┼──────────┤
259- │ ┆ ┆ ┆
260- │ ├──────────┼──────────┤
261- │ │ 00000000 │ 00001000 │
262- └ └──────────┴──────────┘
263-
264- └─────────────────────┘
265- Z.itemsize
266- Z.dtype.itemsize
267- (=2 )
250+ ┌ ┌──────────┬──────────┐ ┐ ┐
251+ ┌─┤ p+ 00 : │ 00000000 │ 00000000 │ │ │
252+ │ └ ├──────────┼──────────┤ │ strides[1 ] │
253+ ┌─┤ p+ 02 : │ │ │ │ (=4 ) │
254+ │ │ ┌ ├──────────┼──────────┤ ┘ │
255+ │ └─┤ p+ 04 │ 00000000 │ 00000010 │ │
256+ │ └ ├──────────┼──────────┤ │ strides[0 ]
257+ │ p+ 06 : │ │ │ │ (=12 )
258+ │ ├──────────┼──────────┤ │
259+ Z.nbytes ─┤ p+ 08 : │ │ │ │
260+ (=8 ) │ ├──────────┼──────────┤ │
261+ │ p+ 10 : │ │ │ │
262+ │ ┌ ├──────────┼──────────┤ ┘
263+ │ ┌─┤ p+ 12 : │ 00000000 │ 00000110 │
264+ │ │ └ ├──────────┼──────────┤
265+ └─┤ p+ 14 : │ │ │
266+ │ ┌ ├──────────┼──────────┤
267+ └─┤ p+ 16 : │ 00000000 │ 00001000 │
268+ └ └──────────┴──────────┘
269+
270+ └─────────────────────┘
271+ Z.itemsize
272+ Z.dtype.itemsize
273+ (=2 )
268274
269275
270276
0 commit comments