Skip to content

Commit de0b9e8

Browse files
committed
Revamped np.reshape
1 parent a87610b commit de0b9e8

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

src/NumSharp.Core/Creation/NdArray.ReShape.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,46 @@ namespace NumSharp
1010
public partial class NDArray
1111
{
1212
/// <summary>
13-
///
13+
/// Gives a new shape to an array without changing its data.
1414
/// </summary>
15-
/// <param name="shape"></param>
16-
/// <param name="order">
17-
/// C: row major
18-
/// F: column major
19-
/// </param>
20-
/// <returns></returns>
21-
public NDArray reshape(Shape shape, char order = 'C')
15+
/// <param name="newshape">The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.</param>
16+
/// <returns>This will be a new view object if possible; otherwise, it will be a copy. Note there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.</returns>
17+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html</remarks>
18+
public NDArray reshape(ref Shape newshape)
2219
{
23-
//TODO! support for order 'F'
24-
return new NDArray(new UnmanagedStorage(Storage.InternalArray, shape)) {TensorEngine = TensorEngine};
20+
if (newshape.size == 0 && size != 0)
21+
throw new ArgumentException("Value cannot be an empty collection.", nameof(newshape));
22+
23+
if (Shape.IsSliced)
24+
Console.WriteLine("reshaping a sliced NDArray performs performs a copy.");
25+
26+
var storage = newshape.IsSliced ? new UnmanagedStorage(Storage.CloneData(), newshape) : Storage.Alias(newshape);
27+
return new NDArray(storage) { TensorEngine = TensorEngine };
2528
}
2629

27-
public NDArray reshape(params int[] shape)
30+
/// <summary>
31+
/// Gives a new shape to an array without changing its data.
32+
/// </summary>
33+
/// <param name="newshape">The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.</param>
34+
/// <returns>This will be a new view object if possible; otherwise, it will be a copy. Note there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.</returns>
35+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html</remarks>
36+
public NDArray reshape(Shape newshape)
2837
{
29-
//TODO support negative index.
30-
return new NDArray(new UnmanagedStorage(Storage.InternalArray, shape)) {TensorEngine = TensorEngine};
38+
return reshape(ref newshape);
3139
}
3240

33-
public NDArray reshape(ref Shape shape)
41+
/// <summary>
42+
/// Gives a new shape to an array without changing its data.
43+
/// </summary>
44+
/// <param name="shape">The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.</param>
45+
/// <returns>This will be a new view object if possible; otherwise, it will be a copy. Note there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.</returns>
46+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html</remarks>
47+
public NDArray reshape(params int[] shape)
3448
{
35-
return new NDArray(new UnmanagedStorage(Storage.InternalArray, shape)) {TensorEngine = TensorEngine};
49+
if (shape.Length == 0 && size != 0)
50+
throw new ArgumentException("Value cannot be an empty collection.", nameof(shape));
51+
52+
return reshape(new Shape(shape));
3653
}
3754

3855
protected static int FindNegativeIndex(params int[] shape)

src/NumSharp.Core/Manipulation/np.reshape.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public static partial class np
1616
public static NDArray reshape(NDArray nd, params int[] shape)
1717
{
1818
return nd.reshape(shape);
19-
}
20-
19+
}
20+
2121
/// <summary>
2222
/// Gives a new shape to an array without changing its data.
2323
/// </summary>

0 commit comments

Comments
 (0)