Skip to content

Commit 4751fe6

Browse files
authored
Merge pull request #140 from dotChris90/master
Did rewrite more non generic methods
2 parents 8d3ecb8 + ff74d80 commit 4751fe6

19 files changed

Lines changed: 223 additions & 362 deletions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ public NDArray ones(Type dtype = null, params int[] shapes)
2222

2323
return this;
2424
}
25+
public NDArray ones(params int[] shapes)
26+
{
27+
return this.ones(typeof(double),shapes);
28+
}
2529
}
2630
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@
77

88
namespace NumSharp.Core
99
{
10-
public static partial class NumPyExtensions
10+
public partial class NumPy
1111
{
12-
public static NDArray array(this NumPy np, Array array, Type dtype = null, int ndim = 1)
12+
public NDArray array(Array array, Type dtype = null, int ndim = 1)
1313
{
1414
dtype = (dtype == null) ? array.GetType().GetElementType() : dtype;
1515

1616
var nd = new NDArray(dtype);
17-
18-
nd.Storage = NDStorage.CreateByShapeAndType (dtype, new Shape(new int[] { array.Length }));
19-
nd.Storage.SetData(array);
17+
18+
if ((array.Rank == 1) && ( !array.GetType().GetElementType().IsArray ))
19+
{
20+
nd.Storage = NDStorage.CreateByShapeAndType (dtype, new Shape(new int[] { array.Length }));
21+
nd.Storage.SetData(array);
22+
}
23+
else
24+
{
25+
throw new Exception("Method is not implemeneted for multidimensional arrays or jagged arrays.");
26+
}
27+
2028
return nd;
2129
}
2230

23-
public static NDArray array<T>(this NumPy np, System.Drawing.Bitmap image)
31+
public NDArray array(System.Drawing.Bitmap image)
2432
{
25-
var imageArray = new NDArray(typeof(T));
33+
var imageArray = new NDArray(typeof(Byte));
2634

2735
var bmpd = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
2836
var dataSize = bmpd.Stride * bmpd.Height;
@@ -37,7 +45,7 @@ public static NDArray array<T>(this NumPy np, System.Drawing.Bitmap image)
3745
return imageArray;
3846
}
3947

40-
public static NDArray array<T>(this NumPy np, T[][] data)
48+
public NDArray array<T>(T[][] data)
4149
{
4250
int size = data.Length * data[0].Length;
4351
var all = new T[size];

src/NumSharp.Core/Extensions/NDArray.Std.cs

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

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

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

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

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

src/NumSharp.Core/Extensions/NumPy.min.cs

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

src/NumSharp.Core/Math/NdArray.Convolve.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public NDArray Convolve(NDArray numSharpArray2, string mode = "full" )
2424
if (shape.NDim > 1)
2525
throw new IncorrectShapeException();
2626

27-
var numSharpReturn = new NDArray(this.dtype);
27+
var numSharpReturn = new NDArray(typeof(double));
2828

2929
double[] np1 = this.Storage.GetData<double>();
3030
double[] np2 = numSharpArray2.Storage.GetData<double>();

src/NumSharp.Core/Math/NumPy.amax.cs

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,7 @@ public partial class NumPy
99
{
1010
public NDArray amax(NDArray nd, int? axis = null)
1111
{
12-
var res = new NDArray(nd.dtype, nd.shape);
13-
14-
if (axis == null)
15-
{
16-
res.Set(new double[1] { nd.Data<double>().Max() });
17-
res.Storage.Shape = new Shape(new int[] { 1 });
18-
}
19-
else
20-
{
21-
if (axis < 0 || axis >= nd.ndim)
22-
throw new Exception("Invalid input: axis");
23-
int[] resShapes = new int[nd.ndim - 1];
24-
int index = 0; //index for result shape set
25-
//axis departs the shape into three parts: prev, cur and post. They are all product of shapes
26-
int prev = 1;
27-
int cur = 1;
28-
int post = 1;
29-
int size = 1; //total number of the elements for result
30-
//Calculate new Shape
31-
for (int i = 0; i < nd.ndim; i++)
32-
{
33-
if (i == axis)
34-
cur = nd.shape.Shapes[i];
35-
else
36-
{
37-
resShapes[index++] = nd.shape.Shapes[i];
38-
size *= nd.shape.Shapes[i];
39-
if (i < axis)
40-
prev *= nd.shape.Shapes[i];
41-
else
42-
post *= nd.shape.Shapes[i];
43-
}
44-
}
45-
res.Storage.Shape = new Shape(resShapes);
46-
//Fill in data
47-
index = 0; //index for result data set
48-
int sameSetOffset = nd.shape.DimOffset[axis.Value];
49-
int increments = cur * post;
50-
int start = 0;
51-
double min = 0;
52-
for (int i = 0; i < nd.size; i += increments)
53-
{
54-
for (int j = i; j < i + post; j++)
55-
{
56-
start = j;
57-
min = nd.Data<double>()[start];
58-
for (int k = 0; k < cur; k++)
59-
{
60-
min = Math.Max(min, nd.Data<double>()[start]);
61-
start += sameSetOffset;
62-
}
63-
res[index++] = min;
64-
}
65-
}
66-
}
67-
68-
return res;
12+
return nd.amax(axis);
6913
}
7014
}
7115
}

src/NumSharp.Core/Math/NumPy.amin.cs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,7 @@ public partial class NumPy
99
{
1010
public NDArray amin(NDArray nd, int? axis = null)
1111
{
12-
NDArray res = null;
13-
14-
if (axis == null)
15-
{
16-
res = new NDArray(nd.dtype,new Shape(new int[] { 1 }));
17-
18-
Array resArray = Array.CreateInstance(nd.dtype,1);
19-
resArray.SetValue(nd.Storage.GetData<double>().Min(),0);
20-
21-
res.Storage.SetData(resArray);
22-
23-
}
24-
else
25-
{
26-
if (axis < 0 || axis >= nd.ndim)
27-
throw new Exception("Invalid input: axis");
28-
int[] resShapes = new int[nd.ndim - 1];
29-
int index = 0; //index for result shape set
30-
//axis departs the shape into three parts: prev, cur and post. They are all product of shapes
31-
int prev = 1;
32-
int cur = 1;
33-
int post = 1;
34-
int size = 1; //total number of the elements for result
35-
//Calculate new Shape
36-
for (int i = 0; i < nd.ndim; i++)
37-
{
38-
if (i == axis)
39-
cur = nd.shape.Shapes[i];
40-
else
41-
{
42-
resShapes[index++] = nd.shape.Shapes[i];
43-
size *= nd.shape.Shapes[i];
44-
if (i < axis)
45-
prev *= nd.shape.Shapes[i];
46-
else
47-
post *= nd.shape.Shapes[i];
48-
}
49-
}
50-
51-
//Fill in data
52-
index = 0; //index for result data set
53-
int sameSetOffset = nd.shape.DimOffset[axis.Value];
54-
int increments = cur * post;
55-
int start = 0;
56-
double min = 0;
57-
58-
res = new NDArray(nd.dtype,new Shape(resShapes));
59-
60-
for (int i = 0; i < nd.size; i += increments)
61-
{
62-
for (int j = i; j < i + post; j++)
63-
{
64-
start = j;
65-
min = nd.Data<double>()[start];
66-
for (int k = 0; k < cur; k++)
67-
{
68-
min = Math.Min(min, nd.Data<double>()[start]);
69-
start += sameSetOffset;
70-
}
71-
res.Data<double>()[index++] = min;
72-
}
73-
}
74-
}
75-
76-
return res;
12+
return nd.amin(axis);
7713
}
7814
}
7915
}

0 commit comments

Comments
 (0)