-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathdifferentiator.cpp
More file actions
755 lines (683 loc) · 25.3 KB
/
differentiator.cpp
File metadata and controls
755 lines (683 loc) · 25.3 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#include "differentiator.h"
#include "simplifier.h"
#include <math.h>
using namespace febcode;
Type Differentiator::getDerivativeType(Type varType, TypeKind derivType)
{
if (derivType == TypeKind::Double)
{
return varType;
}
else if (derivType == TypeKind::Vec2)
{
if (varType->kind == TypeKind::Double) return prg.types.Vec2();
if (varType->kind == TypeKind::Vec2) return prg.types.Mat2();
}
else if (derivType == TypeKind::Vec3)
{
if (varType->kind == TypeKind::Double) return prg.types.Vec3();
if (varType->kind == TypeKind::Vec3 ) return prg.types.Mat3();
}
throw std::runtime_error("Can't determine type of derivative.");
}
void Differentiator::differentiate(const std::string& var)
{
// get the variable's type
Type varType = prg.globalType(var);
if (varType == nullptr)
throw std::runtime_error("Variable not found in program: " + var);
// update the program's return type
if (prg.returnType)
{
Type derivType = getDerivativeType(prg.returnType, varType->kind);
prg.returnType = derivType;
}
DerivVar dvar{ var, varType };
// differentiate the program's AST
auto diffAST = differentiate(*prg.ast, dvar);
prg.ast.reset(diffAST.release()); // replace original AST with derivative AST
}
std::unique_ptr<AST> Differentiator::differentiate(const AST& ast, const DerivVar& var)
{
auto derivativeAst = std::make_unique<AST>();
// first, see if the source AST actually depends on the variable we're differentiating with respect to.
// If not, we can just return an empty AST
dependencyFound = false;
for (const auto& stmt : ast.root.statements)
{
if (febcode::dependsOn(stmt.get(), var.name))
{
dependencyFound = true;
break;
}
}
if (!dependencyFound)
{
// the derivative of a constant is zero, so we can just return an AST with a single statement that returns zero.
ExprPtr returnValue;
if (prg.returnType)
{
returnValue = Zero(prg.returnType);
}
derivativeAst->addStatement(std::make_unique<ReturnStmt>(std::move(returnValue)));
return derivativeAst;
}
// add all injected globals to vartypes
for (const auto& global : prg.globalIndices)
{
std::string name = global.first;
Type type = prg.globals[global.second].type;
varTypes[name] = type;
}
for (const auto& stmt : ast.root.statements)
{
differentiateStmt(derivativeAst->root, stmt.get(), var);
}
return derivativeAst;
}
void Differentiator::differentiateStmt(BlockStmt& ast, Statement* stmt, const DerivVar& var)
{
if (auto exprStmt = dynamic_cast<ExpressionStmt*>(stmt)) diffExpressionStmt(ast, exprStmt , var);
else if (auto returnStmt = dynamic_cast<ReturnStmt* >(stmt)) diffReturnStmt (ast, returnStmt, var);
else if (auto structStmt = dynamic_cast<StructStmt* >(stmt)) diffStructStmt (ast, structStmt, var);
else if (auto varStmt = dynamic_cast<VarDeclStmt* >(stmt)) diffVarDeclStmt (ast, varStmt , var);
else if (auto ifStmt = dynamic_cast<IfStmt* >(stmt)) diffIfStmt (ast, ifStmt , var);
else if (auto blockStmt = dynamic_cast<BlockStmt* >(stmt)) diffBlockStmt (ast, blockStmt , var);
else
{
throw std::runtime_error("Unsupported statement type for differentiation");
}
}
void Differentiator::diffExpressionStmt(BlockStmt& ast, ExpressionStmt* stmt, const DerivVar& var)
{
auto derivativeExpr = differentiate(stmt->expr.get(), var);
ast.addStatement(std::make_unique<ExpressionStmt>(std::move(derivativeExpr)));
}
void Differentiator::diffReturnStmt(BlockStmt& ast, ReturnStmt* stmt, const DerivVar& var)
{
auto derivativeExpr = differentiate(stmt->value.get(), var);
ast.addStatement(std::make_unique<ReturnStmt>(std::move(derivativeExpr)));
}
void Differentiator::diffStructStmt(BlockStmt& ast, StructStmt* stmt, const DerivVar& var)
{
// copy the original struct declaration to the derivative AST
ast.addStatement(std::make_unique<StructStmt>(stmt->name, stmt->type, stmt->fields));
}
void Differentiator::diffVarDeclStmt(BlockStmt& ast, VarDeclStmt* stmt, const DerivVar& var)
{
if (stmt->input)
{
// input variables are treated as constants with respect to differentiation, so we can just copy the variable declaration to the derivative AST without creating a derivative variable for it.
ast.addStatement(std::make_unique<VarDeclStmt>(stmt->type, stmt->vars, stmt->input));
return;
}
std::vector<Var> copyVars; // copy of the original variables for the derivative AST
std::vector<Var> newVars; // new variables for the derivatives in the derivative AST
Type baseType = stmt->type;
// determine the type of the derivative variable based on the type of the original variable and the derivative variable.
Type derivType = getDerivativeType(baseType, var.type->kind);
for (auto& var_i : stmt->vars)
{
// handle array types by getting the base type and then reconstructing the array type
Type type = baseType;
if (var_i.arraySizes.size() > 0)
{
type = prg.types.getArrayType(baseType, var_i.arraySizes);
}
// copy the original variable declaration to the derivative AST
copyVars.push_back({ var_i.name, var_i.arraySizes, clone(var_i.initializer.get())});
// store the type of this variable in the map for later use when creating derivative variables for it.
varTypes[var_i.name] = type;
// No need to differentiate non-numeric types, since they don't contribute to the derivative.
// We can just copy them to the derivative AST without creating a derivative variable for them.
if (type->kind == TypeKind::Bool || type->kind == TypeKind::Int) continue;
// create a new variable for the derivative of this variable
std::string derivName = "__d" + var_i.name + "_d" + var.name;
ExprPtr init = nullptr;
if (var_i.initializer)
{
// lets differentiate the initializer.
init = differentiate(var_i.initializer.get(), var);
// if it's zero, then this variable didn't contribute to the derivative, so we can skip creating a derivative variable for it.
if (isZero(init))
continue;
}
else
{
// If the variable was not initialized, it could be assigned later.
// To be safe, we should create a derivative variable for it and initialize it to zero.
switch (derivType->kind)
{
case TypeKind::Double: init = Literal(0.0); break;
case TypeKind::Vec2 : init = Literal(vec2()); break;
case TypeKind::Vec3 : init = Literal(vec3()); break;
case TypeKind::Mat2 : init = Literal(mat2()); break;
case TypeKind::Mat3 : init = Literal(mat3()); break;
case TypeKind::Array : init = Initializer(type); break;
case TypeKind::Struct: init = Constructor(type); break;
default:
throw std::runtime_error("Don't know how to differentiate variable.");
}
}
// add the new derivative variable to the map and the list of new variables for the derivative AST
std::vector<size_t> arraySizes;
if (type->kind == TypeKind::Array)
{
arraySizes.push_back(type->arraySize);
}
deriveVars[var_i.name] = derivName;
varTypes[derivName] = derivType;
newVars.push_back({ derivName, arraySizes, std::move(init)});
}
// create new variable declaration statements for the derivatives and add it to the derivative AST
ast.addStatement(std::make_unique<VarDeclStmt>(stmt->type, copyVars));
if (!newVars.empty()) ast.addStatement(std::make_unique<VarDeclStmt>(derivType, newVars));
}
void Differentiator::diffIfStmt(BlockStmt& ast, IfStmt* stmt, const DerivVar& var)
{
// copy the condition
auto newIf = std::make_unique<IfStmt>();
newIf->condition = std::move(clone(stmt->condition.get()));
// differentiate the then branch
std::unique_ptr<BlockStmt> thenStmt = std::make_unique<BlockStmt>();
differentiateStmt(*thenStmt, stmt->thenBranch.get(), var);
newIf->thenBranch = std::move(thenStmt);
// differentiate the else branch if it exists
if (stmt->elseBranch)
{
std::unique_ptr<BlockStmt> elseStmt = std::make_unique<BlockStmt>();
differentiateStmt(*elseStmt, stmt->elseBranch.get(), var);
newIf->elseBranch = std::move(elseStmt);
}
// create the new if statement with the differentiated branches
ast.addStatement(std::move(newIf));
}
void Differentiator::diffBlockStmt(BlockStmt& ast, BlockStmt* stmt, const DerivVar& var)
{
for (const auto& s : stmt->statements)
{
differentiateStmt(ast, s.get(), var);
}
}
ExprPtr Differentiator::differentiate(const Expression* expr, const DerivVar& var)
{
if (auto literal = dynamic_cast<const LiteralExpr* >(expr)) return simplify(diffLiteral (literal , var));
else if (auto variable = dynamic_cast<const VariableExpr* >(expr)) return simplify(diffVariable (variable, var));
else if (auto unary = dynamic_cast<const UnaryExpr* >(expr)) return simplify(diffUnary (unary , var));
else if (auto binary = dynamic_cast<const BinaryExpr* >(expr)) return simplify(diffBinary (binary , var));
else if (auto call = dynamic_cast<const CallExpr* >(expr)) return simplify(diffCall (call , var));
else if (auto init = dynamic_cast<const InitExpr* >(expr)) return simplify(diffInit (init , var));
else if (auto ctor = dynamic_cast<const ConstructorExpr*>(expr)) return simplify(diffConstructor(ctor , var));
else if (auto assign = dynamic_cast<const AssignExpr* >(expr)) return simplify(diffAssign (assign , var));
else if (auto index = dynamic_cast<const IndexExpr* >(expr)) return simplify(diffIndex (index , var));
else if (auto member = dynamic_cast<const MemberExpr* >(expr)) return simplify(diffMember (member , var));
else
throw std::runtime_error("Unsupported expression type for differentiation");
return nullptr;
}
ExprPtr Differentiator::diffLiteral(const LiteralExpr* literal, const DerivVar& var)
{
// scalar derivation
if (var.type->kind == TypeKind::Double)
{
// The derivative of a constant is zero
switch (literal->value.index)
{
case ValueIndex::INT: return Literal(0);
case ValueIndex::DOUBLE: return Literal(0.0);
case ValueIndex::VEC2: return Literal(vec2());
case ValueIndex::VEC3: return Literal(vec3());
case ValueIndex::MAT2: return Literal(mat2());
case ValueIndex::MAT3: return Literal(mat3());
}
}
// 2d gradient
if (var.type->kind == TypeKind::Vec2)
{
switch (literal->value.index)
{
case ValueIndex::INT:
case ValueIndex::DOUBLE: return Literal(vec2());
case ValueIndex::VEC2 : return Literal(mat2());
}
}
// 3d gradient
if (var.type->kind == TypeKind::Vec3)
{
switch (literal->value.index)
{
case ValueIndex::INT:
case ValueIndex::DOUBLE: return Literal(vec3());
case ValueIndex::VEC3 : return Literal(mat3());
}
}
throw std::runtime_error("Don't know how to differentiate this literal type.");
}
ExprPtr Differentiator::diffVariable(const VariableExpr* variable, const DerivVar& var)
{
Type derivType = getDerivativeType(variable->valType, var.type->kind);
// The derivative of a variable with respect to itself is 1
if (variable->name == var.name)
{
switch (derivType->kind)
{
case TypeKind::Double: return Literal(1.0);
case TypeKind::Mat2 : return Literal(mat2(1.0));
case TypeKind::Mat3 : return Literal(mat3(1.0));
default:
throw std::runtime_error("Don't know how to make literal of derivative type.");
}
}
// see if we have a derivative for this variable
auto it = deriveVars.find(variable->name);
if (it != deriveVars.end())
{
return Variable(it->second, derivType);
}
else
{
// we may get here if a derivative was not created for this variable
// (e.g. if it's a non-numeric type or has a literal initializer),
// in which case we treat it as a constant and return zero
switch (derivType->kind)
{
case TypeKind::Double: return Literal(0.0);
case TypeKind::Vec2: return Literal(vec2());
case TypeKind::Vec3: return Literal(vec3());
case TypeKind::Mat2: return Literal(mat2());
case TypeKind::Mat3: return Literal(mat3());
default:
throw std::runtime_error("Don't know how to make literal of derivative type.");
}
}
}
ExprPtr Differentiator::diffUnary(const UnaryExpr* unary, const DerivVar& var)
{
auto right = simplify(unary->right);
// For unary negation, the derivative is the negation of the derivative of the operand
// d(-f) = -df
if (unary->op == UnaryOp::Negate) {
auto operandDerivative = differentiate(right.get(), var);
return Negate(operandDerivative);
}
throw std::runtime_error("Unsupported unary operator for differentiation");
}
std::unique_ptr<Expression> Differentiator::diffBinary(const BinaryExpr* binary, const DerivVar& var)
{
auto left = simplify(binary->left);
auto right = simplify(binary->right);
auto dleft = differentiate(left.get(), var);
auto dright = differentiate(right.get(), var);
// plus and minus are easy, so let's handle them first.
if (binary->op == BinaryOp::Plus)
return Add(dleft, dright);
if (binary->op == BinaryOp::Minus)
return Sub(dleft, dright);
// The other operators are more complex.
// First, figure out the types of all the expressions involved, so we can determine how to apply the differentiation rules.
Type ltype = left->valType;
Type rtype = right->valType;
Type dltype = dleft->valType;
Type drtype = dright->valType;
// scalar differentation
if (var.type->kind == TypeKind::Double)
{
if (isScalarType(ltype) && isScalarType(rtype))
{
switch (binary->op)
{
case BinaryOp::Multiply: return Add(Mul(dleft, right), Mul(left, dright)); // d( f * g ) = df * g + f * dg
case BinaryOp::Divide: return Div(Sub(Mul(dleft, right), Mul(left, dright)), Mul(right, right)); // d( f / g ) = (df * g - f * dg) / (g * g)
case BinaryOp::Exponent:
{
// some special cases first
if (auto lit = dynamic_cast<LiteralExpr*>(right.get()))
{
const Value& e = lit->value;
if (isIntNumber(e))
{
double p = (double)toIntNumber(e);
if (p == 1) return clone(dleft.get());
else if (p != 0)
{
return Mul(Mul(Literal(p), Pow(left, Literal(p - 1.0))), dleft); // d(x^p) = p * x^(p-1)*dx
}
}
}
break;
}
}
}
else
{
switch (binary->op)
{
// The rule d( f * g ) = df * g + f * dg applies to all types, since it doesn't matter what the types of f and g are, as long as we can multiply them together.
case BinaryOp::Multiply: return Add(Mul(dleft, right), Mul(left, dright));
};
}
}
else if (var.type->kind == TypeKind::Vec2) // gradient w.r.t. a vec2 variable
{
}
else if (var.type->kind == TypeKind::Vec3) // gradient w.r.t. a vec3 variable
{
if (binary->op == BinaryOp::Multiply)
{
if (isScalarType(ltype) && isScalarType(rtype))
{
// the derivatives of scalars with respect to a vec3 variable should be vec3s)
assert(isVec3Type(dltype) && isVec3Type(drtype));
// grad( f * g ) = grad(f) * g + f * grad(g), where f and g are scalars
return Add(Mul(dleft, right), Mul(left, dright));
}
if (isScalarType(ltype) && (rtype->kind == TypeKind::Vec3))
{
// grad( f * v ) = v * grad(f) + f * grad(v), where f is a scalar and v is a vector
return Add(OuterProduct(right, dleft), Mul(left, dright));
}
else if ((ltype->kind == TypeKind::Vec3) && isScalarType(rtype))
{
// grad( v * f ) = grad(v) * f + v * grad(f), where v is a vector and f is a scalar
return Add(Mul(dleft, right), OuterProduct(left, dright));
}
else if ((ltype->kind == TypeKind::Vec3) && (rtype->kind == TypeKind::Vec3))
{
// grad( v1 . v2 ) = transpose(grad(v1)) . v2 + transpose(grad(v2)) . v1, where v1 and v2 are vectors
return Add(Mul(Transpose(dleft), right), Mul(Transpose(dright), left));
}
}
if (binary->op == BinaryOp::Divide)
{
if (isScalarType(ltype) && isScalarType(rtype))
{
// the derivatives of scalars with respect to a vec3 variable should be vec3s)
assert(isVec3Type(dltype) && isVec3Type(drtype));
// grad( f / g ) = (grad(f) * g - f * grad(g)) / (g * g), where f and g are scalars
return Div(Sub(Mul(dleft, right), Mul(left, dright)), Mul(right, right));
}
else if ((ltype->kind == TypeKind::Vec3) && isScalarType(rtype))
{
// grad( v / f ) = (grad(v) * f - v & grad(f)) / (f * f), where v is a vector and f is a scalar
return Div(Sub(Mul(dleft, right), OuterProduct(left, dright)), Mul(right, right));
}
}
if (binary->op == BinaryOp::Exponent)
{
if (isScalarType(ltype) && isLiteral(right))
{
LiteralExpr* exponent = dynamic_cast<LiteralExpr*>(right.get());
if (isIntNumber(exponent->value))
{
int p = toIntNumber(exponent->value);
if (p == 1) return clone(dleft.get());
else if (p != 0)
{
// grad(x^p) = p * x^(p-1)*grad(x), where x is a vector and p is a scalar literal
return Mul(Mul(Literal(p), Pow(left, Literal(p - 1.0))), dleft);
}
}
}
}
}
throw std::runtime_error("AD error: Unsupported binary operator '" + opToString(binary->op) + "' with operand types '" + TypeToString(ltype) + "' and '" + TypeToString(rtype) + "'.");
}
ExprPtr Differentiator::diffCall(const CallExpr* call, const DerivVar& var)
{
std::vector<Type> argTypes;
for (const auto& arg : call->arguments)
{
argTypes.push_back(arg->valType);
}
// find the function with the matching name and argument types in the program's function definitions
int fnIndex = prg.resolveFunction(call->name, argTypes);
if (fnIndex == -1)
throw std::runtime_error("Function \"" + call->name + "\" not found for differentiation.");
Type returnType = call->valType;
Type derivType = getDerivativeType(returnType, var.type->kind);
const std::string& fnc = call->name;
auto& args = call->arguments;
if (args.size() == 1)
{
// differentiate argument
auto diffArg = differentiate(args[0].get(), var);
if (fnc == "sin") return Mul(Call("cos", args), diffArg);
else if (fnc == "cos") return Negate(Mul(Call("sin", args), diffArg));
else if (fnc == "exp") return Mul(Call("exp", args), diffArg);
else if (fnc == "sqrt") return Mul(Div(Literal(0.5), Call("sqrt", args)), diffArg);
else if (fnc == "length" && isVec3Type(args[0]->valType))
{
if (derivType->kind == TypeKind::Double)
{
assert(diffArg->valType->kind == TypeKind::Vec3);
// d(length(v)) = (v . d(v)) / length(v)
return Div(Mul(args[0], diffArg), Call("length", args));
}
else if (derivType->kind == TypeKind::Vec3)
{
assert(diffArg->valType->kind == TypeKind::Mat3);
// grad(length(v)) = (transpose(grad(v)) . v) / length(v)
return Div(Mul(Transpose(diffArg), args[0]), Call("length", args));
}
}
else if (fnc == "normalize" && isVec3Type(args[0]->valType))
{
// rewrite normalize(v) as v / length(v), then derivate that
ExprPtr normalize = Div(args[0], Call("length", args));
return differentiate(normalize.get(), var);
}
}
throw std::runtime_error("Don't know how to differentiate function \"" + fnc + "\".");
}
ExprPtr Differentiator::diffInit(const InitExpr* init, const DerivVar& var)
{
std::vector<ExprPtr> diffElements;
for (const auto& elem : init->elements)
{
diffElements.push_back(differentiate(elem.get(), var));
}
return std::make_unique<InitExpr>(std::move(diffElements));
}
ExprPtr Differentiator::diffConstructor(const ConstructorExpr* ctor, const DerivVar& var)
{
// determine the type of the derivative variable based on the type of the original variable and the derivative variable.
Type derivType = getDerivativeType(ctor->valType, var.type->kind);
if (var.type->kind == TypeKind::Double)
{
std::vector<ExprPtr> diffArgs;
for (const auto& arg : ctor->args)
{
diffArgs.push_back(differentiate(arg.get(), var));
}
return std::make_unique<ConstructorExpr>(ctor->valType, std::move(diffArgs));
}
else if (var.type->kind == TypeKind::Vec3)
{
if (ctor->args.size() == 3)
{
std::vector<ExprPtr> diffArgs;
for (const auto& arg : ctor->args)
{
ExprPtr darg = differentiate(arg.get(), var);
if (darg->valType != prg.types.Vec3())
{
throw std::runtime_error("Don't know how to differentiate this constructor for vec3 variable.");
}
diffArgs.push_back(std::move(darg));
}
// put all the components into a mat3 constructor
// grad(vec3(x, y, z)) = mat3( grad(x),
// grad(y),
// grad(z) )
return std::make_unique<ConstructorExpr>(prg.types.Mat3(), std::move(diffArgs));
}
else
throw std::runtime_error("Don't know how to differentiate this constructor for vec3 variable.");
}
throw std::runtime_error("Don't know how to differentiate constructor.");
}
std::unique_ptr<Expression> Differentiator::diffAssign(const AssignExpr* assign, const DerivVar& var)
{
// For an assignment expression, we can use the rule: d( y = expr ) --> dy = d(expr)
auto du = differentiate(assign->target.get(), var);
auto dv = differentiate(assign->value.get(), var);
// if the left is zero, that meants that it did not depend on the variable.
// In that case, we just copy the original expression, since it doesn't contribute to the derivative.
if (isZero(du))
return clone(assign);
return Assign(du, dv);
}
std::unique_ptr<Expression> Differentiator::diffMember(const MemberExpr* member, const DerivVar& var)
{
if (var.type->kind == TypeKind::Double)
{
// For a member access expression, we can use the rule: d( obj.field ) --> dobj.field
auto dobj = differentiate(member->object.get(), var);
return Member(dobj, member->property);
}
else if (var.type->kind == TypeKind::Vec2)
{
if (isVariable(member->object))
{
auto var = dynamic_cast<const VariableExpr*>(member->object.get());
if (var->name == var->name)
{
if (member->property == "x") return Literal(vec2(1, 0));
else if (member->property == "y") return Literal(vec2(0, 1));
else
throw std::runtime_error("Don't know how to differentiate this member access for vec2 variable.");
}
}
}
else if (var.type->kind == TypeKind::Vec3)
{
if (isVariable(member->object))
{
auto var = dynamic_cast<const VariableExpr*>(member->object.get());
if (var->name == var->name)
{
if (member->property == "x") return Literal(vec3(1, 0, 0));
else if (member->property == "y") return Literal(vec3(0, 1, 0));
else if (member->property == "z") return Literal(vec3(0, 0, 1));
else
throw std::runtime_error("Don't know how to differentiate this member access for vec3 variable.");
}
}
}
throw std::runtime_error("Don't know how to differentiate member access for this variable type.");
}
std::unique_ptr<Expression> Differentiator::diffIndex(const IndexExpr* index, const DerivVar& var)
{
if (var.type->kind == TypeKind::Double)
{
// For an index access expression, we can use the rule: d( obj[i] ) --> d(obj)[i]
auto dobj = differentiate(index->object.get(), var);
return Index(dobj, index->index);
}
throw std::runtime_error("Don't know how to differentiate index access for this variable type.");
}
bool febcode::dependsOn(const Expression* expr, const std::string& var)
{
if (auto literal = dynamic_cast<const LiteralExpr*>(expr))
{
return false;
}
else if (auto variable = dynamic_cast<const VariableExpr*>(expr))
{
return variable->name == var;
}
else if (auto binary = dynamic_cast<const BinaryExpr*>(expr))
{
return dependsOn(binary->left.get(), var) || dependsOn(binary->right.get(), var);
}
else if (auto unary = dynamic_cast<const UnaryExpr*>(expr))
{
return dependsOn(unary->right.get(), var);
}
else if (auto call = dynamic_cast<const CallExpr*>(expr))
{
for (const auto& arg : call->arguments)
{
if (dependsOn(arg.get(), var))
return true;
}
return false;
}
else if (auto member = dynamic_cast<const MemberExpr*>(expr))
{
return dependsOn(member->object.get(), var);
}
else if (auto index = dynamic_cast<const IndexExpr*>(expr))
{
return dependsOn(index->object.get(), var) || dependsOn(index->index.get(), var);
}
else if (auto assign = dynamic_cast<const AssignExpr*>(expr))
{
return dependsOn(assign->target.get(), var) || dependsOn(assign->value.get(), var);
}
else if (auto init = dynamic_cast<const InitExpr*>(expr))
{
for (const auto& elem : init->elements)
{
if (dependsOn(elem.get(), var))
return true;
}
return false;
}
else if (auto constructor = dynamic_cast<const ConstructorExpr*>(expr))
{
for (const auto& elem : constructor->args)
{
if (dependsOn(elem.get(), var))
return true;
}
return false;
}
else
throw std::runtime_error("Unsupported expression type for dependency analysis");
}
bool febcode::dependsOn(const Statement* stmt, const std::string& varName)
{
if (auto exprStmt = dynamic_cast<const ExpressionStmt*>(stmt))
{
return ::dependsOn(exprStmt->expr.get(), varName);
}
else if (auto returnStmt = dynamic_cast<const ReturnStmt*>(stmt))
{
return ::dependsOn(returnStmt->value.get(), varName);
}
else if (auto structStmt = dynamic_cast<const StructStmt*>(stmt))
{
return false;
}
else if (auto varDeclStmt = dynamic_cast<const VarDeclStmt*>(stmt))
{
if (!varDeclStmt->input)
{
for (const auto& var : varDeclStmt->vars)
{
if (::dependsOn(var.initializer.get(), varName))
return true;
}
}
return false;
}
else if (auto ifStmt = dynamic_cast<const IfStmt*>(stmt))
{
if (::dependsOn(ifStmt->condition.get(), varName)) return true;
if (dependsOn(ifStmt->thenBranch.get(), varName)) return true;
if (ifStmt->elseBranch && dependsOn(ifStmt->elseBranch.get(), varName)) return true;
return false;
}
else if (auto block = dynamic_cast<const BlockStmt*>(stmt))
{
for (const StmtPtr& s : block->statements)
{
if (dependsOn(s.get(), varName))
return true;
}
return false;
}
else
throw std::runtime_error("Unsupported statement type for dependency analysis");
}