approximation.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. from ..libmp.backend import xrange
  2. from .calculus import defun
  3. #----------------------------------------------------------------------------#
  4. # Approximation methods #
  5. #----------------------------------------------------------------------------#
  6. # The Chebyshev approximation formula is given at:
  7. # http://mathworld.wolfram.com/ChebyshevApproximationFormula.html
  8. # The only major changes in the following code is that we return the
  9. # expanded polynomial coefficients instead of Chebyshev coefficients,
  10. # and that we automatically transform [a,b] -> [-1,1] and back
  11. # for convenience.
  12. # Coefficient in Chebyshev approximation
  13. def chebcoeff(ctx,f,a,b,j,N):
  14. s = ctx.mpf(0)
  15. h = ctx.mpf(0.5)
  16. for k in range(1, N+1):
  17. t = ctx.cospi((k-h)/N)
  18. s += f(t*(b-a)*h + (b+a)*h) * ctx.cospi(j*(k-h)/N)
  19. return 2*s/N
  20. # Generate Chebyshev polynomials T_n(ax+b) in expanded form
  21. def chebT(ctx, a=1, b=0):
  22. Tb = [1]
  23. yield Tb
  24. Ta = [b, a]
  25. while 1:
  26. yield Ta
  27. # Recurrence: T[n+1](ax+b) = 2*(ax+b)*T[n](ax+b) - T[n-1](ax+b)
  28. Tmp = [0] + [2*a*t for t in Ta]
  29. for i, c in enumerate(Ta): Tmp[i] += 2*b*c
  30. for i, c in enumerate(Tb): Tmp[i] -= c
  31. Ta, Tb = Tmp, Ta
  32. @defun
  33. def chebyfit(ctx, f, interval, N, error=False):
  34. r"""
  35. Computes a polynomial of degree `N-1` that approximates the
  36. given function `f` on the interval `[a, b]`. With ``error=True``,
  37. :func:`~mpmath.chebyfit` also returns an accurate estimate of the
  38. maximum absolute error; that is, the maximum value of
  39. `|f(x) - P(x)|` for `x \in [a, b]`.
  40. :func:`~mpmath.chebyfit` uses the Chebyshev approximation formula,
  41. which gives a nearly optimal solution: that is, the maximum
  42. error of the approximating polynomial is very close to
  43. the smallest possible for any polynomial of the same degree.
  44. Chebyshev approximation is very useful if one needs repeated
  45. evaluation of an expensive function, such as function defined
  46. implicitly by an integral or a differential equation. (For
  47. example, it could be used to turn a slow mpmath function
  48. into a fast machine-precision version of the same.)
  49. **Examples**
  50. Here we use :func:`~mpmath.chebyfit` to generate a low-degree approximation
  51. of `f(x) = \cos(x)`, valid on the interval `[1, 2]`::
  52. >>> from mpmath import *
  53. >>> mp.dps = 15; mp.pretty = True
  54. >>> poly, err = chebyfit(cos, [1, 2], 5, error=True)
  55. >>> nprint(poly)
  56. [0.00291682, 0.146166, -0.732491, 0.174141, 0.949553]
  57. >>> nprint(err, 12)
  58. 1.61351758081e-5
  59. The polynomial can be evaluated using ``polyval``::
  60. >>> nprint(polyval(poly, 1.6), 12)
  61. -0.0291858904138
  62. >>> nprint(cos(1.6), 12)
  63. -0.0291995223013
  64. Sampling the true error at 1000 points shows that the error
  65. estimate generated by ``chebyfit`` is remarkably good::
  66. >>> error = lambda x: abs(cos(x) - polyval(poly, x))
  67. >>> nprint(max([error(1+n/1000.) for n in range(1000)]), 12)
  68. 1.61349954245e-5
  69. **Choice of degree**
  70. The degree `N` can be set arbitrarily high, to obtain an
  71. arbitrarily good approximation. As a rule of thumb, an
  72. `N`-term Chebyshev approximation is good to `N/(b-a)` decimal
  73. places on a unit interval (although this depends on how
  74. well-behaved `f` is). The cost grows accordingly: ``chebyfit``
  75. evaluates the function `(N^2)/2` times to compute the
  76. coefficients and an additional `N` times to estimate the error.
  77. **Possible issues**
  78. One should be careful to use a sufficiently high working
  79. precision both when calling ``chebyfit`` and when evaluating
  80. the resulting polynomial, as the polynomial is sometimes
  81. ill-conditioned. It is for example difficult to reach
  82. 15-digit accuracy when evaluating the polynomial using
  83. machine precision floats, no matter the theoretical
  84. accuracy of the polynomial. (The option to return the
  85. coefficients in Chebyshev form should be made available
  86. in the future.)
  87. It is important to note the Chebyshev approximation works
  88. poorly if `f` is not smooth. A function containing singularities,
  89. rapid oscillation, etc can be approximated more effectively by
  90. multiplying it by a weight function that cancels out the
  91. nonsmooth features, or by dividing the interval into several
  92. segments.
  93. """
  94. a, b = ctx._as_points(interval)
  95. orig = ctx.prec
  96. try:
  97. ctx.prec = orig + int(N**0.5) + 20
  98. c = [chebcoeff(ctx,f,a,b,k,N) for k in range(N)]
  99. d = [ctx.zero] * N
  100. d[0] = -c[0]/2
  101. h = ctx.mpf(0.5)
  102. T = chebT(ctx, ctx.mpf(2)/(b-a), ctx.mpf(-1)*(b+a)/(b-a))
  103. for (k, Tk) in zip(range(N), T):
  104. for i in range(len(Tk)):
  105. d[i] += c[k]*Tk[i]
  106. d = d[::-1]
  107. # Estimate maximum error
  108. err = ctx.zero
  109. for k in range(N):
  110. x = ctx.cos(ctx.pi*k/N) * (b-a)*h + (b+a)*h
  111. err = max(err, abs(f(x) - ctx.polyval(d, x)))
  112. finally:
  113. ctx.prec = orig
  114. if error:
  115. return d, +err
  116. else:
  117. return d
  118. @defun
  119. def fourier(ctx, f, interval, N):
  120. r"""
  121. Computes the Fourier series of degree `N` of the given function
  122. on the interval `[a, b]`. More precisely, :func:`~mpmath.fourier` returns
  123. two lists `(c, s)` of coefficients (the cosine series and sine
  124. series, respectively), such that
  125. .. math ::
  126. f(x) \sim \sum_{k=0}^N
  127. c_k \cos(k m x) + s_k \sin(k m x)
  128. where `m = 2 \pi / (b-a)`.
  129. Note that many texts define the first coefficient as `2 c_0` instead
  130. of `c_0`. The easiest way to evaluate the computed series correctly
  131. is to pass it to :func:`~mpmath.fourierval`.
  132. **Examples**
  133. The function `f(x) = x` has a simple Fourier series on the standard
  134. interval `[-\pi, \pi]`. The cosine coefficients are all zero (because
  135. the function has odd symmetry), and the sine coefficients are
  136. rational numbers::
  137. >>> from mpmath import *
  138. >>> mp.dps = 15; mp.pretty = True
  139. >>> c, s = fourier(lambda x: x, [-pi, pi], 5)
  140. >>> nprint(c)
  141. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  142. >>> nprint(s)
  143. [0.0, 2.0, -1.0, 0.666667, -0.5, 0.4]
  144. This computes a Fourier series of a nonsymmetric function on
  145. a nonstandard interval::
  146. >>> I = [-1, 1.5]
  147. >>> f = lambda x: x**2 - 4*x + 1
  148. >>> cs = fourier(f, I, 4)
  149. >>> nprint(cs[0])
  150. [0.583333, 1.12479, -1.27552, 0.904708, -0.441296]
  151. >>> nprint(cs[1])
  152. [0.0, -2.6255, 0.580905, 0.219974, -0.540057]
  153. It is instructive to plot a function along with its truncated
  154. Fourier series::
  155. >>> plot([f, lambda x: fourierval(cs, I, x)], I) #doctest: +SKIP
  156. Fourier series generally converge slowly (and may not converge
  157. pointwise). For example, if `f(x) = \cosh(x)`, a 10-term Fourier
  158. series gives an `L^2` error corresponding to 2-digit accuracy::
  159. >>> I = [-1, 1]
  160. >>> cs = fourier(cosh, I, 9)
  161. >>> g = lambda x: (cosh(x) - fourierval(cs, I, x))**2
  162. >>> nprint(sqrt(quad(g, I)))
  163. 0.00467963
  164. :func:`~mpmath.fourier` uses numerical quadrature. For nonsmooth functions,
  165. the accuracy (and speed) can be improved by including all singular
  166. points in the interval specification::
  167. >>> nprint(fourier(abs, [-1, 1], 0), 10)
  168. ([0.5000441648], [0.0])
  169. >>> nprint(fourier(abs, [-1, 0, 1], 0), 10)
  170. ([0.5], [0.0])
  171. """
  172. interval = ctx._as_points(interval)
  173. a = interval[0]
  174. b = interval[-1]
  175. L = b-a
  176. cos_series = []
  177. sin_series = []
  178. cutoff = ctx.eps*10
  179. for n in xrange(N+1):
  180. m = 2*n*ctx.pi/L
  181. an = 2*ctx.quadgl(lambda t: f(t)*ctx.cos(m*t), interval)/L
  182. bn = 2*ctx.quadgl(lambda t: f(t)*ctx.sin(m*t), interval)/L
  183. if n == 0:
  184. an /= 2
  185. if abs(an) < cutoff: an = ctx.zero
  186. if abs(bn) < cutoff: bn = ctx.zero
  187. cos_series.append(an)
  188. sin_series.append(bn)
  189. return cos_series, sin_series
  190. @defun
  191. def fourierval(ctx, series, interval, x):
  192. """
  193. Evaluates a Fourier series (in the format computed by
  194. by :func:`~mpmath.fourier` for the given interval) at the point `x`.
  195. The series should be a pair `(c, s)` where `c` is the
  196. cosine series and `s` is the sine series. The two lists
  197. need not have the same length.
  198. """
  199. cs, ss = series
  200. ab = ctx._as_points(interval)
  201. a = interval[0]
  202. b = interval[-1]
  203. m = 2*ctx.pi/(ab[-1]-ab[0])
  204. s = ctx.zero
  205. s += ctx.fsum(cs[n]*ctx.cos(m*n*x) for n in xrange(len(cs)) if cs[n])
  206. s += ctx.fsum(ss[n]*ctx.sin(m*n*x) for n in xrange(len(ss)) if ss[n])
  207. return s