문제

I need to get a Function of Discrete sum.
I mean Discrete sum of another function, not of Array!
I have got the function of Array. How to modify it for custom Func.

public static double Sum(double[] A1, double t1, double t2)
{
    double s = 0;
    for(long i = t1; i < t2; i++)
    {
        s += A1[i];
    }
    return s;
}
도움이 되었습니까?

해결책

This is an answer.
As you can see in the main method where you call this function, it is not necessary to fill bounds correctly. It does not depend how you will sort bounds.

    public static double DiscreteSum(Func<double, double> F1, double t1, double t2)
    {
        double s = 0;
        for(long i = (t1<t2)?t1:t2; i < (t1<t2)?t2:t1; i++)
        {
            s += F1(i);
        }
        return s;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top