-
Notifications
You must be signed in to change notification settings - Fork 0
/
rephraseRel.m
239 lines (223 loc) · 6.78 KB
/
rephraseRel.m
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function [adderRel, multRel] = rephraseRel(relation)
displayRelation(relation); % display relationship inputed
multRel = [];
pq = PriorityQueue();
for rel = relation
addedList = sort(rel.comps);
if ~pq.contains(addedList)
pq.insert( addedList , size(addedList,2) );
end
end
index = 0;
while ~pq.isEmpty()
list = pq.pop();
if size(list,2) <= 1
continue;
end
if size(list,2) == 2
index = index + 1;
multRel(index).list = list;
multRel(index).a.t = 0; % for adder
multRel(index).b.t = 0; % for adder
multRel(index).a.i = list(1);
multRel(index).b.i = list(2);
continue;
end
flag = 0;
for i = size(multRel, 2): -1 : 1
[flag, diffList] = includedIn(multRel(i).list, list);
if flag
if size(diffList, 2) == 1
index = index + 1;
multRel(index).list = list;
multRel(index).a.t = 1; % for adder
multRel(index).b.t = 0; % for multiplier
multRel(index).a.i = i;
multRel(index).b.i = diffList(1);
break;
end
found = 0;
for j = size(multRel, 2): -1 : 1
if compareElement(multRel(j).list, diffList) == 0
index = index + 1;
multRel(index).list = list;
multRel(index).a.t = 1; % for multiplier
multRel(index).b.t = 1; % for multiplier
multRel(index).a.i = i;
multRel(index).b.i = j;
found = 1;
break;
end
end
if found == 0
if ( minCompList(list) >= minCompList(diffList) + 1 )
pq.insert(list, size(list, 2));
pq.insert(diffList, size(diffList, 2));
else
flag = 0;
continue;
end
end
break;
end
end
if flag == 0
pq.insert(list, size(list, 2) );
[a , b] = splitList(list)
if size(b, 2) == 0
s = floor( size(a, 2) / 2 );
if s == 1
pq.insert(list(1: 2), 2);
else
pq.insert(list(1: s), s);
end
else
pq.insert(a, size(a, 2));
pq.insert(b, size(b, 2));
end
end
end
for rel = relation
added.coefficient = rel.coefficient ;
added.order = rel.order ;
if size(rel.comps, 2) == 1
added.multer.t = 0;
added.multer.i = rel.comps;
else
for i = 1 : size(multRel, 2)
if compareElement(rel.comps, multRel(i).list) == 0
added.multer.t = 1;
added.multer.i = i;
break;
end
end
end
if ~ isfield( added , 'multer')
display(rel);
error('cannot find needed multiplier!');
end
if exist( 'adderRel' , 'var' )
if size(adderRel, 2) < rel.addTo
len = 0;
else
len = size( adderRel(rel.addTo).list , 2);
end
adderRel(rel.addTo).list(len+1) = added;
else
adderRel(rel.addTo).list(1).coefficient = added.coefficient;
adderRel(rel.addTo).list(1).order = added.order;
adderRel(rel.addTo).list(1).multer = added.multer;
end
end
displayConvertedRel(adderRel, multRel); % display processed relationship
end
function [ret, diffList] = includedIn(a, b)
ret = 0;
diffList = b;
for x = a
found = 0;
for i = 1 : size(diffList,2)
if diffList(i) == x
diffList(i) = [];
found = 1;
break;
end
end
if found == 0
return;
end
end
ret = 1;
if size(diffList,1) == 0 % two set equal, no need to worry
error('identical comps');
end
end
function displayRelation(relation)
display('Input relation meaning');
for k = relation
compstr = '';
for i = 1 : size(k.comps,2)
if i ~= 1
compstr = strcat(compstr, ' * ');
end
compstr = strcat(compstr, sprintf('Comp%d', k.comps(i)) );
end
display(sprintf('Comp%d += %s * t^%d * %s', k.addTo , ...
realNum(k.coefficient), k.order, compstr));
end
display(' ');
end
function displayConvertedRel(adderRel, multRel)
display('Output computational model meaning');
for i = 1 : size(adderRel, 2)
adder = '';
for j = 1 : size(adderRel(i).list, 2)
added = sprintf(' %s * t^%d * %s ' , realNum(adderRel(i).list(j).coefficient), ...
adderRel(i).list(j).order, str(adderRel(i).list(j).multer) );
if j ~= 1
adder = strcat(adder, ' + ');
end
adder = strcat(adder, added);
end
display(sprintf('Adder %d''\t=%s', i , adder ) );
end
for i = 1 : size(multRel, 2)
list = '';
for k = multRel(i).list
list = strcat(list, sprintf(' %d',k));
end
display(sprintf('Multer%d''\t= %s * %s \t\t\t (%s )', ...
i, str(multRel(i).a), str(multRel(i).b), list));
end
display(' ');
end
function s = str(a)
if a.t == 0
s = sprintf('Adder %d', a.i);
else
s = sprintf('Multer%d', a.i);
end
end
function s = realNum(x)
if ceil(x) == x
s = sprintf('%d', x);
else
[N,D] = rat(x);
s = sprintf('%d/%d', N, D);
end
end
function [totalCount] = minCompList(list)
totalCount = 0;
if size(list, 2) == 0
return
end
num = list(1);
sameCount = 1;
for i = 2 : size(list, 2)
if ( num == list(i))
sameCount = sameCount + 1;
else
totalCount = totalCount + 1 + minCompMono(sameCount);
sameCount = 1;
num = list(i);
end
end
totalCount = totalCount + minCompMono(sameCount);
end
function [count] = minCompMono(c)
count = -2;
while (c ~= 0)
count = count + mod(c, 2) + 1;
c = floor( c / 2 );
end
end
function [a, b] = splitList(list)
for i = 1 : size(list,2)
if list(i) ~= list(1)
i = i - 1;
break;
end
end
a = list(1:i);
b = list(i+1:end);
end