PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/compare_samples_id/pymodules/python2.7/lib/python/pygsl/chebyshev.py

https://gitlab.com/pooja043/Globus_Docker_2
Python | 226 lines | 197 code | 0 blank | 29 comment | 0 complexity | 7af9cbc42fb7ed8bde1f38fcc8eba8df MD5 | raw file
  1. #!/usr/bin/env python
  2. # Author : Pierre Schnizer
  3. """
  4. This module describes routines for computing Chebyshev
  5. approximations to univariate functions. A Chebyshev approximation is a
  6. truncation of the series \M{f(x) = S{sum} c_n T_n(x)}, where the Chebyshev
  7. polynomials \M{T_n(x) = cos(n \arccos x)} provide an orthogonal basis of
  8. polynomials on the interval [-1,1] with the weight function
  9. \M{1 / sqrt{1-x^2}}. The first few Chebyshev polynomials are, T_0(x) = 1,
  10. T_1(x) = x, T_2(x) = 2 x^2 - 1.
  11. def f(x, p):
  12. if x < 0.5:
  13. return 0.25
  14. else:
  15. return 0.75
  16. n = 10000;
  17. cs = cheb_series(40)
  18. F = gsl_function(f, None)
  19. cs.init(F, 0.0, 1.0)
  20. nf = float(n)
  21. for i in range(100):
  22. x = i / nf
  23. r10 = cs.eval_n(10, x)
  24. r40 = cs.eval(x)
  25. print "%g %g %g %g" % (x, f(x, None), r10, r40)
  26. """
  27. import _callback
  28. from _generic_solver import _workspace
  29. from gsl_function import gsl_function
  30. class cheb_series(_workspace):
  31. """
  32. This class manages all internal detail. It provides the space for a
  33. Chebyshev series of order N.
  34. """
  35. _alloc = _callback.gsl_cheb_alloc
  36. _free = _callback.gsl_cheb_free
  37. _init = _callback.gsl_cheb_init
  38. _eval = _callback.gsl_cheb_eval
  39. _eval_err = _callback.gsl_cheb_eval_err
  40. _eval_n = _callback.gsl_cheb_eval_n
  41. _eval_n_err = _callback.gsl_cheb_eval_n_err
  42. #_eval_mode = _callback.gsl_cheb_eval_mode
  43. #_eval_mode_e = _callback.gsl_cheb_eval_mode_e
  44. _calc_deriv = _callback.gsl_cheb_calc_deriv
  45. _calc_integ = _callback.gsl_cheb_calc_integ
  46. _get_coeff = _callback.pygsl_cheb_get_coefficients
  47. _set_coeff = _callback.pygsl_cheb_set_coefficients
  48. _get_a = _callback.pygsl_cheb_get_a
  49. _set_a = _callback.pygsl_cheb_set_a
  50. _get_b = _callback.pygsl_cheb_get_b
  51. _set_b = _callback.pygsl_cheb_set_b
  52. _get_f = _callback.pygsl_cheb_get_f
  53. _set_f = _callback.pygsl_cheb_set_f
  54. _get_order_sp = _callback.pygsl_cheb_get_order_sp
  55. _set_order_sp = _callback.pygsl_cheb_set_order_sp
  56. def __init__(self, size):
  57. """
  58. input : n
  59. @params n : number of coefficients
  60. """
  61. self._size = size
  62. _workspace.__init__(self, size)
  63. def init(self, f, a, b):
  64. """
  65. This function computes the Chebyshev approximation for the
  66. function F over the range (a,b) to the previously specified order.
  67. The computation of the Chebyshev approximation is an \M{O(n^2)}
  68. process, and requires n function evaluations.
  69. input : f, a, b
  70. @params f : a gsl_function
  71. @params a : lower limit
  72. @params b : upper limit
  73. """
  74. return self._init(self._ptr, f.get_ptr(), a, b)
  75. def eval(self, x):
  76. """
  77. This function evaluates the Chebyshev series CS at a given point X
  78. input : x
  79. x ... value where the series shall be evaluated.
  80. """
  81. return self._eval(self._ptr, x)
  82. def eval_err(self, x):
  83. """
  84. This function computes the Chebyshev series at a given point X,
  85. estimating both the series RESULT and its absolute error ABSERR.
  86. The error estimate is made from the first neglected term in the
  87. series.
  88. input : x
  89. x ... value where the error shall be evaluated.
  90. """
  91. return self._eval_err(self._ptr, x)
  92. def eval_n(self, order, x):
  93. """
  94. This function evaluates the Chebyshev series CS at a given point
  95. N, to (at most) the given order ORDER.
  96. input : n, x
  97. n ... number of cooefficients
  98. x ... value where the series shall be evaluated.
  99. """
  100. return self._eval_n(self._ptr, order, x)
  101. def eval_n_err(self, order, x):
  102. """
  103. This function evaluates a Chebyshev series CS at a given point X,
  104. estimating both the series RESULT and its absolute error ABSERR,
  105. to (at most) the given order ORDER. The error estimate is made
  106. from the first neglected term in the series.
  107. input : n, x
  108. n ... number of cooefficients
  109. x ... value where the error shall be evaluated.
  110. """
  111. return self._eval_n_err(self._ptr, order, x)
  112. # def eval_mode(self, x, mode):
  113. # """
  114. #
  115. # """
  116. # return self._eval(self._ptr, x, mode)
  117. #
  118. # def eval_mode_e(self, x, mode):
  119. # return self._eval(self._ptr, x, mode)
  120. def calc_deriv(self):
  121. """
  122. This method computes the derivative of the series CS. It returns
  123. a new instance of the cheb_series class.
  124. """
  125. tmp = cheb_series(self._size)
  126. self._calc_deriv(tmp._ptr, self._ptr)
  127. return tmp
  128. def calc_integ(self):
  129. """
  130. This method computes the integral of the series CS. It returns
  131. a new instance of the cheb_series class.
  132. """
  133. tmp = cheb_series(self._size)
  134. self._calc_integ(tmp._ptr, self._ptr)
  135. return tmp
  136. def get_coefficients(self):
  137. """
  138. Get the chebyshev coefficients.
  139. """
  140. return self._get_coeff(self._ptr)
  141. def set_coefficients(self, coefs):
  142. """
  143. Sets the chebyshev coefficients.
  144. """
  145. return self._set_coeff(self._ptr, coefs)
  146. def get_a(self):
  147. """
  148. Get the lower boundary of the current representation
  149. """
  150. return self._get_a(self._ptr)
  151. def set_a(self, a):
  152. """
  153. Set the lower boundary of the current representation
  154. """
  155. return self._set_a(self._ptr, a)
  156. def get_b(self):
  157. """
  158. Get the upper boundary of the current representation
  159. """
  160. return self._get_b(self._ptr)
  161. def set_b(self, a):
  162. """
  163. Set the upper boundary of the current representation
  164. """
  165. return self._set_b(self._ptr, a)
  166. def get_f(self):
  167. """
  168. Get the value f (what is it ?) The documentation does not tell anything
  169. about it.
  170. """
  171. return self._get_f(self._ptr)
  172. def set_f(self, a):
  173. """
  174. Set the value f (what is it ?)
  175. """
  176. return self._set_f(self._ptr, a)
  177. def get_order_sp(self):
  178. """
  179. Get the value f (what is it ?) The documentation does not tell anything
  180. about it.
  181. """
  182. return self._get_order_sp(self._ptr)
  183. def set_order_sp(self, a):
  184. """
  185. Set the value f (what is it ?)
  186. """
  187. return self._set_order_sp(self._ptr, a)