```  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
```
```//! Calculation graph

use ndarray::*;
use ndarray_linalg::*;
use petgraph;
use petgraph::prelude::*;

use operators::*;
use error::*;

/// Node of the calculation graph.
///
/// This struct keeps the last value, and `Graph` calculates the derivative
/// using this value.
#[derive(Debug, Clone)]
pub struct Node<A: Scalar> {
value: Option<Value<A>>,
deriv: Option<Value<A>>,
prop: Property,
}

/// Extra propaties of the `Node` accoding to the node type.
#[derive(Debug, Clone, IntoEnum)]
enum Property {
Variable(Variable),
UnaryOperator(UnaryOperatorAny),
BinaryOperator(BinaryOperatorAny),
}

impl<A: Scalar> Node<A> {
fn new(prop: Property) -> Self {
Self {
value: None,
deriv: None,
prop,
}
}

/// Check the node is variable
pub fn is_variable(&self) -> bool {
match self.prop {
Property::Variable(_) => true,
Property::UnaryOperator(_) => false,
Property::BinaryOperator(_) => false,
}
}
}

#[derive(Debug, Clone)]
struct Variable {
name: String,
}

impl Variable {
fn new(name: &str) -> Self {
Variable { name: name.to_string() }
}
}

/// Calculation graph based on `petgraph::graph::Graph`
#[derive(Debug, NewType)]
pub struct Graph<A: Scalar>(petgraph::graph::Graph<Node<A>, ()>);

impl<A: Scalar> Graph<A> {
/// new graph.
pub fn new() -> Self {
petgraph::graph::Graph::new().into()
}

/// Create new empty variable
pub fn variable(&mut self, name: &str) -> NodeIndex {
let var = Variable::new(name);
}

/// Create new scalar variable
pub fn scalar_variable(&mut self, name: &str, value: A) -> NodeIndex {
let var = self.variable(name);
self.set_value(var, value).unwrap();
var
}

/// Create new vector variable
pub fn vector_variable(&mut self, name: &str, value: Array<A, Ix1>) -> NodeIndex {
let var = self.variable(name);
self.set_value(var, value).unwrap();
var
}

/// Set a value to a variable node, and returns `NodeTypeError` if the node is an operator.
pub fn set_value<V: Into<Value<A>>>(&mut self, node: NodeIndex, value: V) -> Result<()> {
if self[node].is_variable() {
self[node].value = Some(value.into());
Ok(())
} else {
Err(NodeTypeError {}.into())
}
}

pub fn add(&mut self, lhs: NodeIndex, rhs: NodeIndex) -> NodeIndex {
p
}

pub fn neg(&mut self, arg: NodeIndex) -> NodeIndex {
n
}

pub fn sub(&mut self, lhs: NodeIndex, rhs: NodeIndex) -> NodeIndex {
let m_rhs = self.neg(rhs);
}

fn get_arg1(&mut self, op: NodeIndex) -> NodeIndex {
let mut iter = self.neighbors_directed(op, Direction::Incoming);
iter.next().unwrap()
}

fn get_arg2(&mut self, op: NodeIndex) -> (NodeIndex, NodeIndex) {
let mut iter = self.neighbors_directed(op, Direction::Incoming);
let rhs = iter.next().unwrap();
let lhs = iter.next().unwrap();
(lhs, rhs)
}

/// Get the value of the node. If the value has not been caluclated,
/// returns `None`
pub fn get_value(&self, node: NodeIndex) -> Option<&Value<A>> {
self[node].value.as_ref()
}

/// Get the value of the node. If the value has not been caluclated,
/// returns `None`
pub fn get_deriv(&self, node: NodeIndex) -> Option<&Value<A>> {
self[node].deriv.as_ref()
}

/// Evaluate the value of the node recusively.
///
/// * `use_cached` - Use the value if already calculated.
pub fn eval_value(&mut self, node: NodeIndex, use_cached: bool) -> Result<()> {
// FIXME This code traces the graph twice, but it may be able to done by once.
let prop = self[node].prop.clone();
let value_exists = self[node].value.is_some();
match prop {
Property::Variable(ref v) => {
if value_exists {
return Ok(());
}
panic!("Variable '{}' is evaluated before set value", v.name)
}
Property::UnaryOperator(ref op) => {
if use_cached && value_exists {
return Ok(());
}
let arg = self.get_arg1(node);
self.eval_value(arg, use_cached)?;
let res = op.eval_value(self.get_value(arg).unwrap())?;
self[node].value = Some(res);
}
Property::BinaryOperator(ref op) => {
if use_cached && value_exists {
return Ok(());
}
let (lhs, rhs) = self.get_arg2(node);
self.eval_value(rhs, use_cached)?;
self.eval_value(lhs, use_cached)?;
let res = op.eval_value(
self.get_value(lhs).unwrap(),
self.get_value(rhs).unwrap(),
)?;
self[node].value = Some(res);
}
};
Ok(())
}

fn deriv_recur(&mut self, node: NodeIndex, der: Value<A>) -> Result<()> {
self[node].deriv = Some(der);
let prop = self[node].prop.clone();
match prop {
Property::Variable(_) => {}
Property::UnaryOperator(ref op) => {
let arg = self.get_arg1(node);
let der = op.eval_deriv(
self.get_value(arg).unwrap(),
self.get_deriv(node).unwrap(),
)?;
self.deriv_recur(arg, der)?;
}
Property::BinaryOperator(ref op) => {
let (lhs, rhs) = self.get_arg2(node);
let (l_der, r_der) = op.eval_deriv(
self.get_value(lhs).unwrap(),
self.get_value(rhs).unwrap(),
self.get_deriv(node).unwrap(),
)?;
self.deriv_recur(lhs, l_der)?;
self.deriv_recur(rhs, r_der)?;
}
};
Ok(())
}

/// Evaluate derivative recursively.
pub fn eval_deriv(&mut self, node: NodeIndex) -> Result<()> {
self.deriv_recur(node, Value::identity())
}
}
```