[8703] | 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 term after it has been compiled for efficient evaluation/differentiation.
|
---|
| 12 | /// </summary>
|
---|
| 13 | [ContractClass(typeof(CompiledTermContract))]
|
---|
| 14 | public interface ICompiledTerm
|
---|
| 15 | {
|
---|
| 16 | /// <summary>
|
---|
| 17 | /// Evaluates the compiled term at the given point.
|
---|
| 18 | /// </summary>
|
---|
| 19 | /// <param name="arg">The point at which to evaluate.</param>
|
---|
| 20 | /// <returns>The value of the function represented by the term at the given point.</returns>
|
---|
| 21 | /// <remarks>The number at <c>arg[i]</c> is the value assigned to the variable <c>Variables[i]</c>.</remarks>
|
---|
| 22 | double Evaluate(params double[] arg);
|
---|
| 23 |
|
---|
| 24 | /// <summary>
|
---|
| 25 | /// Computes the gradient of the compiled term at the given point.
|
---|
| 26 | /// </summary>
|
---|
| 27 | /// <param name="arg">The point at which to differentiate.</param>
|
---|
| 28 | /// <returns>A tuple, where the first item is the gradient at <paramref name="arg"/> and the second item is
|
---|
| 29 | /// the value at <paramref name="arg"/>. That is, the second value is the same as running <see cref="Evaluate"/> on
|
---|
| 30 | /// <paramref name="arg"/>.</returns>
|
---|
| 31 | /// <remarks>The number at <c>arg[i]</c> is the value assigned to the variable <c>Variables[i]</c>.</remarks>
|
---|
| 32 | Tuple<double[], double> Differentiate<T>(T arg) where T : IList<double>;
|
---|
| 33 |
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// Computes the gradient of the compiled term at the given point.
|
---|
| 36 | /// </summary>
|
---|
| 37 | /// <param name="arg">The point at which to differentiate.</param>
|
---|
| 38 | /// <returns>A tuple, where the first item is the gradient at <paramref name="arg"/> and the second item is
|
---|
| 39 | /// the value at <paramref name="arg"/>. That is, the second value is the same as running <see cref="Evaluate"/> on
|
---|
| 40 | /// <paramref name="arg"/>.</returns>
|
---|
| 41 | /// <remarks>The number at <c>arg[i]</c> is the value assigned to the variable <c>Variables[i]</c>.</remarks>
|
---|
| 42 | Tuple<double[], double> Differentiate(params double[] arg);
|
---|
| 43 |
|
---|
| 44 | /// <summary>
|
---|
| 45 | /// The collection of variables contained in this compiled term.
|
---|
| 46 | /// </summary>
|
---|
| 47 | /// <remarks>
|
---|
| 48 | /// The order of variables in this collection specifies the meaning of each argument in <see cref="Differentiate"/> or
|
---|
| 49 | /// <see cref="Evaluate"/>. That is, the variable at <c>Variables[i]</c> corresponds to the i-th parameter of <see cref="Differentiate"/>
|
---|
| 50 | /// and <see cref="Evaluate"/>.
|
---|
| 51 | /// </remarks>
|
---|
| 52 | ReadOnlyCollection<Variable> Variables { get; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | [ContractClassFor(typeof(ICompiledTerm))]
|
---|
| 56 | abstract class CompiledTermContract : ICompiledTerm
|
---|
| 57 | {
|
---|
| 58 | public double Evaluate(params double[] arg)
|
---|
| 59 | {
|
---|
| 60 | Contract.Requires(arg != null);
|
---|
| 61 | Contract.Requires(arg.Length == Variables.Count);
|
---|
| 62 | return default(double);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public Tuple<double[], double> Differentiate<T>(T arg)
|
---|
| 66 | where T : IList<double>
|
---|
| 67 | {
|
---|
| 68 | Contract.Requires(arg != null);
|
---|
| 69 | Contract.Requires(arg.Count == Variables.Count);
|
---|
| 70 | Contract.Ensures(Contract.Result<Tuple<double[], double>>() != null);
|
---|
| 71 | Contract.Ensures(Contract.Result<Tuple<double[], double>>().Item1.Length == arg.Count);
|
---|
| 72 | return null;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public Tuple<double[], double> Differentiate(params double[] arg)
|
---|
| 76 | {
|
---|
| 77 | Contract.Requires(arg != null);
|
---|
| 78 | Contract.Requires(arg.Length == Variables.Count);
|
---|
| 79 | Contract.Ensures(Contract.Result<Tuple<double[], double>>() != null);
|
---|
| 80 | Contract.Ensures(Contract.Result<Tuple<double[], double>>().Item1.Length == arg.Length);
|
---|
| 81 | return null;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public ReadOnlyCollection<Variable> Variables
|
---|
| 85 | {
|
---|
| 86 | get
|
---|
| 87 | {
|
---|
| 88 | Contract.Ensures(Contract.Result<ReadOnlyCollection<Variable>>() != null);
|
---|
| 89 | return null;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | }
|
---|