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