Repository navigation
Expand file tree
/
Copy pathnp.array.cs
More file actions
777 lines (699 loc) · 41.9 KB
/
Copy pathnp.array.cs
File metadata and controls
777 lines (699 loc) · 41.9 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using NumSharp.Backends;
using NumSharp.Backends.Unmanaged;
using NumSharp.Utilities;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Create an array from an existing <see cref="NDArray"/>. Matches NumPy's default
/// <c>np.array(a)</c> which always copies (NumPy 2.x: <c>copy=True</c> by default).
/// </summary>
/// <param name="nd">Source array.</param>
/// <param name="copy">When <c>true</c> (default) the source storage is cloned; when <c>false</c> the storage is shared (alias). For "copy only if needed" semantics use <see cref="asarray(NDArray, Type, char, bool?, NDArray, string)"/>.</param>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
[MethodImpl(OptimizeAndInline)]
public static NDArray array(NDArray nd, bool copy = true) =>
copy
? new NDArray(nd.Storage.Clone()) { TensorEngine = nd.TensorEngine }
: new NDArray(nd.Storage) { TensorEngine = nd.TensorEngine };
/// <summary>
/// Create an array from a <see cref="MemoryView"/> (obtained from <see cref="NDArray.data"/>) —
/// the consumer round-trip of <c>ndarray.data</c>. Matches NumPy's <c>np.array(a.data)</c>,
/// which reads the buffer's shape/strides/dtype and (by default) COPIES, preserving the
/// source's N-D shape and layout. Equivalent to <c>np.array(buffer.obj, copy)</c>; a copy is
/// always writeable, even from a read-only (broadcast) source.
/// </summary>
/// <param name="buffer">A <see cref="MemoryView"/> over an array.</param>
/// <param name="copy">When <c>true</c> (default) the source storage is cloned; when <c>false</c> the storage is shared (alias).</param>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
[MethodImpl(OptimizeAndInline)]
public static NDArray array(MemoryView buffer, bool copy = true)
{
if (buffer is null)
throw new ArgumentNullException(nameof(buffer));
return array(buffer.obj, copy);
}
/// <summary>
/// Creates a scalar (0-dimensional) <see cref="NDArray"/> from a single value.
/// </summary>
/// <typeparam name="T">The type of the value, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="scalar">The scalar value.</param>
/// <returns>A 0-dimensional <see cref="NDArray"/> containing the scalar value.</returns>
/// <remarks>
/// NumPy: np.array(42) creates a 0-dimensional array with shape ().
/// https://numpy.org/doc/stable/reference/generated/numpy.array.html
/// </remarks>
[MethodImpl(OptimizeAndInline)]
public static NDArray array<T>(T scalar) where T : unmanaged => NDArray.Scalar(scalar);
/// <summary>
/// Creates an <see cref="NDArray"/> from an array with an unknown size or dtype.
/// </summary>
/// <param name="ndmin">Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement.</param>
/// <param name="copy">Always copies if the array is larger than 1-d.</param>
/// <param name="order">Memory layout: 'C' (row-major, default), 'F' (column-major), 'A'/'K' (resolved from source).</param>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
[MethodImpl(Optimize)]
[SuppressMessage("ReSharper", "InvalidXmlDocComment")]
public static NDArray array(Array array, DType dtype = null, int ndmin = 0, bool copy = true, char order = 'C')
{
if (array == null)
throw new ArgumentNullException(nameof(array));
var arrType = array.ResolveElementType();
//handle dim expansion and extract shape
Shape shape;
var dims = array.ResolveRank();
var missing = dims - ndmin;
if (missing < 0)
{
shape = Arrays.Concat(Enumerable.Repeat(1, Math.Abs(missing)).ToArray(), Shape.ExtractShape(array));
}
else
{
shape = Shape.ExtractShape(array);
}
//flatten
if (shape.NDim > 1)
{
array = Arrays.Flatten(array);
copy = false;
}
if (dtype != null && !dtype.Equals(arrType))
{
array = ArrayConvert.To(array, (Type)dtype);
copy = false;
}
// C-contiguous materialization from the managed array.
var result = new NDArray(copy ? (Array)array.Clone() : array, shape, 'C');
// Honor F-order request: relay out into F-contig layout.
char physical = OrderResolver.Resolve(order, result.Shape);
if (physical == 'F' && result.Shape.NDim > 1 && !result.Shape.IsFContiguous)
result = result.copy('F');
return result;
}
/// <summary>
/// Creates a Vector <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>Always performs a copy.</remarks>
public static NDArray array<T>(params T[] data) where T : unmanaged => new NDArray(ArraySlice.FromArray(data, true), Shape.Vector(data.Length));
/// <summary>
/// Creates a Vector <see cref="NDArray"/> from given <paramref name="data"/> with specified dtype.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="dtype">
/// The desired dtype for the array — one descriptor parameter, like NumPy's <c>dtype</c>: a C# <see cref="Type"/>, an
/// <see cref="NPTypeCode"/>, a NumPy dtype string (<c>"f4"</c>) or a <see cref="DType"/> all convert implicitly.
/// If different from <typeparamref name="T"/>, the data will be cast.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data cast to the specified dtype.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[] data, DType dtype) where T : unmanaged
{
var arr = new NDArray(ArraySlice.FromArray(data, true), Shape.Vector(data.Length));
if (dtype != null && dtype.GetTypeCode() != InfoOf<T>.NPTypeCode)
return arr.astype(dtype);
return arr;
}
/// <summary>
/// Creates a Vector <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The enumeration of data to create <see cref="NDArray"/> from.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>Always performs a copy.</remarks>
public static NDArray array<T>(IEnumerable<T> data) where T : unmanaged
{
var slice = ArraySlice.FromArray(data.ToArray(), false);
return new NDArray(slice, Shape.Vector(slice.Count));
}
/// <summary>
/// Creates a Vector <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The enumeration of data to create <see cref="NDArray"/> from.</param>
/// <param name="size">Maximum number of items to read from <paramref name="data"/>.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>
/// https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>
/// Always performs a copy.<br></br>
/// <paramref name="size"/> can be used to limit the amount of items to read form <paramref name="data"/>. Reading stops on either size or <paramref name="data"/> ends.
/// </remarks>
public static NDArray array<T>(IEnumerable<T> data, int size) where T : unmanaged
=> array(data, (long)size);
/// <summary>
/// Creates a Vector <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The enumeration of data to create <see cref="NDArray"/> from.</param>
/// <param name="size">Maximum number of items to read from <paramref name="data"/>.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>
/// https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>
/// Always performs a copy.<br></br>
/// <paramref name="size"/> can be used to limit the amount of items to read form <paramref name="data"/>. Reading stops on either size or <paramref name="data"/> ends.
/// </remarks>
public static NDArray array<T>(IEnumerable<T> data, long size) where T : unmanaged
{
var slice = new ArraySlice<T>(new UnmanagedMemoryBlock<T>(size));
unsafe
{
using (var enumerator = data.GetEnumerator())
{
Func<bool> next = enumerator.MoveNext;
var addr = slice.Address;
for (long i = 0; i < size && next(); i++)
{
addr[i] = enumerator.Current;
}
}
}
return new NDArray(slice, Shape.Vector(slice.Count));
}
/// <summary>
/// Create a vector <see cref="NDArray"/> of dtype <see cref="char"/>.
/// </summary>
/// <param name="chars"></param>
/// <returns></returns>
[MethodImpl(Inline)]
public static NDArray array(string chars)
{
if (chars == null)
throw new ArgumentNullException(nameof(chars));
if (chars.Length == 0)
return new NDArray(NPTypeCode.Char, 0);
unsafe
{
var ret = new ArraySlice<char>(new UnmanagedMemoryBlock<char>(chars.Length));
fixed (char* strChars = chars)
{
var src = strChars;
var dst = ret.Address;
var len = sizeof(char) * chars.Length;
Buffer.MemoryCopy(src, dst, len, len);
}
return new NDArray(ret);
}
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from. Shape is taken from the first item of each array/nested array.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>Always performs a copy.</remarks>
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
[SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")]
[MethodImpl(Optimize)]
public static NDArray array<T>(T[][] data) where T : unmanaged
{
unsafe
{
int len1 = data.Length;
int len2 = data[0].Length;
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2));
var strides = @out.Shape.Strides;
long stride1 = strides[0];
Debug.Assert(strides[1] == 1);
T* addr = (T*)@out.Address;
for (int i1 = 0; i1 < len1; i1++)
{
T* addr1 = addr + i1 * stride1;
T[] src1 = data[i1];
for (int i2 = 0; i2 < len2; i2++)
{
*(addr1 + i2) = src1[i2];
}
}
return @out;
}
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from. Shape is taken from the first item of each array/nested array.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>Always performs a copy.</remarks>
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
[SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")]
[MethodImpl(Optimize)]
public static NDArray array<T>(T[][][] data) where T : unmanaged
{
unsafe
{
int len1 = data.Length;
int len2 = data[0].Length;
int len3 = data[0][0].Length;
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2, len3));
var strides = @out.Shape.Strides;
long stride1 = strides[0];
long stride2 = strides[1];
Debug.Assert(strides[2] == 1);
T* addr = (T*)@out.Address;
for (int i1 = 0; i1 < len1; i1++)
{
T* addr1 = addr + i1 * stride1;
T[][] src1 = data[i1];
for (int i2 = 0; i2 < len2; i2++)
{
T* addr2 = addr1 + i2 * stride2;
T[] src2 = src1[i2];
for (int i3 = 0; i3 < len3; i3++)
{
*(addr2 + i3) = src2[i3];
}
}
}
return @out;
}
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from. Shape is taken from the first item of each array/nested array.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>Always performs a copy.</remarks>
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
[SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")]
[MethodImpl(Optimize)]
public static NDArray array<T>(T[][][][] data) where T : unmanaged
{
unsafe
{
int len1 = data.Length;
int len2 = data[0].Length;
int len3 = data[0][0].Length;
int len4 = data[0][0][0].Length;
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2, len3, len4));
var strides = @out.Shape.Strides;
long stride1 = strides[0];
long stride2 = strides[1];
long stride3 = strides[2];
Debug.Assert(strides[3] == 1);
T* addr = (T*)@out.Address;
for (int i1 = 0; i1 < len1; i1++)
{
T* addr1 = addr + i1 * stride1;
T[][][] src1 = data[i1];
for (int i2 = 0; i2 < len2; i2++)
{
T* addr2 = addr1 + i2 * stride2;
T[][] src2 = src1[i2];
for (int i3 = 0; i3 < len3; i3++)
{
T* addr3 = addr2 + i3 * stride3;
T[] src3 = src2[i3];
for (int i4 = 0; i4 < len4; i4++)
{
*(addr3 + i4) = src3[i4];
}
}
}
}
return @out;
}
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from. Shape is taken from the first item of each array/nested array.</param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html <br></br>Always performs a copy.</remarks>
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
[SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")]
[MethodImpl(Optimize)]
public static NDArray array<T>(T[][][][][] data) where T : unmanaged
{
unsafe
{
int len1 = data.Length;
int len2 = data[0].Length;
int len3 = data[0][0].Length;
int len4 = data[0][0][0].Length;
int len5 = data[0][0][0][0].Length;
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2, len3, len4, len5));
var strides = @out.Shape.Strides;
long stride1 = strides[0];
long stride2 = strides[1];
long stride3 = strides[2];
long stride4 = strides[3];
Debug.Assert(strides[4] == 1);
T* addr = (T*)@out.Address;
for (int i1 = 0; i1 < len1; i1++)
{
T* addr1 = addr + i1 * stride1;
T[][][][] src1 = data[i1];
for (int i2 = 0; i2 < len2; i2++)
{
T* addr2 = addr1 + i2 * stride2;
T[][][] src2 = src1[i2];
for (int i3 = 0; i3 < len3; i3++)
{
T* addr3 = addr2 + i3 * stride3;
T[][] src3 = src2[i3];
for (int i4 = 0; i4 < len4; i4++)
{
T* addr4 = addr3 + i4 * stride4;
T[] src4 = src3[i4];
for (int i5 = 0; i5 < len5; i5++)
{
*(addr4 + i5) = src4[i5];
}
}
}
}
}
return @out;
}
}
// %l = "data.GetLength("
// %r = ")"
// %foreach range(1,16)%
// /// <summary>
// /// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
// /// </summary>
// /// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
// /// <param name="data">The array to create <see cref="NDArray"/> from.</param>
// /// <param name="copy">
// /// If true then the array will be copied to a newly allocated memory.<br></br>
// /// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
// /// </param>
// /// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
// /// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
// public static NDArray array<T>(T[#(repeat(",", i))] data, bool copy = true) where T : unmanaged
// {
// if (data == null)
// throw new ArgumentNullException(nameof(data));
// return new NDArray(ArraySlice.FromArray(data, copy), new Shape(#(repeat("^l+str(n.ToString())+r", i+1, ", "))));
// }
// %
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[] data, bool copy) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.Length));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/> with specified dtype.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="dtype">
/// The desired dtype for the array — one descriptor parameter, like NumPy's <c>dtype</c>: a C# <see cref="Type"/>, an
/// <see cref="NPTypeCode"/>, a NumPy dtype string (<c>"f4"</c>) or a <see cref="DType"/> all convert implicitly.
/// If different from <typeparamref name="T"/>, the data will be cast.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data cast to the specified dtype.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,] data, DType dtype) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
var arr = new NDArray(ArraySlice.FromArray(data, true), new Shape(data.GetLength(0), data.GetLength(1)));
if (dtype != null && dtype.GetTypeCode() != InfoOf<T>.NPTypeCode)
return arr.astype(dtype);
return arr;
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12), data.GetLength(13)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12), data.GetLength(13), data.GetLength(14)));
}
/// <summary>
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
/// </summary>
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
/// <param name="copy">
/// If true then the array will be copied to a newly allocated memory.<br></br>
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
/// </param>
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.array.html</remarks>
public static NDArray array<T>(T[,,,,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12), data.GetLength(13), data.GetLength(14), data.GetLength(15)));
}
}
}