commit last state
[ppam-mpi.git] / code / src / Algorithm / get_dissimilarities.c
1 #include "Util/types.h"
2 #include <stdlib.h>
3 #include <math.h>
4
5 // compute L^p dissimilarities for a nxm matrix
6 Real* get_dissimilarities_intra(Real* samples, uint32_t nbSamples, uint32_t nbValues, uint32_t p)
7 {
8 Real* dissimilarities = (Real*) malloc(nbSamples*nbSamples*sizeof(Real));
9 for (uint32_t i=0; i<nbSamples; i++)
10 {
11 dissimilarities[i*nbSamples+i] = 0.0;
12 for (uint32_t j=0; j<i; j++)
13 {
14 // dissimilarities[i*nbSamples+j] = L^p distance between reduced rows i and j
15 double dissim = 0.0;
16 for (uint32_t m=0; m<nbValues; m++)
17 {
18 double delta = fabs(samples[i*nbValues+m] - samples[j*nbValues+m]);
19 dissim += pow(delta, p);
20 }
21 dissimilarities[i*nbSamples+j] = pow(dissim, 1.0/p);
22 dissimilarities[j*nbSamples+i] = dissimilarities[i*nbSamples+j];
23 }
24 }
25 return dissimilarities;
26 }
27
28 // compute L^p dissimilarities between rows of 2 matrices
29 Real* get_dissimilarities_inter(Real* mat1, uint32_t n1, Real* mat2, uint32_t n2,
30 uint32_t nbValues, uint32_t p)
31 {
32 Real* dissimilarities = (Real*) malloc(n1*n2*sizeof(Real));
33 for (uint32_t i=0; i<n1; i++)
34 {
35 for (uint32_t j=0; j<n2; j++)
36 {
37 double dissim = 0.0;
38 for (uint32_t m=0; m<nbValues; m++)
39 {
40 double delta = fabs(mat1[i*nbValues+m] - mat2[j*nbValues+m]);
41 dissim += pow(delta, p);
42 }
43 dissimilarities[i*n2+j] = pow(dissim, 1.0/p);
44 }
45 }
46 return dissimilarities;
47 }