Suppose I want to define function SinX = sin(x)/x which is continuous and has a continuous derivative at x=0. I am trying to avoid divisions by 0 by specifying that: if x < 1e3 set SinX, otherwise set SinX to sin(x)/x. This is what I have for now:
private class SinXFunction : Function
{
private Function f;
public SinXFunction(Function f)
{
this.f = f;
}
protected override double ComputeValue(IEvaluator evaluation)
{
double temp = f.Value(evaluation);
if (Math.Abs(temp) < 1e3)
return 1;
return Math.Sin(temp) / temp;
}
protected override Function ComputeDerivative(Variable variable)
{
//What do I need to write here to avoid divisions by 0???
CosFunction cos = new CosFunction(f);
return (cos  this) / f * f.Derivative(variable);//this gives NaN if f=0!
}
public override Expression Compile(CodeGenerator generator)
{
return generator.GetReference(f) + "<1e3?1:(Math.Cos(" + generator.GetReference(f) + ")Math.Sin(" + generator.GetReference(f) + ")/" + generator.GetReference(f) + ")/" + generator.GetReference(f);
}
}
Is there any way to define function ComputeDerivative so we avoid divisions by 0? Even if we define a new class (say SinXDerivativeFunction) the derivative of this new class will again involve divisions by 0.
Thanks a lot!
Andrew
