ROL
OrthogonalPolynomials.hpp
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<iostream>
11#include<iomanip>
12#include<cmath>
13#include"ROL_StdVector.hpp"
14#include"Teuchos_LAPACK.hpp"
15#include"LinearAlgebra.hpp"
16
17
18#ifndef __ORTHOGONAL_POLYNOMIALS__
19#define __ORTHOGONAL_POLYNOMIALS__
20
21
37template<class Real>
38void rec_jacobi( ROL::Ptr<Teuchos::LAPACK<int,Real> > lapack,
39 const double alpha,
40 const double beta,
41 std::vector<Real> &a,
42 std::vector<Real> &b ) {
43
44 int N = a.size();
45 Real nu = (beta-alpha)/double(alpha+beta+2.0);
46 Real mu = pow(2.0,alpha+beta+1.0)*tgamma(alpha+1.0)*tgamma(beta+1.0)/tgamma(alpha+beta+2.0);
47 Real nab;
48 Real sqdif = pow(beta,2)-pow(alpha,2);
49
50 a[0] = nu;
51 b[0] = mu;
52
53 if(N>1){
54
55 for(int n=1;n<N;++n) {
56 nab = 2*n+alpha+beta;
57 a[n] = sqdif/(nab*(nab+2));
58 }
59
60 b[1] = 4.0*(alpha+1.0)*(beta+1.0)/(pow(alpha+beta+2.0,2)*(alpha+beta+3.0));
61
62 if(N>2) {
63 for(int n=2;n<N;++n) {
64 nab = 2*n+alpha+beta;
65 b[n] = 4.0*(n+alpha)*(n+beta)*n*(n+alpha+beta)/(nab*nab*(nab+1.0)*(nab-1.0));
66 }
67 }
68 }
69}
70
80template<class Real>
81void vandermonde( const std::vector<Real> &a,
82 const std::vector<Real> &b,
83 const std::vector<Real> &x,
84 std::vector<Real> &V) {
85 const int n = a.size();
86
87 for(int i=0;i<n;++i) {
88 // Zeroth polynomial is always 1
89 V[i] = 1.0;
90 // First polynomial is (x-a)
91 V[i+n] = x[i] - a[0];
92 }
93 for(int j=1;j<n-1;++j) {
94 for(int i=0;i<n;++i) {
95 V[i+(j+1)*n] = (x[i] - a[j])*V[i+j*n] - b[j]*V[i+(j-1)*n];
96 }
97 }
98}
99
100
112template<class Real>
113void gauss( ROL::Ptr<Teuchos::LAPACK<int,Real> > lapack,
114 const std::vector<Real> &a,
115 const std::vector<Real> &b,
116 std::vector<Real> &x,
117 std::vector<Real> &w ) {
118 int INFO;
119 const int N = a.size();
120 const int LDZ = N;
121 const char COMPZ = 'I';
122
123 std::vector<Real> D(N,0.0);
124 std::vector<Real> E(N,0.0);
125 std::vector<Real> WORK(4*N,0.0);
126
127 // Column-stacked matrix of eigenvectors needed for weights
128 std::vector<Real> Z(N*N,0);
129
130 D = a;
131
132 for(int i=0;i<N-1;++i) {
133 E[i] = sqrt(b[i+1]);
134 }
135
136 lapack->STEQR(COMPZ,N,&D[0],&E[0],&Z[0],LDZ,&WORK[0],&INFO);
137
138 for(int i=0;i<N;++i){
139 x[i] = D[i];
140 w[i] = b[0]*pow(Z[N*i],2);
141 }
142
143}
144
145
146
159template<class Real>
160void rec_lobatto( ROL::Ptr<Teuchos::LAPACK<int,Real> > const lapack,
161 const double xl1,
162 const double xl2,
163 std::vector<Real> &a,
164 std::vector<Real> &b ) {
165 const int N = a.size()-1;
166
167 std::vector<Real> amod(N,0.0);
168 std::vector<Real> bmod(N-1,0.0);
169 std::vector<Real> en(N,0.0);
170 std::vector<Real> g(N,0.0);
171
172 // Nth canonical vector
173 en[N-1] = 1;
174
175 for(int i=0;i<N-1;++i) {
176 bmod[i] = sqrt(b[i+1]);
177 }
178
179 for(int i=0;i<N;++i) {
180 amod[i] = a[i]-xl1;
181 }
182
183 trisolve(lapack,bmod,amod,bmod,en,g);
184 Real g1 = g[N-1];
185
186 for(int i=0;i<N;++i) {
187 amod[i] = a[i]-xl2;
188 }
189
190 trisolve(lapack,bmod,amod,bmod,en,g);
191 Real g2 = g[N-1];
192
193 a[N] = (g1*xl2-g2*xl1)/(g1-g2);
194 b[N] = (xl2-xl1)/(g1-g2);
195
196}
197
198
199#endif
200
201
void trisolve(ROL::Ptr< Teuchos::LAPACK< int, Real > > lapack, const std::vector< Real > &a, const std::vector< Real > &b, const std::vector< Real > &c, const std::vector< Real > &r, std::vector< Real > &x)
Solve a tridiagonal system.
void vandermonde(const std::vector< Real > &a, const std::vector< Real > &b, const std::vector< Real > &x, std::vector< Real > &V)
Construct the generalized Vandermonde matrix (in column stacked form) based upon the recurrence coeff...
void rec_jacobi(ROL::Ptr< Teuchos::LAPACK< int, Real > > lapack, const double alpha, const double beta, std::vector< Real > &a, std::vector< Real > &b)
Generate the Jacobi polynomial recursion coeffcients .
void gauss(ROL::Ptr< Teuchos::LAPACK< int, Real > > lapack, const std::vector< Real > &a, const std::vector< Real > &b, std::vector< Real > &x, std::vector< Real > &w)
Compute the Gauss quadrature nodes and weights for the polynomials generated by the recurrence coeffi...
void rec_lobatto(ROL::Ptr< Teuchos::LAPACK< int, Real > > const lapack, const double xl1, const double xl2, std::vector< Real > &a, std::vector< Real > &b)
Modify the given recurrence coefficients so that the set of zeros of the maximal order polynomial inc...
Vector< Real > V