summaryrefslogtreecommitdiff
path: root/lib/correlations.c
blob: 98c5767d0c067216b1ca268821ac09fcd204ca9c (plain)
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

#include "fracture.h"

double *get_corr(net_t *instance, uint_t **dists, cholmod_common *c) {
  uint_t nv = instance->graph->dnv;
  uint_t ne = instance->graph->ne;
  uint_t *ev = instance->graph->dev;
  bool nulldists = false;
  if (dists == NULL) {
    dists = graph_dists(instance->graph, false);
    nulldists = true;
  }
  double *corr = calloc(nv, sizeof(double));
  uint_t *numat = calloc(nv, sizeof(uint_t));

  for (uint_t i = 0; i < ne; i++) {
    uint_t v1 = ev[2 * i];
    uint_t v2 = ev[2 * i + 1];
    for (uint_t j = 0; j < ne; j++) {
      uint_t v3 = ev[2 * j];
      uint_t v4 = ev[2 * j + 1];
      uint_t dist1 = dists[v1][v3];
      uint_t dist2 = dists[v1][v4];
      uint_t dist3 = dists[v2][v3];
      uint_t dist4 = dists[v2][v4];
      uint_t dist = (dist1 + dist2 + dist3 + dist4) / 4;
      corr[dist] += instance->fuses[i] && instance->fuses[j];
      numat[dist]++;
    }
  }

  for (uint_t i = 0; i < nv; i++) {
    if (numat[i] > 0) {
      corr[i] /= numat[i];
    }
  }

  if (nulldists) {
    for (int i = 0; i < nv; i++) {
      free(dists[i]);
    }
    free(dists);
  }

  return corr;
}