forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArray.cs
More file actions
1042 lines (924 loc) · 45.4 KB
/
NDArray.cs
File metadata and controls
1042 lines (924 loc) · 45.4 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
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
/*
* NumSharp
* Copyright (C) 2018 Haiping Chen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Apache License 2.0 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Apache License 2.0
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0/>.
*/
//TODO! Complete all TODOs return(?:.*;|;)[\n\r]{1,2}[\t\s]+//
using System;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using NumSharp.Backends;
using NumSharp.Backends.Unmanaged;
using NumSharp.Generic;
using NumSharp.Utilities;
namespace NumSharp
{
/// <summary>
/// An array object represents a multidimensional, homogeneous array of fixed-size items.<br></br>
/// An associated data-type object describes the format of each element in the array (its byte-order,<br></br>
/// how many bytes it occupies in memory, whether it is an integer, a floating point number, or something else, etc.)
/// </summary>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html</remarks>
[DebuggerTypeProxy(nameof(NDArrayDebuggerProxy))]
[SuppressMessage("ReSharper", "ParameterHidesMember")]
public partial class NDArray : IIndex, ICloneable, IEnumerable
{
protected TensorEngine tensorEngine;
/// <summary>
/// Gets the array owning the memory, or <c>null</c> if this array owns its data.
/// </summary>
/// <value>
/// An <see cref="NDArray"/> wrapping the base storage for views, or <c>null</c> for arrays
/// that own their data (e.g., created via <c>np.arange</c>, <c>np.zeros</c>, or <c>copy()</c>).
/// </value>
/// <remarks>
/// <para>
/// <b>NumPy Compatibility:</b> This property mirrors NumPy's <c>ndarray.base</c> attribute.
/// All views chain to the ultimate owner (not intermediate views).
/// </para>
/// <para>
/// <b>Example:</b>
/// <code>
/// var a = np.arange(10); // a.@base == null (owns data)
/// var b = a["2:5"]; // b.@base.Storage == a.Storage (view)
/// var c = b["1:2"]; // c.@base.Storage == a.Storage (chains to original!)
/// var d = a.copy(); // d.@base == null (copy owns data)
/// var e = a.reshape(2, 5); // e.@base.Storage == a.Storage (view)
/// </code>
/// </para>
/// <para>
/// <b>View Detection:</b> Use <c>arr.@base != null</c> or <c>arr.Storage.IsView</c> to
/// detect if an array is a view. Note that <c>arr.@base != null</c> may trigger NDArray's
/// operator overloading for element-wise comparison. Prefer <c>arr.Storage.IsView</c> for
/// simple boolean checks.
/// </para>
/// <para>
/// <b>Semantic Difference from NumPy:</b> In NumPy, <c>c.base is a</c> returns <c>True</c>
/// (object identity). In NumSharp, <c>c.@base</c> creates a new wrapper each call, so
/// <c>ReferenceEquals(c.@base, a)</c> is <c>false</c>. However, the underlying storage
/// is the same: <c>c.@base.Storage == a.Storage</c> is <c>true</c>.
/// </para>
/// <para>
/// <b>Memory Safety:</b> The underlying memory is kept alive by the shared Disposer
/// in the MemoryBlock, not by this property. Views remain valid even if the original
/// array reference is garbage collected.
/// </para>
/// </remarks>
/// <seealso href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.base.html"/>
/// <seealso cref="UnmanagedStorage.BaseStorage"/>
/// <seealso cref="UnmanagedStorage.IsView"/>
public NDArray? @base => Storage._baseStorage is { } bs ? new NDArray(bs) : null;
#region Constructors
/// <summary>
/// Creates a new <see cref="NDArray"/> with this storage.
/// </summary>
/// <param name="storage"></param>
public NDArray(UnmanagedStorage storage)
{
Storage = storage;
tensorEngine = storage.Engine;
}
/// <summary>
/// Creates a new <see cref="NDArray"/> with this storage.
/// </summary>
/// <param name="shape">The shape to set for this NDArray, does not perform checks.</param>
/// <remarks>Doesn't copy. Does not perform checks for <paramref name="shape"/>.</remarks>
protected internal NDArray(UnmanagedStorage storage, Shape shape)
{
Storage = storage.Alias(ref shape);
tensorEngine = storage.Engine;
}
/// <summary>
/// Creates a new <see cref="NDArray"/> with this storage.
/// </summary>
/// <param name="shape">The shape to set for this NDArray, does not perform checks.</param>
/// <remarks>Doesn't copy. Does not perform checks for <paramref name="shape"/>.</remarks>
protected internal NDArray(UnmanagedStorage storage, ref Shape shape)
{
Storage = storage.Alias(ref shape);
tensorEngine = storage.Engine;
}
/// <summary>
/// Constructor for init data type
/// internal storage is 1D with 1 element
/// </summary>
/// <param name="dtype">Data type of elements</param>
/// <param name="engine">The engine of this <see cref="NDArray"/></param>
/// <remarks>This constructor does not call allocation/></remarks>
protected internal NDArray(Type dtype, TensorEngine engine)
{
tensorEngine = engine;
Storage = TensorEngine.GetStorage(dtype);
}
/// <summary>
/// Constructor for init data type
/// internal storage is 1D with 1 element
/// </summary>
/// <param name="typeCode">Data type of elements</param>
/// <param name="engine">The engine of this <see cref="NDArray"/></param>
/// <remarks>This constructor does not call allocation/></remarks>
protected internal NDArray(NPTypeCode typeCode, TensorEngine engine)
{
tensorEngine = engine;
Storage = TensorEngine.GetStorage(typeCode);
}
/// <summary>
/// Constructor for init data type
/// internal storage is 1D with 1 element
/// </summary>
/// <param name="dtype">Data type of elements</param>
/// <remarks>This constructor does not call allocation/></remarks>
public NDArray(Type dtype) : this(dtype, BackendFactory.GetEngine()) { }
/// <summary>
/// Constructor for init data type
/// internal storage is 1D with 1 element
/// </summary>
/// <param name="typeCode">Data type of elements</param>
/// <remarks>This constructor does not call allocation/></remarks>
public NDArray(NPTypeCode typeCode) : this(typeCode, BackendFactory.GetEngine()) { }
/// <summary>
/// Constructor which takes .NET array
/// dtype and shape is determined from array
/// </summary>
/// <param name="values"></param>
/// <param name="shape"></param>
/// <param name="order"></param>
/// <returns>Array with values</returns>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(Array values, Shape shape = default, char order = 'C') : this(values.GetType().GetElementType())
{
// Note: F-order not supported, order parameter is accepted but ignored (C-order only)
if (shape.IsEmpty)
shape = Shape.ExtractShape(values);
Storage.Allocate(values.ResolveRank() != 1 ? ArraySlice.FromArray(Arrays.Flatten(values), false) : ArraySlice.FromArray(values, false), shape);
}
/// <summary>
/// Constructor which takes .NET array
/// dtype and shape is determined from array
/// </summary>
/// <param name="values"></param>
/// <param name="shape"></param>
/// <param name="order"></param>
/// <returns>Array with values</returns>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(IArraySlice values, Shape shape = default, char order = 'C') : this(values.TypeCode)
{
// Note: F-order not supported, order parameter is accepted but ignored (C-order only)
if (shape.IsEmpty)
shape = Shape.Vector((int) values.Count); //TODO! when long index, remove cast int
Storage.Allocate(values, shape);
}
/// <summary>
/// Constructor which initialize elements with 0
/// type and shape are given.
/// </summary>
/// <param name="dtype">internal data type</param>
/// <param name="shape">Shape of NDArray</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(Type dtype, Shape shape) : this(dtype, shape, true) { }
/// <summary>
/// Constructor which initialize elements with length of <paramref name="size"/>
/// </summary>
/// <param name="dtype">Internal data type</param>
/// <param name="size">The size as a single dimension shape</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(Type dtype, int size) : this(dtype, Shape.Vector(size), true) { }
/// <summary>
/// Constructor which initialize elements with length of <paramref name="size"/>
/// </summary>
/// <param name="dtype">Internal data type</param>
/// <param name="size">The size as a single dimension shape</param>
/// <param name="fillZeros">Should set the values of the new allocation to default(dtype)? otherwise - old memory noise</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(Type dtype, int size, bool fillZeros) : this(dtype, Shape.Vector(size), fillZeros) { }
/// <summary>
/// Constructor which initialize elements with 0
/// type and shape are given.
/// </summary>
/// <param name="dtype">internal data type</param>
/// <param name="shape">Shape of NDArray</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(NPTypeCode dtype, Shape shape) : this(dtype, shape, true) { }
/// <summary>
/// Constructor which initialize elements with length of <paramref name="size"/>
/// </summary>
/// <param name="dtype">Internal data type</param>
/// <param name="size">The size as a single dimension shape</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(NPTypeCode dtype, int size) : this(dtype, Shape.Vector(size), true) { }
/// <summary>
/// Constructor which initialize elements with length of <paramref name="size"/>
/// </summary>
/// <param name="dtype">Internal data type</param>
/// <param name="size">The size as a single dimension shape</param>
/// <param name="fillZeros">Should set the values of the new allocation to default(dtype)? otherwise - old memory noise</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(NPTypeCode dtype, int size, bool fillZeros) : this(dtype, Shape.Vector(size), true) { }
/// <summary>
/// Constructor which initialize elements with 0
/// type and shape are given.
/// </summary>
/// <param name="dtype">internal data type</param>
/// <param name="shape">Shape of NDArray</param>
/// <param name="fillZeros">Should set the values of the new allocation to default(dtype)? otherwise - old memory noise</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(Type dtype, Shape shape, bool fillZeros) : this(dtype)
{
Storage.Allocate(shape, dtype, fillZeros);
}
/// <summary>
/// Constructor which initialize elements with 0
/// type and shape are given.
/// </summary>
/// <param name="dtype">internal data type</param>
/// <param name="shape">Shape of NDArray</param>
/// <param name="fillZeros">Should set the values of the new allocation to default(dtype)? otherwise - old memory noise</param>
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
public NDArray(NPTypeCode dtype, Shape shape, bool fillZeros) : this(dtype)
{
Storage.Allocate(shape, dtype, fillZeros);
}
private NDArray(IArraySlice array, Shape shape) : this(array.TypeCode)
{
Storage.Allocate(array, shape);
}
#endregion
/// <summary>
/// The dtype of this array.
/// </summary>
public Type dtype => Storage.DType;
/// <summary>
/// The <see cref="NPTypeCode"/> of this array.
/// </summary>
public NPTypeCode typecode => Storage.TypeCode;
/// <summary>
/// Gets the precomputed <see cref="NPTypeCode"/> of <see cref="dtype"/>.
/// </summary>
internal NPTypeCode GetTypeCode
{
[MethodImpl(Inline)]
get => Storage.TypeCode;
}
/// <summary>
/// Gets the address that this NDArray starts from.
/// </summary>
protected internal unsafe void* Address
{
[MethodImpl(Inline)]
get => Storage.Address;
}
/// <summary>
/// Data length of every dimension
/// </summary>
public int[] shape
{
get => Storage.Shape.Dimensions;
set => Storage.Reshape(value);
}
/// <summary>
/// Dimension count
/// </summary>
public int ndim => Storage.Shape.NDim;
/// <summary>
/// Total of elements
/// </summary>
public int size => Storage.Shape.Size;
public int dtypesize => Storage.DTypeSize;
public char order => Storage.Shape.Order;
public int[] strides => Storage.Shape.Strides;
/// <summary>
/// A 1-D iterator over the array.
/// </summary>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flat.html</remarks>
public NDArray flat
{
get
{
if (ndim == 1 || Shape.IsScalar) //because it is already flat, there is no need to clone even if it is already sliced.
return new NDArray(Storage);
return this.reshape(size);
}
}
/// <summary>
/// The transposed array. <br></br>
/// Same as self.transpose().
/// </summary>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ndarray.T.html</remarks>
public NDArray T
{
get
{
return transpose();
}
}
/// <summary>
/// The shape representing this <see cref="NDArray"/>.
/// </summary>
public Shape Shape
{
[MethodImpl(Inline)]
get => Storage.Shape;
[MethodImpl(Inline)]
set => Storage.Reshape(value);
}
/// <summary>
/// The internal storage that stores data for this <see cref="NDArray"/>.
/// </summary>
protected internal UnmanagedStorage Storage;
/// <summary>
/// The tensor engine that handles this <see cref="NDArray"/>.
/// </summary>
public TensorEngine TensorEngine
{
[DebuggerStepThrough] get => tensorEngine ?? Storage.Engine ?? BackendFactory.GetEngine();
set => tensorEngine = (value ?? Storage.Engine ?? BackendFactory.GetEngine());
}
/// <summary>
/// Shortcut for access internal elements
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public ArraySlice<T> Data<T>() where T : unmanaged
{
return Storage.GetData<T>();
}
/// <summary>
/// Get: Gets internal storage array by calling <see cref="IStorage.GetData"/><br></br>
/// Set: Replace internal storage by calling <see cref="IStorage.ReplaceData(System.Array)"/>
/// </summary>
/// <remarks>Setting does not replace internal storage array.</remarks>
protected internal IArraySlice Array
{
[MethodImpl(Inline)]
get => Storage.InternalArray;
}
public ArraySlice<T> CloneData<T>() where T : unmanaged => Storage.CloneData<T>();
public IArraySlice CloneData() => Storage.CloneData();
/// <summary>
/// Copy of the array, cast to a specified type.
/// </summary>
/// <param name="dtype">The dtype to cast this array.</param>
/// <param name="copy">By default, astype always returns a newly allocated array. If this is set to false, the input internal array is replaced instead of returning a new NDArray with the casted data.</param>
/// <returns>An <see cref="NDArray"/> of given <paramref name="dtype"/>.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html</remarks>
[SuppressMessage("ReSharper", "ParameterHidesMember")]
public NDArray astype(Type dtype, bool copy = true) => TensorEngine.Cast(this, dtype, copy);
/// <summary>
/// Copy of the array, cast to a specified type.
/// </summary>
/// <param name="dtype">The dtype to cast this array.</param>
/// <param name="copy">By default, astype always returns a newly allocated array. If this is set to false, the input internal array is replaced instead of returning a new NDArray with the casted data.</param>
/// <returns>An <see cref="NDArray"/> of given <paramref name="dtype"/>.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html</remarks>
public NDArray astype(NPTypeCode typeCode, bool copy = true) => TensorEngine.Cast(this, typeCode, copy);
/// <summary>
/// Clone the whole NDArray
/// internal storage is also cloned into 2nd memory area
/// </summary>
/// <returns>Cloned NDArray</returns>
object ICloneable.Clone() => Clone();
/// <summary>
/// Clone the whole NDArray
/// internal storage is also cloned into 2nd memory area
/// </summary>
/// <returns>Cloned NDArray</returns>
public NDArray Clone() => new NDArray(this.Storage.Clone()) {tensorEngine = TensorEngine};
public IEnumerator GetEnumerator()
{
if (Array == null || Shape.IsEmpty || Shape.size == 0)
return _empty().GetEnumerator();
#if _REGEN
#region Compute
switch (GetTypeCode)
{
%foreach supported_dtypes,supported_dtypes_lowercase%
case NPTypeCode.#1: return new NDIterator<#2>(this, false).GetEnumerator();
%
default:
throw new NotSupportedException();
}
#endregion
#else
#region Compute
switch (GetTypeCode)
{
case NPTypeCode.Boolean: return new NDIterator<bool>(this, false).GetEnumerator();
case NPTypeCode.Byte: return new NDIterator<byte>(this, false).GetEnumerator();
case NPTypeCode.Int16: return new NDIterator<short>(this, false).GetEnumerator();
case NPTypeCode.UInt16: return new NDIterator<ushort>(this, false).GetEnumerator();
case NPTypeCode.Int32: return new NDIterator<int>(this, false).GetEnumerator();
case NPTypeCode.UInt32: return new NDIterator<uint>(this, false).GetEnumerator();
case NPTypeCode.Int64: return new NDIterator<long>(this, false).GetEnumerator();
case NPTypeCode.UInt64: return new NDIterator<ulong>(this, false).GetEnumerator();
case NPTypeCode.Char: return new NDIterator<char>(this, false).GetEnumerator();
case NPTypeCode.Double: return new NDIterator<double>(this, false).GetEnumerator();
case NPTypeCode.Single: return new NDIterator<float>(this, false).GetEnumerator();
case NPTypeCode.Decimal: return new NDIterator<decimal>(this, false).GetEnumerator();
default:
throw new NotSupportedException();
}
#endregion
#endif
IEnumerable _empty()
{
yield break;
}
}
/// <summary>
/// New view of array with the same data.
/// </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>
/// <returns></returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html</remarks>
public NDArray view(Type dtype = null)
{
//TODO! this shouldnt be a cast in case dtype != null, it should be an unsafe reinterpret (see remarks).
if (dtype == null || dtype == this.dtype)
{
return new NDArray(Storage.Alias());
}
// Cast creates a copy, not a view - no base needed
return new NDArray(Storage.Cast(dtype));
}
/// <summary>
/// New view of array with the same data.
/// </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>
/// <returns></returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html</remarks>
public NDArray<T> view<T>() where T : unmanaged
=> view(typeof(T)).AsGeneric<T>();
#region Getters
/// <summary>
/// Get all NDArray slices at that specific dimension.
/// </summary>
/// <param name="axis">Zero-based dimension index on which axis and forward of it to select data., e.g. dimensions=1, shape is (2,2,3,3), returned shape = 4 times of (3,3)</param>
/// <remarks>Does not perform copy.</remarks>
/// <example>
/// <code>
/// var nd = np.arange(27).reshape(3,1,3,3);<br></br>
/// var ret = nd.GetNDArrays(1);<br></br>
/// Assert.IsTrue(ret.All(n=>n.Shape == new Shape(3,3));<br></br>
/// Assert.IsTrue(ret.Length == 3);<br></br><br></br>
/// var nd = np.arange(27).reshape(3,1,3,3);<br></br>
///
/// var ret = nd.GetNDArrays(0);<br></br>
/// Assert.IsTrue(ret.All(n=>n.Shape == new Shape(1,3,3));<br></br>
/// Assert.IsTrue(ret.Length == 3);<br></br>
/// </code>
/// </example>
[SuppressMessage("ReSharper", "LoopCanBeConvertedToQuery")]
public NDArray[] GetNDArrays(int axis = 0)
{
axis += 1; //axis is 0-based, we need 1 based (aka count)
if (axis <= 0 || axis > Shape.dimensions.Length)
throw new ArgumentOutOfRangeException(nameof(axis));
//get all the dimensions involved till the axis
var dims = Storage.Shape.dimensions;
int[] selectDimensions = new int[axis];
for (int i = 0; i < axis; i++)
selectDimensions[i] = dims[i];
//compute len
int len = 1;
foreach (var i in selectDimensions)
len = len * i;
var ret = new NDArray[len];
var iter = new ValueCoordinatesIncrementor(selectDimensions);
var index = iter.Index; //heap the pointer to that array.
for (int i = 0; i < ret.Length; i++)
{
ret[i] = new NDArray(Storage.GetData(index));
iter.Next();
}
return ret;
}
/// <summary>
/// Gets the internal storage and converts it to <typeparamref name="T"/> if necessary.
/// </summary>
/// <typeparam name="T">The returned type.</typeparam>
/// <returns>An array of type <typeparamref name="T"/></returns>
public ArraySlice<T> GetData<T>() where T : unmanaged => Storage.GetData<T>();
/// <summary>
/// Get reference to internal data storage
/// </summary>
/// <returns>reference to internal storage as System.Array</returns>
public IArraySlice GetData() => Storage.GetData();
/// <summary>
/// Gets a NDArray at given <paramref name="indices"/>.
/// </summary>
/// <param name="indices">The coordinates to the wanted value</param>
/// <remarks>Does not copy, returns a memory slice - this is similar to this[int[]]</remarks>
public NDArray GetData(params int[] indices) => new NDArray(Storage.GetData(indices)) {tensorEngine = this.tensorEngine};
/// <summary>
/// Retrieves value of type <see cref="bool"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="bool"/></exception>
[MethodImpl(Inline)]
public bool GetBoolean(params int[] indices) => Storage.GetBoolean(indices);
/// <summary>
/// Retrieves value of type <see cref="byte"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="byte"/></exception>
[MethodImpl(Inline)]
public byte GetByte(params int[] indices) => Storage.GetByte(indices);
/// <summary>
/// Retrieves value of type <see cref="char"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="char"/></exception>
[MethodImpl(Inline)]
public char GetChar(params int[] indices) => Storage.GetChar(indices);
/// <summary>
/// Retrieves value of type <see cref="decimal"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="decimal"/></exception>
[MethodImpl(Inline)]
public decimal GetDecimal(params int[] indices) => Storage.GetDecimal(indices);
/// <summary>
/// Retrieves value of type <see cref="double"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="double"/></exception>
[MethodImpl(Inline)]
public double GetDouble(params int[] indices) => Storage.GetDouble(indices);
/// <summary>
/// Retrieves value of type <see cref="short"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="short"/></exception>
[MethodImpl(Inline)]
public short GetInt16(params int[] indices) => Storage.GetInt16(indices);
/// <summary>
/// Retrieves value of type <see cref="int"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="int"/></exception>
[MethodImpl(Inline)]
public int GetInt32(params int[] indices) => Storage.GetInt32(indices);
/// <summary>
/// Retrieves value of type <see cref="long"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="long"/></exception>
[MethodImpl(Inline)]
public long GetInt64(params int[] indices) => Storage.GetInt64(indices);
/// <summary>
/// Retrieves value of type <see cref="float"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="float"/></exception>
[MethodImpl(Inline)]
public float GetSingle(params int[] indices) => Storage.GetSingle(indices);
/// <summary>
/// Retrieves value of type <see cref="ushort"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="ushort"/></exception>
[MethodImpl(Inline)]
public ushort GetUInt16(params int[] indices) => Storage.GetUInt16(indices);
/// <summary>
/// Retrieves value of type <see cref="uint"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="uint"/></exception>
[MethodImpl(Inline)]
public uint GetUInt32(params int[] indices) => Storage.GetUInt32(indices);
/// <summary>
/// Retrieves value of type <see cref="ulong"/>.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="ulong"/></exception>
[MethodImpl(Inline)]
public ulong GetUInt64(params int[] indices) => Storage.GetUInt64(indices);
/// <summary>
/// Retrieves value of unspecified type (will figure using <see cref="DType"/>).
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="object"/></exception>
[MethodImpl(Inline)]
public ValueType GetValue(params int[] indices) => Storage.GetValue(indices);
/// <summary>
/// Retrieves value of unspecified type (will figure using <see cref="DType"/>).
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="object"/></exception>
[MethodImpl(Inline)]
public T GetValue<T>(params int[] indices) where T : unmanaged => Storage.GetValue<T>(indices);
/// <summary>
/// Retrieves value of
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[MethodImpl(Inline)]
public ValueType GetAtIndex(int index) => Storage.GetAtIndex(index);
/// <summary>
/// Retrieves value of
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[MethodImpl(Inline)]
public T GetAtIndex<T>(int index) where T : unmanaged => Storage.GetAtIndex<T>(index);
#endregion
#region Setters
/// <summary>
/// Set a <see cref="IArraySlice"/> at given <see cref="indices"/>.
/// </summary>
/// <param name="value">The value to set</param>
/// <param name="indices">The </param>
/// <remarks>
/// Does not change internal storage data type.<br></br>
/// If <paramref name="value"/> does not match <see cref="DType"/>, <paramref name="value"/> will be converted.
/// </remarks>
public void SetData(IArraySlice value, params int[] indices)
{
Storage.SetData(value, indices);
}
/// <summary>
/// Set a <see cref="NDArray"/> at given <see cref="indices"/>.
/// </summary>
/// <param name="value">The value to set</param>
/// <param name="indices">The </param>
/// <remarks>
/// Does not change internal storage data type.<br></br>
/// If <paramref name="value"/> does not match <see cref="DType"/>, <paramref name="value"/> will be converted.
/// </remarks>
public void SetData(NDArray value, params int[] indices)
{
Storage.SetData(value, indices);
}
/// <summary>
/// Set a <see cref="NDArray"/>, <see cref="IArraySlice"/>, <see cref="Array"/> or a scalar value at given <see cref="indices"/>.
/// </summary>
/// <param name="value">The value to set</param>
/// <param name="indices">The </param>
/// <remarks>
/// Does not change internal storage data type.<br></br>
/// If <paramref name="value"/> does not match <see cref="DType"/>, <paramref name="value"/> will be converted.
/// </remarks>
public void SetData(object value, params int[] indices)
{
Storage.SetData(value, indices);
}
/// <summary>
/// Set a single value at given <see cref="indices"/>.
/// </summary>
/// <param name="value">The value to set</param>
/// <param name="indices">The </param>
/// <remarks>
/// Does not change internal storage data type.<br></br>
/// If <paramref name="value"/> does not match <see cref="DType"/>, <paramref name="value"/> will be converted.
/// </remarks>
public void SetValue(ValueType value, params int[] indices)
{
Storage.SetValue(value, indices);
}
/// <summary>
/// Set a single value at given <see cref="indices"/>.
/// </summary>
/// <param name="value">The value to set</param>
/// <param name="indices">The </param>
/// <remarks>
/// Does not change internal storage data type.<br></br>
/// If <paramref name="value"/> does not match <see cref="DType"/>, <paramref name="value"/> will be converted.
/// </remarks>
public void SetValue(object value, params int[] indices)
{
Storage.SetValue(value, indices);
}
/// <summary>
/// Set a single value at given <see cref="indices"/>.
/// </summary>
/// <param name="value">The value to set</param>
/// <param name="indices">The </param>
/// <remarks>
/// Does not change internal storage data type.<br></br>
/// If <paramref name="value"/> does not match <see cref="DType"/>, <paramref name="value"/> will be converted.
/// </remarks>
public void SetValue<T>(T value, params int[] indices) where T : unmanaged
{
Storage.SetValue<T>(value, indices);
}
/// <summary>
/// Sets <see cref="values"/> as the internal data storage and changes the internal storage data type to <see cref="dtype"/> and casts <see cref="values"/> if necessary.
/// </summary>
/// <param name="values">The values to set as internal data soruce</param>
/// <param name="dtype">The type to change this storage to and the type to cast <see cref="values"/> if necessary.</param>
/// <remarks>Does not copy values unless cast is necessary.</remarks>
// ReSharper disable once ParameterHidesMember
public void ReplaceData(Array values, Type dtype)
{
Storage.ReplaceData(values, dtype);
}
/// <summary>
/// Sets <see cref="values"/> as the internal data storage and changes the internal storage data type to <see cref="values"/> type.
/// </summary>
/// <param name="values"></param>
/// <remarks>Does not copy values.</remarks>
public void ReplaceData(Array values)
{
Storage.ReplaceData(values);
}
/// <summary>
/// Sets <see cref="nd"/> as the internal data storage and changes the internal storage data type to <see cref="nd"/> type.
/// </summary>
/// <param name="nd"></param>
/// <remarks>Does not copy values and does change shape and dtype.</remarks>
public void ReplaceData(NDArray nd)
{
Storage.ReplaceData(nd);
}
/// <summary>
/// Set an Array to internal storage, cast it to new dtype and if necessary change dtype
/// </summary>
/// <param name="values"></param>
/// <param name="typeCode"></param>
/// <remarks>Does not copy values unless cast is necessary and doesn't change shape.</remarks>
public void ReplaceData(Array values, NPTypeCode typeCode)
{
Storage.ReplaceData(values, typeCode);
}
/// <summary>
/// Sets <see cref="values"/> as the internal data source and changes the internal storage data type to <see cref="values"/> type.
/// </summary>
/// <param name="values"></param>
/// <param name="dtype"></param>
/// <remarks>Does not copy values and doesn't change shape.</remarks>
public void ReplaceData(IArraySlice values, Type dtype)
{
Storage.ReplaceData(values, dtype);
}
/// <summary>
/// Sets <see cref="values"/> as the internal data source and changes the internal storage data type to <see cref="values"/> type.
/// </summary>
/// <param name="values"></param>
/// <remarks>Does not copy values and doesn't change shape.</remarks>
public void ReplaceData(IArraySlice values)
{
Storage.ReplaceData(values);
}
/// <summary>
/// Retrieves value at given linear (offset) <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[MethodImpl(Inline)]
public void SetAtIndex(object obj, int index) => Storage.SetAtIndex(obj, index);
/// <summary>
/// Retrieves value of
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[MethodImpl(Inline)]
public void SetAtIndex<T>(T value, int index) where T : unmanaged => Storage.SetAtIndex(value, index);
#if _REGEN
%foreach supported_dtypes,supported_dtypes_lowercase%
/// <summary>
/// Sets a #2 at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void Set#1(#2 value, params int[] indices) => Storage.Set#1(value, indices);
%
#else
/// <summary>
/// Sets a bool at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetBoolean(bool value, params int[] indices) => Storage.SetBoolean(value, indices);
/// <summary>
/// Sets a byte at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetByte(byte value, params int[] indices) => Storage.SetByte(value, indices);
/// <summary>
/// Sets a short at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetInt16(short value, params int[] indices) => Storage.SetInt16(value, indices);
/// <summary>
/// Sets a ushort at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetUInt16(ushort value, params int[] indices) => Storage.SetUInt16(value, indices);
/// <summary>
/// Sets a int at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetInt32(int value, params int[] indices) => Storage.SetInt32(value, indices);
/// <summary>
/// Sets a uint at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetUInt32(uint value, params int[] indices) => Storage.SetUInt32(value, indices);
/// <summary>
/// Sets a long at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetInt64(long value, params int[] indices) => Storage.SetInt64(value, indices);
/// <summary>
/// Sets a ulong at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetUInt64(ulong value, params int[] indices) => Storage.SetUInt64(value, indices);
/// <summary>
/// Sets a char at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>
[MethodImpl(Inline)]
public void SetChar(char value, params int[] indices) => Storage.SetChar(value, indices);
/// <summary>
/// Sets a double at specific coordinates.
/// </summary>
/// <param name="value">The values to assign</param>
/// <param name="indices">The coordinates to set <paramref name="value"/> at.</param>