ndtypes 0.2.0dev4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +50 -0
  3. data/Gemfile +2 -0
  4. data/History.md +0 -0
  5. data/README.md +19 -0
  6. data/Rakefile +125 -0
  7. data/ext/ruby_ndtypes/extconf.rb +55 -0
  8. data/ext/ruby_ndtypes/gc_guard.c +36 -0
  9. data/ext/ruby_ndtypes/gc_guard.h +12 -0
  10. data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
  11. data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
  12. data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
  13. data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
  14. data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
  15. data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
  16. data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
  17. data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
  18. data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
  19. data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
  20. data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
  21. data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
  22. data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
  23. data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
  24. data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
  25. data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
  26. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
  27. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
  28. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
  29. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
  30. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
  31. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
  32. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
  33. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
  34. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
  35. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
  36. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
  37. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
  38. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
  39. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
  40. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
  41. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
  42. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
  43. data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
  44. data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
  45. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
  46. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
  47. data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
  48. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
  49. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
  50. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
  51. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
  52. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
  53. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
  54. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
  55. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
  56. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
  57. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
  58. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
  59. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
  60. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
  61. data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
  62. data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
  63. data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
  64. data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
  65. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
  66. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
  67. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
  68. data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
  69. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
  70. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
  71. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
  72. data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
  73. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
  74. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
  75. data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
  76. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
  77. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
  78. data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
  79. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
  80. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
  81. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
  82. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
  83. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
  84. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
  85. data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
  86. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
  87. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
  88. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
  89. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
  90. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
  91. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
  92. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
  93. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
  94. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
  95. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
  96. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
  97. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
  98. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
  99. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
  100. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
  101. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
  102. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
  103. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
  104. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
  105. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
  106. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
  107. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
  108. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
  109. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
  110. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
  111. data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
  112. data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
  113. data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
  114. data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
  115. data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
  116. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
  117. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
  118. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
  119. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
  120. data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
  121. data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
  122. data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
  123. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
  124. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
  125. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
  126. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
  127. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
  128. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
  129. data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
  130. data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
  131. data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
  132. data/lib/ndtypes.rb +45 -0
  133. data/lib/ndtypes/errors.rb +2 -0
  134. data/lib/ndtypes/version.rb +6 -0
  135. data/ndtypes.gemspec +47 -0
  136. data/spec/gc_table_spec.rb +10 -0
  137. data/spec/ndtypes_spec.rb +289 -0
  138. data/spec/spec_helper.rb +241 -0
  139. metadata +242 -0
