diff --git a/Makefile b/Makefile index 9faaf2b..9968f28 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ VAL_FLAGS ?= $(VALZIN_FLAGS) --leak-check=full VALZAO_FLAGS ?= $(VAL_FLAGS) --show-leak-kinds=all --track-origins=yes $(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) - $(CC) $(OBJS) -o $@ $(LDFLAGS) + $(CC) $(OBJS) -o $@ -lm make clean # c source diff --git a/in-exemplos/1-diminuido.txt b/in-exemplos/1-diminuido.txt new file mode 100644 index 0000000..2443a42 --- /dev/null +++ b/in-exemplos/1-diminuido.txt @@ -0,0 +1,15 @@ +Ri7990zl24y6,-25.552615001487293,91.19998350820217 +kAo042ZEgV58F2xuG6c1pG,45.91961412443572,19.546037862743503 +8vk0a7v35sJEp8CS3,-25.207624512915746,89.84175758639395 +635062b43VFr6,47.21131418467521,21.087936867978144 +96JVM7414kFA1q0aJL53f4,-24.850013958961473,88.22958103732545 +j75m17MAr26Y3lko0PW0l2,45.559570839058374,19.422484463556106 +674bNNQE94hGivVe6094n,47.87668240702253,19.21342662113367 +HI5SJafPi5Jy92vSD6Brl,-25.63635895505269,90.25378387169312 +Z1679bWK95,-25.768898230833464,90.75453757082411 +L846915d8986,-25.55539392333997,89.67713152841299 +LcdF18i4qR7my0h0Zdcjr,43.779043258191265,20.553599343782544 +wz7s65223KeH2k47L1m,-26.81689406304054,89.58057375274227 +h28dx1zd982ZrEd6JdV,47.429787884776964,20.66297695852352 +618Ftc38ZQh6,45.69673526840366,19.404034692809553 +gP1A01o4g195H33s1Vpyki,-26.570498220894933,89.42301707358854 diff --git a/in-exemplos/exemplo-doc.txt b/in-exemplos/exemplo-doc.txt new file mode 100644 index 0000000..432ea5a --- /dev/null +++ b/in-exemplos/exemplo-doc.txt @@ -0,0 +1,10 @@ +A,3.0,7.0 +B,1.0,2.0 +C,7.0,3.0 +D,4.0,7.0 +E,1.0,1.0 +F,6.0,4.0 +G,4.0,6.0 +H,2.0,1.0 +I,6.0,3.0 +J,7.0,4.0 diff --git a/lib/data.h b/lib/data.h index 35f6d1b..a65170f 100644 --- a/lib/data.h +++ b/lib/data.h @@ -15,9 +15,9 @@ #define _DATA_H_ #include "./fileReader.h" +#include typedef struct data_t { - // [0] is char* Identifier, [1...] is long double* feature; void*** dataMatrix; size_t i, j; } data_t; @@ -25,6 +25,7 @@ typedef struct data_t { data_t* loadData(FILE* file, const char* separator); void printData(const data_t* dataStruct); +data_t* getDistances(data_t* data); void destroyData(data_t* data); diff --git a/src/data.c b/src/data.c index 8e639dc..8f62458 100644 --- a/src/data.c +++ b/src/data.c @@ -1,9 +1,11 @@ #include "../lib/data.h" -void printData(const data_t *dataStruct) { - for (int i = 0; i < dataStruct->i; i++) { +// TODO - Check if a Macro to derefence (*((long double*)b[i])) is viable + +void printData(const data_t* dataStruct) { + for (size_t i = 0; i < dataStruct->i; i++) { printf("%s", (char*)dataStruct->dataMatrix[i][0]); - for (int j = 1; j < dataStruct->j; j++) { + for (size_t j = 1; j < dataStruct->j; j++) { printf(",%.15Lf", *((long double*)dataStruct->dataMatrix[i][j])); } puts(""); @@ -12,8 +14,8 @@ void printData(const data_t *dataStruct) { // TODO check before freeing -- don't check whole array void destroyData(data_t* data) { - for(size_t i = 0; i < data->i; i++) { - for(size_t j = 0; j < data->j; j++){ + for (size_t i = 0; i < data->i; i++) { + for (size_t j = 0; j < data->j; j++) { free(data->dataMatrix[i][j]); } free(data->dataMatrix[i]); @@ -44,13 +46,65 @@ data_t* loadData(FILE* file, const char* separator) { } // void** line = NULL; - for (int i = 0; i < dataStruct->i; i++) { + for (size_t i = 0; i < dataStruct->i; i++) { dataStruct->dataMatrix[i] = readLine(file, separator, buffer, &bufferSize, &dataStruct->j); } return dataStruct; } +long double distance(long double** a, long double** b, size_t dimensions) { + long double accumulator = 0; + for (size_t i = 1; i <= dimensions; i++) { + // printf("dim = %ld Val a = %Lf b = %Lf\n", dimensions, *a[i], *b[i]); + if ((*a[i]) < (*b[i])) { + accumulator += ((*b[i]) - (*a[i])) * ((*b[i]) - (*a[i])); + } else { + accumulator += ((*a[i]) - (*b[i])) * ((*a[i]) - (*b[i])); + } + } + // TODO Try using sqrtl. Defined in + return sqrt(accumulator); +} + data_t* getDistances(data_t* data) { + data_t* distances = (data_t*)malloc(sizeof(data_t)); + if (distances == NULL) { + perror("Error allocating new distancesure. Exiting"); + exit(1); + }; + + distances->i = data->i; + // Data has [0] as identifiers, won't be using for calculations; + distances->j = data->j - 1; + // 0 will always be id, read as `char*`. + distances->dataMatrix = (void***)malloc(sizeof(long double**) * distances->i); + if (distances->dataMatrix == NULL) { + perror("Error allocating new dataString lines. Exiting"); + exit(1); + } + + + for (size_t i = 0; i < distances->i; i++) { + printf("Lin %ld\t", i); + distances->dataMatrix[i] = (void**)malloc(sizeof(void*) * i); + if (distances->dataMatrix[i] == NULL) { + perror("Error allocating new dataString lines on distances struct. Exiting"); + exit(1); + } + for (size_t j = 0; j < i; j++) { + distances->dataMatrix[i][j] = (long double*)malloc(sizeof(long double)); + if (distances->dataMatrix[i] == NULL) { + perror("Error allocating new dataString lines on distances struct. Exiting"); + exit(1); + } + *((long double*)distances->dataMatrix[i][j]) = distance((long double**)data->dataMatrix[i], (long double**)data->dataMatrix[j], distances->j); + printf("%.2Lf\t", *((long double*)distances->dataMatrix[i][j])); + } + puts(""); + } + + // destroyData(distances); + return NULL; } diff --git a/src/main.c b/src/main.c index a1664c5..3e6bdd5 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,9 @@ int main(int argc, char** argv) { data_t* data = loadData(file, ","); closeFile(file); - printData(data); + getDistances(data); + + // printData(data); destroyData(data);