2

I've got similar formulas for counting Hartley transformation. The only difference is input function - sin, cos, exp in the following lines of code:

Math.Exp((double)tau)
Math.Sin((double)tau)
Math.Cos((double)tau)

How can I escape almost identical code fragments and shorten my code in following fragment?

 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));
                }
            }
}

2 Answers 2

6

Instead of passing in a string with the function to use, you can pass in the function directly.

You can use the Func<T, TResult> Delegate for this as follows:

private void CountHartley(ref double [] arr, Func<double, double> function)
{
    int N = arr.Length;
    for (int nu = 0, tau = 0; ((nu < N) && (tau < N)); nu++, tau++)
    {
        arr[nu] = 1 / (double)N *
                     function((double)tau) *
                     (Math.Sin(2 * Math.PI * nu * tau / (double) N) +
                     Math.Cos(2 * Math.PI * nu * tau / (double) N));
    }
}

Usage:

var result = CountHartley(arr, Math.Cos);
Sign up to request clarification or add additional context in comments.

2 Comments

The casts to double could probably be removed (and N declared as double too).
ref is also not needed, and nu and tau seem to be always the same. So the code could indeed be refactored a bit. But I'll leave it intact so the OP can more easily spot the difference to his/her own code.
0

How about passing in the Math.* as an argument to a Func<double, double> function parameter?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.