Line | |
---|
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 | {
|
---|
22 | var combinedArg = arg.Concat(parameters).ToArray();
|
---|
23 | return compiledTerm.Evaluate(combinedArg);
|
---|
24 | }
|
---|
25 |
|
---|
26 | public Tuple<double[], double> Differentiate(double[] arg, double[] parameters)
|
---|
27 | {
|
---|
28 | var combinedArg = arg.Concat(parameters).ToArray();
|
---|
29 | var diffResult = compiledTerm.Differentiate(combinedArg);
|
---|
30 |
|
---|
31 | var partialGradient = new double[arg.Length];
|
---|
32 | Array.Copy(diffResult.Item1, partialGradient, partialGradient.Length);
|
---|
33 |
|
---|
34 | return Tuple.Create(partialGradient, diffResult.Item2);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public ReadOnlyCollection<Variable> Variables { get; private set;}
|
---|
38 |
|
---|
39 | public ReadOnlyCollection<Variable> Parameters { get; private set;}
|
---|
40 | }
|
---|
41 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.