<commit_before>
#include <stdio.h>
#include <stdlib.h>
#include "force-atlas-2.h"
#include "math.h"
#include "vector.h"

#define K_R 1.0
#define K_S 0.1
#define K_G 5
#define K_SMAX 10.0
#define TAU 1.0
#define EPSILON 0.1
#define FLOAT_EPSILON 0.0000001

// Calculate the number of neighbours for every node.
void calcNumNeighbours(Graph*, int*);
// Gravity force
void fa2Gravity(Graph*, float*, float*, int*);
// Repulsion between vertices
void fa2Repulsion(Graph*, float*, float*, int*);
// Attraction on edges
void fa2Attraction(Graph*, float*, float*);

void fa2UpdateSwing(Graph*, float*, float*, float*, float*, float*);
void fa2UpdateTract(Graph*, float*, float*, float*, float*, float*);
void fa2UpdateSwingGraph(Graph*, float*, int*, float*);
void fa2UpdateTractGraph(Graph*, float*, int*, float*);
void fa2UpdateSpeedGraph(float, float, float*);
void fa2UpdateSpeed(Graph*, float*, float*, float*, float*, float);
void fa2UpdateDisplacement(Graph*, float*, float*, float*, float*, float*);
void fa2SaveOldForces(Graph*, float*, float*, float*, float*);
void fa2UpdateLocation(Graph*, float*, float*);

void calcNumNeighbours(Graph* g, int* deg)
{
  for (size_t i = 0; i < g->numedges; i++)
  {
    deg[g->edgeSources[i]]++; 
  }
}

void fa2Gravity(Graph* g, float* forceX, float* forceY, int* deg)
{
  if (!g) return;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float vx = g->vertexXLocs[i];
    float vy = g->vertexYLocs[i];
    vectorNormalize(&vx, &vy);
    vectorInverse(&vx, &vy);
    vectorMultiply(&vx, &vy, K_G * (deg[i] + 1));
    vectorAdd(&forceX[i], &forceY[i], vx, vy);
  }
}

void fa2Repulsion(Graph* g, float* forceX, float* forceY, int* deg)
{
  if (!g) return;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    for (size_t j = 0; j < g->numvertices; j++)
    {
      if (i == j) continue;
      float vx1 = g->vertexXLocs[i];
      float vy1 = g->vertexYLocs[i];
      float vx2 = g->vertexXLocs[j];
      float vy2 = g->vertexYLocs[j];

      vectorSubtract(&vx1, &vy1, vx2, vy2);
      float dist = vectorGetLength(vx1, vy1);
      if (dist < FLOAT_EPSILON)
        dist = EPSILON;

      vectorNormalize(&vx1, &vy1);
      vectorMultiply(&vx1, &vy1, K_R * (((deg[i] + 1) * (deg[j] + 1))
              / dist));
      vectorMultiply(&vx1, &vy1, 0.5);

      vectorAdd(&forceX[i], &forceY[i], vx1, vy1);
    }
  }
}

void fa2Attraction(Graph* g, float* forceX, float* forceY)
{
  if (!g) return;
  for (size_t i = 0; i < g->numedges; i++)
  {
    int v1Index = g->edgeSources[i];
    int v2Index = g->edgeTargets[i];

    float vx1 = g->vertexXLocs[v1Index];
    float vy1 = g->vertexYLocs[v1Index];
    float vx2 = g->vertexXLocs[v2Index];
    float vy2 = g->vertexYLocs[v2Index];

    vectorSubtract(&vx2, &vy2, vx1, vy1);
    vectorMultiply(&vx2, &vy2, 0.5);
    vectorAdd(&forceX[v1Index], &forceY[v1Index], vx2, vy2);
  }
}

// Updates the swing for each vertex, as described in the Force Atlas 2 paper.
void fa2UpdateSwing(Graph* g, float* forceX, float* forceY,
    float* oldForceX, float* oldForceY, float* swg)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float fx = forceX[i];
    float fy = forceY[i];
    vectorSubtract(&fx, &fy, oldForceX[i], oldForceY[i]);
    float vlen = vectorGetLength(fx, fy);
    swg[i] = vlen;
  }
}

// Updates the traction for each vertex, as described in the Force Atlas 2
// paper.
void fa2UpdateTract(Graph* g, float* forceX, float* forceY,
    float* oldForceX, float* oldForceY, float* tra)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float fx = forceX[i];
    float fy = forceY[i];
    vectorAdd(&fx, &fy, oldForceX[i], oldForceY[i]);
    float vlen = vectorGetLength(fx, fy);
    tra[i] = vlen / 2;
  }
}

