using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; namespace AutoDiff { class ParametricCompiledTerm : IParametricCompiledTerm { private readonly ICompiledTerm compiledTerm; public ParametricCompiledTerm(Term term, Variable[] variables, Variable[] parameters) { compiledTerm = term.Compile(variables.Concat(parameters).ToArray()); Variables = Array.AsReadOnly(variables.ToArray()); Parameters = Array.AsReadOnly(parameters.ToArray()); } public double Evaluate(double[] arg, double[] parameters) { var combinedArg = new double[arg.Length + parameters.Length]; arg.CopyTo(combinedArg, 0); parameters.CopyTo(combinedArg, arg.Length); return compiledTerm.Evaluate(combinedArg); } public Tuple Differentiate(double[] arg, double[] parameters) { var combinedArg = new double[arg.Length + parameters.Length]; arg.CopyTo(combinedArg, 0); parameters.CopyTo(combinedArg, arg.Length); var diffResult = compiledTerm.Differentiate(combinedArg); var partialGradient = new double[arg.Length]; Array.Copy(diffResult.Item1, partialGradient, partialGradient.Length); return Tuple.Create(partialGradient, diffResult.Item2); } public ReadOnlyCollection Variables { get; private set;} public ReadOnlyCollection Parameters { get; private set;} } }