Ticket #7748: trac_7748gamma_wrapper.2.patch
File trac_7748gamma_wrapper.2.patch, 8.9 KB (added by , 12 years ago) 


sage/functions/other.py
# HG changeset patch # User Burcin Erocal <burcin@erocal.org> # Date 1266688561 3600 # Node ID 0e031be94a208a3ab8ed3d7484dbaba76900cdda # Parent a0ce93292e40d05415d90614464b257b5cce98ec trac 7748: add wrapper for gamma and incomplete gamma functions to call the appropriate one based on number of arguments. diff git a/sage/functions/other.py b/sage/functions/other.py
a b 18 18 from sage.symbolic.function import is_inexact 19 19 from sage.functions.log import exp 20 20 from sage.functions.transcendental import Ei 21 from sage.libs.mpmath import utils as mpmath_utils 21 22 22 23 one_half = ~SR(2) 23 24 … … 394 395 395 396 EXAMPLES:: 396 397 397 sage: gamma(CDF(0.5,14)) 398 sage: from sage.functions.other import gamma1 399 sage: gamma1(CDF(0.5,14)) 398 400 4.05370307804e10  5.77329983455e10*I 399 sage: gamma (CDF(I))401 sage: gamma1(CDF(I)) 400 402 0.154949828302  0.498015668118*I 401 403 402 404 Recall that `\Gamma(n)` is `n1` factorial:: 403 405 404 sage: gamma (11) == factorial(10)406 sage: gamma1(11) == factorial(10) 405 407 True 406 sage: gamma (6)408 sage: gamma1(6) 407 409 120 408 sage: gamma (1/2)410 sage: gamma1(1/2) 409 411 sqrt(pi) 410 sage: gamma (1)412 sage: gamma1(1) 411 413 Infinity 412 sage: gamma (I)414 sage: gamma1(I) 413 415 gamma(I) 414 sage: gamma (x/2)(x=5)416 sage: gamma1(x/2)(x=5) 415 417 3/4*sqrt(pi) 416 418 417 sage: gamma (float(6))419 sage: gamma1(float(6)) 418 420 120.0 419 sage: gamma (x)421 sage: gamma1(x) 420 422 gamma(x) 421 423 422 424 :: 423 425 424 sage: gamma (pi)426 sage: gamma1(pi) 425 427 gamma(pi) 426 sage: gamma (i)428 sage: gamma1(i) 427 429 gamma(I) 428 sage: gamma (i).n()430 sage: gamma1(i).n() 429 431 0.154949828301811  0.498015668118356*I 430 sage: gamma (int(5))432 sage: gamma1(int(5)) 431 433 24 432 434 433 435 434 sage: plot(gamma(x),(x,1,5)) 435 436 The gamma function only works with input that can be coerced to the 437 Symbolic Ring:: 438 439 sage: Q.<i> = NumberField(x^2+1) 440 sage: gamma(i) 441 doctest:...: DeprecationWarning: Calling symbolic functions with arguments that cannot be coerced into symbolic expressions is deprecated. 442 0.154949828301811  0.498015668118356*I 443 444 We make an exception for elements of AA or QQbar, which cannot be 445 coerced into symbolic expressions to allow this usage:: 446 447 sage: t = QQbar(sqrt(2)) + sqrt(3); t 448 3.146264369941973? 449 sage: t.parent() 450 Algebraic Field 451 452 Symbolic functions convert the arguments to symbolic expressions if they 453 are in QQbar or AA:: 454 455 sage: gamma(QQbar(I)) 456 0.154949828301811  0.498015668118356*I 436 sage: plot(gamma1(x),(x,1,5)) 457 437 458 438 TESTS: 459 439 … … 461 441 convert back to Sage:: 462 442 463 443 sage: z = var('z') 464 sage: maxima(gamma (z)).sage()444 sage: maxima(gamma1(z)).sage() 465 445 gamma(z) 466 sage: latex(gamma (z))446 sage: latex(gamma1(z)) 467 447 \Gamma\left(z\right) 468 448 469 449 Test that Trac ticket 5556 is fixed:: 470 450 471 sage: gamma (3/4)451 sage: gamma1(3/4) 472 452 gamma(3/4) 473 453 474 sage: gamma (3/4).n(100)454 sage: gamma1(3/4).n(100) 475 455 1.2254167024651776451290983034 476 456 477 457 Check that negative integer input works:: … … 488 468 Infinity 489 469 """ 490 470 GinacFunction.__init__(self, "gamma", latex_name=r'\Gamma', 491 ginac_name='tgamma') 471 ginac_name='tgamma', 472 conversions={'mathematica':'Gamma','maple':'GAMMA'}) 492 473 493 def __call__(self, x, coerce=True, hold=False, prec=None):474 def __call__(self, x, prec=None, coerce=True, hold=False): 494 475 """ 495 476 Note that the ``prec`` argument is deprecated. The precision for 496 477 the result is deduced from the precision of the input. Convert … … 502 483 sage: t.prec() 503 484 100 504 485 505 506 486 sage: gamma(6, prec=53) 507 487 doctest:...: DeprecationWarning: The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead. 508 488 120.000000000000 … … 518 498 if prec is not None: 519 499 from sage.misc.misc import deprecation 520 500 deprecation("The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead.") 501 import mpmath 502 return mpmath_utils.call(mpmath.gamma, x, prec=prec) 521 503 522 504 # this is a kludge to keep 523 505 # sage: Q.<i> = NumberField(x^2+1) … … 542 524 x = parent.complex_field()(x) 543 525 res = GinacFunction.__call__(self, x, coerce=coerce, hold=hold) 544 526 545 if prec is not None:546 return res.n(prec)547 548 527 return res 549 528 550 gamma = Function_gamma()529 gamma1 = Function_gamma() 551 530 552 531 class Function_gamma_inc(BuiltinFunction): 553 532 def __init__(self): … … 573 552 sage: gamma_inc(2., 5) 574 553 0.0404276819945128 575 554 """ 576 BuiltinFunction.__init__(self, "gamma", nargs=2, latex_name=r"\Gamma") 555 BuiltinFunction.__init__(self, "gamma", nargs=2, latex_name=r"\Gamma", 556 conversions={'maxima':'gamma_incomplete', 'mathematica':'Gamma', 557 'maple':'GAMMA'}) 577 558 578 559 def _eval_(self, x, y): 579 560 """ … … 636 617 # synonym. 637 618 incomplete_gamma = gamma_inc=Function_gamma_inc() 638 619 620 def gamma(a, *args, **kwds): 621 r""" 622 Gamma and incomplete gamma functions. 623 This is defined by the integral 624 `\Gamma(a, z) = \int_z^\infty t^{a1}e^{t} dt`. 625 626 EXAMPLES:: 627 628 Recall that `\Gamma(n)` is `n1` factorial:: 629 630 sage: gamma(11) == factorial(10) 631 True 632 sage: gamma(6) 633 120 634 sage: gamma(1/2) 635 sqrt(pi) 636 sage: gamma(1) 637 Infinity 638 639 :: 640 641 sage: gamma_inc(3,2) 642 gamma(3, 2) 643 sage: gamma_inc(x,0) 644 gamma(x) 645 646 :: 647 648 sage: gamma(5, hold=True) 649 gamma(5) 650 sage: gamma(x, 0, hold=True) 651 gamma(x, 0) 652 653 :: 654 655 sage: gamma(CDF(0.5,14)) 656 4.05370307804e10  5.77329983455e10*I 657 sage: gamma(CDF(I)) 658 0.154949828302  0.498015668118*I 659 660 The gamma function only works with input that can be coerced to the 661 Symbolic Ring:: 662 663 sage: Q.<i> = NumberField(x^2+1) 664 sage: gamma(i) 665 doctest:...: DeprecationWarning: Calling symbolic functions with arguments that cannot be coerced into symbolic expressions is deprecated. 666 0.154949828301811  0.498015668118356*I 667 668 We make an exception for elements of AA or QQbar, which cannot be 669 coerced into symbolic expressions to allow this usage:: 670 671 sage: t = QQbar(sqrt(2)) + sqrt(3); t 672 3.146264369941973? 673 sage: t.parent() 674 Algebraic Field 675 676 Symbolic functions convert the arguments to symbolic expressions if they 677 are in QQbar or AA:: 678 679 sage: gamma(QQbar(I)) 680 0.154949828301811  0.498015668118356*I 681 """ 682 if not args: 683 return gamma1(a, **kwds) 684 if len(args) > 1: 685 raise TypeError, "Symbolic function gamma takes at most 2 arguments (%s given)"%(len(args)+1) 686 return incomplete_gamma(a,args[0],**kwds) 639 687 640 688 class Function_psi1(GinacFunction): 641 689 def __init__(self): … … 775 823 sage: psi(2, x, 3) 776 824 Traceback (most recent call last): 777 825 ... 778 TypeError: Symbolic function psi takes exactly2 arguments (3 given)826 TypeError: Symbolic function psi takes at most 2 arguments (3 given) 779 827 """ 780 828 if not args: 781 829 return psi1(x, **kwds) 782 830 if len(args) > 1: 783 raise TypeError, "Symbolic function psi takes exactly2 arguments (%s given)"%(len(args)+1)831 raise TypeError, "Symbolic function psi takes at most 2 arguments (%s given)"%(len(args)+1) 784 832 return psi2(x,args[0],**kwds) 785 833 786 834 
sage/misc/functional.py
diff git a/sage/misc/functional.py b/sage/misc/functional.py
a b 696 696 sage: sin(x).integral(x, 0,pi/2) 697 697 1 698 698 sage: integral(exp(x), (x, 1, oo)) 699 gamma_incomplete(1,1)699 e^(1) 700 700 701 701 Numerical approximation:: 702 702