forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.squeeze.cs
More file actions
87 lines (78 loc) · 4.51 KB
/
np.squeeze.cs
File metadata and controls
87 lines (78 loc) · 4.51 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
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using NumSharp.Utilities;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Remove single-dimensional entries from the shape of an array.
/// </summary>
/// <param name="a">Input data.</param>
/// <returns>The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into a.</returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html</remarks>
public static NDArray squeeze(NDArray a)
{
return a.reshape(a.shape.Where(x => x != 1).ToArray());
}
/// <summary>
/// Remove single-dimensional entries from the shape of an array.
/// </summary>
/// <param name="a">Input data.</param>
/// <param name="axis">Selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.</param>
/// <returns>The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into a.</returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html</remarks>
/// <exception cref="IncorrectShapeException">If axis is not None, and an axis being squeezed is not of length 1</exception>
public static NDArray squeeze(NDArray a, int axis)
{
while (axis < 0)
axis = a.ndim + axis; //handle negative axis
if (axis >= a.ndim)
throw new ArgumentOutOfRangeException(nameof(axis));
if (a.shape[axis] != 1)
throw new IncorrectShapeException($"Unable to squeeze axis {axis} because it is of length {a.shape[axis]} and not 1.");
return a.reshape(squeeze_fast(a.Shape, axis));
}
/// <summary>
/// Remove single-dimensional entries from a shape.
/// </summary>
/// <param name="shape">Input shape.</param>
/// <returns>The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into a.</returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html</remarks>
public static Shape squeeze(Shape shape)
{
//TODO! what will happen if its a slice?
return new Shape(shape.dimensions.Where(d => d != 1).ToArray());
}
/// <summary>
/// Remove single-dimensional entries from the shape of an array.
/// </summary>
/// <param name="a">Input data.</param>
/// <param name="axis">Selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.</param>
/// <returns>The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into a.</returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html</remarks>
/// <exception cref="IncorrectShapeException">If axis is not None, and an axis being squeezed is not of length 1</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static NDArray squeeze_fast(NDArray a, int axis)
{
return a.reshape(squeeze_fast(a.Shape, axis));
}
/// <summary>
/// Remove single-dimensional entries from the shape of an array.
/// </summary>
/// <param name="a">Input data.</param>
/// <param name="axis">Selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.</param>
/// <returns>The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into a.</returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html</remarks>
/// <exception cref="IncorrectShapeException">If axis is not None, and an axis being squeezed is not of length 1</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Shape squeeze_fast(Shape a, int axis)
{
var r = a.dimensions.RemoveAt(axis);
if (r.Length == 0 || r.Length == 1 && r[0] == 1)
return Shape.Scalar;
return new Shape(r);
}
}
}