-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathToolbox.java
More file actions
208 lines (168 loc) · 4.8 KB
/
MathToolbox.java
File metadata and controls
208 lines (168 loc) · 4.8 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package ImageAnalysisLibrary;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
public class MathToolbox {
public double getMean(int[] Data) {
double sum =0;
for(int i = 0 ; i < Data.length;i++) {
sum+=Data[i];
}
return sum/Data.length;
}
public double getMedian(int[] Data) {
int tmp[];
tmp = Data.clone();
Arrays.sort(tmp);
if (tmp.length % 2 == 0) {
return (double)(tmp[tmp.length / 2] + (double)tmp[(tmp.length / 2) - 1]) / 2;
}
else {
return tmp[tmp.length / 2];
}
}
public double getStandard_Deviation(int[] Data) {
double mean = this.getMean(Data);
double sum = 0;
for (int i = 0; i < Data.length; i++) {
sum += ((Data[i] - mean)*(Data[i] - mean));
}
sum /= (double)Data.length;
sum = Math.sqrt(sum);
return sum;
}
public double getVariance(int[] Data) {
double Deviation = getStandard_Deviation(Data);
return (Deviation)*(Deviation);
}
public double[] getRank_Array(int[] Data) {
double R[] = new double[Data.length];
for (int i = 0; i < Data.length; i++) {
int r = 1, s = 1;
for (int j = 0; j < Data.length; j++)
{
if (j != i && Data[j] < Data[i])
r += 1;
if (j != i && Data[j] == Data[i])
s += 1;
}
R[i] = r + (double)(s - 1) / (double)2;
}
return R;
}
public double get_Min_Of_Arrayd(double Data[]) {
double min =Double.MAX_VALUE;
for(int i =1 ;i<Data.length;i++) {
if(Data[i]<min) {
min = Data[i];
}
}
return min;
}
public double nthRoot(int A, int N){
double xPre = Math.random() % 10;
double eps = 0.001;
double delX = 2147483647;
double xK = 0.0;
while (delX > eps){
xK = ((N - 1.0) * xPre +
(double)A / Math.pow(xPre, N - 1)) / (double)N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
public int getMax_From_MultiyD(ArrayList<ArrayList<Integer>> data) {
int max = Integer.MIN_VALUE;
for(int i = 0 ; i < data.size();i++) {
for(int j =0;j<data.get(i).size();j++) {
ArrayList<Integer> temp = data.get(i);
if(temp.get(j) > max) {
max = temp.get(j);
}
}
}
return max;
}
public int getMin_From_MultiyD(ArrayList<ArrayList<Integer>> data) {
int min = Integer.MAX_VALUE;
for(int i = 0 ; i < data.size();i++) {
for(int j =0;j<data.get(i).size();j++) {
ArrayList<Integer> temp = data.get(i);
if(temp.get(j) < min) {
min = temp.get(j);
}
}
}
return min;
}
public int getMax_From_ArrayList(ArrayList<Integer> data) {
int max = Integer.MIN_VALUE;
for(int j =0;j<data.size();j++) {
if(data.get(j) > max) {
max = data.get(j);
}
}
return max;
}
public int get_array_max(int arr[]) {
int max = Integer.MIN_VALUE;
for(int j =0;j<arr.length;j++) {
if(arr[j] > max) {
max = arr[j];
}
}
return max;
}
public static int random_int_in_range(int lower,int upper) {
int rand = ThreadLocalRandom.current().nextInt(lower,upper);
return rand;
}
public static double random_double_in_range(double lower,double upper) {
double rand = ThreadLocalRandom.current().nextDouble(lower,upper);
return rand;
}
public float Remap(float value, float fromMin, float fromMax, float toMin, float toMax){
var fromAbs = value - fromMin;
var fromMaxAbs = fromMax - fromMin;
var normal = fromAbs / fromMaxAbs;
var toMaxAbs = toMax - toMin;
var toAbs = toMaxAbs * normal;
var to = toAbs + toMin;
return to;
}
// Get a random numbers between min and max
public static float RandomFloat(float min, float max) {
float a = (float) Math.random();
float num = min + (float) Math.random() * (max - min);
if(a < 0.5)
return num;
else
return -num;
}
// Sigmoid function
public static double Sigmoid(double x) {
return (float) (1/(1+Math.pow(Math.E, -x)));
}
// Derivative of the sigmoid function
public static double SigmoidDerivative(double x) {
return Sigmoid(x)*(1-Sigmoid(x));
}
public static double squaredError(double output,double target) {
return (float) (0.5*Math.pow(2,(target-output)));
}
public static double sumSquaredError(double[] outputs,double[] targets) {
double sum = 0;
for(int i=0;i<outputs.length;i++) {
sum += squaredError(outputs[i],targets[i]);
}
return sum;
}
public static double Rectified(double x) {
if(x >= 0) {
return x;
}else {
return 0;
}
}
}