]> www.ginac.de Git - ginac.git/blob - doc/examples/compile2.cpp
[BUGFIX] Fix crash in parser.
[ginac.git] / doc / examples / compile2.cpp
1 #include <iostream>
2 #include <ginac/ginac.h>
3 // Yes, we are using CUBA (should be installed on the system!)
4 #include <cuba.h>
5
6 using namespace std;
7 using namespace GiNaC;
8
9 /*
10  * Demonstrates the use of compile_ex with the CUBA library.
11  *
12  * The user can enter an expression on the command line. This expression is
13  * compiled via compile_ex and integrated over the region 0 <= x,y <= 1 with
14  * the help of the CUBA library (http://www.feynarts.de/cuba).
15  *
16  */
17
18 int main()
19 {
20         // Let the user enter a expression
21         symbol x("x"), y("y");
22         string s;
23         cout << "Enter an expression containing 'x' and/or 'y': ";
24         cin >> s;
25         // Expression now in expr
26         ex expr(s, lst{x,y});
27  
28         cout << "start integration of " << expr << " ..." << endl;
29
30         // Some definitions for VEGAS 
31         #define NDIM 2
32         #define NCOMP 1
33         #define EPSREL 1e-3
34         #define EPSABS 1e-12
35         #define VERBOSE 0
36         #define MINEVAL 0
37         #define MAXEVAL 50000
38         #define NSTART 1000
39         #define NINCREASE 500
40
41         // Some variables for VEGAS
42         int comp, nregions, neval, fail;
43         double integral[NCOMP], error[NCOMP], prob[NCOMP];
44
45         // Our function pointer that points to the compiled ex
46         FUNCP_CUBA fp;
47
48         // Optionally, compile with custom compiler flags:
49         // setenv("CXXFLAGS", "-O3 -fomit-frame-pointer -ffast-math", 1);
50         compile_ex(lst{expr}, lst{x,y}, fp);
51
52         // Starting VEGAS
53         // By invocation of compile() the expression in expr is converted into the
54         // appropriate function pointer
55         Vegas(NDIM, NCOMP, fp,
56               EPSREL, EPSABS, VERBOSE, MINEVAL, MAXEVAL, NSTART, NINCREASE,
57               &neval, &fail, integral, error, prob);
58
59         // Show the result
60         cout << "result: " << integral[0] << endl;
61
62         return 0;
63 }