1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Diagnostics.Contracts;
|
---|
6 | using System.Collections.ObjectModel;
|
---|
7 |
|
---|
8 | namespace AutoDiff
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Represents a custom n-ary function term. The user provides custom delegates
|
---|
12 | /// to evaluate and compute the gradient of the function.
|
---|
13 | /// </summary>
|
---|
14 | public class NaryFunc : Term
|
---|
15 | {
|
---|
16 | private readonly Func<double[], double> eval;
|
---|
17 | private readonly Func<double[], double[]> diff;
|
---|
18 |
|
---|
19 | /// <summary>
|
---|
20 | /// Initializes a new instance of the <see cref="NaryFunc"/> class.
|
---|
21 | /// </summary>
|
---|
22 | /// <param name="eval">The evaluation method for the custom function.</param>
|
---|
23 | /// <param name="diff">The differentiation method for the custom function.</param>
|
---|
24 | /// <param name="terms">The argument terms for the n-ary function.</param>
|
---|
25 | public NaryFunc(
|
---|
26 | Func<double[], double> eval,
|
---|
27 | Func<double[], double[]> diff,
|
---|
28 | IEnumerable<Term> terms)
|
---|
29 | {
|
---|
30 | this.eval = eval;
|
---|
31 | this.diff = diff;
|
---|
32 | this.Terms = Array.AsReadOnly(terms.ToArray());
|
---|
33 | }
|
---|
34 |
|
---|
35 | /// <summary>
|
---|
36 | /// Constructs a factory delegate that creates similary n-ary functions for different terms.
|
---|
37 | /// </summary>
|
---|
38 | /// <param name="eval">The evaluation method for the custom function.</param>
|
---|
39 | /// <param name="diff">The differentiation method for the custom function.</param>
|
---|
40 | /// <returns>The described factory delegate</returns>
|
---|
41 | public static Func<IEnumerable<Term>, NaryFunc> Factory(Func<double[], double> eval, Func<double[], double[]> diff)
|
---|
42 | {
|
---|
43 | Contract.Requires(eval != null);
|
---|
44 | Contract.Requires(diff != null);
|
---|
45 | Contract.Ensures(Contract.Result<Func<IEnumerable<Term>, NaryFunc>>() != null);
|
---|
46 |
|
---|
47 | Func<IEnumerable<Term>, NaryFunc> result = (terms) => new NaryFunc(eval, diff, terms);
|
---|
48 | return result;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Gets the evaluation delegate
|
---|
53 | /// </summary>
|
---|
54 | public Func<double[], double> Eval { get { return eval; } }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Gets the differentiation delegate
|
---|
58 | /// </summary>
|
---|
59 | public Func<double[], double[]> Diff { get { return diff; } }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Gets the arguments of this function
|
---|
63 | /// </summary>
|
---|
64 | public ReadOnlyCollection<Term> Terms { get; private set; }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Accepts a term visitor
|
---|
68 | /// </summary>
|
---|
69 | /// <param name="visitor">The term visitor to accept</param>
|
---|
70 | public override void Accept(ITermVisitor visitor)
|
---|
71 | {
|
---|
72 | visitor.Visit(this);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// Accepts a term visitor with a generic result
|
---|
77 | /// </summary>
|
---|
78 | /// <typeparam name="TResult">The type of the result from the visitor's function</typeparam>
|
---|
79 | /// <param name="visitor">The visitor to accept</param>
|
---|
80 | /// <returns>The result from the visitor's visit function.</returns>
|
---|
81 | public override TResult Accept<TResult>(ITermVisitor<TResult> visitor)
|
---|
82 | {
|
---|
83 | return visitor.Visit(this);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|