forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.dtype.cs
More file actions
432 lines (394 loc) · 16.3 KB
/
np.dtype.cs
File metadata and controls
432 lines (394 loc) · 16.3 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using NumSharp.Backends;
namespace NumSharp
{
/// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.dtype.html#numpy.dtype</remarks>
public class DType
{
protected internal static readonly Dictionary<NPTypeCode, char> _kind_list_map = new Dictionary<NPTypeCode, char>()
{
{NPTypeCode.NDArray, 'O'},
{NPTypeCode.Complex, 'c'},
{NPTypeCode.Boolean, '?'},
{NPTypeCode.Byte, 'b'},
{NPTypeCode.Int16, 'i'},
{NPTypeCode.UInt16, 'u'},
{NPTypeCode.Int32, 'i'},
{NPTypeCode.UInt32, 'u'},
{NPTypeCode.Int64, 'i'},
{NPTypeCode.UInt64, 'u'},
{NPTypeCode.Char, 'S'},
{NPTypeCode.Double, 'f'},
{NPTypeCode.Single, 'f'},
{NPTypeCode.Decimal, 'f'},
{NPTypeCode.String, 'S'},
};
/// <summary>Initializes a new instance of the <see cref="T:System.Object"></see> class.</summary>
public DType(Type type)
{
this.type = type ?? throw new ArgumentNullException(nameof(type));
typecode = type.GetTypeCode();
name = type.Name;
byteorder = '=';
itemsize = typecode.SizeOf();
TYPECHAR = typecode.ToTYPECHAR();
kind = _kind_list_map[typecode];
}
/// <summary>
/// A character indicating the byte-order of this data-type object.<br></br>
/// One of:<br></br>
///
/// '=' native<br></br>
/// '\<' little-endian<br></br>
/// '>' big-endian<br></br>
/// '|' not applicable<br></br>
/// </summary>
public char byteorder;
/// <summary>
/// The size of the dtype in bytes.
/// </summary>
public int itemsize;
/// <summary>
/// The name of this dtype.
/// </summary>
public string name;
/// <summary>
/// The actual type this dtype represents.
/// </summary>
public Type type;
/// <summary>
/// The NumSharp type code.
/// </summary>
public NPTypeCode typecode;
/// <summary>
/// A unique character code for each of the 21 different built-in types.
/// </summary>
internal NPY_TYPECHAR TYPECHAR;
/// <summary>
/// A character code (one of ‘biufcmMOSUV’) identifying the general kind of data.<br></br><br></br>
/// b boolean<br></br>
/// i signed integer<br></br>
/// u unsigned integer<br></br>
/// f floating-point<br></br>
/// c complex floating-point<br></br>
/// m timedelta<br></br>
/// M datetime<br></br>
/// O object<br></br>
/// S(byte-)string<br></br>
/// U Unicode<br></br>
/// V void<br></br>
/// </summary>
public char kind;
/// <summary>
/// A unique character code for each of the 21 different built-in types.
/// </summary>
public char @char => (char)TYPECHAR;
/// <summary>
/// Return a new dtype with a different byte order.
/// Changes are also made in all fields and sub-arrays of the data type.
/// </summary>
/// <param name="new_order">
/// Byte order to force; a value from the byte order specifications below.<br></br> The default value (‘S’) results in swapping the current byte order.<br></br> new_order codes can be any of:<br></br>
/// ‘S’ - swap dtype from current to opposite endian<br></br>
/// '=' - native order<br></br>
/// '\<' - little-endian<br></br>
/// '>' - big-endian<br></br>
/// '|' - ignore(no change to byte order)<br></br>
/// The code does a case-insensitive check on the first letter of new_order for these alternatives.<br></br>For example, any of ‘>’ or ‘B’ or ‘b’ or ‘brian’ are valid to specify big-endian.
/// </param>
/// <returns>New dtype object with the given change to the byte order.</returns>
public DType newbyteorder(char new_order = 'S')
{
throw new NotSupportedException();
}
}
public static partial class np
{
/// <summary>
/// Return the character for the minimum-size type to which given types can be safely cast.
/// The returned type character must represent the smallest size dtype such that an array of the returned type can handle the data from an array of all types in typechars(or if typechars is an array, then its dtype.char).
/// </summary>
/// <param name="typechars">every character represents a type. see <see cref="DType.@char"/></param>
/// <param name="typeset">The set of characters that the returned character is chosen from. The default set is ‘GDFgdf’.</param>
/// <param name="default">The default character, this is returned if none of the characters in typechars matches a character in typeset.</param>
/// <returns>The character representing the minimum-size type that was found.</returns>
public static char mintypecode(string typechars, string typeset = "GDFgdf", char @default = 'd')
{
const string _typecodes_by_elsize = "GDFgdfQqLlIiHhBb?";
var chars = typechars.ToCharArray();
var intersect = chars.Intersect(typeset.ToCharArray()).ToArray();
if (intersect.Length == 0)
return @default;
if (intersect.Contains('F') && intersect.Contains('d'))
return 'D';
return intersect.OrderBy(c => _typecodes_by_elsize.IndexOf(c)).First();
}
/// <summary>
/// Return the character for the minimum-size type to which given types can be safely cast.
/// The returned type character must represent the smallest size dtype such that an array of the returned type can handle the data from an array of all types in typechars(or if typechars is an array, then its dtype.char).
/// </summary>
/// <param name="typechars"></param>
/// <param name="typeset">The set of characters that the returned character is chosen from. The default set is ‘GDFgdf’.</param>
/// <param name="default">The default character, this is returned if none of the characters in typechars matches a character in typeset.</param>
/// <returns>The character representing the minimum-size type that was found.</returns>
public static char mintypecode(char[] typechars, string typeset = "GDFgdf", char @default = 'd')
{
const string _typecodes_by_elsize = "GDFgdfQqLlIiHhBb?";
var chars = typechars;
var intersect = chars.Intersect(typeset.ToCharArray()).ToArray();
if (intersect.Length == 0)
return @default;
if (intersect.Contains('F') && intersect.Contains('d'))
return 'D';
return intersect.OrderBy(c => _typecodes_by_elsize.IndexOf(c)).First();
}
/// <summary>
/// Parse a string into a <see cref="DType"/>.
/// </summary>
/// <param name="dtype"></param>
/// <returns>A <see cref="DType"/> based on <paramref name="dtype"/>, return can be null.</returns>
/// <remarks>
/// https://docs.scipy.org/doc/numpy-1.16.0/reference/arrays.dtypes.html <br></br>
/// This was created to ease the porting of C++ numpy to C#.
/// </remarks>
public static DType dtype(string dtype)
{
//TODO! we parse here the string according to docs and return the relevant dtype.
const string regex = @"^([\>\<\|S\=]?)([a-zA-Z\?]+)(\d+)?";
if (dtype.Contains("("))
throw new NotSupportedException("NumSharp does not support custom nested array dtypes");
if (Enum.TryParse<NPTypeCode>(dtype, out var code))
{
switch (code)
{
case NPTypeCode.NDArray: return new DType(typeof(IntPtr)); //todo! ??
#if _REGEN
%foreach all_dtypes%
case NPTypeCode.#1: return new DType(typeof(#1));
%
default:
throw new NotSupportedException();
#else
case NPTypeCode.Complex: return new DType(typeof(Complex));
case NPTypeCode.Boolean: return new DType(typeof(Boolean));
case NPTypeCode.Byte: return new DType(typeof(Byte));
case NPTypeCode.Int16: return new DType(typeof(Int16));
case NPTypeCode.UInt16: return new DType(typeof(UInt16));
case NPTypeCode.Int32: return new DType(typeof(Int32));
case NPTypeCode.UInt32: return new DType(typeof(UInt32));
case NPTypeCode.Int64: return new DType(typeof(Int64));
case NPTypeCode.UInt64: return new DType(typeof(UInt64));
case NPTypeCode.Char: return new DType(typeof(Char));
case NPTypeCode.Double: return new DType(typeof(Double));
case NPTypeCode.Single: return new DType(typeof(Single));
case NPTypeCode.Decimal: return new DType(typeof(Decimal));
case NPTypeCode.String: return new DType(typeof(String));
default:
throw new NotSupportedException();
#endif
}
}
var match = Regex.Match(dtype, regex);
if (!match.Success)
return null;
var byteorder = match.Groups[1].Value;
var type = match.Groups[2].Value;
var size_str = match.Groups[3].Value?.Trim();
if (string.IsNullOrEmpty(size_str))
size_str = "-1";
int size = int.Parse(size_str);
//sizeless types
switch (type)
{
case "c":
case "complex":
case "Complex":
return new DType(typeof(Complex));
case "string":
case "chars":
case "char":
case "S":
case "U":
return new DType(typeof(char));
case "b":
case "byte":
case "Byte":
return new DType(typeof(byte));
case "bool":
case "Bool":
case "Boolean":
case "boolean":
case "?":
return new DType(typeof(bool));
}
//size-specific
switch (size)
{
case -1:
switch (type)
{
case "i":
case "int":
return new DType(typeof(Int32));
case "u":
case "uint":
return new DType(typeof(UInt32));
case "f":
case "float":
case "single":
case "Float":
case "Single":
return new DType(typeof(float));
case "d":
case "double":
case "Double":
return new DType(typeof(double));
}
break;
case 1:
switch (type)
{
case "?":
return new DType(typeof(bool));
case "b":
case "i":
case "int":
case "Int":
return new DType(typeof(byte));
case "u":
case "uint":
case "Uint":
return new DType(typeof(UInt16));
}
break;
case 2:
switch (type)
{
case "i":
case "int":
case "Int":
return new DType(typeof(Int16));
case "u":
case "uint":
case "Uint":
return new DType(typeof(UInt16));
case "f":
case "float":
case "Float":
case "single":
case "Single":
return new DType(typeof(float));
}
break;
case 4:
switch (type)
{
case "i":
case "int":
return new DType(typeof(Int32));
case "u":
case "uint":
return new DType(typeof(UInt32));
case "f":
case "float":
case "single":
case "Float":
case "Single":
return new DType(typeof(float));
case "d":
case "double":
case "Double":
return new DType(typeof(double));
}
break;
case 8:
case 16:
switch (type)
{
case "i":
case "int":
case "Int":
return new DType(typeof(Int64));
case "u":
case "uint":
case "Uint":
return new DType(typeof(UInt64));
case "d":
case "f":
case "float":
case "Float":
case "single":
case "Single":
case "double":
case "Double":
return new DType(typeof(double));
}
break;
}
throw new NotSupportedException($"NumSharp does not support this specific {type}");
}
}
internal enum NPY_SCALARKIND
{
NPY_NOSCALAR = -1,
NPY_BOOL_SCALAR,
NPY_INTPOS_SCALAR,
NPY_INTNEG_SCALAR,
NPY_FLOAT_SCALAR,
NPY_COMPLEX_SCALAR,
NPY_OBJECT_SCALAR
};
/// <summary>
/// https://docs.scipy.org/doc/numpy-1.16.1/reference/c-api.dtype.html#enumerated-types
/// </summary>
internal enum NPY_TYPECHAR
{
NPY_BOOLLTR = '?',
NPY_BYTELTR = 'b',
NPY_UBYTELTR = 'B',
NPY_SHORTLTR = 'h',
NPY_USHORTLTR = 'H',
NPY_INTLTR = 'i',
NPY_UINTLTR = 'I',
NPY_LONGLTR = 'l',
NPY_ULONGLTR = 'L',
NPY_LONGLONGLTR = 'q',
NPY_ULONGLONGLTR = 'Q',
NPY_HALFLTR = 'e',
NPY_FLOATLTR = 'f',
NPY_DOUBLELTR = 'd',
NPY_LONGDOUBLELTR = 'g',
NPY_CFLOATLTR = 'F',
NPY_CDOUBLELTR = 'D',
NPY_CLONGDOUBLELTR = 'G',
NPY_OBJECTLTR = 'O',
NPY_STRINGLTR = 'S',
NPY_STRINGLTR2 = 'a',
NPY_UNICODELTR = 'U',
NPY_VOIDLTR = 'V',
NPY_DATETIMELTR = 'M',
NPY_TIMEDELTALTR = 'm',
NPY_CHARLTR = 'c',
/*
* No Descriptor, just a define -- this let's
* Python users specify an array of integers
* large enough to hold a pointer on the
* platform
*/
NPY_INTPLTR = 'p',
NPY_UINTPLTR = 'P',
/*
* These are for dtype 'kinds', not dtype 'typecodes'
* as the above are for.
*/
NPY_GENBOOLLTR = 'b',
NPY_SIGNEDLTR = 'i',
NPY_UNSIGNEDLTR = 'u',
NPY_FLOATINGLTR = 'f',
NPY_COMPLEXLTR = 'c'
};
}