Overview | Getting Started | Tutorials | Examples and Showcases
A simple parser demo
If you want to evaluate a simple math expression like sin(pi/6)+3, first you need to create the Expression that encapsules the string value and then you can use Analyzer to get the result:
#include <iostream>
#include <analitza/analyzer.h>
#include <analitza/value.h>
using namespace std;
using namespace Analitza;
int main(int argc, char *argv[]) {
QString input("sin(pi/6)+3");
Expression exp(input);
Analyzer a;
a.setExpression(exp);
double result = a.calculate().toReal().value();
cout << result << endl;
return 0;
}
You can also use Expression to make some queries, for example, in regards to the data type of the expression, the next code prints 0 (false):
bool eq = exp.isEquation();
cout << eq << endl;
Some advanced parsing features
Lets create a variable k = 2.1 and a function f(x) = sin(x) + k. We will calculate values for a lambda expression (our f(x) function) and get the derivative of f(x) (wich is a lambda expression too: d(f(x))=f'(x)):
#include <iostream>
#include <analitza/analyzer.h>
#include <analitza/variables.h>
#include <analitza/value.h>
using namespace std;
using namespace Analitza;
int main(int argc, char *argv[]) {
Variables *vars = new Variables();
vars->modify("k", 2.1);
Expression func("x->sin(x) + k");
Cn *x = new Cn();
x->setValue(3.14);
Analyzer a(vars);
QStack<Object*> runStack;
runStack.push(x);
Expression result;
a.setExpression(func);
a.setStack(runStack);
result = a.calculateLambda();
cout << func.toString().toStdString() << endl;
cout << result.isReal() << endl;
cout << result.toReal().value() << endl;
Expression dfunc = a.derivative("x");
a.setExpression(dfunc);
a.setStack(runStack);
result = a.calculateLambda();
cout << dfunc.toString().toStdString() << endl;
cout << result.toReal().value() << endl;
delete vars;
return 0;
}