-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.cpp
107 lines (93 loc) · 2.22 KB
/
tools.cpp
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
#include "tools.hpp"
int thread_count;
int maxIndx(double *pred_in, int size){
double temp = pred_in[0];
int indx = 0;
for (int i = 1; i < size; i++)
{
if (pred_in[i] > temp){
indx = i;
temp = pred_in[i];
}
}
return indx;
}
double norm(double *A, int n){
double temp=0.0;
for(int i=0; i<n; i++){
temp+= A[i]*A[i];
}
return sqrt(temp);
}
void VecNormalizer(double *A, int n){
double mod = norm(A,n);
for(int i=0; i<n; i++){
A[i]/=mod;
}
}
void printVec(double *v, int vec_size){
cout<<"[";
for (int i = 0; i < vec_size-1; i++)
{
cout<<v[i]<<",";
}
cout<<v[vec_size-1]<<"]"<<endl;
}
void getS(double **S, double **X, double **Xm, double **Xm_t, double *V, int X_rows, int X_cols){
// Get Xm (all column with mu_j = 0)
int mu;
for(int pi=0; pi<X_cols; pi++){
mu = 0;
for(int ni=0; ni<X_rows; ni++){
mu += X[ni][pi];
}
mu = mu/((double)X_rows);
V[pi] = mu;
for(int ni=0; ni<X_rows; ni++){
Xm[ni][pi] = X[ni][pi] - mu;
}
}
cout<<"\n->\tXm"<<endl;
// Get transpose
for(int ni=0; ni<X_rows; ni++){
for(int pi=0; pi<X_cols; pi++){
Xm_t[pi][ni] = Xm[ni][pi];
}
}
cout<<"->\tXm_t\t= Xm.T"<<endl;
// Get S
double temp;
for(int sr=0; sr<X_rows; sr++){
for(int sc=0; sc<X_rows; sc++){
temp=0.0;
for(int it=0; it<X_cols; it++){
temp += Xm[sr][it]*Xm_t[it][sc];
}
S[sr][sc] = temp;
}
}
cout<<"-> \tS\t= Xm*Xm_t"<<endl;
}
void getW(double **A, double **B, double **W, int m, int n){
cout<<"\nGetting W ...\n"<<endl;
double temp;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
temp = 0.0;
for(int k=0; k<n; k++){
temp += A[i][k]*B[k][j];
}
W[i][j] = temp;
}
}
double *vecW_j = new double[m];
for(int jW=0; jW<n; jW++){
for(int iW=0; iW<m; iW++){
vecW_j[iW] = W[iW][jW];
}
VecNormalizer(vecW_j, m);
for(int iW=0; iW<m; iW++){
W[iW][jW] = vecW_j[iW];
}
}
}