forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdArray.AMin.cs
More file actions
70 lines (69 loc) · 2.55 KB
/
NdArray.AMin.cs
File metadata and controls
70 lines (69 loc) · 2.55 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumSharp.Core.Extensions
{
public static partial class NDArrayExtensions
{
public static NDArray<double> AMin(this NDArray<double> np, int? axis = null)
{
NDArray<double> res = new NDArray<double>();
if (axis == null)
{
res.Data = new double[1] { np.Data.Min() };
res.Shape = new Shape(new int[] { 1 });
}
else
{
if (axis < 0 || axis >= np.Shape.Length)
throw new Exception("Invalid input: axis");
int[] resShapes = new int[np.Shape.Shapes.Count - 1];
int index = 0; //index for result shape set
//axis departs the shape into three parts: prev, cur and post. They are all product of shapes
int prev = 1;
int cur = 1;
int post = 1;
int size = 1; //total number of the elements for result
//Calculate new Shape
for (int i = 0; i < np.Shape.Shapes.Count; i++)
{
if (i == axis)
cur = np.Shape.Shapes[i];
else
{
resShapes[index++] = np.Shape.Shapes[i];
size *= np.Shape.Shapes[i];
if (i < axis)
prev *= np.Shape.Shapes[i];
else
post *= np.Shape.Shapes[i];
}
}
res.Shape = new Shape(resShapes);
//Fill in data
index = 0; //index for result data set
int sameSetOffset = np.Shape.DimOffset[axis.Value];
int increments = cur * post;
res.Data = new double[size];
int start = 0;
double min = 0;
for (int i = 0; i < np.Size; i += increments)
{
for (int j = i; j < i + post; j++)
{
start = j;
min = np.Data[start];
for (int k = 0; k < cur; k++)
{
min = Math.Min(min, np.Data[start]);
start += sameSetOffset;
}
res.Data[index++] = min;
}
}
}
return res;
}
}
}