Skip to content

Commit 8348e9b

Browse files
authored
Merge pull request #156 from dotChris90/master
Made sure all methods and tests do not use the generic numpy
2 parents e3a8b07 + 7557107 commit 8348e9b

File tree

17 files changed

+256
-274
lines changed

17 files changed

+256
-274
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NumSharp.Core
7+
{
8+
public partial class NDArray
9+
{
10+
/// <summary>
11+
/// Stack arrays in sequence horizontally
12+
/// </summary>
13+
/// <param name="nps"></param>
14+
/// <returns></returns>
15+
public NDArray hstack(params NDArray[] nps)
16+
{
17+
if (nps == null || nps.Length == 0)
18+
throw new Exception("Input arrays can not be empty");
19+
20+
var npAll = new NDArray[nps.Length+1];
21+
npAll[0] = this;
22+
23+
for (int idx = 0; idx < nps.Length; idx++)
24+
if (nps[0].shape != nps[idx].shape)
25+
throw new Exception("Arrays mush have same shapes");
26+
else
27+
npAll[idx+1] = nps[idx];
28+
29+
NDArray np = new NDArray();
30+
31+
// easy 1D case
32+
if (this.ndim == 1)
33+
{
34+
var list1D = new List<object>();
35+
for (int idx = 0; idx < npAll.Length;idx++)
36+
list1D.AddRange(npAll[idx].Storage.GetData<object>().ToList());
37+
38+
np = NumPy.array(list1D.ToArray(),this.dtype);
39+
}
40+
else
41+
{
42+
var list = new List<object>();
43+
44+
int total = npAll[0].ndim == 1 ? 1 : npAll[0].shape.Shapes[0];
45+
46+
int pageSize = npAll[0].ndim == 1 ? npAll[0].shape.Shapes[0] : npAll[0].shape.DimOffset[0];
47+
48+
for (int i = 0; i < total; i++)
49+
{
50+
for (int k = 0; k < npAll.Length; k++)
51+
{
52+
for (int j = i * pageSize; j < (i + 1) * pageSize; j++)
53+
list.Add(npAll[k].Storage.GetData<object>()[j]);
54+
}
55+
}
56+
57+
np.Storage.SetData( list.ToArray());
58+
59+
int[] shapes = npAll[0].shape.Shapes.ToArray();
60+
61+
if (shapes.Length == 1)
62+
shapes[0] *= npAll.Length;
63+
else
64+
shapes[1] *= npAll.Length;
65+
66+
np.Storage.Shape = new Shape(shapes);
67+
68+
69+
}
70+
71+
return np;
72+
}
73+
}
74+
}

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,31 @@ public NDArray vstack(params NDArray[] nps)
1111
{
1212
if (nps == null || nps.Length == 0)
1313
throw new Exception("Input arrays can not be empty");
14+
15+
List<object> list = this.Storage.GetData<object>().ToList();
1416

15-
int dataLength = this.Storage.Length;
16-
17-
for (int idx = 0;idx < nps.Length;idx++)
18-
dataLength += nps[idx].Storage.Length;
19-
20-
Array dataSysArr = Array.CreateInstance(this.dtype,dataLength);
21-
NDArray np = new NDArray(this.dtype);
22-
23-
this.Storage.GetData(this.dtype).CopyTo(dataSysArr,0);
24-
25-
int idxOfLastCopyiedElement = this.Storage.Length;
17+
NDArray np = new NDArray(dtype);
2618

27-
for (int idx = 0; idx < nps.Length;idx++)
19+
foreach (NDArray ele in nps)
2820
{
29-
nps[idx].Storage.GetData(this.dtype).CopyTo(dataSysArr,idxOfLastCopyiedElement);
30-
idxOfLastCopyiedElement += nps[idx].Storage.Length;
21+
if (nps[0].shape != ele.shape)
22+
throw new Exception("Arrays mush have same shapes");
23+
24+
list.AddRange(ele.Storage.GetData<object>());
3125
}
32-
33-
var check = dataSysArr.GetValue(idxOfLastCopyiedElement-1);
3426

35-
if (ndim == 1)
27+
np.Storage.SetData(list.ToArray());
28+
29+
if (nps[0].shape.Shapes.Count == 1)
3630
{
37-
np.Storage.Shape = new Shape(new int[] { nps.Length+1, shape.Shapes[0] });
31+
np.Storage.Shape = new Shape(new int[] { nps.Length +1, nps[0].shape.Shapes[0] });
3832
}
3933
else
4034
{
4135
int[] shapes = nps[0].shape.Shapes.ToArray();
42-
shapes[0] *= nps.Length;
43-
np.Storage.Shape = new Shape(shapes);
36+
shapes[0] *= nps.Length + 1;
37+
np.Storage.Shape = new Shape(shapes) ;
4438
}
45-
46-
np.Storage.SetData(dataSysArr);
47-
4839
return np;
4940
}
5041
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NumSharp.Core.Extensions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core
7+
{
8+
public static partial class NumPy
9+
{
10+
public static NDArray hstack(params NDArray[] nps)
11+
{
12+
var np1 = nps[0];
13+
14+
var npn = new NDArray[nps.Length-1];
15+
for (int idx = 1;idx < nps.Length;idx++)
16+
npn[idx-1] = nps[idx];
17+
18+
return np1.hstack(npn);
19+
}
20+
}
21+
}

src/NumSharp.Core/Creation/NumPy.ones.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,5 @@ public static NDArray ones<T>(params int[] shapes)
6363
}
6464
}
6565

66-
public static partial class NumPyExtensions
67-
{
68-
/// <summary>
69-
/// Return a new array of given shape and type, filled with ones.
70-
/// </summary>
71-
/// <typeparam name="T"></typeparam>
72-
/// <param name="np"></param>
73-
/// <param name="shape"></param>
74-
/// <returns></returns>
75-
public static NDArrayGeneric<T> ones<T>(this NumPyGeneric<T> np, Shape shape)
76-
{
77-
var nd = new NDArrayGeneric<T>();
78-
nd.Shape = shape;
79-
80-
switch (default(T))
81-
{
82-
case int data:
83-
nd.Data = Enumerable.Range(0, nd.Size).Select(x => (T)(object)1).ToArray();
84-
break;
85-
86-
case double data:
87-
nd.Data = Enumerable.Range(0, nd.Size).Select(x => (T)(object)1.0).ToArray();
88-
break;
89-
}
90-
91-
return nd;
92-
}
93-
}
66+
9467
}

src/NumSharp.Core/Creation/NumPy.ones_like.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
namespace NumSharp.Core
77
{
8-
public static partial class NumPyExtensions
8+
public static partial class NumPy
99
{
10-
public static NDArrayGeneric<T> ones_like<T>(this NumPyGeneric<T> np, NDArrayGeneric<T> nd, string order = "C")
10+
public static NDArray ones_like(NDArray nd, string order = "C")
1111
{
12-
return np.ones(new Shape(nd.Shape.Shapes));
12+
return NumPy.ones(new Shape(nd.shape.Shapes));
1313
}
1414
}
1515
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NumSharp.Core.Extensions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core
7+
{
8+
public static partial class NumPy
9+
{
10+
public static NDArray vstack(params NDArray[] nps)
11+
{
12+
var np1 = nps[0];
13+
14+
var npn = new NDArray[nps.Length-1];
15+
for (int idx = 1;idx < nps.Length;idx++)
16+
npn[idx-1] = nps[idx];
17+
18+
return np1.vstack(npn);
19+
}
20+
}
21+
}

src/NumSharp.Core/Extensions/NdArray.HStack.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/NumSharp.Core/Extensions/NdArray.Mean.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)