protected override Expression TransformFunction(FunctionExpression functionExpression) { if (functionExpression.Function == Operators.Diff) { VariableListExtractor extractor = new VariableListExtractor(); extractor.Visit(functionExpression[1]); IReadOnlyList <Variable> variables = extractor.Variables; if (variables.Count == 0) { throw new InvalidOperationException("Cannot calculate derivative by constant"); } throw new NotImplementedException(); } if (functionExpression.Function == Operators.Plus) { return(Transform(functionExpression[0])); } if (functionExpression.Function is Operator) { SumAggregator aggregator = new SumAggregator(); aggregator.VisitFunction(functionExpression); return(aggregator.ToExpression()); } return(functionExpression.Function.Apply(functionExpression.Select(Transform))); }
public override void VisitFunction(FunctionExpression functionExpression) { if (functionExpression.Function == Operators.Plus) { Visit(functionExpression[0].Simplify()); } else if (functionExpression.Function == Operators.Negate) { _sign = !_sign; Visit(functionExpression[0].Simplify()); } else if (functionExpression.Function == Operators.Multiply) { foreach (Expression factor in functionExpression) { Visit(factor.Simplify()); } } else if (functionExpression.Function == Operators.Divide) { Visit(functionExpression[0].Simplify()); ProductAggregator aggregator = new ProductAggregator(); aggregator.Visit(functionExpression[1].Simplify()); _sign ^= aggregator._sign; _value /= aggregator._value; Visit(aggregator._divider); foreach (Expression factor in aggregator._factors) { _divider *= factor; } } else { _factors = _factors.Add(functionExpression.Function. Apply(functionExpression.Select(arg => arg.Simplify()))); } }
protected override Expression TransformFunction(FunctionExpression function) => new FunctionExpression(function.Function, function.Select(Transform).ToArray());