This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPoint.java
124 lines (104 loc) · 3.2 KB
/
Point.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package it.unipi.hadoop.model;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable;
public class Point implements Writable {
private float[] components = null;
private int dim;
private int numPoints; // For partial sums
public Point() {
this.dim = 0;
}
public Point(final float[] c) {
this.set(c);
}
public Point(final String[] s) {
this.set(s);
}
public static Point copy(final Point p) {
Point ret = new Point(p.components);
ret.numPoints = p.numPoints;
return ret;
}
public void set(final float[] c) {
this.components = c;
this.dim = c.length;
this.numPoints = 1;
}
public void set(final String[] s) {
this.components = new float[s.length];
this.dim = s.length;
this.numPoints = 1;
for (int i = 0; i < s.length; i++) {
this.components[i] = Float.parseFloat(s[i]);
}
}
@Override
public void readFields(final DataInput in) throws IOException {
this.dim = in.readInt();
this.numPoints = in.readInt();
this.components = new float[this.dim];
for(int i = 0; i < this.dim; i++) {
this.components[i] = in.readFloat();
}
}
@Override
public void write(final DataOutput out) throws IOException {
out.writeInt(this.dim);
out.writeInt(this.numPoints);
for(int i = 0; i < this.dim; i++) {
out.writeFloat(this.components[i]);
}
}
@Override
public String toString() {
StringBuilder point = new StringBuilder();
for (int i = 0; i < this.dim; i++) {
point.append(Float.toString(this.components[i]));
if(i != dim - 1) {
point.append(",");
}
}
return point.toString();
}
public void sum(Point p) {
for (int i = 0; i < this.dim; i++) {
this.components[i] += p.components[i];
}
this.numPoints += p.numPoints;
}
public float distance(Point p, int h){
if (h < 0) {
// Consider only metric distances
h = 2;
}
if (h == 0) {
// Chebyshev
float max = -1f;
float diff = 0.0f;
for (int i = 0; i < this.dim; i++) {
diff = Math.abs(this.components[i] - p.components[i]);
if (diff > max) {
max = diff;
}
}
return max;
} else {
// Manhattan, Euclidean, Minkowsky
float dist = 0.0f;
for (int i = 0; i < this.dim; i++) {
dist += Math.pow(Math.abs(this.components[i] - p.components[i]), h);
}
dist = (float)Math.round(Math.pow(dist, 1f/h)*100000)/100000.0f;
return dist;
}
}
public void average() {
for (int i = 0; i < this.dim; i++) {
float temp = this.components[i] / this.numPoints;
this.components[i] = (float)Math.round(temp*100000)/100000.0f;
}
this.numPoints = 1;
}
}