Operation.cpp 28.1 KB
Newer Older
1
2
3
4
5
6
7
8
9

/* -------------------------------------------------------------------------- *
 *                                   Lepton                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the Lepton expression parser originating from              *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
10
 * Portions copyright (c) 2009-2019 Stanford University and the Authors.      *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

33
34
#include "lepton/Operation.h"
#include "lepton/ExpressionTreeNode.h"
35
#include "MSVC_erfc.h"
36
37
38
39

using namespace Lepton;
using namespace std;

40
41
42
43
44
45
static bool isZero(const ExpressionTreeNode& node) {
    if (node.getOperation().getId() != Operation::CONSTANT)
        return false;
    return dynamic_cast<const Operation::Constant&>(node.getOperation()).getValue() == 0.0;
}

46
47
48
49
50
51
52
53
double Operation::Erf::evaluate(double* args, const map<string, double>& variables) const {
    return erf(args[0]);
}

double Operation::Erfc::evaluate(double* args, const map<string, double>& variables) const {
    return erfc(args[0]);
}

54
55
56
57
58
59
60
61
62
63
64
ExpressionTreeNode Operation::Constant::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    return ExpressionTreeNode(new Operation::Constant(0.0));
}

ExpressionTreeNode Operation::Variable::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    if (variable == name)
        return ExpressionTreeNode(new Operation::Constant(1.0));
    return ExpressionTreeNode(new Operation::Constant(0.0));
}

ExpressionTreeNode Operation::Custom::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
Peter Eastman's avatar
Peter Eastman committed
65
66
    if (function->getNumArguments() == 0)
        return ExpressionTreeNode(new Operation::Constant(0.0));
67
68
69
70
71
72
73
74
75
76
77
78
79
    ExpressionTreeNode result;
    bool foundTerm = false;
    for (int i = 0; i < getNumArguments(); i++) {
        if (!isZero(childDerivs[i])) {
            if (foundTerm)
                result = ExpressionTreeNode(new Operation::Add(),
                                            result,
                                            ExpressionTreeNode(new Operation::Multiply(), ExpressionTreeNode(new Operation::Custom(*this, i), children), childDerivs[i]));
            else {
                result = ExpressionTreeNode(new Operation::Multiply(), ExpressionTreeNode(new Operation::Custom(*this, i), children), childDerivs[i]);
                foundTerm = true;
            }
        }
Peter Eastman's avatar
Peter Eastman committed
80
    }
81
82
83
    if (foundTerm)
        return result;
    return ExpressionTreeNode(new Operation::Constant(0.0));
84
85
86
}

ExpressionTreeNode Operation::Add::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
87
88
89
90
    if (isZero(childDerivs[0]))
        return childDerivs[1];
    if (isZero(childDerivs[1]))
        return childDerivs[0];
91
92
93
94
    return ExpressionTreeNode(new Operation::Add(), childDerivs[0], childDerivs[1]);
}

ExpressionTreeNode Operation::Subtract::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
95
96
97
98
99
100
101
    if (isZero(childDerivs[0])) {
        if (isZero(childDerivs[1]))
            return ExpressionTreeNode(new Operation::Constant(0.0));
        return ExpressionTreeNode(new Operation::Negate(), childDerivs[1]);
    }
    if (isZero(childDerivs[1]))
        return childDerivs[0];
102
103
104
105
    return ExpressionTreeNode(new Operation::Subtract(), childDerivs[0], childDerivs[1]);
}

ExpressionTreeNode Operation::Multiply::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
106
107
108
109
110
111
112
    if (isZero(childDerivs[0])) {
        if (isZero(childDerivs[1]))
            return ExpressionTreeNode(new Operation::Constant(0.0));
        return ExpressionTreeNode(new Operation::Multiply(), children[0], childDerivs[1]);
    }
    if (isZero(childDerivs[1]))
        return ExpressionTreeNode(new Operation::Multiply(), children[1], childDerivs[0]);
113
114
115
116
117
118
    return ExpressionTreeNode(new Operation::Add(),
                              ExpressionTreeNode(new Operation::Multiply(), children[0], childDerivs[1]),
                              ExpressionTreeNode(new Operation::Multiply(), children[1], childDerivs[0]));
}

ExpressionTreeNode Operation::Divide::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
119
120
121
122
123
124
125
126
127
128
    ExpressionTreeNode subexp;
    if (isZero(childDerivs[0])) {
        if (isZero(childDerivs[1]))
            return ExpressionTreeNode(new Operation::Constant(0.0));
        subexp = ExpressionTreeNode(new Operation::Negate(), ExpressionTreeNode(new Operation::Multiply(), children[0], childDerivs[1]));
    }
    else if (isZero(childDerivs[1]))
        subexp = ExpressionTreeNode(new Operation::Multiply(), children[1], childDerivs[0]);
    else
        subexp = ExpressionTreeNode(new Operation::Subtract(),
129
                                                 ExpressionTreeNode(new Operation::Multiply(), children[1], childDerivs[0]),
130
131
                                                 ExpressionTreeNode(new Operation::Multiply(), children[0], childDerivs[1]));
    return ExpressionTreeNode(new Operation::Divide(), subexp, ExpressionTreeNode(new Operation::Square(), children[1]));
132
133
134
135
136
137
138
139
}

ExpressionTreeNode Operation::Power::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    return ExpressionTreeNode(new Operation::Add(),
                              ExpressionTreeNode(new Operation::Multiply(),
                                                 ExpressionTreeNode(new Operation::Multiply(),
                                                                    children[1],
                                                                    ExpressionTreeNode(new Operation::Power(),
140
                                                                                       children[0], ExpressionTreeNode(new Operation::AddConstant(-1.0), children[1]))),
141
142
143
144
145
146
147
148
149
                                                 childDerivs[0]),
                              ExpressionTreeNode(new Operation::Multiply(),
                                                 ExpressionTreeNode(new Operation::Multiply(),
                                                                    ExpressionTreeNode(new Operation::Log(), children[0]),
                                                                    ExpressionTreeNode(new Operation::Power(), children[0], children[1])),
                                                 childDerivs[1]));
}

ExpressionTreeNode Operation::Negate::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
150
151
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
152
153
154
155
    return ExpressionTreeNode(new Operation::Negate(), childDerivs[0]);
}

ExpressionTreeNode Operation::Sqrt::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
156
157
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
158
    return ExpressionTreeNode(new Operation::Multiply(),
159
                              ExpressionTreeNode(new Operation::MultiplyConstant(0.5),
160
161
162
163
164
165
                                                 ExpressionTreeNode(new Operation::Reciprocal(),
                                                                    ExpressionTreeNode(new Operation::Sqrt(), children[0]))),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Exp::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
166
167
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
168
169
170
171
172
173
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Exp(), children[0]),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Log::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
174
175
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
176
177
178
179
180
181
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Reciprocal(), children[0]),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Sin::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
182
183
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
184
185
186
187
188
189
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Cos(), children[0]),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Cos::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
190
191
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
192
193
194
195
196
197
198
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Negate(),
                                                 ExpressionTreeNode(new Operation::Sin(), children[0])),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Sec::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
199
200
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
201
202
203
204
205
206
207
208
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Multiply(),
                                                 ExpressionTreeNode(new Operation::Sec(), children[0]),
                                                 ExpressionTreeNode(new Operation::Tan(), children[0])),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Csc::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
209
210
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
211
212
213
214
215
216
217
218
219
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Negate(),
                                                 ExpressionTreeNode(new Operation::Multiply(),
                                                                    ExpressionTreeNode(new Operation::Csc(), children[0]),
                                                                    ExpressionTreeNode(new Operation::Cot(), children[0]))),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Tan::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
220
221
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
222
223
224
225
226
227
228
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Square(),
                                                 ExpressionTreeNode(new Operation::Sec(), children[0])),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Cot::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
229
230
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
231
232
233
234
235
236
237
238
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Negate(),
                                                 ExpressionTreeNode(new Operation::Square(),
                                                                    ExpressionTreeNode(new Operation::Csc(), children[0]))),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Asin::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
239
240
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
241
242
243
244
245
246
247
248
249
250
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Reciprocal(),
                                                 ExpressionTreeNode(new Operation::Sqrt(),
                                                                    ExpressionTreeNode(new Operation::Subtract(),
                                                                                       ExpressionTreeNode(new Operation::Constant(1.0)),
                                                                                       ExpressionTreeNode(new Operation::Square(), children[0])))),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Acos::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
251
252
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
253
254
255
256
257
258
259
260
261
262
263
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Negate(),
                                                 ExpressionTreeNode(new Operation::Reciprocal(),
                                                                    ExpressionTreeNode(new Operation::Sqrt(),
                                                                                       ExpressionTreeNode(new Operation::Subtract(),
                                                                                                          ExpressionTreeNode(new Operation::Constant(1.0)),
                                                                                                          ExpressionTreeNode(new Operation::Square(), children[0]))))),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Atan::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
264
265
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
266
267
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Reciprocal(),
268
                                                 ExpressionTreeNode(new Operation::AddConstant(1.0),
269
270
271
272
                                                                    ExpressionTreeNode(new Operation::Square(), children[0]))),
                              childDerivs[0]);
}

273
274
275
276
277
278
279
280
281
282
ExpressionTreeNode Operation::Atan2::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    return ExpressionTreeNode(new Operation::Divide(),
                              ExpressionTreeNode(new Operation::Subtract(),
                                                 ExpressionTreeNode(new Operation::Multiply(), children[1], childDerivs[0]),
                                                 ExpressionTreeNode(new Operation::Multiply(), children[0], childDerivs[1])),
                              ExpressionTreeNode(new Operation::Add(),
                                                 ExpressionTreeNode(new Operation::Square(), children[0]),
                                                 ExpressionTreeNode(new Operation::Square(), children[1])));
}

283
ExpressionTreeNode Operation::Sinh::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
284
285
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
286
287
288
289
290
291
292
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Cosh(),
                                                 children[0]),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Cosh::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
293
294
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
295
296
297
298
299
300
301
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Sinh(),
                                                 children[0]),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Tanh::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
302
303
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
304
305
306
307
308
309
310
311
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Subtract(),
                                                 ExpressionTreeNode(new Operation::Constant(1.0)),
                                                 ExpressionTreeNode(new Operation::Square(),
                                                                    ExpressionTreeNode(new Operation::Tanh(), children[0]))),
                              childDerivs[0]);
}

312
ExpressionTreeNode Operation::Erf::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
313
314
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
315
316
317
318
319
320
321
322
323
324
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Multiply(),
                                                 ExpressionTreeNode(new Operation::Constant(2.0/sqrt(M_PI))),
                                                 ExpressionTreeNode(new Operation::Exp(),
                                                                    ExpressionTreeNode(new Operation::Negate(),
                                                                                       ExpressionTreeNode(new Operation::Square(), children[0])))),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Erfc::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
325
326
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
327
328
329
330
331
332
333
334
335
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Multiply(),
                                                 ExpressionTreeNode(new Operation::Constant(-2.0/sqrt(M_PI))),
                                                 ExpressionTreeNode(new Operation::Exp(),
                                                                    ExpressionTreeNode(new Operation::Negate(),
                                                                                       ExpressionTreeNode(new Operation::Square(), children[0])))),
                              childDerivs[0]);
}

336
337
338
339
ExpressionTreeNode Operation::Step::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    return ExpressionTreeNode(new Operation::Constant(0.0));
}

340
341
342
343
ExpressionTreeNode Operation::Delta::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    return ExpressionTreeNode(new Operation::Constant(0.0));
}

344
ExpressionTreeNode Operation::Square::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
345
346
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
347
    return ExpressionTreeNode(new Operation::Multiply(),
348
                              ExpressionTreeNode(new Operation::MultiplyConstant(2.0),
349
350
351
352
353
                                                 children[0]),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Cube::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
354
355
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
356
    return ExpressionTreeNode(new Operation::Multiply(),
357
                              ExpressionTreeNode(new Operation::MultiplyConstant(3.0),
358
359
360
361
362
                                                 ExpressionTreeNode(new Operation::Square(), children[0])),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Reciprocal::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
363
364
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
365
366
367
368
369
370
371
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::Negate(),
                                                 ExpressionTreeNode(new Operation::Reciprocal(),
                                                                    ExpressionTreeNode(new Operation::Square(), children[0]))),
                              childDerivs[0]);
}

372
ExpressionTreeNode Operation::AddConstant::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
373
374
375
    return childDerivs[0];
}

376
ExpressionTreeNode Operation::MultiplyConstant::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
377
378
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
379
380
381
382
383
    return ExpressionTreeNode(new Operation::MultiplyConstant(value),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::PowerConstant::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
384
385
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    return ExpressionTreeNode(new Operation::Multiply(),
                              ExpressionTreeNode(new Operation::MultiplyConstant(value),
                                                 ExpressionTreeNode(new Operation::PowerConstant(value-1),
                                                                    children[0])),
                              childDerivs[0]);
}

ExpressionTreeNode Operation::Min::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    ExpressionTreeNode step(new Operation::Step(),
                            ExpressionTreeNode(new Operation::Subtract(), children[0], children[1]));
    return ExpressionTreeNode(new Operation::Subtract(),
                              ExpressionTreeNode(new Operation::Multiply(), childDerivs[1], step),
                              ExpressionTreeNode(new Operation::Multiply(), childDerivs[0],
                                                 ExpressionTreeNode(new Operation::AddConstant(-1), step)));
}

ExpressionTreeNode Operation::Max::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    ExpressionTreeNode step(new Operation::Step(),
                            ExpressionTreeNode(new Operation::Subtract(), children[0], children[1]));
    return ExpressionTreeNode(new Operation::Subtract(),
                              ExpressionTreeNode(new Operation::Multiply(), childDerivs[0], step),
                              ExpressionTreeNode(new Operation::Multiply(), childDerivs[1],
                                                 ExpressionTreeNode(new Operation::AddConstant(-1), step)));
}

ExpressionTreeNode Operation::Abs::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
412
413
    if (isZero(childDerivs[0]))
        return ExpressionTreeNode(new Operation::Constant(0.0));
414
415
416
417
418
    ExpressionTreeNode step(new Operation::Step(), children[0]);
    return ExpressionTreeNode(new Operation::Multiply(),
                              childDerivs[0],
                              ExpressionTreeNode(new Operation::AddConstant(-1),
                                                 ExpressionTreeNode(new Operation::MultiplyConstant(2), step)));
419
}
420
421
422
423
424
425
426
427

ExpressionTreeNode Operation::Floor::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    return ExpressionTreeNode(new Operation::Constant(0.0));
}

ExpressionTreeNode Operation::Ceil::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    return ExpressionTreeNode(new Operation::Constant(0.0));
}
428
429
430
431
432
433
434
435

ExpressionTreeNode Operation::Select::differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const {
    vector<ExpressionTreeNode> derivChildren;
    derivChildren.push_back(children[0]);
    derivChildren.push_back(childDerivs[1]);
    derivChildren.push_back(childDerivs[2]);
    return ExpressionTreeNode(new Operation::Select(), derivChildren);
}