Skip to content

Commit 047ae49

Browse files
committed
NDArray.FromMultiDimArray<T>: Fixed #367
1 parent bad8767 commit 047ae49

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

src/NumSharp.Core/Casting/NdArrayFromMultiDimArr.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,46 @@ public partial class NDArray
2525
/// <summary>
2626
/// Creates an NDArray out of given array of type <typeparamref name="T"/>
2727
/// </summary>
28-
/// <param name="dotNetArray"></param>
29-
public static NDArray FromMultiDimArray<T>(Array dotNetArray)
28+
/// <param name="ndarray"></param>
29+
public static NDArray FromMultiDimArray<T>(Array ndarray, bool copy = true) where T : unmanaged
3030
{
31-
if (dotNetArray.GetType().GetElementType().IsArray)
32-
throw new Exception("Jagged arrays are not allowed here!");
31+
if (ndarray.GetType().GetElementType().IsArray)
32+
throw new Exception("Given array is a jagged array.");
3333

34-
switch (dotNetArray.Rank)
34+
switch (ndarray.Rank)
3535
{
3636
case 1:
37-
return np.array((T[])dotNetArray);
37+
return np.array((T[])ndarray, copy);
3838
case 2:
39-
return np.array((T[,])dotNetArray);
39+
return np.array((T[,])ndarray, copy);
4040
case 3:
41-
return np.array((T[,,])dotNetArray);
41+
return np.array((T[,,])ndarray, copy);
4242
case 4:
43-
return np.array((T[,,,])dotNetArray);
43+
return np.array((T[,,,])ndarray, copy);
4444
case 5:
45-
return np.array((T[,,,,])dotNetArray);
45+
return np.array((T[,,,,])ndarray, copy);
4646
case 6:
47-
return np.array((T[,,,,,])dotNetArray);
47+
return np.array((T[,,,,,])ndarray, copy);
4848
case 7:
49-
return np.array((T[,,,,,,])dotNetArray);
49+
return np.array((T[,,,,,,])ndarray, copy);
5050
case 8:
51-
return np.array((T[,,,,,,,])dotNetArray);
51+
return np.array((T[,,,,,,,])ndarray, copy);
5252
case 9:
53-
return np.array((T[,,,,,,,,])dotNetArray);
53+
return np.array((T[,,,,,,,,])ndarray, copy);
5454
case 10:
55-
return np.array((T[,,,,,,,,,])dotNetArray);
55+
return np.array((T[,,,,,,,,,])ndarray, copy);
5656
case 11:
57-
return np.array((T[,,,,,,,,,,])dotNetArray);
57+
return np.array((T[,,,,,,,,,,])ndarray, copy);
5858
case 12:
59-
return np.array((T[,,,,,,,,,,,])dotNetArray);
59+
return np.array((T[,,,,,,,,,,,])ndarray, copy);
60+
case 13:
61+
return np.array((T[,,,,,,,,,,,,])ndarray, copy);
62+
case 14:
63+
return np.array((T[,,,,,,,,,,,,,])ndarray, copy);
64+
case 15:
65+
return np.array((T[,,,,,,,,,,,,,,])ndarray, copy);
66+
case 16:
67+
return np.array((T[,,,,,,,,,,,,,,,])ndarray, copy);
6068
}
6169

6270
throw new NotImplementedException("FromMultiDimArray<T>(Array dotNetArray)");

src/NumSharp.Core/Creation/np.array.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace NumSharp
1515
{
1616
public static partial class np
1717
{
18-
1918
/// <summary>
2019
/// Wraps given <paramref name="nd"/> in an alias. If <paramref name="copy"/> is true then returns a clone.
2120
/// </summary>
@@ -111,7 +110,7 @@ public static NDArray array<T>(IEnumerable<T> data, int size) where T : unmanage
111110
using (var enumerator = data.GetEnumerator())
112111
{
113112
Func<bool> next = enumerator.MoveNext;
114-
113+
115114
var addr = slice.Address;
116115
for (int i = 0; i < size && next(); i++)
117116
{
@@ -348,7 +347,7 @@ public static NDArray array<T>(T[][][][][] data) where T : unmanaged
348347
#if _REGEN
349348
%l = "data.GetLength("
350349
%r = ")"
351-
%foreach range(1,15)%
350+
%foreach range(1,16)%
352351

353352
/// <summary>
354353
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
@@ -655,6 +654,25 @@ public static NDArray array<T>(T[,,,,,,,,,,,,,,] data, bool copy = true) where T
655654

656655
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)));
657656
}
657+
658+
/// <summary>
659+
/// Creates an <see cref="NDArray"/> from given <paramref name="data"/>.
660+
/// </summary>
661+
/// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam>
662+
/// <param name="data">The array to create <see cref="NDArray"/> from.</param>
663+
/// <param name="copy">
664+
/// If true then the array will be copied to a newly allocated memory.<br></br>
665+
/// If false then the array will be pinned by calling <see cref="GCHandle.Alloc(object)"/>.
666+
/// </param>
667+
/// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns>
668+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html</remarks>
669+
public static NDArray array<T>(T[,,,,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged
670+
{
671+
if (data == null)
672+
throw new ArgumentNullException(nameof(data));
673+
674+
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)));
675+
}
658676
#endif
659677
}
660678
}

0 commit comments

Comments
 (0)