-
Notifications
You must be signed in to change notification settings - Fork 1
/
DistanceMethod.cpp
388 lines (328 loc) · 10.1 KB
/
DistanceMethod.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <cfloat>
#include <climits>
#include "DistanceMethod.h"
#include "DistanceMatrix.h"
#ifdef _MSC_VER
#include <cmath>
namespace cfp_internal
{
double cbrt(double x)
{
// Check for simple cases:
if(x == 0.)
return 0.;
else if(x == 1.)
return 1.;
else if(x == -1.)
return -1.;
else
{
double y; // Guess
double g = fabs(x); // Do this guess on a positive number
double l = g * 1E-14; // The limit for optimal guess
// the multiplication with x (its magnitude) should
// ensure no infinite loops, at the cost
// of some precision on high numbers.
// Make initial guess:
if(g < 1)
y = x;
else if(g < 10)
y = x / 3;
else if(g < 20)
y = x / 6;
else if(g < 50)
y = x / 10;
else if(g < 100)
y = x / 20;
else if(g < 1000)
y = x / 50;
else if(g < 5000)
y = x / 100;
else if(g < 10000)
y = x / 500;
else if(g < 50000)
y = x / 1000;
else if(g < 100000)
y = x / 50000;
else
y = x / 100000;
// Improve guess immediately:
y = ((x / (y * y)) + 2. * y) / 3.; // Newton's approx. for new guess
double d = fabs(y * y * y - x); // Calculate difference (Last difference of y^3 and x)
while(l < d)
{
y = ((x / (y * y)) + 2. * y) / 3.; // Newton's approx. for new guess
d = fabs(y * y * y - x); // Calculate difference
}
return y;
}
}
}
#endif
using namespace cfp_internal;
float CosineDistance::computeDistance(const Structure& aStructure1, const Structure& aStructure2) const
{
unsigned int i;
unsigned int nsect1 = aStructure1.mFingerprintNumSections;
unsigned int sectlen1 = aStructure1.mFingerprintSectionLen;
// Only one section
if(nsect1 == 1)
{
double distance = 0.;
double a_norm = 0.;
double b_norm = 0.;
for(i=0; i < sectlen1; ++i)
{
distance += aStructure1.mFingerprint[i] * aStructure2.mFingerprint[i];
a_norm += aStructure1.mFingerprint[i] * aStructure1.mFingerprint[i];
b_norm += aStructure2.mFingerprint[i] * aStructure2.mFingerprint[i];
}
distance /= sqrt(a_norm*b_norm);
distance = (1. - distance)/2.;
return (float)distance;
}
// More than one section, created by pseudo diffraction per atom
if(!aStructure1.mWeights.empty())
{
double distance = 0.0;
double a_norm = 0.0;
double b_norm = 0.0;
for(unsigned int sect=0; sect < nsect1; ++sect)
{
float w1 = aStructure1.mWeights[sect];
float w2 = aStructure2.mWeights[sect];
for(i=0; i < sectlen1; ++i)
{
distance += aStructure1.mFingerprint[i+sectlen1*sect] * aStructure2.mFingerprint[i+sectlen1*sect] * w1 * w2;
a_norm += aStructure1.mFingerprint[i+sectlen1*sect] * aStructure1.mFingerprint[i+sectlen1*sect] * w1 * w1;
b_norm += aStructure2.mFingerprint[i+sectlen1*sect] * aStructure2.mFingerprint[i+sectlen1*sect] * w2 * w2;
}
}
distance /= sqrt(a_norm*b_norm);
return (float)((1. - distance)/2.);
}
// Distances per atom
unsigned int nsect2 = aStructure2.mFingerprintNumSections;
// Mark atoms already paired
bool* a_atom_used = new bool[nsect1];
bool* b_atom_used = new bool[nsect2];
for(i=0; i < nsect1; ++i) a_atom_used[i] = false;
for(i=0; i < nsect2; ++i) b_atom_used[i] = false;
// For each section find the most similar section in the other structure
double distance = 0.;
for(i=0; i < nsect1; ++i)
{
// If already paired, skip it
if(a_atom_used[i]) continue;
double curr_distance = DBL_MAX;
unsigned int curr_min_idx = UINT_MAX;
unsigned int z1 = aStructure1.mAtomZ[i];
for(unsigned int j=0; j < nsect2; ++j)
{
// If already paired, skip it
if(b_atom_used[j]) continue;
unsigned int z2 = aStructure2.mAtomZ[j];
// If different type, skip it
if(z1 != z2) continue;
// Compute the distance
double one_distance = 0.0;
double a_norm = 0.0;
double b_norm = 0.0;
for(unsigned int k=0; k < sectlen1; ++k)
{
one_distance += aStructure1.mFingerprint[i*sectlen1+k] * aStructure2.mFingerprint[j*sectlen1+k];
a_norm += aStructure1.mFingerprint[i*sectlen1+k] * aStructure1.mFingerprint[i*sectlen1+k];
b_norm += aStructure2.mFingerprint[j*sectlen1+k] * aStructure2.mFingerprint[j*sectlen1+k];
}
one_distance /= sqrt(a_norm*b_norm);
one_distance = (1. - one_distance)/2.;
// Find the most similar atom in the other structure
if(one_distance < curr_distance)
{
curr_distance = one_distance;
curr_min_idx = j;
}
}
// If a pairing has been found
if(curr_min_idx < UINT_MAX)
{
distance += curr_distance;
a_atom_used[i] = true;
b_atom_used[curr_min_idx] = true;
}
}
delete [] a_atom_used;
delete [] b_atom_used;
return (float)distance;
}
float EuclideanDistance::computeDistance(const Structure& aStructure1, const Structure& aStructure2) const
{
unsigned int i;
unsigned int nsect1 = aStructure1.mFingerprintNumSections;
unsigned int sectlen1 = aStructure1.mFingerprintSectionLen;
if(nsect1 == 1)
{
double distance = 0.;
for(i=0; i < sectlen1; ++i)
{
double d = aStructure1.mFingerprint[i] - aStructure2.mFingerprint[i];
distance += d*d;
}
return (float)sqrt(distance);
}
// More than one section, created by pseudo diffraction per atom
if(!aStructure1.mWeights.empty())
{
double distance = 0.;
for(unsigned int sect=0; sect < nsect1; ++sect)
{
double sdistance = 0.;
for(i=0; i < sectlen1; ++i)
{
double d = aStructure1.mFingerprint[i+sectlen1*sect] - aStructure2.mFingerprint[i+sectlen1*sect];
sdistance += d*d;
}
distance += sqrt(sdistance*aStructure1.mWeights[sect]*aStructure2.mWeights[sect]);
}
return (float)distance;
}
// Distances per atom
unsigned int nsect2 = aStructure2.mFingerprintNumSections;
unsigned int sectlen2 = aStructure2.mFingerprintSectionLen;
// Mark atoms already paired
bool* a_atom_used = new bool[nsect1];
bool* b_atom_used = new bool[nsect2];
for(i=0; i < nsect1; ++i) a_atom_used[i] = false;
for(i=0; i < nsect2; ++i) b_atom_used[i] = false;
// For each section find the most similar section in the other structure
double distance = 0.;
for(i=0; i < nsect1; ++i)
{
// If already paired, skip it
if(a_atom_used[i]) continue;
double curr_distance = DBL_MAX;
unsigned int curr_min_idx = UINT_MAX;
unsigned int z1 = aStructure1.mAtomZ[i];
for(unsigned int j=0; j < nsect2; ++j)
{
// If already paired, skip it
if(b_atom_used[j]) continue;
unsigned int z2 = aStructure2.mAtomZ[j];
// If different type, skip it
if(z1 != z2) continue;
// Compute the distance
double one_distance = 0.0;
for(unsigned int k=0; k < sectlen1; ++k)
{
double d = aStructure1.mFingerprint[i+sectlen1*k] - aStructure2.mFingerprint[j+sectlen2*k];
one_distance += d*d;
}
one_distance = sqrt(one_distance);
// Find the most similar atom in the other structure
if(one_distance < curr_distance)
{
curr_distance = one_distance;
curr_min_idx = j;
}
}
// If a pairing has been found
if(curr_min_idx < UINT_MAX)
{
distance += curr_distance;
a_atom_used[i] = true;
b_atom_used[curr_min_idx] = true;
}
}
delete [] a_atom_used;
delete [] b_atom_used;
return (float)distance;
}
float MinkowskiDistance::computeDistance(const Structure& aStructure1, const Structure& aStructure2) const
{
unsigned int i;
unsigned int nsect1 = aStructure1.mFingerprintNumSections;
unsigned int sectlen1 = aStructure1.mFingerprintSectionLen;
if(nsect1 == 1)
{
double distance = 0.;
for(i=0; i < sectlen1; ++i)
{
double d = aStructure1.mFingerprint[i] - aStructure2.mFingerprint[i];
if(d < 0.) d = -d;
distance += cbrt(d);
}
return (float)(distance*distance*distance);
}
// More than one section, created by pseudo diffraction per atom
if(!aStructure1.mWeights.empty())
{
double distance = 0.;
for(unsigned int sect=0; sect < nsect1; ++sect)
{
double sdistance = 0.;
for(i=0; i < sectlen1; ++i)
{
double d = aStructure1.mFingerprint[i+sectlen1*sect] - aStructure2.mFingerprint[i+sectlen1*sect];
if(d < 0.) d = -d;
sdistance += cbrt(d);
}
distance += sdistance*sdistance*sdistance*sqrt(aStructure1.mWeights[sect]*aStructure2.mWeights[sect]);
}
return (float)distance;
}
// Distances per atom
unsigned int nsect2 = aStructure2.mFingerprintNumSections;
unsigned int sectlen2 = aStructure2.mFingerprintSectionLen;
// Mark atoms already paired
bool* a_atom_used = new bool[nsect1];
bool* b_atom_used = new bool[nsect2];
for(i=0; i < nsect1; ++i) a_atom_used[i] = false;
for(i=0; i < nsect2; ++i) b_atom_used[i] = false;
// For each section find the most similar section in the other structure
double distance = 0.;
for(i=0; i < nsect1; ++i)
{
// If already paired, skip it
if(a_atom_used[i]) continue;
double curr_distance = DBL_MAX;
unsigned int curr_min_idx = UINT_MAX;
unsigned int z1 = aStructure1.mAtomZ[i];
for(unsigned int j=0; j < nsect2; ++j)
{
// If already paired, skip it
if(b_atom_used[j]) continue;
unsigned int z2 = aStructure2.mAtomZ[j];
// If different type, skip it
if(z1 != z2) continue;
// Compute the distance
double one_distance = 0.0;
for(unsigned int k=0; k < sectlen1; ++k)
{
double d = aStructure1.mFingerprint[i+sectlen1*k] - aStructure2.mFingerprint[j+sectlen2*k];
if(d < 0.) d = -d;
one_distance += cbrt(d);
}
one_distance = one_distance*one_distance*one_distance;
// Find the most similar atom in the other structure
if(one_distance < curr_distance)
{
curr_distance = one_distance;
curr_min_idx = j;
}
}
// If a pairing has been found
if(curr_min_idx < UINT_MAX)
{
distance += curr_distance;
a_atom_used[i] = true;
b_atom_used[curr_min_idx] = true;
}
}
delete [] a_atom_used;
delete [] b_atom_used;
return (float)distance;
}