[8703] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Collections.ObjectModel;
|
---|
| 6 |
|
---|
| 7 | namespace AutoDiff
|
---|
| 8 | {
|
---|
| 9 | class ParametricCompiledTerm : IParametricCompiledTerm
|
---|
| 10 | {
|
---|
| 11 | private readonly ICompiledTerm compiledTerm;
|
---|
| 12 |
|
---|
| 13 | public ParametricCompiledTerm(Term term, Variable[] variables, Variable[] parameters)
|
---|
| 14 | {
|
---|
| 15 | compiledTerm = term.Compile(variables.Concat(parameters).ToArray());
|
---|
| 16 | Variables = Array.AsReadOnly(variables.ToArray());
|
---|
| 17 | Parameters = Array.AsReadOnly(parameters.ToArray());
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | public double Evaluate(double[] arg, double[] parameters)
|
---|
| 21 | {
|
---|
[8952] | 22 | var combinedArg = new double[arg.Length + parameters.Length];
|
---|
| 23 | arg.CopyTo(combinedArg, 0);
|
---|
| 24 | parameters.CopyTo(combinedArg, arg.Length);
|
---|
[8703] | 25 | return compiledTerm.Evaluate(combinedArg);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | public Tuple<double[], double> Differentiate(double[] arg, double[] parameters)
|
---|
| 29 | {
|
---|
[8952] | 30 | var combinedArg = new double[arg.Length + parameters.Length];
|
---|
| 31 | arg.CopyTo(combinedArg, 0);
|
---|
| 32 | parameters.CopyTo(combinedArg, arg.Length);
|
---|
[8703] | 33 | var diffResult = compiledTerm.Differentiate(combinedArg);
|
---|
| 34 |
|
---|
| 35 | var partialGradient = new double[arg.Length];
|
---|
| 36 | Array.Copy(diffResult.Item1, partialGradient, partialGradient.Length);
|
---|
| 37 |
|
---|
| 38 | return Tuple.Create(partialGradient, diffResult.Item2);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public ReadOnlyCollection<Variable> Variables { get; private set;}
|
---|
| 42 |
|
---|
| 43 | public ReadOnlyCollection<Variable> Parameters { get; private set;}
|
---|
| 44 | }
|
---|
| 45 | }
|
---|