0

Very simply, I’m using an optimization library in C++ that takes in a function of a single variable. I would like to be able to pass in multiple parameters though (which this library does not support). What I would like to do is create a lambda function of the sort (kind of like in Python) that lets me represent the cost function as a function of a single variable that passes in two parameters.

Here’s a simplified version of what I’m going for in pseudocode. Any help would be much appreciated. I can’t seem to get this to work with lambda in C++.

Optimize comes from a library (asa047). The version I wrote here isn’t at all realistic, but is just meant to demonstrate what this function takes in.

double cost(double x, double param1, double param2){
    return x*param1 + param2;

}

double optimize(double fn( double x), double initial_value){
    return optimal_x;

}

int main(){

    double param1 = 2;
    double param2 = 3; 
    function_object f; //What I would like to create
    f(double x){
        return cost(x,param1,param2);
    }
    optimize(f,2); 
}
  • Where do `param1` and `param2` come from in the "real thing" ? (I'm sure they aren't just local variables as you have in the samples) – John3136 Mar 11 '21 at 23:34
  • 1
    Unless the optimization library (which one?) allows you to pass user-defined data to your callback function (do you have any documentation for it?), what you are asking for is simply not possible (without resorting to creating a low-level thunk). The syntax you propose can be done easily using a [lambda](https://en.cppreference.com/w/cpp/language/lambda), however a *capturing* lambda (which yours would have to be) can't be used with a C-style function pointer, only a *non-capturing* lambda can do that. – Remy Lebeau Mar 11 '21 at 23:34
  • 1
    @JerryJeremiah that won't work if `optimize()` expects a C-style function pointer, ie because it itself is in a C library (which I suspect it is), or otherwise does not support `std::function` or templated Callable parameters. – Remy Lebeau Mar 11 '21 at 23:36
  • Is ```optimize``` your own function? Or is this from a library? – lnogueir Mar 11 '21 at 23:41
  • 2
    If it's [this asa047](https://people.sc.fsu.edu/~jburkardt/cpp_src/asa047/asa047.html), the function is declared to take an *array* of values as an argument, not a single value. – dxiv Mar 11 '21 at 23:48
  • If you only want GCC, you can do it with local functions: https://stackoverflow.com/questions/1023261/is-there-a-way-to-do-currying-in-c – Jerry Jeremiah Mar 11 '21 at 23:49
  • 1
    You can fake it sorta, by abusing static template members http://coliru.stacked-crooked.com/a/f66a402941b535ee – Mooing Duck Mar 11 '21 at 23:51
  • 1
    Another _really_ bad idea is to `static_assert(sizeof(double)>=sizeof(void*))`, and then play `reinterpret_cast` games on the `x` parameter... – Mooing Duck Mar 12 '21 at 00:05

1 Answers1

1

What I could see under the link to the asa47 library is that the function comes with source code. That means you can modify its parameters to pass any additional stuff as you need. I think that's the easiest and most correct way to achieve what you need. I.e. if fir example you want to additional int parameter, double fn ( double x[] ) can be replaces with something like double fn ( double x[], int p), then add int p to the "nelmin" function itself, and then modify call to fn() in the nelmin() to pass that additional p.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41