diff --git a/meanshift_declarations.c b/meanshift_declarations.c new file mode 100644 index 0000000..4fbf8ed --- /dev/null +++ b/meanshift_declarations.c @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include + +#include "meanshift_declarations.h" + +void get_args(int argc, char **argv, int *h){ + if (argc != 6) { + printf("Usage: %s h N D Pd Pl\nwhere:\n", argv[0]); + printf("\th is the variance\n"); + printf("\tN is the the number of points\n"); + printf("\tD is the number of dimensions of each point\n"); + printf("\tPd is the path of the dataset file\n"); + printf("\tPl is the path of the labels file\n"); + exit(1); + } + + *h = atoi(argv[1]); + NUMBER_OF_POINTS = atoi(argv[2]); + DIMENSIONS = atoi(argv[3]); + POINTS_FILENAME = argv[4]; + LABELS_FILENAME = argv[5]; +} + +int meanshift(double **original_points, double ***shifted_points, int h + , parameters *opt, int iteration){ + + // allocates space and copies original points on first iteration + if (iteration == 1){ + (*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); + duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points); + } + + // mean shift vector + double **mean_shift_vector; + mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); + // initialize elements of mean_shift_vector to inf + for (int i=0;i opt->epsilon) { + return meanshift(original_points, shifted_points, h, opt, ++iteration); + } + + return iteration; +} + +// TODO check why there's is a difference in the norm calculate in matlab +double norm(double **matrix, int rows, int cols){ + double sum=0, temp_mul=0; + for (int i=0; i + +extern int NUMBER_OF_POINTS; +extern int DIMENSIONS; +extern char* POINTS_FILENAME; +extern char* LABELS_FILENAME; + +typedef struct parameters { + double epsilon; + bool verbose; + bool display; +} parameters; + +//Function get_args parses command line arguments. +void get_args(int argc, char **argv, int *h); + +//Function meanshift recursively shifts original points according to th +//mean-shift algorithm saving the result to shiftedPoints. Struct opt has user +//options, h is the desirable deviation, iteration is this call's iteration +//number. +int meanshift(double **original_points, double ***shifted_points, int h + , parameters *opt, int iteration); + +//Function norm returns the second norm of matrix of dimensions rowsXcols. +double norm(double **matrix, int rows, int cols); + +//Function alloc_2d_double allocates rows*cols bytes of continuous memory. +double **alloc_2d_double(int rows, int cols); + +//Function duplicate copies the values of source array to dest array. +void duplicate(double **source, int rows, int cols, double ***dest); + +//Function print_matrix prints array of dimensions rowsXcols to the console. +void print_matrix(double **array, int rows, int cols); + +//Function save_matrix prints matrix in a csv file with path/filename +//"output/output_iteration". If a file already exists new lines are concatenated. +void save_matrix(double **matrix + , int iteration); + +#endif //SERIAL_DECLARATIONS_H \ No newline at end of file diff --git a/serial_declarations.c b/serial_declarations.c index c922ddb..9665c9f 100644 --- a/serial_declarations.c +++ b/serial_declarations.c @@ -54,9 +54,7 @@ int meanshift(double **original_points, double ***shifted_points, int h double dist = calculateDistance((*shifted_points)[i] , original_points[j]); - if (i == j){ - kernel_matrix[i][j] = 1; - } else if (dist < h*h){ + if (dist < h*h){ kernel_matrix[i][j] = dist * dist; // compute kernel matrix double pow = ((-1)*(kernel_matrix[i][j]))/(2*(h*h)); @@ -64,6 +62,9 @@ int meanshift(double **original_points, double ***shifted_points, int h } else { kernel_matrix[i][j] = 0; } + if (i == j){ + kernel_matrix[i][j] += 1; + } sum = sum + kernel_matrix[i][j]; } denominator[i] = sum; @@ -183,4 +184,4 @@ void save_matrix(double **matrix, int iteration){ } fprintf(file, "\n"); } -} \ No newline at end of file +}