Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.AutoDiff/1.0/AutoDiff-1.0/UnaryFunc.cs @ 12440

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

#1960 added HL wrapper plugin for AutoDiff

File size: 3.5 KB
Line 
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 unary function term. The user provides custom delegates
11    /// to evaluate and compute the derivative (differentiate) the function.
12    /// </summary>
13    public class UnaryFunc : Term
14    {
15        private readonly Func<double, double> eval;
16        private readonly Func<double, double> diff;
17        private readonly Term argument;
18
19        /// <summary>
20        /// Initializes a new instance of the <see cref="UnaryFunc"/> 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="argument">The argument term for the unary function</param>
25        public UnaryFunc(Func<double, double> eval, Func<double, double> diff, Term argument)
26        {
27            Contract.Requires(eval != null);
28            Contract.Requires(diff != null);
29            Contract.Requires(argument != null);
30
31            Contract.Ensures(Eval == eval);
32            Contract.Ensures(Diff == diff);
33            Contract.Ensures(Argument == argument);
34
35            this.eval = eval;
36            this.diff = diff;
37            this.argument = argument;
38        }
39
40        /// <summary>
41        /// Constructs a factory delegate that creates similar unary functions for different terms.
42        /// </summary>
43        /// <param name="eval">The evaluation method for the custom function.</param>
44        /// <param name="diff">The differentiation method for the custom function.</param>
45        /// <returns>The described factory delegate</returns>
46        public static Func<Term, UnaryFunc> Factory(Func<double, double> eval, Func<double, double> diff)
47        {
48            Contract.Requires(eval != null);
49            Contract.Requires(diff != null);
50            Contract.Ensures(Contract.Result<Func<Term, UnaryFunc>>() != null);
51
52            Func<Term, UnaryFunc> result = term => new UnaryFunc(eval, diff, term);
53            return result;
54        }
55
56        /// <summary>
57        /// Gets the evaluation delegate.
58        /// </summary>
59        public Func<double, double> Eval { get { return eval; } }
60
61
62        /// <summary>
63        /// Gets the differentiation delegate.
64        /// </summary>
65        public Func<double, double> Diff { get { return diff; } }
66
67        /// <summary>
68        /// Gets the function's argument term
69        /// </summary>
70        public Term Argument { get { return argument; } }
71
72        /// <summary>
73        /// Accepts a term visitor
74        /// </summary>
75        /// <param name="visitor">The term visitor to accept</param>
76        public override void Accept(ITermVisitor visitor)
77        {
78            visitor.Visit(this);
79        }
80
81        /// <summary>
82        /// Accepts a term visitor with a generic result
83        /// </summary>
84        /// <typeparam name="TResult">The type of the result from the visitor's function</typeparam>
85        /// <param name="visitor">The visitor to accept</param>
86        /// <returns>
87        /// The result from the visitor's visit function.
88        /// </returns>
89        public override TResult Accept<TResult>(ITermVisitor<TResult> visitor)
90        {
91            return visitor.Visit(this);
92        }
93    }
94}
Note: See TracBrowser for help on using the repository browser.