Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.AutoDiff/1.0/AutoDiff-1.0/ParametricCompiledTerm.cs @ 8703

Last change on this file since 8703 was 8703, checked in by gkronber, 12 years ago

#1960 added HL wrapper plugin for AutoDiff

File size: 1.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Collections.ObjectModel;
6
7namespace 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.