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 (2659B)
---
1 #include <iostream>
2 #include <armadillo>
3 #include <functional>
4 #include "header.h"
5 #include "functions.h"
6 using namespace arma;
7 using namespace std;
8
9 vec newton(function<vec(vec)> f, vec x_0, vec dx, Floattype eps);
10 vec newtonJac(function<vec(vec)> f, vec x_0, vec dx, Floattype eps,
11 mat (*J)(vec));
12
13 int main(int argc, char* argv[])
14 {
15 // Namespace declarations
16 using std::cout;
17
18 // Calculate machine precision
19 Floattype eps = 1.0f;
20 while (1.0f + eps != 1.0f)
21 eps /= 2.0f;
22
23 cout << "\nFinding the solution to the two-equation linear system:\n";
24 vec x1(2); x1[0] = 2.0f; x1[1] = 2.1f;
25 vec dx1(2); dx1[0] = 1e-6f; dx1[1] = 1e-6f;
26 ncalls = 0;
27 vec root1 = newton(sys_2_eqs, x1, dx1, eps*10.f);
28 root1.print("Solution x:");
29 sys_2_eqs(root1).print("f(x):");
30 cout << "It took " << ncalls << " function calls\n";
31
32 cout << "\nFinding the minumum of the Rosenbrock's valley function:\n";
33 vec x2(2); x2[0] = 5.0f; x2[1] = 6.0f;
34 vec dx2(2); dx2[0] = 1e-6f; dx2[1] = 1e-6f;
35 ncalls = 0;
36 vec root2 = newton(rosenbrockGrad, x2, dx2, eps*10.f);
37 root2.print("Solution x:");
38 rosenbrock(root2).print("Rosenbrock at x:");
39 cout << "It took " << ncalls << " function calls\n";
40
41 cout << "\nFinding the minumum of the Rosenbrock's valley function, Jacobian matrix predefined:\n";
42 vec x2J(2); x2J[0] = 5.0f; x2J[1] = 6.0f;
43 vec dx2J(2); dx2J[0] = 1e-6f; dx2J[1] = 1e-6f;
44 ncalls = 0;
45 vec root2J = newtonJac(rosenbrockGrad, x2J, dx2J, eps*10.f, rosenbrockJacobian);
46 root2J.print("Solution x, Jacobian:");
47 rosenbrock(root2J).print("Rosenbrock at x, Jacobian:");
48 cout << "It took " << ncalls << " function calls\n";
49
50 cout << "\nFinding the minumum of the Himmelblau's function:\n";
51 vec x3(2); x3[0] = 5.0f; x3[1] = 6.0f;
52 vec dx3(2); dx3[0] = 1e-6f; dx3[1] = 1e-6f;
53 ncalls = 0;
54 vec root3 = newton(himmelblauGrad, x3, dx3, eps*10.f);
55 root3.print("Solution x:");
56 himmelblau(root3).print("Himmelblau at x:");
57 cout << "It took " << ncalls << " function calls\n";
58
59 cout << "\nFinding the minumum of the Himmelblau's function, Jacobian matrix predefined:\n";
60 vec x3J(2); x3J[0] = 5.0f; x3J[1] = 6.0f;
61 vec dx3J(2); dx3J[0] = 1e-6f; dx3J[1] = 1e-6f;
62 ncalls = 0;
63 vec root3J = newtonJac(himmelblauGrad, x3, dx3, eps*10.f, himmelblauJacobian);
64 root3J.print("Solution x:");
65 himmelblau(root3J).print("Himmelblau at x, Jacobian:");
66 cout << "It took " << ncalls << " function calls\n";
67
68 // Return successfully
69 return 0;
70 }
71
72 void check(const bool statement)
73 {
74 using std::cout;
75 if (statement == true)
76 cout << "\t\033[0;32mPassed\033[0m\n";
77 else
78 cout << "\t\033[1;31mFail!!\033[0m\n";
79 }