]> www.ginac.de Git - ginac.git/commitdiff
New examples for compile_ex and link_ex.
authorJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Wed, 30 Aug 2006 20:08:21 +0000 (20:08 +0000)
committerJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Wed, 30 Aug 2006 20:08:21 +0000 (20:08 +0000)
doc/examples/compile1.cpp
doc/examples/compile2.cpp [new file with mode: 0644]
doc/examples/compile3.cpp [new file with mode: 0644]

index e2fa8e022f4b261a25bf3f49a76622bd1942b025..cc81229e84c2ac721613696552253337f85b0c31 100644 (file)
@@ -1,45 +1,69 @@
+#include <ctime>
 #include <iostream>
 using namespace std;
 #include <ginac/ginac.h>
 using namespace GiNaC;
 #include <iostream>
 using namespace std;
 #include <ginac/ginac.h>
 using namespace GiNaC;
-// Yes, we are using CUBA (should be installed on the system!)
-#include "cuba.h"
+
+/*
+ * Demonstrates the use of compile_ex.
+ *
+ * Compiles a small expression as C code via compile_ex and evaluates the
+ * expression numerically. The evalation speed is timed and compared to the
+ * evaluation of the original GiNaC expression.
+ *
+ */
 
 int main()
 {
 
 int main()
 {
-       // Let the user enter a expression
-       symbol x("x"), y("y");
-       string s;
-       cout << "Enter an expression containing 'x' and/or 'y': ";
-       cin >> s;
-       // Expression now in expr
-       ex expr(s, lst(x,y));
+       // Define some expression
+       symbol x("x");
+       ex expr = sin(x);
  
  
-       cout << "start integration of " << expr << " ..." << endl;
-
-       // Some definitions for VEGAS 
-       #define NDIM 2
-       #define NCOMP 1
-       #define EPSREL 1e-3
-       #define EPSABS 1e-12
-       #define VERBOSE 0
-       #define MINEVAL 0
-       #define MAXEVAL 50000
-       #define NSTART 1000
-       #define NINCREASE 500
-
-       // Some variables for VEGAS
-       int comp, nregions, neval, fail;
-       double integral[NCOMP], error[NCOMP], prob[NCOMP];
-
-       // Starting VEGAS
-       // By invocation of compile() the expression in expr is converted into the
-       // appropriate function pointer
-       Vegas(NDIM, NCOMP, compile(lst(expr), lst(x,y)), EPSREL, EPSABS, VERBOSE,
-             MINEVAL, MAXEVAL, NSTART, NINCREASE, &neval, &fail, integral, error, prob);
-
-       // Show the result
-       cout << "result: " << integral[0] << endl;
+       // Some variables for timing
+       time_t start, end;
+       double cpu_time_used;
+
+       // Our function pointer that points to the compiled ex
+       FUNCP_1P fp;
+       compile_ex(expr, x, fp);
+
+       // Do some (not necessarily meaningful ;-)) numerical stuff ...
+
+       cout << "Doing numerics with compile_ex ..." << endl;
+
+       // First using compile_ex
+       {
+               double result;
+               double point = 0.2;
+               start = clock();
+               for (int i=0; i<100000; ++i) {
+                       point += 0.001;
+                       result += fp(point);
+               }
+               end = clock();
+
+               // Show the result
+               cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
+               cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
+       }
+
+       cout << "Doing numerics without compile_ex ..." << endl;
+
+       // Then without compile_ex
+       {
+               ex result;
+               ex point = 0.2;
+               start = clock();
+               for (int i=0; i<100000; ++i) {
+                       point += 0.001;
+                       result += sin(point);
+               }
+               end = clock();
+
+               // Show the other result
+               cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
+               cout << "result = " << result << " in " << cpu_time_used << " seconds" << endl;
+       }
 
        return 0;
 }
 
        return 0;
 }
diff --git a/doc/examples/compile2.cpp b/doc/examples/compile2.cpp
new file mode 100644 (file)
index 0000000..1d344dd
--- /dev/null
@@ -0,0 +1,59 @@
+#include <iostream>
+using namespace std;
+#include <ginac/ginac.h>
+using namespace GiNaC;
+// Yes, we are using CUBA (should be installed on the system!)
+#include <cuba.h>
+
+/*
+ * Demonstrates the use of compile_ex with the CUBA library.
+ *
+ * The user can enter an expression on the command line. This expression is
+ * compiled via compile_ex and integrated over the region 0 <= x,y <= 1 with
+ * the help of the CUBA library (http://www.feynarts.de/cuba).
+ *
+ */
+
+int main()
+{
+       // Let the user enter a expression
+       symbol x("x"), y("y");
+       string s;
+       cout << "Enter an expression containing 'x' and/or 'y': ";
+       cin >> s;
+       // Expression now in expr
+       ex expr(s, lst(x,y));
+       cout << "start integration of " << expr << " ..." << endl;
+
+       // Some definitions for VEGAS 
+       #define NDIM 2
+       #define NCOMP 1
+       #define EPSREL 1e-3
+       #define EPSABS 1e-12
+       #define VERBOSE 0
+       #define MINEVAL 0
+       #define MAXEVAL 50000
+       #define NSTART 1000
+       #define NINCREASE 500
+
+       // Some variables for VEGAS
+       int comp, nregions, neval, fail;
+       double integral[NCOMP], error[NCOMP], prob[NCOMP];
+
+       // Our function pointer that points to the compiled ex
+       FUNCP_CUBA fp;
+       compile_ex(lst(expr), lst(x,y), fp);
+
+       // Starting VEGAS
+       // By invocation of compile() the expression in expr is converted into the
+       // appropriate function pointer
+       Vegas(NDIM, NCOMP, fp,
+             EPSREL, EPSABS, VERBOSE, MINEVAL, MAXEVAL, NSTART, NINCREASE,
+             &neval, &fail, integral, error, prob);
+
+       // Show the result
+       cout << "result: " << integral[0] << endl;
+
+       return 0;
+}
diff --git a/doc/examples/compile3.cpp b/doc/examples/compile3.cpp
new file mode 100644 (file)
index 0000000..11a5a17
--- /dev/null
@@ -0,0 +1,36 @@
+#include <ctime>
+#include <iostream>
+using namespace std;
+#include <ginac/ginac.h>
+using namespace GiNaC;
+
+/*
+ * Demonstrates the use of link_ex.
+ *
+ * When run for the first time link_ex will fail. This is a rude way of
+ * checking whether the needed .so file is available.  The .so is then created
+ * by compile_ex using the filename parameter. When run again link_ex will use
+ * the existing .so file.
+ *
+ */
+int main()
+{
+       FUNCP_2P fp;
+       try {
+               link_ex("compile3_testprg.so", fp);
+               cout << "Using existing 'compile3_testprg.so'." << endl;
+       }
+       catch (const std::exception& e) {
+               // hope the exception is just raised because of missing 'compile2_testprg.so' file,
+               // so being lazy no error management here ...
+               cout << "Error: " << e.what() << endl;
+               cout << "Building new 'compile3_testprg.so'." << endl;
+               symbol a, b;
+               ex expr = a*b;
+               compile_ex(expr, a, b, fp, "compile3_testprg");
+       }
+
+       cout << "result of 2.3*1.5 is " << fp(2.3, 1.5) << endl;
+
+       return 0;
+}