-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algebra.cs
195 lines (178 loc) · 7.2 KB
/
Algebra.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Reflection.Differentiation
{
public static class Algebra
{
public static Expression<Func<double, double>> Differentiate(
Expression<Func<double, double>> function)
{
x = function.Parameters.First();
return function.Body.GetTerms()
.DifferentiateTerms()
.BuildAddExpression()
.CreateLambda(x);
}
private static ParameterExpression x;
private static IEnumerable<Expression> GetTerms(this Expression expression)
{
if (expression.NodeType == ExpressionType.Add &&
expression is BinaryExpression addExpression)
{
foreach (var left in GetTerms(addExpression.Left))
{
yield return left;
}
foreach (var rigth in GetTerms(addExpression.Right))
{
yield return rigth;
}
}
else
yield return expression;
}
private static IEnumerable<Expression> DifferentiateTerms(this IEnumerable<Expression> terms)
{
foreach (var term in terms)
{
switch (term.NodeType)
{
case ExpressionType.Constant:
yield return GetDerivativeOfConstant();
break;
case ExpressionType.Call:
yield return GetDerivativeOfTrigonometric(term as MethodCallExpression);
break;
default:
yield return GetDerivativeOfParametr(term);
break;
}
}
}
private static Expression BuildAddExpression(this IEnumerable<Expression> differentiatedTerms)
{
var listOfDifferentiatedTerms = differentiatedTerms.ToList();
if (listOfDifferentiatedTerms.Count == 1)
return listOfDifferentiatedTerms.First();
var left = listOfDifferentiatedTerms[0];
for (int i = 1; i < listOfDifferentiatedTerms.Count; i++)
{
left = Expression.Add(left, listOfDifferentiatedTerms[i]);
}
return left;
}
private static Expression<Func<double, double>> CreateLambda(this Expression expression,
ParameterExpression parameter)
{
return Expression
.Lambda<Func<double, double>>(expression, parameter);
}
#region Rules of differentiation
//f(x) = c*x^y => f'(x) = c*y*x^y-1 c,y - constant
private static Expression GetDerivativeOfParametr(Expression function)
{
if (function is ParameterExpression)
return Expression.Constant(1d);
var functionFeatures = FunctionFeatures.GetFeatures(function as BinaryExpression);
return Expression.Multiply(
Expression.Constant(functionFeatures.Constant * functionFeatures.Pow),
Expression.Call(null,
typeof(Math).GetMethod("Pow"),
x,
Expression.Constant((functionFeatures.Pow - 1))));
}
//f(x) = c => f'(x) = 0 c - constant
private static Expression GetDerivativeOfConstant()
{
return Expression.Constant(0d);
}
//f(x) = sin(x) => f'(x) = cos(x)
//f(x) = sin(x^2) => f'(x) = sin(x^2)*2
private static Expression GetDerivativeOfTrigonometric(MethodCallExpression function)
{
var derivative = GetDerivative(function.Method, function.Arguments.ToArray());
if ((function.Arguments.First().NodeType != ExpressionType.Parameter))
{
var derivativeOfArgument = function.Arguments.First()
.GetTerms()
.DifferentiateTerms()
.BuildAddExpression();
return Expression.Multiply(derivative, derivativeOfArgument);
}
else
{
return derivative;
}
Expression GetDerivative(MethodInfo currentMethod, Expression[] arguments)
{
if (currentMethod.Name == "Sin")
{
return Expression.Call(null, typeof(Math).GetMethod("Cos"), arguments);
}
if (currentMethod.Name == "Cos")
{
return Expression.Multiply(
Expression.Constant(-1d),
Expression.Call(null, typeof(Math).GetMethod("Sin"), arguments));
}
throw new NotImplementedException($"Differentiation for the {currentMethod.Name}" +
$" method has not yet been implemented");
}
}
#endregion
}
public class FunctionFeatures
{
public double Constant { get; private set; }
public double Pow { get; private set; }
public static FunctionFeatures operator +(FunctionFeatures left, FunctionFeatures rigth)
{
return new FunctionFeatures()
{
Constant = left.Constant + rigth.Constant,
Pow = left.Pow + rigth.Pow
};
}
public static FunctionFeatures GetFeatures(BinaryExpression subExpression)
{
var funcFeatures = CalculateFeatures(subExpression);
if (funcFeatures.Constant == 0)
funcFeatures.Constant = 1;
return funcFeatures;
}
private static FunctionFeatures CalculateFeatures(BinaryExpression function)
{
var funcFeatures = new FunctionFeatures();
funcFeatures += CheckSubExpression(function.Left);
funcFeatures += CheckSubExpression(function.Right);
FunctionFeatures CheckSubExpression(Expression checkExpression)
{
var ff = new FunctionFeatures();
if (!IsTheEndNode(checkExpression))
{
ff += CalculateFeatures(checkExpression as BinaryExpression);
}
else
{
if (checkExpression is ConstantExpression constant)
{
ff.Constant += (double)constant.Value;
}
else
{
ff.Pow++;
}
}
return ff;
}
bool IsTheEndNode(Expression checkExpression)
{
return checkExpression is ConstantExpression || checkExpression is ParameterExpression;
}
return funcFeatures;
}
}
}