-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharrays.classes.html
1406 lines (1198 loc) · 112 KB
/
arrays.classes.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>Standard array subclasses — 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=9cbc30f1"></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 async="async" src="../_static/scipy-mathjax/MathJax.js?config=scipy-mathjax"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'reference/arrays.classes';</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="numpy.matrix.T" href="generated/numpy.matrix.T.html" />
<link rel="prev" title="Iterating over arrays" href="arrays.nditer.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 19, 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"><a class="reference internal" href="arrays.nditer.html">Iterating over arrays</a></li>
<li class="toctree-l2 current active has-children"><a class="current reference internal" href="#">Standard array subclasses</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.matrix.T.html">numpy.matrix.T</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.matrix.H.html">numpy.matrix.H</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.matrix.I.html">numpy.matrix.I</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.matrix.A.html">numpy.matrix.A</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.matrix.html">numpy.matrix</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.asmatrix.html">numpy.asmatrix</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.bmat.html">numpy.bmat</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.memmap.html">numpy.memmap</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.memmap.flush.html">numpy.memmap.flush</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.char.chararray.html">numpy.char.chararray</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.char.array.html">numpy.char.array</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.recarray.html">numpy.recarray</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.record.html">numpy.record</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.lib.user_array.container.html">numpy.lib.user_array.container</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.ndarray.flat.html">numpy.ndarray.flat</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.ndenumerate.html">numpy.ndenumerate</a></li>
<li class="toctree-l3"><a class="reference internal" href="generated/numpy.broadcast.html">numpy.broadcast</a></li>
</ul>
</details></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">Standard array subclasses</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="standard-array-subclasses">
<span id="arrays-classes"></span><h1>Standard array subclasses<a class="headerlink" href="#standard-array-subclasses" title="Link to this heading">#</a></h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Subclassing a <code class="docutils literal notranslate"><span class="pre">numpy.ndarray</span></code> is possible but if your goal is to create
an array with <em>modified</em> behavior, as do dask arrays for distributed
computation and cupy arrays for GPU-based computation, subclassing is
discouraged. Instead, using numpy’s
<a class="reference internal" href="../user/basics.dispatch.html#basics-dispatch"><span class="std std-ref">dispatch mechanism</span></a> is recommended.</p>
</div>
<p>The <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a> can be inherited from (in Python or in C)
if desired. Therefore, it can form a foundation for many useful
classes. Often whether to sub-class the array object or to simply use
the core array component as an internal part of a new class is a
difficult decision, and can be simply a matter of choice. NumPy has
several tools for simplifying how your new object interacts with other
array objects, and so the choice may not be significant in the
end. One way to simplify the question is by asking yourself if the
object you are interested in can be replaced as a single array or does
it really require two or more arrays at its core.</p>
<p>Note that <a class="reference internal" href="generated/numpy.asarray.html#numpy.asarray" title="numpy.asarray"><code class="xref py py-func docutils literal notranslate"><span class="pre">asarray</span></code></a> always returns the base-class ndarray. If
you are confident that your use of the array object can handle any
subclass of an ndarray, then <a class="reference internal" href="generated/numpy.asanyarray.html#numpy.asanyarray" title="numpy.asanyarray"><code class="xref py py-func docutils literal notranslate"><span class="pre">asanyarray</span></code></a> can be used to allow
subclasses to propagate more cleanly through your subroutine. In
principal a subclass could redefine any aspect of the array and
therefore, under strict guidelines, <a class="reference internal" href="generated/numpy.asanyarray.html#numpy.asanyarray" title="numpy.asanyarray"><code class="xref py py-func docutils literal notranslate"><span class="pre">asanyarray</span></code></a> would rarely be
useful. However, most subclasses of the array object will not
redefine certain aspects of the array object such as the buffer
interface, or the attributes of the array. One important example,
however, of why your subroutine may not be able to handle an arbitrary
subclass of an array is that matrices redefine the “*” operator to be
matrix-multiplication, rather than element-by-element multiplication.</p>
<section id="special-attributes-and-methods">
<span id="id1"></span><h2>Special attributes and methods<a class="headerlink" href="#special-attributes-and-methods" title="Link to this heading">#</a></h2>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../user/basics.subclassing.html#basics-subclassing"><span class="std std-ref">Subclassing ndarray</span></a></p>
</div>
<p>NumPy provides several hooks that classes can customize:</p>
<dl class="py method">
<dt class="sig sig-object py" id="numpy.class.__array_ufunc__">
<span class="sig-prename descclassname"><span class="pre">class.</span></span><span class="sig-name descname"><span class="pre">__array_ufunc__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ufunc</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">inputs</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.class.__array_ufunc__" title="Link to this definition">#</a></dt>
<dd><p>Any class, ndarray subclass or not, can define this method or set it to
None in order to override the behavior of NumPy’s ufuncs. This works
quite similarly to Python’s <code class="docutils literal notranslate"><span class="pre">__mul__</span></code> and other binary operation routines.</p>
<ul class="simple">
<li><p><em>ufunc</em> is the ufunc object that was called.</p></li>
<li><p><em>method</em> is a string indicating which Ufunc method was called
(one of <code class="docutils literal notranslate"><span class="pre">"__call__"</span></code>, <code class="docutils literal notranslate"><span class="pre">"reduce"</span></code>, <code class="docutils literal notranslate"><span class="pre">"reduceat"</span></code>,
<code class="docutils literal notranslate"><span class="pre">"accumulate"</span></code>, <code class="docutils literal notranslate"><span class="pre">"outer"</span></code>, <code class="docutils literal notranslate"><span class="pre">"inner"</span></code>).</p></li>
<li><p><em>inputs</em> is a tuple of the input arguments to the <code class="docutils literal notranslate"><span class="pre">ufunc</span></code>.</p></li>
<li><p><em>kwargs</em> is a dictionary containing the optional input arguments
of the ufunc. If given, any <code class="docutils literal notranslate"><span class="pre">out</span></code> arguments, both positional
and keyword, are passed as a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.13)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">tuple</span></code></a> in <em>kwargs</em>. See the
discussion in <a class="reference internal" href="ufuncs.html#ufuncs"><span class="std std-ref">Universal functions (ufunc)</span></a> for details.</p></li>
</ul>
<p>The method should return either the result of the operation, or
<a class="reference external" href="https://docs.python.org/3/library/constants.html#NotImplemented" title="(in Python v3.13)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> if the operation requested is not implemented.</p>
<p>If one of the input, output, or <code class="docutils literal notranslate"><span class="pre">where</span></code> arguments has a <a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a>
method, it is executed <em>instead</em> of the ufunc. If more than one of the
arguments implements <a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a>, they are tried in the
order: subclasses before superclasses, inputs before outputs,
outputs before <code class="docutils literal notranslate"><span class="pre">where</span></code>, otherwise
left to right. The first routine returning something other than
<a class="reference external" href="https://docs.python.org/3/library/constants.html#NotImplemented" title="(in Python v3.13)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> determines the result. If all of the
<a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a> operations return <a class="reference external" href="https://docs.python.org/3/library/constants.html#NotImplemented" title="(in Python v3.13)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">NotImplemented</span></code></a>, a
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.13)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>We intend to re-implement numpy functions as (generalized)
Ufunc, in which case it will become possible for them to be
overridden by the <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span></code> method. A prime candidate is
<a class="reference internal" href="generated/numpy.matmul.html#numpy.matmul" title="numpy.matmul"><code class="xref py py-func docutils literal notranslate"><span class="pre">matmul</span></code></a>, which currently is not a Ufunc, but could be
relatively easily be rewritten as a (set of) generalized Ufuncs. The
same may happen with functions such as <a class="reference internal" href="generated/numpy.median.html#numpy.median" title="numpy.median"><code class="xref py py-func docutils literal notranslate"><span class="pre">median</span></code></a>,
<a class="reference internal" href="generated/numpy.amin.html#numpy.amin" title="numpy.amin"><code class="xref py py-func docutils literal notranslate"><span class="pre">amin</span></code></a>, and <a class="reference internal" href="generated/numpy.argsort.html#numpy.argsort" title="numpy.argsort"><code class="xref py py-func docutils literal notranslate"><span class="pre">argsort</span></code></a>.</p>
</div>
<p>Like with some other special methods in python, such as <code class="docutils literal notranslate"><span class="pre">__hash__</span></code> and
<code class="docutils literal notranslate"><span class="pre">__iter__</span></code>, it is possible to indicate that your class does <em>not</em>
support ufuncs by setting <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span> <span class="pre">=</span> <span class="pre">None</span></code>. Ufuncs always raise
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.13)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> when called on an object that sets
<code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span> <span class="pre">=</span> <span class="pre">None</span></code>.</p>
<p>The presence of <a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a> also influences how
<a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a> handles binary operations like <code class="docutils literal notranslate"><span class="pre">arr</span> <span class="pre">+</span> <span class="pre">obj</span></code> and <code class="docutils literal notranslate"><span class="pre">arr</span>
<span class="pre"><</span> <span class="pre">obj</span></code> when <code class="docutils literal notranslate"><span class="pre">arr</span></code> is an <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a> and <code class="docutils literal notranslate"><span class="pre">obj</span></code> is an instance
of a custom class. There are two possibilities. If
<code class="docutils literal notranslate"><span class="pre">obj.__array_ufunc__</span></code> is present and not None, then
<code class="docutils literal notranslate"><span class="pre">ndarray.__add__</span></code> and friends will delegate to the ufunc machinery,
meaning that <code class="docutils literal notranslate"><span class="pre">arr</span> <span class="pre">+</span> <span class="pre">obj</span></code> becomes <code class="docutils literal notranslate"><span class="pre">np.add(arr,</span> <span class="pre">obj)</span></code>, and then
<a class="reference internal" href="generated/numpy.add.html#numpy.add" title="numpy.add"><code class="xref py py-func docutils literal notranslate"><span class="pre">add</span></code></a> invokes <code class="docutils literal notranslate"><span class="pre">obj.__array_ufunc__</span></code>. This is useful if you
want to define an object that acts like an array.</p>
<p>Alternatively, if <code class="docutils literal notranslate"><span class="pre">obj.__array_ufunc__</span></code> is set to None, then as a
special case, special methods like <code class="docutils literal notranslate"><span class="pre">ndarray.__add__</span></code> will notice this
and <em>unconditionally</em> raise <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.13)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. This is useful if you want to
create objects that interact with arrays via binary operations, but
are not themselves arrays. For example, a units handling system might have
an object <code class="docutils literal notranslate"><span class="pre">m</span></code> representing the “meters” unit, and want to support the
syntax <code class="docutils literal notranslate"><span class="pre">arr</span> <span class="pre">*</span> <span class="pre">m</span></code> to represent that the array has units of “meters”, but
not want to otherwise interact with arrays via ufuncs or otherwise. This
can be done by setting <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span> <span class="pre">=</span> <span class="pre">None</span></code> and defining <code class="docutils literal notranslate"><span class="pre">__mul__</span></code>
and <code class="docutils literal notranslate"><span class="pre">__rmul__</span></code> methods. (Note that this means that writing an
<code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span></code> that always returns <a class="reference external" href="https://docs.python.org/3/library/constants.html#NotImplemented" title="(in Python v3.13)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> is not
quite the same as setting <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span> <span class="pre">=</span> <span class="pre">None</span></code>: in the former
case, <code class="docutils literal notranslate"><span class="pre">arr</span> <span class="pre">+</span> <span class="pre">obj</span></code> will raise <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.13)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>, while in the latter
case it is possible to define a <code class="docutils literal notranslate"><span class="pre">__radd__</span></code> method to prevent this.)</p>
<p>The above does not hold for in-place operators, for which <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a>
never returns <a class="reference external" href="https://docs.python.org/3/library/constants.html#NotImplemented" title="(in Python v3.13)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">NotImplemented</span></code></a>. Hence, <code class="docutils literal notranslate"><span class="pre">arr</span> <span class="pre">+=</span> <span class="pre">obj</span></code> would always
lead to a <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.13)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. This is because for arrays in-place operations
cannot generically be replaced by a simple reverse operation. (For
instance, by default, <code class="docutils literal notranslate"><span class="pre">arr</span> <span class="pre">+=</span> <span class="pre">obj</span></code> would be translated to <code class="docutils literal notranslate"><span class="pre">arr</span> <span class="pre">=</span>
<span class="pre">arr</span> <span class="pre">+</span> <span class="pre">obj</span></code>, i.e., <code class="docutils literal notranslate"><span class="pre">arr</span></code> would be replaced, contrary to what is expected
for in-place array operations.)</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If you define <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span></code>:</p>
<ul class="simple">
<li><p>If you are not a subclass of <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a>, we recommend your
class define special methods like <code class="docutils literal notranslate"><span class="pre">__add__</span></code> and <code class="docutils literal notranslate"><span class="pre">__lt__</span></code> that
delegate to ufuncs just like ndarray does. An easy way to do this
is to subclass from <a class="reference internal" href="generated/numpy.lib.mixins.NDArrayOperatorsMixin.html#numpy.lib.mixins.NDArrayOperatorsMixin" title="numpy.lib.mixins.NDArrayOperatorsMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">NDArrayOperatorsMixin</span></code></a>.</p></li>
<li><p>If you subclass <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a>, we recommend that you put all your
override logic in <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span></code> and not also override special
methods. This ensures the class hierarchy is determined in only one
place rather than separately by the ufunc machinery and by the binary
operation rules (which gives preference to special methods of
subclasses; the alternative way to enforce a one-place only hierarchy,
of setting <a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a> to None, would seem very
unexpected and thus confusing, as then the subclass would not work at
all with ufuncs).</p></li>
<li><p><a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a> defines its own <a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a>, which,
evaluates the ufunc if no arguments have overrides, and returns
<a class="reference external" href="https://docs.python.org/3/library/constants.html#NotImplemented" title="(in Python v3.13)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> otherwise. This may be useful for subclasses
for which <a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a> converts any instances of its own
class to <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a>: it can then pass these on to its
superclass using <code class="docutils literal notranslate"><span class="pre">super().__array_ufunc__(*inputs,</span> <span class="pre">**kwargs)</span></code>,
and finally return the results after possible back-conversion. The
advantage of this practice is that it ensures that it is possible
to have a hierarchy of subclasses that extend the behaviour. See
<a class="reference internal" href="../user/basics.subclassing.html#basics-subclassing"><span class="std std-ref">Subclassing ndarray</span></a> for details.</p></li>
</ul>
</div>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="numpy.class.__array_function__">
<span class="sig-prename descclassname"><span class="pre">class.</span></span><span class="sig-name descname"><span class="pre">__array_function__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">types</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.class.__array_function__" title="Link to this definition">#</a></dt>
<dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">func</span></code> is an arbitrary callable exposed by NumPy’s public API,
which was called in the form <code class="docutils literal notranslate"><span class="pre">func(*args,</span> <span class="pre">**kwargs)</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">types</span></code> is a collection <a class="reference external" href="https://docs.python.org/3/library/collections.abc.html#collections.abc.Collection" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Collection</span></code></a>
of unique argument types from the original NumPy function call that
implement <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code>.</p></li>
<li><p>The tuple <code class="docutils literal notranslate"><span class="pre">args</span></code> and dict <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> are directly passed on from the
original call.</p></li>
</ul>
<p>As a convenience for <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> implementers, <code class="docutils literal notranslate"><span class="pre">types</span></code>
provides all argument types with an <code class="docutils literal notranslate"><span class="pre">'__array_function__'</span></code> attribute.
This allows implementers to quickly identify cases where they should defer
to <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> implementations on other arguments.
Implementations should not rely on the iteration order of <code class="docutils literal notranslate"><span class="pre">types</span></code>.</p>
<p>Most implementations of <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> will start with two
checks:</p>
<ol class="arabic simple">
<li><p>Is the given function something that we know how to overload?</p></li>
<li><p>Are all arguments of a type that we know how to handle?</p></li>
</ol>
<p>If these conditions hold, <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> should return the result
from calling its implementation for <code class="docutils literal notranslate"><span class="pre">func(*args,</span> <span class="pre">**kwargs)</span></code>. Otherwise,
it should return the sentinel value <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code>, indicating that the
function is not implemented by these types.</p>
<p>There are no general requirements on the return value from
<code class="docutils literal notranslate"><span class="pre">__array_function__</span></code>, although most sensible implementations should
probably return array(s) with the same type as one of the function’s
arguments.</p>
<p>It may also be convenient to define a custom decorators (<code class="docutils literal notranslate"><span class="pre">implements</span></code>
below) for registering <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> implementations.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">HANDLED_FUNCTIONS</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">class</span><span class="w"> </span><span class="nc">MyArray</span><span class="p">:</span>
<span class="k">def</span><span class="w"> </span><span class="nf">__array_function__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">types</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="n">func</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">HANDLED_FUNCTIONS</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">NotImplemented</span>
<span class="c1"># Note: this allows subclasses that don't override</span>
<span class="c1"># __array_function__ to handle MyArray objects</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="nb">issubclass</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">MyArray</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">types</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">NotImplemented</span>
<span class="k">return</span> <span class="n">HANDLED_FUNCTIONS</span><span class="p">[</span><span class="n">func</span><span class="p">](</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">implements</span><span class="p">(</span><span class="n">numpy_function</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Register an __array_function__ implementation for MyArray objects."""</span>
<span class="k">def</span><span class="w"> </span><span class="nf">decorator</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
<span class="n">HANDLED_FUNCTIONS</span><span class="p">[</span><span class="n">numpy_function</span><span class="p">]</span> <span class="o">=</span> <span class="n">func</span>
<span class="k">return</span> <span class="n">func</span>
<span class="k">return</span> <span class="n">decorator</span>
<span class="nd">@implements</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">concatenate</span><span class="p">(</span><span class="n">arrays</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="o">...</span> <span class="c1"># implementation of concatenate for MyArray objects</span>
<span class="nd">@implements</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">broadcast_to</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">broadcast_to</span><span class="p">(</span><span class="n">array</span><span class="p">,</span> <span class="n">shape</span><span class="p">):</span>
<span class="o">...</span> <span class="c1"># implementation of broadcast_to for MyArray objects</span>
</pre></div>
</div>
<p>Note that it is not required for <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> implementations to
include <em>all</em> of the corresponding NumPy function’s optional arguments
(e.g., <code class="docutils literal notranslate"><span class="pre">broadcast_to</span></code> above omits the irrelevant <code class="docutils literal notranslate"><span class="pre">subok</span></code> argument).
Optional arguments are only passed in to <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> if they
were explicitly used in the NumPy function call.</p>
<p>Just like the case for builtin special methods like <code class="docutils literal notranslate"><span class="pre">__add__</span></code>, properly
written <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> methods should always return
<code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> when an unknown type is encountered. Otherwise, it will
be impossible to correctly override NumPy functions from another object
if the operation also includes one of your objects.</p>
<p>For the most part, the rules for dispatch with <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code>
match those for <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span></code>. In particular:</p>
<ul class="simple">
<li><p>NumPy will gather implementations of <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> from all
specified inputs and call them in order: subclasses before
superclasses, and otherwise left to right. Note that in some edge cases
involving subclasses, this differs slightly from the
<a class="reference external" href="https://bugs.python.org/issue30140">current behavior</a> of Python.</p></li>
<li><p>Implementations of <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> indicate that they can
handle the operation by returning any value other than
<code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code>.</p></li>
<li><p>If all <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> methods return <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code>,
NumPy will raise <code class="docutils literal notranslate"><span class="pre">TypeError</span></code>.</p></li>
</ul>
<p>If no <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> methods exists, NumPy will default to calling
its own implementation, intended for use on NumPy arrays. This case arises,
for example, when all array-like arguments are Python numbers or lists.
(NumPy arrays do have a <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> method, given below, but it
always returns <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> if any argument other than a NumPy array
subclass implements <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code>.)</p>
<p>One deviation from the current behavior of <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span></code> is that
NumPy will only call <code class="docutils literal notranslate"><span class="pre">__array_function__</span></code> on the <em>first</em> argument of each
unique type. This matches Python’s <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__ror__">rule for calling reflected methods</a>, and
this ensures that checking overloads has acceptable performance even when
there are a large number of overloaded arguments.</p>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="numpy.class.__array_finalize__">
<span class="sig-prename descclassname"><span class="pre">class.</span></span><span class="sig-name descname"><span class="pre">__array_finalize__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.class.__array_finalize__" title="Link to this definition">#</a></dt>
<dd><p>This method is called whenever the system internally allocates a
new array from <em>obj</em>, where <em>obj</em> is a subclass (subtype) of the
<a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a>. It can be used to change attributes of <em>self</em>
after construction (so as to ensure a 2-d matrix for example), or
to update meta-information from the “parent.” Subclasses inherit
a default implementation of this method that does nothing.</p>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="numpy.class.__array_wrap__">
<span class="sig-prename descclassname"><span class="pre">class.</span></span><span class="sig-name descname"><span class="pre">__array_wrap__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">array</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">return_scalar</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.class.__array_wrap__" title="Link to this definition">#</a></dt>
<dd><p>At the end of every <a class="reference internal" href="../user/basics.ufuncs.html#ufuncs-output-type"><span class="std std-ref">ufunc</span></a>, this method
is called on the input object with the highest array priority, or
the output object if one was specified. The ufunc-computed array
is passed in and whatever is returned is passed to the user.
Subclasses inherit a default implementation of this method, which
transforms the array into a new instance of the object’s class.
Subclasses may opt to use this method to transform the output array
into an instance of the subclass and update metadata before
returning the array to the user.</p>
<p>NumPy may also call this function without a context from non-ufuncs to
allow preserving subclass information.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 2.0: </span><code class="docutils literal notranslate"><span class="pre">return_scalar</span></code> is now passed as either <code class="docutils literal notranslate"><span class="pre">False</span></code> (usually) or <code class="docutils literal notranslate"><span class="pre">True</span></code>
indicating that NumPy would return a scalar.
Subclasses may ignore the value, or return <code class="docutils literal notranslate"><span class="pre">array[()]</span></code> to behave more
like NumPy.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>It is hoped to eventually deprecate this method in favour of
<a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a> for ufuncs (and <a class="reference internal" href="#numpy.class.__array_function__" title="numpy.class.__array_function__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_function__</span></code></a>
for a few other functions like <a class="reference internal" href="generated/numpy.squeeze.html#numpy.squeeze" title="numpy.squeeze"><code class="xref py py-func docutils literal notranslate"><span class="pre">numpy.squeeze</span></code></a>).</p>
</div>
</dd></dl>
<dl class="py attribute">
<dt class="sig sig-object py" id="numpy.class.__array_priority__">
<span class="sig-prename descclassname"><span class="pre">class.</span></span><span class="sig-name descname"><span class="pre">__array_priority__</span></span><a class="headerlink" href="#numpy.class.__array_priority__" title="Link to this definition">#</a></dt>
<dd><p>The value of this attribute is used to determine what type of
object to return in situations where there is more than one
possibility for the Python type of the returned object. Subclasses
inherit a default value of 0.0 for this attribute.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>For ufuncs, it is hoped to eventually deprecate this method in
favour of <a class="reference internal" href="#numpy.class.__array_ufunc__" title="numpy.class.__array_ufunc__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array_ufunc__</span></code></a>.</p>
</div>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="numpy.class.__array__">
<span class="sig-prename descclassname"><span class="pre">class.</span></span><span class="sig-name descname"><span class="pre">__array__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dtype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">copy</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.class.__array__" title="Link to this definition">#</a></dt>
<dd><p>If defined on an object, it must return a NumPy <code class="docutils literal notranslate"><span class="pre">ndarray</span></code>.
This method is called by array-coercion functions like <code class="docutils literal notranslate"><span class="pre">np.array()</span></code>
if an object implementing this interface is passed to those functions.</p>
<p>Third-party implementations of <code class="docutils literal notranslate"><span class="pre">__array__</span></code> must take <code class="docutils literal notranslate"><span class="pre">dtype</span></code> and
<code class="docutils literal notranslate"><span class="pre">copy</span></code> arguments.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version NumPy: </span>2.0
Not implementing <code class="docutils literal notranslate"><span class="pre">copy</span></code> and <code class="docutils literal notranslate"><span class="pre">dtype</span></code> is deprecated as of NumPy 2.
When adding them, you must ensure correct behavior for <code class="docutils literal notranslate"><span class="pre">copy</span></code>.</p>
</div>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">dtype</span></code> is the requested data type of the returned array and is passed
by NumPy positionally (only if requested by the user).
It is acceptable to ignore the <code class="docutils literal notranslate"><span class="pre">dtype</span></code> because NumPy will check the
result and cast to <code class="docutils literal notranslate"><span class="pre">dtype</span></code> if necessary. If it is more efficient to
coerce the data to the requested dtype without relying on NumPy,
you should handle it in your library.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">copy</span></code> is a boolean passed by keyword. If <code class="docutils literal notranslate"><span class="pre">copy=True</span></code> you <em>must</em>
return a copy. Returning a view into existing data will lead to incorrect
user code.
If <code class="docutils literal notranslate"><span class="pre">copy=False</span></code> the user requested that a copy is never made and you <em>must</em>
raise an error unless no copy is made and the returned array is a view into
existing data. It is valid to always raise an error for <code class="docutils literal notranslate"><span class="pre">copy=False</span></code>.
The default <code class="docutils literal notranslate"><span class="pre">copy=None</span></code> (not passed) allows for the result to either be a
view or a copy. However, a view return should be preferred when possible.</p></li>
</ul>
<p>Please refer to <a class="reference internal" href="../user/basics.interoperability.html#basics-interoperability"><span class="std std-ref">Interoperability with NumPy</span></a>
for the protocol hierarchy, of which <code class="docutils literal notranslate"><span class="pre">__array__</span></code> is the oldest and least
desirable.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If a class (ndarray subclass or not) having the <a class="reference internal" href="#numpy.class.__array__" title="numpy.class.__array__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array__</span></code></a>
method is used as the output object of an <a class="reference internal" href="../user/basics.ufuncs.html#ufuncs-output-type"><span class="std std-ref">ufunc</span></a>, results will <em>not</em> be written to the object
returned by <a class="reference internal" href="#numpy.class.__array__" title="numpy.class.__array__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__array__</span></code></a>. This practice will return <code class="docutils literal notranslate"><span class="pre">TypeError</span></code>.</p>
</div>
</dd></dl>
</section>
<section id="matrix-objects">
<span id="id2"></span><h2>Matrix objects<a class="headerlink" href="#matrix-objects" title="Link to this heading">#</a></h2>
<div class="admonition note" id="index-0">
<p class="admonition-title">Note</p>
<p>It is strongly advised <em>not</em> to use the matrix subclass. As described
below, it makes writing functions that deal consistently with matrices
and regular arrays very difficult. Currently, they are mainly used for
interacting with <code class="docutils literal notranslate"><span class="pre">scipy.sparse</span></code>. We hope to provide an alternative
for this use, however, and eventually remove the <code class="docutils literal notranslate"><span class="pre">matrix</span></code> subclass.</p>
</div>
<p><a class="reference internal" href="generated/numpy.matrix.html#numpy.matrix" title="numpy.matrix"><code class="xref py py-class docutils literal notranslate"><span class="pre">matrix</span></code></a> objects inherit from the ndarray and therefore, they
have the same attributes and methods of ndarrays. There are six
important differences of matrix objects, however, that may lead to
unexpected results when you use matrices but expect them to act like
arrays:</p>
<ol class="arabic">
<li><p>Matrix objects can be created using a string notation to allow
Matlab-style syntax where spaces separate columns and semicolons
(‘;’) separate rows.</p></li>
<li><p>Matrix objects are always two-dimensional. This has far-reaching
implications, in that m.ravel() is still two-dimensional (with a 1
in the first dimension) and item selection returns two-dimensional
objects so that sequence behavior is fundamentally different than
arrays.</p></li>
<li><p>Matrix objects over-ride multiplication to be
matrix-multiplication. <strong>Make sure you understand this for
functions that you may want to receive matrices. Especially in
light of the fact that asanyarray(m) returns a matrix when m is
a matrix.</strong></p></li>
<li><p>Matrix objects over-ride power to be matrix raised to a power. The
same warning about using power inside a function that uses
asanyarray(…) to get an array object holds for this fact.</p></li>
<li><p>The default __array_priority__ of matrix objects is 10.0, and
therefore mixed operations with ndarrays always produce matrices.</p></li>
<li><p>Matrices have special attributes which make calculations easier.
These are</p>
<div class="pst-scrollable-table-container"><table class="autosummary longtable table autosummary">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.matrix.T.html#numpy.matrix.T" title="numpy.matrix.T"><code class="xref py py-obj docutils literal notranslate"><span class="pre">matrix.T</span></code></a></p></td>
<td><p>Returns the transpose of the matrix.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.matrix.H.html#numpy.matrix.H" title="numpy.matrix.H"><code class="xref py py-obj docutils literal notranslate"><span class="pre">matrix.H</span></code></a></p></td>
<td><p>Returns the (complex) conjugate transpose of <em class="xref py py-obj">self</em>.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.matrix.I.html#numpy.matrix.I" title="numpy.matrix.I"><code class="xref py py-obj docutils literal notranslate"><span class="pre">matrix.I</span></code></a></p></td>
<td><p>Returns the (multiplicative) inverse of invertible <em class="xref py py-obj">self</em>.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.matrix.A.html#numpy.matrix.A" title="numpy.matrix.A"><code class="xref py py-obj docutils literal notranslate"><span class="pre">matrix.A</span></code></a></p></td>
<td><p>Return <em class="xref py py-obj">self</em> as an <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ndarray</span></code></a> object.</p></td>
</tr>
</tbody>
</table>
</div>
</li>
</ol>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Matrix objects over-ride multiplication, ‘*’, and power, ‘**’, to
be matrix-multiplication and matrix power, respectively. If your
subroutine can accept sub-classes and you do not convert to base-
class arrays, then you must use the ufuncs multiply and power to
be sure that you are performing the correct operation for all
inputs.</p>
</div>
<p>The matrix class is a Python subclass of the ndarray and can be used
as a reference for how to construct your own subclass of the ndarray.
Matrices can be created from other matrices, strings, and anything
else that can be converted to an <code class="docutils literal notranslate"><span class="pre">ndarray</span></code> . The name “mat “is an
alias for “matrix “in NumPy.</p>
<div class="pst-scrollable-table-container"><table class="autosummary longtable table autosummary">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.matrix.html#numpy.matrix" title="numpy.matrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">matrix</span></code></a>(data[, dtype, copy])</p></td>
<td><p>Returns a matrix from an array-like object, or from a string of data.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.asmatrix.html#numpy.asmatrix" title="numpy.asmatrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">asmatrix</span></code></a>(data[, dtype])</p></td>
<td><p>Interpret the input as a matrix.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.bmat.html#numpy.bmat" title="numpy.bmat"><code class="xref py py-obj docutils literal notranslate"><span class="pre">bmat</span></code></a>(obj[, ldict, gdict])</p></td>
<td><p>Build a matrix object from a string, nested sequence, or array.</p></td>
</tr>
</tbody>
</table>
</div>
<p>Example 1: Matrix creation from a string</p>
<div class="try_examples_outer_container docutils container" id="4d8ddfa0-d645-4b93-a9bd-a8eb8eacecc2">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('4d8ddfa0-d645-4b93-a9bd-a8eb8eacecc2','34e684d4-dccd-4e61-a237-d3fd8d16f7e3','ea0d7115-4233-4fa0-b94f-38bf3fcbf9fe','../lite/tree/../notebooks/index.html?path=09e5c1dc_022b_42f3_8e31_a4832e814935.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>
<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">asmatrix</span><span class="p">(</span><span class="s1">'1 2 3; 4 5 3'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">((</span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="p">)</span><span class="o">.</span><span class="n">I</span><span class="p">)</span>
<span class="go"> [[ 0.29239766 -0.13450292]</span>
<span class="go"> [-0.13450292 0.08187135]]</span>
</pre></div>
</div>
</div>
</div>
<div id="ea0d7115-4233-4fa0-b94f-38bf3fcbf9fe" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('4d8ddfa0-d645-4b93-a9bd-a8eb8eacecc2','ea0d7115-4233-4fa0-b94f-38bf3fcbf9fe')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('4d8ddfa0-d645-4b93-a9bd-a8eb8eacecc2','ea0d7115-4233-4fa0-b94f-38bf3fcbf9fe')">Open In Tab</button></div><div id="34e684d4-dccd-4e61-a237-d3fd8d16f7e3" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script><p>Example 2: Matrix creation from a nested sequence</p>
<div class="try_examples_outer_container docutils container" id="f0640dff-b978-4d87-8412-c7bab11ab51a">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('f0640dff-b978-4d87-8412-c7bab11ab51a','e9ded606-0431-493c-bd96-5cce71de792d','2c46d6fe-eb71-47c5-8702-6ccb0e93d94b','../lite/tree/../notebooks/index.html?path=9cd924be_31e0_457e_9c7c_e571d6555ba0.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>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">asmatrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">10</span><span class="p">],[</span><span class="mf">1.0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="n">j</span><span class="p">]])</span>
<span class="go">matrix([[ 1.+0.j, 5.+0.j, 10.+0.j],</span>
<span class="go"> [ 1.+0.j, 3.+0.j, 0.+4.j]])</span>
</pre></div>
</div>
</div>
</div>
<div id="2c46d6fe-eb71-47c5-8702-6ccb0e93d94b" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('f0640dff-b978-4d87-8412-c7bab11ab51a','2c46d6fe-eb71-47c5-8702-6ccb0e93d94b')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('f0640dff-b978-4d87-8412-c7bab11ab51a','2c46d6fe-eb71-47c5-8702-6ccb0e93d94b')">Open In Tab</button></div><div id="e9ded606-0431-493c-bd96-5cce71de792d" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script><p>Example 3: Matrix creation from an array</p>
<div class="try_examples_outer_container docutils container" id="414cb285-a462-47f2-b290-06fe4fd24791">
<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('414cb285-a462-47f2-b290-06fe4fd24791','8616f712-8e2f-485a-9279-723accdb2c6a','b9202858-2f78-4d98-828e-e5d6d6f25c90','../lite/tree/../notebooks/index.html?path=0c98a449_8c53_4dad_a878_25cd8ef39a8e.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>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">asmatrix</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span><span class="o">.</span><span class="n">T</span>
<span class="go">matrix([[4.17022005e-01, 3.02332573e-01, 1.86260211e-01],</span>
<span class="go"> [7.20324493e-01, 1.46755891e-01, 3.45560727e-01],</span>
<span class="go"> [1.14374817e-04, 9.23385948e-02, 3.96767474e-01]])</span>
</pre></div>
</div>
</div>
</div>
<div id="b9202858-2f78-4d98-828e-e5d6d6f25c90" class="try_examples_outer_iframe hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('414cb285-a462-47f2-b290-06fe4fd24791','b9202858-2f78-4d98-828e-e5d6d6f25c90')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('414cb285-a462-47f2-b290-06fe4fd24791','b9202858-2f78-4d98-828e-e5d6d6f25c90')">Open In Tab</button></div><div id="8616f712-8e2f-485a-9279-723accdb2c6a" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script></section>
<section id="memory-mapped-file-arrays">
<h2>Memory-mapped file arrays<a class="headerlink" href="#memory-mapped-file-arrays" title="Link to this heading">#</a></h2>
<p id="index-1">Memory-mapped files are useful for reading and/or modifying small