Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
Distances calculateredteds
Browse files Browse the repository at this point in the history
  • Loading branch information
Henriquelay committed Mar 11, 2021
1 parent b6c95b6 commit 30b9f55
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions in-exemplos/1-diminuido.txt
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions in-exemplos/exemplo-doc.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
#define _DATA_H_

#include "./fileReader.h"
#include <math.h>

typedef struct data_t {
// [0] is char* Identifier, [1...] is long double* feature;
void*** dataMatrix;
size_t i, j;
} 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);


Expand Down
66 changes: 60 additions & 6 deletions src/data.c
Original file line number Diff line number Diff line change
@@ -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("");
Expand All @@ -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]);
Expand Down Expand Up @@ -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 <tgmath.h>
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;
}
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 30b9f55

Please sign in to comment.