[8703] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 |
|
---|
| 6 | namespace AutoDiff
|
---|
| 7 | {
|
---|
| 8 | partial class CompiledDifferentiator<T>
|
---|
| 9 | {
|
---|
| 10 | private class EvalVisitor : Compiled.ITapeVisitor
|
---|
| 11 | {
|
---|
| 12 | private readonly Compiled.TapeElement[] tape;
|
---|
| 13 |
|
---|
| 14 | public EvalVisitor(Compiled.TapeElement[] tape)
|
---|
| 15 | {
|
---|
| 16 | this.tape = tape;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | public void Visit(Compiled.Constant elem)
|
---|
| 20 | {
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public void Visit(Compiled.Exp elem)
|
---|
| 24 | {
|
---|
| 25 | elem.Value = Math.Exp(ValueOf(elem.Arg));
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | public void Visit(Compiled.Log elem)
|
---|
| 29 | {
|
---|
| 30 | elem.Value = Math.Log(ValueOf(elem.Arg));
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public void Visit(Compiled.ConstPower elem)
|
---|
| 34 | {
|
---|
| 35 | elem.Value = Math.Pow(ValueOf(elem.Base), elem.Exponent);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public void Visit(Compiled.TermPower elem)
|
---|
| 39 | {
|
---|
| 40 | elem.Value = Math.Pow(ValueOf(elem.Base), ValueOf(elem.Exponent));
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public void Visit(Compiled.Product elem)
|
---|
| 44 | {
|
---|
| 45 | elem.Value = ValueOf(elem.Left) * ValueOf(elem.Right);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public void Visit(Compiled.Sum elem)
|
---|
| 49 | {
|
---|
| 50 | elem.Value = 0;
|
---|
| 51 | for (int i = 0; i < elem.Terms.Length; ++i)
|
---|
| 52 | elem.Value += ValueOf(elem.Terms[i]);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public void Visit(Compiled.Variable var)
|
---|
| 56 | {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public void Visit(Compiled.UnaryFunc elem)
|
---|
| 60 | {
|
---|
| 61 | elem.Value = elem.Eval(ValueOf(elem.Arg));
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public void Visit(Compiled.BinaryFunc elem)
|
---|
| 65 | {
|
---|
| 66 | elem.Value = elem.Eval(ValueOf(elem.Left), ValueOf(elem.Right));
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public void Visit(Compiled.NaryFunc elem)
|
---|
| 70 | {
|
---|
| 71 | double[] args = new double[elem.Terms.Length];
|
---|
| 72 | for(int i=0;i<args.Length;i++)
|
---|
| 73 | args[i] = ValueOf(elem.Terms[i]);
|
---|
| 74 | elem.Value = elem.Eval(args);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private double ValueOf(int index)
|
---|
| 78 | {
|
---|
| 79 | return tape[index].Value;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|