-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
142 lines (122 loc) · 3.85 KB
/
Utils.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Linq;
namespace Nni {
class RandomNumberGenerator
{
private Random random;
public RandomNumberGenerator(int seed = 0)
{
random = new Random(seed);
}
public int Integer(int high)
{
return random.Next(high);
}
public double Uniform(double low, double high)
{
return random.NextDouble() * (high - low) + low;
}
public double Normal(double location, double scale)
{
double u = 1 - Uniform(0, 1);
double v = 1 - Uniform(0, 1);
double std = Math.Sqrt(-2.0 * Math.Log(u)) * Math.Sin(2.0 * Math.PI * v);
return location + std * scale;
}
public int Categorical(double[] possibility)
{
double x = Uniform(0, 1);
for (int i = 0; i < possibility.Length; i++) {
x -= possibility[i];
if (x < 0) { return i; }
}
return possibility.Length - 1;
}
public int[] Categorical(double[] possibility, int size)
{
int[] ret = new int[size];
for (int i = 0; i < ret.Length; i++) {
ret[i] = Categorical(possibility);
}
return ret;
}
}
class ArrayMath{
/* x + y */
public static double[] Add(double[] xArray, double y)
{
return xArray.Select(x => x + y).ToArray();
}
/* np.argmax */
public static int ArgMax(double[] array)
{
int index = 0;
for (int i = 1; i < array.Length; i++) {
if (array[i] > array[index]) {
index = i;
}
}
return index;
}
/* np.argsort */
public static int[] ArgSort(double[] array)
{
return Enumerable.Range(0, array.Length).OrderBy(index => array[index]).ToArray();
}
/* np.clip */
public static double[] Clip(double[] xArray, double min, double max)
{
return xArray.Select(x => Math.Min(Math.Max(x, min), max)).ToArray();
}
/* x / y */
public static double[] Div(double[] xArray, double y)
{
return xArray.Select(x => x / y).ToArray();
}
/* x / np.sum(x) */
public static double[] DivSum(double[] xArray)
{
return Div(xArray, xArray.Sum());
}
/* x[y] */
public static double[] Index(double[] array, int[] indices)
{
return indices.Select(index => array[index]).ToArray();
}
/* List.Insert */
public static double[] Insert(double[] array, int index, double item)
{
double[] ret = new double[array.Length + 1];
Array.Copy(array, 0, ret, 0, index);
ret[index] = item;
Array.Copy(array, index, ret, index + 1, array.Length - index);
return ret;
}
/* np.log */
public static double[] Log(double[] xArray)
{
return xArray.Select(x => Math.Log(x)).ToArray();
}
/* x * y */
public static double[] Mul(double[] xArray, double[] yArray)
{
return Enumerable.Zip(xArray, yArray, (x, y) => x * y).ToArray();
}
/* np.searchsorted */
public static int SearchSorted(double[] array, double item)
{
int index = Array.BinarySearch(array, item);
return index >= 0 ? index : ~index;
}
/* x ^ 2 */
public static double Square(double x)
{
return x * x;
}
/* x - y */
public static double[] Sub(double[] xArray, double[] yArray)
{
return Enumerable.Zip(xArray, yArray, (x, y) => x - y).ToArray();
}
}
}