Skip to content
Snippets Groups Projects
Commit 8f1689ff authored by Boris Stefanovic's avatar Boris Stefanovic
Browse files

ADD: cluster structure and beginning of logic

parent 23a7d5fb
Branches
Tags
No related merge requests found
#include "cluster.h"
#include "vector.h"
cluster_int_t* cluster_create_int(vector_int_t* center) {
cluster_int_t* cluster = malloc(sizeof(cluster_int_t));
if (NULL == cluster) return NULL;
cluster->center = center;
cluster->points = list_points_create_int();
}
cluster_fpt_t* cluster_create_fpt(vector_fpt_t* center) {
cluster_fpt_t* cluster = malloc(sizeof(cluster_fpt_t));
if (NULL == cluster) return NULL;
cluster->center = center;
cluster->points = list_points_create_fpt();
}
void cluster_destroy_int(cluster_int_t* cluster) {
if (NULL == cluster) return;
vector_destroy_int(cluster->center);
list_points_destroy_int(cluster->points, false);
free(cluster);
}
void cluster_destroy_fpt(cluster_fpt_t* cluster) {
if (NULL == cluster) return;
vector_destroy_fpt(cluster->center);
list_points_destroy_fpt(cluster->points, false);
free(cluster);
}
void cluster_add_point_int(cluster_int_t* cluster, point_int_t* point) {
//TODO
if (NULL == cluster || NULL == point) return;
if (NULL == cluster->center) {
cluster->center = vector_copy_int(point->vector);
} else {
vector_int_t* delta = vector_copy_int(point->vector);
vector_div_inplace_int(delta, cluster->points->size);
vector_add_inplace_int(cluster->center, *delta);
vector_destroy_int(delta);
}
}
void cluster_add_point_fpt(cluster_fpt_t* cluster, point_fpt_t* point) {
//TODO
if (NULL == cluster || NULL == point) return;
if (NULL == cluster->center) {
cluster->center = vector_copy_fpt(point->vector);
} else {
vector_fpt_t* delta = vector_copy_fpt(point->vector);
vector_div_inplace_fpt(delta, cluster->points->size);
vector_add_inplace_fpt(cluster->center, *delta);
vector_destroy_fpt(delta);
}
}
void cluster_update_center_int(cluster_int_t* cluster) {
//TODO
}
void cluster_update_center_fpt(cluster_fpt_t* cluster) {
//TODO
}
void cluster_reset_int(cluster_int_t* cluster) {
//TODO
list_points_destroy_int(cluster->points, false);
cluster->points = list_points_create_int();
}
void cluster_reset_fpt(cluster_fpt_t* cluster) {
//TODO
list_points_destroy_fpt(cluster->points, false);
cluster->points = list_points_create_fpt();
}
#ifndef PROG_KMEANS_CLUSTER_H
#define PROG_KMEANS_CLUSTER_H
#include <stdlib.h>
#include "linkedlist.h"
#include "point.h"
#include "vector.h"
typedef struct cluster_int {
vector_int_t* center;
list_points_int_t* points;
} cluster_int_t;
typedef struct cluster_fpt {
vector_fpt_t* center;
list_points_fpt_t* points;
} cluster_fpt_t;
cluster_int_t* cluster_create_int(vector_int_t* center);
cluster_fpt_t* cluster_create_fpt(vector_fpt_t* center);
void cluster_destroy_int(cluster_int_t* center);
void cluster_destroy_fpt(cluster_fpt_t* center);
void cluster_add_point_int(cluster_int_t* cluster, point_int_t* point);
void cluster_add_point_fpt(cluster_fpt_t* cluster, point_fpt_t* point);
void cluster_update_center_int(cluster_int_t* cluster);
void cluster_update_center_fpt(cluster_fpt_t* cluster);
void cluster_reset_int(cluster_int_t* cluster);
void cluster_reset_fpt(cluster_fpt_t* cluster);
#endif //PROG_KMEANS_CLUSTER_H
//
// by Boris Stefanovic on 24/05/22
//
#include "distance.h" #include "distance.h"
#include <math.h> #include <math.h>
#include "common.h" #include "common.h"
......
//
// by Boris Stefanovic on 24/05/22
//
#ifndef PROG_KMEANS_DISTANCE_H #ifndef PROG_KMEANS_DISTANCE_H
#define PROG_KMEANS_DISTANCE_H #define PROG_KMEANS_DISTANCE_H
......
...@@ -4,20 +4,20 @@ ...@@ -4,20 +4,20 @@
#include "point.h" #include "point.h"
list_points_node_int_t* list_points_create_node_int(vector_int_t* vec) { list_points_node_int_t* list_points_node_create_int(vector_int_t* vec) {
list_points_node_int_t* node = malloc(sizeof(list_points_node_int_t)); list_points_node_int_t* node = malloc(sizeof(list_points_node_int_t));
if (NULL == node) return NULL; if (NULL == node) return NULL;
point_int_t* point = point_int_create(vec); point_int_t* point = point_create_int(vec);
if (NULL == point) return NULL; if (NULL == point) return NULL;
node->point = point; node->point = point;
node->next = NULL; node->next = NULL;
return node; return node;
} }
list_points_node_fpt_t* list_points_create_node_fpt(vector_fpt_t* vec) { list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* vec) {
list_points_node_fpt_t* node = malloc(sizeof(list_points_node_fpt_t)); list_points_node_fpt_t* node = malloc(sizeof(list_points_node_fpt_t));
if (NULL == node) return NULL; if (NULL == node) return NULL;
point_fpt_t* point = point_fpt_create(vec); point_fpt_t* point = point_create_fpt(vec);
if (NULL == point) return NULL; if (NULL == point) return NULL;
node->point = point; node->point = point;
node->next = NULL; node->next = NULL;
...@@ -25,15 +25,15 @@ list_points_node_fpt_t* list_points_create_node_fpt(vector_fpt_t* vec) { ...@@ -25,15 +25,15 @@ list_points_node_fpt_t* list_points_create_node_fpt(vector_fpt_t* vec) {
} }
void list_points_destroy_node_int(list_points_node_int_t* node, const bool full) { void list_points_node_destroy_int(list_points_node_int_t* node, const bool full) {
if (NULL == node) return; if (NULL == node) return;
if (full) point_int_destroy(node->point); if (full) point_destroy_int(node->point);
free(node); free(node);
} }
void list_points_destroy_node_fpt(list_points_node_fpt_t* node, const bool full) { void list_points_node_destroy_fpt(list_points_node_fpt_t* node, const bool full) {
if (NULL == node) return; if (NULL == node) return;
if (full) point_fpt_destroy(node->point); if (full) point_destroy_fpt(node->point);
free(node); free(node);
} }
...@@ -64,7 +64,7 @@ void list_points_destroy_int(list_points_int_t* list, const bool full) { ...@@ -64,7 +64,7 @@ void list_points_destroy_int(list_points_int_t* list, const bool full) {
list_points_node_int_t* node; list_points_node_int_t* node;
while ((node = list->head) != NULL) { while ((node = list->head) != NULL) {
list->head = node->next; list->head = node->next;
list_points_destroy_node_int(node, full); list_points_node_destroy_int(node, full);
} }
free(list); free(list);
} }
...@@ -74,7 +74,7 @@ void list_points_destroy_fpt(list_points_fpt_t* list, const bool full) { ...@@ -74,7 +74,7 @@ void list_points_destroy_fpt(list_points_fpt_t* list, const bool full) {
list_points_node_fpt_t* node; list_points_node_fpt_t* node;
while ((node = list->head) != NULL) { while ((node = list->head) != NULL) {
list->head = node->next; list->head = node->next;
list_points_destroy_node_fpt(node, full); list_points_node_destroy_fpt(node, full);
} }
free(list); free(list);
} }
...@@ -82,7 +82,7 @@ void list_points_destroy_fpt(list_points_fpt_t* list, const bool full) { ...@@ -82,7 +82,7 @@ void list_points_destroy_fpt(list_points_fpt_t* list, const bool full) {
void list_points_append_int(list_points_int_t* list, vector_int_t* vector) { void list_points_append_int(list_points_int_t* list, vector_int_t* vector) {
if (NULL == vector) return; if (NULL == vector) return;
list_points_node_int_t* node = list_points_create_node_int(vector); list_points_node_int_t* node = list_points_node_create_int(vector);
if (NULL == list->head) { // if list is empty if (NULL == list->head) { // if list is empty
list->head = node; list->head = node;
list->tail = list->head; list->tail = list->head;
...@@ -95,7 +95,7 @@ void list_points_append_int(list_points_int_t* list, vector_int_t* vector) { ...@@ -95,7 +95,7 @@ void list_points_append_int(list_points_int_t* list, vector_int_t* vector) {
void list_points_append_fpt(list_points_fpt_t* list, vector_fpt_t* vector) { void list_points_append_fpt(list_points_fpt_t* list, vector_fpt_t* vector) {
if (NULL == vector) return; if (NULL == vector) return;
list_points_node_fpt_t* node = list_points_create_node_fpt(vector); list_points_node_fpt_t* node = list_points_node_create_fpt(vector);
if (NULL == list->head) { // if list is empty if (NULL == list->head) { // if list is empty
list->head = node; list->head = node;
list->tail = list->head; list->tail = list->head;
......
//
// by Boris Stefanovic on 31/05/22
//
#ifndef PROG_KMEANS_LINKEDLIST_H #ifndef PROG_KMEANS_LINKEDLIST_H
#define PROG_KMEANS_LINKEDLIST_H #define PROG_KMEANS_LINKEDLIST_H
...@@ -15,33 +11,33 @@ typedef struct list_points_node_int { ...@@ -15,33 +11,33 @@ typedef struct list_points_node_int {
struct list_points_node_int* next; struct list_points_node_int* next;
} list_points_node_int_t; } list_points_node_int_t;
typedef struct list_points_int {
list_points_node_int_t* head;
list_points_node_int_t* tail;
size_t size;
} list_points_int_t;
typedef struct list_points_node_fpt { typedef struct list_points_node_fpt {
point_fpt_t* point; point_fpt_t* point;
struct list_points_node_fpt* next; struct list_points_node_fpt* next;
} list_points_node_fpt_t; } list_points_node_fpt_t;
typedef struct list_points_fpt {
list_points_node_fpt_t* head;
list_points_node_fpt_t* tail;
size_t size;
} list_points_fpt_t;
list_points_node_int_t* list_points_node_create_int(vector_int_t* vec);
list_points_node_int_t* list_points_create_node_int(vector_int_t* vec); list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* vec);
list_points_node_fpt_t* list_points_create_node_fpt(vector_fpt_t* vec);
void list_points_node_destroy_int(list_points_node_int_t* node, const bool full);
void list_points_destroy_node_int(list_points_node_int_t* node, const bool full); void list_points_node_destroy_fpt(list_points_node_fpt_t* node, const bool full);
void list_points_destroy_node_fpt(list_points_node_fpt_t* node, const bool full);
typedef struct list_points_int {
list_points_node_int_t* head;
list_points_node_int_t* tail;
size_t size;
} list_points_int_t;
typedef struct list_points_fpt {
list_points_node_fpt_t* head;
list_points_node_fpt_t* tail;
size_t size;
} list_points_fpt_t;
list_points_int_t* list_points_create_int(); list_points_int_t* list_points_create_int();
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include "vector.h" #include "vector.h"
point_int_t* point_int_create(vector_int_t* vector) { point_int_t* point_create_int(vector_int_t* vector) {
point_int_t* point = malloc(sizeof(point_int_t)); point_int_t* point = malloc(sizeof(point_int_t));
if (NULL == point) return NULL; if (NULL == point) return NULL;
point->vector = vector; point->vector = vector;
...@@ -11,8 +11,7 @@ point_int_t* point_int_create(vector_int_t* vector) { ...@@ -11,8 +11,7 @@ point_int_t* point_int_create(vector_int_t* vector) {
return point; return point;
} }
point_fpt_t* point_create_fpt(vector_fpt_t* vector) {
point_fpt_t* point_fpt_create(vector_fpt_t* vector) {
point_fpt_t* point = malloc(sizeof(point_fpt_t)); point_fpt_t* point = malloc(sizeof(point_fpt_t));
if (NULL == point) return NULL; if (NULL == point) return NULL;
point->vector = vector; point->vector = vector;
...@@ -21,15 +20,14 @@ point_fpt_t* point_fpt_create(vector_fpt_t* vector) { ...@@ -21,15 +20,14 @@ point_fpt_t* point_fpt_create(vector_fpt_t* vector) {
} }
void point_int_destroy(point_int_t* cp) { void point_destroy_int(point_int_t* cp) {
if (NULL == cp) return; if (NULL == cp) return;
vector_int_destroy(cp->vector); vector_destroy_int(cp->vector);
free(cp); free(cp);
} }
void point_destroy_fpt(point_fpt_t* cp) {
void point_fpt_destroy(point_fpt_t* cp) {
if (NULL == cp) return; if (NULL == cp) return;
vector_fpt_destroy(cp->vector); vector_destroy_fpt(cp->vector);
free(cp); free(cp);
} }
#ifndef PROG_KMEANS_CLUSTER_H #ifndef PROG_KMEANS_POINT_H
#define PROG_KMEANS_CLUSTER_H #define PROG_KMEANS_POINT_H
#include "vector.h" #include "vector.h"
...@@ -9,21 +9,20 @@ typedef struct point_int { ...@@ -9,21 +9,20 @@ typedef struct point_int {
vector_int_t* cluster; vector_int_t* cluster;
} point_int_t; } point_int_t;
typedef struct point_fpt { typedef struct point_fpt {
vector_fpt_t* vector; vector_fpt_t* vector;
vector_fpt_t* cluster; vector_fpt_t* cluster;
} point_fpt_t; } point_fpt_t;
point_int_t* point_int_create(vector_int_t* vector); point_int_t* point_create_int(vector_int_t* vector);
point_fpt_t* point_fpt_create(vector_fpt_t* vector); point_fpt_t* point_create_fpt(vector_fpt_t* vector);
void point_int_destroy(point_int_t* cp); void point_destroy_int(point_int_t* cp);
void point_fpt_destroy(point_fpt_t* cp); void point_destroy_fpt(point_fpt_t* cp);
#endif //PROG_KMEANS_CLUSTER_H #endif //PROG_KMEANS_POINT_H
...@@ -87,3 +87,27 @@ void vector_print_to_file_fpt(FILE* file, const vector_fpt_t* v) { ...@@ -87,3 +87,27 @@ void vector_print_to_file_fpt(FILE* file, const vector_fpt_t* v) {
for (size_t i = 1; i < v->dim; ++i) fprintf(file, " , %lf", v->data[i]); for (size_t i = 1; i < v->dim; ++i) fprintf(file, " , %lf", v->data[i]);
fprintf(file, "\n"); fprintf(file, "\n");
} }
void vector_add_inplace_int(vector_int_t* v, const vector_int_t a) {
if (NULL == v) return;
const size_t dim = v->dim < a.dim ? v->dim : a.dim;
for (size_t i = 0; i < dim; ++i) v->data[i] += a.data[i];
}
void vector_add_inplace_fpt(vector_fpt_t* v, const vector_fpt_t a) {
if (NULL == v) return;
const size_t dim = v->dim < a.dim ? v->dim : a.dim;
for (size_t i = 0; i < dim; ++i) v->data[i] += a.data[i];
}
void vector_div_inplace_int(vector_int_t* v, const int_t a) {
if (NULL == v) return;
for (size_t i = 0; i < v->dim; ++i) v->data[i] /= a;
}
void vector_div_inplace_fpt(vector_fpt_t* v, const fpt_t a) {
if (NULL == v) return;
for (size_t i = 0; i < v->dim; ++i) v->data[i] /= a;
}
...@@ -44,4 +44,14 @@ void vector_print_to_file_int(FILE* file, const vector_int_t* v); ...@@ -44,4 +44,14 @@ void vector_print_to_file_int(FILE* file, const vector_int_t* v);
void vector_print_to_file_fpt(FILE* file, const vector_fpt_t* v); void vector_print_to_file_fpt(FILE* file, const vector_fpt_t* v);
void vector_add_inplace_int(vector_int_t* v, const vector_int_t a);
void vector_add_inplace_fpt(vector_fpt_t* v, const vector_fpt_t a);
void vector_div_inplace_int(vector_int_t* v, const int_t a);
void vector_div_inplace_fpt(vector_fpt_t* v, const fpt_t a);
#endif //PROG_KMEANS_VECTOR_H #endif //PROG_KMEANS_VECTOR_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment