forked from SciSharp/Numpy.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDarray.cs
More file actions
675 lines (625 loc) · 25.6 KB
/
NDarray.cs
File metadata and controls
675 lines (625 loc) · 25.6 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using Numpy.Models;
using Python.Runtime;
namespace Numpy
{
public partial class NDarray : PythonObject
{
// this is needed for constructors in NDarray<T>
protected NDarray() : base() { }
// these are manual overrides of functions or properties that can not be automatically generated
public NDarray(PyObject pyobj) : base(pyobj)
{
}
public NDarray(NDarray t) : base((PyObject) t.PyObject)
{
}
/// <summary>
/// Creates an array from an unmanaged (or fixed) memory pointer without copying.
/// </summary>
/// <param name="dataPtr">Pointer to a block of unmanaged memory or to a block of pinned managed memory</param>
/// <param name="dataLength">The length of the block of memory in bytes</param>
/// <param name="dtype">The data type of the resulting NDarray</param>
public NDarray(IntPtr dataPtr, long dataLength, Dtype dtype) : base()
{
// adapted from https://pastebin.com/4hANmBNy
// thanks to Eli Belash
var g = (Numpy.ctypes.dynamic_self.c_uint8 * dataLength).from_address(dataPtr.ToInt64());
self = np.dynamic_self.frombuffer(g, dtype.PyObject, -1);
}
/// <summary>
/// Returns a copy of the array data
/// </summary>
public T[] GetData<T>()
{
if (!PyObject.flags.c_contiguous)
return np.ascontiguousarray(this).GetData<T>();
long ptr = PyObject.ctypes.data;
int size = PyObject.size;
object array = null;
if (typeof(T) == typeof(byte)) array = new byte[size];
else if (typeof(T) == typeof(short)) array = new short[size];
else if (typeof(T) == typeof(int)) array = new int[size];
else if (typeof(T) == typeof(long)) array = new long[size];
else if (typeof(T) == typeof(float)) array = new float[size];
else if (typeof(T) == typeof(double)) array = new double[size];
else if (typeof(T) == typeof(bool)) array = new byte[size];
else if (typeof(T) == typeof(Complex)) array = new Complex[size];
else
throw new InvalidOperationException(
"Can not copy the data with data type due to limitations of Marshal.Copy: " + typeof(T).Name);
switch (array)
{
case byte[] a:
Marshal.Copy(new IntPtr(ptr), a, 0, a.Length);
break;
case short[] a:
Marshal.Copy(new IntPtr(ptr), a, 0, a.Length);
break;
case int[] a:
Marshal.Copy(new IntPtr(ptr), a, 0, a.Length);
break;
case long[] a:
Marshal.Copy(new IntPtr(ptr), a, 0, a.Length);
break;
case float[] a:
Marshal.Copy(new IntPtr(ptr), a, 0, a.Length);
break;
case double[] a:
Marshal.Copy(new IntPtr(ptr), a, 0, a.Length);
break;
case Complex[] a:
var real = this.real.GetData<double>();
var imag = this.imag.GetData<double>();
for(int i =0; i<a.Length; i++)
a[i]=new Complex(real[i], imag[i]);
break;
}
// special handling for types that are not supported by Marshal.Copy: must be converted i.e. 1 => true, 0 => false
if (typeof(T) == typeof(bool)) return (T[])(object)((byte[])array).Select(x=>x>0).ToArray();
return (T[]) array;
}
/// <summary>
/// Information about the memory layout of the array.
/// </summary>
public Flags flags => new Flags(self.GetAttr("flags")); // TODO: implement Flags
/// <summary>
/// Tuple of array dimensions.
/// </summary>
public Shape shape => new Shape( self.GetAttr("shape").As<int[]>());
/// <summary>
/// Tuple of bytes to step in each dimension when traversing an array.
/// </summary>
public int[] strides => self.GetAttr("strides").As<int[]>();
/// <summary>
/// Number of array dimensions.
/// </summary>
public int ndim => self.GetAttr("ndim").As<int>();
/// <summary>
/// Python buffer object pointing to the start of the array’s data.
/// </summary>
public PyObject data => self.GetAttr("data");
/// <summary>
/// Number of elements in the array.
/// </summary>
public int size => self.GetAttr("size").As<int>();
/// <summary>
/// Length of one array element in bytes.
/// </summary>
public int itemsize => self.GetAttr("itemsize").As<int>();
/// <summary>
/// Total bytes consumed by the elements of the array.
/// </summary>
public int nbytes => self.GetAttr("nbytes").As<int>();
/// <summary>
/// Base object if memory is from some other object.
/// </summary>
public NDarray @base
{
get
{
PyObject base_obj = self.GetAttr("base");
if (base_obj.IsNone())
return null;
return new NDarray(base_obj);
}
}
/// <summary>
/// Data-type of the array’s elements.
/// </summary>
public Dtype dtype => new Dtype(self.GetAttr("dtype"));
/// <summary>
/// Same as self.transpose(), except that self is returned if self.ndim < 2.
/// </summary>
public NDarray T => new NDarray(self.GetAttr("T"));
///// <summary>
///// The real part of the array.
///// </summary>
//public NDarray real => new NDarray(self.GetAttr("real"));
///// <summary>
///// The imaginary part of the array.
///// </summary>
//public NDarray imag => new NDarray(self.GetAttr("imag"));
/// <summary>
/// A 1-D iterator over the array.
/// </summary>
public PyObject flat => self.GetAttr("flat"); // todo: wrap and support usecases
/// <summary>
/// An object to simplify the interaction of the array with the ctypes module.
/// </summary>
public PyObject ctypes => self.GetAttr("ctypes"); // TODO: wrap ctypes
/// <summary>
/// Length of the array (same as size)
/// </summary>
public int len => self.InvokeMethod("__len__").As<int>();
/// <summary>
/// Insert scalar into an array (scalar is cast to array’s dtype, if possible)
///
/// There must be at least 1 argument, and define the last argument
/// as item. Then, a.itemset(*args) is equivalent to but faster
/// than a[args] = item. The item should be a scalar value and args
/// must select a single item in the array a.
///
/// Notes
///
/// Compared to indexing syntax, itemset provides some speed increase
/// for placing a scalar into a particular location in an ndarray,
/// if you must do this. However, generally this is discouraged:
/// among other problems, it complicates the appearance of the code.
/// Also, when using itemset (and item) inside a loop, be sure
/// to assign the methods to a local variable to avoid the attribute
/// look-up at each loop iteration.
/// </summary>
/// <param name="args">
/// If one argument: a scalar, only used in case a is of size 1.
/// If two arguments: the last argument is the value to be set
/// and must be a scalar, the first argument specifies a single array
/// element location. It is either an int or a tuple.
/// </param>
public void itemset(params object[] args)
{
var pyargs = ToTuple(args);
var kwargs = new PyDict();
dynamic py = self.InvokeMethod("itemset", pyargs, kwargs);
}
/// <summary>
/// Construct Python bytes containing the raw data bytes in the array.
///
/// Constructs Python bytes showing a copy of the raw contents of
/// data memory. The bytes object can be produced in either ‘C’ or ‘Fortran’,
/// or ‘Any’ order (the default is ‘C’-order). ‘Any’ order means C-order
/// unless the F_CONTIGUOUS flag in the array is set, in which case it
/// means ‘Fortran’ order.
///
/// This function is a compatibility alias for tobytes. Despite its name it returns bytes not strings.
/// </summary>
/// <param name="order">
/// Order of the data for multidimensional arrays:
/// C, Fortran, or the same as for the original array.
/// </param>
/// <returns>
/// Python bytes exhibiting a copy of a’s raw data.
/// </returns>
public byte[] tostring(string order = null)
{
return tobytes();
}
/// <summary>
/// Construct Python bytes containing the raw data bytes in the array.
///
/// Constructs Python bytes showing a copy of the raw contents of
/// data memory. The bytes object can be produced in either ‘C’ or ‘Fortran’,
/// or ‘Any’ order (the default is ‘C’-order). ‘Any’ order means C-order
/// unless the F_CONTIGUOUS flag in the array is set, in which case it
/// means ‘Fortran’ order.
/// </summary>
/// <param name="order">
/// Order of the data for multidimensional arrays:
/// C, Fortran, or the same as for the original array.
/// </param>
/// <returns>
/// Python bytes exhibiting a copy of a’s raw data.
/// </returns>
public byte[] tobytes(string order = null)
{
throw new NotImplementedException("TODO: this needs to be implemented with Marshal.Copy");
var pyargs = ToTuple(new object[]
{
});
var kwargs = new PyDict();
if (order != null) kwargs["order"] = ToPython(order);
dynamic py = self.InvokeMethod("tobytes", pyargs, kwargs);
return ToCsharp<byte[]>(py);
}
/// <summary>
/// New view of array with the same data.
///
/// Notes
///
/// a.view() is used two different ways:
///
/// a.view(some_dtype) or a.view(dtype=some_dtype) constructs a view
/// of the array’s memory with a different data-type. This can cause a
/// reinterpretation of the bytes of memory.
///
/// a.view(ndarray_subclass) or a.view(type=ndarray_subclass) just
/// returns an instance of ndarray_subclass that looks at the same array
/// (same shape, dtype, etc.) This does not cause a reinterpretation of the
/// memory.
///
/// For a.view(some_dtype), if some_dtype has a different number of
/// bytes per entry than the previous dtype (for example, converting a
/// regular array to a structured array), then the behavior of the view
/// cannot be predicted just from the superficial appearance of a (shown
/// by print(a)). It also depends on exactly how a is stored in
/// memory. Therefore if a is C-ordered versus fortran-ordered, versus
/// defined as a slice or transpose, etc., the view may give different
/// results.
/// </summary>
/// <param name="dtype">
/// Data-type descriptor of the returned view, e.g., float32 or int16. The
/// default, None, results in the view having the same data-type as a.
/// This argument can also be specified as an ndarray sub-class, which
/// then specifies the type of the returned object (this is equivalent to
/// setting the type parameter).
/// </param>
/// <param name="type">
/// Type of the returned view, e.g., ndarray or matrix. Again, the
/// default None results in type preservation.
/// </param>
public void view(Dtype dtype = null, Type type = null)
{
throw new NotImplementedException("Get python type 'ndarray' and 'matrix' and substitute them for the given .NET type");
var pyargs = ToTuple(new object[]
{
});
var kwargs = new PyDict();
if (dtype != null) kwargs["dtype"] = ToPython(dtype);
if (type != null) kwargs["type"] = ToPython(type);
dynamic py = self.InvokeMethod("view", pyargs, kwargs);
}
/// <summary>
/// Change shape and size of array in-place.
///
/// Notes
///
/// This reallocates space for the data area if necessary.
///
/// Only contiguous arrays (data elements consecutive in memory) can be
/// resized.
///
/// The purpose of the reference count check is to make sure you
/// do not use this array as a buffer for another Python object and then
/// reallocate the memory. However, reference counts can increase in
/// other ways so if you are sure that you have not shared the memory
/// for this array with another Python object, then you may safely set
/// refcheck to False.
/// </summary>
/// <param name="new_shape">
/// Shape of resized array.
/// </param>
/// <param name="refcheck">
/// If False, reference count will not be checked. Default is True.
/// </param>
public void resize(Shape new_shape, bool? refcheck = null)
{
var pyargs = ToTuple(new object[]
{
new_shape,
});
var kwargs = new PyDict();
if (refcheck != null) kwargs["refcheck"] = ToPython(refcheck);
dynamic py = self.InvokeMethod("resize", pyargs, kwargs);
}
/// <summary>
/// Gives a new shape to an array without changing its data.
///
/// Notes
///
/// It is not always possible to change the shape of an array without
/// copying the data. If you want an error to be raised when the data is copied,
/// you should assign the new shape to the shape attribute of the array:
///
/// The order keyword gives the index ordering both for fetching the values
/// from a, and then placing the values into the output array.
/// For example, let’s say you have an array:
///
/// You can think of reshaping as first raveling the array (using the given
/// index order), then inserting the elements from the raveled array into the
/// new array using the same kind of index ordering as was used for the
/// raveling.
/// </summary>
/// <param name="newshape">
/// The new shape should be compatible with the original shape. If
/// an integer, then the result will be a 1-D array of that length.
/// One shape dimension can be -1. In this case, the value is
/// inferred from the length of the array and remaining dimensions.
/// </param>
/// <returns>
/// This will be a new view object if possible; otherwise, it will
/// be a copy. Note there is no guarantee of the memory layout (C- or
/// Fortran- contiguous) of the returned array.
/// </returns>
public NDarray reshape(params int[] newshape)
{
//auto-generated code, do not change
var @this = this;
return np.reshape(@this, new Shape(newshape));
}
/// <summary>
/// returns the 'array([ .... ])'-representation known from the console
/// </summary>
public string repr => self.InvokeMethod("__repr__").As<string>();
/// <summary>
/// returns the '[ .... ]'-representation
/// </summary>
public string str => self.InvokeMethod("__str__").As<string>();
public NDarray this[string slicing_notation]
{
get
{
var tuple=new PyTuple(Slice.ParseSlices(slicing_notation).Select(s =>
{
if (s.IsIndex)
return new PyInt(s.Start.Value);
else
return s.ToPython();
}).ToArray());
return new NDarray(this.PyObject[tuple]);
}
set
{
var tuple = new PyTuple(Slice.ParseSlices(slicing_notation).Select(s =>
{
if (s.IsIndex)
return new PyInt(s.Start.Value);
else
return s.ToPython();
}).ToArray());
self.SetItem(tuple, ToPython(value));
}
}
public NDarray this[params int[] coords]
{
get
{
var tuple = ToTuple(coords);
return new NDarray(this.PyObject[tuple]);
}
set
{
var tuple = ToTuple(coords);
self.SetItem(tuple, ToPython(value));
}
}
public NDarray this[params NDarray[] indices]
{
get
{
var tuple = new PyTuple(indices.Select(a => (PyObject)a.PyObject).ToArray());
return new NDarray(this.PyObject[tuple]);
}
set
{
var tuple = new PyTuple(indices.Select(a => (PyObject)a.PyObject).ToArray());
self.SetItem(tuple, ToPython(value));
}
}
public NDarray this[params object[] arrays_slices_or_indices]
{
get
{
var pyobjs = arrays_slices_or_indices.Select<object ,PyObject>(x =>
{
switch (x)
{
case int i: return new PyInt(i);
case NDarray a: return a.PyObject;
case string s: return new Slice(s).ToPython();
default: return ToPython(x);
}
}).ToArray();
var tuple = new PyTuple(pyobjs);
return new NDarray(this.PyObject[tuple]);
}
set
{
var pyobjs = arrays_slices_or_indices.Select<object, PyObject>(x =>
{
switch (x)
{
case int i: return new PyInt(i);
case NDarray a: return a.PyObject;
case string s: return new Slice(s).ToPython();
default: return ToPython(x);
}
}).ToArray();
var tuple = new PyTuple(pyobjs);
self.SetItem(tuple, ToPython(value));
}
}
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <returns>
/// Scalar representation of a. The output data type is the same type
/// returned by the input’s item method.
/// </returns>
public T asscalar<T>()
{
return np.asscalar<T>(this);
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
var array = obj as NDarray;
if (!object.ReferenceEquals(array, null))
return np.array_equal(this, array);
return base.Equals(obj);
}
public NDarray real
{
get
{
dynamic py = self.GetAttr("real");
return ToCsharp<NDarray>(py);
}
set
{
self.SetAttr("real", value.PyObject);
}
}
public NDarray imag
{
get
{
dynamic py = self.GetAttr("imag");
return ToCsharp<NDarray>(py);
}
set
{
self.SetAttr("imag", value.PyObject);
}
}
/// <summary>
/// Returns a view of the array with axes transposed.<br></br>
///
/// For a 1-D array, this has no effect.<br></br>
/// (To change between column and
/// row vectors, first cast the 1-D array into a matrix object.)
/// For a 2-D array, this is the usual matrix transpose.<br></br>
///
/// For an n-D array, if axes are given, their order indicates how the
/// axes are permuted (see Examples).<br></br>
/// If axes are not provided and
/// a.shape = (i[0], i[1], ... i[n-2], i[n-1]), then
/// a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0]).
/// </summary>
/// <returns>
/// View of a, with axes suitably permuted.
/// </returns>
public NDarray transpose(params int[] axes)
{
//auto-generated code, do not change
var __self__ = self;
var pyargs = ToTuple(new object[]
{
ToPython(axes),
});
//if (axes != null) kwargs["axes"] = ToPython(axes);
dynamic py = __self__.InvokeMethod("transpose", pyargs);
return ToCsharp<NDarray>(py);
}
}
public class NDarray<T> : NDarray
{
public NDarray(NDarray t) : base(t)
{
}
public NDarray(PyObject pyobject) : base(pyobject)
{
}
public NDarray(T[] array) : base()
{
var nd = np.array(array);
self = nd.self;
}
/// <summary>
/// Returns a copy of the array data
/// </summary>
public T[] GetData()
{
return base.GetData<T>();
}
public new NDarray<T> this[string slicing_notation]
{
get
{
var tuple = new PyTuple(Slice.ParseSlices(slicing_notation).Select(s =>
{
if (s.IsIndex)
return new PyInt(s.Start.Value);
else
return s.ToPython();
}).ToArray());
return new NDarray<T>(this.PyObject[tuple]);
}
set
{
var tuple = new PyTuple(Slice.ParseSlices(slicing_notation).Select(s =>
{
if (s.IsIndex)
return new PyInt(s.Start.Value);
else
return s.ToPython();
}).ToArray());
self.SetItem(tuple, ToPython(value));
}
}
public new NDarray this[params int[] coords]
{
get
{
var tuple = ToTuple(coords);
return new NDarray<T>(this.PyObject[tuple]);
}
set
{
var tuple = ToTuple(coords);
self.SetItem(tuple, ToPython(value));
}
}
public new NDarray this[params NDarray[] indices]
{
get
{
var tuple = new PyTuple(indices.Select(a => (PyObject)a.PyObject).ToArray());
return new NDarray<T>(this.PyObject[tuple]);
}
set
{
var tuple = new PyTuple(indices.Select(a => (PyObject)a.PyObject).ToArray());
self.SetItem(tuple, ToPython(value));
}
}
public new NDarray this[params object[] arrays_slices_or_indices]
{
get
{
var pyobjs = arrays_slices_or_indices.Select<object, PyObject>(x =>
{
switch (x)
{
case int i: return new PyInt(i);
case NDarray a: return a.PyObject;
case string s: return new Slice(s).ToPython();
default: return ToPython(x);
}
}).ToArray();
var tuple = new PyTuple(pyobjs);
return new NDarray(this.PyObject[tuple]);
}
set
{
var pyobjs = arrays_slices_or_indices.Select<object, PyObject>(x =>
{
switch (x)
{
case int i: return new PyInt(i);
case NDarray a: return a.PyObject;
case string s: return new Slice(s).ToPython();
default: return ToPython(x);
}
}).ToArray();
var tuple = new PyTuple(pyobjs);
self.SetItem(tuple, ToPython(value));
}
}
}
}