문제

I'm interested in creating a function Derivative that returns a function that is the derivative of some function that is passed to it, at some point. However, I want to be able to specialize this so that, for specific functions, I can return the analytical solution.

So, I'm looking for something like this:

auto Derivate(alias Function)(x)
{ return (Function(x+h) - Function(x-h))/(2h);}

auto Derivate(BSpline!(k)(x))(x)
{ return k * BSpline!(k-1)(x) + x * BSpline!(k-1)(x); }

However, I currently have BSpline defined this way:

pure Real BSpline(int k : 0, Real)(scope Real x, scope const(Real)[] t)
{
    if (t[0] <= x && x < t[k+1])
        return 1;
    else
        return 0;
}

pure Real BSpline(int k, Real)(scope Real x, scope const(Real)[] t)
{
    if (t[0] <= x && x < t[k+1])
    {
        Real a = (x - t[0]) / (t[k] - t[0]);
        Real b = (t[k+1] - x) / (t[k+1] - t[1]);
        Real c = BSpline!(k-1,Real)(x, t[0..k+1]);
        Real d = BSpline!(k-1,Real)(x, t[1..k+2]);
        Real rv = (c?c*a:c) + (d?d*b:d);
        return rv;
    }
    else
        return 0;
}

So the type signature on BSpline is going to be Real function(Real,Real), which isn't differentiable from any other kind of function. Is the way to solve this to create a "BSpline" class with opCall defined? Or can I do some sort of typedef to identify this function?

Thanks!

도움이 되었습니까?

해결책

To specialize a template, you have to use the : notation:

auto foo(alias F_, X_)(X_ x) {
    /* code here ... */
}

auto foo(alias F_ : BSpline, X_)(X_ x) {
    /* specialized version here */
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top