basic_functions.h
3.34 KB
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
#include <stdio.h>
size_t* sortIndx(float* input, size_t size){
//sort indices of score in ascending order (fitness value)
size_t *idx;
idx = (size_t*) malloc (size * sizeof (size_t));
for (size_t i = 0; i < size; i++)
idx[i] = i;
for (size_t i=0; i<size; i++){
for (size_t j=i+1; j<size; j++){
if (input[idx[i]] < input[idx[j]]){
std::swap (idx[i], idx[j]); //float check : it was like this b(&idx[i], &idx[j]) but gave me error
}
}
}
return idx; //use as sortSIdx in selection
}
template<typename T>
void mtxMul(T* M3, T* M1, T* M2, size_t r1, size_t c1, size_t r2, size_t c2){
//compute output matrix M3 of size row1 X column2 and data is column major
for(size_t i = 0 ; i <r1; i++){
for(size_t j = 0; j< c2; j++){
T temp = 0;
for(size_t k = 0; k < c1 ; k++){ //column1 = row2 for matrix multiplication
temp+= M1[i * c1 + k] * M2[k * c2 + j]; //compute an element of output matrix
}
M3[i * c1 + j] = temp; //copy an element to output matrix
}
}
}
template<typename T>
void mtxMultranspose(T* M3, T* M1, T* M2, size_t r1, size_t c1, size_t r2, size_t c2){
//compute output matrix M3 of size row1 X column2 and data is column major
for(size_t i = 0 ; i <r1; i++){
for(size_t j = 0; j< r2; j++){
T temp = 0;
for(size_t k = 0; k < c1 ; k++){ //column1 = row2 for matrix multiplication
temp+= M1[i * c1 + k] * M2[j * c2 + k]; //compute an element of output matrix
}
M3[i * r1 + j] = temp; //copy an element to output matrix
}
}
}
template<typename T>
void mtxOutputFile(std::string filename, T* M, size_t rows, size_t cols){
std::cout<<"Outputting "<<rows<<" x "<<cols<<" matrix to file: "<<filename<<std::endl;
ofstream outfile(filename.c_str()); //open a file for writing
//output the matrix
for(size_t r = 0; r < rows; r++){
for(size_t c = 0; c < cols; c++){
outfile<<M[r * cols + c];
if(c != cols-1)
outfile<<",";
}
if(r != rows - 1)
outfile<<std::endl;
}
outfile.close();
//outfile<<"This is a test."<<std::endl;
}
//display within class scatter
template<typename T>
void displayS(T* sw, size_t f){
for(size_t g = 0; g<1; g++){
std::cout<<std::endl;
for(size_t j = 0; j < f; j++){ //total number of features in a gnome
for(size_t k = 0; k < f; k++){ //total number of features in a gnome
std::cout<<sw[g*f*f + j*f + k]<<" ";
}
std::cout<<std::endl;
}
}
std::cout<<std::endl;
}
//sort eigenvalues from lapacke results
size_t* sortEigenVectorIndx(float* eigenvalue, size_t N){
//sort indices of score in ascending order (fitness value)
size_t *idx = (size_t*) malloc (N * sizeof (size_t));
for (size_t i = 0; i < N; i++)
idx[i] = i;
for (size_t i=0; i<N; i++){
for (size_t j=i+1; j<N; j++){
if (eigenvalue[idx[i]] > eigenvalue[idx[j]]){
std::swap (idx[i], idx[j]); //float check : it was like this b(&idx[i], &idx[j]) but gave me error
}
}
}
std::cout<<"best eigenvalue index: "<<eigenvalue[idx[0]]<<std::endl;
return idx; //use as sortSIdx in selection
}