-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.m
330 lines (256 loc) · 9.63 KB
/
Node.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
classdef Node < handle & matlab.mixin.Heterogeneous & matlab.mixin.Copyable
properties
parents(1, :) Node
name(1, 1) string
% Used for internal operations
value(1, 1) double
flag(1, 1) logical
new(1, :) Node
children(1, :) Node
scale(1, 2) double
depth(1, 1) double
end
methods
function obj = Node(parents, varargin)
obj.value = NaN;
obj.flag = false;
obj.parents = parents;
obj.scale = [NaN NaN];
obj.depth = -inf;
if nargin > 1
obj.name = varargin{1};
end
end
function cpObj = copyGraph(obj)
obj.setAll('new', Node.empty());
cpObj = obj.copy();
obj.setAll('new', Node.empty());
end
function res = plus(obj1, obj2)
if isa(obj1, 'Node') && isa(obj2, 'Node')
res = Linear([obj1 obj2], [0 1 1]);
elseif isa(obj1, 'Node') && isa(obj2, 'double')
res = Linear(obj1, [obj2 1]);
elseif isa(obj1, 'double') && isa(obj2, 'Node')
res = Linear(obj2, [obj1 1]);
end
end
function res = minus(obj1, obj2)
if isa(obj1, 'Node') && isa(obj2, 'Node')
res = Linear([obj1 obj2], [0 1 -1]);
elseif isa(obj1, 'Node') && isa(obj2, 'double')
res = Linear(obj1, [-obj2 1]);
elseif isa(obj1, 'double') && isa(obj2, 'Node')
res = Linear(obj2, [obj1 -1]);
end
end
function res = mtimes(obj1, obj2)
if isa(obj1, 'Node') && isa(obj2, 'Node')
res = Multiply([obj1 obj2]);
elseif isa(obj1, 'Node') && isa(obj2, 'double')
res = Linear(obj1, [0 obj2]);
elseif isa(obj1, 'double') && isa(obj2, 'Node')
res = Linear(obj2, [0 obj1]);
end
end
end
methods (Sealed)
function simplify(obj)
for i = 1:numel(obj)
obj(i).parents.simplify();
obj(i).simplifyElement();
end
end
function res = eq(H1, H2)
res = eq@handle(H1, H2);
end
function computeScale(obj)
obj.setAll('flag', false);
obj.computeScaleImpl();
end
function computeDepth(obj)
obj.setAll('depth', -inf);
obj.computeDepthImpl(0);
end
function replaceNonlinear(obj)
obj.computeScale();
obj.findChildren();
obj.setAll('flag', false);
obj.replaceNonlinearImpl();
end
function nodes = enumerate(obj)
obj.setAll('flag', false);
nodes = obj.enumerateImpl();
end
function g = plot(obj)
nodes = obj.enumerate();
A = zeros(numel(nodes));
for i = 1:numel(nodes)
for p = nodes(i).parents
j = find(nodes == p, 1);
A(i, j) = 1;
end
end
nodetypes = strings(size(nodes));
for i = 1:numel(nodetypes)
nodetypes(i) = class(nodes(i)) + string(i);
end
names = [nodes.name];
names(names == "") = nodetypes(names == "");
% obj.computeScale();
% sc = reshape([nodes.scale], 2, numel(nodes));
% names = names + ", " + "[" + string(sc(1, :)) + " " + string(sc(2, :)) + "]";
% obj.eval([0.8 sin(0.1) cos(0.1)]);
% names = names + ", " + fillmissing(string([nodes.value]), 'constant', "NaN");
% obj.computeDepth();
% names = names + ", " + [nodes.depth];
G = digraph(A, cellstr(names));
g = plot(G);
end
end
methods (Access = protected)
function inputs = findInputsImpl(obj)
inputs = Input.empty();
for i = 1:numel(obj.parents)
if ~obj.parents(i).flag
inputs = [inputs obj.parents(i).findInputsImpl()];
end
end
obj.flag = true;
end
function simplifyElement(~)
end
function [headObj, tailObj] = replaceNonlinearElement(obj)
headObj = obj;
tailObj = obj;
end
end
methods (Access = protected, Sealed)
function cpObj = copyElement(obj)
% Error when using copy() instead of copyGraph()
if ~any(arrayfun(@(s)strcmp(s.name, 'Node.copyGraph'), dbstack()))
error('Use copyGraph() instead of copy() to duplicate part of a graph');
end
if ~isempty(obj.new)
cpObj = obj.new;
return
end
cpObj = [email protected](obj);
obj.new = cpObj;
for i = 1:numel(obj.parents)
cpObj.parents(i) = cpObj.parents(i).copy();
end
end
function setAll(obj, field, value)
for i = 1:numel(obj)
obj(i).(field) = value;
obj(i).parents.setAll(field, value);
end
end
function values = evalImpl(obj)
% Evaluate an array of nodes
values = zeros(size(obj));
for i = 1:numel(obj)
% If there is no memoized value, calculate it and set the flag
if ~obj(i).flag
obj(i).value = obj(i).evalElement();
obj(i).flag = true;
end
values(i) = obj(i).value;
end
end
function findChildren(obj)
obj.setAll('children', Node.empty());
obj.setAll('flag', false);
obj.findChildrenImpl();
end
function findChildrenImpl(obj)
for i = 1:numel(obj)
if ~obj(i).flag
for j = 1:numel(obj(i).parents)
if ~any(obj(i).parents(j).children == obj(i))
obj(i).parents(j).children(end + 1) = obj(i);
end
end
obj(i).flag = true;
end
obj(i).parents.findChildrenImpl();
end
end
function nodes = enumerateImpl(obj)
nodes = Node.empty();
for i = 1:numel(obj)
if ~obj(i).flag
nodes = [nodes obj(i) obj(i).parents.enumerateImpl()];
obj(i).flag = true;
end
end
end
function computeScaleImpl(obj)
for i = 1:numel(obj)
if ~obj(i).flag
obj(i).parents.computeScaleImpl();
obj(i).computeScaleElement();
obj(i).flag = true;
end
end
end
function computeDepthImpl(obj, depth)
for i = 1:numel(obj)
obj(i).depth = max(obj(i).depth, depth + obj(i).getCompiledHeight());
obj(i).parents.computeDepthImpl(obj(i).depth);
end
end
function replaceNonlinearImpl(obj)
for i = 1:numel(obj)
[headObj, tailObj] = obj(i).replaceNonlinearElement();
obj(i).replaceNode(headObj, tailObj);
obj(i).parents.replaceNonlinearImpl();
end
end
function headObj = getPassthrough(obj)
m = mean(obj.scale);
s = 10 * diff(obj.scale) / 2;
headObj = Linear(Tanh(Linear(obj, [-m/s 1/s])), [m s]);
headObj.computeDepthImpl(obj.depth);
end
function normalizeChildDepthImpl(obj)
for i = 1:numel(obj)
if ~obj(i).flag
obj(i).normalizeChildDepthElement();
obj(i).children.normalizeChildDepthImpl();
obj(i).flag = true;
end
end
end
function normalizeChildDepthElement(obj)
head = obj;
for d = obj.depth-2:-1:min(min([obj.children.depth]))
head = head.getPassthrough();
obj.children([obj.children.depth] == d).replaceParent(obj, head);
end
end
function replaceNode(obj, headObj, tailObj)
if ~(headObj == obj && tailObj == obj)
obj.parents.replaceChild(obj, tailObj);
obj.children.replaceParent(obj, headObj);
end
end
function replaceParent(obj, old, new)
for i = 1:numel(obj)
obj(i).parents(obj(i).parents == old) = new;
end
end
function replaceChild(obj, old, new)
for i = 1:numel(obj)
obj(i).children(obj(i).children == old) = [];
obj(i).children = [obj(i).children new];
end
end
end
methods (Abstract, Access = protected)
value = evalElement(obj)
computeScaleElement(obj)
height = getCompiledHeight(obj)
end
end