Skip to content
Snippets Groups Projects
Commit 8c35d93a authored by dario.genga's avatar dario.genga
Browse files

Add values_utils files

parent 0078dbfb
Branches
No related tags found
No related merge requests found
......@@ -3,27 +3,7 @@
#include "kmeans.h"
int random_with_min_man_value(int min, int max) {
return (rand() % (max - min + 1)) + min;
}
void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
kmeans* kmeans_create_empty() {
kmeans* universe = malloc(sizeof(kmeans));
universe->points_array = NULL;
universe->nb_points = 0;
universe->k = 0;
universe->dimensions = 0;
universe->clusters_array = NULL;
return universe;
}
/// Create empty clusters for the universe
/// \param universe The universe who contains the cluster to create.
......@@ -82,6 +62,17 @@ point* create_point_from_string(char *line, int dimensions) {
return p;
}
kmeans* kmeans_create_empty() {
kmeans* universe = malloc(sizeof(kmeans));
universe->points_array = NULL;
universe->nb_points = 0;
universe->k = 0;
universe->dimensions = 0;
universe->clusters_array = NULL;
return universe;
}
kmeans* kmeans_create(int k, point** data, int nb_points) {
kmeans* universe = malloc(sizeof(kmeans));
universe->points_array = data;
......@@ -227,46 +218,14 @@ void init_clusters(kmeans *universe) {
free(random_index_possible);
}
float compute_euclidean_distance(point* p1, point* p2) {
float sum = 0;
float result = 0;
for (int i = 0; i < p1->dimensions; i++) {
sum += pow(p1->value[i] - p2->value[i], 2);
}
result = sqrt(sum);
return result;
}
float compute_manhattan_distance(point* p1, point* p2) {
float result = 0;
for (int i = 0; i < p1->dimensions; i++) {
result += fabs(p1->value[i] - p2->value[i]);
}
return result;
}
float compute_chebyshev_distance(point* p1, point* p2) {
float result = 0;
for (int i = 0; i < p1->dimensions; i++) {
int abs_diff = fabs(p1->value[i] - p2->value[i]);
if (abs_diff > result) {
result = abs_diff;
}
}
return result;
}
float compute_distance(point* p1, point* p2) {
if (p1->dimensions != p2->dimensions) {
printf("The points don't have the same dimensions!\n");
exit(EXIT_FAILURE);
}
float euclidean = compute_euclidean_distance(p1, p2);
//float manhattan = compute_manhattan_distance(p1, p2);
//float chebyshev = compute_chebyshev_distance(p1, p2);
float euclidean = compute_euclidean_distance(p1->value, p2->value, p1->dimensions);
//float manhattan = compute_manhattan_distance(p1->value, p2->value, p1->dimensions);
//float chebyshev = compute_chebyshev_distance(p1->value, p2->value, p1->dimensions);
return euclidean;
}
......
......@@ -8,8 +8,8 @@
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include "files_utils.h"
#include "values_utils.h"
#define LINE_INDEX_DIMENSIONS 0
#define LINE_INDEX_CLUSTER 1
......
LIB=-lm
CC=gcc -Wall -Wextra -g
main: files_utils.o kmeans.o main.o
main: files_utils.o values_utils.o kmeans.o main.o
$(CC) $^ -fsanitize=address -fsanitize=leak -o $@ $(LIB)
files_utils.o: files_utils.c files_utils.h
$(CC) -c $< $(LIB)
values_utils.o: values_utils.c values_utils.h
$(CC) -c $< $(LIB)
kmeans.o: kmeans.c kmeans.h
$(CC) -c $< $(LIB)
main.o: main.c
......
2
3
*
-1.00,-5.00
-2.00,-4.00
-3.00,-3.00
-4.00,-2.00
-5.00,-1.00
-1.75,-2.25
*
0.00,0.00
-1.00,-5.00
3.10,-4.90
*
1.00,1.00
......@@ -19,3 +13,9 @@
-2.25,4.75
2.20,4.40
4.00,2.00
*
-2.00,-4.00
-3.00,-3.00
-4.00,-2.00
-5.00,-1.00
-1.75,-2.25
// Project : K-means
// Author : Dario GENGA
#include "values_utils.h"
int random_with_min_man_value(int min, int max) {
return (rand() % (max - min + 1)) + min;
}
/// Swap x and y values
/// \param x The first value
/// \param y The second value
void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
float compute_euclidean_distance(float* p1, float* p2, size_t dimensions) {
float sum = 0;
float result = 0;
for (size_t i = 0; i < dimensions; i++) {
sum += pow(p1[i] - p2[i], 2);
}
result = sqrt(sum);
return result;
}
float compute_manhattan_distance(float* p1, float* p2, size_t dimensions) {
float result = 0;
for (size_t i = 0; i < dimensions; i++) {
result += fabs(p1[i] - p2[i]);
}
return result;
}
float compute_chebyshev_distance(float* p1, float* p2, size_t dimensions) {
float result = 0;
for (size_t i = 0; i < dimensions; i++) {
int abs_diff = fabs(p1[i] - p2[i]);
if (abs_diff > result) {
result = abs_diff;
}
}
return result;
}
// Project : K-means
// Author : Dario GENGA
#ifndef _KMEANS_VALUES_UTILS_H
#define _KMEANS_VALUES_UTILS_H
#include <math.h>
#include <stdlib.h>
/// Get a random value in the min-max range.
/// \param min The minimal value (inclued).
/// \param max The maximal value (inclued).
/// \return A random int value.
int random_with_min_man_value(int min, int max);
/// Swap x and y values
/// \param x The first value
/// \param y The second value
void swap(int *x, int *y);
/// Compute the euclidean distance between two points of same dimensions.
/// \param p1 The values of the first point.
/// \param p2 The values of the second point.
/// \return The distance between the two points.
float compute_euclidean_distance(float* p1, float* p2, size_t dimensions);
/// Compute the manhattan distance between two points of same dimensions.
/// \param p1 The values of the first point.
/// \param p2 The values of the second point.
/// \return The distance between the two points.
float compute_manhattan_distance(float* p1, float* p2, size_t dimensions);
/// Compute the chebyshev distance between two points of same dimensions.
/// \param p1 The values of the first point.
/// \param p2 The values of the second point.
/// \return The distance between the two points.
float compute_chebyshev_distance(float* p1, float* p2, size_t dimensions);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment