TMB Documentation  v1.9.11
chebyshev.cpp
1 /*
2  * Mathlib : A C Library of Special Functions
3  * Copyright (C) 1998 Ross Ihaka
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, a copy is available at
17  * https://www.R-project.org/Licenses/
18  *
19  * SYNOPSIS
20  *
21  * int chebyshev_init(double *dos, int nos, double eta)
22  * double chebyshev_eval(double x, double *a, int n)
23  *
24  * DESCRIPTION
25  *
26  * "chebyshev_init" determines the number of terms for the
27  * double precision orthogonal series "dos" needed to insure
28  * the error is no larger than "eta". Ordinarily eta will be
29  * chosen to be one-tenth machine precision.
30  *
31  * "chebyshev_eval" evaluates the n-term Chebyshev series
32  * "a" at "x".
33  *
34  * NOTES
35  *
36  * These routines are translations into C of Fortran routines
37  * by W. Fullerton of Los Alamos Scientific Laboratory.
38  *
39  * Based on the Fortran routine dcsevl by W. Fullerton.
40  * Adapted from R. Broucke, Algorithm 446, CACM., 16, 254 (1973).
41  */
42 
43 
44 /* NaNs propagated correctly */
45 
46 template<class Float>
47 int attribute_hidden chebyshev_init(Float *dos, int nos, Float eta)
48 {
49  int i, ii;
50  Float err;
51 
52  if (nos < 1)
53  return 0;
54 
55  err = 0.0;
56  i = 0; /* just to avoid compiler warnings */
57  for (ii=1; ii<=nos; ii++) {
58  i = nos - ii;
59  err += fabs(dos[i]);
60  if (err > eta) {
61  return i;
62  }
63  }
64  return i;
65 }
66 
67 template<class Float>
68 Float attribute_hidden chebyshev_eval(Float x, const double *a, const int n)
69 {
70  Float b0, b1, b2, twox;
71  int i;
72 
73  if (n < 1 || n > 1000) ML_ERR_return_NAN;
74 
75  if (x < -1.1 || x > 1.1) ML_ERR_return_NAN;
76 
77  twox = x * 2;
78  b2 = b1 = 0;
79  b0 = 0;
80  for (i = 1; i <= n; i++) {
81  b2 = b1;
82  b1 = b0;
83  b0 = twox * b1 - b2 + a[n - i];
84  }
85  return (b0 - b2) * 0.5;
86 }
License: GPL v2