Debian Codegraph @

packages/rol/src/oed/factors/ROL_OED_StdMomentOperator_Def.hpp

raw
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 #ifndef ROL_OED_STD_COVARIANCE_OPERATOR_DEF_HPP
11 #define ROL_OED_STD_COVARIANCE_OPERATOR_DEF_HPP
12
13 namespace ROL {
14 namespace OED {
15
16 template<typename Real>
17 void StdMomentOperator<Real>::initialize(int nfactors) {
18 startTimer("initialize");
19 M_.shape(nfactors,nfactors);
20 Minv_.shape(nfactors,nfactors);
21 U_.shape(nfactors,nfactors);
22 V_.shape(nfactors,nfactors);
23 sval_.resize(nfactors);
24 sval_.assign(nfactors,static_cast<Real>(0));
25 stopTimer("initialize");
26 }
27
28 template<typename Real>
29 void StdMomentOperator<Real>::build(const Vector<Real> &p) {
30 startTimer("build");
31 if (!isBuilt_) {
32 const Real zero(0), one(1);
33 const int nsamples = Xdata_[0].numRows();
34 const int nfactors = Xdata_[0].numCols();
35 M_.putScalar(zero);
36 LA::Matrix<Real> X(nsamples,nfactors);
37 LA::Matrix<Real> M0(nfactors,nfactors);
38 LA::Matrix<Real> Mtmp(nfactors,nfactors);
39 Real wt(0);
40 for (int k = 0; k < nobs_; ++k) {
41 X = Xdata_[k];
42 for (int i = 0; i < nsamples; ++i) {
43 wt = std::sqrt(get(p,i)*getNoise(i));
44 for (int j = 0; j < nfactors; ++j) X(i,j) *= wt;
45 }
46 M0.putScalar(zero);
47 blas_->SYRK(Teuchos::UPPER_TRI,Teuchos::TRANS,nfactors,nsamples,
48 one,X.values(),nsamples,zero,M0.values(),nfactors);
49 Mtmp += M0;
50 }
51 sumAll(p,Mtmp.values(),M_.values(),nfactors*nfactors);
52 if (isPset_) blas_->AXPY(nfactors*nfactors,one,P_.values(),1,M_.values(),1);
53 isBuilt_ = true;
54 isFactorized_ = false;
55 useSVD_ = false;
56 }
57 stopTimer("build");
58 }
59
60 template<typename Real>
61 void StdMomentOperator<Real>::factorize(const Vector<Real> &p) {
62 startTimer("factorize");
63 if (isBuilt_) {
64 if (!isFactorized_) {
65 const int nfactors = M_.numRows();
66 Minv_.assign(M_);
67 int info;
68 // Factorize matrix
69 lapack_->POTRF('U',nfactors,Minv_.values(),nfactors,&info);
70 if (info != 0) {
71 useSVD_ = true;
72 //std::stringstream ss;
73 //ss << ">>> OED::StdMomentOperator::factorize : "
74 // << "POTRF exited with INFO equal to " << info << "!" << std::endl
75 // << " < 0: if INFO = -i, the i-th argument had an illegal value" << std::endl
76 // << " > 0: if INFO = i, the leading minor of order i is not positive definite";
77 //throw Exception::NotImplemented(ss.str());
78 }
79 if (useSVD_) {
80 Minv_.assign(M_);
81 int lwork = -1;
82 std::vector<Real> work(1), rwork(5*nfactors);
83 lapack_->GESVD('A','A',nfactors,nfactors,Minv_.values(),nfactors,
84 &sval_[0],U_.values(),nfactors,V_.values(),nfactors,
85 &work[0],lwork,&rwork[0],&info);
86 if (info != 0) {
87 std::stringstream ss;
88 ss << ">>> OED::StdMomentOperator::factorize : "
89 << "GESVD exited with INFO equal to " << info << "!" << std::endl
90 << " < 0: if INFO = -i, the i-th argument had an illegal value" << std::endl
91 << " > 0: if DBDSQR did not converge, INFO specifies how many" << std::endl
92 << " superdiagonals of an intermediate bidiagonal form B" << std::endl
93 << " did not converge to zero";
94 throw Exception::NotImplemented(ss.str());
95 }
96 lwork = work[0];
97 work.resize(lwork); work.assign(lwork,static_cast<Real>(0));
98 lapack_->GESVD('A','A',nfactors,nfactors,Minv_.values(),nfactors,
99 &sval_[0],U_.values(),nfactors,V_.values(),nfactors,
100 &work[0],lwork,&rwork[0],&info);
101 if (info != 0) {
102 std::stringstream ss;
103 ss << ">>> OED::StdMomentOperator::factorize : "
104 << "GESVD exited with INFO equal to " << info << "!" << std::endl
105 << " < 0: if INFO = -i, the i-th argument had an illegal value" << std::endl
106 << " > 0: if DBDSQR did not converge, INFO specifies how many" << std::endl
107 << " superdiagonals of an intermediate bidiagonal form B" << std::endl
108 << " did not converge to zero";
109 throw Exception::NotImplemented(ss.str());
110 }
111 }
112 isFactorized_ = true;
113 }
114 }
115 else {
116 std::stringstream ss;
117 ss << ">>> OED::StdMomentOperator::factorize : "
118 << "Matrix has not been built!";
119 throw Exception::NotImplemented(ss.str());
120 }
121 stopTimer("factorize");
122 }
123
124 /***************************************************************************/
125 /* Begin Accessor Functions */
126 /***************************************************************************/
127 template<typename Real>
128 std::vector<Real>& StdMomentOperator<Real>::getData(Vector<Real> &x) const {
129 return *dynamic_cast<StdVector<Real>&>(x).getVector();
130 }
131
132 template<typename Real>
133 const std::vector<Real>& StdMomentOperator<Real>::getConstData(const Vector<Real> &x) const {
134 return *dynamic_cast<const StdVector<Real>&>(x).getVector();
135 }
136 /***************************************************************************/
137 /* End Accessor Functions */
138 /***************************************************************************/
139
140 template<typename Real>
141 StdMomentOperator<Real>::StdMomentOperator(RegressionType regType,
142 bool homNoise,
143 const Ptr<Noise<Real>> &noise)
144 : MomentOperator<Real>(regType,homNoise,noise),
145 lapack_(makePtr<LAPACK<int,Real>>()),
146 blas_(makePtr<Teuchos::BLAS<int,Real>>()),
147 isBuilt_(false), isFactorized_(false), isSet_(false),
148 isFullSet_(false), useSVD_(false), isPset_(false) {
149 ProfiledClass<Real,std::string>::rename("OED::StdMomentOperator");
150 }
151
152 template<typename Real>
153 Ptr<MomentOperator<Real>> StdMomentOperator<Real>::clone() const {
154 RegressionType type;
155 bool hom;
156 Ptr<Noise<Real>> noise;
157 MomentOperator<Real>::getRegressionInfo(type,hom,noise);
158 return makePtr<StdMomentOperator<Real>>(type,hom,noise);
159 }
160
161 template<typename Real>
162 void StdMomentOperator<Real>::update(const Vector<Real> &p, UpdateType type, int iter) {
163 if ( type == UpdateType::Initial
164 || type == UpdateType::Trial
165 || type == UpdateType::Temp ) {
166 isBuilt_ = false;
167 }
168 }
169
170 template<typename Real>
171 void StdMomentOperator<Real>::applyInverse(Vector<Real> &Mx,
172 const Vector<Real> &x,
173 const Vector<Real> &p) {
174 startTimer("applyInverse");
175 if (isSet_) {
176 build(p);
177 factorize(p);
178
179 Mx.set(x);
180 std::vector<Real> &Mxdata = getData(Mx);
181 const std::vector<Real> & xdata = getConstData(x);
182
183 const int nfactors = M_.numRows();
184 int info;
185
186 if (!useSVD_) {
187 // Solve triangular systems
188 lapack_->POTRS('U',nfactors,1,Minv_.values(),nfactors,
189 &Mxdata[0],nfactors,&info);
190 if (info != 0) {
191 std::stringstream ss;
192 ss << ">>> OED::StdMomentOperator::applyInverse : "
193 << "POTRS exited with INFO equal to " << info << "!" << std::endl
194 << " < 0: if INFO = -i, the i-th argument had an illegal value";
195 throw Exception::NotImplemented(ss.str());
196 }
197 // Refine solution
198 std::vector<Real> ferr(1), berr(1), work(3*nfactors);
199 std::vector<int> lwork(nfactors);
200 lapack_->PORFS('U',nfactors,1,M_.values(),nfactors,Minv_.values(),
201 nfactors,&xdata[0],nfactors,&Mxdata[0],nfactors,
202 &ferr[0],&berr[0],&work[0],&lwork[0],&info);
203 if (info != 0) {
204 std::stringstream ss;
205 ss << ">>> OED::StdMomentOperator::applyInverse : "
206 << "PORFS exited with INFO equal to " << info << "!" << std::endl
207 << " < 0: if INFO = -i, the i-th argument had an illegal value";
208 throw Exception::NotImplemented(ss.str());
209 }
210 }
211 else {
212 const Real zero(0), one(1);
213 std::vector<Real> Ux(nfactors);
214 blas_->GEMV(Teuchos::TRANS,nfactors,nfactors,one,U_.values(),nfactors,
215 &xdata[0],1,zero,&Ux[0],1);
216 Real maxSV(-1);
217 for (int i = 0; i < nfactors; ++i) {
218 maxSV = (maxSV < sval_[i] ? sval_[i] : maxSV);
219 }
220 const Real tol = ROL_EPSILON<Real>()*static_cast<Real>(nfactors) * maxSV;
221 for (int i = 0; i < nfactors; ++i) {
222 if (sval_[i] > tol) {
223 Ux[i] /= sval_[i];
224 }
225 else {
226 Ux[i] = zero;
227 }
228 }
229 blas_->GEMV(Teuchos::TRANS,nfactors,nfactors,one,V_.values(),nfactors,
230 &Ux[0],1,zero,&Mxdata[0],1);
231 }
232 }
233 else {
234 std::stringstream ss;
235 ss << ">>> OED::StdMomentOperator::applyInverse : "
236 << "Factors have not been set!";
237 throw Exception::NotImplemented(ss.str());
238 }
239 stopTimer("applyInverse");
240 }
241
242 template<typename Real>
243 void StdMomentOperator<Real>::apply(Vector<Real> &Mc,
244 const Vector<Real> &c,
245 const Vector<Real> &p) {
246 startTimer("apply");
247 if (isSet_) {
248 build(p);
249 const Real zero(0), one(1);
250 const int M = M_.numRows();
251 std::vector<Real> &Mcdata = getData(Mc);
252 const std::vector<Real> & cdata = getConstData(c);
253 blas_->SYMM(Teuchos::LEFT_SIDE,Teuchos::UPPER_TRI,M,1,
254 one,M_.values(),M,&cdata[0],M,zero,
255 &Mcdata[0],M);
256 }
257 else {
258 std::stringstream ss;
259 ss << ">>> OED::StdMomentOperator::apply : "
260 << "Factors have not been set!";
261 throw Exception::NotImplemented(ss.str());
262 }
263 stopTimer("apply");
264 }
265
266 template<typename Real>
267 void StdMomentOperator<Real>::applyDeriv(Vector<Real> &Mc,
268 const Vector<Real> &c,
269 const Vector<Real> &p) {
270 startTimer("applyDeriv");
271 if (isSet_) {
272 Mc.zero();
273 const Real zero(0), one(1);
274 const int M = Xdata_[0].numRows();
275 const int N = Xdata_[0].numCols();
276 std::vector<Real> Xctmp(M), mMctmp(N), mMcdata(N);
277 std::vector<Real> &Mcdata = getData(Mc);
278 const std::vector<Real> & cdata = getConstData(c);
279 for (int k = 0; k < nobs_; ++k) {
280 blas_->GEMV(Teuchos::NO_TRANS,M,N,one,Xdata_[k].values(),M,
281 &cdata[0],1,zero,&Xctmp[0],1);
282 for (int i = 0; i < M; ++i) Xctmp[i] *= get(p,i)*getNoise(i);
283 blas_->GEMV(Teuchos::TRANS,M,N,one,Xdata_[k].values(),M,
284 &Xctmp[0],1,zero,&mMctmp[0],1);
285 for (int i = 0; i < N; ++i) mMcdata[i] += mMctmp[i];
286 }
287 sumAll(p,&mMcdata[0],&Mcdata[0],N);
288 }
289 else {
290 std::stringstream ss;
291 ss << ">>> OED::StdMomentOperator::applyDeriv : "
292 << "Factors have not been set!";
293 throw Exception::NotImplemented(ss.str());
294 }
295 stopTimer("applyDeriv");
296 }
297
298 template<typename Real>
299 void StdMomentOperator<Real>::applySampleMatrices(Vector<Real> &uXv, const Vector<Real> &u, const Vector<Real> &v) {
300 startTimer("applySampleMatrices");
301 if (isSet_) {
302 const Real zero(0), one(1);
303 const int M = Xdata_[0].numRows();
304 const int N = Xdata_[0].numCols();
305 uXv.zero();
306 std::vector<Real> Xutmp(M,0), Xvtmp(M,0);
307 std::vector<Real> &Xudata = getLocalDesign(uXv);
308 const std::vector<Real> &udata = getConstData(u);
309 const std::vector<Real> &vdata = getConstData(v);
310 for (int k = 0; k < nobs_; ++k) {
311 blas_->GEMV(Teuchos::NO_TRANS,M,N,one,Xdata_[k].values(),M,
312 &udata[0],1,zero,&Xutmp[0],1);
313 blas_->GEMV(Teuchos::NO_TRANS,M,N,one,Xdata_[k].values(),M,
314 &vdata[0],1,zero,&Xvtmp[0],1);
315 for (int i = 0; i < M; ++i) Xudata[i] += Xutmp[i]*Xvtmp[i];
316 }
317 for (int i = 0; i < M; ++i) Xudata[i] *= getNoise(i);
318 }
319 else {
320 std::stringstream ss;
321 ss << ">>> OED::StdMomentOperator::applySampleMatrices : "
322 << "Factors have not been set!";
323 throw Exception::NotImplemented(ss.str());
324 }
325 stopTimer("applySampleMatrices");
326 }
327
328 template<typename Real>
329 void StdMomentOperator<Real>::setFactors(const Ptr<Factors<Real>> &factors) {
330 startTimer("setFactors");
331 MomentOperator<Real>::setFactors(factors);
332 int nrows = factors->numMySamples(), ncols = factors->numFactors();
333 initialize(ncols);
334 Ptr<Vector<Real>> obs = factors->createObservationVector(true);
335 nobs_ = obs->dimension();
336 Xdata_.resize(nobs_);
337 Xpred_.shape(nrows,ncols);
338 if (nobs_ == 1) {
339 Xdata_[0].shape(nrows,ncols);
340 for (int i = 0; i < nrows; ++i) {
341 const std::vector<Real> &xdata = getConstData(*factors->get(i));
342 for (int j = 0; j < ncols; ++j) Xdata_[0](i,j) = xdata[j];
343 }
344 Xpred_ = Xdata_[0];
345 }
346 else {
347 Ptr<Vector<Real>> c = obs->clone();
348 factors->getPredictionVector(*c);
349 const std::vector<Real> &cdata = getConstData(*c);
350 for (int k = 0; k < nobs_; ++k) {
351 Xdata_[k].shape(nrows,ncols);
352 factors->setPredictionVector(*obs->basis(k));
353 for (int i = 0; i < nrows; ++i) {
354 const std::vector<Real> &xdata = getConstData(*factors->get(i));
355 for (int j = 0; j < ncols; ++j) Xdata_[k](i,j) = xdata[j];
356 }
357 blas_->AXPY(ncols*nrows,cdata[k],Xdata_[k].values(),1,Xpred_.values(),1);
358 //Xpred_ += cdata[k]*Xdata_[k];
359 }
360 factors->setPredictionVector(*c);
361 }
362 if (isPset_) {
363 P_.shape(ncols,ncols);
364 Ptr<Vector<Real>> ei = factors->createParameterVector();
365 Ptr<Vector<Real>> Pei = factors->createParameterVector();
366 for (int i = 0; i < ncols; ++i) {
367 Pei->zero();
368 ei->set(*Pei->basis(i));
369 applyPerturbation(*Pei,*ei);
370 const std::vector<Real> &Pei_data = getConstData(*Pei);
371 // Build upper triangle of perturbation
372 for (int j = 0; j <= i; ++j) P_(j,i) = Pei_data[j];
373 }
374 }
375 isSet_ = true;
376 stopTimer("setFactors");
377 }
378
379 template<typename Real>
380 void StdMomentOperator<Real>::setPerturbation(const Ptr<LinearOperator<Real>> &pOp) {
381 MomentOperator<Real>::setPerturbation(pOp);
382 isPset_ = true;
383 }
384
385 template<typename Real>
386 Real StdMomentOperator<Real>::logDeterminant(const Vector<Real> &z) {
387 startTimer("logDeterminant");
388 Real val(0);
389 if (isSet_) {
390 build(z);
391 factorize(z);
392 const int nfactors = M_.numRows();
393 for (int j = 0; j < nfactors; ++j) {
394 val += std::log(!useSVD_ ? Minv_(j,j) : sval_[j]);
395 }
396 }
397 else {
398 std::stringstream ss;
399 ss << ">>> OED::StdMomentOperator::logDeterminant : "
400 << "Factors have not been set!";
401 throw Exception::NotImplemented(ss.str());
402 }
403 if (!useSVD_) {
404 val *= static_cast<Real>(2);
405 }
406 stopTimer("logDeterminant");
407 return val;
408 }
409
410 } // End OED Namespace
411 } // End ROL Namespace
412
413 #endif