1 package net.obsearch.dimension;
2 import net.obsearch.exception.IllegalIdException;
3 import net.obsearch.exception.OBException;
4 import net.obsearch.ob.OBDouble;
5 import net.obsearch.Index;
6 import java.util.Random;
7 import net.obsearch.exception.OBStorageException;
8 import cern.colt.list.LongArrayList;
9 import java.util.LinkedList;
10 import java.util.Iterator;
11 import java.util.List;
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 public class DimensionDouble extends AbstractDimension implements Comparable<DimensionDouble>{
37
38
39
40
41 private double value;
42
43
44
45
46
47
48 public DimensionDouble(int position, double value){
49 super(position);
50 this.value = value;
51 }
52
53
54
55
56
57
58
59 public static DimensionDouble[] getTuple(OBDouble[] pivots, OBDouble object
60 ) throws
61 IllegalIdException, IllegalAccessException, InstantiationException,
62 OBException
63
64 {
65 DimensionDouble[] res = new DimensionDouble [pivots.length];
66 int i = 0;
67 for (OBDouble p : pivots) {
68 res[i] = new DimensionDouble(i, p.distance(object));
69 i++;
70 }
71 return res;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static long[] select(int k, Random r, LongArrayList source,
94 Index<OBDouble> index, LongArrayList excludes, double minDistance) throws IllegalIdException, OBException, IllegalAccessException, InstantiationException {
95 int max = max(source, index);
96 long[] res = new long[k];
97 int i = 0;
98 List<Long> l = new LinkedList<Long>();
99
100 while (i < res.length) {
101 long id = mapId(r.nextInt(max), source);
102 if ((excludes == null || !excludes.contains(id)) && minDistance(l, index) > minDistance ) {
103 res[i] = id;
104 l.add(id);
105 } else {
106 l.remove(l.size() -1);
107 continue;
108 }
109 i++;
110 }
111 return res;
112 }
113
114
115
116
117
118
119
120
121
122
123 public static double minDistance(List<Long> objects, Index<OBDouble> index)throws IllegalIdException, OBException, IllegalAccessException, InstantiationException{
124 double res = Double.MAX_VALUE;
125 Iterator<Long> it = objects.iterator();
126 for(long l1 : objects){
127 for(long l2 : objects){
128 if(l1 == l2){
129 continue;
130 }
131 res = (double)Math.min(res, index.getObject(l1).distance(index.getObject(l2)));
132 }
133 }
134 return res;
135 }
136
137
138
139
140
141
142
143 public static double[] getBasicTuple(OBDouble[] pivots, OBDouble object
144 ) throws
145 IllegalIdException, IllegalAccessException, InstantiationException,
146 OBException
147
148 {
149 double[] res = new double [pivots.length];
150 int i = 0;
151 for (OBDouble p : pivots) {
152 res[i] = p.distance(object);
153 i++;
154 }
155 return res;
156 }
157
158
159
160
161
162 public static DimensionDouble[] getTuple(long[] pivots, long objectId,
163 Index<? extends OBDouble> index) throws
164 IllegalIdException, IllegalAccessException, InstantiationException,
165 OBException{
166 DimensionDouble[] res = new DimensionDouble [pivots.length];
167 OBDouble o = index.getObject(objectId);
168 int i = 0;
169 for(long pivotId : pivots){
170 res[i] = new DimensionDouble(i, index.getObject(pivotId).distance(o));
171 i++;
172 }
173 return res;
174 }
175
176
177
178
179
180
181 public int compareTo(DimensionDouble other){
182 if(value < other.value){
183 return -1;
184 }else if(value > other.value){
185 return 1;
186 }else{
187 return 0;
188 }
189 }
190
191
192 public double getValue(){
193 return value;
194 }
195
196
197
198
199
200
201
202
203
204 public static double[] getPrimitiveTuple(long[] pivots, long objectId,
205 Index<? extends OBDouble> index) throws
206 IllegalIdException, IllegalAccessException, InstantiationException,
207 OBException
208
209 {
210 double[] res = new double[pivots.length];
211 int i = 0;
212 OBDouble o = index.getObject(objectId);
213 for (long pivotId : pivots) {
214 res[i] = index.getObject(pivotId).distance(o);
215 i++;
216 }
217 return res;
218 }
219
220
221
222
223
224
225 public static DimensionDouble[] transformPrimitiveTuple(double[] tuple){
226 DimensionDouble[] res = new DimensionDouble[tuple.length];
227 int i = 0;
228 while(i < tuple.length){
229 res[i] = new DimensionDouble(i, tuple[i]);
230 i++;
231 }
232 return res;
233 }
234
235
236
237
238
239
240
241
242 public static double[] getPrimitiveTuple(OBDouble[] pivots, OBDouble object
243 ) throws
244 IllegalIdException, IllegalAccessException, InstantiationException,
245 OBException
246
247 {
248 double[] res = new double[pivots.length];
249 int i = 0;
250 for (OBDouble p : pivots) {
251 res[i] = p.distance(object);
252 i++;
253 }
254 return res;
255 }
256
257
258
259
260
261
262
263 public static double lInfinite(double[] a, double[] b){
264 assert a.length == b.length;
265 double max = Double.NEGATIVE_INFINITY
266 ;
267 int i = 0;
268 while(i < a.length){
269
270 double t = (double)(a[i] - b[i]);
271 t = t < 0 ? (double)-t : t;
272 if(t > max){
273 max = t;
274 }
275 i++;
276 }
277 return max;
278 }
279
280
281
282
283
284
285
286
287 public static double euclidean(double[] a, double[] b){
288 assert a.length == b.length;
289 double result = 0;
290 int i = 0;
291 while(i < a.length){
292 double j = a[i] - b[i];
293 result += j * j;
294 i++;
295 }
296 return (double)Math.sqrt(result);
297 }
298
299 }
300
301