Next: , Previous: Introduction, Up: Top


2 Tutorial for gTybalt

2.1 Starting gTybalt

gTybalt can be run in the TeXmacs mode or in a simple text mode. To start gTybalt in text mode, type gtybalt. To quit, type quit. To use gTybalt within TeXmacs, first start TeXmacs with the command texmacs. You can then start a gTybalt session by clicking on the terminal symbol and selecting “gTybalt” from the pop-up menu. Alternatively you can start gTybalt from the “Text” menu via “Text -> session -> gTybalt”.

2.2 Command line input

You can type in regular C++ statements which will be processed by CINT. For example

     gTybalt> int i=1;
     gTybalt> i++;
     gTybalt> cout << "The increased number : " << i << endl;
     The increased number : 2
     

The functionality of gTybalt for symbolic and algebraic calculations is provided by the GiNaC-library. The syntax follows the one for the GiNaC-library. For example:

     gTybalt> symbol a("a"), b("b");
     gTybalt> ex e1=pow(a+b,2);
     gTybalt> print(e1);
     
          2
     (b+a)
     
     gTybalt> ex e2=expand(e1);
     gTybalt> print(e2);
     
      2    2
     a  + b  + 2 a b
     

Here print is a gTybalt-subroutine, which prints a variable to the screen. By default, gTybalt does not print anything onto the screen, unless the user specifically asks for a variable to be printed. If gTybalt is running under TeXmacs, the output will be with TeX fonts. There is also a function rawprint which prints the variable e2 as follows:

     gTybalt> rawprint(e2);
     a^2+b^2+2*a*b

The picture shows how the output of a further example will look like under TeXmacs.

screenshot1.png
A screenshot for gTybalt when running in TeXmacs mode.

The following figure shows the corresponding output, when gTybalt runs in text mode.

screenshot2.png
A screenshot for gTybalt when running in text mode.

Within TeXmacs mode there is the possibility to print a session to a postscript file by choosing from the “File” menu the combination “File -> Export -> Postscript”. It is also possible to generate for a session a corresponding LaTeX file via “File -> Export -> LaTeX”. This is in particular useful if one would like to obtain for a displayed formula the corresponding LaTeX code.

2.3 Scripts

The standard behaviour of the C++/C interpreter CINT is to interpret any command immediately. There is also the possibility to put a few commands into a script and to load this file into a session. This is done through the following commandds:

     .L file.C
     .x file.C

The .L command loads a script into the session, but does not execute the script. This is useful for a script containing the definition of a function. The .x command loads and executes a script. As an example consider that the file hermite.C contains the following code:

     ex HermitePoly(const symbol & x, int n)
     {
      ex HKer=exp(-pow(x,2));
      return normal(pow(numeric(-1),n) * diff(HKer,x,n)/HKer);
     }

This is just a function which calculates the n-th Hermite polynomial. Now try the following lines in gTybalt:

     gTybalt> .L hermite.C
     gTybalt> symbol z("z");
     gTybalt> ex e1=HermitePoly(z,3);
     gTybalt> print(e1);
     
                  3
      - 12 z + 8 z
     

This prints out the third Hermite polynomial. As a further example let the file main.C contain the following code:

     {
      symbol z("z");
      for (int i=0;i<5;i++)
       {
        print( HermitePoly(z,i));
       }
     }

This is called an “un-named script”. Un-named scripts have to start with an opening “{” and end with a closing “}”. Then the following lines in gTybalt

     gTybalt> .L hermite.C
     gTybalt> .x main.C

will print the first five Hermite polynomials.

gTybalt offers also the possibility to execute automatically a script at start time. If there is a logon-file gtybalt_logon in the directory from where gTybalt is started, the commands in this file will be executed when gTybalt is started. The default commands are

     {
      gROOT->ProcessLine("#include <iostream>");
      gROOT->ProcessLine("#include <string>");
      gSystem->Load("libgtybaltdict.so");
      gROOT->ProcessLine("using namespace GiNaC");
      gROOT->ProcessLine("greeting();");
     }

and you are advised to copy these lines into your personal logon-file. A logon-file can be used to include very often used standard definitions. An example would be the following line:

      symbol x("x"), y("y"), z("z");

2.4 Plots

A function can be plotted as follows:

     gTybalt> symbol x("x");
     gTybalt> ex f1=sin(x);
     gTybalt> plot(f1,x,0,20);

This will plot sin(x) in the intervall from 0 to 20. The plot will look like this:

screenshot3.png

To clear the window with the plot, choose from the menubar of the plot “File -> Quit ROOT”.

