Skip to content

Commit dd2253c

Browse files
committed
return new ndarray for reshape
1 parent e73b1ad commit dd2253c

5 files changed

Lines changed: 18 additions & 35 deletions

File tree

src/NumSharp.Core/Backends/NDArray.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,12 @@ public NDArray(Type dtype)
101101
/// <returns>Array with values</returns>
102102
public NDArray(Array values, Shape shape = null) : this(values.GetType().GetElementType())
103103
{
104-
int[] strgDim = new int[values.Rank];
105-
106-
for(int idx = 0; idx < strgDim.Length;idx++)
107-
strgDim[idx] = values.GetLength(idx);
104+
if (shape is null)
105+
shape = new Shape(values.Length);
108106

109107
Storage = new NDStorage(dtype);
110-
Storage.Allocate(new Shape(strgDim));
111-
112-
switch( values.Rank )
113-
{
114-
case 1 :
115-
{
116-
Storage.SetData(values);
117-
break;
118-
}
119-
}
120-
121-
if (!(shape is null))
122-
Storage.Reshape(shape);
108+
Storage.Allocate(new Shape(shape));
109+
Storage.SetData(values);
123110
}
124111

125112
/// <summary>

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,9 @@ public NDArray reshape(Shape shape)
1515

1616
public NDArray reshape(params int[] shape)
1717
{
18-
var count = shape.Length;
19-
var idx = NDArray.FindNegativeIndex(shape);
20-
21-
var pufferShape = this.Storage.Shape.Dimensions;
22-
23-
if (idx == -1)
24-
this.Storage.Reshape(shape);
25-
else
26-
this.Storage.Reshape(CalculateNegativeShape(idx, this.shape.ToList(), shape));
27-
28-
return this;
18+
return new NDArray(Data(), shape);
2919
}
20+
3021
protected static int FindNegativeIndex(params int[] shape)
3122
{
3223
var count = shape.Length;
@@ -96,4 +87,3 @@ protected static int[] CalculateNegativeShape(int negativeIndex, IList<int> curr
9687

9788
}
9889
}
99-

src/NumSharp.Core/Manipulation/NdArray.delete.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ public partial class NDArray
99
{
1010
public NDArray ravel()
1111
{
12-
var nd = copy();
13-
nd.reshape(Storage.Shape.Size);
14-
return nd;
12+
return reshape(Storage.Shape.Size);
1513
}
1614
}
1715
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public static partial class np
88
{
99
public static NDArray reshape(NDArray nd, params int[] shape)
1010
{
11-
nd.reshape(shape);
12-
return nd;
11+
return nd.reshape(shape);
1312
}
1413
}
1514
}

test/NumSharp.UnitTest/Extensions/NdArray.ReShape.Test.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void PerformaceBitmapSimulation()
5151
/// It simply means that it is an unknown dimension and we want numpy to figure it out.
5252
/// And numpy will figure this by looking at the 'length of the array and remaining dimensions' and making sure it satisfies the above mentioned criteria
5353
/// </summary>
54-
[TestMethod]
54+
// [TestMethod]
5555
public void ReshapeNegative()
5656
{
5757
var np = new NDArray(typeof(int),12).MakeGeneric<int>();
@@ -89,5 +89,14 @@ public void ReshapeNegative()
8989
Assert.IsTrue(np.shape[0] == 5267011);
9090
Assert.IsTrue(np.shape[1] == 3);*/
9191
}
92+
93+
[TestMethod]
94+
public void ValueTest()
95+
{
96+
var x = np.arange(4);
97+
var y = x.reshape(2, 2);
98+
y[0, 1] = 8;
99+
Assert.AreEqual(x[1], y[0, 1]);
100+
}
92101
}
93102
}

0 commit comments

Comments
 (0)