1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Collections.ObjectModel;
|
---|
6 | using System.Diagnostics.Contracts;
|
---|
7 |
|
---|
8 | namespace AutoDiff
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Represents a parametric term after it has been compiled for efficient evaluation/differentiation. A parametric
|
---|
12 | /// term has some variables that function as "constant parameters" and others that function as actual variables.
|
---|
13 | /// </summary>
|
---|
14 | [ContractClass(typeof(ParametricCompiledTermContract))]
|
---|
15 | public interface IParametricCompiledTerm
|
---|
16 | {
|
---|
17 | /// <summary>
|
---|
18 | /// Evaluates the compiled term at the given point.
|
---|
19 | /// </summary>
|
---|
20 | /// <param name="arg">The point at which to evaluate..</param>
|
---|
21 | /// <param name="parameters">The parameter values</param>
|
---|
22 | /// <returns>The value of the function represented by the term at the given point.</returns>
|
---|
23 | /// <remarks>The number at <c>arg[i]</c> is the value assigned to the variable <c>Variables[i]</c>.</remarks>
|
---|
24 | double Evaluate(double[] arg, double[] parameters);
|
---|
25 |
|
---|
26 | /// <summary>
|
---|
27 | /// Computes the gradient of the compiled term at the given point.
|
---|
28 | /// </summary>
|
---|
29 | /// <param name="arg">The point at which to differentiate.</param>
|
---|
30 | /// <param name="parameters">The parameter values</param>
|
---|
31 | /// <returns>A tuple, where the first item is the gradient at <paramref name="arg"/> and the second item is
|
---|
32 | /// the value at <paramref name="arg"/>. That is, the second value is the same as running <see cref="Evaluate"/> on
|
---|
33 | /// <paramref name="arg"/> and <paramref name="parameters"/>.</returns>
|
---|
34 | /// <remarks>The number at <c>arg[i]</c> is the value assigned to the variable <c>Variables[i]</c>.</remarks>
|
---|
35 | Tuple<double[], double> Differentiate(double[] arg, double[] parameters);
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// The collection of variables contained in this compiled term.
|
---|
39 | /// </summary>
|
---|
40 | /// <remarks>
|
---|
41 | /// The order of variables in this collection specifies the meaning of each argument in <see cref="Differentiate"/> or
|
---|
42 | /// <see cref="Evaluate"/>. That is, the variable at <c>Variables[i]</c> corresponds to the i-th element in the <c>arg</c> parameter of <see cref="Differentiate"/>
|
---|
43 | /// and <see cref="Evaluate"/>.
|
---|
44 | /// </remarks>
|
---|
45 | ReadOnlyCollection<Variable> Variables { get; }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// The collection of parameter variables contained in this compiled term.
|
---|
49 | /// </summary>
|
---|
50 | /// <remarks>
|
---|
51 | /// The order of variables in this collection specifies the meaning of each argument in <see cref="Differentiate"/> or
|
---|
52 | /// <see cref="Evaluate"/>. That is, the variable at <c>Variables[i]</c> corresponds to the i-th element in the <c>parameters</c> parameter of <see cref="Differentiate"/>
|
---|
53 | /// and <see cref="Evaluate"/>.
|
---|
54 | /// </remarks>
|
---|
55 | ReadOnlyCollection<Variable> Parameters { get; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [ContractClassFor(typeof(IParametricCompiledTerm))]
|
---|
59 | abstract class ParametricCompiledTermContract : IParametricCompiledTerm
|
---|
60 | {
|
---|
61 |
|
---|
62 | public double Evaluate(double[] arg, double[] parameters)
|
---|
63 | {
|
---|
64 | Contract.Requires(arg != null);
|
---|
65 | Contract.Requires(arg.Length == Variables.Count);
|
---|
66 | Contract.Requires(parameters != null);
|
---|
67 | Contract.Requires(parameters.Length == Parameters.Count);
|
---|
68 |
|
---|
69 | return default(double);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public Tuple<double[], double> Differentiate(double[] arg, double[] parameters)
|
---|
73 | {
|
---|
74 | Contract.Requires(arg != null);
|
---|
75 | Contract.Requires(arg.Length == Variables.Count);
|
---|
76 | Contract.Requires(parameters != null);
|
---|
77 | Contract.Requires(parameters.Length == Parameters.Count);
|
---|
78 |
|
---|
79 | Contract.Ensures(Contract.Result<Tuple<double[], double>>() != null);
|
---|
80 | Contract.Ensures(Contract.Result<Tuple<double[], double>>().Item1.Length == arg.Length);
|
---|
81 |
|
---|
82 | return null;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public ReadOnlyCollection<Variable> Variables
|
---|
86 | {
|
---|
87 | get
|
---|
88 | {
|
---|
89 | Contract.Ensures(Contract.Result<ReadOnlyCollection<Variable>>() != null);
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public ReadOnlyCollection<Variable> Parameters
|
---|
95 | {
|
---|
96 | get
|
---|
97 | {
|
---|
98 | Contract.Ensures(Contract.Result<ReadOnlyCollection<Variable>>() != null);
|
---|
99 | return null;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|