-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathswig.interface-file.html
1592 lines (1390 loc) · 116 KB
/
swig.interface-file.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>numpy.i: a SWIG interface file for NumPy — 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=c1318490"></script>
<script src="../_static/doctools.js?v=888ff710"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../_static/copybutton.js?v=30646c52"></script>
<script src="../_static/jupyterlite_sphinx.js?v=96e329c5"></script>
<script src="../_static/design-tabs.js?v=f930bc37"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'reference/swig.interface-file';</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="Testing the numpy.i typemaps" href="swig.testing.html" />
<link rel="prev" title="NumPy and SWIG" href="swig.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 21, 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="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="arrays.html">Array objects</a></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="current 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 current active has-children"><a class="reference internal" href="swig.html">NumPy and SWIG</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="#">numpy.i: a SWIG interface file for NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="swig.testing.html">Testing the numpy.i typemaps</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="../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="swig.html" class="nav-link">NumPy and SWIG</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">numpy.i: a SWIG interface file for NumPy</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="numpy-i-a-swig-interface-file-for-numpy">
<h1>numpy.i: a SWIG interface file for NumPy<a class="headerlink" href="#numpy-i-a-swig-interface-file-for-numpy" title="Link to this heading">#</a></h1>
<section id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">#</a></h2>
<p>The Simple Wrapper and Interface Generator (or <a class="reference external" href="https://www.swig.org">SWIG</a>) is a powerful tool for generating wrapper
code for interfacing to a wide variety of scripting languages.
<a class="reference external" href="https://www.swig.org">SWIG</a> can parse header files, and using only the code prototypes,
create an interface to the target language. But <a class="reference external" href="https://www.swig.org">SWIG</a> is not
omnipotent. For example, it cannot know from the prototype:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">double</span> <span class="n">rms</span><span class="p">(</span><span class="n">double</span><span class="o">*</span> <span class="n">seq</span><span class="p">,</span> <span class="nb">int</span> <span class="n">n</span><span class="p">);</span>
</pre></div>
</div>
<p>what exactly <code class="docutils literal notranslate"><span class="pre">seq</span></code> is. Is it a single value to be altered in-place?
Is it an array, and if so what is its length? Is it input-only?
Output-only? Input-output? <a class="reference external" href="https://www.swig.org">SWIG</a> cannot determine these details,
and does not attempt to do so.</p>
<p>If we designed <code class="docutils literal notranslate"><span class="pre">rms</span></code>, we probably made it a routine that takes an
input-only array of length <code class="docutils literal notranslate"><span class="pre">n</span></code> of <code class="docutils literal notranslate"><span class="pre">double</span></code> values called <code class="docutils literal notranslate"><span class="pre">seq</span></code>
and returns the root mean square. The default behavior of <a class="reference external" href="https://www.swig.org">SWIG</a>,
however, will be to create a wrapper function that compiles, but is
nearly impossible to use from the scripting language in the way the C
routine was intended.</p>
<p>For Python, the preferred way of handling contiguous (or technically,
<em>strided</em>) blocks of homogeneous data is with NumPy, which provides full
object-oriented access to multidimensial arrays of data. Therefore, the most
logical Python interface for the <code class="docutils literal notranslate"><span class="pre">rms</span></code> function would be (including doc
string):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">rms</span><span class="p">(</span><span class="n">seq</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""</span>
<span class="sd"> rms: return the root mean square of a sequence</span>
<span class="sd"> rms(numpy.ndarray) -> double</span>
<span class="sd"> rms(list) -> double</span>
<span class="sd"> rms(tuple) -> double</span>
<span class="sd"> """</span>
</pre></div>
</div>
<p>where <code class="docutils literal notranslate"><span class="pre">seq</span></code> would be a NumPy array of <code class="docutils literal notranslate"><span class="pre">double</span></code> values, and its
length <code class="docutils literal notranslate"><span class="pre">n</span></code> would be extracted from <code class="docutils literal notranslate"><span class="pre">seq</span></code> internally before being
passed to the C routine. Even better, since NumPy supports
construction of arrays from arbitrary Python sequences, <code class="docutils literal notranslate"><span class="pre">seq</span></code>
itself could be a nearly arbitrary sequence (so long as each element
can be converted to a <code class="docutils literal notranslate"><span class="pre">double</span></code>) and the wrapper code would
internally convert it to a NumPy array before extracting its data
and length.</p>
<p><a class="reference external" href="https://www.swig.org">SWIG</a> allows these types of conversions to be defined via a
mechanism called <em>typemaps</em>. This document provides information on
how to use <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code>, a <a class="reference external" href="https://www.swig.org">SWIG</a> interface file that defines a series
of typemaps intended to make the type of array-related conversions
described above relatively simple to implement. For example, suppose
that the <code class="docutils literal notranslate"><span class="pre">rms</span></code> function prototype defined above was in a header file
named <code class="docutils literal notranslate"><span class="pre">rms.h</span></code>. To obtain the Python interface discussed above, your
<a class="reference external" href="https://www.swig.org">SWIG</a> interface file would need the following:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="p">{</span>
<span class="c1">#define SWIG_FILE_WITH_INIT</span>
<span class="c1">#include "rms.h"</span>
<span class="o">%</span><span class="p">}</span>
<span class="o">%</span><span class="n">include</span> <span class="s2">"numpy.i"</span>
<span class="o">%</span><span class="n">init</span> <span class="o">%</span><span class="p">{</span>
<span class="n">import_array</span><span class="p">();</span>
<span class="o">%</span><span class="p">}</span>
<span class="o">%</span><span class="n">apply</span> <span class="p">(</span><span class="n">double</span><span class="o">*</span> <span class="n">IN_ARRAY1</span><span class="p">,</span> <span class="nb">int</span> <span class="n">DIM1</span><span class="p">)</span> <span class="p">{(</span><span class="n">double</span><span class="o">*</span> <span class="n">seq</span><span class="p">,</span> <span class="nb">int</span> <span class="n">n</span><span class="p">)};</span>
<span class="o">%</span><span class="n">include</span> <span class="s2">"rms.h"</span>
</pre></div>
</div>
<p>Typemaps are keyed off a list of one or more function arguments,
either by type or by type and name. We will refer to such lists as
<em>signatures</em>. One of the many typemaps defined by <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> is used
above and has the signature <code class="docutils literal notranslate"><span class="pre">(double*</span> <span class="pre">IN_ARRAY1,</span> <span class="pre">int</span> <span class="pre">DIM1)</span></code>. The
argument names are intended to suggest that the <code class="docutils literal notranslate"><span class="pre">double*</span></code> argument
is an input array of one dimension and that the <code class="docutils literal notranslate"><span class="pre">int</span></code> represents the
size of that dimension. This is precisely the pattern in the <code class="docutils literal notranslate"><span class="pre">rms</span></code>
prototype.</p>
<p>Most likely, no actual prototypes to be wrapped will have the argument
names <code class="docutils literal notranslate"><span class="pre">IN_ARRAY1</span></code> and <code class="docutils literal notranslate"><span class="pre">DIM1</span></code>. We use the <a class="reference external" href="https://www.swig.org">SWIG</a> <code class="docutils literal notranslate"><span class="pre">%apply</span></code>
directive to apply the typemap for one-dimensional input arrays of
type <code class="docutils literal notranslate"><span class="pre">double</span></code> to the actual prototype used by <code class="docutils literal notranslate"><span class="pre">rms</span></code>. Using
<code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> effectively, therefore, requires knowing what typemaps are
available and what they do.</p>
<p>A <a class="reference external" href="https://www.swig.org">SWIG</a> interface file that includes the <a class="reference external" href="https://www.swig.org">SWIG</a> directives given
above will produce wrapper code that looks something like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> 1 PyObject *_wrap_rms(PyObject *args) {
2 PyObject *resultobj = 0;
3 double *arg1 = (double *) 0 ;
4 int arg2 ;
5 double result;
6 PyArrayObject *array1 = NULL ;
7 int is_new_object1 = 0 ;
8 PyObject * obj0 = 0 ;
9
10 if (!PyArg_ParseTuple(args,(char *)"O:rms",&obj0)) SWIG_fail;
11 {
12 array1 = obj_to_array_contiguous_allow_conversion(
13 obj0, NPY_DOUBLE, &is_new_object1);
14 npy_intp size[1] = {
15 -1
16 };
17 if (!array1 || !require_dimensions(array1, 1) ||
18 !require_size(array1, size, 1)) SWIG_fail;
19 arg1 = (double*) array1->data;
20 arg2 = (int) array1->dimensions[0];
21 }
22 result = (double)rms(arg1,arg2);
23 resultobj = SWIG_From_double((double)(result));
24 {
25 if (is_new_object1 && array1) Py_DECREF(array1);
26 }
27 return resultobj;
28 fail:
29 {
30 if (is_new_object1 && array1) Py_DECREF(array1);
31 }
32 return NULL;
33 }
</pre></div>
</div>
<p>The typemaps from <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> are responsible for the following lines
of code: 12–20, 25 and 30. Line 10 parses the input to the <code class="docutils literal notranslate"><span class="pre">rms</span></code>
function. From the format string <code class="docutils literal notranslate"><span class="pre">"O:rms"</span></code>, we can see that the
argument list is expected to be a single Python object (specified
by the <code class="docutils literal notranslate"><span class="pre">O</span></code> before the colon) and whose pointer is stored in
<code class="docutils literal notranslate"><span class="pre">obj0</span></code>. A number of functions, supplied by <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code>, are called
to make and check the (possible) conversion from a generic Python
object to a NumPy array. These functions are explained in the
section <a class="reference internal" href="#helper-functions">Helper Functions</a>, but hopefully their names are
self-explanatory. At line 12 we use <code class="docutils literal notranslate"><span class="pre">obj0</span></code> to construct a NumPy
array. At line 17, we check the validity of the result: that it is
non-null and that it has a single dimension of arbitrary length. Once
these states are verified, we extract the data buffer and length in
lines 19 and 20 so that we can call the underlying C function at line
22. Line 25 performs memory management for the case where we have
created a new array that is no longer needed.</p>
<p>This code has a significant amount of error handling. Note the
<code class="docutils literal notranslate"><span class="pre">SWIG_fail</span></code> is a macro for <code class="docutils literal notranslate"><span class="pre">goto</span> <span class="pre">fail</span></code>, referring to the label at
line 28. If the user provides the wrong number of arguments, this
will be caught at line 10. If construction of the NumPy array
fails or produces an array with the wrong number of dimensions, these
errors are caught at line 17. And finally, if an error is detected,
memory is still managed correctly at line 30.</p>
<p>Note that if the C function signature was in a different order:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">double</span> <span class="n">rms</span><span class="p">(</span><span class="nb">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">double</span><span class="o">*</span> <span class="n">seq</span><span class="p">);</span>
</pre></div>
</div>
<p>that <a class="reference external" href="https://www.swig.org">SWIG</a> would not match the typemap signature given above with
the argument list for <code class="docutils literal notranslate"><span class="pre">rms</span></code>. Fortunately, <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> has a set of
typemaps with the data pointer given last:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">apply</span> <span class="p">(</span><span class="nb">int</span> <span class="n">DIM1</span><span class="p">,</span> <span class="n">double</span><span class="o">*</span> <span class="n">IN_ARRAY1</span><span class="p">)</span> <span class="p">{(</span><span class="nb">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">double</span><span class="o">*</span> <span class="n">seq</span><span class="p">)};</span>
</pre></div>
</div>
<p>This simply has the effect of switching the definitions of <code class="docutils literal notranslate"><span class="pre">arg1</span></code>
and <code class="docutils literal notranslate"><span class="pre">arg2</span></code> in lines 3 and 4 of the generated code above, and their
assignments in lines 19 and 20.</p>
</section>
<section id="using-numpy-i">
<h2>Using numpy.i<a class="headerlink" href="#using-numpy-i" title="Link to this heading">#</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> file is currently located in the <code class="docutils literal notranslate"><span class="pre">tools/swig</span></code>
sub-directory under the <code class="docutils literal notranslate"><span class="pre">numpy</span></code> installation directory. Typically,
you will want to copy it to the directory where you are developing
your wrappers.</p>
<p>A simple module that only uses a single <a class="reference external" href="https://www.swig.org">SWIG</a> interface file should
include the following:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="p">{</span>
<span class="c1">#define SWIG_FILE_WITH_INIT</span>
<span class="o">%</span><span class="p">}</span>
<span class="o">%</span><span class="n">include</span> <span class="s2">"numpy.i"</span>
<span class="o">%</span><span class="n">init</span> <span class="o">%</span><span class="p">{</span>
<span class="n">import_array</span><span class="p">();</span>
<span class="o">%</span><span class="p">}</span>
</pre></div>
</div>
<p>Within a compiled Python module, <code class="docutils literal notranslate"><span class="pre">import_array()</span></code> should only get
called once. This could be in a C/C++ file that you have written and
is linked to the module. If this is the case, then none of your
interface files should <code class="docutils literal notranslate"><span class="pre">#define</span> <span class="pre">SWIG_FILE_WITH_INIT</span></code> or call
<code class="docutils literal notranslate"><span class="pre">import_array()</span></code>. Or, this initialization call could be in a
wrapper file generated by <a class="reference external" href="https://www.swig.org">SWIG</a> from an interface file that has the
<code class="docutils literal notranslate"><span class="pre">%init</span></code> block as above. If this is the case, and you have more than
one <a class="reference external" href="https://www.swig.org">SWIG</a> interface file, then only one interface file should
<code class="docutils literal notranslate"><span class="pre">#define</span> <span class="pre">SWIG_FILE_WITH_INIT</span></code> and call <code class="docutils literal notranslate"><span class="pre">import_array()</span></code>.</p>
</section>
<section id="available-typemaps">
<h2>Available typemaps<a class="headerlink" href="#available-typemaps" title="Link to this heading">#</a></h2>
<p>The typemap directives provided by <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> for arrays of different
data types, say <code class="docutils literal notranslate"><span class="pre">double</span></code> and <code class="docutils literal notranslate"><span class="pre">int</span></code>, and dimensions of different
types, say <code class="docutils literal notranslate"><span class="pre">int</span></code> or <code class="docutils literal notranslate"><span class="pre">long</span></code>, are identical to one another except
for the C and NumPy type specifications. The typemaps are
therefore implemented (typically behind the scenes) via a macro:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">numpy_typemaps</span><span class="p">(</span><span class="n">DATA_TYPE</span><span class="p">,</span> <span class="n">DATA_TYPECODE</span><span class="p">,</span> <span class="n">DIM_TYPE</span><span class="p">)</span>
</pre></div>
</div>
<p>that can be invoked for appropriate <code class="docutils literal notranslate"><span class="pre">(DATA_TYPE,</span> <span class="pre">DATA_TYPECODE,</span>
<span class="pre">DIM_TYPE)</span></code> triplets. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">numpy_typemaps</span><span class="p">(</span><span class="n">double</span><span class="p">,</span> <span class="n">NPY_DOUBLE</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="o">%</span><span class="n">numpy_typemaps</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">NPY_INT</span> <span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> interface file uses the <code class="docutils literal notranslate"><span class="pre">%numpy_typemaps</span></code> macro to
implement typemaps for the following C data types and <code class="docutils literal notranslate"><span class="pre">int</span></code>
dimension types:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">signed</span> <span class="pre">char</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">char</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">short</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">short</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">int</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">int</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">long</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span> <span class="pre">long</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">float</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">double</span></code></p></li>
</ul>
<p>In the following descriptions, we reference a generic <code class="docutils literal notranslate"><span class="pre">DATA_TYPE</span></code>, which
could be any of the C data types listed above, and <code class="docutils literal notranslate"><span class="pre">DIM_TYPE</span></code> which
should be one of the many types of integers.</p>
<p>The typemap signatures are largely differentiated on the name given to
the buffer pointer. Names with <code class="docutils literal notranslate"><span class="pre">FARRAY</span></code> are for Fortran-ordered
arrays, and names with <code class="docutils literal notranslate"><span class="pre">ARRAY</span></code> are for C-ordered (or 1D arrays).</p>
<section id="input-arrays">
<h3>Input Arrays<a class="headerlink" href="#input-arrays" title="Link to this heading">#</a></h3>
<p>Input arrays are defined as arrays of data that are passed into a
routine but are not altered in-place or returned to the user. The
Python input array is therefore allowed to be almost any Python
sequence (such as a list) that can be converted to the requested type
of array. The input array signatures are</p>
<p>1D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">IN_ARRAY1[ANY]</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">IN_ARRAY1,</span> <span class="pre">int</span> <span class="pre">DIM1</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">IN_ARRAY1</span> <span class="pre">)</span></code></p></li>
</ul>
<p>2D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">IN_ARRAY2[ANY][ANY]</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">IN_ARRAY2,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">IN_ARRAY2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">IN_FARRAY2,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">IN_FARRAY2</span> <span class="pre">)</span></code></p></li>
</ul>
<p>3D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">IN_ARRAY3[ANY][ANY][ANY]</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">IN_ARRAY3,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">IN_ARRAY3</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">IN_FARRAY3,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">IN_FARRAY3</span> <span class="pre">)</span></code></p></li>
</ul>
<p>4D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE</span> <span class="pre">IN_ARRAY4[ANY][ANY][ANY][ANY])</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE*</span> <span class="pre">IN_ARRAY4,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">IN_ARRAY4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE*</span> <span class="pre">IN_FARRAY4,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">IN_FARRAY4)</span></code></p></li>
</ul>
<p>The first signature listed, <code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DATA_TYPE</span> <span class="pre">IN_ARRAY[ANY]</span> <span class="pre">)</span></code> is for
one-dimensional arrays with hard-coded dimensions. Likewise,
<code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DATA_TYPE</span> <span class="pre">IN_ARRAY2[ANY][ANY]</span> <span class="pre">)</span></code> is for two-dimensional arrays
with hard-coded dimensions, and similarly for three-dimensional.</p>
</section>
<section id="in-place-arrays">
<h3>In-Place Arrays<a class="headerlink" href="#in-place-arrays" title="Link to this heading">#</a></h3>
<p>In-place arrays are defined as arrays that are modified in-place. The
input values may or may not be used, but the values at the time the
function returns are significant. The provided Python argument
must therefore be a NumPy array of the required type. The in-place
signatures are</p>
<p>1D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">INPLACE_ARRAY1[ANY]</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY1,</span> <span class="pre">int</span> <span class="pre">DIM1</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY1</span> <span class="pre">)</span></code></p></li>
</ul>
<p>2D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">INPLACE_ARRAY2[ANY][ANY]</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY2,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_FARRAY2,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_FARRAY2</span> <span class="pre">)</span></code></p></li>
</ul>
<p>3D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">INPLACE_ARRAY3[ANY][ANY][ANY]</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY3,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY3</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_FARRAY3,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">int</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_FARRAY3</span> <span class="pre">)</span></code></p></li>
</ul>
<p>4D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE</span> <span class="pre">INPLACE_ARRAY4[ANY][ANY][ANY][ANY])</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY4,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE*</span> <span class="pre">INPLACE_FARRAY4,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">INPLACE_FARRAY4)</span></code></p></li>
</ul>
<p>These typemaps now check to make sure that the <code class="docutils literal notranslate"><span class="pre">INPLACE_ARRAY</span></code>
arguments use native byte ordering. If not, an exception is raised.</p>
<p>There is also a “flat” in-place array for situations in which
you would like to modify or process each element, regardless of the
number of dimensions. One example is a “quantization” function that
quantizes each element of an array in-place, be it 1D, 2D or whatever.
This form checks for continuity but allows either C or Fortran ordering.</p>
<p>ND:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE*</span> <span class="pre">INPLACE_ARRAY_FLAT,</span> <span class="pre">DIM_TYPE</span> <span class="pre">DIM_FLAT)</span></code></p></li>
</ul>
</section>
<section id="argout-arrays">
<h3>Argout Arrays<a class="headerlink" href="#argout-arrays" title="Link to this heading">#</a></h3>
<p>Argout arrays are arrays that appear in the input arguments in C, but
are in fact output arrays. This pattern occurs often when there is
more than one output variable and the single return argument is
therefore not sufficient. In Python, the conventional way to return
multiple arguments is to pack them into a sequence (tuple, list, etc.)
and return the sequence. This is what the argout typemaps do. If a
wrapped function that uses these argout typemaps has more than one
return argument, they are packed into a tuple or list, depending on
the version of Python. The Python user does not pass these
arrays in, they simply get returned. For the case where a dimension
is specified, the python user must provide that dimension as an
argument. The argout signatures are</p>
<p>1D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">ARGOUT_ARRAY1[ANY]</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE*</span> <span class="pre">ARGOUT_ARRAY1,</span> <span class="pre">int</span> <span class="pre">DIM1</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">DATA_TYPE*</span> <span class="pre">ARGOUT_ARRAY1</span> <span class="pre">)</span></code></p></li>
</ul>
<p>2D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">ARGOUT_ARRAY2[ANY][ANY]</span> <span class="pre">)</span></code></p></li>
</ul>
<p>3D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">ARGOUT_ARRAY3[ANY][ANY][ANY]</span> <span class="pre">)</span></code></p></li>
</ul>
<p>4D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span>   <span class="pre">DATA_TYPE</span> <span class="pre">ARGOUT_ARRAY4[ANY][ANY][ANY][ANY]</span> <span class="pre">)</span></code></p></li>
</ul>
<p>These are typically used in situations where in C/C++, you would
allocate a(n) array(s) on the heap, and call the function to fill the
array(s) values. In Python, the arrays are allocated for you and
returned as new array objects.</p>
<p>Note that we support <code class="docutils literal notranslate"><span class="pre">DATA_TYPE*</span></code> argout typemaps in 1D, but not 2D
or 3D. This is because of a quirk with the <a class="reference external" href="https://www.swig.org">SWIG</a> typemap syntax and
cannot be avoided. Note that for these types of 1D typemaps, the
Python function will take a single argument representing <code class="docutils literal notranslate"><span class="pre">DIM1</span></code>.</p>
</section>
<section id="argout-view-arrays">
<h3>Argout View Arrays<a class="headerlink" href="#argout-view-arrays" title="Link to this heading">#</a></h3>
<p>Argoutview arrays are for when your C code provides you with a view of
its internal data and does not require any memory to be allocated by
the user. This can be dangerous. There is almost no way to guarantee
that the internal data from the C code will remain in existence for
the entire lifetime of the NumPy array that encapsulates it. If
the user destroys the object that provides the view of the data before
destroying the NumPy array, then using that array may result in bad
memory references or segmentation faults. Nevertheless, there are
situations, working with large data sets, where you simply have no
other choice.</p>
<p>The C code to be wrapped for argoutview arrays are characterized by
pointers: pointers to the dimensions and double pointers to the data,
so that these values can be passed back to the user. The argoutview
typemap signatures are therefore</p>
<p>1D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY1</span> <span class="pre">)</span></code></p></li>
</ul>
<p>2D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_FARRAY2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2</span> <span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_FARRAY2</span> <span class="pre">)</span></code></p></li>
</ul>
<p>3D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY3)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_FARRAY3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_FARRAY3)</span></code></p></li>
</ul>
<p>4D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY4,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_ARRAY4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_FARRAY4,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEW_FARRAY4)</span></code></p></li>
</ul>
<p>Note that arrays with hard-coded dimensions are not supported. These
cannot follow the double pointer signatures of these typemaps.</p>
</section>
<section id="memory-managed-argout-view-arrays">
<h3>Memory Managed Argout View Arrays<a class="headerlink" href="#memory-managed-argout-view-arrays" title="Link to this heading">#</a></h3>
<p>A recent addition to <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> are typemaps that permit argout
arrays with views into memory that is managed.</p>
<p>1D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY1)</span></code></p></li>
</ul>
<p>2D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY2)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_FARRAY2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_FARRAY2)</span></code></p></li>
</ul>
<p>3D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY3)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_FARRAY3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_FARRAY3)</span></code></p></li>
</ul>
<p>4D:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY4,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_ARRAY4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_FARRAY4,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(DIM_TYPE*</span> <span class="pre">DIM1,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM2,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM3,</span> <span class="pre">DIM_TYPE*</span> <span class="pre">DIM4,</span> <span class="pre">DATA_TYPE**</span> <span class="pre">ARGOUTVIEWM_FARRAY4)</span></code></p></li>
</ul>
</section>
<section id="output-arrays">
<h3>Output Arrays<a class="headerlink" href="#output-arrays" title="Link to this heading">#</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">numpy.i</span></code> interface file does not support typemaps for output
arrays, for several reasons. First, C/C++ return arguments are
limited to a single value. This prevents obtaining dimension
information in a general way. Second, arrays with hard-coded lengths
are not permitted as return arguments. In other words:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">double</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="n">newVector</span><span class="p">(</span><span class="n">double</span> <span class="n">x</span><span class="p">,</span> <span class="n">double</span> <span class="n">y</span><span class="p">,</span> <span class="n">double</span> <span class="n">z</span><span class="p">);</span>
</pre></div>
</div>
<p>is not legal C/C++ syntax. Therefore, we cannot provide typemaps of
the form:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">typemap</span><span class="p">(</span><span class="n">out</span><span class="p">)</span> <span class="p">(</span><span class="n">TYPE</span><span class="p">[</span><span class="n">ANY</span><span class="p">]);</span>
</pre></div>
</div>
<p>If you run into a situation where a function or method is returning a
pointer to an array, your best bet is to write your own version of the
function to be wrapped, either with <code class="docutils literal notranslate"><span class="pre">%extend</span></code> for the case of class
methods or <code class="docutils literal notranslate"><span class="pre">%ignore</span></code> and <code class="docutils literal notranslate"><span class="pre">%rename</span></code> for the case of functions.</p>
</section>
<section id="other-common-types-bool">
<h3>Other Common Types: bool<a class="headerlink" href="#other-common-types-bool" title="Link to this heading">#</a></h3>
<p>Note that C++ type <code class="docutils literal notranslate"><span class="pre">bool</span></code> is not supported in the list in the
<a class="reference internal" href="#available-typemaps">Available Typemaps</a> section. NumPy bools are a single byte, while
the C++ <code class="docutils literal notranslate"><span class="pre">bool</span></code> is four bytes (at least on my system). Therefore:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">numpy_typemaps</span><span class="p">(</span><span class="nb">bool</span><span class="p">,</span> <span class="n">NPY_BOOL</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
</pre></div>
</div>
<p>will result in typemaps that will produce code that reference
improper data lengths. You can implement the following macro
expansion:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">numpy_typemaps</span><span class="p">(</span><span class="nb">bool</span><span class="p">,</span> <span class="n">NPY_UINT</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
</pre></div>
</div>
<p>to fix the data length problem, and <a class="reference internal" href="#input-arrays">Input Arrays</a> will work fine,
but <a class="reference internal" href="#in-place-arrays">In-Place Arrays</a> might fail type-checking.</p>
</section>
<section id="other-common-types-complex">
<h3>Other Common Types: complex<a class="headerlink" href="#other-common-types-complex" title="Link to this heading">#</a></h3>
<p>Typemap conversions for complex floating-point types is also not
supported automatically. This is because Python and NumPy are
written in C, which does not have native complex types. Both
Python and NumPy implement their own (essentially equivalent)
<code class="docutils literal notranslate"><span class="pre">struct</span></code> definitions for complex variables:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">/*</span> <span class="n">Python</span> <span class="o">*/</span>
<span class="n">typedef</span> <span class="n">struct</span> <span class="p">{</span><span class="n">double</span> <span class="n">real</span><span class="p">;</span> <span class="n">double</span> <span class="n">imag</span><span class="p">;}</span> <span class="n">Py_complex</span><span class="p">;</span>
<span class="o">/*</span> <span class="n">NumPy</span> <span class="o">*/</span>
<span class="n">typedef</span> <span class="n">struct</span> <span class="p">{</span><span class="nb">float</span> <span class="n">real</span><span class="p">,</span> <span class="n">imag</span><span class="p">;}</span> <span class="n">npy_cfloat</span><span class="p">;</span>
<span class="n">typedef</span> <span class="n">struct</span> <span class="p">{</span><span class="n">double</span> <span class="n">real</span><span class="p">,</span> <span class="n">imag</span><span class="p">;}</span> <span class="n">npy_cdouble</span><span class="p">;</span>
</pre></div>
</div>
<p>We could have implemented:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">numpy_typemaps</span><span class="p">(</span><span class="n">Py_complex</span> <span class="p">,</span> <span class="n">NPY_CDOUBLE</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="o">%</span><span class="n">numpy_typemaps</span><span class="p">(</span><span class="n">npy_cfloat</span> <span class="p">,</span> <span class="n">NPY_CFLOAT</span> <span class="p">,</span> <span class="nb">int</span><span class="p">)</span>