-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
183 lines (137 loc) · 3.74 KB
/
test.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
#include <numeric>
#include <Eigen/Dense>
#define BIT_SIZE 64
typedef uint8_t smallType;
typedef uint64_t myType;
using namespace std;
using namespace Eigen;
double DReLU(double x)
{
if (x > 0.0){
return 1;
}
else{
return 0;
}
}
MatrixXd ReLU(MatrixXd X)
{
MatrixXd X_DReLU = X.unaryExpr(&DReLU);
MatrixXd res = X.cwiseProduct(X_DReLU);
// MatrixXd res = X_DReLU.array() * X.array();
// res = res.array().abs();
return res.cwiseAbs();
}
MatrixXd Softmax(MatrixXd X)
{
MatrixXd res(X.rows(), X.cols());
MatrixXd X_e = X.array().exp();
cout << X_e << endl;
VectorXd X_sum= X_e.rowwise().sum();
cout << X_sum << endl;
for (int i = 0; i < X.rows(); i++)
{
res.row(i) = X_e.row(i)/X_sum(i,0);
}
return res;
}
int main()
{
// MatrixXd X = MatrixXd::Random(3,3);
// cout << X << endl;
// MatrixXd Y = MatrixXd::Random(3,3);
// cout << Y << endl;
// MatrixXd Z = X * Y;
// cout << Z << endl;
// VectorXi argmax(X.rows());
// cout << argmax.rows() << "," << argmax.cols() << endl;
// for (int i = 0; i < X.rows(); i++){
// // cout << i << endl;
// X.row(i).maxCoeff(&argmax[i]);
// }
// cout << argmax << endl;
// MatrixXd X_log = X.array().log();
// cout << X_log << endl;
// MatrixXd X_mult = X.cwiseProduct(X_log);
// cout << X_mult.sum() << endl;
// MatrixXd X_R = ReLU(X);
// cout << X_R << endl;
// MatrixXd X_S = Softmax(X);
// cout << X_S<< endl;
// decimal to binary conversion
// string binary = bitset<64>(128).to_string(); //to binary
// cout << binary << endl;
// cout << binary[54] << endl;
// cout << binary[56] << endl;
// unsigned long decimal = bitset<64>(binary).to_ulong();
// cout << decimal<<"\n";
// cout << sizeof(myType)*CHAR_BIT << endl;
// for (int k = 0; k < BIT_SIZE; k++ )
// {
// cout << ((a >> (BIT_SIZE - 1 - k)) & 1) ;
// }
// cout << endl;
uint64_t a = -45;
cout << "a: " << a << endl;
uint64_t b = 234;
cout << "b: " << b <<endl;
uint64_t c = a^b;
cout << endl;
string a_s = bitset<64>(a).to_string(); // getting the binary representation string
cout << "a: " << a_s << endl;
vector<int> a_bits;
for (int i = 0; i < a_s.length(); i ++ ) // converting the binary string to int array
{
a_bits.push_back((int(a_s[i]) - 48));
}
// cout << "a: ";
// for (int i = 0; i < a_bits.size(); i ++ )
// {
// cout << a_bits[i];
// }
// cout << endl;
string b_s = bitset<64>(b).to_string();
cout << "b: " << b_s << endl;
vector<int> b_bits;
for (int i = 0; i < b_s.length(); i ++ ) // converting the binary string to int array
{
b_bits.push_back((int(b_s[i]) - 48));
}
string c_s = bitset<64>(c).to_string();
cout << "c: " << c_s << endl;
vector<int> c_bits;
for (int i = 0; i < c_s.length(); i ++ ) // converting the binary string to int array
{
c_bits.push_back((int(c_s[i]) - 48));
}
vector<int> res;
int H = 0;
for (int i = 0; i < 64; i++)
{
int temp = (b_bits[i] - a_bits[i]) + 1 + H;
res.push_back(temp);
H = H + c_bits[i];
}
int r = 0;
cout << "r: ";
for (int i = 0; i < res.size(); i ++ )
{
cout << res[i];
if (res[i]==0)
{
r = 1;
}
}
cout << endl;
if (r==0){
cout << endl << "a>b: False" << endl;
}
else if (r==1){
cout << endl << "a>b: True" << endl;
}
return 0;
}