-
25-10-2019 - |
题
我有兴趣创建一个函数导数,该函数导数返回一个函数,该函数在某个时候传递给了某些函数。但是,我希望能够对此进行专门研究,以便对于特定功能,我可以返回分析解决方案。
所以,我正在寻找这样的东西:
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); }
但是,我目前有这样定义的BSPline:
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;
}
因此,BSpline上的类型签名将是真实的功能(真实的,真实的),它与任何其他功能都不可区分。是否解决了使用OPCALL定义的“ BSPLINE”类解决的方法?还是我可以执行某种类型的类型来识别此功能?
谢谢!
解决方案
要专门使用模板,您必须使用 :
符号:
auto foo(alias F_, X_)(X_ x) {
/* code here ... */
}
auto foo(alias F_ : BSpline, X_)(X_ x) {
/* specialized version here */
}
不隶属于 StackOverflow