-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp_norm_encoder.hpp
113 lines (92 loc) · 2.97 KB
/
p_norm_encoder.hpp
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
//
// Created by DongHyeon Ryu on 22. 12. 5..
//
#ifndef LIB_FFHIPE_P_NORM_ENCODER_HPP
#define LIB_FFHIPE_P_NORM_ENCODER_HPP
#include <ffhipe.h>
#include <vector>
#include "encoder.hpp"
namespace ffhipe {
template<typename T>
class PNormEncoder : public ffhipe::CEncoder<T> {
public:
explicit PNormEncoder(ULONG _p): p(_p){};
ULONG getN(ULONG n) override {
return n;
}
/**
*
* @param x a normal state vector
* @param encodedX
* @return encodedX
*/
VECTOR<MSG> & encodeX(const std::vector<T> &x, VECTOR<MSG> &encodedX) override {
long n = x.size();
long encoded_dimension = (p - 1) * n + 2;
encodedX.SetLength(encoded_dimension);
std::vector<T> _x = std::vector<T>(x);
long coef;
encodedX[0] = get_signed_binomial_coefficient(p, p);
for (ULONG i = 0; i < p-1; i++)
{
coef = get_signed_binomial_coefficient(p, p - i - 1);
for (ULONG j = 0; j < n; j++)
{
encodedX[i * n + j + 1] = _x[j] * coef;
_x[j] *= x[j];
}
}
for (ULONG j = 0; j < n; j++)
{
encodedX[encoded_dimension - 1] += _x[j];
}
return encodedX;
}
/**
*
* @param y current state vector to be examined if it is abnormal or not.
* @param encodedY
* @return encodedY
*/
VECTOR<MSG> & encodeY(const std::vector<T> &y, VECTOR<MSG> &encodedY) override {
long n = y.size();
long encoded_dimension = (p - 1) * n + 2;
encodedY.SetLength(encoded_dimension);
std::vector<T> _y = std::vector<T>(y);
encodedY[encoded_dimension-1] = 1;
for (ULONG i = p-1; i > 0; i--)
{
for (ULONG j = 0; j < n ; j++)
{
encodedY[i * n - j] = _y[n - j - 1];
_y[n - j - 1] *= y[n - j - 1];
}
}
for (ULONG j = 0; j < n; j++)
{
encodedY[0] += _y[j];
}
return encodedY;
}
private:
const ULONG p;
std::vector<LONG> nC;
/**
* Time Complexity is O(k). However once calculate nCk the intermediate values are all stored.
* Space complexity is O(k).
*/
LONG get_signed_binomial_coefficient(const ULONG n, const ULONG k)
{
if(this->nC.empty()) {
nC = std::vector<LONG>(n + 1);
nC[0] = nC[n] = 1;
for (ULONG i = 1; i < n; ++i) {
nC[i] = nC[i - 1] * (n + 1 - i) / i ;
}
}
LONG sign = (k % 2 == 0 ? 1 : -1);
return nC[k] * sign;
}
};
}
#endif //LIB_FFHIPE_P_NORM_ENCODER_HPP