Search code examples
c#vectorsumlambdaintegral

Sum an array of Func<double, double> objects


I am programming a method to obtain the result of an integral of a function of two variables, above one of the variables. The result is therefore a function of the other variable. I am using a numerical method to construct the result in intervals, for each one of that I store in a position of a vector of the type Func<double, double>. I want to sum up all the positions in the vector, and I don´t know how can i implement the Sum method of the vector of Func<double,double> to reach that.

Anyone can help me? We need something like obtain m:

class Program
{
    static void Main(string[] args)
    {
        Func<double, double>[] l = new Func<double, double>[2] {(x) => x, (x) => x +1};

        Func<double, double> m = l.Sum<>...;

    }
}

Solution

  • Do you mean something like this?

    Func<double, double>[] l = new Func<double, double>[2] {(x) => x, (x) => x + 1};
    
    Func<double, double> m = x => l.Sum(f => f(x));
    

    For example, m(5)l[0](5) + l[1](5)(5) + (5 + 1)11.