1 //                                               -*- C++ -*-
2 /**
3  *  @brief Chebychev polynomial factory
4  *
5  *  Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6  *
7  *  This library is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 #include "openturns/ChebychevFactory.hxx"
22 #include "openturns/PersistentObjectFactory.hxx"
23 #include "openturns/Arcsine.hxx"
24 #include "openturns/Exception.hxx"
25 
26 BEGIN_NAMESPACE_OPENTURNS
27 
28 CLASSNAMEINIT(ChebychevFactory)
29 
30 static const Factory<ChebychevFactory> Factory_ChebychevFactory;
31 
32 /* Default constructor */
ChebychevFactory()33 ChebychevFactory::ChebychevFactory()
34   : OrthogonalUniVariatePolynomialFactory(Arcsine(-1.0, 1.0))
35 {
36   initializeCache();
37 }
38 
39 /* Virtual constructor */
clone() const40 ChebychevFactory * ChebychevFactory::clone() const
41 {
42   return new ChebychevFactory(*this);
43 }
44 
45 
46 /* Calculate the coefficients of recurrence a0n, a1n, a2n such that
47    Pn+1(x) = (a0n * x + a1n) * Pn(x) + a2n * Pn-1(x) */
getRecurrenceCoefficients(const UnsignedInteger n) const48 ChebychevFactory::Coefficients ChebychevFactory::getRecurrenceCoefficients(const UnsignedInteger n) const
49 {
50   Coefficients recurrenceCoefficients(3, 0.0);
51   if (n == 0)
52   {
53     recurrenceCoefficients[0] = M_SQRT2;
54     return recurrenceCoefficients;
55   }
56   recurrenceCoefficients[0] = 2.0;
57   if (n == 1)
58   {
59     recurrenceCoefficients[2] = -M_SQRT2;
60     return recurrenceCoefficients;
61   }
62   recurrenceCoefficients[2] = -1.0;
63   return recurrenceCoefficients;
64 }
65 
66 /* Roots of the polynomial of degree n */
getRoots(const UnsignedInteger n) const67 Point ChebychevFactory::getRoots(const UnsignedInteger n) const
68 {
69   Point roots(n);
70   for (UnsignedInteger i = 0; i < n; ++i)
71     roots[i] = std::cos((i + 0.5) * M_PI / n);
72   return roots;
73 }
74 
75 /* Nodes and weights of the polynomial of degree n as the eigenvalues of the associated Jacobi matrix and the square
76    of the first component of the associated normalized eigenvectors */
getNodesAndWeights(const UnsignedInteger n,Point & weights) const77 Point ChebychevFactory::getNodesAndWeights(const UnsignedInteger n,
78     Point & weights) const
79 {
80   if (n == 0) throw InvalidArgumentException(HERE) << "Error: cannot compute the roots and weights of a constant polynomial.";
81   weights = Point(n, 1.0 / n);
82   return getRoots(n);
83 }
84 
85 /* String converter */
__repr__() const86 String ChebychevFactory::__repr__() const
87 {
88   return OSS() << "class=" << getClassName()
89          << " measure=" << measure_;
90 }
91 
92 
93 END_NAMESPACE_OPENTURNS
94