-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnep-0043-extensible-ufuncs.html
1758 lines (1542 loc) · 137 KB
/
nep-0043-extensible-ufuncs.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="./" >
<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>NEP 43 — Enhancing the extensibility of UFuncs — NumPy Enhancement Proposals</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</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=03e43079" />
<!-- 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=7f41d439"></script>
<script src="_static/doctools.js?v=888ff710"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'nep-0043-extensible-ufuncs';</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="NEP 53 — Evolving the NumPy C-API for NumPy 2.0" href="nep-0053-c-abi-evolution.html" />
<link rel="prev" title="Open NEPs (under consideration)" href="open.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="" />
<meta name="docbuild:last-update" content="Apr 17, 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="">
<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="content.html">
<img src="_static/numpylogo.svg" class="logo__image only-light" alt="NumPy Enhancement Proposals - Home"/>
<img src="_static/numpylogo_dark.svg" class="logo__image only-dark pst-js-only" alt="NumPy Enhancement Proposals - 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 current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item navbar-persistent--container">
<button class="btn search-button-field 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"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</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"><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="navbar-persistent--mobile">
<button class="btn search-button-field 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"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</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 current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<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"><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="scope.html">The Scope of NumPy</a></li>
<li class="toctree-l1"><a class="reference internal" href="roadmap.html">Current roadmap</a></li>
</ul>
<ul class="current nav bd-sidenav">
<li class="toctree-l1 has-children"><a class="reference internal" href="meta.html">Meta-NEPs (NEPs about NEPs or active Processes)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0000.html">NEP 0 — Purpose and process</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0023-backwards-compatibility.html">NEP 23 — Backwards compatibility and deprecation policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0036-fair-play.html">NEP 36 — Fair play</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0045-c_style_guide.html">NEP 45 — C style guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0046-sponsorship-guidelines.html">NEP 46 — NumPy sponsorship guidelines</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0048-spending-project-funds.html">NEP 48 — Spending NumPy project funds</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-template.html">NEP X — Template and instructions</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="provisional.html">Provisional NEPs (provisionally accepted; interface may change)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="simple">
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="accepted.html">Accepted NEPs (implementation in progress)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0041-improved-dtype-support.html">NEP 41 — First step towards a new datatype system</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0042-new-dtypes.html">NEP 42 — New and extensible DTypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0044-restructuring-numpy-docs.html">NEP 44 — Restructuring the NumPy documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0051-scalar-representation.html">NEP 51 — Changing the representation of NumPy scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0054-simd-cpp-highway.html">NEP 54 — SIMD infrastructure evolution: adopting Google Highway when moving to C++</a></li>
</ul>
</details></li>
<li class="toctree-l1 current active has-children"><a class="reference internal" href="open.html">Open NEPs (under consideration)</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 current active"><a class="current reference internal" href="#">NEP 43 — Enhancing the extensibility of UFuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0053-c-abi-evolution.html">NEP 53 — Evolving the NumPy C-API for NumPy 2.0</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="finished.html">Finished NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0001-npy-format.html">NEP 1 — A simple file format for NumPy arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0005-generalized-ufuncs.html">NEP 5 — Generalized universal functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0007-datetime-proposal.html">NEP 7 — A proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0010-new-iterator-ufunc.html">NEP 10 — Optimizing iterator/UFunc performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0013-ufunc-overrides.html">NEP 13 — A mechanism for overriding Ufuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0014-dropping-python2.7-proposal.html">NEP 14 — Plan for dropping Python 2.7 support</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0015-merge-multiarray-umath.html">NEP 15 — Merging multiarray and umath</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0018-array-function-protocol.html">NEP 18 — A dispatch mechanism for NumPy's high level array functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0019-rng-policy.html">NEP 19 — Random number generator policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0020-gufunc-signature-enhancement.html">NEP 20 — Expansion of generalized universal function signatures</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0022-ndarray-duck-typing-overview.html">NEP 22 — Duck typing for NumPy arrays – high level overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0027-zero-rank-arrarys.html">NEP 27 — Zero rank arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0028-website-redesign.html">NEP 28 — numpy.org website redesign</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0029-deprecation_policy.html">NEP 29 — Recommend Python and NumPy version support as a community policy standard</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0032-remove-financial-functions.html">NEP 32 — Remove the financial functions from NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0034-infer-dtype-is-object.html">NEP 34 — Disallow inferring ``dtype=object`` from sequences</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0035-array-creation-dispatch-with-array-function.html">NEP 35 — Array creation dispatching with __array_function__</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0038-SIMD-optimizations.html">NEP 38 — Using SIMD optimization instructions for performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0040-legacy-datatype-impl.html">NEP 40 — Legacy datatype implementation in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0049.html">NEP 49 — Data allocation strategies</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0050-scalar-promotion.html">NEP 50 — Promotion rules for Python scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0052-python-api-cleanup.html">NEP 52 — Python API cleanup for NumPy 2.0</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0055-string_dtype.html">NEP 55 — Add a UTF-8 variable-width string DType to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0056-array-api-main-namespace.html">NEP 56 — Array API standard support in NumPy's main namespace</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="deferred.html">Deferred and Superseded NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0002-warnfix.html">NEP 2 — A proposal to build numpy without warning with a big set of warning flags</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0003-math_config_clean.html">NEP 3 — Cleaning the math configuration of numpy.core</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0004-datetime-proposal3.html">NEP 4 — A (third) proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0006-newbugtracker.html">NEP 6 — Replacing Trac with a different bug tracker</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0008-groupby_additions.html">NEP 8 — A proposal for adding groupby functionality to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0009-structured_array_extensions.html">NEP 9 — Structured array extensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0011-deferred-ufunc-evaluation.html">NEP 11 — Deferred UFunc evaluation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0012-missing-data.html">NEP 12 — Missing data functionality in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0021-advanced-indexing.html">NEP 21 — Simplified and explicit advanced indexing</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0024-missing-data-2.html">NEP 24 — Missing data functionality - alternative 1 to NEP 12</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0025-missing-data-3.html">NEP 25 — NA support via special dtypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0026-missing-data-summary.html">NEP 26 — Summary of missing data NEPs and discussion</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0030-duck-array-protocol.html">NEP 30 — Duck typing for NumPy arrays - implementation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0031-uarray.html">NEP 31 — Context-local and global overrides of the NumPy API</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0037-array-module.html">NEP 37 — A dispatch protocol for NumPy-like modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0047-array-api-standard.html">NEP 47 — Adopting the array API standard</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="rejected.html">Rejected and Withdrawn NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0016-abstract-array.html">NEP 16 — An abstract base class for identifying "duck arrays"</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0017-split-out-maskedarray.html">NEP 17 — Split out masked arrays</a></li>
</ul>
</details></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="content.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">Roadmap & NumPy enhancement proposals</a></li>
<li class="breadcrumb-item"><a href="open.html" class="nav-link">Open NEPs (under consideration)</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">NEP 43 — Enhancing the extensibility of UFuncs</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="nep-43-enhancing-the-extensibility-of-ufuncs">
<span id="nep43"></span><h1>NEP 43 — Enhancing the extensibility of UFuncs<a class="headerlink" href="#nep-43-enhancing-the-extensibility-of-ufuncs" title="Link to this heading">#</a></h1>
<dl class="field-list simple">
<dt class="field-odd">title<span class="colon">:</span></dt>
<dd class="field-odd"><p>Enhancing the Extensibility of UFuncs</p>
</dd>
<dt class="field-even">Author<span class="colon">:</span></dt>
<dd class="field-even"><p>Sebastian Berg</p>
</dd>
<dt class="field-odd">Status<span class="colon">:</span></dt>
<dd class="field-odd"><p>Draft</p>
</dd>
<dt class="field-even">Type<span class="colon">:</span></dt>
<dd class="field-even"><p>Standard</p>
</dd>
<dt class="field-odd">Created<span class="colon">:</span></dt>
<dd class="field-odd"><p>2020-06-20</p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This NEP is fourth in a series:</p>
<ul class="simple">
<li><p><a class="reference internal" href="nep-0040-legacy-datatype-impl.html#nep40"><span class="std std-ref">NEP 40</span></a> explains the shortcomings of NumPy’s dtype implementation.</p></li>
<li><p><a class="reference internal" href="nep-0041-improved-dtype-support.html#nep41"><span class="std std-ref">NEP 41</span></a> gives an overview of our proposed replacement.</p></li>
<li><p><a class="reference internal" href="nep-0042-new-dtypes.html#nep42"><span class="std std-ref">NEP 42</span></a> describes the new design’s datatype-related APIs.</p></li>
<li><p>NEP 43 (this document) describes the new design’s API for universal functions.</p></li>
</ul>
</div>
<section id="abstract">
<h2>Abstract<a class="headerlink" href="#abstract" title="Link to this heading">#</a></h2>
<p>The previous NEP 42 proposes the creation of new DTypes which can
be defined by users outside of NumPy itself.
The implementation of NEP 42 will enable users to create arrays with a custom dtype
and stored values.
This NEP outlines how NumPy will operate on arrays with custom dtypes in the future.
The most important functions operating on NumPy arrays are the so called
“universal functions” (ufunc) which include all math functions, such as
<code class="docutils literal notranslate"><span class="pre">np.add</span></code>, <code class="docutils literal notranslate"><span class="pre">np.multiply</span></code>, and even <code class="docutils literal notranslate"><span class="pre">np.matmul</span></code>.
These ufuncs must operate efficiently on multiple arrays with
different datatypes.</p>
<p>This NEP proposes to expand the design of ufuncs.
It makes a new distinction between the ufunc which can operate
on many different dtypes such as floats or integers,
and a new <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> which defines the efficient operation for
specific dtypes.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Details of the private and external APIs may change to reflect user
comments and implementation constraints. The underlying principles and
choices should not change significantly.</p>
</div>
</section>
<section id="motivation-and-scope">
<h2>Motivation and scope<a class="headerlink" href="#motivation-and-scope" title="Link to this heading">#</a></h2>
<p>The goal of this NEP is to extend universal
functions support the new DType system detailed in NEPs 41 and 42.
While the main motivation is enabling new user-defined DTypes, this will
also significantly simplify defining universal functions for NumPy strings or
structured DTypes.
Until now, these DTypes are not supported by any of NumPy’s functions
(such as <code class="docutils literal notranslate"><span class="pre">np.add</span></code> or <code class="docutils literal notranslate"><span class="pre">np.equal</span></code>), due to difficulties arising from
their parametric nature (compare NEP 41 and 42), e.g. the string length.</p>
<p>Functions on arrays must handle a number of distinct steps which are
described in more detail in section “<a class="reference internal" href="#steps-involved-in-a-ufunc-call">Steps involved in a UFunc call</a>”.
The most important ones are:</p>
<ul class="simple">
<li><p>Organizing all functionality required to define a ufunc call for specific
DTypes. This is often called the “inner-loop”.</p></li>
<li><p>Deal with input for which no exact matching implementation is found.
For example when <code class="docutils literal notranslate"><span class="pre">int32</span></code> and <code class="docutils literal notranslate"><span class="pre">float64</span></code> are added, the <code class="docutils literal notranslate"><span class="pre">int32</span></code>
is cast to <code class="docutils literal notranslate"><span class="pre">float64</span></code>. This requires a distinct “promotion” step.</p></li>
</ul>
<p>After organizing and defining these, we need to:</p>
<ul class="simple">
<li><p>Define the user API for customizing both of the above points.</p></li>
<li><p>Allow convenient reuse of existing functionality.
For example a DType representing physical units, such as meters,
should be able to fall back to NumPy’s existing math implementations.</p></li>
</ul>
<p>This NEP details how these requirements will be achieved in NumPy:</p>
<ul class="simple">
<li><p>All DTyper-specific functionality currently part of the ufunc
definition will be defined as part of a new <a class="reference internal" href="#arraymethod">ArrayMethod</a> object.
This <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> object will be the new, preferred, way to describe any
function operating on arrays.</p></li>
<li><p>Ufuncs will dispatch to the <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> and potentially use promotion
to find the correct <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> to use.
This will be described in the <a class="reference internal" href="#id1">Promotion and dispatching</a> section.</p></li>
</ul>
<p>A new C-API will be outlined in each section. A future Python API is
expected to be very similar and the C-API is presented in terms of Python
code for readability.</p>
<p>The NEP proposes a large, but necessary, refactor of the NumPy ufunc internals.
This modernization will not affect end users directly and is not only a necessary
step for new DTypes, but in itself a maintenance effort which is expected to
help with future improvements to the ufunc machinery.</p>
<p>While the most important restructure proposed is the new <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code>
object, the largest long-term consideration is the API choice for
promotion and dispatching.</p>
</section>
<section id="backwards-compatibility">
<h2>Backwards compatibility<a class="headerlink" href="#backwards-compatibility" title="Link to this heading">#</a></h2>
<p>The general backwards compatibility issues have also been listed
previously in NEP 41.</p>
<p>The vast majority of users should not see any changes beyond those typical
for NumPy releases.
There are three main users or use-cases impacted by the proposed changes:</p>
<ol class="arabic simple">
<li><p>The Numba package uses direct access to the NumPy C-loops and modifies
the NumPy ufunc struct directly for its own purposes.</p></li>
<li><p>Astropy uses its own “type resolver”, meaning that a default switch over
from the existing type resolution to a new default Promoter requires care.</p></li>
<li><p>It is currently possible to register loops for dtype <em>instances</em>.
This is theoretically useful for structured dtypes and is a resolution
step happening <em>after</em> the DType resolution step proposed here.</p></li>
</ol>
<p>This NEP will try hard to maintain backward compatibility as much as
possible. However, both of these projects have signaled willingness to adapt
to breaking changes.</p>
<p>The main reason why NumPy will be able to provide backward compatibility
is that:</p>
<ul class="simple">
<li><p>Existing inner-loops can be wrapped, adding an indirection to the call but
maintaining full backwards compatibility.
The <code class="docutils literal notranslate"><span class="pre">get_loop</span></code> function can, in this case, search the existing
inner-loop functions (which are stored on the ufunc directly) in order
to maintain full compatibility even with potential direct structure access.</p></li>
<li><p>Legacy type resolvers can be called as a fallback (potentially caching
the result). The resolver may need to be called twice (once for the DType
resolution and once for the <code class="docutils literal notranslate"><span class="pre">resolve_descriptor</span></code> implementation).</p></li>
<li><p>The fallback to the legacy type resolver should in most cases handle loops
defined for such structured dtype instances. This is because if there is no
other <code class="docutils literal notranslate"><span class="pre">np.Void</span></code> implementation, the legacy fallback will retain the old
behaviour at least initially.</p></li>
</ul>
<p>The masked type resolvers specifically will <em>not</em> remain supported, but
has no known users (including NumPy itself, which only uses the default
version).</p>
<p>Further, no compatibility attempt will be made for <em>calling</em> as opposed
to providing either the normal or the masked type resolver. As NumPy
will use it only as a fallback. There are no known users of this
(undocumented) possibility.</p>
<p>While the above changes potentially break some workflows,
we believe that the long-term improvements vastly outweigh this.
Further, packages such as astropy and Numba are capable of adapting so that
end-users may need to update their libraries but not their code.</p>
</section>
<section id="usage-and-impact">
<h2>Usage and impact<a class="headerlink" href="#usage-and-impact" title="Link to this heading">#</a></h2>
<p>This NEP restructures how operations on NumPy arrays are defined both
within NumPy and for external implementers.
The NEP mainly concerns those who either extend ufuncs for custom DTypes
or create custom ufuncs. It does not aim to finalize all
potential use-cases, but rather restructure NumPy to be extensible and allow
addressing new issues or feature requests as they arise.</p>
<section id="overview-and-end-user-api">
<h3>Overview and end user API<a class="headerlink" href="#overview-and-end-user-api" title="Link to this heading">#</a></h3>
<p>To give an overview of how this NEP proposes to structure ufuncs,
the following describes the potential exposure of the proposed restructure
to the end user.</p>
<p>Universal functions are much like a Python method defined on the DType of
the array when considering a ufunc with only a single input:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">res</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">positive</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
</pre></div>
</div>
<p>could be implemented (conceptually) as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">positive_impl</span> <span class="o">=</span> <span class="n">arr</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">positive</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">positive_impl</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
</pre></div>
</div>
<p>However, unlike methods, <code class="docutils literal notranslate"><span class="pre">positive_impl</span></code> is not stored on the dtype itself.
It is rather the implementation of <code class="docutils literal notranslate"><span class="pre">np.positive</span></code> for a specific DType.
Current NumPy partially exposes this “choice of implementation” using
the <code class="docutils literal notranslate"><span class="pre">dtype</span></code> (or more exact <code class="docutils literal notranslate"><span class="pre">signature</span></code>) attribute in universal functions,
although these are rarely used:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">positive</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
</pre></div>
</div>
<p>forces NumPy to use the <code class="docutils literal notranslate"><span class="pre">positive_impl</span></code> written specifically for the Float64
DType.</p>
<p>This NEP makes the distinction more explicit, by creating a new object to
represent <code class="docutils literal notranslate"><span class="pre">positive_impl</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">positive_impl</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">positive</span><span class="o">.</span><span class="n">resolve_impl</span><span class="p">((</span><span class="nb">type</span><span class="p">(</span><span class="n">arr</span><span class="o">.</span><span class="n">dtype</span><span class="p">),</span> <span class="kc">None</span><span class="p">))</span>
<span class="c1"># The `None` represents the output DType which is automatically chosen.</span>
</pre></div>
</div>
<p>While the creation of a <code class="docutils literal notranslate"><span class="pre">positive_impl</span></code> object and the <code class="docutils literal notranslate"><span class="pre">resolve_impl</span></code>
method is part of this NEP, the following code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">res</span> <span class="o">=</span> <span class="n">positive_impl</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
</pre></div>
</div>
<p>may not be implemented initially and is not central to the redesign.</p>
<p>In general NumPy universal functions can take many inputs.
This requires looking up the implementation by considering all of them
and makes ufuncs “multi-methods” with respect to the input DTypes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">add_impl</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="o">.</span><span class="n">resolve_impl</span><span class="p">((</span><span class="nb">type</span><span class="p">(</span><span class="n">arr1</span><span class="o">.</span><span class="n">dtype</span><span class="p">),</span> <span class="nb">type</span><span class="p">(</span><span class="n">arr2</span><span class="o">.</span><span class="n">dtype</span><span class="p">),</span> <span class="kc">None</span><span class="p">))</span>
</pre></div>
</div>
<p>This NEP defines how <code class="docutils literal notranslate"><span class="pre">positive_impl</span></code> and <code class="docutils literal notranslate"><span class="pre">add_impl</span></code> will be represented
as a new <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> which can be implemented outside of NumPy.
Further, it defines how <code class="docutils literal notranslate"><span class="pre">resolve_impl</span></code> will implement and solve dispatching
and promotion.</p>
<p>The reasons for this split may be more clear after reviewing the
<a class="reference internal" href="#steps-involved-in-a-ufunc-call">Steps involved in a UFunc call</a> section.</p>
</section>
<section id="defining-a-new-ufunc-implementation">
<h3>Defining a new ufunc implementation<a class="headerlink" href="#defining-a-new-ufunc-implementation" title="Link to this heading">#</a></h3>
<p>The following is a mock-up of how a new implementation, in this case
to define string equality, will be added to a ufunc.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">StringEquality</span><span class="p">(</span><span class="n">BoundArrayMethod</span><span class="p">):</span>
<span class="n">nin</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">nout</span> <span class="o">=</span> <span class="mi">1</span>
<span class="c1"># DTypes are stored on the BoundArrayMethod and not on the internal</span>
<span class="c1"># ArrayMethod, to reference cyles.</span>
<span class="n">DTypes</span> <span class="o">=</span> <span class="p">(</span><span class="n">String</span><span class="p">,</span> <span class="n">String</span><span class="p">,</span> <span class="n">Bool</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">resolve_descriptors</span><span class="p">(</span><span class="bp">self</span><span class="p">:</span> <span class="n">ArrayMethod</span><span class="p">,</span> <span class="n">DTypes</span><span class="p">,</span> <span class="n">given_descrs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""The strided loop supports all input string dtype instances</span>
<span class="sd"> and always returns a boolean. (String is always native byte order.)</span>
<span class="sd"> Defining this function is not necessary, since NumPy can provide</span>
<span class="sd"> it by default.</span>
<span class="sd"> The `self` argument here refers to the unbound array method, so</span>
<span class="sd"> that DTypes are passed in explicitly.</span>
<span class="sd"> """</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">given_descrs</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">DTypes</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">given_descrs</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">DTypes</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="k">assert</span> <span class="n">given_descrs</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">given_descrs</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">DTypes</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="n">out_descr</span> <span class="o">=</span> <span class="n">given_descrs</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="c1"># preserve input (e.g. metadata)</span>
<span class="k">if</span> <span class="n">given_descrs</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">out_descr</span> <span class="o">=</span> <span class="n">DTypes</span><span class="p">[</span><span class="mi">2</span><span class="p">]()</span>
<span class="c1"># The operation is always "no" casting (most ufuncs are)</span>
<span class="k">return</span> <span class="p">(</span><span class="n">given_descrs</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">given_descrs</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">out_descr</span><span class="p">),</span> <span class="s2">"no"</span>
<span class="k">def</span><span class="w"> </span><span class="nf">strided_loop</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">dimensions</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">strides</span><span class="p">,</span> <span class="n">innerloop_data</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""The 1-D strided loop, similar to those used in current ufuncs"""</span>
<span class="c1"># dimensions: Number of loop items and core dimensions</span>
<span class="c1"># data: Pointers to the array data.</span>
<span class="c1"># strides: strides to iterate all elements</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">dimensions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c1"># number of items to loop over</span>
<span class="n">num_chars1</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">descriptors</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">itemsize</span>
<span class="n">num_chars2</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">descriptors</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">itemsize</span>
<span class="c1"># C code using the above information to compare the strings in</span>
<span class="c1"># both arrays. In particular, this loop requires the `num_chars1`</span>
<span class="c1"># and `num_chars2`. Information which is currently not easily</span>
<span class="c1"># available.</span>
<span class="n">np</span><span class="o">.</span><span class="n">equal</span><span class="o">.</span><span class="n">register_impl</span><span class="p">(</span><span class="n">StringEquality</span><span class="p">)</span>
<span class="k">del</span> <span class="n">StringEquality</span> <span class="c1"># may be deleted.</span>
</pre></div>
</div>
<p>This definition will be sufficient to create a new loop, and the
structure allows for expansion in the future; something that is already
required to implement casting within NumPy itself.
We use <code class="docutils literal notranslate"><span class="pre">BoundArrayMethod</span></code> and a <code class="docutils literal notranslate"><span class="pre">context</span></code> structure here. These
are described and motivated in details later. Briefly:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">context</span></code> is a generalization of the <code class="docutils literal notranslate"><span class="pre">self</span></code> that Python passes to its
methods.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">BoundArrayMethod</span></code> is equivalent to the Python distinction that
<code class="docutils literal notranslate"><span class="pre">class.method</span></code> is a method, while <code class="docutils literal notranslate"><span class="pre">class().method</span></code> returns a “bound” method.</p></li>
</ul>
</section>
<section id="customizing-dispatching-and-promotion">
<h3>Customizing dispatching and Promotion<a class="headerlink" href="#customizing-dispatching-and-promotion" title="Link to this heading">#</a></h3>
<p>Finding the correct implementation when <code class="docutils literal notranslate"><span class="pre">np.positive.resolve_impl()</span></code> is
called is largely an implementation detail.
But, in some cases it may be necessary to influence this process when no
implementation matches the requested DTypes exactly:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">multiple</span><span class="o">.</span><span class="n">resolve_impl</span><span class="p">((</span><span class="n">Timedelta64</span><span class="p">,</span> <span class="n">Int8</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>
</pre></div>
</div>
<p>will not have an exact match, because NumPy only has an implementation for
multiplying <code class="docutils literal notranslate"><span class="pre">Timedelta64</span></code> with <code class="docutils literal notranslate"><span class="pre">Int64</span></code>.
In simple cases, NumPy will use a default promotion step to attempt to find
the correct implementation, but to implement the above step, we will allow
the following:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">promote_timedelta_integer</span><span class="p">(</span><span class="n">ufunc</span><span class="p">,</span> <span class="n">dtypes</span><span class="p">):</span>
<span class="n">new_dtypes</span> <span class="o">=</span> <span class="p">(</span><span class="n">Timdelta64</span><span class="p">,</span> <span class="n">Int64</span><span class="p">,</span> <span class="n">dtypes</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># Resolve again, using Int64:</span>
<span class="k">return</span> <span class="n">ufunc</span><span class="o">.</span><span class="n">resolve_impl</span><span class="p">(</span><span class="n">new_dtypes</span><span class="p">)</span>
<span class="n">np</span><span class="o">.</span><span class="n">multiple</span><span class="o">.</span><span class="n">register_promoter</span><span class="p">(</span>
<span class="p">(</span><span class="n">Timedelta64</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span> <span class="n">promote_timedelta_integer</span><span class="p">)</span>
</pre></div>
</div>
<p>Where <code class="docutils literal notranslate"><span class="pre">Integer</span></code> is an abstract DType (compare NEP 42).</p>
</section>
</section>
<section id="steps-involved-in-a-ufunc-call">
<span id="steps-of-a-ufunc-call"></span><h2>Steps involved in a UFunc call<a class="headerlink" href="#steps-involved-in-a-ufunc-call" title="Link to this heading">#</a></h2>
<p>Before going into more detailed API choices, it is helpful to review the
steps involved in a call to a universal function in NumPy.</p>
<p>A UFunc call is split into the following steps:</p>
<ol class="arabic simple">
<li><p>Handle <code class="docutils literal notranslate"><span class="pre">__array_ufunc__</span></code> protocol:</p>
<ul class="simple">
<li><p>For array-likes such as a Dask arrays, NumPy can defer the operation.
This step is performed first, and unaffected by this NEP (compare <a class="reference internal" href="nep-0018-array-function-protocol.html#nep18"><span class="std std-ref">NEP 18 — A dispatch mechanism for NumPy’s high level array functions</span></a>).</p></li>
</ul>
</li>
<li><p>Promotion and dispatching</p>
<ul class="simple">
<li><p>Given the DTypes of all inputs, find the correct implementation.
E.g. an implementation for <code class="docutils literal notranslate"><span class="pre">float64</span></code>, <code class="docutils literal notranslate"><span class="pre">int64</span></code> or a user-defined DType.</p></li>
<li><p>When no exact implementation exists, <em>promotion</em> has to be performed.
For example, adding a <code class="docutils literal notranslate"><span class="pre">float32</span></code> and a <code class="docutils literal notranslate"><span class="pre">float64</span></code> is implemented by
first casting the <code class="docutils literal notranslate"><span class="pre">float32</span></code> to <code class="docutils literal notranslate"><span class="pre">float64</span></code>.</p></li>
</ul>
</li>
<li><p>Parametric <code class="docutils literal notranslate"><span class="pre">dtype</span></code> resolution:</p>
<ul class="simple">
<li><p>In general, whenever an output DType is parametric the parameters have
to be found (resolved).</p></li>
<li><p>For example, if a loop adds two strings, it is necessary to define the
correct output (and possibly input) dtypes. <code class="docutils literal notranslate"><span class="pre">S5</span> <span class="pre">+</span> <span class="pre">S4</span> <span class="pre">-></span> <span class="pre">S9</span></code>, while
an <code class="docutils literal notranslate"><span class="pre">upper</span></code> function has the signature <code class="docutils literal notranslate"><span class="pre">S5</span> <span class="pre">-></span> <span class="pre">S5</span></code>.</p></li>
<li><p>When they are not parametric, a default implementation is provided
which fills in the default dtype instances (ensuring for example native
byte order).</p></li>
</ul>
</li>
<li><p>Preparing the iteration:</p>
<ul class="simple">
<li><p>This step is largely handled by <code class="docutils literal notranslate"><span class="pre">NpyIter</span></code> internally (the iterator).</p></li>
<li><p>Allocate all outputs and temporary buffers necessary to perform casts.
This <em>requires</em> the dtypes as resolved in step 3.</p></li>
<li><p>Find the best iteration order, which includes information to efficiently
implement broadcasting. For example, adding a single value to an array
repeats the same value.</p></li>
</ul>
</li>
<li><p>Setup and fetch the C-level function:</p>
<ul class="simple">
<li><p>If necessary, allocate temporary working space.</p></li>
<li><p>Find the C-implemented, light weight, inner-loop function.
Finding the inner-loop function can allow specialized implementations
in the future.
For example casting currently optimizes contiguous casts and
reductions have optimizations that are currently handled
inside the inner-loop function itself.</p></li>
<li><p>Signal whether the inner-loop requires the Python API or whether
the GIL may be released (to allow threading).</p></li>
<li><p>Clear floating point exception flags.</p></li>
</ul>
</li>
<li><p>Perform the actual calculation:</p>
<ul class="simple">
<li><p>Run the DType specific inner-loop function.</p></li>
<li><p>The inner-loop may require access to additional data, such as dtypes or
additional data set in the previous step.</p></li>
<li><p>The inner-loop function may be called an undefined number of times.</p></li>
</ul>
</li>
<li><p>Finalize:</p>
<ul class="simple">
<li><p>Free any temporary working space allocated in step 5.</p></li>
<li><p>Check for floating point exception flags.</p></li>
<li><p>Return the result.</p></li>
</ul>
</li>
</ol>
<p>The <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> provides a concept to group steps 3 to 6 and partially 7.
However, implementers of a new ufunc or <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> usually do not need to
customize the behaviour in steps 4 or 6 which NumPy can and does provide.
For the <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> implementer, the central steps to customize
are step 3 and step 5. These provide the custom inner-loop function and
potentially inner-loop specific setup.
Further customization is possible and anticipated as future extensions.</p>
<p>Step 2. is promotion and dispatching and will be restructured
with new API to allow customization of the process where necessary.</p>
<p>Step 1 is listed for completeness and is unaffected by this NEP.</p>
<p>The following sketch provides an overview of step 2 to 6 with an emphasize
of how dtypes are handled and which parts are customizable (“Registered”)
and which are handled by NumPy:</p>
<figure class="align-center align-default">
<img alt="_images/nep43-sketch.svg" src="_images/nep43-sketch.svg" /></figure>
</section>
<section id="arraymethod">
<h2>ArrayMethod<a class="headerlink" href="#arraymethod" title="Link to this heading">#</a></h2>
<p>A central proposal of this NEP is the creation of the <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> as an object
describing each implementation specific to a given set of DTypes.
We use the <code class="docutils literal notranslate"><span class="pre">class</span></code> syntax to describe the information required to create
a new <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> object:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">ArrayMethod</span><span class="p">:</span>
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span> <span class="c1"># Name, mainly useful for debugging</span>
<span class="c1"># Casting safety information (almost always "safe", necessary to</span>
<span class="c1"># unify casting and universal functions)</span>
<span class="n">casting</span><span class="p">:</span> <span class="n">Casting</span> <span class="o">=</span> <span class="s2">"no"</span>
<span class="c1"># More general flags:</span>
<span class="n">flags</span><span class="p">:</span> <span class="nb">int</span>
<span class="k">def</span><span class="w"> </span><span class="nf">resolve_descriptors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">Tuple</span><span class="p">[</span><span class="n">DTypeMeta</span><span class="p">],</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">DType</span><span class="o">|</span><span class="kc">None</span><span class="p">]:</span> <span class="n">given_descrs</span><span class="p">)</span> <span class="o">-></span> <span class="n">Casting</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">DType</span><span class="p">]:</span>
<span class="w"> </span><span class="sd">"""Returns the safety of the operation (casting safety) and the</span>
<span class="sd"> """</span>
<span class="c1"># A default implementation can be provided for non-parametric</span>
<span class="c1"># output dtypes.</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span><span class="w"> </span><span class="nf">get_loop</span><span class="p">(</span><span class="n">Context</span> <span class="p">:</span> <span class="n">context</span><span class="p">,</span> <span class="n">strides</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span> <span class="o">-></span> <span class="n">strided_loop_function</span><span class="p">,</span> <span class="n">flags</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Returns the low-level C (strided inner-loop) function which</span>
<span class="sd"> performs the actual operation.</span>
<span class="sd"> This method may initially private, users will be able to provide</span>
<span class="sd"> a set of optimized inner-loop functions instead:</span>
<span class="sd"> * `strided_inner_loop`</span>
<span class="sd"> * `contiguous_inner_loop`</span>
<span class="sd"> * `unaligned_strided_loop`</span>
<span class="sd"> * ...</span>
<span class="sd"> """</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span><span class="w"> </span><span class="nf">strided_inner_loop</span><span class="p">(</span>
<span class="n">Context</span> <span class="p">:</span> <span class="n">context</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">dimensions</span><span class="p">,</span> <span class="n">strides</span><span class="p">,</span> <span class="n">innerloop_data</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""The inner-loop (equivalent to the current ufunc loop)</span>
<span class="sd"> which is returned by the default `get_loop()` implementation."""</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
</pre></div>
</div>
<p>With <code class="docutils literal notranslate"><span class="pre">Context</span></code> providing mostly static information about the function call:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">Context</span><span class="p">:</span>
<span class="c1"># The ArrayMethod object itself:</span>
<span class="n">ArrayMethod</span> <span class="p">:</span> <span class="n">method</span>
<span class="c1"># Information about the caller, e.g. the ufunc, such as `np.add`:</span>
<span class="nb">callable</span> <span class="p">:</span> <span class="n">caller</span> <span class="o">=</span> <span class="kc">None</span>
<span class="c1"># The number of input arguments:</span>
<span class="nb">int</span> <span class="p">:</span> <span class="n">nin</span> <span class="o">=</span> <span class="mi">1</span>
<span class="c1"># The number of output arguments:</span>
<span class="nb">int</span> <span class="p">:</span> <span class="n">nout</span> <span class="o">=</span> <span class="mi">1</span>
<span class="c1"># The actual dtypes instances the inner-loop operates on:</span>
<span class="n">Tuple</span><span class="p">[</span><span class="n">DType</span><span class="p">]</span> <span class="p">:</span> <span class="n">descriptors</span>
<span class="c1"># Any additional information required. In the future, this will</span>
<span class="c1"># generalize or duplicate things currently stored on the ufunc:</span>
<span class="c1"># - The ufunc signature of generalized ufuncs</span>
<span class="c1"># - The identity used for reductions</span>
</pre></div>
</div>
<p>And <code class="docutils literal notranslate"><span class="pre">flags</span></code> stored properties, for whether:</p>
<ul class="simple">
<li><p>the <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> supports unaligned input and output arrays</p></li>
<li><p>the inner-loop function requires the Python API (GIL)</p></li>
<li><p>NumPy has to check the floating point error CPU flags.</p></li>
</ul>
<p><em>Note: More information is expected to be added as necessary.</em></p>
<section id="the-call-context">
<h3>The call <code class="docutils literal notranslate"><span class="pre">Context</span></code><a class="headerlink" href="#the-call-context" title="Link to this heading">#</a></h3>
<p>The “context” object is analogous to Python’s <code class="docutils literal notranslate"><span class="pre">self</span></code> that is
passed to all methods.
To understand why the “context” object is necessary and its
internal structure, it is helpful to remember
that a Python method can be written in the following way
(see also the <a class="reference external" href="https://docs.python.org/3.8/reference/datamodel.html#object.__get__">documentation of __get__</a>):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">BoundMethod</span><span class="p">:</span>
<span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">method</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="n">instance</span>
<span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">method</span>
<span class="k">def</span><span class="w"> </span><span class="fm">__call__</span><span class="p">(</span><span class="bp">self</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">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">function</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</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">class</span><span class="w"> </span><span class="nc">Method</span><span class="p">:</span>
<span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">function</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">function</span> <span class="o">=</span> <span class="n">function</span>
<span class="k">def</span><span class="w"> </span><span class="fm">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">owner</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">instance</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="c1"># unsupported here</span>
<span class="k">return</span> <span class="n">BoundMethod</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
</pre></div>
</div>
<p>With which the following <code class="docutils literal notranslate"><span class="pre">method1</span></code> and <code class="docutils literal notranslate"><span class="pre">method2</span></code> below, behave identically:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">function</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="k">class</span><span class="w"> </span><span class="nc">MyClass</span><span class="p">:</span>
<span class="k">def</span><span class="w"> </span><span class="nf">method1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="n">method2</span> <span class="o">=</span> <span class="n">Method</span><span class="p">(</span><span class="n">function</span><span class="p">)</span>
</pre></div>
</div>
<p>And both will print the same result:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">myinstance</span> <span class="o">=</span> <span class="n">MyClass</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">myinstance</span><span class="o">.</span><span class="n">method1</span><span class="p">()</span>
<span class="go"><__main__.MyClass object at 0x7eff65436d00></span>
<span class="gp">>>> </span><span class="n">myinstance</span><span class="o">.</span><span class="n">method2</span><span class="p">()</span>
<span class="go"><__main__.MyClass object at 0x7eff65436d00></span>
</pre></div>
</div>
<p>Here <code class="docutils literal notranslate"><span class="pre">self.instance</span></code> would be all information passed on by <code class="docutils literal notranslate"><span class="pre">Context</span></code>.
The <code class="docutils literal notranslate"><span class="pre">Context</span></code> is a generalization and has to pass additional information:</p>
<ul class="simple">
<li><p>Unlike a method which operates on a single class instance, the <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code>
operates on many input arrays and thus multiple dtypes.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">__call__</span></code> of the <code class="docutils literal notranslate"><span class="pre">BoundMethod</span></code> above contains only a single call
to a function. But an <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> has to call <code class="docutils literal notranslate"><span class="pre">resolve_descriptors</span></code>
and later pass on that information to the inner-loop function.</p></li>
<li><p>A Python function has no state except that defined by its outer scope.
Within C, <code class="docutils literal notranslate"><span class="pre">Context</span></code> is able to provide additional state if necessary.</p></li>
</ul>
<p>Just as Python requires the distinction of a method and a bound method,
NumPy will have a <code class="docutils literal notranslate"><span class="pre">BoundArrayMethod</span></code>.
This stores all of the constant information that is part of the <code class="docutils literal notranslate"><span class="pre">Context</span></code>,
such as:</p>
<ul class="simple">
<li><p>the <code class="docutils literal notranslate"><span class="pre">DTypes</span></code></p></li>
<li><p>the number of input and output arguments</p></li>
<li><p>the ufunc signature (specific to generalized ufuncs, compare <a class="reference internal" href="nep-0020-gufunc-signature-enhancement.html#nep20"><span class="std std-ref">NEP 20 — Expansion of generalized universal function signatures</span></a>).</p></li>
</ul>
<p>Fortunately, most users and even ufunc implementers will not have to worry
about these internal details; just like few Python users need to know
about the <code class="docutils literal notranslate"><span class="pre">__get__</span></code> dunder method.
The <code class="docutils literal notranslate"><span class="pre">Context</span></code> object or C-structure provides all necessary data to the
fast C-functions and NumPy API creates the new <code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> or
<code class="docutils literal notranslate"><span class="pre">BoundArrayMethod</span></code> as required.</p>
</section>
<section id="arraymethod-specifications">
<span id="arraymethod-specs"></span><h3>ArrayMethod specifications<a class="headerlink" href="#arraymethod-specifications" title="Link to this heading">#</a></h3>
<p>These specifications provide a minimal initial C-API, which shall be expanded
in the future, for example to allow specialized inner-loops.</p>
<p>Briefly, NumPy currently relies on strided inner-loops and this
will be the only allowed method of defining a ufunc initially.