1 /*  _______________________________________________________________________
2 
3     PECOS: Parallel Environment for Creation Of Stochastics
4     Copyright (c) 2011, Sandia National Laboratories.
5     This software is distributed under the GNU Lesser General Public License.
6     For more information, see the README file in the top Pecos directory.
7     _______________________________________________________________________ */
8 
9 //- Class:        PolynomialApproximation
10 //- Description:  Base Class for Orthogonal/Interpolation Polynomial
11 //-               Approximations
12 //-
13 //- Owner:        Mike Eldred
14 
15 #ifndef POLYNOMIAL_APPROXIMATION_HPP
16 #define POLYNOMIAL_APPROXIMATION_HPP
17 
18 #include "BasisApproximation.hpp"
19 #include "SurrogateData.hpp"
20 #include "SharedPolyApproxData.hpp"
21 
22 namespace Pecos {
23 
24 
25 /// Derived approximation class for global basis polynomials.
26 
27 /** The PolynomialApproximation class provides a global approximation
28     based on basis polynomials.  This includes orthogonal polynomials
29     used for polynomial chaos expansions and interpolation polynomials
30     used for stochastic collocation. */
31 
32 class PolynomialApproximation: public BasisApproximation
33 {
34 public:
35 
36   //
37   //- Heading: Constructor and destructor
38   //
39 
40   /// standard constructor
41   PolynomialApproximation(const SharedBasisApproxData& shared_data);
42   /// destructorboth
43   ~PolynomialApproximation();
44 
45   //
46   //- Heading: Virtual member functions
47   //
48 
49   /// size derived class data attributes
50   virtual void allocate_arrays() = 0;
51 
52   /// initialize product (covariance) accumulators with pointers to other QoI
53   virtual void initialize_covariance(PolynomialApproximation* poly_approx_2);
54   /// clear pointers to other QoI
55   virtual void clear_covariance_pointers();
56   /// (re)initialize and update product accumulators
57   virtual void initialize_products();
58   /// query whether product interpolants are defined (non-empty)
59   virtual bool product_interpolants();
60   /// clear bits indicating previously computed moment data
61   virtual void clear_computed_bits();
62 
63   /// Computes sensitivity indices according to VBD specification
64   virtual void compute_component_sobol() = 0;
65   /// Computes total sensitivity indices according to VBD specification
66   /// and existing computations from compute_component_sobol()
67   virtual void compute_total_sobol() = 0;
68 
69   /// return RegressOrthogPolyApproximation::sparseSobolIndexMap
70   virtual ULongULongMap sparse_sobol_index_map() const;
71   /// return the number of non-zero expansion coefficients for this QoI
72   virtual size_t expansion_terms() const;
73 
74   /// retrieve the gradient for a response expansion with respect to
75   /// all variables included in the polynomial bases using the given
76   /// parameter vector and default DVV
77   virtual const RealVector& gradient_basis_variables(const RealVector& x) = 0;
78   /// retrieve the gradient for a response expansion with respect to
79   /// variables included in the polynomial basis for a given parameter
80   /// vector and a given DVV subset
81   virtual const RealVector& gradient_basis_variables(const RealVector& x,
82 						     const SizetArray& dvv) = 0;
83   /// retrieve the gradient for a response expansion with respect to
84   /// all variables not included in the polynomial bases
85   /// (nonprobabilistic variables such as design or epistemic when not
86   /// in "all" mode) using the given parameter vector and default DVV
87   virtual const RealVector&
88     gradient_nonbasis_variables(const RealVector& x) = 0;
89   /// retrieve the Hessian of the response expansion with respect to all
90   /// variables included in the polynomial basis (e.g., probabilistic
91   /// variables) for a given parameter vector
92   virtual const RealSymMatrix& hessian_basis_variables(const RealVector& x) = 0;
93 
94   /// retrieve the response value for a stored expansion using the
95   /// given parameter vector
96   virtual Real stored_value(const RealVector& x, const UShortArray& key) = 0;
97   /// retrieve the response gradient for a stored expansion with
98   /// respect to all variables included in the polynomial bases;
99   /// evaluate for the given parameter vector.
100   virtual const RealVector& stored_gradient_basis_variables(const RealVector& x,
101     const UShortArray& key) = 0;
102   /// retrieve the gradient for a stored expansion with respect to
103   /// variables included in the polynomial basis for a given parameter
104   /// vector and a given DVV subset
105   virtual const RealVector& stored_gradient_basis_variables(const RealVector& x,
106     const SizetArray& dvv, const UShortArray& key) = 0;
107   /// retrieve the response gradient for a stored expansion with
108   /// respect to all variables not included in the polynomial bases;
109   /// evaluate for the given parameter vector.
110   virtual const RealVector& stored_gradient_nonbasis_variables(
111     const RealVector& x, const UShortArray& key) = 0;
112   /// retrieve the Hessian for a stored expansion with respect to all
113   /// variables included in the polynomial basis (e.g., probabilistic
114   /// variables) for a given parameter vector
115   virtual const RealSymMatrix& stored_hessian_basis_variables(
116     const RealVector& x, const UShortArray& key) = 0;
117 
118   /// return the mean of the expansion, treating all variables as random
119   virtual Real mean() = 0;
120   /// return the mean of the expansion for a given parameter vector,
121   /// treating a subset of the variables as random
122   virtual Real mean(const RealVector& x) = 0;
123   /// return the covariance between two response expansions, treating
124   /// all variables as random
125   virtual Real covariance(PolynomialApproximation* poly_approx_2) = 0;
126   /// return the covariance between two response expansions for a given
127   /// parameter vector, treating a subset of the variables as random
128   virtual Real covariance(const RealVector& x,
129 			  PolynomialApproximation* poly_approx_2) = 0;
130 
131   /// return the gradient of the expansion mean for a given parameter
132   /// vector, treating all variables as random
133   virtual const RealVector& mean_gradient() = 0;
134   /// return the gradient of the expansion mean for a given parameter vector
135   /// and given DVV, treating a subset of the variables as random
136   virtual const RealVector& mean_gradient(const RealVector& x,
137 					  const SizetArray& dvv) = 0;
138   /// return the gradient of the expansion variance for a given parameter
139   /// vector, treating all variables as random
140   virtual const RealVector& variance_gradient() = 0;
141   /// return the gradient of the expansion variance for a given parameter
142   /// vector and given DVV, treating a subset of the variables as random
143   virtual const RealVector& variance_gradient(const RealVector& x,
144 					      const SizetArray& dvv) = 0;
145 
146   /// return the mean of the combined expansion, treating all variables
147   /// as random
148   virtual Real combined_mean();
149   /// return the mean of the combined expansion for a given parameter vector,
150   /// treating a subset of the variables as random
151   virtual Real combined_mean(const RealVector& x);
152   /// return the covariance between two combined response expansions,
153   /// treating all variables as random
154   virtual Real combined_covariance(PolynomialApproximation* poly_approx_2);
155   /// return the covariance between two combined response expansions for a
156   /// given parameter vector, treating a subset of the variables as random
157   virtual Real combined_covariance(const RealVector& x,
158 				   PolynomialApproximation* poly_approx_2);
159 
160   /// return the reliability index (mapped from z_bar), where all active
161   /// variables are random
162   virtual Real beta(bool cdf_flag, Real z_bar);
163   /// return the reliability index (mapped from z_bar), treating a subset of
164   /// variables as random
165   virtual Real beta(const RealVector& x, bool cdf_flag, Real z_bar);
166   /// return the reliability index (mapped from z_bar), where all active
167   /// variables are random
168   virtual Real combined_beta(bool cdf_flag, Real z_bar);
169   /// return the reliability index (mapped from z_bar), treating a subset of
170   /// variables as random
171   virtual Real combined_beta(const RealVector& x, bool cdf_flag, Real z_bar);
172 
173   /// return the change in mean resulting from expansion refinement,
174   /// treating all variables as random
175   virtual Real delta_mean();
176   /// return the change in mean resulting from expansion refinement,
177   /// treating a subset of the variables as random
178   virtual Real delta_mean(const RealVector& x);
179   /// return the change in mean resulting from combined expansion refinement,
180   /// treating all variables as random
181   virtual Real delta_combined_mean();
182   /// return the change in mean resulting from combined expansion refinement,
183   /// treating a subset of the variables as random
184   virtual Real delta_combined_mean(const RealVector& x);
185 
186   /// return the change in standard deviation resulting from expansion
187   /// refinement, treating all variables as random
188   virtual Real delta_std_deviation();
189   /// return the change in standard deviation resulting from expansion
190   /// refinement, treating a subset of the variables as random
191   virtual Real delta_std_deviation(const RealVector& x);
192   /// return the change in standard deviation resulting from combined
193   /// expansion refinement, treating all variables as random
194   virtual Real delta_combined_std_deviation();
195   /// return the change in standard deviation resulting from combined
196   /// expansion refinement, treating a subset of the variables as random
197   virtual Real delta_combined_std_deviation(const RealVector& x);
198 
199   /// return the change in covariance between two response expansions,
200   /// treating all variables as random
201   virtual Real delta_covariance(PolynomialApproximation* poly_approx_2);
202   /// return the change in covariance between two response expansions for a
203   /// given parameter vector, treating a subset of the variables as random
204   virtual Real delta_covariance(const RealVector& x,
205 				PolynomialApproximation* poly_approx_2);
206   /// return the change in covariance between two combined response expansions,
207   /// treating all variables as random
208   virtual Real
209     delta_combined_covariance(PolynomialApproximation* poly_approx_2);
210   /// return the change in covariance between two combined response expansions
211   /// for given parameter vector, treating a subset of the variables as random
212   virtual Real
213     delta_combined_covariance(const RealVector& x,
214 			      PolynomialApproximation* poly_approx_2);
215 
216   /// return the change in reliability index (mapped from z_bar) resulting
217   /// from expansion refinement, treating all variables as random
218   virtual Real delta_beta(bool cdf_flag, Real z_bar);
219   /// return the change in reliability index (mapped from z_bar) resulting
220   /// from expansion refinement, treating a subset of the variables as random
221   virtual Real delta_beta(const RealVector& x, bool cdf_flag, Real z_bar);
222   /// return the change in reliability index (mapped from z_bar) resulting
223   /// from expansion refinement, treating all variables as random
224   virtual Real delta_combined_beta(bool cdf_flag, Real z_bar);
225   /// return the change in reliability index (mapped from z_bar) resulting
226   /// from expansion refinement, treating a subset of the variables as random
227   virtual Real delta_combined_beta(const RealVector& x, bool cdf_flag,
228 				   Real z_bar);
229 
230   /// return the change in response level (mapped from beta_bar) resulting
231   /// from expansion refinement, treating all variables as random
232   virtual Real delta_z(bool cdf_flag, Real beta_bar);
233   /// return the change in response level (mapped from beta_bar) resulting
234   /// from expansion refinement, treating a subset of the variables as random
235   virtual Real delta_z(const RealVector& x, bool cdf_flag, Real beta_bar);
236   /// return the change in response level (mapped from beta_bar) resulting
237   /// from expansion refinement, treating all variables as random
238   virtual Real delta_combined_z(bool cdf_flag, Real beta_bar);
239   /// return the change in response level (mapped from beta_bar) resulting
240   /// from expansion refinement, treating a subset of the variables as random
241   virtual Real delta_combined_z(const RealVector& x, bool cdf_flag,
242 				Real beta_bar);
243 
244   /// compute central response moments using some combination of expansion
245   /// post-processing and numerical integration
246   virtual void compute_moments(bool full_stats = true,
247 			       bool combined_stats = false);
248   /// compute central response moments in all-variables mode using some
249   /// combination of expansion post-processing and numerical integration
250   virtual void compute_moments(const RealVector& x, bool full_stats = true,
251 			       bool combined_stats = false);
252 
253   /// return moments computed analytically from the expansion
254   /// (corresponding to active key if primary)
255   virtual const RealVector& expansion_moments() const = 0;
256   /// return moments computed numerically by (sparse) quadrature
257   /// (corresponding to active key if primary)
258   virtual const RealVector& numerical_integration_moments() const = 0;
259 
260   //
261   //- Heading: Member functions
262   //
263 
264   /// return preferred response moments (either expansion or numerical
265   /// integration, depending on approximation type)
266   const RealVector& moments() const;
267   /// set preferred response moments (either expansion or numerical
268   /// integration, depending on approximation type); this is generally
269   /// used to restore previous values when popping an adaptation, without
270   /// having to recompute them
271   void moments(const RealVector& mom);
272   /// return preferred response moments (either expansion or numerical
273   /// integration, depending on approximation type)
274   Real moment(size_t i) const;
275   /// set preferred response moments (either expansion or numerical
276   /// integration, depending on approximation type); this is generally
277   /// used to restore previous values when popping an adaptation, without
278   /// having to recompute them
279   void moment(Real mom, size_t i);
280 
281   /// return combined response moments
282   const RealVector& combined_moments() const;
283   /// set combined response moments; this is generally used to restore previous
284   /// values when popping an adaptation, without having to recompute
285   void combined_moments(const RealVector& mom);
286   /// return i-th combined response moment
287   Real combined_moment(size_t i) const;
288   /// set i-th combined response moment; this is generally used to restore
289   /// previous values when popping an adaptation, without having to recompute
290   void combined_moment(Real mom, size_t i);
291 
292   /// standardize central moments 2-n and eliminate excess kurtosis
293   static void standardize_moments(const RealVector& central_moments,
294 				  RealVector& std_moments);
295 
296   /// return the variance of the expansion, treating all variables as random
297   Real variance();
298   /// return the variance of the expansion for a given parameter vector,
299   /// treating a subset of the variables as random
300   Real variance(const RealVector& x);
301   /// return the variance of the combined expansion, treating all
302   /// variables as random
303   Real combined_variance();
304   /// return the variance of the combined expansion for a given parameter
305   /// vector, treating a subset of the variables as random
306   Real combined_variance(const RealVector& x);
307 
308   /// return the change in the variance of the expansion, treating all
309   /// variables as random
310   Real delta_variance();
311   /// return the change in the variance of the expansion for a given
312   /// parameter vector, treating a subset of the variables as random
313   Real delta_variance(const RealVector& x);
314   /// return the change in the variance of the combined expansion,
315   /// treating all variables as random
316   Real delta_combined_variance();
317   /// return the change in the variance of the combined expansion for a
318   /// given parameter vector, treating a subset of the variables as random
319   Real delta_combined_variance(const RealVector& x);
320 
321   // number of data points to remove in a decrement
322   //size_t pop_count();
323 
324   /// set ExpansionConfigOptions::expansionCoeffFlag
325   void expansion_coefficient_flag(bool coeff_flag);
326   /// get ExpansionConfigOptions::expansionCoeffFlag
327   bool expansion_coefficient_flag() const;
328 
329   /// set ExpansionConfigOptions::expansionCoeffGradFlag
330   void expansion_coefficient_gradient_flag(bool grad_flag);
331   /// get ExpansionConfigOptions::expansionCoeffGradFlag
332   bool expansion_coefficient_gradient_flag() const;
333 
334   /// clear Sobol' indices when inactive
335   void clear_component_sobol();
336 
337   /// return sobolIndices
338   const RealVector& sobol_indices() const;
339   /// return totalSobolIndices
340   const RealVector& total_sobol_indices() const;
341 
342 protected:
343 
344   //
345   //- Heading: Virtual function redefinitions
346   //
347 
348   void surrogate_data(const SurrogateData& data);
349   const SurrogateData& surrogate_data() const;
350   SurrogateData& surrogate_data();
351 
352   void compute_coefficients();
353   void combined_to_active(bool clear_combined = true);
354 
355   /// generic base class function mapped to gradient_basis_variables(x)
356   const RealVector& gradient(const RealVector& x);
357   /// generic base class function mapped to hessian_basis_variables(x)
358   const RealSymMatrix& hessian(const RealVector& x);
359 
360   //
361   //- Heading: New virtual functions
362   //
363 
364   /// update *Iter for new activeKey from sharedDataRep
365   virtual bool update_active_iterators(const UShortArray& key);
366 
367   //
368   //- Heading: Member functions
369   //
370 
371   /// update surrData to define aggregated data from raw data, when indicated
372   /// by an active aggregated key
373   void synchronize_surrogate_data();
374   /// generate synthetic data for the surrogate QoI prediction corresponding
375   /// to the level key preceding active key; for use in surplus estimation
376   /// for new level data relative to a previous level's surrogate prediction
377   void generate_synthetic_data(SurrogateData& surr_data,
378 			       const UShortArray& active_key,
379 			       short combine_type);
380 
381   /// compute central moments of response using type1 numerical integration
382   void integrate_moments(const RealVector& coeffs, const RealVector& t1_wts,
383 			 RealVector& moments);
384   /// compute central moments of response using type1/2 numerical integration
385   void integrate_moments(const RealVector& t1_coeffs,
386 			 const RealMatrix& t2_coeffs, const RealVector& t1_wts,
387 			 const RealMatrix& t2_wts, RealVector& moments);
388 
389   /// size component Sobol arrays
390   void allocate_component_sobol();
391   /// size total Sobol arrays
392   void allocate_total_sobol();
393 
394   /// zero out bit trackers for active moments
395   void clear_active_bits();
396   /// zero out bit trackers for combined moments
397   void clear_combined_bits();
398 
399   //
400   //- Heading: Data
401   //
402 
403   /// SurrogateData instance containing the variables (shared) and response
404   /// (unique) data arrays for constructing a surrogate of a single response
405   /// function; this includes the original unmodified data set for one or more
406   /// level keys as well as combinations for aggregated keys (e.g., additive
407   /// or multiplicative discrepancies)
408   SurrogateData surrData;
409 
410   /// flag for calculation of expansion coefficients from response values
411   bool expansionCoeffFlag;
412   /// flag for calculation of gradients of expansion coefficients from
413   /// response gradients
414   bool expansionCoeffGradFlag;
415 
416   /// gradient of the polynomial approximation returned by gradient()
417   RealVector    approxGradient;
418   /// Hessian of the polynomial approximation returned by hessian()
419   RealSymMatrix approxHessian;
420 
421   /// mean and central moments 2/3/4 computed from either the expansion form
422   /// (OrthogPolyApproximation) or via numerical integration of the response
423   /// (InterpPolyApproximation).  Conversions to standardized moments
424   /// (std deviation, skewness, kurtosis) are performed elsewhere as needed.
425   std::map<UShortArray, RealVector> primaryMoments;
426   /// iterator to active entry in primaryMoments
427   std::map<UShortArray, RealVector>::iterator primaryMomIter;
428   /// track computation of mean and mean gradient to avoid unnecessary
429   /// recomputation
430   std::map<UShortArray, short> primaryMeanBits;
431   /// iterator to active entry in primaryMeanBits
432   std::map<UShortArray, short>::iterator primaryMeanIter;
433   /// track computation of variance and variance gradient to avoid
434   /// unnecessary recomputation
435   std::map<UShortArray, short> primaryVarBits;
436   /// iterator to active entry in primaryVarBits
437   std::map<UShortArray, short>::iterator primaryVarIter;
438   /// track previous evaluation point for all_variables mean to avoid
439   /// unnecessary recomputation
440   std::map<UShortArray, RealVector> xPrevMean;
441   /// track previous evaluation point for all_variables variance to
442   /// avoid unnecessary recomputation
443   std::map<UShortArray, RealVector> xPrevVar;
444 
445   /// gradient of mean/variance/etc. for the primary moments (expansion
446   /// for OrthogPoly, numerical for InterpPoly)
447   std::map<UShortArray, RealVectorArray> primaryMomentGrads;
448   /// iterator to active entry in primaryMomentGrads
449   std::map<UShortArray, RealVectorArray>::iterator primaryMomGradsIter;
450   /// track previous evaluation point for all_variables mean gradient
451   /// to avoid unnecessary recomputation
452   std::map<UShortArray, RealVector> xPrevMeanGrad;
453   /// track previous evaluation point for all_variables variance
454   /// gradient to avoid unnecessary recomputation
455   std::map<UShortArray, RealVector> xPrevVarGrad;
456 
457   /// alternate non-active moments (numerical for OrthogPolyApproximation or
458   /// expansion for InterpPolyApproximation).  These are computed in final
459   /// post-processing and do not currently require key management.
460   RealVector secondaryMoments;
461 
462   /// moments resulting from expansion roll-up across model index keys
463   RealVector combinedMoments;
464   /// track computation of combined mean and combined mean gradient to
465   /// avoid unnecessary recomputation
466   short combinedMeanBits;
467   /// track computation of combined variance and combined variance
468   /// gradient to avoid unnecessary recomputation
469   short combinedVarBits;
470   /// track previous evaluation point for all_variables combined mean to avoid
471   /// unnecessary recomputation
472   RealVector xPrevCombMean;
473   /// track previous evaluation point for all_variables combined variance to
474   /// avoid unnecessary recomputation
475   RealVector xPrevCombVar;
476 
477   /// global sensitivities as given by Sobol'
478   RealVector sobolIndices;
479   /// total global sensitivities as given by Sobol'
480   RealVector totalSobolIndices;
481 
482 private:
483 
484   //
485   //- Heading: Data
486   //
487 };
488 
489 
490 inline PolynomialApproximation::
PolynomialApproximation(const SharedBasisApproxData & shared_data)491 PolynomialApproximation(const SharedBasisApproxData& shared_data):
492   BasisApproximation(BaseConstructor(), shared_data), expansionCoeffFlag(true),
493   expansionCoeffGradFlag(false), primaryMomIter(primaryMoments.end()),
494   combinedMeanBits(0), combinedVarBits(0)
495 { }
496 
497 
~PolynomialApproximation()498 inline PolynomialApproximation::~PolynomialApproximation()
499 { }
500 
501 
502 inline bool PolynomialApproximation::
update_active_iterators(const UShortArray & key)503 update_active_iterators(const UShortArray& key)
504 {
505   if (primaryMomIter != primaryMoments.end() && primaryMomIter->first == key)
506     return false;
507 
508   primaryMomIter = primaryMoments.find(key);
509   if (primaryMomIter == primaryMoments.end()) {
510     std::pair<UShortArray, RealVector> rv_pair(key, RealVector());
511     primaryMomIter = primaryMoments.insert(rv_pair).first;
512   }
513 
514   //if (expansionCoeffGradFlag) { // or all_vars for combined exp grads?
515     primaryMomGradsIter = primaryMomentGrads.find(key);
516     if (primaryMomGradsIter == primaryMomentGrads.end()) {
517       std::pair<UShortArray, RealVectorArray> rva_pair(key, RealVectorArray(2));
518       primaryMomGradsIter = primaryMomentGrads.insert(rva_pair).first;
519     }
520   //}
521 
522   primaryMeanIter = primaryMeanBits.find(key);
523   if (primaryMeanIter == primaryMeanBits.end()) {
524     std::pair<UShortArray, short> us_pair(key, 0);
525     primaryMeanIter = primaryMeanBits.insert(us_pair).first;
526   }
527   primaryVarIter = primaryVarBits.find(key);
528   if (primaryVarIter == primaryVarBits.end()) {
529     std::pair<UShortArray, short> us_pair(key, 0);
530     primaryVarIter = primaryVarBits.insert(us_pair).first;
531   }
532 
533   return true;
534 }
535 
536 
surrogate_data() const537 inline const SurrogateData& PolynomialApproximation::surrogate_data() const
538 { return surrData; }
539 
540 
surrogate_data()541 inline SurrogateData& PolynomialApproximation::surrogate_data()
542 { return surrData; }
543 
544 
surrogate_data(const SurrogateData & data)545 inline void PolynomialApproximation::surrogate_data(const SurrogateData& data)
546 { surrData = data; }
547 
548 
clear_active_bits()549 inline void PolynomialApproximation::clear_active_bits()
550 { primaryMeanIter->second = primaryVarIter->second = 0; }
551 
552 
clear_combined_bits()553 inline void PolynomialApproximation::clear_combined_bits()
554 { combinedMeanBits        = combinedVarBits        = 0; }
555 
556 
clear_computed_bits()557 inline void PolynomialApproximation::clear_computed_bits()
558 {
559   //SharedPolyApproxData* data_rep = (SharedPolyApproxData*)sharedDataRep;
560   //if (data_rep->expConfigOptions.refineStatsType == COMBINED_EXPANSION_STATS)
561     clear_combined_bits();
562   //else
563     clear_active_bits();
564 }
565 
566 
moments() const567 inline const RealVector& PolynomialApproximation::moments() const
568 { return primaryMomIter->second; }
569 
570 
moments(const RealVector & mom)571 inline void PolynomialApproximation::moments(const RealVector& mom)
572 {
573   primaryMomIter->second = mom;//copy_data_partial(mom, primaryMoments, 0);
574 
575   // activate bit trackers for moment set (a truncated array _does_ invalidate
576   // other moments as this is assumed to be the available moment set)
577   switch (mom.length()) {
578   case 0:  primaryMeanIter->second &= ~1; primaryVarIter->second &= ~1; break;
579   case 1:  primaryMeanIter->second |=  1; primaryVarIter->second &= ~1; break;
580   default: primaryMeanIter->second |=  1; primaryVarIter->second |=  1; break;
581   }
582 }
583 
584 
moment(size_t i) const585 inline Real PolynomialApproximation::moment(size_t i) const
586 {
587   const RealVector& moments = primaryMomIter->second;
588   if (moments.length() <= i) {
589     PCerr << "Error: index (" << i << ") out of bounds in Polynomial"
590 	  << "Approximation::moment()." << std::endl;
591     abort_handler(-1);
592   }
593   return moments[i];
594 }
595 
596 
moment(Real mom,size_t i)597 inline void PolynomialApproximation::moment(Real mom, size_t i)
598 {
599   // see discussion above regarding future consolidation
600 
601   RealVector& moments = primaryMomIter->second;
602   if (moments.length() <= i) {
603     PCerr << "Error: index (" << i << ") out of bounds in Polynomial"
604 	  << "Approximation::moment()." << std::endl;
605     abort_handler(-1);
606   }
607   moments[i] = mom;
608 
609   // activate bit tracker for i-th moment (does _not_ invalidate other moments)
610   switch (i) {
611   case 0: primaryMeanIter->second |= 1; break;
612   case 1: primaryVarIter->second  |= 1; break;
613   //default: other indices do not have trackers
614   }
615 }
616 
617 
combined_moments() const618 inline const RealVector& PolynomialApproximation::combined_moments() const
619 { return combinedMoments; }
620 
621 
combined_moments(const RealVector & mom)622 inline void PolynomialApproximation::combined_moments(const RealVector& mom)
623 {
624   combinedMoments = mom;//copy_data_partial(mom, primaryMoments, 0);
625 
626   // activate bit trackers for moment set (a truncated array _does_ invalidate
627   // other moments as this is assumed to be the available moment set)
628   switch (mom.length()) {
629   case 0:  combinedMeanBits &= ~1; combinedVarBits &= ~1; break;
630   case 1:  combinedMeanBits |=  1; combinedVarBits &= ~1; break;
631   default: combinedMeanBits |=  1; combinedVarBits |=  1; break;//usual
632   }
633 }
634 
635 
combined_moment(size_t i) const636 inline Real PolynomialApproximation::combined_moment(size_t i) const
637 {
638   if (combinedMoments.length() <= i) {
639     PCerr << "Error: index (" << i << ") out of bounds in Polynomial"
640 	  << "Approximation::combined_moment()." << std::endl;
641     abort_handler(-1);
642   }
643   return combinedMoments[i];
644 }
645 
646 
combined_moment(Real mom,size_t i)647 inline void PolynomialApproximation::combined_moment(Real mom, size_t i)
648 {
649   // see discussion above regarding future consolidation
650 
651   if (combinedMoments.length() <= i) {
652     PCerr << "Error: index (" << i << ") out of bounds in Polynomial"
653 	  << "Approximation::moment()." << std::endl;
654     abort_handler(-1);
655   }
656   combinedMoments[i] = mom;
657 
658   // activate bit tracker for i-th moment (does _not_ invalidate other moments)
659   switch (i) {
660   case 0: combinedMeanBits |= 1; break;
661   case 1: combinedVarBits  |= 1; break;
662   //default: other indices do not have trackers
663   }
664 }
665 
666 
variance()667 inline Real PolynomialApproximation::variance()
668 { return covariance(this); }
669 
670 
variance(const RealVector & x)671 inline Real PolynomialApproximation::variance(const RealVector& x)
672 { return covariance(x, this); }
673 
674 
combined_variance()675 inline Real PolynomialApproximation::combined_variance()
676 { return combined_covariance(this); }
677 
678 
combined_variance(const RealVector & x)679 inline Real PolynomialApproximation::combined_variance(const RealVector& x)
680 { return combined_covariance(x, this); }
681 
682 
delta_variance()683 inline Real PolynomialApproximation::delta_variance()
684 { return delta_covariance(this); }
685 
686 
delta_variance(const RealVector & x)687 inline Real PolynomialApproximation::delta_variance(const RealVector& x)
688 { return delta_covariance(x, this); }
689 
690 
delta_combined_variance()691 inline Real PolynomialApproximation::delta_combined_variance()
692 { return delta_combined_covariance(this); }
693 
694 
695 inline Real PolynomialApproximation::
delta_combined_variance(const RealVector & x)696 delta_combined_variance(const RealVector& x)
697 { return delta_combined_covariance(x, this); }
698 
699 
700 //inline size_t PolynomialApproximation::pop_count()
701 //{
702 //  SharedPolyApproxData* spad_rep = (SharedPolyApproxData*)sharedDataRep;
703 //  SparseGridDriver* ssg_driver = (SparseGridDriver*)(spad_rep->driverRep);
704 //  return (size_t)ssg_driver->unique_trial_points();
705 //}
706 
707 
expansion_coefficient_flag(bool coeff_flag)708 inline void PolynomialApproximation::expansion_coefficient_flag(bool coeff_flag)
709 { expansionCoeffFlag = coeff_flag; }
710 
711 
expansion_coefficient_flag() const712 inline bool PolynomialApproximation::expansion_coefficient_flag() const
713 { return expansionCoeffFlag; }
714 
715 
716 inline void PolynomialApproximation::
expansion_coefficient_gradient_flag(bool grad_flag)717 expansion_coefficient_gradient_flag(bool grad_flag)
718 { expansionCoeffGradFlag = grad_flag; }
719 
720 
721 inline bool PolynomialApproximation::
expansion_coefficient_gradient_flag() const722 expansion_coefficient_gradient_flag() const
723 { return expansionCoeffGradFlag; }
724 
725 
clear_component_sobol()726 inline void PolynomialApproximation::clear_component_sobol()
727 {
728   //if (data_rep->expConfigOptions.vbdFlag)
729     sobolIndices = 0.;
730   //else
731   //  sobolIndices.resize(0);
732 }
733 
734 
sobol_indices() const735 inline const RealVector& PolynomialApproximation::sobol_indices() const
736 { return sobolIndices; }
737 
738 
total_sobol_indices() const739 inline const RealVector& PolynomialApproximation::total_sobol_indices() const
740 { return totalSobolIndices; }
741 
742 
gradient(const RealVector & x)743 inline const RealVector& PolynomialApproximation::gradient(const RealVector& x)
744 { return gradient_basis_variables(x); }
745 
746 
747 inline const RealSymMatrix& PolynomialApproximation::
hessian(const RealVector & x)748 hessian(const RealVector& x)
749 { return hessian_basis_variables(x); }
750 
751 } // namespace Pecos
752 
753 #endif
754