Aufgrund einer Wartung wird GitLab am 25.01 zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 25.01 between 8:00 and 9:00 am.

HelperA.h 2.24 KB
Newer Older
Sascha Niklas Schneiders's avatar
Sascha Niklas Schneiders committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef HELPERA_H
#define HELPERA_H
#include <iostream>
#include "armadillo.h"
#include <stdarg.h>
#include <initializer_list>
using namespace arma;
class HelperA{
public:
static mat getEigenVectors(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenVectors;
}
static vec getEigenValues(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenValues;
}

static mat getKMeansClusters(mat A, int k){
mat clusters;
kmeans(clusters,A.t(),k,random_subset,20,true);
26
/*printf("cluster centroid calculation done\n");
Sascha Niklas Schneiders's avatar
Sascha Niklas Schneiders committed
27
28
29
30
31
32
33
34
std::ofstream myfile;
     myfile.open("data after cluster.txt");
     myfile << A;
     myfile.close();
	 
	 std::ofstream myfile2;
     myfile2.open("cluster centroids.txt");
     myfile2 << clusters;
35
     myfile2.close();*/
Sascha Niklas Schneiders's avatar
Sascha Niklas Schneiders committed
36
37
mat indexedData=getKMeansClustersIndexData(A.t(), clusters);

38
/*std::ofstream myfile3;
Sascha Niklas Schneiders's avatar
Sascha Niklas Schneiders committed
39
40
41
     myfile3.open("data after index.txt");
     myfile3 << indexedData;
     myfile3.close();
42
	 */
Sascha Niklas Schneiders's avatar
Sascha Niklas Schneiders committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
return indexedData;
}

static mat getKMeansClustersIndexData(mat A, mat centroids){
	mat result=mat(A.n_cols, 1);
	for(int i=0;i<A.n_cols;++i){
		result(i, 0) = getIndexForClusterCentroids(A, i, centroids);
	}
	return result;
}

static int getIndexForClusterCentroids(mat A, int colIndex, mat centroids){
	int index=1;
	double lowestDistance=getEuclideanDistance(A, colIndex, centroids, 0);
	for(int i=1;i<centroids.n_cols;++i){
		double curDistance=getEuclideanDistance(A, colIndex, centroids, i);
		if(curDistance<lowestDistance){
			lowestDistance=curDistance;
			index=i+1;
		}
	}
	return index;
}

static double getEuclideanDistance(mat A, int colIndexA, mat B, int colIndexB){
	double distance=0;
	for(int i=0;i<A.n_rows;++i){
		double elementA=A(i,colIndexA);
		double elementB=B(i,colIndexB);
		double diff=elementA-elementB;
		distance+=diff*diff;
	}
	return sqrt(distance);
}

static mat getSqrtMat(mat A){
79
80
81
82
83
84
85
86
87
88
89
90

for(int i=0;i<A.n_rows;++i){
    double curVal = A(i,i);
    A(i,i) = sqrt(curVal);
}
return A;
}

static mat getSqrtMatDiag(mat A){
for(int i=0;i<A.n_rows;++i){
    double curVal = A(i,i);
    A(i,i) = sqrt(curVal);
Sascha Niklas Schneiders's avatar
Sascha Niklas Schneiders committed
91
}
92
93
94
return A;
}

Sascha Niklas Schneiders's avatar
Sascha Niklas Schneiders committed
95
96
97
98
99
100
101
102
103
static mat invertDiagMatrix(mat A){
for(int i=0;i<A.n_rows;++i){
    double curVal = A(i,i);
    A(i,i) = 1/curVal;
}
return A;
}
};
#endif