1 | using System.Diagnostics;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
4 | /// <summary>
|
---|
5 | /// An algebraic type which has a value as well as the partial derivatives of the value over multiple variables.
|
---|
6 | /// </summary>
|
---|
7 | /// <typeparam name="V"></typeparam>
|
---|
8 | [DebuggerDisplay("v={Value}; dv={dv}")]
|
---|
9 | public class MultivariateDual<V> : IAlgebraicType<MultivariateDual<V>> where V : IAlgebraicType<V>, new() {
|
---|
10 | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
---|
11 | private V v;
|
---|
12 | public V Value => v;
|
---|
13 |
|
---|
14 | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
---|
15 | private AlgebraicSparseVector<object, V> dv;
|
---|
16 | public AlgebraicSparseVector<object, V> Gradient => dv; // <key,value> partial derivative identified via the key
|
---|
17 |
|
---|
18 | private MultivariateDual(MultivariateDual<V> orig) { this.v = orig.v.Clone(); this.dv = orig.dv.Clone(); }
|
---|
19 |
|
---|
20 | /// <summary>
|
---|
21 | /// Constructor without partial derivative
|
---|
22 | /// </summary>
|
---|
23 | /// <param name="v"></param>
|
---|
24 | public MultivariateDual(V v) { this.v = v.Clone(); this.dv = new AlgebraicSparseVector<object, V>(); }
|
---|
25 |
|
---|
26 | /// <summary>
|
---|
27 | /// Constructor for multiple partial derivatives
|
---|
28 | /// </summary>
|
---|
29 | /// <param name="v"></param>
|
---|
30 | /// <param name="keys"></param>
|
---|
31 | /// <param name="dv"></param>
|
---|
32 | public MultivariateDual(V v, object[] keys, V[] dv) { this.v = v.Clone(); this.dv = new AlgebraicSparseVector<object, V>(keys, dv); }
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Constructor for a single partial derivative
|
---|
36 | /// </summary>
|
---|
37 | /// <param name="v"></param>
|
---|
38 | /// <param name="key"></param>
|
---|
39 | /// <param name="dv"></param>
|
---|
40 | public MultivariateDual(V v, object key, V dv) { this.v = v.Clone(); this.dv = new AlgebraicSparseVector<object, V>(new[] { key }, new[] { dv }); }
|
---|
41 |
|
---|
42 | /// <summary>
|
---|
43 | /// Constructor with a given value and gradient. For internal use.
|
---|
44 | /// </summary>
|
---|
45 | /// <param name="v">The value (not cloned).</param>
|
---|
46 | /// <param name="gradient">The gradient (not cloned).</param>
|
---|
47 | internal MultivariateDual(V v, AlgebraicSparseVector<object, V> gradient) { this.v = v; this.dv = gradient; }
|
---|
48 |
|
---|
49 | public MultivariateDual<V> Clone() { return new MultivariateDual<V>(this); }
|
---|
50 |
|
---|
51 | public MultivariateDual<V> Zero => new MultivariateDual<V>(Value.Zero, Gradient.Zero);
|
---|
52 | public MultivariateDual<V> One => new MultivariateDual<V>(Value.One, Gradient.Zero);
|
---|
53 |
|
---|
54 | public MultivariateDual<V> Scale(double s) { v.Scale(s); dv.Scale(s); return this; }
|
---|
55 |
|
---|
56 | public MultivariateDual<V> Add(MultivariateDual<V> a) { v.Add(a.v); dv.Add(a.dv); return this; }
|
---|
57 | public MultivariateDual<V> Sub(MultivariateDual<V> a) { v.Sub(a.v); dv.Sub(a.dv); return this; }
|
---|
58 | public MultivariateDual<V> Assign(MultivariateDual<V> a) { v.Assign(a.v); dv.Assign(a.dv); return this; }
|
---|
59 | public MultivariateDual<V> Mul(MultivariateDual<V> a) {
|
---|
60 | // (a(x) * b(x))' = b(x)*a(x)' + b(x)'*a(x);
|
---|
61 | var t1 = a.dv.Clone().Scale(v);
|
---|
62 | var t2 = dv.Clone().Scale(a.v);
|
---|
63 | dv.Assign(t1).Add(t2);
|
---|
64 |
|
---|
65 | v.Mul(a.v);
|
---|
66 | return this;
|
---|
67 | }
|
---|
68 | public MultivariateDual<V> Div(MultivariateDual<V> a) { v.Div(a.v); dv.Mul(a.dv.Inv()); return this; }
|
---|
69 | public MultivariateDual<V> AssignNeg(MultivariateDual<V> a) { v.AssignNeg(a.v); dv.AssignNeg(a.dv); return this; }
|
---|
70 | public MultivariateDual<V> AssignInv(MultivariateDual<V> a) { v.AssignInv(a.v); dv.AssignNeg(a.dv).Scale(v).Scale(v); return this; } // (1/f(x))' = - f(x)' / f(x)^2
|
---|
71 |
|
---|
72 | public MultivariateDual<V> AssignSin(MultivariateDual<V> a) { v.AssignSin(a.v); dv.Assign(a.dv).Scale(a.v.Clone().Cos()); return this; }
|
---|
73 | public MultivariateDual<V> AssignCos(MultivariateDual<V> a) { v.AssignCos(a.v); dv.AssignNeg(a.dv).Scale(a.v.Clone().Sin()); return this; }
|
---|
74 | public MultivariateDual<V> AssignTanh(MultivariateDual<V> a) { v.AssignTanh(a.v); dv.Assign(a.dv.Scale(v.Clone().IntPower(2).Neg().Add(Value.One))); return this; } // tanh(f(x))' = f(x)'sech²(f(x)) = f(x)'(1 - tanh²(f(x)))
|
---|
75 |
|
---|
76 | public MultivariateDual<V> AssignIntPower(MultivariateDual<V> a, int p) { v.AssignIntPower(a.v, p); dv.Assign(a.dv).Scale(p).Scale(a.v.Clone().IntPower(p - 1)); return this; }
|
---|
77 | public MultivariateDual<V> AssignIntRoot(MultivariateDual<V> a, int r) { v.AssignIntRoot(a.v, r); dv.Assign(a.dv).Scale(1.0 / r).Scale(a.v.IntRoot(r - 1)); return this; }
|
---|
78 |
|
---|
79 | public MultivariateDual<V> AssignExp(MultivariateDual<V> a) { v.AssignExp(a.v); dv.Assign(a.dv).Scale(v); return this; } // exp(f(x)) = exp(f(x))*f(x)'
|
---|
80 | public MultivariateDual<V> AssignLog(MultivariateDual<V> a) { v.AssignLog(a.v); dv.Assign(a.dv).Scale(a.v.Clone().Inv()); return this; } // log(x)' = 1/f(x) * f(x)'
|
---|
81 |
|
---|
82 | public MultivariateDual<V> AssignAbs(MultivariateDual<V> a) { v.AssignAbs(a.v); dv.Assign(a.dv).Scale(a.v.Clone().Sgn()); return this; } // abs(f(x))' = f(x)*f'(x) / |f(x)| doesn't work for intervals
|
---|
83 | public MultivariateDual<V> AssignSgn(MultivariateDual<V> a) { v.AssignSgn(a.v); dv = a.dv.Zero; return this; } // sign(f(x))' = 0;
|
---|
84 |
|
---|
85 | public MultivariateDual<V> AssignMin(MultivariateDual<V> other) {
|
---|
86 | XXX
|
---|
87 | }
|
---|
88 |
|
---|
89 | public MultivariateDual<V> AssignMax(MultivariateDual<V> other) {
|
---|
90 | XXX
|
---|
91 | }
|
---|
92 | }
|
---|
93 | } |
---|