// Calculate the current swing of the graph.
void fa2UpdateSwingGraph(Graph* g, float* swg, int* deg, float* gswing)
{
  *gswing = 0;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    *gswing += (deg[i] + 1) * swg[i];
  }
}

// Calculate the current traction of the graph.
void fa2UpdateTractGraph(Graph* g, float* tra, int* deg, float* gtract)
{
  *gtract = 0;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    *gtract += (deg[i] + 1) * tra[i];
  }
}

void fa2UpdateSpeedGraph(float gswing, float gtract, float* gspeed)
{
  float oldSpeed = *gspeed;
  *gspeed = gswing > 0 ? TAU * (gtract / gswing) : EPSILON;
  if (*gspeed <= 0)
    *gspeed = EPSILON;
  // Do not allow more then 50% speed increase.
  if (oldSpeed > FLOAT_EPSILON && *gspeed > 1.5 * oldSpeed)
    *gspeed = 1.5 * oldSpeed;
}

void fa2UpdateSpeed(Graph* g, float* speed, float* swg, float* forceX,
    float* forceY, float gs)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float vSwg = swg[i];
    if (vSwg <= 0)
      vSwg = EPSILON;
    float vForceLen = vectorGetLength(forceX[i], forceY[i]);
    if (vForceLen <= 0)
      vForceLen = EPSILON;

    speed[i] = K_S * gs / (1 + (gs * sqrt(vSwg)));
    speed[i] = fmin(speed[i],
        K_SMAX / vForceLen);
  }
}

// Save current forces as the previous forces for the next tick.
void fa2SaveOldForces(Graph* g, float* forceX, float* forceY,
    float* oldForceX, float* oldForceY)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    oldForceX[i] = forceX[i];
    oldForceY[i] = forceY[i];
  }
}

void fa2UpdateDisplacement(Graph* g, float* forceX, float* forceY,
    float* speed, float* dispX, float* dispY)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    dispX[i] = forceX[i];
    dispY[i] = forceY[i];
    vectorMultiply(&forceX[i], &forceY[i], speed[i]);
  }
}

void fa2UpdateLocation(Graph* g, float* xdisp, float* ydisp)
{<commit_msg>debug.<commit_after>
#include <stdio.h>
#include <stdlib.h>
#include "force-atlas-2.h"
#include "math.h"
#include "vector.h"

#define K_R 1.0
#define K_S 0.1
#define K_G 5
#define K_SMAX 10.0
#define TAU 1.0
#define EPSILON 0.1
#define FLOAT_EPSILON 0.0000001

// Calculate the number of neighbours for every node.
void calcNumNeighbours(Graph*, int*);
// Gravity force
void fa2Gravity(Graph*, float*, float*, int*);
// Repulsion between vertices
void fa2Repulsion(Graph*, float*, float*, int*);
// Attraction on edges
void fa2Attraction(Graph*, float*, float*);

void fa2UpdateSwing(Graph*, float*, float*, float*, float*, float*);
void fa2UpdateTract(Graph*, float*, float*, float*, float*, float*);
void fa2UpdateSwingGraph(Graph*, float*, int*, float*);
void fa2UpdateTractGraph(Graph*, float*, int*, float*);
void fa2UpdateSpeedGraph(float, float, float*);
void fa2UpdateSpeed(Graph*, float*, float*, float*, float*, float);
void fa2UpdateDisplacement(Graph*, float*, float*, float*, float*, float*);
void fa2SaveOldForces(Graph*, float*, float*, float*, float*);
void fa2UpdateLocation(Graph*, float*, float*);

void calcNumNeighbours(Graph* g, int* deg)
{
  for (size_t i = 0; i < g->numedges; i++)
  {
    deg[g->edgeSources[i]]++; 
  }
}

void fa2Gravity(Graph* g, float* forceX, float* forceY, int* deg)
{
  if (!g) return;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float vx = g->vertexXLocs[i];
    float vy = g->vertexYLocs[i];
    vectorNormalize(&vx, &vy);
    vectorInverse(&vx, &vy);
    vectorMultiply(&vx, &vy, K_G * (deg[i] + 1));
    vectorAdd(&forceX[i], &forceY[i], vx, vy);
  }
}

void fa2Repulsion(Graph* g, float* forceX, float* forceY, int* deg)
{
  if (!g) return;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    for (size_t j = 0; j < g->numvertices; j++)
    {
      if (i == j) continue;
      float vx1 = g->vertexXLocs[i];
      float vy1 = g->vertexYLocs[i];
      float vx2 = g->vertexXLocs[j];
      float vy2 = g->vertexYLocs[j];

      vectorSubtract(&vx1, &vy1, vx2, vy2);
      float dist = vectorGetLength(vx1, vy1);
      if (dist < FLOAT_EPSILON)
        dist = EPSILON;

      vectorNormalize(&vx1, &vy1);
      vectorMultiply(&vx1, &vy1, K_R * (((deg[i] + 1) * (deg[j] + 1))
              / dist));
      vectorMultiply(&vx1, &vy1, 0.5);

      vectorAdd(&forceX[i], &forceY[i], vx1, vy1);
    }
  }
}

