Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ functor library for evaluating mathematical/arithmetic expressions

Does anyone know of a library that allows you to do something like this?

std::transform(vecA.begin(), vecA.end(), 
               vecB.begin(), 
               vecOutput.begin(), 
               // run-time specified expression 
               magic_functor<float>("exp(a/(b+3))") 
);

Where magic_functor is the library-provided functor and a and b are iterated through vecA and vecB.

I could come up with something myself (and would have a lot of fun doing so), but it's probably better to avoid reinventing the wheel (also my boss would kill me). Have spent some time searching the web, but can't really find anything that fits the bill.

It needs to be flexible and fast, so a functor approach that only parses the string once (e.g. creates an execution stack internally on construction) would be ideal, but am open to other solutions.

like image 209
smocking Avatar asked Apr 11 '12 21:04

smocking


1 Answers

Several options (need some work and won't work “out of the box”):

  • Write your own (Boost Spirit is your friend) parser (example)
  • MuParserX
  • Mathpresso
  • other libraries
like image 73
Anonymous Avatar answered Oct 16 '22 06:10

Anonymous