-
Notifications
You must be signed in to change notification settings - Fork 2
/
converter.cc
111 lines (103 loc) · 2.11 KB
/
converter.cc
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
# include <converter.h>
# include <math.h>
Converter::Converter(Data x,int s,int d)
{
weight.resize(x.size());
weight=x;
sigcount = s;
dimension = d;
nodes = x.size()/(d+2);
}
void Converter::add(int x,vector<int> &g,int &pos)
{
if(pos>=g.size())
{
g.resize(pos+10);
}
g[pos++]=x;
}
void Converter::makeNumber(double out,vector<int> &g,int &pos)
{
if(out>=0) add(0,g,pos); else add(1,g,pos);
char ss[100];
sprintf(ss,"%.5lf",fabs(out));
string leftpart="",rightpart="";
int i;
for(i=0;ss[i]!='.';i++) leftpart=leftpart+ss[i];
for(i++;i<strlen(ss);i++) rightpart=rightpart+ss[i];
for(int i=0;i<leftpart.size();i++)
{
if(i!=leftpart.size()-1) add(1,g,pos); else add(0,g,pos);
int digit=leftpart[i]-'0';
add(digit,g,pos);
}
for(int i=0;i<rightpart.size();i++)
{
if(i!=rightpart.size()-1) add(1,g,pos); else add(0,g,pos);
int digit=rightpart[i]-'0';
add(digit,g,pos);
}
}
void Converter::makeOut(vector<int> &g,int &pos)
{
double out=weight[(dimension+2)*nodecounter-(dimension+1)-1];
makeNumber(out,g,pos);
}
void Converter::makeBias(vector<int> &g,int &pos)
{
double out=weight[(dimension+2)*nodecounter-1];
makeNumber(out,g,pos);
}
void Converter::makeWeight(vector<int> &g,int &pos)
{
double out=weight[(dimension+2)*nodecounter-(dimension+1)+xcounter-1];
makeNumber(out,g,pos);
}
void Converter::makeX(vector<int> &g,int &pos)
{
add(xcounter-1,g,pos);
}
void Converter::makeNode(vector<int> &g,int &pos)
{
xcounter=0;
list.resize(0);
for(int i=1;i<=dimension;i++)
{
double v=weight[(dimension+2)*nodecounter-(dimension+1)+i-1];
if(fabs(v)>1e-7) list.push_back(i);
}
//
for(int i=0;i<list.size();i++)
{
xcounter=list[i];
if(i==list.size()-1) add(0,g,pos); else add(1,g,pos);
makeWeight(g,pos);
makeX(g,pos);
}
makeBias(g,pos);
nodecounter++;
}
void Converter::convert(vector<int> &g)
{
for(int i=0;i<g.size();i++) g[i]=0;
int pos=0;
nodecounter=1;
if(sigcount==1)
{
add(0,g,pos);
makeOut(g,pos);
makeNode(g,pos);
}
else
{
for(int i=1;i<sigcount;i++)
{
add(1,g,pos);
makeOut(g,pos);
makeNode(g,pos);
}
add(0,g,pos);
makeOut(g,pos);
makeNode(g,pos);
}
}