1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Diagnostics;
|
---|
6 |
|
---|
7 | namespace AutoDiff
|
---|
8 | {
|
---|
9 | partial class CompiledDifferentiator<T>
|
---|
10 | {
|
---|
11 | private class ForwardSweepVisitor : Compiled.ITapeVisitor
|
---|
12 | {
|
---|
13 | private Compiled.TapeElement[] tape;
|
---|
14 |
|
---|
15 | public ForwardSweepVisitor(Compiled.TapeElement[] tape)
|
---|
16 | {
|
---|
17 | this.tape = tape;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public void Visit(Compiled.Constant elem)
|
---|
21 | {
|
---|
22 | }
|
---|
23 |
|
---|
24 | public void Visit(Compiled.Exp elem)
|
---|
25 | {
|
---|
26 | elem.Value = Math.Exp(ValueOf(elem.Arg));
|
---|
27 | elem.Inputs[0].Weight = elem.Value;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public void Visit(Compiled.Log elem)
|
---|
31 | {
|
---|
32 | double arg = ValueOf(elem.Arg);
|
---|
33 | elem.Value = Math.Log(arg);
|
---|
34 | elem.Inputs[0].Weight = 1 / arg;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void Visit(Compiled.ConstPower elem)
|
---|
38 | {
|
---|
39 | double baseVal = ValueOf(elem.Base);
|
---|
40 | elem.Value = Math.Pow(baseVal, elem.Exponent);
|
---|
41 | elem.Inputs[0].Weight = elem.Exponent * Math.Pow(baseVal, elem.Exponent - 1);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void Visit(Compiled.TermPower elem)
|
---|
45 | {
|
---|
46 | double baseVal = ValueOf(elem.Base);
|
---|
47 | double exponent = ValueOf(elem.Exponent);
|
---|
48 |
|
---|
49 | elem.Value = Math.Pow(baseVal, exponent);
|
---|
50 | elem.Inputs[0].Weight = exponent * Math.Pow(baseVal, exponent - 1);
|
---|
51 | elem.Inputs[1].Weight = elem.Value * Math.Log(baseVal);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void Visit(Compiled.Product elem)
|
---|
55 | {
|
---|
56 | double left = ValueOf(elem.Left);
|
---|
57 | double right = ValueOf(elem.Right);
|
---|
58 |
|
---|
59 | elem.Value = left * right;
|
---|
60 | elem.Inputs[0].Weight = right;
|
---|
61 | elem.Inputs[1].Weight = left;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void Visit(Compiled.Sum elem)
|
---|
65 | {
|
---|
66 | elem.Value = 0;
|
---|
67 | for (int i = 0; i < elem.Terms.Length; ++i)
|
---|
68 | elem.Value += ValueOf(elem.Terms[i]);
|
---|
69 |
|
---|
70 | for (int i = 0; i < elem.Inputs.Length; ++i)
|
---|
71 | elem.Inputs[i].Weight = 1;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void Visit(Compiled.Variable var)
|
---|
75 | {
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void Visit(Compiled.UnaryFunc elem)
|
---|
79 | {
|
---|
80 | double arg = ValueOf(elem.Arg);
|
---|
81 | elem.Value = elem.Eval(arg);
|
---|
82 | elem.Inputs[0].Weight = elem.Diff(arg);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void Visit(Compiled.BinaryFunc elem)
|
---|
86 | {
|
---|
87 | double left = ValueOf(elem.Left);
|
---|
88 | double right = ValueOf(elem.Right);
|
---|
89 |
|
---|
90 | elem.Value = elem.Eval(left, right);
|
---|
91 | var grad = elem.Diff(left, right);
|
---|
92 | elem.Inputs[0].Weight = grad.Item1;
|
---|
93 | elem.Inputs[1].Weight = grad.Item2;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void Visit(Compiled.NaryFunc elem)
|
---|
97 | {
|
---|
98 | double[] args = new double[elem.Terms.Length];
|
---|
99 | for (int i = 0; i < args.Length; i++)
|
---|
100 | args[i] = ValueOf(elem.Terms[i]);
|
---|
101 |
|
---|
102 | elem.Value = elem.Eval(args);
|
---|
103 | var grad = elem.Diff(args);
|
---|
104 | for (int i = 0; i < grad.Length; ++i)
|
---|
105 | elem.Inputs[i].Weight = grad[i];
|
---|
106 | }
|
---|
107 |
|
---|
108 | private double ValueOf(int index)
|
---|
109 | {
|
---|
110 | return tape[index].Value;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | } |
---|