Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
recurrence_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // recurrence_example
11 //
12 // usage:
13 // recurrence_example
14 //
15 // output:
16 // Prints the recurrence coefficients for the first 5 normalized polynomials
17 // orthogonal wrt the given weight. Follows up by printing the computed
18 // norms and outputting a 11 point gaussian quadrature rule.
19 // Demonstrate orthogonality by outputting the maximum computed
20 // |<psi_i, psi_j>| for j != i.
21 
22 #include <iostream>
23 #include <iomanip>
24 
25 #include "Stokhos.hpp"
26 
27 double weightFunction(const double& x){
28  if(2*fabs(x+2) < 1){
29  return exp(-1/(1-pow(2*(x+2),2)));
30  }else if(2*fabs(x-2)<1){
31  return exp(-1/(1-pow(2*(x-2),2)));
32  }else{
33  return 0;
34  }
35 }
36 
37 int main(int argc, char **argv)
38 {
39  try {
40  const int p = 5;
41  const double leftEndPt = -3;
42  const double rightEndPt = 3;
43 
44  //Generate a basis up to order p with the support of the weight = [-5,5] using normalization.
46  Teuchos::rcp(new Stokhos::DiscretizedStieltjesBasis<int,double>("Beta",p,&weightFunction,leftEndPt,rightEndPt,true));
51  Teuchos::Array<double> norm_sq;
52  norm_sq = basis->norm_squared();
53  basis->getRecurrenceCoefficients(alpha, beta, delta, gamma);
54 
55  //Fetch alpha, beta, gamma and the computed norms and print.
56  for(int i = 0; i<p; i++){
57  std::cout << "alpha[" << i <<"]= " << alpha[i] << " beta[" << i <<"]= " << beta[i] << " gamma[" << i <<"]= " << gamma[i] << "\n";
58  }
59 
60  for(int i = 0; i<=p; i++){
61  std::cout << "E(P_"<<i<<"^2) = "<< norm_sq[i] <<"\n";
62  }
63 
64  Teuchos::Array<double> quad_points;
65  Teuchos::Array<double> quad_weights;
67  basis->getQuadPoints(20, quad_points, quad_weights, quad_values);
68  for(int i = 0; i<quad_points.size(); i++){
69  std::cout << "x_i = "<<quad_points[i]<<" w_i = "<< quad_weights[i] <<" " << i << " / " << quad_points.size()<< "\n";
70  }
71 
72  double maxOffDiag = 0;
73  double currentInnerProduct;
74  for(int i = 0; i<=p; i++){
75  for(int j = 0; j<i; j++){
76  currentInnerProduct = fabs(basis->eval_inner_product(i, j));
77  if(currentInnerProduct > maxOffDiag){
78  maxOffDiag = currentInnerProduct;
79  }
80  }
81  }
82  std::cout<<"Maximum Off Diagonal Inner Product is: " << maxOffDiag << "\n";
83  }
84 
85  catch (std::exception& e) {
86  std::cout << e.what() << std::endl;
87  }
88 
89 
90 }
KOKKOS_INLINE_FUNCTION PCE< Storage > fabs(const PCE< Storage > &a)
Generates three-term recurrence using the Discretized Stieltjes procedure.
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
double weightFunction(const double &x)
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
int main(int argc, char **argv)
size_type size() const