1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43
44 /*! \file test_01.cpp
45 \brief Test of StdLinearOperator, its inverse and transpose
46
47 \f$ A=\begin{pmatrix} 4 & 1 \\ 2 & 3 \end{pmatrix},\quad
48 A^{-1}=\frac{1}{10}\begin{pmatrix} 4 & -1 \\ -2 & 3 \end{pmatrix} \f$
49
50 1) Compute \f$b\f$ in \f$Ax = b\f$, when \f$ x=\begin{pmatrix} 1 \\ -1 \end{pmatrix}\f$
51
52 2) Solve for \f$x\f$ in the above when \f$b=\begin{pmatrix} 3 \\ -1 \end{pmatrix}\f$
53
54 3) Compute \f$c\f$ in \f$A^\top y=c\f$ when \f$y=\begin{pmatrix} -2 \\ 1 \end{pmatrix}\f$
55
56 4) Solve for \f$y\f$ in the above when \f$c=\begin{pmatrix} -6 \\ 1 \end{pmatrix}\f$
57
58 Also ensure that the interface works with both ROL::Vector and std::vector arguments
59 */
60
61 #include "ROL_StdLinearOperator.hpp"
62 #include "ROL_Stream.hpp"
63 #include "Teuchos_GlobalMPISession.hpp"
64
65 typedef double RealT;
66
main(int argc,char * argv[])67 int main(int argc, char *argv[]) {
68
69
70
71 typedef std::vector<RealT> vector;
72
73 typedef ROL::StdVector<RealT> SV;
74
75 typedef ROL::StdLinearOperator<RealT> StdLinearOperator;
76
77
78 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
79
80 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
81 int iprint = argc - 1;
82 ROL::Ptr<std::ostream> outStream;
83 ROL::nullstream bhs; // outputs nothing
84 if (iprint > 0)
85 outStream = ROL::makePtrFromRef(std::cout);
86 else
87 outStream = ROL::makePtrFromRef(bhs);
88
89 // Save the format state of the original std::cout.
90 ROL::nullstream oldFormatState;
91 oldFormatState.copyfmt(std::cout);
92
93 int errorFlag = 0;
94
95 // *** Test body.
96
97 try {
98
99 ROL::Ptr<vector> a_ptr = ROL::makePtr<vector>(
100 std::initializer_list<RealT>{4.0,2.0,1.0,3.0});
101 ROL::Ptr<vector> ai_ptr = ROL::makePtr<vector>(
102 std::initializer_list<RealT>{3.0/10.0, -2.0/10.0, -1.0/10.0, 4.0/10.0});
103
104 ROL::Ptr<vector> x1_ptr = ROL::makePtr<vector>(
105 std::initializer_list<RealT>{1.0,-1.0});
106 ROL::Ptr<vector> b1_ptr = ROL::makePtr<vector>(2);
107
108 ROL::Ptr<vector> x2_ptr = ROL::makePtr<vector>(2);
109 ROL::Ptr<vector> b2_ptr = ROL::makePtr<vector>(
110 std::initializer_list<RealT>{3.0,-1.0});
111
112 ROL::Ptr<vector> y3_ptr = ROL::makePtr<vector>(
113 std::initializer_list<RealT>{-2.0,1.0});
114 ROL::Ptr<vector> c3_ptr = ROL::makePtr<vector>(2);
115
116 ROL::Ptr<vector> y4_ptr = ROL::makePtr<vector>(2);
117 ROL::Ptr<vector> c4_ptr = ROL::makePtr<vector>(
118 std::initializer_list<RealT>{-6.0,1.0});
119
120 StdLinearOperator A(a_ptr);
121 StdLinearOperator Ai(ai_ptr);
122
123 SV x1(x1_ptr); SV x2(x2_ptr); SV y3(y3_ptr); SV y4(y4_ptr);
124 SV b1(b1_ptr); SV b2(b2_ptr); SV c3(c3_ptr); SV c4(c4_ptr);
125
126 RealT tol = ROL::ROL_EPSILON<RealT>();
127
128 // Test 1
129 *outStream << "\nTest 1: Matrix multiplication" << std::endl;
130 A.apply(b1,x1,tol);
131 *outStream << "x = [" << (*x1_ptr)[0] << "," << (*x1_ptr)[1] << "]" << std::endl;
132 *outStream << "b = [" << (*b1_ptr)[0] << "," << (*b1_ptr)[1] << "]" << std::endl;
133 b1.axpy(-1.0,b2);
134
135 RealT error1 = b1.norm();
136 errorFlag += error1 > tol;
137 *outStream << "Error = " << error1 << std::endl;
138
139 // Test 2
140 *outStream << "\nTest 2: Linear solve" << std::endl;
141 A.applyInverse(*x2_ptr,*b2_ptr,tol);
142 *outStream << "x = [" << (*x2_ptr)[0] << "," << (*x2_ptr)[1] << "]" << std::endl;
143 *outStream << "b = [" << (*b2_ptr)[0] << "," << (*b2_ptr)[1] << "]" << std::endl;
144 x2.axpy(-1.0,x1);
145
146 RealT error2 = x2.norm();
147 errorFlag += error2 > tol;
148 *outStream << "Error = " << error2 << std::endl;
149
150 // Test 3
151 *outStream << "\nTest 3: Transposed matrix multiplication" << std::endl;
152 A.applyAdjoint(*c3_ptr,*y3_ptr,tol);
153 *outStream << "y = [" << (*y3_ptr)[0] << "," << (*y3_ptr)[1] << "]" << std::endl;
154 *outStream << "c = [" << (*c3_ptr)[0] << "," << (*c3_ptr)[1] << "]" << std::endl;
155 c3.axpy(-1.0,c4);
156
157 RealT error3 = c3.norm();
158 errorFlag += error3 > tol;
159 *outStream << "Error = " << error3 << std::endl;
160
161 // Test 4
162 *outStream << "\nTest 4: Linear solve with transpose" << std::endl;
163 A.applyAdjointInverse(y4,c4,tol);
164 *outStream << "y = [" << (*y4_ptr)[0] << "," << (*y4_ptr)[1] << "]" << std::endl;
165 *outStream << "c = [" << (*c4_ptr)[0] << "," << (*c4_ptr)[1] << "]" << std::endl;
166 y4.axpy(-1.0,y3);
167
168 RealT error4 = y4.norm();
169 errorFlag += error4 > tol;
170 *outStream << "Error = " << error4 << std::endl;
171
172 *outStream << "x1 = "; x1.print(*outStream);
173 Ai.applyInverse(b1,x1,tol);
174 *outStream << "b1 = "; b1.print(*outStream);
175 A.apply(b1,x1,tol);
176 *outStream << "b1 = "; b1.print(*outStream);
177 A.applyInverse(x1,b1,tol);
178 *outStream << "x1 = "; x1.print(*outStream);
179 Ai.apply(x1,b1,tol);
180 *outStream << "x1 = "; x1.print(*outStream);
181
182
183 }
184 catch (std::logic_error& err) {
185 *outStream << err.what() << "\n";
186 errorFlag = -1000;
187 }; // end try
188
189 if (errorFlag != 0)
190 std::cout << "End Result: TEST FAILED\n";
191 else
192 std::cout << "End Result: TEST PASSED\n";
193
194 // reset format state of std::cout
195 std::cout.copyfmt(oldFormatState);
196
197 return 0;
198
199 }
200
201