-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_functions.h
More file actions
104 lines (95 loc) · 2.16 KB
/
basic_functions.h
File metadata and controls
104 lines (95 loc) · 2.16 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 <torch/torch.h>
#include <iostream>
#include <stdlib.h>
#include <torch/csrc/autograd/variable.h>
#include <cmath>
//using namespace std;
torch::Tensor function()
{
torch::Tensor a = torch::ones({3, 33});
return a;
}
torch::Tensor binaryVector(torch::Tensor a, int n, int q) {
int l=floor(log2(q)+1);
torch::Tensor b = torch::ones({(n+1)*l, 1});
int i=0, j=0;
torch::Tensor k;
while(i<=n) {
j=0;
k=a[i];
while(j<l) {
//std::cout<<b<<std::endl;
b[i*l+l-j-1]=torch::remainder(k,2);
k=(k-torch::remainder(k,2))/2;
j++;
}
i++;
}
return b;
}
torch::Tensor binary(torch::Tensor a, int l = 11)
{
torch::Tensor b = torch::ones({1,l});
int index = 0;
while (l)
{
b[0][index++] = a % 2;
a = (a - a % 2) / 2;
l--;
}
return b;
}
torch::Tensor bit_decomp(torch::Tensor temp, int rows = 5, int k = 3, int l = 11)
{
torch::Tensor b = torch::ones({rows, k * l});
for (int y = 0; y < rows; y++)
{
int i = 0, size = 0;
for (int i = 0; i < k; i++)
{
torch::Tensor c = binary(temp[y][i], l);
//std::cout << temp[y][i];
for (int j = 0; j < l; j++)
{
b[y][size++] = c[0][j];
}
}
}
//std::cout << "Bit Decomp done" << b;
return b;
}
torch::Tensor bit_decomp_inverse(torch::Tensor temp, int rows = 5, int k = 3, int l = 11)
{
torch::Tensor b = torch::zeros({rows, k});
for (int y = 0; y < rows; y++)
{
int i = 0, size = 0, N = k * l, count = 0;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < l; j++)
{
b[y][i] += pow(2, j) * temp[y][count++];
//std::cout << "done here";
}
}
}
//std::cout << "Bit Decomp inverse done" << b;
return b;
}
torch::Tensor flatten(torch::Tensor temp, int r = 5, int k = 3, int l = 11)
{
torch::Tensor b = bit_decomp(bit_decomp_inverse(temp, r, k, l), r, k, l);
return b;
}
torch::Tensor powersof2(torch::Tensor b, int n = 3, int q = 11) {
int l=floor(log2(q)+1);
torch::Tensor ans = torch::zeros({(n+1)*(l), 1});
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < l; j++)
{
ans[i*(l)+j] = torch::remainder(pow(2,j)*b[i],q);
}
}
return ans;
}