CodePlexProject Hosting for Open Source Software

public class Sqrt : Function { private Function f; public Sqrt(Function f) { this.f = f; } protected override double ComputeValue(IEvaluation evaluation) { // When asked for the value, just use the static function from the Math class. return Math.Sqrt(f.Value(evaluation)); } protected override Function ComputeDerivative(Variable variable) { // The derivative, on the other hand, is another Function, so that it can be differentiated multiple times. return 0.5 / this * f.Derivative(variable); } public override Expression Compile(Compiler compiler) { // Of course this only needs to be implemented if this function should support compilation. return "Math.Sqrt(" + compiler.GetReference(f) + ")"; } }

public class Atan2 : Function { private Function f, g; public Atan2(Function f, Function g) { this.f = f; this.g = g; } protected override double ComputeValue(IEvaluation evaluation) { return Math.Atan2(f.Value(evaluation), g.Value(evaluation)); } protected override Function ComputeDerivative(Variable variable) { // Using the formula at http://en.wikipedia.org/wiki/Atan2#Derivative. return (g * f.Derivative(variable) - f * g.Derivative(variable)) / (Function.Sqr(g) + Function.Sqr(f)); } public override Expression Compile(Compiler compiler) { return "Math.Atan2(" + compiler.GetReference(f) + "," + compiler.GetReference(g) + ")"; } }

Last edited Jul 17, 2011 at 8:59 AM by bakkedal, version 2