Pass lambda expression on command line

Asked

Viewed 90 times

4

I created a program to calculate the defined integral of a function, but I want to be able to run it through the terminal.

#include <iostream>
#include <functional>
#include <cmath>

#define PI acos(-1)

using namespace std;

// Função que calcula a integral de uma função matemática (func) no intervalo
// [inf, sup] com Δx = (inf - sup) / n e inf <= sup
inline long double integral (const function<long double (const long double&)> &func, const long double &inf, const long double &sup, const long double &n) {
  if (inf > sup) return 0;
  if (n <= 0) return 0;

  register long double &&area = 0;
  register long double &&delta = (sup - inf) / n;

  for (register long double i = inf; i <= sup; i += delta) {
    area += delta * func(i);
  }
  return area;
}

int main () {
  const function<long double (const long double&)> &&f1 = [](const long double &x)-> long double {return pow(sin(x), 3) * cos(x);};
  const long double &vf1 = integral(f1, 0, PI / 2, 10000000.0);

  const function<long double (const long double&)> &&f2 = [](const long double &x)-> long double {return sin(x);};
  const long double &vf2 = integral(f2, 0, 2 * PI, 10000000.0);

  const function<long double (const long double&)> &&f3 = [](const long double &x)-> long double {return x * x;};
  const long double &vf3 = integral(f3, 0, 1, 10000000.0);

  const function<long double (const long double&)> &&f4 = [](const long double &x)-> long double {return sqrt(1 - x * x);};
  const long double &vf4 = integral(f4, 0, 1, 10000000.0);

  cout << "Area: " << vf1 << " u.a" << endl;
  cout << "Area: " << vf2 << " u.a" << endl;
  cout << "Area: " << vf3 << " u.a" << endl;
  cout << "Area: " << vf4 << " u.a" << endl;
  return 0;
}

I wish I could do something like that on the command line:

integral "x*x" 0 1 100000

Any suggestions/hint?

  • At a glance here maybe it’ll help you.

  • I would like to know why both operator && in its code, as well as the register also

1 answer

5


Lambda is something internal to the code (essentially a pointer to a function), it does not pass through the command line (it is not a magic thing that turns into executable code).

The only way to achieve something close to what you want is to create a parser (even if simplified to fit only some types of code) of the content passed to the parameter of the main() and select the lambda you want according to what you find. It’s a very complex thing to do for something that seems to be just a simple exercise.

What you could do more simply is to have a parameter that would receive a number that would serve as an index for a vector of Amble predefined with some operations.

register is innocuous in C++. You probably shouldn’t use inline. I think there are other unnecessary complications in this code.

  • I know they’re outside the code. It seemed that not, the way I posted the question, but anyway... I know it’s not a simple thing to be done, but I have the idea of a personal project and I wanted to take this forward, maybe use something ready to help me. I didn’t know Register was in disuse in this new version of c++, thanks for warning. This lot of const is because this is a mathematical program, and if it is necessary to prove the operation I am safer. The references are to increase the performance of the code a little. And why you should not use inline?

  • 1

    In every specified version of C++. In C++17 its use is prohibited (until you find a new function for the word) http://en.cppreference.com/w/cpp/keyword/register. The compiler usually better determines whether or not to do it inline of the code (and it does if you want). Almost always the programmer misses or hits by coincidence. I think you like to do premature optimization, in addition to doing unnecessary things that do not help anything, can end up doing things that will bring otherwise. Want an optimization? End the use of lambda.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.