Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.ExtLibs/HeuristicLab.AutoDiff/1.0/AutoDiff-1.0/TermBuilder.cs @ 17137

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

#1960 added HL wrapper plugin for AutoDiff

File size: 6.4 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    /// A collection of static methods to build new terms
11    /// </summary>
12    public static class TermBuilder
13    {
14        /// <summary>
15        /// Builds a new constant term.
16        /// </summary>
17        /// <param name="value">The constant value</param>
18        /// <returns>The constant term.</returns>
19        public static Term Constant(double value)
20        {
21            Contract.Ensures(Contract.Result<Term>() != null);
22
23            if (value == 0)
24                return new Zero();
25            else
26                return new Constant(value);
27        }
28
29        /// <summary>
30        /// Builds a sum of given terms.
31        /// </summary>
32        /// <param name="terms">The collection of terms in the sum.</param>
33        /// <returns>A term representing the sum of the terms in <paramref name="terms"/>.</returns>
34        public static Sum Sum(IEnumerable<Term> terms)
35        {
36            Contract.Requires(terms.Where(term => !(term is Zero)).Count() >= 2); // require at-least two non-zero terms.
37            Contract.Requires(Contract.ForAll(terms, term => term != null));
38            Contract.Ensures(Contract.Result<Sum>() != null);
39
40            terms = terms.Where(term => !(term is Zero));
41            return new Sum(terms);
42        }
43
44        /// <summary>
45        /// Builds a sum of given terms.
46        /// </summary>
47        /// <param name="v1">The first term in the sum</param>
48        /// <param name="v2">The second term in the sum</param>
49        /// <param name="rest">The rest of the terms in the sum.</param>
50        /// <returns>A term representing the sum of <paramref name="v1"/>, <paramref name="v2"/> and the terms in <paramref name="rest"/>.</returns>
51        public static Sum Sum(Term v1, Term v2, params Term[] rest)
52        {
53            Contract.Requires(v1 != null);
54            Contract.Requires(v2 != null);
55            Contract.Requires(Contract.ForAll(rest, term => term != null));
56            Contract.Ensures(Contract.Result<Sum>() != null);
57
58            var allTerms = new Term[] { v1, v2 }.Concat(rest);
59            return Sum(allTerms);
60        }
61
62        /// <summary>
63        /// Builds a product of given terms.
64        /// </summary>
65        /// <param name="v1">The first term in the product</param>
66        /// <param name="v2">The second term in the product</param>
67        /// <param name="rest">The rest of the terms in the product</param>
68        /// <returns>A term representing the product of <paramref name="v1"/>, <paramref name="v2"/> and the terms in <paramref name="rest"/>.</returns>
69        public static Term Product(Term v1, Term v2, params Term[] rest)
70        {
71            Contract.Requires(v1 != null);
72            Contract.Requires(v2 != null);
73            Contract.Requires(Contract.ForAll(rest, term => term != null));
74            Contract.Ensures(Contract.Result<Term>() != null);
75
76            var result = new Product(v1, v2);
77            foreach (var item in rest)
78                result = new Product(result, item);
79
80            return result;
81        }
82
83        /// <summary>
84        /// Builds a power terms given a base and a constant exponent
85        /// </summary>
86        /// <param name="t">The power base term</param>
87        /// <param name="power">The exponent</param>
88        /// <returns>A term representing <c>t^power</c>.</returns>
89        public static Term Power(Term t, double power)
90        {
91            Contract.Requires(t != null);
92            Contract.Ensures(Contract.Result<Term>() != null);
93
94            return new ConstPower(t, power);
95        }
96
97        /// <summary>
98        /// Builds a power term given a base term and an exponent term.
99        /// </summary>
100        /// <param name="baseTerm">The base term</param>
101        /// <param name="exponent">The exponent term</param>
102        /// <returns></returns>
103        public static Term Power(Term baseTerm, Term exponent)
104        {
105            Contract.Requires(baseTerm != null);
106            Contract.Requires(exponent != null);
107            Contract.Ensures(Contract.Result<Term>() != null);
108
109            return new TermPower(baseTerm, exponent);
110        }
111
112        /// <summary>
113        /// Builds a term representing the exponential function e^x.
114        /// </summary>
115        /// <param name="arg">The function's exponent</param>
116        /// <returns>A term representing e^arg.</returns>
117        public static Term Exp(Term arg)
118        {
119            Contract.Requires(arg != null);
120            Contract.Ensures(Contract.Result<Term>() != null);
121
122            return new Exp(arg);
123        }
124
125        /// <summary>
126        /// Builds a term representing the natural logarithm.
127        /// </summary>
128        /// <param name="arg">The natural logarithm's argument.</param>
129        /// <returns>A term representing the natural logarithm of <paramref name="arg"/></returns>
130        public static Term Log(Term arg)
131        {
132            Contract.Requires(arg != null);
133            Contract.Ensures(Contract.Result<Term>() != null);
134
135            return new Log(arg);
136        }
137
138        /// <summary>
139        /// Constructs a 2D quadratic form given the vector components x1, x2 and the matrix coefficients a11, a12, a21, a22.
140        /// </summary>
141        /// <param name="x1">First vector component</param>
142        /// <param name="x2">Second vector component</param>
143        /// <param name="a11">First row, first column matrix component</param>
144        /// <param name="a12">First row, second column matrix component</param>
145        /// <param name="a21">Second row, first column matrix component</param>
146        /// <param name="a22">Second row, second column matrix component</param>
147        /// <returns>A term describing the quadratic form</returns>
148        public static Term QuadForm(Term x1, Term x2, Term a11, Term a12, Term a21, Term a22)
149        {
150            Contract.Requires(x1 != null);
151            Contract.Requires(x2 != null);
152            Contract.Requires(a11 != null);
153            Contract.Requires(a12 != null);
154            Contract.Requires(a21 != null);
155            Contract.Requires(a22 != null);
156            Contract.Ensures(Contract.Result<Term>() != null);
157
158            return Sum(a11 * Power(x1, 2), (a12 + a21) * x1 * x2, a22 * Power(x2, 2));
159        }
160    }
161}
Note: See TracBrowser for help on using the repository browser.