m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
11 KiB

6 months ago
  1. from sympy.core import S
  2. from sympy.core.function import Function, ArgumentIndexError
  3. from sympy.core.symbol import Dummy
  4. from sympy.functions.special.gamma_functions import gamma, digamma
  5. from sympy.functions.elementary.complexes import conjugate
  6. # See mpmath #569 and SymPy #20569
  7. def betainc_mpmath_fix(a, b, x1, x2, reg=0):
  8. from mpmath import betainc, mpf
  9. if x1 == x2:
  10. return mpf(0)
  11. else:
  12. return betainc(a, b, x1, x2, reg)
  13. ###############################################################################
  14. ############################ COMPLETE BETA FUNCTION ##########################
  15. ###############################################################################
  16. class beta(Function):
  17. r"""
  18. The beta integral is called the Eulerian integral of the first kind by
  19. Legendre:
  20. .. math::
  21. \mathrm{B}(x,y) \int^{1}_{0} t^{x-1} (1-t)^{y-1} \mathrm{d}t.
  22. Explanation
  23. ===========
  24. The Beta function or Euler's first integral is closely associated
  25. with the gamma function. The Beta function is often used in probability
  26. theory and mathematical statistics. It satisfies properties like:
  27. .. math::
  28. \mathrm{B}(a,1) = \frac{1}{a} \\
  29. \mathrm{B}(a,b) = \mathrm{B}(b,a) \\
  30. \mathrm{B}(a,b) = \frac{\Gamma(a) \Gamma(b)}{\Gamma(a+b)}
  31. Therefore for integral values of $a$ and $b$:
  32. .. math::
  33. \mathrm{B} = \frac{(a-1)! (b-1)!}{(a+b-1)!}
  34. A special case of the Beta function when `x = y` is the
  35. Central Beta function. It satisfies properties like:
  36. .. math::
  37. \mathrm{B}(x) = 2^{1 - 2x}\mathrm{B}(x, \frac{1}{2})
  38. \mathrm{B}(x) = 2^{1 - 2x} cos(\pi x) \mathrm{B}(\frac{1}{2} - x, x)
  39. \mathrm{B}(x) = \int_{0}^{1} \frac{t^x}{(1 + t)^{2x}} dt
  40. \mathrm{B}(x) = \frac{2}{x} \prod_{n = 1}^{\infty} \frac{n(n + 2x)}{(n + x)^2}
  41. Examples
  42. ========
  43. >>> from sympy import I, pi
  44. >>> from sympy.abc import x, y
  45. The Beta function obeys the mirror symmetry:
  46. >>> from sympy import beta, conjugate
  47. >>> conjugate(beta(x, y))
  48. beta(conjugate(x), conjugate(y))
  49. Differentiation with respect to both $x$ and $y$ is supported:
  50. >>> from sympy import beta, diff
  51. >>> diff(beta(x, y), x)
  52. (polygamma(0, x) - polygamma(0, x + y))*beta(x, y)
  53. >>> diff(beta(x, y), y)
  54. (polygamma(0, y) - polygamma(0, x + y))*beta(x, y)
  55. >>> diff(beta(x), x)
  56. 2*(polygamma(0, x) - polygamma(0, 2*x))*beta(x, x)
  57. We can numerically evaluate the Beta function to
  58. arbitrary precision for any complex numbers x and y:
  59. >>> from sympy import beta
  60. >>> beta(pi).evalf(40)
  61. 0.02671848900111377452242355235388489324562
  62. >>> beta(1 + I).evalf(20)
  63. -0.2112723729365330143 - 0.7655283165378005676*I
  64. See Also
  65. ========
  66. gamma: Gamma function.
  67. uppergamma: Upper incomplete gamma function.
  68. lowergamma: Lower incomplete gamma function.
  69. polygamma: Polygamma function.
  70. loggamma: Log Gamma function.
  71. digamma: Digamma function.
  72. trigamma: Trigamma function.
  73. References
  74. ==========
  75. .. [1] https://en.wikipedia.org/wiki/Beta_function
  76. .. [2] http://mathworld.wolfram.com/BetaFunction.html
  77. .. [3] http://dlmf.nist.gov/5.12
  78. """
  79. unbranched = True
  80. def fdiff(self, argindex):
  81. x, y = self.args
  82. if argindex == 1:
  83. # Diff wrt x
  84. return beta(x, y)*(digamma(x) - digamma(x + y))
  85. elif argindex == 2:
  86. # Diff wrt y
  87. return beta(x, y)*(digamma(y) - digamma(x + y))
  88. else:
  89. raise ArgumentIndexError(self, argindex)
  90. @classmethod
  91. def eval(cls, x, y=None):
  92. if y is None:
  93. return beta(x, x)
  94. if y is S.One:
  95. return 1/x
  96. if x is S.One:
  97. return 1/y
  98. def _eval_expand_func(self, **hints):
  99. x, y = self.args
  100. return gamma(x)*gamma(y) / gamma(x + y)
  101. def _eval_is_real(self):
  102. return self.args[0].is_real and self.args[1].is_real
  103. def _eval_conjugate(self):
  104. return self.func(self.args[0].conjugate(), self.args[1].conjugate())
  105. def _eval_rewrite_as_gamma(self, x, y, piecewise=True, **kwargs):
  106. return self._eval_expand_func(**kwargs)
  107. def _eval_rewrite_as_Integral(self, x, y, **kwargs):
  108. from sympy.integrals.integrals import Integral
  109. t = Dummy('t')
  110. return Integral(t**(x - 1)*(1 - t)**(y - 1), (t, 0, 1))
  111. ###############################################################################
  112. ########################## INCOMPLETE BETA FUNCTION ###########################
  113. ###############################################################################
  114. class betainc(Function):
  115. r"""
  116. The Generalized Incomplete Beta function is defined as
  117. .. math::
  118. \mathrm{B}_{(x_1, x_2)}(a, b) = \int_{x_1}^{x_2} t^{a - 1} (1 - t)^{b - 1} dt
  119. The Incomplete Beta function is a special case
  120. of the Generalized Incomplete Beta function :
  121. .. math:: \mathrm{B}_z (a, b) = \mathrm{B}_{(0, z)}(a, b)
  122. The Incomplete Beta function satisfies :
  123. .. math:: \mathrm{B}_z (a, b) = (-1)^a \mathrm{B}_{\frac{z}{z - 1}} (a, 1 - a - b)
  124. The Beta function is a special case of the Incomplete Beta function :
  125. .. math:: \mathrm{B}(a, b) = \mathrm{B}_{1}(a, b)
  126. Examples
  127. ========
  128. >>> from sympy import betainc, symbols, conjugate
  129. >>> a, b, x, x1, x2 = symbols('a b x x1 x2')
  130. The Generalized Incomplete Beta function is given by:
  131. >>> betainc(a, b, x1, x2)
  132. betainc(a, b, x1, x2)
  133. The Incomplete Beta function can be obtained as follows:
  134. >>> betainc(a, b, 0, x)
  135. betainc(a, b, 0, x)
  136. The Incomplete Beta function obeys the mirror symmetry:
  137. >>> conjugate(betainc(a, b, x1, x2))
  138. betainc(conjugate(a), conjugate(b), conjugate(x1), conjugate(x2))
  139. We can numerically evaluate the Incomplete Beta function to
  140. arbitrary precision for any complex numbers a, b, x1 and x2:
  141. >>> from sympy import betainc, I
  142. >>> betainc(2, 3, 4, 5).evalf(10)
  143. 56.08333333
  144. >>> betainc(0.75, 1 - 4*I, 0, 2 + 3*I).evalf(25)
  145. 0.2241657956955709603655887 + 0.3619619242700451992411724*I
  146. The Generalized Incomplete Beta function can be expressed
  147. in terms of the Generalized Hypergeometric function.
  148. >>> from sympy import hyper
  149. >>> betainc(a, b, x1, x2).rewrite(hyper)
  150. (-x1**a*hyper((a, 1 - b), (a + 1,), x1) + x2**a*hyper((a, 1 - b), (a + 1,), x2))/a
  151. See Also
  152. ========
  153. beta: Beta function
  154. hyper: Generalized Hypergeometric function
  155. References
  156. ==========
  157. .. [1] https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function
  158. .. [2] https://dlmf.nist.gov/8.17
  159. .. [3] https://functions.wolfram.com/GammaBetaErf/Beta4/
  160. .. [4] https://functions.wolfram.com/GammaBetaErf/BetaRegularized4/02/
  161. """
  162. nargs = 4
  163. unbranched = True
  164. def fdiff(self, argindex):
  165. a, b, x1, x2 = self.args
  166. if argindex == 3:
  167. # Diff wrt x1
  168. return -(1 - x1)**(b - 1)*x1**(a - 1)
  169. elif argindex == 4:
  170. # Diff wrt x2
  171. return (1 - x2)**(b - 1)*x2**(a - 1)
  172. else:
  173. raise ArgumentIndexError(self, argindex)
  174. def _eval_mpmath(self):
  175. return betainc_mpmath_fix, self.args
  176. def _eval_is_real(self):
  177. if all(arg.is_real for arg in self.args):
  178. return True
  179. def _eval_conjugate(self):
  180. return self.func(*map(conjugate, self.args))
  181. def _eval_rewrite_as_Integral(self, a, b, x1, x2, **kwargs):
  182. from sympy.integrals.integrals import Integral
  183. t = Dummy('t')
  184. return Integral(t**(a - 1)*(1 - t)**(b - 1), (t, x1, x2))
  185. def _eval_rewrite_as_hyper(self, a, b, x1, x2, **kwargs):
  186. from sympy.functions.special.hyper import hyper
  187. return (x2**a * hyper((a, 1 - b), (a + 1,), x2) - x1**a * hyper((a, 1 - b), (a + 1,), x1)) / a
  188. ###############################################################################
  189. #################### REGULARIZED INCOMPLETE BETA FUNCTION #####################
  190. ###############################################################################
  191. class betainc_regularized(Function):
  192. r"""
  193. The Generalized Regularized Incomplete Beta function is given by
  194. .. math::
  195. \mathrm{I}_{(x_1, x_2)}(a, b) = \frac{\mathrm{B}_{(x_1, x_2)}(a, b)}{\mathrm{B}(a, b)}
  196. The Regularized Incomplete Beta function is a special case
  197. of the Generalized Regularized Incomplete Beta function :
  198. .. math:: \mathrm{I}_z (a, b) = \mathrm{I}_{(0, z)}(a, b)
  199. The Regularized Incomplete Beta function is the cumulative distribution
  200. function of the beta distribution.
  201. Examples
  202. ========
  203. >>> from sympy import betainc_regularized, symbols, conjugate
  204. >>> a, b, x, x1, x2 = symbols('a b x x1 x2')
  205. The Generalized Regularized Incomplete Beta
  206. function is given by:
  207. >>> betainc_regularized(a, b, x1, x2)
  208. betainc_regularized(a, b, x1, x2)
  209. The Regularized Incomplete Beta function
  210. can be obtained as follows:
  211. >>> betainc_regularized(a, b, 0, x)
  212. betainc_regularized(a, b, 0, x)
  213. The Regularized Incomplete Beta function
  214. obeys the mirror symmetry:
  215. >>> conjugate(betainc_regularized(a, b, x1, x2))
  216. betainc_regularized(conjugate(a), conjugate(b), conjugate(x1), conjugate(x2))
  217. We can numerically evaluate the Regularized Incomplete Beta function
  218. to arbitrary precision for any complex numbers a, b, x1 and x2:
  219. >>> from sympy import betainc_regularized, pi, E
  220. >>> betainc_regularized(1, 2, 0, 0.25).evalf(10)
  221. 0.4375000000
  222. >>> betainc_regularized(pi, E, 0, 1).evalf(5)
  223. 1.00000
  224. The Generalized Regularized Incomplete Beta function can be
  225. expressed in terms of the Generalized Hypergeometric function.
  226. >>> from sympy import hyper
  227. >>> betainc_regularized(a, b, x1, x2).rewrite(hyper)
  228. (-x1**a*hyper((a, 1 - b), (a + 1,), x1) + x2**a*hyper((a, 1 - b), (a + 1,), x2))/(a*beta(a, b))
  229. See Also
  230. ========
  231. beta: Beta function
  232. hyper: Generalized Hypergeometric function
  233. References
  234. ==========
  235. .. [1] https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function
  236. .. [2] https://dlmf.nist.gov/8.17
  237. .. [3] https://functions.wolfram.com/GammaBetaErf/Beta4/
  238. .. [4] https://functions.wolfram.com/GammaBetaErf/BetaRegularized4/02/
  239. """
  240. nargs = 4
  241. unbranched = True
  242. def __new__(cls, a, b, x1, x2):
  243. return Function.__new__(cls, a, b, x1, x2)
  244. def _eval_mpmath(self):
  245. return betainc_mpmath_fix, (*self.args, S(1))
  246. def fdiff(self, argindex):
  247. a, b, x1, x2 = self.args
  248. if argindex == 3:
  249. # Diff wrt x1
  250. return -(1 - x1)**(b - 1)*x1**(a - 1) / beta(a, b)
  251. elif argindex == 4:
  252. # Diff wrt x2
  253. return (1 - x2)**(b - 1)*x2**(a - 1) / beta(a, b)
  254. else:
  255. raise ArgumentIndexError(self, argindex)
  256. def _eval_is_real(self):
  257. if all(arg.is_real for arg in self.args):
  258. return True
  259. def _eval_conjugate(self):
  260. return self.func(*map(conjugate, self.args))
  261. def _eval_rewrite_as_Integral(self, a, b, x1, x2, **kwargs):
  262. from sympy.integrals.integrals import Integral
  263. t = Dummy('t')
  264. integrand = t**(a - 1)*(1 - t)**(b - 1)
  265. expr = Integral(integrand, (t, x1, x2))
  266. return expr / Integral(integrand, (t, 0, 1))
  267. def _eval_rewrite_as_hyper(self, a, b, x1, x2, **kwargs):
  268. from sympy.functions.special.hyper import hyper
  269. expr = (x2**a * hyper((a, 1 - b), (a + 1,), x2) - x1**a * hyper((a, 1 - b), (a + 1,), x1)) / a
  270. return expr / beta(a, b)