tmain.cpp - numeric - C++ library with numerical algorithms
(HTM) git clone git://src.adamsgaard.dk/numeric
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tmain.cpp (1622B)
---
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <cmath>
5 #include <cstdlib>
6 #include <ctime>
7 #include "header.h"
8 #include "functions.h"
9 #include "montecarlo.h"
10
11 int main(int argc, char* argv[])
12 {
13 // Namespace declarations
14 using std::cout;
15
16 // Number of sample points as input argument
17 Lengthtype N;
18 if (argc == 1) // If no args are given..
19 N = 100; // 100 points are sampled
20 else
21 N = atol(argv[1]); // Else the specified number
22
23 cout << "Sampling function at " << N << " points.\n";
24
25 // Calculate machine precision
26 Floattype eps_machine = 1.0f;
27 while (1.0f + eps_machine != 1.0f)
28 eps_machine /= 2.0f;
29
30 // Evaluate 3D function A at 10 points in interval [a;b]
31 Lengthtype d = 3; // No of dimensions
32 std::vector<Floattype> a(d); // Lower limits
33 std::vector<Floattype> b(d); // Upper limits
34 for (Lengthtype i=0; i<d; ++i) { // Assign equidimensional limits
35 a[i] = 0.0f;
36 b[i] = M_PI;
37 }
38 Floattype tic = clock();
39 MC mc(functionA, a, b, N);
40 mc.plain(); // Plain Monte-Carlo integration
41 mc.show(); // Display results
42 Floattype t_elapsed = (clock() - tic)/(CLOCKS_PER_SEC);
43 cout << "Elapsed time: " << t_elapsed << " s.\n";
44
45 // Append results to performance.dat
46 std::ofstream file;
47 file.open("performance.dat", std::fstream::app); // Append to file
48 file << N << '\t' << t_elapsed << '\t' << mc.error() << '\n';
49 file.close();
50
51 // Return successfully
52 return 0;
53 }
54
55 void check(const bool statement)
56 {
57 using std::cout;
58 if (statement == true)
59 cout << "\t\033[0;32mPassed\033[0m\n";
60 else
61 cout << "\t\033[1;31mFail!!\033[0m\n";
62 }