• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

Analitza

Tutorials

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); // create a math expression
Analyzer a; // setup the Analyzer instance with its own Variables module
a.setExpression(exp); // Analyzer needs to know about expression
double result = a.calculate().toReal().value(); // get the numeric result
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(); // or a.expression().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(); // create a variables container
vars->modify("k", 2.1); // add a new variable called k with initial value 2.1
Expression func("x->sin(x) + k"); // create our lambda f(x)=sin(x) + k
Cn *x = new Cn(); // create a bound variable to manipulate x in f(x)
x->setValue(3.14); // set a initial value for x
Analyzer a(vars); // when calculating, Analyzer will access to variables in vars
QStack<Object*> runStack; // Analyzer use this stack to calculate f(x)
runStack.push(x); // add our bound variable to the stack
Expression result; // an expression instance to save computations and result
// play with f(x)
a.setExpression(func);
a.setStack(runStack); // this is important to calculate lambda expressions
result = a.calculateLambda();
cout << func.toString().toStdString() << endl;
cout << result.isReal() << endl; // simple query
cout << result.toReal().value() << endl; // get f(3.14)
// get the derivative of f(x) and play with it
Expression dfunc = a.derivative("x");
a.setExpression(dfunc); // update the expression
a.setStack(runStack); // this is important, update the stack too
result = a.calculateLambda();
cout << dfunc.toString().toStdString() << endl; // get the derivative string
cout << result.toReal().value() << endl; // derivative of f(x) at x = 3.14
delete vars; // free the memory
return 0;
}
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Analitza

Skip menu "Analitza"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal