-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharrays.nditer.html
1576 lines (1370 loc) · 153 KB
/
arrays.nditer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" data-content_root="../" data-theme="light">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Iterating over arrays — NumPy v2.3.dev0 Manual</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "light";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "light";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="../_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link href="../_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=8f2a1f02" />
<link rel="stylesheet" type="text/css" href="../_static/graphviz.css?v=eafc0fe6" />
<link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Vibur" />
<link rel="stylesheet" type="text/css" href="../_static/jupyterlite_sphinx.css?v=2c9f8f05" />
<link rel="stylesheet" type="text/css" href="../_static/sphinx-design.min.css?v=95c83b7e" />
<link rel="stylesheet" type="text/css" href="../_static/numpy.css?v=a1b581f7" />
<!-- So that users can add custom icons -->
<script src="../_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" />
<script src="../_static/documentation_options.js?v=5f219352"></script>
<script src="../_static/doctools.js?v=888ff710"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../_static/copybutton.js?v=30646c52"></script>
<script src="../_static/jupyterlite_sphinx.js?v=96e329c5"></script>
<script src="../_static/design-tabs.js?v=f930bc37"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'reference/arrays.nditer';</script>
<script>
DOCUMENTATION_OPTIONS.theme_version = '0.16.1';
DOCUMENTATION_OPTIONS.theme_switcher_json_url = 'https://numpy.org/doc/_static/versions.json';
DOCUMENTATION_OPTIONS.theme_switcher_version_match = 'devdocs';
DOCUMENTATION_OPTIONS.show_version_warning_banner =
true;
</script>
<link rel="icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Standard array subclasses" href="arrays.classes.html" />
<link rel="prev" title="Data type promotion in NumPy" href="arrays.promotion.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="2.3.dev0" />
<meta name="docbuild:last-update" content="Apr 20, 2025"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="light">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="../search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
<div class="bd-header__inner bd-page-width">
<button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation">
<span class="fa-solid fa-bars"></span>
</button>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="../index.html">
<img src="../_static/numpylogo.svg" class="logo__image only-light" alt="NumPy v2.3.dev0 Manual - Home"/>
<img src="../_static/numpylogo_dark.svg" class="logo__image only-dark pst-js-only" alt="NumPy v2.3.dev0 Manual - Home"/>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item ">
<a class="nav-link nav-internal" href="../user/index.html">
User Guide
</a>
</li>
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
API reference
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../building/index.html">
Building from source
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../dev/index.html">
Development
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../release.html">
Release notes
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://numpy.org/numpy-tutorials/">
Learn
</a>
</li>
<li class="nav-item dropdown">
<button class="btn dropdown-toggle nav-item" type="button"
data-bs-toggle="dropdown" aria-expanded="false"
aria-controls="pst-nav-more-links">
More
</button>
<ul id="pst-nav-more-links" class="dropdown-menu">
<li class=" ">
<a class="nav-link dropdown-item nav-external" href="https://numpy.org/neps">
NEPs
</a>
</li>
</ul>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm pst-navbar-icon search-button search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button></div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item">
<div class="version-switcher__container dropdown pst-js-only">
<button id="pst-version-switcher-button-2"
type="button"
class="version-switcher__button btn btn-sm dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-2"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-2"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-2">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page">
<span class="fa-solid fa-outdent"></span>
</button>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item ">
<a class="nav-link nav-internal" href="../user/index.html">
User Guide
</a>
</li>
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
API reference
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../building/index.html">
Building from source
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../dev/index.html">
Development
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../release.html">
Release notes
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://numpy.org/numpy-tutorials/">
Learn
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://numpy.org/neps">
NEPs
</a>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm pst-navbar-icon search-button search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button></div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item">
<div class="version-switcher__container dropdown pst-js-only">
<button id="pst-version-switcher-button-3"
type="button"
class="version-switcher__button btn btn-sm dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-3"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-3"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-3">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
aria-label="Section Navigation">
<p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
<div class="bd-toc-item navbar-nav"><ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="module_structure.html">NumPy’s module structure</a></li>
</ul>
<ul class="current nav bd-sidenav">
<li class="toctree-l1 current active has-children"><a class="reference internal" href="arrays.html">Array objects</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="arrays.ndarray.html">The N-dimensional array (<code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code>)</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.scalars.html">Scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.dtypes.html">Data type objects (<code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code>)</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.promotion.html">Data type promotion in NumPy</a></li>
<li class="toctree-l2 current active"><a class="current reference internal" href="#">Iterating over arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.classes.html">Standard array subclasses</a></li>
<li class="toctree-l2"><a class="reference internal" href="maskedarray.html">Masked arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.interface.html">The array interface protocol</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.datetime.html">Datetimes and timedeltas</a></li>
</ul>
</details></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="ufuncs.html">Universal functions (<code class="xref py py-class docutils literal notranslate"><span class="pre">ufunc</span></code>)</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="routines.html">Routines and objects by topic</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="typing.html">Typing (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.typing</span></code>)</a></li>
<li class="toctree-l1"><a class="reference internal" href="distutils.html">Packaging</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="c-api/index.html">NumPy C-API</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="array_api.html">Array API standard compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="simd/index.html">CPU/SIMD optimizations</a></li>
<li class="toctree-l1"><a class="reference internal" href="thread_safety.html">Thread Safety</a></li>
<li class="toctree-l1"><a class="reference internal" href="global_state.html">Global Configuration Options</a></li>
<li class="toctree-l1"><a class="reference internal" href="security.html">NumPy security</a></li>
<li class="toctree-l1"><a class="reference internal" href="distutils_status_migration.html">Status of <code class="docutils literal notranslate"><span class="pre">numpy.distutils</span></code> and migration advice</a></li>
<li class="toctree-l1"><a class="reference internal" href="distutils_guide.html"><code class="docutils literal notranslate"><span class="pre">numpy.distutils</span></code> user guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="swig.html">NumPy and SWIG</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item">
<div id="ethical-ad-placement"
class="flat"
data-ea-publisher="readthedocs"
data-ea-type="readthedocs-sidebar"
data-ea-manual="true">
</div></div>
</div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb" class="d-print-none">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="../index.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item"><a href="index.html" class="nav-link">NumPy reference</a></li>
<li class="breadcrumb-item"><a href="arrays.html" class="nav-link">Array objects</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">Iterating over arrays</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="iterating-over-arrays">
<span id="arrays-nditer"></span><h1>Iterating over arrays<a class="headerlink" href="#iterating-over-arrays" title="Link to this heading">#</a></h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Arrays support the iterator protocol and can be iterated over like Python
lists. See the <a class="reference internal" href="../user/quickstart.html#quickstart-indexing-slicing-and-iterating"><span class="std std-ref">Indexing, slicing and iterating</span></a> section in
the Quickstart guide for basic usage and examples. The remainder of
this document presents the <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> object and covers more
advanced usage.</p>
</div>
<p>The iterator object <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a>, introduced in NumPy 1.6, provides
many flexible ways to visit all the elements of one or more arrays in
a systematic fashion. This page introduces some basic ways to use the
object for computations on arrays in Python, then concludes with how one
can accelerate the inner loop in Cython. Since the Python exposure of
<a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> is a relatively straightforward mapping of the C array
iterator API, these ideas will also provide help working with array
iteration from C or C++.</p>
<section id="single-array-iteration">
<h2>Single array iteration<a class="headerlink" href="#single-array-iteration" title="Link to this heading">#</a></h2>
<p>The most basic task that can be done with the <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> is to
visit every element of an array. Each element is provided one by one
using the standard Python iterator interface.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="60aa074f-a4ee-4c1d-b5ca-970ff82a3d74">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('60aa074f-a4ee-4c1d-b5ca-970ff82a3d74','8a06aa74-ae63-445d-b7c5-0e01086a5318','494f81e2-9897-4181-bf08-55b4b4259b6f','../lite/tree/../notebooks/index.html?path=2b68243e_f018_42d2_98d3_962728dbdd5e.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 1 2 3 4 5</span>
</pre></div>
</div>
</div>
</div>
<div id="494f81e2-9897-4181-bf08-55b4b4259b6f" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('60aa074f-a4ee-4c1d-b5ca-970ff82a3d74','494f81e2-9897-4181-bf08-55b4b4259b6f')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('60aa074f-a4ee-4c1d-b5ca-970ff82a3d74','494f81e2-9897-4181-bf08-55b4b4259b6f')">Open In Tab</button></div><div id="8a06aa74-ae63-445d-b7c5-0e01086a5318" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
<p>An important thing to be aware of for this iteration is that the order
is chosen to match the memory layout of the array instead of using a
standard C or Fortran ordering. This is done for access efficiency,
reflecting the idea that by default one simply wants to visit each element
without concern for a particular ordering. We can see this by iterating
over the transpose of our previous array, compared to taking a copy
of that transpose in C order.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="81165e80-b037-49e7-bfd0-35e96ceacb5e">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('81165e80-b037-49e7-bfd0-35e96ceacb5e','82ef31a7-6f5c-42ad-8ad2-2edfa3e51568','d4529645-9481-4436-9452-37e4ccc41b4c','../lite/tree/../notebooks/index.html?path=08f2b17f_030f_4cc5_bfc5_fceacc371d5c.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 1 2 3 4 5</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="s1">'C'</span><span class="p">)):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 3 1 4 2 5</span>
</pre></div>
</div>
</div>
</div>
<div id="d4529645-9481-4436-9452-37e4ccc41b4c" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('81165e80-b037-49e7-bfd0-35e96ceacb5e','d4529645-9481-4436-9452-37e4ccc41b4c')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('81165e80-b037-49e7-bfd0-35e96ceacb5e','d4529645-9481-4436-9452-37e4ccc41b4c')">Open In Tab</button></div><div id="82ef31a7-6f5c-42ad-8ad2-2edfa3e51568" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
<p>The elements of both <em class="xref py py-obj">a</em> and <em class="xref py py-obj">a.T</em> get traversed in the same order,
namely the order they are stored in memory, whereas the elements of
<em class="xref py py-obj">a.T.copy(order=’C’)</em> get visited in a different order because they
have been put into a different memory layout.</p>
<section id="controlling-iteration-order">
<h3>Controlling iteration order<a class="headerlink" href="#controlling-iteration-order" title="Link to this heading">#</a></h3>
<p>There are times when it is important to visit the elements of an array
in a specific order, irrespective of the layout of the elements in memory.
The <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> object provides an <em class="xref py py-obj">order</em> parameter to control this
aspect of iteration. The default, having the behavior described above,
is order=’K’ to keep the existing order. This can be overridden with
order=’C’ for C order and order=’F’ for Fortran order.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="88dead75-8710-496a-8175-8b8cebecd76f">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('88dead75-8710-496a-8175-8b8cebecd76f','08877b92-cfb5-4bbe-9827-60297138d511','0115ff9f-efcb-4e4c-999a-6b5dc597c8ed','../lite/tree/../notebooks/index.html?path=d8c7e1a1_1a38_460d_a68e_100fdf59dccd.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s1">'F'</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 3 1 4 2 5</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s1">'C'</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 3 1 4 2 5</span>
</pre></div>
</div>
</div>
</div>
<div id="0115ff9f-efcb-4e4c-999a-6b5dc597c8ed" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('88dead75-8710-496a-8175-8b8cebecd76f','0115ff9f-efcb-4e4c-999a-6b5dc597c8ed')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('88dead75-8710-496a-8175-8b8cebecd76f','0115ff9f-efcb-4e4c-999a-6b5dc597c8ed')">Open In Tab</button></div><div id="08877b92-cfb5-4bbe-9827-60297138d511" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
</section>
<section id="modifying-array-values">
<span id="nditer-context-manager"></span><h3>Modifying array values<a class="headerlink" href="#modifying-array-values" title="Link to this heading">#</a></h3>
<p>By default, the <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> treats the input operand as a read-only
object. To be able to modify the array elements, you must specify either
read-write or write-only mode using the <em class="xref py py-obj">‘readwrite’</em> or <em class="xref py py-obj">‘writeonly’</em>
per-operand flags.</p>
<p>The nditer will then yield writeable buffer arrays which you may modify. However,
because the nditer must copy this buffer data back to the original array once
iteration is finished, you must signal when the iteration is ended, by one of two
methods. You may either:</p>
<ul class="simple">
<li><p>used the nditer as a context manager using the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#with" title="(in Python v3.13)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, and
the temporary data will be written back when the context is exited.</p></li>
<li><p>call the iterator’s <a class="reference internal" href="generated/numpy.nditer.close.html#numpy.nditer.close" title="numpy.nditer.close"><code class="xref py py-obj docutils literal notranslate"><span class="pre">close</span></code></a> method once finished iterating, which will trigger
the write-back.</p></li>
</ul>
<p>The nditer can no longer be iterated once either <a class="reference internal" href="generated/numpy.nditer.close.html#numpy.nditer.close" title="numpy.nditer.close"><code class="xref py py-obj docutils literal notranslate"><span class="pre">close</span></code></a> is called or its
context is exited.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="f6980dfa-4882-43ac-8d15-eaa87e1d26c1">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('f6980dfa-4882-43ac-8d15-eaa87e1d26c1','99589153-37d4-4936-bd92-1e3b4a21759b','169b87a6-1701-480c-bced-89c79e25d111','../lite/tree/../notebooks/index.html?path=04ccb827_8a47_437e_ac2d_ae1fa12dd342.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([[0, 1, 2],</span>
<span class="go"> [3, 4, 5]])</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">op_flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'readwrite'</span><span class="p">])</span> <span class="k">as</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">x</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">x</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([[ 0, 2, 4],</span>
<span class="go"> [ 6, 8, 10]])</span>
</pre></div>
</div>
</div>
</div>
<div id="169b87a6-1701-480c-bced-89c79e25d111" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('f6980dfa-4882-43ac-8d15-eaa87e1d26c1','169b87a6-1701-480c-bced-89c79e25d111')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('f6980dfa-4882-43ac-8d15-eaa87e1d26c1','169b87a6-1701-480c-bced-89c79e25d111')">Open In Tab</button></div><div id="99589153-37d4-4936-bd92-1e3b4a21759b" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
<p>If you are writing code that needs to support older versions of numpy,
note that prior to 1.15, <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> was not a context manager and
did not have a <a class="reference internal" href="generated/numpy.nditer.close.html#numpy.nditer.close" title="numpy.nditer.close"><code class="xref py py-obj docutils literal notranslate"><span class="pre">close</span></code></a> method. Instead it relied on the destructor to
initiate the writeback of the buffer.</p>
</section>
<section id="using-an-external-loop">
<h3>Using an external loop<a class="headerlink" href="#using-an-external-loop" title="Link to this heading">#</a></h3>
<p>In all the examples so far, the elements of <em class="xref py py-obj">a</em> are provided by the
iterator one at a time, because all the looping logic is internal to the
iterator. While this is simple and convenient, it is not very efficient.
A better approach is to move the one-dimensional innermost loop into your
code, external to the iterator. This way, NumPy’s vectorized operations
can be used on larger chunks of the elements being visited.</p>
<p>The <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> will try to provide chunks that are
as large as possible to the inner loop. By forcing ‘C’ and ‘F’ order,
we get different external loop sizes. This mode is enabled by specifying
an iterator flag.</p>
<p>Observe that with the default of keeping native memory order, the
iterator is able to provide a single one-dimensional chunk, whereas
when forcing Fortran order, it has to provide three chunks of two
elements each.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="9d289a93-916f-40b9-89d8-1defc49873fc">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('9d289a93-916f-40b9-89d8-1defc49873fc','366566a2-2f40-4336-97f9-1de6de9c6fcd','e444ecbc-0fba-4db9-98c5-a0698afe5a52','../lite/tree/../notebooks/index.html?path=9498478a_dbb4_46c7_9394_81e7917debdc.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'external_loop'</span><span class="p">]):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">[0 1 2 3 4 5]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'external_loop'</span><span class="p">],</span> <span class="n">order</span><span class="o">=</span><span class="s1">'F'</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">[0 3] [1 4] [2 5]</span>
</pre></div>
</div>
</div>
</div>
<div id="e444ecbc-0fba-4db9-98c5-a0698afe5a52" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('9d289a93-916f-40b9-89d8-1defc49873fc','e444ecbc-0fba-4db9-98c5-a0698afe5a52')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('9d289a93-916f-40b9-89d8-1defc49873fc','e444ecbc-0fba-4db9-98c5-a0698afe5a52')">Open In Tab</button></div><div id="366566a2-2f40-4336-97f9-1de6de9c6fcd" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
</section>
<section id="tracking-an-index-or-multi-index">
<h3>Tracking an index or multi-index<a class="headerlink" href="#tracking-an-index-or-multi-index" title="Link to this heading">#</a></h3>
<p>During iteration, you may want to use the index of the current
element in a computation. For example, you may want to visit the
elements of an array in memory order, but use a C-order, Fortran-order,
or multidimensional index to look up values in a different array.</p>
<p>The index is tracked by the iterator object itself, and accessible
through the <em class="xref py py-obj">index</em> or <em class="xref py py-obj">multi_index</em> properties, depending on what was
requested. The examples below show printouts demonstrating the
progression of the index:</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="b975969f-881b-4d4d-80d7-0e191c08e2d6">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('b975969f-881b-4d4d-80d7-0e191c08e2d6','b8df3b2a-e7a4-483e-8b12-46fe3de23005','a1ce8a19-3d80-4b3d-b9a3-7899bc96cdda','../lite/tree/../notebooks/index.html?path=ee84b5d0_10cc_4093_a2c3_93700a6ece50.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'f_index'</span><span class="p">])</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">%d</span><span class="s2"> <</span><span class="si">%d</span><span class="s2">>"</span> <span class="o">%</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">it</span><span class="o">.</span><span class="n">index</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 <0> 1 <2> 2 <4> 3 <1> 4 <3> 5 <5></span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'multi_index'</span><span class="p">])</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">%d</span><span class="s2"> <</span><span class="si">%s</span><span class="s2">>"</span> <span class="o">%</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">it</span><span class="o">.</span><span class="n">multi_index</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 <(0, 0)> 1 <(0, 1)> 2 <(0, 2)> 3 <(1, 0)> 4 <(1, 1)> 5 <(1, 2)></span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'multi_index'</span><span class="p">],</span> <span class="n">op_flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'writeonly'</span><span class="p">])</span> <span class="k">as</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">x</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">multi_index</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">it</span><span class="o">.</span><span class="n">multi_index</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([[ 0, 1, 2],</span>
<span class="go"> [-1, 0, 1]])</span>
</pre></div>
</div>
</div>
</div>
<div id="a1ce8a19-3d80-4b3d-b9a3-7899bc96cdda" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('b975969f-881b-4d4d-80d7-0e191c08e2d6','a1ce8a19-3d80-4b3d-b9a3-7899bc96cdda')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('b975969f-881b-4d4d-80d7-0e191c08e2d6','a1ce8a19-3d80-4b3d-b9a3-7899bc96cdda')">Open In Tab</button></div><div id="b8df3b2a-e7a4-483e-8b12-46fe3de23005" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
<p>Tracking an index or multi-index is incompatible with using an external
loop, because it requires a different index value per element. If
you try to combine these flags, the <a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> object will
raise an exception.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="ed6f8452-7128-430e-8f74-a2de8d9d61c9">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('ed6f8452-7128-430e-8f74-a2de8d9d61c9','b5eacc87-8188-498a-9f7c-9a0dcf377111','33172d74-ae39-4a4a-9241-ea5c6b659ed7','../lite/tree/../notebooks/index.html?path=ffb3b724_ba5c_42de_b31e_28de3041f4d5.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'c_index'</span><span class="p">,</span> <span class="s1">'external_loop'</span><span class="p">])</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">ValueError</span>: <span class="n">Iterator flag EXTERNAL_LOOP cannot be used if an index or multi-index is being tracked</span>
</pre></div>
</div>
</div>
</div>
<div id="33172d74-ae39-4a4a-9241-ea5c6b659ed7" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('ed6f8452-7128-430e-8f74-a2de8d9d61c9','33172d74-ae39-4a4a-9241-ea5c6b659ed7')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('ed6f8452-7128-430e-8f74-a2de8d9d61c9','33172d74-ae39-4a4a-9241-ea5c6b659ed7')">Open In Tab</button></div><div id="b5eacc87-8188-498a-9f7c-9a0dcf377111" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
</section>
<section id="alternative-looping-and-element-access">
<h3>Alternative looping and element access<a class="headerlink" href="#alternative-looping-and-element-access" title="Link to this heading">#</a></h3>
<p>To make its properties more readily accessible during iteration,
<a class="reference internal" href="generated/numpy.nditer.html#numpy.nditer" title="numpy.nditer"><code class="xref py py-class docutils literal notranslate"><span class="pre">nditer</span></code></a> has an alternative syntax for iterating, which works
explicitly with the iterator object itself. With this looping construct,
the current value is accessible by indexing into the iterator. Other
properties, such as tracked indices remain as before. The examples below
produce identical results to the ones in the previous section.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="7b6141d8-3d9e-4108-a14b-41e0c0a0d814">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('7b6141d8-3d9e-4108-a14b-41e0c0a0d814','d5fff5d7-e5bc-46a0-8fe5-557aec3f3c89','93474c6b-bea1-4e5e-abbb-996eb307f45a','../lite/tree/../notebooks/index.html?path=700e13b1_e9ee_4a07_8bd1_71e8c2b9303e.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'f_index'</span><span class="p">])</span>
<span class="gp">>>> </span><span class="k">while</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">finished</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">%d</span><span class="s2"> <</span><span class="si">%d</span><span class="s2">>"</span> <span class="o">%</span> <span class="p">(</span><span class="n">it</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">it</span><span class="o">.</span><span class="n">index</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">is_not_finished</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">iternext</span><span class="p">()</span>
<span class="gp">...</span>
<span class="go">0 <0> 1 <2> 2 <4> 3 <1> 4 <3> 5 <5></span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'multi_index'</span><span class="p">])</span>
<span class="gp">>>> </span><span class="k">while</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">finished</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">%d</span><span class="s2"> <</span><span class="si">%s</span><span class="s2">>"</span> <span class="o">%</span> <span class="p">(</span><span class="n">it</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">it</span><span class="o">.</span><span class="n">multi_index</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">is_not_finished</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">iternext</span><span class="p">()</span>
<span class="gp">...</span>
<span class="go">0 <(0, 0)> 1 <(0, 1)> 2 <(0, 2)> 3 <(1, 0)> 4 <(1, 1)> 5 <(1, 2)></span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'multi_index'</span><span class="p">],</span> <span class="n">op_flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'writeonly'</span><span class="p">])</span> <span class="k">as</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">while</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">finished</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">it</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">multi_index</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">it</span><span class="o">.</span><span class="n">multi_index</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">... </span> <span class="n">is_not_finished</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">iternext</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">array([[ 0, 1, 2],</span>
<span class="go"> [-1, 0, 1]])</span>
</pre></div>
</div>
</div>
</div>
<div id="93474c6b-bea1-4e5e-abbb-996eb307f45a" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('7b6141d8-3d9e-4108-a14b-41e0c0a0d814','93474c6b-bea1-4e5e-abbb-996eb307f45a')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('7b6141d8-3d9e-4108-a14b-41e0c0a0d814','93474c6b-bea1-4e5e-abbb-996eb307f45a')">Open In Tab</button></div><div id="d5fff5d7-e5bc-46a0-8fe5-557aec3f3c89" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
</section>
<section id="buffering-the-array-elements">
<h3>Buffering the array elements<a class="headerlink" href="#buffering-the-array-elements" title="Link to this heading">#</a></h3>
<p>When forcing an iteration order, we observed that the external loop
option may provide the elements in smaller chunks because the elements
can’t be visited in the appropriate order with a constant stride.
When writing C code, this is generally fine, however in pure Python code
this can cause a significant reduction in performance.</p>
<p>By enabling buffering mode, the chunks provided by the iterator to
the inner loop can be made larger, significantly reducing the overhead
of the Python interpreter. In the example forcing Fortran iteration order,
the inner loop gets to see all the elements in one go when buffering
is enabled.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="f59a2d6e-f706-4f96-b516-2bb2d9613928">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('f59a2d6e-f706-4f96-b516-2bb2d9613928','d4e64e96-dbca-4935-b0fe-1bdf74661cb3','d2d82db5-84df-479e-bb51-a8b373747e2a','../lite/tree/../notebooks/index.html?path=94cc4943_7d34_46d6_8a97_7dbbb1fb06cf.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'external_loop'</span><span class="p">],</span> <span class="n">order</span><span class="o">=</span><span class="s1">'F'</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">[0 3] [1 4] [2 5]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'external_loop'</span><span class="p">,</span><span class="s1">'buffered'</span><span class="p">],</span> <span class="n">order</span><span class="o">=</span><span class="s1">'F'</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">[0 3 1 4 2 5]</span>
</pre></div>
</div>
</div>
</div>
<div id="d2d82db5-84df-479e-bb51-a8b373747e2a" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('f59a2d6e-f706-4f96-b516-2bb2d9613928','d2d82db5-84df-479e-bb51-a8b373747e2a')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('f59a2d6e-f706-4f96-b516-2bb2d9613928','d2d82db5-84df-479e-bb51-a8b373747e2a')">Open In Tab</button></div><div id="d4e64e96-dbca-4935-b0fe-1bdf74661cb3" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
</section>
<section id="iterating-as-a-specific-data-type">
<h3>Iterating as a specific data type<a class="headerlink" href="#iterating-as-a-specific-data-type" title="Link to this heading">#</a></h3>
<p>There are times when it is necessary to treat an array as a different
data type than it is stored as. For instance, one may want to do all
computations on 64-bit floats, even if the arrays being manipulated
are 32-bit floats. Except when writing low-level C code, it’s generally
better to let the iterator handle the copying or buffering instead
of casting the data type yourself in the inner loop.</p>
<p>There are two mechanisms which allow this to be done, temporary copies
and buffering mode. With temporary copies, a copy of the entire array is
made with the new data type, then iteration is done in the copy. Write
access is permitted through a mode which updates the original array after
all the iteration is complete. The major drawback of temporary copies is
that the temporary copy may consume a large amount of memory, particularly
if the iteration data type has a larger itemsize than the original one.</p>
<p>Buffering mode mitigates the memory usage issue and is more cache-friendly
than making temporary copies. Except for special cases, where the whole
array is needed at once outside the iterator, buffering is recommended
over temporary copying. Within NumPy, buffering is used by the ufuncs and
other functions to support flexible inputs with minimal memory overhead.</p>
<p>In our examples, we will treat the input array with a complex data type,
so that we can take square roots of negative numbers. Without enabling
copies or buffering mode, the iterator will raise an exception if the
data type doesn’t match precisely.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="ef577772-186b-4182-b211-f7f3958943ba">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('ef577772-186b-4182-b211-f7f3958943ba','534b46ad-e1ee-4ce4-8c51-abdfe29ed3e5','4ca05ff1-2b4e-47dd-86b6-497412b5c2df','../lite/tree/../notebooks/index.html?path=a24b8709_ea94_4721_acef_36949f769893.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">-</span> <span class="mi">3</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="s1">'complex128'</span><span class="p">]):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">TypeError</span>: <span class="n">Iterator operand required copying or buffering, but neither copying nor buffering was enabled</span>
</pre></div>
</div>
</div>
</div>
<div id="4ca05ff1-2b4e-47dd-86b6-497412b5c2df" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('ef577772-186b-4182-b211-f7f3958943ba','4ca05ff1-2b4e-47dd-86b6-497412b5c2df')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('ef577772-186b-4182-b211-f7f3958943ba','4ca05ff1-2b4e-47dd-86b6-497412b5c2df')">Open In Tab</button></div><div id="534b46ad-e1ee-4ce4-8c51-abdfe29ed3e5" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
<p>In copying mode, ‘copy’ is specified as a per-operand flag. This is
done to provide control in a per-operand fashion. Buffering mode is
specified as an iterator flag.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="00792dc7-86e4-4ee0-8691-6cd400bad4e8">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('00792dc7-86e4-4ee0-8691-6cd400bad4e8','e70abbcd-63de-4a16-af1d-11f26739db97','2a1279bf-2b5b-429a-b993-7dc176d74b69','../lite/tree/../notebooks/index.html?path=9e5093e0_62f5_4b89_bb99_06cb665dc919.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="o">-</span> <span class="mi">3</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">op_flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'readonly'</span><span class="p">,</span><span class="s1">'copy'</span><span class="p">],</span>
<span class="gp">... </span> <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="s1">'complex128'</span><span class="p">]):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">1.7320508075688772j 1.4142135623730951j 1j 0j (1+0j) (1.4142135623730951+0j)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'buffered'</span><span class="p">],</span> <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="s1">'complex128'</span><span class="p">]):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">1.7320508075688772j 1.4142135623730951j 1j 0j (1+0j) (1.4142135623730951+0j)</span>
</pre></div>
</div>
</div>
</div>
<div id="2a1279bf-2b5b-429a-b993-7dc176d74b69" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('00792dc7-86e4-4ee0-8691-6cd400bad4e8','2a1279bf-2b5b-429a-b993-7dc176d74b69')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('00792dc7-86e4-4ee0-8691-6cd400bad4e8','2a1279bf-2b5b-429a-b993-7dc176d74b69')">Open In Tab</button></div><div id="e70abbcd-63de-4a16-af1d-11f26739db97" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
<p>The iterator uses NumPy’s casting rules to determine whether a specific
conversion is permitted. By default, it enforces ‘safe’ casting. This means,
for example, that it will raise an exception if you try to treat a
64-bit float array as a 32-bit float array. In many cases, the rule
‘same_kind’ is the most reasonable rule to use, since it will allow
conversion from 64 to 32-bit float, but not from float to int or from
complex to float.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="33a3a5a9-3e23-4710-b17e-981877cfca1d">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('33a3a5a9-3e23-4710-b17e-981877cfca1d','0ebbd020-b97d-4a6f-811e-fbf3b093aff1','b24d076e-febe-4764-ad4f-679a5591d57f','../lite/tree/../notebooks/index.html?path=5a6c10ef_76ae_4791_a54e_74941cb3769a.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">6.</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'buffered'</span><span class="p">],</span> <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="s1">'float32'</span><span class="p">]):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">TypeError</span>: <span class="n">Iterator operand 0 dtype could not be cast from dtype('float64') to dtype('float32') according to the rule 'safe'</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'buffered'</span><span class="p">],</span> <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="s1">'float32'</span><span class="p">],</span>
<span class="gp">... </span> <span class="n">casting</span><span class="o">=</span><span class="s1">'same_kind'</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0.0 1.0 2.0 3.0 4.0 5.0</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'buffered'</span><span class="p">],</span> <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="s1">'int32'</span><span class="p">],</span> <span class="n">casting</span><span class="o">=</span><span class="s1">'same_kind'</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">TypeError</span>: <span class="n">Iterator operand 0 dtype could not be cast from dtype('float64') to dtype('int32') according to the rule 'same_kind'</span>
</pre></div>
</div>
</div>
</div>
<div id="b24d076e-febe-4764-ad4f-679a5591d57f" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('33a3a5a9-3e23-4710-b17e-981877cfca1d','b24d076e-febe-4764-ad4f-679a5591d57f')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('33a3a5a9-3e23-4710-b17e-981877cfca1d','b24d076e-febe-4764-ad4f-679a5591d57f')">Open In Tab</button></div><div id="0ebbd020-b97d-4a6f-811e-fbf3b093aff1" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
<p>One thing to watch out for is conversions back to the original data
type when using a read-write or write-only operand. A common case is
to implement the inner loop in terms of 64-bit floats, and use ‘same_kind’
casting to allow the other floating-point types to be processed as well.
While in read-only mode, an integer array could be provided, read-write
mode will raise an exception because conversion back to the array
would violate the casting rule.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="try_examples_outer_container docutils container" id="c7bf62df-00ff-44b6-8767-e81d4fb7854f">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('c7bf62df-00ff-44b6-8767-e81d4fb7854f','21821ffa-531c-40e9-a0fb-a2392ff51f4e','95006f6f-bb76-4bc0-8e38-5cde83138768','../lite/tree/../notebooks/index.html?path=8ff88d0b_055c_4ff7_97b0_8392ac5464fa.ipynb','None')">Try it in your browser!</button></div><div class="try_examples_content docutils container">
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'buffered'</span><span class="p">],</span> <span class="n">op_flags</span><span class="o">=</span><span class="p">[</span><span class="s1">'readwrite'</span><span class="p">],</span>
<span class="gp">... </span> <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="s1">'float64'</span><span class="p">],</span> <span class="n">casting</span><span class="o">=</span><span class="s1">'same_kind'</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">x</span> <span class="o">/</span> <span class="mf">2.0</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">2</span>, in <span class="n"><module></span>
<span class="gr">TypeError</span>: <span class="n">Iterator requested dtype could not be cast from dtype('float64') to dtype('int64'), the operand 0 dtype, according to the rule 'same_kind'</span>
</pre></div>
</div>
</div>
</div>
<div id="95006f6f-bb76-4bc0-8e38-5cde83138768" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('c7bf62df-00ff-44b6-8767-e81d4fb7854f','95006f6f-bb76-4bc0-8e38-5cde83138768')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('c7bf62df-00ff-44b6-8767-e81d4fb7854f','95006f6f-bb76-4bc0-8e38-5cde83138768')">Open In Tab</button></div><div id="21821ffa-531c-40e9-a0fb-a2392ff51f4e" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></div>
</section>