void fa2Attraction(Graph* g, float* forceX, float* forceY)
{
  if (!g) return;
  for (size_t i = 0; i < g->numedges; i++)
  {
    int v1Index = g->edgeSources[i];
    int v2Index = g->edgeTargets[i];

    float vx1 = g->vertexXLocs[v1Index];
    float vy1 = g->vertexYLocs[v1Index];
    float vx2 = g->vertexXLocs[v2Index];
    float vy2 = g->vertexYLocs[v2Index];

    vectorSubtract(&vx2, &vy2, vx1, vy1);
    vectorMultiply(&vx2, &vy2, 0.5);
    vectorAdd(&forceX[v1Index], &forceY[v1Index], vx2, vy2);
  }
}

// Updates the swing for each vertex, as described in the Force Atlas 2 paper.
void fa2UpdateSwing(Graph* g, float* forceX, float* forceY,
    float* oldForceX, float* oldForceY, float* swg)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float fx = forceX[i];
    float fy = forceY[i];
    vectorSubtract(&fx, &fy, oldForceX[i], oldForceY[i]);
    float vlen = vectorGetLength(fx, fy);
    swg[i] = vlen;
  }
}

// Updates the traction for each vertex, as described in the Force Atlas 2
// paper.
void fa2UpdateTract(Graph* g, float* forceX, float* forceY,
    float* oldForceX, float* oldForceY, float* tra)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float fx = forceX[i];
    float fy = forceY[i];
    vectorAdd(&fx, &fy, oldForceX[i], oldForceY[i]);
    float vlen = vectorGetLength(fx, fy);
    tra[i] = vlen / 2;
  }
}

// Calculate the current swing of the graph.
void fa2UpdateSwingGraph(Graph* g, float* swg, int* deg, float* gswing)
{
  *gswing = 0;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    *gswing += (deg[i] + 1) * swg[i];
  }
}

// Calculate the current traction of the graph.
void fa2UpdateTractGraph(Graph* g, float* tra, int* deg, float* gtract)
{
  *gtract = 0;
  for (size_t i = 0; i < g->numvertices; i++)
  {
    *gtract += (deg[i] + 1) * tra[i];
  }
}

void fa2UpdateSpeedGraph(float gswing, float gtract, float* gspeed)
{
  float oldSpeed = *gspeed;
  *gspeed = gswing > 0 ? TAU * (gtract / gswing) : EPSILON;
  if (*gspeed <= 0)
    *gspeed = EPSILON;
  // Do not allow more then 50% speed increase.
  if (oldSpeed > FLOAT_EPSILON && *gspeed > 1.5 * oldSpeed)
    *gspeed = 1.5 * oldSpeed;
}

void fa2UpdateSpeed(Graph* g, float* speed, float* swg, float* forceX,
    float* forceY, float gs)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    float vSwg = swg[i];
    if (vSwg <= 0)
      vSwg = EPSILON;
    float vForceLen = vectorGetLength(forceX[i], forceY[i]);
    if (vForceLen <= 0)
      vForceLen = EPSILON;

    speed[i] = K_S * gs / (1 + (gs * sqrt(vSwg)));
    speed[i] = fmin(speed[i],
        K_SMAX / vForceLen);
  }
}

// Save current forces as the previous forces for the next tick.
void fa2SaveOldForces(Graph* g, float* forceX, float* forceY,
    float* oldForceX, float* oldForceY)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    oldForceX[i] = forceX[i];
    oldForceY[i] = forceY[i];
  }
}

void fa2UpdateDisplacement(Graph* g, float* speed, float* forceX,
    float* forceY, float* dispX, float* dispY)
{
  for (size_t i = 0; i < g->numvertices; i++)
  {
    dispX[i] = forceX[i];
    dispY[i] = forceY[i];
    vectorMultiply(&forceX[i], &forceY[i], speed[i]);
  }
}

void fa2UpdateLocation(Graph* g, float* xdisp, float* ydisp)
{
{'fraction_non_alphanumeric': 0.16724645437516725, 'fraction_numerical': 0.02140754616002141, 'mean_word_length': 3.488390712570056, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2715373', 'n_tokens_mistral': 4635, 'n_tokens_neox': 4462, 'n_words': 1196}