-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculus.cpp
204 lines (180 loc) · 8.51 KB
/
calculus.cpp
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
/*
* Copyright 2020 Casey Sanchez
*/
#include "calculus.hpp"
Calculus::Calculus(std::variant<Scalar, Matrix> const &node_variant, std::map<std::string, std::variant<Scalar, Matrix>> const &node_map) : m_node_variant(node_variant), m_node_map(node_map)
{
}
Scalar Calculus::Partial(Scalar const &with_respect_to_ptr)
{
return Partial(m_node_variant, with_respect_to_ptr);
}
Scalar Calculus::Partial(std::variant<Scalar, Matrix> const &node_variant, Scalar const &with_respect_to_ptr)
{
if (!std::holds_alternative<Scalar>(node_variant)) {
throw std::invalid_argument("Partial only defined for Scalar");
}
Scalar scalar = std::get<Scalar>(node_variant);
// d/dx { x } = 1
if (Node::Equivalent(scalar, with_respect_to_ptr)) {
return std::shared_ptr<ConstantNode>(new ConstantNode(1.0));
}
// d/dx { 1 } = 0
else if (scalar->Type() == "ConstantNode") {
return std::shared_ptr<ConstantNode>(new ConstantNode(0.0));
}
// d/dx { y } = 0
else if (scalar->Type() == "VariableNode") {
return std::shared_ptr<ConstantNode>(new ConstantNode(0.0));
}
// d/dx { f(x) + g(x) } = f'(x) + g'(x)
else if (scalar->Type() == "AdditionNode") {
return std::shared_ptr<AdditionNode>(new AdditionNode({
Partial(scalar->Argument(0), with_respect_to_ptr),
Partial(scalar->Argument(1), with_respect_to_ptr)
}));
}
// d/dx { f(x) - g(x) } = f'(x) - g'(x)
else if (scalar->Type() == "SubtractionNode") {
return std::shared_ptr<SubtractionNode>(new SubtractionNode({
Partial(scalar->Argument(0), with_respect_to_ptr),
Partial(scalar->Argument(1), with_respect_to_ptr)
}));
}
// d/dx { f(x) * g(x) } = f'(x) * g(x) + g'(x) * f(x)
else if (scalar->Type() == "MultiplicationNode") {
return std::shared_ptr<AdditionNode>(new AdditionNode({
std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
Partial(scalar->Argument(0), with_respect_to_ptr),
scalar->Argument(1)
})),
std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
Partial(scalar->Argument(1), with_respect_to_ptr),
scalar->Argument(0)
}))
}));
}
// d/dx { f(x) / g(x) } = { f'(x) * g(x) - g'(x) * f(x) } / { g(x) }^2
else if (scalar->Type() == "DivisionNode") {
return std::shared_ptr<DivisionNode>(new DivisionNode({
std::shared_ptr<SubtractionNode>(new SubtractionNode({
std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
Partial(scalar->Argument(0), with_respect_to_ptr),
scalar->Argument(1)
})),
std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
Partial(scalar->Argument(1), with_respect_to_ptr),
scalar->Argument(0)
}))
})),
std::shared_ptr<ExponentiationNode>(new ExponentiationNode({
scalar->Argument(1),
std::shared_ptr<ConstantNode>(new ConstantNode(2.0))
}))
}));
}
// d/dx { x^2 } = d/dx { e^{ ln(x) * 2 } } = e^{ ln(x) * 2 } * d/dx { ln(x) * 2 } = 2 * x^2 * x^{-1} = 2 * x
else if (scalar->Type() == "ExponentiationNode") {
return Partial(std::shared_ptr<ExpNode>(new ExpNode({
std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
std::shared_ptr<LnNode>(new LnNode({ scalar->Argument(0) })),
scalar->Argument(1)
}))
})), with_respect_to_ptr);
}
// d/dx { e^{ x^2 } } = e^{ x^2 } * d/dx { x^2 } = e^{ x^2 } * 2 * x
else if (scalar->Type() == "ExpNode") {
return std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
scalar,
Partial(scalar->Argument(0), with_respect_to_ptr)
}));
}
// d/dx { ln { x^2 } } = { x^2 }^{-1} * d/dx { x^2 } = 2 * x * x^{-2} = 2 * x^{-1}
else if (scalar->Type() == "LnNode") {
return std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
std::shared_ptr<ExponentiationNode>(new ExponentiationNode({
scalar->Argument(0),
std::shared_ptr<ConstantNode>(new ConstantNode(-1.0))
})),
Partial(scalar->Argument(0), with_respect_to_ptr)
}));
}
else if (scalar->Type() == "SinNode") {
return std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
std::shared_ptr<CosNode>(new CosNode({
scalar->Argument(0)
})),
Partial(scalar->Argument(0), with_respect_to_ptr)
}));
}
else if (scalar->Type() == "CosNode") {
return std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
std::shared_ptr<MultiplicationNode>(new MultiplicationNode({
std::shared_ptr<ConstantNode>(new ConstantNode(-1.0)),
std::shared_ptr<SinNode>(new SinNode({
scalar->Argument(0)
}))
})),
Partial(scalar->Argument(0), with_respect_to_ptr)
}));
}
else {
return std::shared_ptr<ConstantNode>(new ConstantNode(0.0));
}
}
Matrix Calculus::Gradient(Scalar const &with_respect_to_11_ptr, Scalar const &with_respect_to_21_ptr, Scalar const &with_respect_to_31_ptr)
{
return Gradient(m_node_variant, with_respect_to_11_ptr, with_respect_to_21_ptr, with_respect_to_31_ptr);
}
Matrix Calculus::Gradient(std::variant<Scalar, Matrix> const &node_variant, Scalar const &with_respect_to_11_ptr, Scalar const &with_respect_to_21_ptr, Scalar const &with_respect_to_31_ptr)
{
if (!std::holds_alternative<Scalar>(node_variant)) {
throw std::invalid_argument("Gradient only defined for Scalar");
}
Scalar scalar = std::get<Scalar>(node_variant);
Matrix gradient_matrix(3, 1);
gradient_matrix(0, 0) = Partial(scalar, with_respect_to_11_ptr);
gradient_matrix(1, 0) = Partial(scalar, with_respect_to_21_ptr);
gradient_matrix(2, 0) = Partial(scalar, with_respect_to_31_ptr);
return gradient_matrix;
}
Scalar Calculus::Divergence(Scalar const &with_respect_to_11_ptr, Scalar const &with_respect_to_21_ptr, Scalar const &with_respect_to_31_ptr)
{
return Divergence(m_node_variant, with_respect_to_11_ptr, with_respect_to_21_ptr, with_respect_to_31_ptr);
}
Scalar Calculus::Divergence(std::variant<Scalar, Matrix> const &node_variant, Scalar const &with_respect_to_11_ptr, Scalar const &with_respect_to_21_ptr, Scalar const &with_respect_to_31_ptr)
{
if (!std::holds_alternative<Matrix>(node_variant)) {
throw std::invalid_argument("Divergence only defined for Matrix");
}
Matrix matrix = std::get<Matrix>(node_variant);
if (matrix.Rows() != 3 && matrix.Cols() != 1) {
throw std::invalid_argument("Divergence requires a 3x1 matrix");
}
return std::shared_ptr<AdditionNode>(new AdditionNode({
std::shared_ptr<AdditionNode>(new AdditionNode({
Partial(matrix(0, 0), with_respect_to_11_ptr),
Partial(matrix(1, 0), with_respect_to_21_ptr)
})),
Partial(matrix(2, 0), with_respect_to_31_ptr)
}));
}
Matrix Calculus::Curl(Scalar const &with_respect_to_11_ptr, Scalar const &with_respect_to_21_ptr, Scalar const &with_respect_to_31_ptr)
{
return Curl(m_node_variant, with_respect_to_11_ptr, with_respect_to_21_ptr, with_respect_to_31_ptr);
}
Matrix Calculus::Curl(std::variant<Scalar, Matrix> const &node_variant, Scalar const &with_respect_to_11_ptr, Scalar const &with_respect_to_21_ptr, Scalar const &with_respect_to_31_ptr)
{
if (!std::holds_alternative<Matrix>(node_variant)) {
throw std::invalid_argument("Curl only defined for Matrix");
}
Matrix matrix = std::get<Matrix>(node_variant);
if (matrix.Rows() != 3 && matrix.Cols() != 1) {
throw std::invalid_argument("Divergence requires a 3x1 matrix");
}
Matrix curl_matrix(3, 1);
curl_matrix(0, 0) = std::shared_ptr<SubtractionNode>(new SubtractionNode({ Partial(matrix(2, 0), with_respect_to_21_ptr), Partial(matrix(1, 0), with_respect_to_31_ptr) }));
curl_matrix(1, 0) = std::shared_ptr<SubtractionNode>(new SubtractionNode({ Partial(matrix(0, 0), with_respect_to_31_ptr), Partial(matrix(2, 0), with_respect_to_11_ptr) }));
curl_matrix(2, 0) = std::shared_ptr<SubtractionNode>(new SubtractionNode({ Partial(matrix(1, 0), with_respect_to_11_ptr), Partial(matrix(0, 0), with_respect_to_21_ptr) }));
return curl_matrix;
}