Repository navigation
Expand file tree
/
Copy pathexample_01.cpp
More file actions
112 lines (78 loc) · 3.05 KB
/
Copy pathexample_01.cpp
File metadata and controls
112 lines (78 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// @HEADER
// *****************************************************************************
// Rapid Optimization Library (ROL) Package
//
// Copyright 2014 NTESS and the ROL contributors.
// SPDX-License-Identifier: BSD-3-Clause
// *****************************************************************************
// @HEADER
/*! \file example_01.cpp
\brief Example demonstrating optimzation over a complex field using
the Rayleigh quotient for a Hermitian matrix as an objective
Random (complex-valued) initial guesses for \f$z\$f are
expected to converge to the eigenvector corresponding to
the minimum eigenvalue.
*/
#include <cmath>
#include "ROL_GlobalMPISession.hpp"
#include "ROL_OptimizationSolver.hpp"
#include "ROL_ComplexStdVector.hpp"
#include "HermitianMatrix.hpp"
#include "RayleighQuotient.hpp"
#include "OrthogonalProjector.hpp"
int main(int argc, char *argv[]) {
using namespace ROL;
using RealT = double;
ROL::GlobalMPISession mpiSession(&argc, &argv);
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
int iprint = argc - 1;
Ptr<std::ostream> os_ptr;
nullstream bhs; // outputs nothing
if (iprint > 0)
os_ptr = ROL::makePtrFromRef(std::cout);
else
os_ptr = ROL::makePtrFromRef(bhs);
auto& os = *os_ptr;
int errorFlag = 0;
// *** Example body.
try {
auto A = HermitianMatrix<RealT>::example_Matrix();
auto d = HermitianMatrix<RealT>::example_eigenvalues();
auto Z = HermitianMatrix<RealT>::example_eigenvectors();
auto N = A.size();
os << "Test Matrix A:" << std::endl;
os << A;
os << "The eigendecomposition AZ=ZD has the eigenvalues " << std::endl;
os << "D = diag([" << d[0] << "," << d[1] << ","
<< d[2] << "," << d[3] << "])" << std::endl;
ParameterList parlist;
auto& steplist = parlist.sublist("Step");
steplist.set("Type", "Trust Region");
auto& trlist = steplist.sublist("Trust Region");
trlist.set("Subproblem Solver", "Truncated CG");
auto z = makePtr<ComplexStdVector<RealT>>(N);
auto obj = makePtr<RayleighQuotient<RealT>>(A);
z->randomize();
auto problem = OptimizationProblem<RealT>(obj,z);
auto solver = OptimizationSolver<RealT>(problem,parlist);
solver.solve(os);
os << "Optimal z:" << std::endl;
z->set(*(problem.getSolutionVector()));
z->print(os);
auto tol = std::sqrt(ROL_EPSILON<RealT>());
auto value_opt = obj->value(*z,tol);
auto value_error = std::abs(d[0]-value_opt);
errorFlag += value_error > tol;
os << "rho(z_opt): " << std::setprecision(12) << value_opt << std::endl;
os << "Minimum eigenvalue error: " << std::setprecision(12) << value_error << std::endl;
}
catch (std::logic_error& err) {
os << err.what() << "\n";
errorFlag = -1000;
}; // end try
if (errorFlag != 0)
std::cout << "End Result: TEST FAILED\n";
else
std::cout << "End Result: TEST PASSED\n";
return 0;
}