HelperA.h 3.22 KB
Newer Older
Nicola Gatto's avatar
Nicola Gatto 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#ifndef HELPERA_H
#define HELPERA_H
#include <iostream>
#include "armadillo"
#include <stdarg.h>
#include <initializer_list>
#include <fstream>
using namespace arma;
#ifndef _FILESTRING_CONVERSION___A
#define _FILESTRING_CONVERSION___A
void toFileString(std::ofstream& myfile, mat A){
    myfile << "[";
    for (int i = 0; i < A.n_rows; i++){
        for (int j = 0; j < A.n_cols; j++){
            myfile << A(i,j);
            if(j + 1 < A.n_cols){
                myfile << ", ";
            }
        }
        if(i + 1 < A.n_rows){
            myfile << ";";
        }
    }
    myfile << "]";
}
void toFileString(std::ofstream& myfile, double A){
    myfile << A;
}
void toFileString(std::ofstream& myfile, float A){
    myfile << A;
}
void toFileString(std::ofstream& myfile, int A){
    myfile << A;
}
void toFileString(std::ofstream& myfile, bool A){
    myfile << A;
}
bool Is_close(mat& X, mat& Y, double tol)
{
    // abs returns a mat type then max checks columns and returns a row_vec
    // max used again will return the biggest element in the row_vec
    bool close(false);
    if(arma::max(arma::max(arma::abs(X-Y))) < tol)
    {
        close = true;
    }
    return close;
}
#endif
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);
/*printf("cluster centroid calculation done\n");
std::ofstream myfile;
     myfile.open("data after cluster.txt");
     myfile << A;
     myfile.close();
	 
	 std::ofstream myfile2;
     myfile2.open("cluster centroids.txt");
     myfile2 << clusters;
     myfile2.close();*/
mat indexedData=getKMeansClustersIndexData(A.t(), clusters);

/*std::ofstream myfile3;
     myfile3.open("data after index.txt");
     myfile3 << indexedData;
     myfile3.close();
	 */
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){
    cx_mat result=sqrtmat(A);
    return real(result);
}

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

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