-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathL2_distance.c
47 lines (41 loc) · 1.29 KB
/
L2_distance.c
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
#include "matrix.h"
#include <stdlib.h>
/*===========================================================================
* l2_disatnce
* Given a two matrices with identical widths (i.e. same dimensionality),
* this method returns an a->height by b->height matrix of the l2-norm
* distance measures (without the square root calculation)
*
* if a and b are vectors, the l2-norm is given by:
* ||a - b|| = sqrt( ||a||^2 + ||b||^2 - 2 * a * b )
*=========================================================================*/
matrix* L2_distance(matrix* a, matrix* b) {
matrix* out;
matrix* aa;
matrix* bb;
matrix* bt;
matrix* ab;
int i, j;
double* ptrAB;
double* ptrOut;
assert(a->width == b->width, "Matrices a and b must be the same dimensionality.");
out = makeMatrix(b->height, a->height);
aa = dotDiagonalMatrix(a, NULL);
bb = dotDiagonalMatrix(b, NULL);
bt = transposeMatrix(b);
ab = multiplyMatrix(a, bt);
ptrOut = out->data;
ptrAB = ab->data;
for (i = 0; i < a->height; i++) {
for (j = 0; j < b->height; j++) {
*ptrOut = aa->data[i] + bb->data[j] - 2 * *ptrAB;
ptrOut++;
ptrAB++;
}
}
freeMatrix(aa);
freeMatrix(bb);
freeMatrix(bt);
freeMatrix(ab);
return out;
}