ROL
vector/test_10.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include "Teuchos_GlobalMPISession.hpp"
11
12#include "ROL_Objective.hpp"
13
14#include "ROL_StdVector.hpp"
16#include "ROL_RandomVector.hpp"
18#include "ROL_Stream.hpp"
19
20#include <iomanip>
21
22using RealT = double;
23using size_type = typename std::vector<RealT>::size_type;
24
25namespace details {
26
27using namespace ROL;
28
29// An alternative, more expensive way to compute the dot product!
30template<typename Real>
32private:
33
35
36public:
37
38 Real dot( const Vector<Real>& x, const Vector<Real>& y ) {
39
40 auto w = workspace_.copy(x);
41 auto z = workspace_.copy(x);
42 w->plus(y);
43 z->axpy(-1.0,y);
44 auto result = 0.25*( w->dot(*w) - z->dot(*z) );
45 return result;
46 } // w and z should go out of scope - decrement ref counts
47
48 void status( std::ostream& os ) const { workspace_.status(os); }
49};
50
51
52
53
54} // namespace details
55
57
58
59int main( int argc, char* argv[] ) {
60
61 using namespace ROL;
62
63 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
64
65 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
66 auto outStream = makeStreamPtr( std::cout, argc > 1 );
67
68 int errorFlag = 0;
69 RealT errtol = std::sqrt(ROL_EPSILON<RealT>());
70
71 try {
72
74
75 size_type N = 20;
76
77 auto xp = makePtr<std::vector<RealT>>(N);
78 auto x = makePtr<StdVector<RealT>>(xp);
79 auto y = x->clone();
80
81 auto xx = PartitionedVector<RealT>::create({x,x});
82 auto yy = PartitionedVector<RealT>::create({y,y});
83
84 RandomizeVector( *x );
85 RandomizeVector( *y );
86
87 auto result = 0.5*polar.dot(*x,*y);
88 result += 0.5*polar.dot(*y,*x);
89
90 auto x_dot_y = x->dot(*y);
91 errorFlag += ( std::abs( x_dot_y - result ) > errtol );
92
93 *outStream << std::setprecision(16) << x_dot_y << std::endl;
94 *outStream << std::setprecision(16) << result << std::endl;
95
96
97 polar.dot(*xx,*yy);
98
99 polar.status(*outStream);
100
101 }
102 catch (std::logic_error& err) {
103 *outStream << err.what() << "\n";
104 errorFlag = -1000;
105 }; // end try
106
107 if (errorFlag != 0)
108 std::cout << "End Result: TEST FAILED\n";
109 else
110 std::cout << "End Result: TEST PASSED\n";
111
112 return 0;
113}
114
typename PV< Real >::size_type size_type
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Defines the linear algebra of vector space on a generic partitioned vector.
Defines the linear algebra or vector space interface.
VectorWorkspace< Real > workspace_
void status(std::ostream &os) const
Real dot(const Vector< Real > &x, const Vector< Real > &y)
int main(int argc, char *argv[])