我有类似的公式来计算哈特利变换。唯一的区别是以下代码行中的输入函数 - sin、cos、exp:
Math.Exp((double)tau)
Math.Sin((double)tau)
Math.Cos((double)tau)
如何转义几乎相同的代码片段并缩短后续片段中的代码?
private void CountHartley(ref double [] arr, string function)
{
int N = arr.Length;
if (function == "exp")
{
for (int nu = 0, tau = 0; ((nu < N) && (tau < N)); nu++, tau++)
{
arr[nu] = 1 / (double)N *
Math.Exp((double)tau) *
(Math.Sin(2 * Math.PI * nu * tau / (double) N) +
Math.Cos(2 * Math.PI * nu * tau / (double) N));
}
}
else if (function == "sin")
{
for (int nu = 0, tau = 0; ((nu < N) && (tau < N)); nu++, tau++)
{
arr[nu] = 1 / (double)N *
Math.Sin((double)tau) *
(Math.Sin(2 * Math.PI * nu * tau / (double)N) +
Math.Cos(2 * Math.PI * nu * tau / (double)N));
}
}
else
{
for (int nu = 0, tau = 0; ((nu < N) && (tau < N)); nu++, tau++)
{
arr[nu] = 1 / (double)N *
Math.Cos((double)tau) *
(Math.Sin(2 * Math.PI * nu * tau / (double)N) +
Math.Cos(2 * Math.PI * nu * tau / (double)N));
}
}
}