forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.stack.cs
More file actions
38 lines (34 loc) · 1.6 KB
/
np.stack.cs
File metadata and controls
38 lines (34 loc) · 1.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
using System;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Join a sequence of arrays along a new axis.
/// The axis parameter specifies the index of the new axis in the dimensions of the result.
/// For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.
/// </summary>
/// <param name="arrays">Each array must have the same shape.</param>
/// <param name="axis">The axis in the result array along which the input arrays are stacked.</param>
/// <returns>The stacked array has one more dimension than the input arrays.</returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.stack.html</remarks>
public static NDArray stack(NDArray[] arrays, int axis = 0)
{
if (arrays == null)
throw new ArgumentNullException(nameof(arrays));
if (arrays.Length == 0)
throw new ArgumentException("Value cannot be an empty collection.", nameof(arrays));
arrays = np.atleast_1d(arrays); //handle scalars
var first = arrays[0];
arrays[0] = np.expand_dims(first, axis);
for (int i = 1; i < arrays.Length; i++)
{
var curr = arrays[i];
if (curr.Shape != first.Shape)
throw new InvalidOperationException("all input arrays must have the same shape");
arrays[i] = np.expand_dims(curr, axis);
}
return np.concatenate(arrays, axis);
}
}
}