Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AutoDiff/1.0/AutoDiff-1.0/ParametricCompiledTerm.cs @ 16440

Last change on this file since 16440 was 8952, checked in by mkommend, 12 years ago

#1976: Performance improvements in AutoDiff library.

File size: 1.6 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 = new double[arg.Length + parameters.Length];
23            arg.CopyTo(combinedArg, 0);
24            parameters.CopyTo(combinedArg, arg.Length);
25            return compiledTerm.Evaluate(combinedArg);
26        }
27
28        public Tuple<double[], double> Differentiate(double[] arg, double[] parameters)
29        {
30            var combinedArg = new double[arg.Length + parameters.Length];
31            arg.CopyTo(combinedArg, 0);
32            parameters.CopyTo(combinedArg, arg.Length);
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}
Note: See TracBrowser for help on using the repository browser.