@@ -0,0 +1,144 @@
1
+ .. meta::
2
+ :robots: index,follow
3
+ :description: ndtypes quickstart
4
+ :keywords: ndtypes examples
5
+
6
+ .. sectionauthor:: Stefan Krah <skrah at bytereef.org>
7
+
8
+
9
+ Quick Start
10
+ ===========
11
+
12
+ Install
13
+ -------
14
+
15
+ Prerequisites
16
+ ~~~~~~~~~~~~~
17
+
18
+ Python2 is not supported. If not already present, install the Python3
19
+ development packages:
20
+
21
+ .. code-block:: sh
22
+
23
+ # Debian, Ubuntu:
24
+ sudo apt-get install gcc make
25
+ sudo apt-get install python3-dev
26
+
27
+ # Fedora, RedHat:
28
+ sudo yum install gcc make
29
+ sudo yum install python3-devel
30
+
31
+ # openSUSE:
32
+ sudo zypper install gcc make
33
+ sudo zypper install python3-devel
34
+
35
+ # BSD:
36
+ # You know what to do.
37
+
38
+ # Mac OS X:
39
+ # Install Xcode and Python 3 headers.
40
+
41
+
42
+ Install
43
+ ~~~~~~~
44
+
45
+ If `pip <http://pypi.python.org/pypi/pip>`_ is present on the system, installation
46
+ should be as easy as:
47
+
48
+ .. code-block:: sh
49
+
50
+ pip install ndtypes
51
+
52
+
53
+ Otherwise:
54
+
55
+ .. code-block:: sh
56
+
57
+ tar xvzf ndtypes-0.2.0dev3.tar.gz
58
+ cd ndtypes-0.2.0dev3
59
+ python3 setup.py install
60
+
61
+
62
+ Windows
63
+ ~~~~~~~
64
+
65
+ Refer to the instructions in the *vcbuild* directory in the source distribution.
66
+
67
+
68
+ Examples
69
+ --------
70
+
71
+ The libndtypes Python bindings are mostly useful in conjunction with other
72
+ modules like the xnd module. While the underlying libndtypes does most of
73
+ the heavy-lifting for libraries like libxnd, virtually all of this happens
74
+ on the C level.
75
+
76
+
77
+ Nevertheless, some selected examples should give a good understanding of
78
+ what libndtypes and ndtypes actually do:
79
+
80
+
81
+ Create types
82
+ ~~~~~~~~~~~~
83
+
84
+ The most fundamental operation is to create a type:
85
+
86
+ .. doctest::
87
+
88
+ >>> from ndtypes import *
89
+ >>> t = ndt("2 * 3 * int64")
90
+ >>> t
91
+ ndt("2 * 3 * int64")
92
+
93
+
94
+ This type describes a 2 by 3 array with an int64 data type. Types have
95
+ common and individual properties.
96
+
97
+
98
+ Type properties
99
+ ~~~~~~~~~~~~~~~
100
+
101
+ All types have the following properties (continuing the example above):
102
+
103
+ .. doctest::
104
+
105
+ >>> t.ndim
106
+ 2
107
+ >>> t.datasize
108
+ 48
109
+ >>> t.itemsize
110
+ 8
111
+ >>> t.align
112
+ 8
113
+
114
+ Array types have these individual properties:
115
+
116
+ .. doctest::
117
+
118
+ >>> t.shape
119
+ (2, 3)
120
+
121
+ >>> t.strides
122
+ (24, 8)
123
+
124
+ For NumPy compatibility ndtypes displays *strides* (amount of bytes to skip).
125
+ Internally, libndtypes uses steps (amount of indices to skip).
126
+
127
+
128
+ Internals
129
+ ~~~~~~~~~
130
+
131
+ This is how to display the internal type AST:
132
+
133
+ .. doctest::
134
+
135
+ >>> print(t.ast_repr())
136
+ FixedDim(
137
+ FixedDim(
138
+ Int64(access=Concrete, ndim=0, datasize=8, align=8, flags=[]),
139
+ tag=None, shape=3, itemsize=8, step=1,
140
+ access=Concrete, ndim=1, datasize=24, align=8, flags=[]
141
+ ),
142
+ tag=None, shape=2, itemsize=8, step=3,
143
+ access=Concrete, ndim=2, datasize=48, align=8, flags=[]
144
+ )
@@ -0,0 +1,544 @@
1
+ .. meta::
2
+ :robots: index,follow
3
+ :description: ndtypes datashape specification
4
+ :keywords: ndtypes, datashape, specification
5
+
6
+ .. sectionauthor:: Stefan Krah <skrah at bytereef.org>
7
+
8
+
9
+ .. _types:
10
+
11
+ #####
12
+ Types
13
+ #####
14
+
15
+ The set of all types comprises :ref:`dtypes <dtypes>` and :ref:`arrays <arrays>`.
16
+
17
+ The rest of this document assumes that the :py:mod:`ndtypes` module has been
18
+ imported:
19
+
20
+ .. testcode::
21
+
22
+ from ndtypes import ndt
23
+
24
+ .. _dtypes:
25
+
26
+ ******
27
+ Dtypes
28
+ ******
29
+
30
+ An important notion in datashape is the ``dtype``, which roughly translates to
31
+ the element type of an array. In datashape, the ``dtype`` can be of arbitrary
32
+ complexity and can contain e.g. tuples, records and functions.
33
+
34
+
35
+ .. _scalars:
36
+
37
+ =======
38
+ Scalars
39
+ =======
40
+
41
+ Scalars are the primitive C/C++ types. Most scalars are fixed-size and platform
42
+ independent.
43
+
44
+
45
+ Fixed size
46
+ ----------
47
+
48
+ Datashape offers a number of fixed-size scalars. Here's how to construct a simple
49
+ :c:type:`int64_t` type:
50
+
51
+ .. doctest::
52
+
53
+ >>> ndt('int64')
54
+ ndt("int64")
55
+
56
+
57
+ All fixed-size scalars:
58
+
59
+ +-----------+-----------------+------------+--------------+---------------+-----------------------+
60
+ | void | boolean | signed int | unsigned int | float [#f2]_ | complex |
61
+ +===========+=================+============+==============+===============+=======================+
62
+ | ``void`` | ``bool`` [#f1]_ | ``int8`` | ``uint8`` | ``float16`` | ``complex32`` |
63
+ +-----------+-----------------+------------+--------------+---------------+-----------------------+
64
+ | | | ``int16`` | ``uint16`` | ``float32`` | ``complex64`` [#f3]_ |
65
+ +-----------+-----------------+------------+--------------+---------------+-----------------------+
66
+ | | | ``int32`` | ``uint32`` | ``float64`` | ``complex128`` [#f4]_ |
67
+ +-----------+-----------------+------------+--------------+---------------+-----------------------+
68
+ | | | ``int64`` | ``uint64`` | | |
69
+ +-----------+-----------------+------------+--------------+---------------+-----------------------+
70
+
71
+ .. [#f1] implemented as :c:type:`char`
72
+ .. [#f2] IEEE 754-2008 binary floating point types
73
+ .. [#f3] implemented as :c:type:`complex\<float32\>`
74
+ .. [#f4] implemented as :c:type:`complex\<float64\>`
75
+
76
+
77
+ Aliases
78
+ -------
79
+
80
+ Datashape has a number of aliases for scalars, which are internally mapped
81
+ to their corresponding platform specific fixed-size types. This is how to
82
+ construct an :c:type:`intptr_t`:
83
+
84
+ .. doctest::
85
+
86
+ >>> ndt('intptr')
87
+ ndt("int64")
88
+
89
+ Machine dependent aliases:
90
+
91
+ +-----------------+----------+------------------+
92
+ | ``intptr`` | :c:type:`intptr_t` |
93
+ +-----------------+----------+------------------+
94
+ | ``uintptr`` | :c:type:`uintptr_t` |
95
+ +-----------------+-----------------------------+
96
+
97
+
98
+ =====================
99
+ Chars, strings, bytes
100
+ =====================
101
+
102
+ Encodings
103
+ ---------
104
+
105
+ Datashape defines the following encodings for strings and characters. Each encoding
106
+ has several aliases:
107
+
108
+ +-----------------+----------------------+
109
+ | canonical form | aliases |
110
+ +=================+=========+============+
111
+ | 'ascii' | 'A' | 'us-ascii' |
112
+ +-----------------+---------+------------+
113
+ | 'utf8' | 'U8' | 'utf-8' |
114
+ +-----------------+---------+------------+
115
+ | 'utf16' | 'U16' | 'utf-16' |
116
+ +-----------------+---------+------------+
117
+ | 'utf32' | 'U32' | 'utf-32' |
118
+ +-----------------+---------+------------+
119
+ | 'ucs2' | 'ucs_2' | 'ucs2' |
120
+ +-----------------+---------+------------+
121
+
122
+
123
+ As seen in the table, encodings must be given in string form:
124
+
125
+ .. doctest::
126
+
127
+ >>> ndt("char('utf16')")
128
+ ndt("char('utf16')")
129
+
130
+
131
+ Chars
132
+ -----
133
+
134
+ The ``char`` constructor accepts ``'ascii'``, ``'ucs2'`` and ``'utf32'`` encoding
135
+ arguments. ``char`` without arguments is equivalent to ``char(utf32)``.
136
+
137
+ .. doctest::
138
+
139
+ >>> ndt("char('ascii')")
140
+ ndt("char('ascii')")
141
+
142
+ >>> ndt("char('utf32')")
143
+ ndt("char('utf32')")
144
+
145
+ >>> ndt("char")
146
+ ndt("char('utf32')")
147
+
148
+
149
+ UTF-8 strings
150
+ -------------
151
+
152
+ The ``string`` type is a variable length NUL-terminated UTF-8 string:
153
+
154
+ .. doctest::
155
+
156
+ >>> ndt("string")
157
+ ndt("string")
158
+
159
+
160
+ .. _fixed-string:
161
+
162
+ Fixed size strings
163
+ ------------------
164
+
165
+ The ``fixed_string`` type takes a length and an optional encoding argument:
166
+
167
+ .. doctest::
168
+
169
+ >>> ndt("fixed_string(1729)")
170
+ ndt("fixed_string(1729)")
171
+
172
+ >>> ndt("fixed_string(1729, 'utf16')")
173
+ ndt("fixed_string(1729, 'utf16')")
174
+
175
+
176
+ Bytes
177
+ -----
178
+
179
+ The `bytes` type is variable length and takes an optional alignment argument.
180
+ Valid values are powers of two in the range ``[1, 16]``.
181
+
182
+ .. doctest::
183
+
184
+ >>> ndt("bytes")
185
+ ndt("bytes")
186
+
187
+ >>> ndt("bytes(align=2)")
188
+ ndt("bytes(align=2)")
189
+
190
+
191
+ .. _fixed-bytes:
192
+
193
+ Fixed size bytes
194
+ ----------------
195
+
196
+ The ``fixed_bytes`` type takes a length and an optional alignment argument.
197
+ The latter is a keyword-only argument in order to prevent accidental swapping of
198
+ the two integer arguments:
199
+
200
+ .. doctest::
201
+
202
+ >>> ndt("fixed_bytes(size=32)")
203
+ ndt("fixed_bytes(size=32)")
204
+
205
+ >>> ndt("fixed_bytes(size=128, align=8)")
206
+ ndt("fixed_bytes(size=128, align=8)")
207
+
208
+
209
+ ==========
210
+ References
211
+ ==========
212
+
213
+ Datashape references are fully general and can point to types of arbitrary
214
+ complexity:
215
+
216
+ .. doctest::
217
+
218
+ >>> ndt("ref(int64)")
219
+ ndt("ref(int64)")
220
+
221
+ >>> ndt("ref(10 * {a: int64, b: 10 * float64})")
222
+ ndt("ref(10 * {a : int64, b : 10 * float64})")
223
+
224
+
225
+ ================
226
+ Categorical type
227
+ ================
228
+
229
+ The categorical type allows to specify subsets of types. This is implemented
230
+ as a set of typed values. Types are inferred and interpreted as int64, float64
231
+ or strings. The *NA* keyword creates a category for missing values.
232
+
233
+ .. doctest::
234
+
235
+ >>> ndt("categorical(1, 10)")
236
+ ndt("categorical(1, 10)")
237
+
238
+ >>> ndt("categorical(1.2, 100.0)")
239
+ ndt("categorical(1.2, 100)")
240
+
241
+ >>> ndt("categorical('January', 'August')")
242
+ ndt("categorical('January', 'August')")
243
+
244
+ >>> ndt("categorical('January', 'August', NA)")
245
+ ndt("categorical('January', 'August', NA)")
246
+
247
+
248
+ ===========
249
+ Option type
250
+ ===========
251
+
252
+ The option type provides safe handling of values that may or may not be present.
253
+ The concept is well-known from languages like ML or SQL.
254
+
255
+ .. doctest::
256
+
257
+ >>> ndt("?complex64")
258
+ ndt("?complex64")
259
+
260
+
261
+ .. _dtype-variables:
262
+
263
+ ===============
264
+ Dtype variables
265
+ ===============
266
+
267
+ Dtype variables are used in quantifier free type schemes and pattern matching.
268
+ The range of a variable extends over the entire type term.
269
+
270
+ .. doctest::
271
+
272
+ >>> ndt("T")
273
+ ndt("T")
274
+
275
+ >>> ndt("10 * 16 * T")
276
+ ndt("10 * 16 * T")
277
+
278
+
279
+ .. _symbolic-constructors:
280
+
281
+ =====================
282
+ Symbolic constructors
283
+ =====================
284
+
285
+ Symbolic constructors stand for any constructor that takes the given datashape
286
+ argument. Used in pattern matching.
287
+
288
+ .. doctest::
289
+
290
+ >>> ndt("Coulomb(float64)")
291
+ ndt("Coulomb(float64)")
292
+
293
+
294
+ .. _type-kinds:
295
+
296
+ ==========
297
+ Type kinds
298
+ ==========
299
+
300
+ Type kinds denote specific subsets of :ref:`dtypes <dtypes>`, :ref:`types <types>`
301
+ or :ref:`dimension types <arrays>`. Type kinds are in the dtype section because
302
+ of the way the grammar is organized. Currently available are:
303
+
304
+ +---------------------+-------------------------------+-------------------------------+
305
+ | type kind | set | specific subset |
306
+ +=====================+===============================+===============================+
307
+ | ``Any`` | ``datashape`` | ``datashape`` |
308
+ +---------------------+-------------------------------+-------------------------------+
309
+ | ``Scalar`` | ``dtypes`` | ``scalars`` |
310
+ +---------------------+-------------------------------+-------------------------------+
311
+ | ``Categorical`` | ``dtypes`` | ``categoricals`` |
312
+ +---------------------+-------------------------------+-------------------------------+
313
+ | ``FixedString`` | ``dtypes`` | ``fixed_strings`` |
314
+ +---------------------+-------------------------------+-------------------------------+
315
+ | ``FixedBytes`` | ``dtypes`` | ``fixed_bytes`` |
316
+ +---------------------+-------------------------------+-------------------------------+
317
+ | ``Fixed`` | ``dimension kind instances`` | ``fixed dimensions`` |
318
+ +---------------------+-------------------------------+-------------------------------+
319
+
320
+
321
+ Type kinds are used in :ref:`pattern matching <type-kinds-matching>`.
322
+
323
+
324
+ ===============
325
+ Composite types
326
+ ===============
327
+
328
+ Datashape has container and function :ref:`dtypes <dtypes>`.
329
+
330
+ Tuples
331
+ ------
332
+
333
+ As usual, the tuple type is the product type of a fixed number of types:
334
+
335
+ .. doctest::
336
+
337
+ >>> ndt("(int64, float32, string)")
338
+ ndt("(int64, float32, string)")
339
+
340
+
341
+ Tuples can be nested:
342
+
343
+ .. doctest::
344
+
345
+ >>> ndt("(bytes, (int8, fixed_string(10)))")
346
+ ndt("(bytes, (int8, fixed_string(10)))")
347
+
348
+
349
+ Records
350
+ -------
351
+
352
+ Records are equivalent to tuples with named fields:
353
+
354
+ .. doctest::
355
+
356
+ >>> ndt("{a: float32, b: float64}")
357
+ ndt("{a : float32, b : float64}")
358
+
359
+
360
+ Functions
361
+ ---------
362
+
363
+ In datashape, function types can have positional and keyword arguments.
364
+ Internally, positional arguments are represented by a tuple and keyword
365
+ arguments by a record. Both kinds of arguments can be variadic.
366
+
367
+
368
+ Positional-only
369
+ ~~~~~~~~~~~~~~~
370
+
371
+ This is a function type with a single positional ``int32`` argument, returning
372
+ an ``int32``:
373
+
374
+ .. doctest::
375
+
376
+ >>> ndt("(int32) -> int32")
377
+ ndt("(int32) -> int32")
378
+
379
+
380
+ This is a function type with three positional arguments:
381
+
382
+ .. doctest::
383
+
384
+ >>> ndt("(int32, complex128, string) -> float64")
385
+ ndt("(int32, complex128, string) -> float64")
386
+
387
+
388
+ Positional-variadic
389
+ ~~~~~~~~~~~~~~~~~~~
390
+
391
+ This is a function type with a single required positional argument,
392
+ followed by any number of additional positional arguments:
393
+
394
+ .. doctest::
395
+
396
+ >>> ndt("(int32, ...) -> int32")
397
+ ndt("(int32, ...) -> int32")
398
+
399
+
400
+
401
+ .. _arrays:
402
+
403
+ ******
404
+ Arrays
405
+ ******
406
+
407
+ In datashape dimension kinds [#f6]_ are part of array type declarations. Datashape
408
+ supports the following dimension kinds:
409
+
410
+
411
+ .. _fixed-dimension:
412
+
413
+ ===============
414
+ Fixed Dimension
415
+ ===============
416
+
417
+ A fixed dimension denotes an array type with a fixed number of elements of
418
+ a specific type. The type can be written in two ways:
419
+
420
+ .. doctest::
421
+
422
+ >>> ndt("fixed(shape=10) * uint64")
423
+ ndt("10 * uint64")
424
+
425
+ >>> ndt("10 * uint64")
426
+ ndt("10 * uint64")
427
+
428
+ Formally, ``fixed(shape=10)`` is a dimension constructor, not a type constructor.
429
+ The ``*`` is the array type constructor in infix notation, taking as arguments
430
+ a dimension and an element type.
431
+
432
+ The second form is equivalent to the first one. For users of other languages,
433
+ it may be helpful to view this type as ``array[10] of uint64``.
434
+
435
+
436
+ Multidimensional arrays are constructed in the same manner, the ``*`` is
437
+ right associative:
438
+
439
+ .. doctest::
440
+
441
+ >>> ndt("10 * 25 * float64")
442
+ ndt("10 * 25 * float64")
443
+
444
+
445
+ Again, it may help to view this type as ``array[10] of (array[25] of float64)``.
446
+
447
+ In this case, ``float64`` is the :ref:`dtype <dtypes>` of the multidimensional
448
+ array.
449
+
450
+ Dtypes can be arbitrarily complex. Here is an array with a dtype of a record that
451
+ contains another array:
452
+
453
+ .. doctest::
454
+
455
+ >>> ndt("120 * {size: int32, items: 10 * int8}")
456
+ ndt("120 * {size : int32, items : 10 * int8}")
457
+
458
+
459
+ .. _variable-dimension:
460
+
461
+ ==================
462
+ Variable Dimension
463
+ ==================
464
+
465
+ The variable dimension kind describes an array type with a variable number
466
+ of elements of a specific type:
467
+
468
+ .. doctest::
469
+
470
+ >>> ndt("var * float32")
471
+ ndt("var * float32")
472
+
473
+ In this case, ``var`` is the dimension constructor and the ``*`` fulfils the
474
+ same role as above. Many managed languages have variable sized arrays, so this
475
+ type could be viewed as ``array of float32``. In a sense, fixed size arrays
476
+ are just a special case of variable sized arrays.
477
+
478
+
479
+ .. _symbolic-dim:
480
+
481
+ ==================
482
+ Symbolic Dimension
483
+ ==================
484
+
485
+ Datashape supports symbolic dimensions, which are used in pattern matching. A
486
+ symbolic dimension is an uppercase variable that stands for a fixed dimension.
487
+
488
+ In this manner entire sets of array types can be specified. The following type
489
+ describes the set of all ``M * N`` matrices with a ``float32`` dtype:
490
+
491
+ .. doctest::
492
+
493
+ >>> ndt("M * N * float32")
494
+ ndt("M * N * float32")
495
+
496
+
497
+ The next type describes a function that performs matrix multiplication on any
498
+ permissible pair of input matrices with dtype ``T``:
499
+
500
+ .. doctest::
501
+
502
+ >>> ndt("(M * N * T, N * P * T) -> M * P * T")
503
+ ndt("(M * N * T, N * P * T) -> M * P * T")
504
+
505
+ In this case, we have used both symbolic dimensions and the type variable ``T``.
506
+
507
+
508
+ Symbolic dimensions can be mixed fixed dimensions:
509
+
510
+ .. doctest::
511
+
512
+ >>> ndt("10 * N * float64")
513
+ ndt("10 * N * float64")
514
+
515
+
516
+ .. _ellipsis-dim:
517
+
518
+ ==================
519
+ Ellipsis Dimension
520
+ ==================
521
+
522
+ The ellipsis, used in pattern matching, stands for any number of dimensions.
523
+ Datashape supports both named and unnamed ellipses:
524
+
525
+ .. doctest::
526
+
527
+ >>> ndt("... * float32")
528
+ ndt("... * float32")
529
+
530
+
531
+ Named form:
532
+
533
+ .. doctest::
534
+
535
+ >>> ndt("Dim... * float32")
536
+ ndt("Dim... * float32")
537
+
538
+ Ellipsis dimensions play an important role in broadcasting, more on the topic
539
+ in the section on pattern matching.
540
+
541
+ |
542
+ |
543
+
544
+ .. [#f6] In the whole text *dimension kind* and *dimension* are synonymous.