Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.AutoDiff/1.0/AutoDiff-1.0/BinaryFunc.cs @ 13400

Last change on this file since 13400 was 8703, checked in by gkronber, 12 years ago

#1960 added HL wrapper plugin for AutoDiff

File size: 4.0 KB
RevLine 
[8703]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Diagnostics.Contracts;
6
7namespace AutoDiff
8{
9    /// <summary>
10    /// Represents a custom binary function term. The user provides custom delegates
11    /// to evaluate and compute the gradient of the function.
12    /// </summary>
13    public class BinaryFunc : Term
14    {
15        private readonly Func<double, double, double> eval;
16        private readonly Func<double, double, Tuple<double, double>> diff;
17        private readonly Term left;
18        private readonly Term right;
19
20        /// <summary>
21        /// Initializes a new instance of the <see cref="BinaryFunc"/> class.
22        /// </summary>
23        /// <param name="eval">The evaluation method for the custom function.</param>
24        /// <param name="diff">The differentiation method for the custom function.</param>
25        /// <param name="left">The left argument term for the binary function.</param>
26        /// <param name="right">The right argument term for the binary function.</param>
27        public BinaryFunc(
28            Func<double, double, double> eval,
29            Func<double, double, Tuple<double, double>> diff,
30            Term left, Term right)
31        {
32            Contract.Requires(eval != null);
33            Contract.Requires(diff != null);
34            Contract.Requires(left != null);
35            Contract.Requires(right != null);
36
37            Contract.Ensures(Eval == eval);
38            Contract.Ensures(Diff == diff);
39            Contract.Ensures(Left == left);
40            Contract.Ensures(Right == right);
41
42            this.eval = eval;
43            this.diff = diff;
44            this.left = left;
45            this.right = right;
46        }
47
48        /// <summary>
49        /// Constructs a factory delegate that creates similary binary functions for different terms.
50        /// </summary>
51        /// <param name="eval">The evaluation method for the custom function.</param>
52        /// <param name="diff">The differentiation method for the custom function.</param>
53        /// <returns>The described factory delegate</returns>
54        public static Func<Term, Term, BinaryFunc> Factory(Func<double, double, double> eval, Func<double, double, Tuple<double, double>> diff)
55        {
56            Contract.Requires(eval != null);
57            Contract.Requires(diff != null);
58            Contract.Ensures(Contract.Result < Func<Term, Term, BinaryFunc>>() != null);
59
60            Func<Term, Term, BinaryFunc> result = (left, right) => new BinaryFunc(eval, diff, left, right);
61            return result;
62        }
63
64        /// <summary>
65        /// Gets the evaluation delegate
66        /// </summary>
67        public Func<double, double, double> Eval { get { return eval; } }
68
69        /// <summary>
70        /// Gets the differentiation delegate
71        /// </summary>
72        public Func<double, double, Tuple<double, double>> Diff { get { return diff; } }
73
74        /// <summary>
75        /// Gets the function's left argument term
76        /// </summary>
77        public Term Left { get { return left; } }
78
79        /// <summary>
80        /// Gets the function's right argument term
81        /// </summary>
82        public Term Right { get { return right; } }
83
84        /// <summary>
85        /// Accepts a term visitor
86        /// </summary>
87        /// <param name="visitor">The term visitor to accept</param>
88        public override void Accept(ITermVisitor visitor)
89        {
90            visitor.Visit(this);
91        }
92
93        /// <summary>
94        /// Accepts a term visitor with a generic result
95        /// </summary>
96        /// <typeparam name="TResult">The type of the result from the visitor's function</typeparam>
97        /// <param name="visitor">The visitor to accept</param>
98        /// <returns>The result from the visitor's visit function.</returns>
99        public override TResult Accept<TResult>(ITermVisitor<TResult> visitor)
100        {
101            return visitor.Visit(this);
102        }
103    }
104}
Note: See TracBrowser for help on using the repository browser.