-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetrics.java
57 lines (41 loc) · 1.13 KB
/
Metrics.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
import ijaux.datatype.ProductSpace;
import ijaux.hypergeom.index.BaseIndex;
import ijaux.hypergeom.index.Indexing;
public class Metrics<I extends BaseIndex> {
double [][] g;
double ndims;
public Metrics(int n) {
g=new double[n][n];
ndims=n;
}
public void transpose() {
for (int i=0; i< ndims; i++) {
for (int j=i+1; j< ndims; j++) {
g[j][i]=g[i][j];
}
}
}
public double OrthogonalDistance(I a, I b) {
int[] ac=a.getCoordinates();
int[] bc=b.getCoordinates();
double d=0;
if( ac.length!=bc.length || ac.length!=ndims)
throw new IllegalArgumentException("dimensions do not match");
for (int i=0;i<ndims; i++) {
d+=g[i][i]*(ac[i]-bc[i])*(ac[i]-bc[i]);
}
return Math.sqrt(d);
}
public double distance(I a, I b) {
int[] ac=a.getCoordinates();
int[] bc=b.getCoordinates();
double d=0;
if( ac.length!=bc.length || ac.length!=ndims)
throw new IllegalArgumentException("dimensions do not match");
for (int i=0;i<ndims; i++)
for (int j=i;i<ndims; i++) { // makes use of the symmetry
d+=g[i][j]*(ac[i] - bc[i])*(ac[j] - bc[j]);
}
return Math.sqrt(d);
}
}