Walker
Walker

Reputation: 341

Defining a mathemetical function and evaluate within a C/C++ function

The pseudo-code below depicts a computation I want to do. The idea is designing a C/C++ function that takes any mathematical function of x and evaluates the sum of the first N terms. function(x) could be any function e.g. 2x-1 , 2x , 1/x , etc . x varies from zero to N . I think challenge is how to design the function(x) datastructure , am not sure if this is achievable without any datastructure(this would be better) .

   function(x) = 2*x - 1 ;              

   sum_expression_to_N(  function(x)  , N ){

      float sum = 0.0;

      for ( int x =0; x<=N; x++){
           sum = sum +  function(x)       
      }
      return sum ;
  }

Upvotes: 4

Views: 225

Answers (5)

MSalters
MSalters

Reputation: 179991

You're reinventing std::accumulate. (your usage with x=0...N can be handled by boost's counting iterator, and accumulate would want the binary operator sum += f(x) instead of the default sum+=x.

In C++, functions that take other functions avoid making assumptions about what exactly they're calling. You see this in std::accumulate: it is a template so it can accept any kind of function (as well as any type of input iterator).

Upvotes: 2

Lingxi
Lingxi

Reputation: 14987

This is trivial in modern C++. For example, you can write

#include <iostream>

template <typename F>
float sum_expr_to_n(F f, int n) {
    float sum = 0;
    for (int i = 0; i <= n; ++i) sum += f(i);
    return sum;
}

int main() {
    auto f = [](int x) { return 2 * x - 1; };
    std::cout << sum_expr_to_n(f, 3) << std::endl;
}

Upvotes: 1

smac89
smac89

Reputation: 43206

This is my crack at it (C++ answer):

#include <iostream>
#include <vector>

using func = int (*)(int);

// define your functions here -> f1, f2, f3,...

int main() {
    std::vector<func> functions = {f1, f2, f3,...};

    for (func f : functions) {
        int sum = 0;
        for (int x = 0; x <= N; x++) {
           sum = sum + f(x)       
        }
        std::cout << sum << '\n';
    }
    return 0;
}

Note that the functions you define should follow the pattern given by the func pointer.

So the function for this 2x-1, has to look like:

int f1(int x) {
    return 2 * x - 1;
}

And the same thing goes for the other ones. So only the logic changes, but not the function parameters or return type

Upvotes: 0

Baum mit Augen
Baum mit Augen

Reputation: 50081

For your usecase, you can use the very easy and generic template feature C++ has:

template <class MathFunction> 
double foo (MathFunction function) {
    return function(1);
}

This handles anything that can be used as a 1-D math function, including lambdas, plain functions, std::functions, functors etc. (Live)

As this is also tagged C and the above is C++, you can also use function pointers:

double fun (double (*function)(double)) {
    return function(1);
}

This works for normal functions, which is all you have in C anyways. (Live)

Upvotes: 1

Chris Cinelli
Chris Cinelli

Reputation: 4829

Is that function supposed to be input by the user at runtime or can you just have it at compile time? In the second case it is trivial. If you need to evaluate at runtime you need to parse the string and create a representation of the expression (usually a tree). Google "Parsing Trees" and look also at parsing math expression in c/c++

Upvotes: 0

Related Questions