forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.array.cs
More file actions
105 lines (86 loc) · 3.59 KB
/
np.array.cs
File metadata and controls
105 lines (86 loc) · 3.59 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
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using NumSharp.Backends.Unmanaged;
using NumSharp.Utilities;
namespace NumSharp
{
public static partial class np
{
[MethodImpl((MethodImplOptions)768)]
public static NDArray array(NDArray nd, bool copy = false)
{
return copy ? new NDArray(nd.Storage.Clone()) : new NDArray(nd.Storage);
}
public static NDArray array(Array array, Type dtype = null, int ndmin = 1, bool copy = true, char order = 'C')
{
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 != arrType)
{
array = ArrayConvert.To(array, dtype);
copy = false;
}
return new NDArray(copy ? (Array)array.Clone() : array, shape, order);
}
public static NDArray array<T>(params T[] data) where T : unmanaged
{
return new NDArray(ArraySlice.FromArray(data), new Shape(data.Length));
}
public static NDArray array<T>(T[][] data)
{
var array = data.SelectMany(inner => inner).ToArray(); //todo! not use selectmany.
return new NDArray(array, new Shape(data.Length, data[0].Length));
}
public static NDArray array<T>(T[][][] data)
{
var array = data.SelectMany(inner => inner //todo! not use selectmany.
.SelectMany(innerInner => innerInner))
.ToArray();
return new NDArray(array, new Shape(data.Length, data[0].Length, data[0][0].Length));
}
public static NDArray array<T>(T[][][][] data)
{
var array = data.SelectMany(inner => inner //todo! not use selectmany.
.SelectMany(innerInner => innerInner
.SelectMany(innerInnerInner => innerInnerInner)))
.ToArray();
return new NDArray(array, new Shape(data.Length, data[0].Length, data[0][0].Length, data[0][0][0].Length));
}
public static NDArray array<T>(T[,] data) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data.Cast<T>().ToArray()), new Shape(data.GetLength(0), data.GetLength(1)));
}
public static NDArray array<T>(T[,,] data) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data.Cast<T>().ToArray()), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2)));
}
public static NDArray array<T>(T[,,,] data) where T : unmanaged
{
if (data == null)
throw new ArgumentNullException(nameof(data));
return new NDArray(ArraySlice.FromArray(data.Cast<T>().ToArray()), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3)));
}
}
}