//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))))); }
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); }