Similar, a scalar function of two variables can be plotted as follows:

     gTybalt> symbol x("x"), y("y");
     gTybalt> ex f2=sin(x)*sin(y);
     gTybalt> plot(f2,x,y,0,10,0,20);

This will plot sin(x) sin(y) for x from 0 to 10 and y from 0 to 20. The plot will look like this:

screenshot4.png

To view the plot from a different angle, just grap the plot with the mouse and move it around. There is a wide variety of options on how to draw a graph. To access the draw panel, click on the right mouse button, when the mouse is placed inside the window containing the plot and choose “DrawPanel” from the pop-up menu. The options include among others lego- and contour-plots. The default option corresponds to the style “surf” and draws a (coloured) surface.

The plot can be saved to a file. For example, to save the plot as a postscript file, choose from the “File” menu the option “Save As canvas.ps”.

2.5 Numerical integration

Functions can be integrated numerically by Monte Carlo integration. For example to evaluate

       1     1     1
      /     /     /
      | dx  | dy  | dz  x y z
      /     /     /
      0     0     0


one types

     gTybalt> symbol x("x"), y("y"), z("z");
     gTybalt> ex f = x*y*z;
     gTybalt> ex g = intnum(f,lst(x,y,z),lst(0,0,0),lst(1,1,1));
     gTybalt> print(g);
     0.12500320720479463077

The result of the integration can also be accessed with the help of the global variable gTybalt_int_res. In addition the global variables gTybalt_int_err and gTybalt_int_chi2 give information on the error and the chi^2. For our example, one gets

     gTybalt> print(gTybalt_int_res);
     0.125003
     gTybalt> print(gTybalt_int_err);
     5.26234e-06
     gTybalt> print(gTybalt_int_chi2);
     0.385639

The Monte Carlo integration uses the adaptive algorithm VEGAS. gTybalt uses the implementation from the GNU Scientific Library. The algorithm first uses gTybalt_int_iter_low iterations with gTybalt_int_calls_low calls to obtain some rough information where the integrand is largest in magnitude. The results of this run are discarded, but the grid is kept. The algorithm then performs gTybalt_int_iter_high iterations with gTybalt_int_calls_high calls to obtain a Monte Carlo estimate. The default values are

     gTybalt> print(gTybalt_int_iter_low);
     5
     gTybalt> print(gTybalt_int_calls_low);
     10000
     gTybalt> print(gTybalt_int_iter_high);
     10
     gTybalt> print(gTybalt_int_calls_low);
     100000

The values of these variables can be adjusted by the user. For functions of one or two variables there are in addition the following simpler forms

     double intnum(ex expr, ex x, double xmin, double xmax);
     double intnum(ex expr, ex x, ex y, double xmin, double xmax,
                                        double ymin, double ymax);

2.6 Factorization

When gTybalt is compiled with the NTL library, gTybalt provides an interface to factorize univariate polynomials with integer coefficients. For example

     gTybalt> symbol x("x");
     gTybalt> ex f = expand( pow(x+2,13)*pow(x+3,5)*pow(x+5,7)*pow(x+7,2) );
     gTybalt> ex g = factorpoly(f,x);
     gTybalt> print(g);
     
             2        5        7        13
      (7 + x)  (3 + x)  (5 + x)  (2 + x)
     

If the first argument of the function factorpoly is not an univariate polynomial with integer coefficients, it returns unevaluated.

2.7 Expansion of transcendental functions

When gTybalt is compiled with the nestedsums library, gTybalt provides an interface to expand a certain class of transcendental functions in a small parameter. The class of functions comprises among others generalized hypergeometric functions, the first and second Appell function and the first Kampe de Feriet function. A hypergeometric function can be expanded as follows:

     gTybalt> symbol x("x"), eps("epsilon");
     gTybalt> transcendental_fct_type_A F21(x,lst(1,-eps),lst(1-eps),lst(1-eps),
              lst(1,-eps));
     gTybalt> ex f = F21.set_expansion(eps,5);
     gTybalt> rawprint(f);
     -Li(3,x)*epsilon^3-Li(2,x)*epsilon^2-Li(4,x)*epsilon^4-Li(1,x)*epsilon
     +Z(Infinity)

This expands the hypergeometric function 2F1(1,-eps;1-eps;x) in eps up to order 5 and agrees with the known expansion
2F1(1,-eps;1-eps;x) = 1 - eps Li1(x) - eps^2 Li2(x) - eps^3 Li3(x) - eps^4 Li4(x) + O(eps^5).
The algorithm for the expansion is based on an algebra for nested sums. Z(Infinity) represent the unit element in this algebra and is equal to 1.