Frage

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;
}
War es hilfreich?

Lösung

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;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top