-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_main.cc
75 lines (57 loc) · 1.68 KB
/
simple_main.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
// Author: [email protected] (Afroz Mohiuddin)
#include <iostream>
#include "node.h"
#include "node_utils.h"
#include "add_node.h"
#include "multiply_node.h"
#include "constant_node.h"
#include "input_node.h"
using flow_graphs::AddNode;
using flow_graphs::ConstantNode;
using flow_graphs::MultiplyNode;
using flow_graphs::Node;
using flow_graphs::Nodes;
using flow_graphs::InputNode;
using flow_graphs::NodeUtils;
using flow_graphs::Value;
void SetInputs(InputNode* x, InputNode* y, Value x_val, Value y_val) {
x->set_value(x_val);
y->set_value(y_val);
}
int main(int argc, char **argv) {
// In here we'll show the flow graph to calculate
// 2x^2 + 3xy
// TODO(afro): Draw this somehow.
// ---
// |*|
// ---
//
// --- --- --- ---
// |2| |x| |3| |y|
// --- --- --- ---
ConstantNode c2("2", 2.0);
ConstantNode c3("3", 3.0);
InputNode x("x");
InputNode y("y");
MultiplyNode m1("2x^2");
MultiplyNode m2("3xy");
AddNode a("2x^2 + 3xy");
NodeUtils::AddEdge(&m1, &c2);
NodeUtils::AddEdge(&m1, &x);
NodeUtils::AddEdge(&m1, &x);
NodeUtils::AddEdge(&m2, &c3);
NodeUtils::AddEdge(&m2, &x);
NodeUtils::AddEdge(&m2, &y);
NodeUtils::AddEdge(&a, &m1);
NodeUtils::AddEdge(&a, &m2);
Nodes execution_order{&m1, &m2, &a};
NodeUtils::ResetTree(&a);
SetInputs(&x, &y, 5, 8);
NodeUtils::ComputeValues(&execution_order);
std::cout << "Final value of " << a.name() << " is: " << a.value() << std::endl;
NodeUtils::ResetTree(&a);
SetInputs(&x, &y, 1, 2);
NodeUtils::ComputeValues(&execution_order);
std::cout << "Final value of " << a.name() << " is: " << a.value() << std::endl;
return 0;
}