Created
December 2, 2017 21:24
-
-
Save vinipsmaker/08349a74566df4c4a9bf82624c13a33b to your computer and use it in GitHub Desktop.
// Taken from https://github.com/kobalicek/mathpresso
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mathpresso/mathpresso.h> | |
#include <stdio.h> | |
int main(int argc, char* argv[]) { | |
mathpresso::Context ctx; | |
mathpresso::Expression exp; | |
// Initialize the context by adding MathPresso built-ins. Without this line | |
// functions like round(), sin(), etc won't be available. | |
ctx.addBuiltIns(); | |
// Let the context know the name of the variables we will refer to and | |
// their positions in the data pointer. We will use an array of 3 doubles, | |
// so index them by using `sizeof(double)`, like a normal C array. | |
// | |
// The `addVariable()` also contains a third parameter that describes | |
// variable flags, use `kVariableRO` to make a certain variable read-only. | |
ctx.addVariable("x", 0 * sizeof(double)); | |
ctx.addVariable("y", 1 * sizeof(double)); | |
ctx.addVariable("z", 2 * sizeof(double)); | |
// Compile the expression. | |
// | |
// The create parameters are: | |
// 1. `mathpresso::Context&` - The expression's context / environment. | |
// 2. `const char* body` - The expression body. | |
// 3. `unsigned int` - Options, just pass `mathpresso::kNoOptions`. | |
mathpresso::Error err = exp.compile(ctx, "(x*y) % z", mathpresso::kNoOptions); | |
// Handle possible syntax or compilation error. | |
if (err != mathpresso::kErrorOk) { | |
printf("Expression Error: %u\n", err); | |
return 1; | |
} | |
// To evaluate the expression you need to create the `data` to be passed | |
// to the expression and initialize it. Every expression returns `double`, | |
// to return more members simply use the passed `data`. | |
double data[] = { | |
1.2, // 'x' - available at data[0] | |
3.8, // 'y' - available at data[1] | |
1.3 // 'z' - available at data[2] | |
}; | |
printf("Output: %f\n", exp.evaluate(data)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment