forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.Reshaping.cs
More file actions
196 lines (168 loc) · 7.13 KB
/
Shape.Reshaping.cs
File metadata and controls
196 lines (168 loc) · 7.13 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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using NumSharp.Utilities;
namespace NumSharp
{
/// <summary>
/// Represents a shape of an N-D array.
/// </summary>
/// <remarks>Handles slicing, indexing based on coordinates or linear offset and broadcastted indexing.</remarks>
public partial struct Shape
{
/// <summary>
/// Changes the shape representing this storage.
/// </summary>
/// <exception cref="IncorrectShapeException">If shape's size mismatches current shape size.</exception>
/// <exception cref="ArgumentException">If <paramref name="newShape"/>'s size == 0</exception>
/// <param name="unsafe">When true, then guards are skipped.</param>
[MethodImpl((MethodImplOptions)768)]
public readonly Shape Reshape(Shape newShape, bool @unsafe = true)
{
if (IsBroadcasted)
{
_reshapeBroadcast(ref newShape, @unsafe);
return newShape;
}
//handle -1 in reshape
_inferMissingDimension(ref newShape);
if (!@unsafe)
{
if (newShape.size == 0 && size != 0)
throw new ArgumentException("Value cannot be an empty collection.", nameof(newShape));
if (size != newShape.size)
throw new IncorrectShapeException($"Given shape size ({newShape.size}) does not match the size of the given storage size ({size})");
}
if (IsSliced)
// Set up the new shape (of reshaped slice) to recursively represent a shape within a sliced shape
newShape.ViewInfo = new ViewInfo() { ParentShape = this, Slices = null };
return newShape;
}
/// <summary>
/// Changes the shape representing this storage.
/// </summary>
/// <exception cref="IncorrectShapeException">If shape's size mismatches current shape size.</exception>
/// <exception cref="ArgumentException">If <paramref name="newShape"/>'s size == 0</exception>
[MethodImpl((MethodImplOptions)768)]
private readonly void _reshapeBroadcast(ref Shape newShape, bool @unsafe = true)
{
//handle -1 in reshape
_inferMissingDimension(ref newShape);
if (!@unsafe)
{
if (newShape.size == 0 && size != 0)
throw new ArgumentException("Value cannot be an empty collection.", nameof(newShape));
if (size != newShape.size)
throw new IncorrectShapeException($"Given shape size ({newShape.size}) does not match the size of the given storage size ({size})");
}
if (IsSliced)
// Set up the new shape (of reshaped slice) to recursively represent a shape within a sliced shape
newShape.ViewInfo = new ViewInfo() { ParentShape = this, Slices = null };
newShape.BroadcastInfo = IsBroadcasted ? BroadcastInfo.Clone() : new BroadcastInfo(this);
}
[SuppressMessage("ReSharper", "ParameterHidesMember")]
private readonly void _inferMissingDimension(ref Shape shape)
{
var indexOfNegOne = -1;
int product = 1;
for (int i = 0; i < shape.NDim; i++)
{
if (shape[i] == -1)
{
if (indexOfNegOne != -1)
throw new ArgumentException("Only allowed to pass one shape dimension as -1");
indexOfNegOne = i;
}
else
{
product *= shape[i];
}
}
if (indexOfNegOne == -1)
return;
if (this.IsBroadcasted)
{
/* //TODO: the following case needs to be handled.
* a = np.arange(4).reshape(1,2,2)
* print(a.strides)
*
* b = np.broadcast_to(a, (2,2,2))
* print(b.strides)
*
* c = np.reshape(b, (2, -1))
* print(c.strides)
*
* c = np.reshape(b, (-1, 2))
* print(c.strides)
*
* (16, 8, 4)
* (0, 8, 4)
* (0, 4) //here it handles broadcast
* (8, 4) //here can be seen that numpy performs a copy
*/
//var originalReshaped = np.broadcast_to(this.BroadcastInfo.OriginalShape.Reshape(new int[] { this.BroadcastInfo.OriginalShape.size }), (Shape) new int[] { this.size });
throw new NotSupportedException("Reshaping a broadcasted array with a -1 (unknown) dimension is not supported.");
}
int missingValue = this.size / product;
if (missingValue * product != this.size)
{
throw new ArgumentException("Bad shape: missing dimension would have to be non-integer");
}
shape.dimensions[indexOfNegOne] = missingValue;
var strides = shape.strides;
var dimensions = shape.dimensions;
if (indexOfNegOne == strides.Length - 1)
strides[strides.Length - 1] = 1;
for (int idx = indexOfNegOne; idx >= 1; idx--)
strides[idx - 1] = strides[idx] * dimensions[idx];
shape.ComputeHashcode();
}
/// <summary>
/// Expands a specific <paramref name="axis"/> with 1 dimension.
/// </summary>
/// <param name="axis"></param>
/// <returns></returns>
[SuppressMessage("ReSharper", "LocalVariableHidesMember")]
public readonly Shape ExpandDimension(int axis)
{
Shape ret;
if (IsScalar)
{
ret = Vector(1);
ret.strides[0] = 0;
}
else
{
ret = Clone(true, true, false);
}
var dimensions = ret.dimensions;
var strides = ret.strides;
// Allow negative axis specification
if (axis < 0)
{
axis = dimensions.Length + 1 + axis;
if (axis < 0)
{
throw new ArgumentException($"Effective axis {axis} is less than 0");
}
}
Arrays.Insert(ref dimensions, axis, 1);
Arrays.Insert(ref strides, axis, 0);
ret.dimensions = dimensions;
ret.strides = strides;
if (IsSliced)
{
ret.ViewInfo = new ViewInfo() { ParentShape = this, Slices = null };
}
if (IsBroadcasted)
{
ret.BroadcastInfo = new BroadcastInfo(!BroadcastInfo.OriginalShape.IsEmpty ? BroadcastInfo.OriginalShape : this);
ret.BroadcastInfo.UnreducedBroadcastedShape = null; //reset so it will be recomupted.
}
ret.ComputeHashcode();
return ret;
}
}
}