-
Notifications
You must be signed in to change notification settings - Fork 0
/
TB.java
108 lines (100 loc) · 2.55 KB
/
TB.java
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
package SOM;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
//This is a toolbox where I store all commonly needed functions
public class TB {
private static FileWriter writer;
private static boolean log_called;
public static double activate(double netVal){
return 1/(1+Math.pow(Math.E, -netVal));
}
public static void ShowArray(double[] arr){
System.out.print("{");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.print("}");
}
public static double distance(double[] a,double[] b){
if(a.length!=b.length){
System.out.print("Error in Function distance: Arrays have different Dimension");
return 0;
}
double sum=0;
for(int i=0;i<a.length;i++){
sum+=Math.pow(a[i]-b[i],2);
}
sum=Math.sqrt(sum);
return sum;
}
public static double distance(int[] a, int[] b){
return Math.sqrt(Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));
}
public static double[] To1D(double[][] in,int dim,int index){
double[] out=new double[dim];
for(int i=0;i<dim;i++){
out[i]=in[index][i];
}
return out;
}
public static double[] Normalize(double[] vec){
double[] res= new double[vec.length];
double c=0;
for(int i=0;i<vec.length;i++){
c+=vec[i]*vec[i];
}
c=1/Math.sqrt(c);
for(int i=0;i<vec.length;i++){
res[i]=c*vec[i];
}
return res;
}
public static void log(String msg){
Date current;
DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
if(!log_called){
try {
current=new Date();
writer = new FileWriter("log-"+df.format(current), true);
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log_called=true;
}
try {
current=new Date();
writer.write(current+": "+msg+"\n");
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closelog(){
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}