| 332 | from sage.symbolic.function import BuiltinFunction, GinacFunction, is_inexact |
| 333 | from sage.symbolic.expression import is_Expression |
| 334 | import sage.functions.special |
| 335 | from sage.functions.special import MaximaFunction, meval |
| 336 | from sage.functions.other import floor, gamma, factorial, abs, binomial |
| 337 | from sage.functions.other import sqrt, conjugate |
| 338 | from sage.functions.trig import sin, cos |
| 339 | from sage.functions.log import ln |
| 340 | import sage.symbolic.expression as expression |
| 341 | from sage.structure.parent import Parent |
| 342 | from sage.structure.coerce import parent |
| 343 | |
| 344 | from numpy import array as nparray |
| 345 | |
| 346 | from scipy.special import eval_chebyt, eval_legendre, eval_chebyu |
| 347 | from scipy.special import eval_gegenbauer, eval_genlaguerre, eval_hermite |
| 348 | from scipy.special import eval_jacobi, eval_laguerre, lpmv |
| 349 | |
| 350 | #Those imports are commented out, and are found in the _evalf_ |
| 351 | #functions of the ortho polys. The reason is a problem with |
| 352 | #mpmath. gen_legendre_P, and _gen_legendre_Q start to throw |
| 353 | #exceptions if mpmath is included globally. Wehn it is done |
| 354 | #locally no problems occour so far. |
| 355 | #See ticket Nr. 9706 for more details. They should be exchanged, |
| 356 | #when this problem with mpmath is solved, because of performance |
| 357 | #reasons. |
| 358 | |
| 359 | #from sage.libs.mpmath.all import call as mpcall |
| 360 | #from sage.libs.mpmath.all import legendre as mplegendre |
| 361 | #from sage.libs.mpmath.all import legenp as mplegenp |
| 362 | #from sage.libs.mpmath.all import chebyt as mpchebyt |
| 363 | #from sage.libs.mpmath.all import chebyu as mpchebyu |
| 364 | #from sage.libs.mpmath.all import gegenbauer as mpgegenbauer |
| 365 | #from sage.libs.mpmath.all import laguerre as mplaguerre |
| 366 | #from sage.libs.mpmath.all import hermite as mphermite |
| 367 | #from sage.libs.mpmath.all import jacobi as mpjacobi |
| 368 | #from sage.libs.mpmath.all import legenq as mplegenq |
| 369 | |
361 | | Returns the Chebyshev function of the first kind for integers |
362 | | `n>-1`. |
| 416 | Base Class for Orthogonal Polynomials. The evaluation as a polynomial |
| 417 | is either done via maxima, or with pynac due to performance reasons. |
| 418 | Convention: The first argument is always the order of the polynomial, |
| 419 | he last one is always the value x where the polynomial is evaluated. |
| 420 | |
| 421 | """ |
| 422 | def __init__(self, name, nargs = 2, latex_name = None, conversions = {}): |
| 423 | try: |
| 424 | self._maxima_name = conversions['maxima'] |
| 425 | except KeyError: |
| 426 | self._maxima_name = None |
| 427 | |
| 428 | BuiltinFunction.__init__(self, name = name, |
| 429 | nargs = nargs, latex_name = latex_name, conversions = conversions) |
| 430 | |
| 431 | def _maxima_init_evaled_(self, *args): |
| 432 | """ |
| 433 | Returns a string which represents this function evaluated at |
| 434 | *args* in Maxima. |
| 435 | In fact these are thought to be the old wrappers for the orthogonal |
| 436 | polynomials. These are used when the other evaluation methods fail, |
| 437 | or are not fast enough. It appears that direct computation |
| 438 | with pynac is in most cases faster than maxima. Maxima comes into |
| 439 | play when all other methods fail. |
| 440 | A little switch does the trick... |
| 441 | |
| 442 | EXAMPLES:: |
| 443 | |
| 444 | sage: chebyshev_T(3,x) |
| 445 | 4*x^3 - 3*x |
| 446 | """ |
| 447 | return None |
| 448 | |
| 449 | def _clenshaw_method_(self,*args): |
| 450 | """ |
| 451 | The Clenshaw method uses the three term recursion of the polynomial, |
| 452 | or explicit formulas instead of maxima to evaluate the polynomial |
| 453 | efficiently, if the x argument is not a symbolic expression. |
| 454 | The name comes from the Clenshaw algorithm for fast evaluation of |
| 455 | polynomials in chebyshev form. |
| 456 | |
| 457 | comparison: Maxima and Clenshaw algorithm for non-symbolic evaluation: |
| 458 | #sage: time chebyshev_T(50,10) #clenshaw |
| 459 | #CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 460 | #Wall time: 0.00 s |
| 461 | #49656746733678312490954442369580252421769338391329426325400124999 |
| 462 | #sage: time sage.functions.orthogonal_polys.chebyshev_T(50,10) |
| 463 | #maxima |
| 464 | #CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 465 | #Wall time: 0.05 s |
| 466 | #49656746733678312490954442369580252421769338391329426325400124999 |
| 467 | |
| 468 | #sage: time chebyshev_T(500,10); #clenshaw |
| 469 | #CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 470 | #Wall time: 0.00 s |
| 471 | #sage: time sage.functions.orthogonal_polys.chebyshev_T(500,10); |
| 472 | #maxima |
| 473 | #CPU times: user 0.11 s, sys: 0.00 s, total: 0.11 s |
| 474 | #Wall time: 0.77 s |
| 475 | """ |
| 476 | raise NotImplementedError( |
| 477 | "No recursive calculation of values implemented (yet)!") |
| 478 | |
| 479 | def _eval_special_values_(self,*args): |
| 480 | """ |
| 481 | Evals the polynomial explicitly for special values. |
| 482 | EXAMPLES: |
| 483 | |
| 484 | sage: var('n') |
| 485 | n |
| 486 | sage: chebyshev_T(n,-1) |
| 487 | (-1)^n |
| 488 | """ |
| 489 | raise ValueError("No special values known!") |
| 490 | |
| 491 | |
| 492 | def _eval_(self, *args): |
| 493 | """ |
| 494 | |
| 495 | The symbolic evaluation is either done with maxima, or with direct |
| 496 | computaion in pynac, because it seems the evaluation done in maxima |
| 497 | doesn't seem very clever.. |
| 498 | For the fast numerical evaluation an other method should be used... |
| 499 | Therefore I suggest Clenshaw's algorithm, which uses the recursion! |
| 500 | The function also checks for special values, and if |
| 501 | the order is an integer and in range! |
| 502 | |
| 503 | performance: |
| 504 | #sage: time chebyshev_T(5,x) #maxima |
| 505 | #CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 506 | #Wall time: 0.16 s |
| 507 | #16*(x - 1)^5 + 80*(x - 1)^4 + 140*(x - 1)^3 + 100*(x - 1)^2 + 25*x - 24 |
| 508 | |
| 509 | #sage: time chebyshev_T(5,x) #clenshaw |
| 510 | #CPU times: user 0.01 s, sys: 0.00 s, total: 0.01 s |
| 511 | #Wall time: 0.01 s |
| 512 | #16*x^5 - 20*x^3 + 5*x |
| 513 | |
| 514 | #time chebyshev_T(50,x) |
| 515 | #CPU times: user 0.03 s, sys: 0.00 s, total: 0.03 s |
| 516 | #Wall time: 0.04 s |
| 517 | #562949953421312*(x - 1)^50 + 28147497671065600*(x - 1)^49 +.... |
| 518 | |
| 519 | #time chebyshev_T(100,x); |
| 520 | #CPU times: user 0.08 s, sys: 0.00 s, total: 0.08 s |
| 521 | #Wall time: 0.08 s |
| 522 | |
| 523 | EXAMPLES:: |
| 524 | sage: chebyshev_T(5,x) |
| 525 | 16*x^5 - 20*x^3 + 5*x |
| 526 | sage: var('n') |
| 527 | n |
| 528 | sage: chebyshev_T(n,-1) |
| 529 | (-1)^n |
| 530 | sage: chebyshev_T(-7,x) |
| 531 | chebyshev_T(-7, x) |
| 532 | sage: chebyshev_T(3/2,x) |
| 533 | chebyshev_T(3/2, x) |
| 534 | |
| 535 | """ |
| 536 | |
| 537 | if not is_Expression(args[0]): |
| 538 | |
| 539 | if not is_Expression(args[-1]) and is_inexact(args[-1]): |
| 540 | try: |
| 541 | import sage.libs.mpmath.all as mpmath |
| 542 | return self._evalf_(*args) |
| 543 | except AttributeError: |
| 544 | pass |
| 545 | except mpmath.NoConvergence: |
| 546 | print "Warning: mpmath returns NoConvergence!" |
| 547 | print "Switching to clenshaw_method, but it \ |
| 548 | may not be stable!" |
| 549 | except ValueError: |
| 550 | pass |
| 551 | |
| 552 | #A faster check would be nice... |
| 553 | if args[0] != floor(args[0]): |
| 554 | if not is_Expression(args[-1]): |
| 555 | try: |
| 556 | return self._evalf_(*args) |
| 557 | except AttributeError: |
| 558 | pass |
| 559 | else: |
| 560 | return None |
| 561 | |
| 562 | if args[0] < 0: |
| 563 | return None |
| 564 | |
| 565 | |
| 566 | try: |
| 567 | return self._eval_special_values_(*args) |
| 568 | except ValueError: |
| 569 | pass |
| 570 | |
| 571 | |
| 572 | if not is_Expression(args[0]): |
| 573 | |
| 574 | try: |
| 575 | return self._clenshaw_method_(*args) |
| 576 | except NotImplementedError: |
| 577 | pass |
| 578 | |
| 579 | if self._maxima_name is None: |
| 580 | return None |
| 581 | else: |
| 582 | _init() |
| 583 | try: |
| 584 | #s = maxima(self._maxima_init_evaled_(*args)) |
| 585 | #This above is very inefficient! The older |
| 586 | #methods were much faster... |
| 587 | return self._maxima_init_evaled_(*args) |
| 588 | except TypeError: |
| 589 | return None |
| 590 | if self._maxima_name in repr(s): |
| 591 | return None |
| 592 | else: |
| 593 | return s.sage() |
| 594 | |
| 595 | |
| 596 | class Func_chebyshev_T(OrthogonalPolynomial): |
| 597 | """ |
| 598 | Class for the Chebyshev polynomial of the first kind. |
377 | | def chebyshev_U(n,x): |
| 641 | if (args[-1] == 0): |
| 642 | return (1+(-1)**args[0])*(-1)**(args[0]/2)/2 |
| 643 | |
| 644 | raise ValueError("Value not found!") |
| 645 | |
| 646 | def _evalf_(self, *args,**kwds): |
| 647 | """ |
| 648 | Evals chebyshev_T |
| 649 | numerically with mpmath. |
| 650 | EXAMPLES:: |
| 651 | sage: chebyshev_T(10,3).n(75) |
| 652 | 2.261953700000000000000e7 |
| 653 | sage: chebyshev_T(10,I).n() |
| 654 | -3363.00000000000 |
| 655 | sage: chebyshev_T(5,0.3).n() |
| 656 | 0.998880000000000 |
| 657 | """ |
| 658 | try: |
| 659 | step_parent = kwds['parent'] |
| 660 | except KeyError: |
| 661 | step_parent = parent(args[-1]) |
| 662 | |
| 663 | try: |
| 664 | precision = step_parent.prec() |
| 665 | except AttributeError: |
| 666 | precision = RR.prec() |
| 667 | |
| 668 | from sage.libs.mpmath.all import call as mpcall |
| 669 | from sage.libs.mpmath.all import chebyt as mpchebyt |
| 670 | |
| 671 | return mpcall(mpchebyt,args[0],args[-1],prec = precision) |
| 672 | |
| 673 | def _maxima_init_evaled_(self, *args): |
| 674 | n = args[0] |
| 675 | x = args[1] |
| 676 | return sage_eval(maxima.eval('chebyshev_t(%s,x)'%ZZ(n)), locals={'x':x}) |
| 677 | |
| 678 | def _clenshaw_method_(self,*args): |
| 679 | """ |
| 680 | Clenshaw method for chebyshev_T (means use recursions in this case) |
| 681 | This is much faster for numerical evaluation than maxima! |
| 682 | See A.S. 227 (p. 782) for details for the recurions |
| 683 | """ |
| 684 | |
| 685 | k = args[0] |
| 686 | x = args[1] |
| 687 | |
| 688 | if k == 0: |
| 689 | return 1 |
| 690 | elif k == 1: |
| 691 | return x |
| 692 | else: |
| 693 | #TODO: When evaluation of Symbolic Expressions works better |
| 694 | #use these explicit formulas instead! |
| 695 | #if -1 <= x <= 1: |
| 696 | # return cos(k*acos(x)) |
| 697 | #elif 1 < x: |
| 698 | # return cosh(k*acosh(x)) |
| 699 | #else: # x < -1 |
| 700 | # return (-1)**(k%2)*cosh(k*acosh(-x)) |
| 701 | |
| 702 | help1 = 1 |
| 703 | help2 = x |
| 704 | if is_Expression(x): |
| 705 | #raise NotImplementedError |
| 706 | help3 = 0 |
| 707 | for j in xrange(0,floor(k/2)+1): |
| 708 | help3 = \ |
| 709 | help3 +(-1)**j*(2*x)**(k-2*j)*factorial(k-j-1)/factorial(j)/factorial(k-2*j) |
| 710 | help3 = help3*k/2 |
| 711 | else: |
| 712 | for j in xrange(0,k-1): |
| 713 | help3 = 2*x*help2 - help1 |
| 714 | help1 = help2 |
| 715 | help2 = help3 |
| 716 | |
| 717 | return help3 |
| 718 | |
| 719 | def _eval_numpy_(self, *args): |
| 720 | """ |
| 721 | EXAMPLES:: |
| 722 | sage: import numpy |
| 723 | sage: z = numpy.array([1,2]) |
| 724 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 725 | sage: z3 = numpy.array([1,2,3.]) |
| 726 | sage: chebyshev_T(1,z) |
| 727 | array([ 1., 2.]) |
| 728 | sage: chebyshev_T(1,z2) |
| 729 | array([[ 1., 2.], |
| 730 | [ 1., 2.]]) |
| 731 | sage: chebyshev_T(1,z3) |
| 732 | array([ 1., 2., 3.]) |
| 733 | sage: chebyshev_T(z,0.1) |
| 734 | array([ 0.1 , -0.98]) |
| 735 | |
| 736 | """ |
| 737 | |
| 738 | return eval_chebyt(args[0],args[-1]) |
| 739 | |
| 740 | def _derivative_(self, *args, **kwds): |
| 741 | """ |
| 742 | Returns the derivative of chebyshev_T in form of the chebyshev Polynomial |
| 743 | of the second kind chebyshev_U |
| 744 | EXAMPLES:: |
| 745 | sage: var('k') |
| 746 | k |
| 747 | sage: derivative(chebyshev_T(k,x),x) |
| 748 | k*chebyshev_U(k - 1, x) |
| 749 | sage: derivative(chebyshev_T(3,x),x) |
| 750 | 12*x^2 - 3 |
| 751 | sage: derivative(chebyshev_T(k,x),k) |
| 752 | Traceback (most recent call last): |
| 753 | ... |
| 754 | NotImplementedError: Derivative w.r.t. to the index is not supported, yet, and perhaps never will be... |
| 755 | |
| 756 | """ |
| 757 | diff_param = kwds['diff_param'] |
| 758 | if diff_param == 0: |
| 759 | raise NotImplementedError( |
| 760 | "Derivative w.r.t. to the index is not supported, yet, \ |
| 761 | and perhaps never will be...") |
| 762 | else: |
| 763 | return args[0]*chebyshev_U(args[0]-1,args[1]) |
| 764 | |
| 765 | |
| 766 | chebyshev_T = Func_chebyshev_T() |
| 767 | |
| 768 | class Func_chebyshev_U(OrthogonalPolynomial): |
| 769 | |
394 | | def gen_laguerre(n,a,x): |
| 786 | def __init__(self): |
| 787 | OrthogonalPolynomial.__init__(self,"chebyshev_U", |
| 788 | nargs = 2,conversions =dict(maxima='chebyshev_u',mathematica='ChebyshevU')) |
| 789 | |
| 790 | def _clenshaw_method_(self,*args): |
| 791 | """ |
| 792 | Clenshaw method for chebyshev_U (means use the recursion...) |
| 793 | This is much faster for numerical evaluation than maxima! |
| 794 | See A.S. 227 (p. 782) for details for the recurions |
| 795 | """ |
| 796 | k = args[0] |
| 797 | x = args[1] |
| 798 | |
| 799 | if k == 0: |
| 800 | return 1 |
| 801 | elif k == 1: |
| 802 | return 2*x |
| 803 | else: |
| 804 | help1 = 1 |
| 805 | help2 = 2*x |
| 806 | if is_Expression(x): |
| 807 | #raise NotImplementedError("Maxima is faster here!") |
| 808 | help3 = 0 |
| 809 | for j in xrange(0,floor(k/2)+1): |
| 810 | help3 = \ |
| 811 | help3 + (-1)**j*(2*x)**(k-2*j)*factorial(k-j)/factorial(j)/factorial(k-2*j) |
| 812 | |
| 813 | else: |
| 814 | for j in xrange(0,k-1): |
| 815 | help3 = 2*x*help2 - help1 |
| 816 | help1 = help2 |
| 817 | help2 = help3 |
| 818 | |
| 819 | return help3 |
| 820 | |
| 821 | def _maxima_init_evaled_(self, *args): |
| 822 | """ |
| 823 | Uses |
| 824 | EXAMPLES:: |
| 825 | sage: x = PolynomialRing(QQ, 'x').gen() |
| 826 | sage: chebyshev_T(2,x) |
| 827 | 2*x^2 - 1 |
| 828 | """ |
| 829 | n = args[0] |
| 830 | x = args[1] |
| 831 | return sage_eval(maxima.eval('chebyshev_u(%s,x)'%ZZ(n)), locals={'x':x}) |
| 832 | |
| 833 | |
| 834 | def _evalf_(self, *args,**kwds): |
| 835 | """ |
| 836 | Evals chebyshev_U |
| 837 | numerically with mpmath. |
| 838 | EXAMPLES:: |
| 839 | sage: chebyshev_U(5,-4+3.*I) |
| 840 | 98280.0000000000 - 11310.0000000000*I |
| 841 | sage: chebyshev_U(10,3).n(75) |
| 842 | 4.661117900000000000000e7 |
| 843 | """ |
| 844 | try: |
| 845 | step_parent = kwds['parent'] |
| 846 | except KeyError: |
| 847 | step_parent = parent(args[-1]) |
| 848 | |
| 849 | try: |
| 850 | precision = step_parent.prec() |
| 851 | except AttributeError: |
| 852 | precision = RR.prec() |
| 853 | |
| 854 | from sage.libs.mpmath.all import call as mpcall |
| 855 | from sage.libs.mpmath.all import chebyu as mpchebyu |
| 856 | |
| 857 | return mpcall(mpchebyu,args[0],args[-1],prec = precision) |
| 858 | |
| 859 | def _eval_special_values_(self,*args): |
| 860 | """ |
| 861 | Special values known. A.S. 22.4 (p.777). |
| 862 | EXAMPLES: |
| 863 | |
| 864 | sage: var('n') |
| 865 | n |
| 866 | sage: chebyshev_U(n,1) |
| 867 | n + 1 |
| 868 | sage: chebyshev_U(n,-1) |
| 869 | (n + 1)*(-1)^n |
| 870 | """ |
| 871 | if args[-1] == 1: |
| 872 | return (args[0]+1) |
| 873 | |
| 874 | if args[-1] == -1: |
| 875 | return (-1)**args[0]*(args[0]+1) |
| 876 | |
| 877 | if (args[-1] == 0): |
| 878 | return (1+(-1)**args[0])*(-1)**(args[0]/2)/2 |
| 879 | |
| 880 | raise ValueError("Value not found") |
| 881 | |
| 882 | def _eval_numpy_(self, *args): |
| 883 | """ |
| 884 | EXAMPLES:: |
| 885 | sage: import numpy |
| 886 | sage: z = numpy.array([1,2]) |
| 887 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 888 | sage: z3 = numpy.array([1,2,3.]) |
| 889 | sage: chebyshev_U(1,z) |
| 890 | array([ 2., 4.]) |
| 891 | sage: chebyshev_U(1,z2) |
| 892 | array([[ 2., 4.], |
| 893 | [ 2., 4.]]) |
| 894 | sage: chebyshev_U(1,z3) |
| 895 | array([ 2., 4., 6.]) |
| 896 | sage: chebyshev_U(z,0.1) |
| 897 | array([ 0.2 , -0.96]) |
| 898 | |
| 899 | """ |
| 900 | |
| 901 | return eval_chebyu(args[0],args[1]) |
| 902 | |
| 903 | |
| 904 | def _derivative_(self, *args, **kwds): |
| 905 | """ |
| 906 | Returns the derivative of chebyshev_U in form of the chebyshev |
| 907 | Polynomials of the first and second kind |
| 908 | |
| 909 | EXAMPLES:: |
| 910 | sage: var('k') |
| 911 | k |
| 912 | sage: derivative(chebyshev_U(k,x),x) |
| 913 | ((k + 1)*chebyshev_T(k + 1, x) - x*chebyshev_U(k, x))/(x^2 - 1) |
| 914 | sage: derivative(chebyshev_U(3,x),x) |
| 915 | 24*x^2 - 4 |
| 916 | sage: derivative(chebyshev_U(k,x),k) |
| 917 | Traceback (most recent call last): |
| 918 | ... |
| 919 | NotImplementedError: Derivative w.r.t. to the index is not supported, yet, and perhaps never will be... |
| 920 | |
| 921 | """ |
| 922 | diff_param = kwds['diff_param'] |
| 923 | if diff_param == 0: |
| 924 | raise NotImplementedError( |
| 925 | "Derivative w.r.t. to the index is not supported, \ |
| 926 | yet, and perhaps never will be...") |
| 927 | else: |
| 928 | return ((args[0]+1)*chebyshev_T(args[0]+1,args[1])-args[1]* |
| 929 | chebyshev_U(args[0],args[1]))/(args[1]**2-1) |
| 930 | |
| 931 | chebyshev_U = Func_chebyshev_U() |
| 932 | |
| 933 | class Func_gegenbauer(OrthogonalPolynomial): |
| 934 | """ |
| 935 | Returns the ultraspherical (or Gegenbauer) polynomial. |
| 936 | |
| 937 | REFERENCE: |
| 938 | |
| 939 | - AS 22.5.27 |
| 940 | |
| 941 | EXAMPLES:: |
| 942 | |
| 943 | sage: x = PolynomialRing(QQ, 'x').gen() |
| 944 | sage: ultraspherical(2,3/2,x) |
| 945 | 15/2*x^2 - 3/2 |
| 946 | sage: ultraspherical(2,1/2,x) |
| 947 | 3/2*x^2 - 1/2 |
| 948 | sage: ultraspherical(1,1,x) |
| 949 | 2*x |
| 950 | sage: t = PolynomialRing(RationalField(),"t").gen() |
| 951 | sage: gegenbauer(3,2,t) |
| 952 | 32*t^3 - 12*t |
| 953 | """ |
| 954 | def __init__(self): |
| 955 | OrthogonalPolynomial.__init__(self,"gegenbauer",nargs = 3, |
| 956 | conversions =dict(maxima='ultraspherical',mathematica='GegenbauerC')) |
| 957 | |
| 958 | def _clenshaw_method_(self,*args): |
| 959 | """ |
| 960 | Clenshaw method for gegenbauer poly (means use the recursion...) |
| 961 | This is much faster for numerical evaluation than maxima! |
| 962 | See A.S. 227 (p. 782) for details for the recurions |
| 963 | """ |
| 964 | k = args[0] |
| 965 | x = args[-1] |
| 966 | alpha = args[1] |
| 967 | |
| 968 | #if is_Expression(alpha) or abs(alpha) > numpy.finfo(float).eps: |
| 969 | # alpha_zero = False |
| 970 | #else: |
| 971 | # alpha_zero = True |
| 972 | #This should be discussed. The case 0 is the chebyshev_T. |
| 973 | #(see A.S. 22.4 (p.777)); But since all systems handle |
| 974 | #it this way we perhaps should leave it. |
| 975 | |
| 976 | if k == 0: |
| 977 | return 1 |
| 978 | elif k == 1: |
| 979 | return 2*x*alpha |
| 980 | else: |
| 981 | help1 = 1 |
| 982 | help2 = 2*x*alpha |
| 983 | gamma_alpha = gamma(alpha) |
| 984 | |
| 985 | help3 = 0 |
| 986 | if is_Expression(x): |
| 987 | for j in xrange(0,floor(k/2)+1): |
| 988 | help3 = \ |
| 989 | help3 + (-1)**j*gamma(alpha+k-j)/factorial(j)/factorial(k-2*j)*(2*x)**(k-2*j)/gamma_alpha |
| 990 | |
| 991 | else: |
| 992 | for j in xrange(1,k): |
| 993 | help3 = 2*(j+alpha)*x*help2 - (j+2*alpha-1)*help1 |
| 994 | help3 = help3/(j+1) |
| 995 | help1 = help2 |
| 996 | help2 = help3 |
| 997 | |
| 998 | return help3 |
| 999 | |
| 1000 | def _maxima_init_evaled_(self, *args): |
| 1001 | """ |
| 1002 | Uses |
| 1003 | EXAMPLES:: |
| 1004 | sage: x = PolynomialRing(QQ, 'x').gen() |
| 1005 | sage: ultraspherical(2,1/2,x) |
| 1006 | 3/2*x^2 - 1/2 |
| 1007 | """ |
| 1008 | n = args[0] |
| 1009 | a = args[1] |
| 1010 | x = args[2] |
| 1011 | return sage_eval(maxima.eval('ultraspherical(%s,%s,x)'%(ZZ(n),a)),\ |
| 1012 | locals={'x':x}) |
| 1013 | |
| 1014 | |
| 1015 | def _evalf_(self, *args,**kwds): |
| 1016 | """ |
| 1017 | Evals gegenbauer |
| 1018 | numerically with mpmath. |
| 1019 | EXAMPLES:: |
| 1020 | sage: gegenbauer(10,2,3.).n(54) |
| 1021 | 5.25360702000000e8 |
| 1022 | """ |
| 1023 | |
| 1024 | try: |
| 1025 | step_parent = kwds['parent'] |
| 1026 | except KeyError: |
| 1027 | step_parent = parent(args[-1]) |
| 1028 | |
| 1029 | try: |
| 1030 | precision = step_parent.prec() |
| 1031 | except AttributeError: |
| 1032 | precision = RR.prec() |
| 1033 | |
| 1034 | from sage.libs.mpmath.all import call as mpcall |
| 1035 | from sage.libs.mpmath.all import gegenbauer as mpgegenbauer |
| 1036 | |
| 1037 | return mpcall( |
| 1038 | mpgegenbauer,args[0],args[1],args[-1],prec = precision) |
| 1039 | |
| 1040 | def _eval_special_values_(self,*args): |
| 1041 | """ |
| 1042 | Special values known. A.S. 22.4 (p.777) |
| 1043 | EXAMPLES: |
| 1044 | |
| 1045 | sage: var('n a') |
| 1046 | (n, a) |
| 1047 | sage: gegenbauer(n,1/2,x) |
| 1048 | legendre_P(n, x) |
| 1049 | sage: gegenbauer(n,1,x) |
| 1050 | chebyshev_U(n, x) |
| 1051 | sage: gegenbauer(n,a,1) |
| 1052 | binomial(2*a + n - 1, n) |
| 1053 | sage: gegenbauer(n,a,-1) |
| 1054 | (-1)^n*binomial(2*a + n - 1, n) |
| 1055 | sage: gegenbauer(n,a,0) |
| 1056 | 1/2*((-1)^n + 1)*(-1)^(1/2*n)*gamma(a + 1/2*n)/(gamma(a)*gamma(1/2*n)) |
| 1057 | """ |
| 1058 | #if args[1] == 0 and args[0] != 0: |
| 1059 | # return args[0]*chebyshev_T(args[0],args[-1])/2 |
| 1060 | #This should be discussed |
| 1061 | |
| 1062 | if args[1] == 0.5: |
| 1063 | return legendre_P(args[0],args[-1]) |
| 1064 | |
| 1065 | if args[1] == 1: |
| 1066 | return chebyshev_U(args[0],args[-1]) |
| 1067 | |
| 1068 | if args[-1] == 1: |
| 1069 | return binomial(args[0] + 2*args[1] - 1,args[0]) |
| 1070 | |
| 1071 | if args[-1] == -1: |
| 1072 | return (-1)**args[0]*binomial(args[0] + 2*args[1] - 1,args[0]) |
| 1073 | |
| 1074 | if (args[-1] == 0): |
| 1075 | return (1+(-1)**args[0])*(-1)**(args[0]/2)/2*\ |
| 1076 | gamma(args[1]+args[0]/2)/gamma(args[1])/gamma(args[0]/2) |
| 1077 | |
| 1078 | raise ValueError("Value not found") |
| 1079 | |
| 1080 | def _eval_numpy_(self, *args): |
| 1081 | """ |
| 1082 | EXAMPLES:: |
| 1083 | sage: import numpy |
| 1084 | sage: z = numpy.array([1,2]) |
| 1085 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 1086 | sage: z3 = numpy.array([1,2,3.]) |
| 1087 | sage: gegenbauer(2,0,z) |
| 1088 | array([ 2.78134232e-309, 1.94693963e-308]) |
| 1089 | sage: gegenbauer(2,1,z) |
| 1090 | array([ 3., 15.]) |
| 1091 | sage: gegenbauer(2,1,z2) |
| 1092 | array([[ 3., 15.], |
| 1093 | [ 3., 15.]]) |
| 1094 | sage: gegenbauer(2,1,z3) |
| 1095 | array([ 3., 15., 35.]) |
| 1096 | """ |
| 1097 | |
| 1098 | return eval_gegenbauer(args[0],args[1],args[-1]) |
| 1099 | |
| 1100 | def _derivative_(self, *args, **kwds): |
| 1101 | """ |
| 1102 | Returns the derivative of chebyshev_U in form of the chebyshev |
| 1103 | Polynomials of the first and second kind |
| 1104 | |
| 1105 | EXAMPLES:: |
| 1106 | sage: var('k a') |
| 1107 | (k, a) |
| 1108 | sage: derivative(gegenbauer(k,a,x),x) |
| 1109 | (k*x*gegenbauer(k, a, x) - (2*a + k - 1)*gegenbauer(k - 1, a, x))/(x^2 - 1) |
| 1110 | sage: derivative(gegenbauer(4,3,x),x) |
| 1111 | 960*x^3 - 240*x |
| 1112 | sage: derivative(gegenbauer(k,a,x),a) |
| 1113 | Traceback (most recent call last): |
| 1114 | ... |
| 1115 | NotImplementedError: Derivative w.r.t. to the index is not supported, yet,and perhaps never will be... |
| 1116 | """ |
| 1117 | diff_param = kwds['diff_param'] |
| 1118 | if diff_param in [0,1]: |
| 1119 | raise NotImplementedError( |
| 1120 | "Derivative w.r.t. to the index is not supported, yet,\ |
| 1121 | and perhaps never will be...") |
| 1122 | else: |
| 1123 | return (-args[0]*args[-1]*gegenbauer(args[0],args[1],args[2])+\ |
| 1124 | (args[0] + 2*args[1]-1)*gegenbauer(args[0]-1,args[1],args[2]))/(1-args[-1]**2) |
| 1125 | |
| 1126 | gegenbauer = Func_gegenbauer() |
| 1127 | ultraspherical = Func_gegenbauer() |
| 1128 | |
| 1129 | class Func_gen_laguerre(OrthogonalPolynomial): |
528 | | def jacobi_P(n,a,b,x): |
| 1316 | def _clenshaw_method_(self,*args): |
| 1317 | """ |
| 1318 | Clenshaw method for hermite polynomial (means use the recursion...) |
| 1319 | See A.S. 227 (p. 782) for details for the recurions |
| 1320 | For the symbolic evaluation, maxima seems to be quite fast. |
| 1321 | The break even point between the recursion and Maxima is about |
| 1322 | n = 25 |
| 1323 | """ |
| 1324 | k = args[0] |
| 1325 | x = args[1] |
| 1326 | |
| 1327 | if k == 0: |
| 1328 | return 1 |
| 1329 | elif k == 1: |
| 1330 | return 2*x |
| 1331 | else: |
| 1332 | help1 = 1 |
| 1333 | help2 = 2*x |
| 1334 | if is_Expression(x): |
| 1335 | help3 = 0 |
| 1336 | for j in xrange(0,floor(k/2)+1): |
| 1337 | help3 = help3 + (-1)**j*(2*x)**(k-2*j)/factorial(j)/\ |
| 1338 | factorial(k-2*j) |
| 1339 | help3 = help3*factorial(k) |
| 1340 | else: |
| 1341 | for j in xrange(1,k): |
| 1342 | help3 = 2*x*help2 - 2*j*help1 |
| 1343 | help1 = help2 |
| 1344 | help2 = help3 |
| 1345 | |
| 1346 | return help3 |
| 1347 | |
| 1348 | |
| 1349 | def _evalf_(self, *args,**kwds): |
| 1350 | """ |
| 1351 | Evals hermite |
| 1352 | numerically with mpmath. |
| 1353 | EXAMPLES:: |
| 1354 | sage: hermite(3,2.).n(74) |
| 1355 | 40.0000000000000000000 |
| 1356 | """ |
| 1357 | try: |
| 1358 | step_parent = kwds['parent'] |
| 1359 | except KeyError: |
| 1360 | step_parent = parent(args[-1]) |
| 1361 | |
| 1362 | try: |
| 1363 | precision = step_parent.prec() |
| 1364 | except AttributeError: |
| 1365 | precision = RR.prec() |
| 1366 | |
| 1367 | from sage.libs.mpmath.all import call as mpcall |
| 1368 | from sage.libs.mpmath.all import hermite as mphermite |
| 1369 | |
| 1370 | return mpcall(mphermite,args[0],args[-1],prec = precision) |
| 1371 | |
| 1372 | def _eval_special_values_(self,*args): |
| 1373 | """ |
| 1374 | Special values known. A.S. 22.4 (p.777) |
| 1375 | EXAMPLES: |
| 1376 | |
| 1377 | sage: var('n') |
| 1378 | n |
| 1379 | sage: hermite(n,0) |
| 1380 | ((-1)^n + 1)*(-1)^(1/2*n)*factorial(n)/gamma(1/2*n + 1) |
| 1381 | """ |
| 1382 | |
| 1383 | if (args[-1] == 0): |
| 1384 | return (1+(-1)**args[0])*(-1)**(args[0]/2)*\ |
| 1385 | factorial(args[0])/gamma(args[0]/2+1) |
| 1386 | |
| 1387 | raise ValueError("Value not found") |
| 1388 | |
| 1389 | def _maxima_init_evaled_(self, *args): |
| 1390 | """ |
| 1391 | Old maxima method. |
| 1392 | """ |
| 1393 | n = args[0] |
| 1394 | x = args[1] |
| 1395 | return sage_eval(maxima.eval('hermite(%s,x)'%ZZ(n)), locals={'x':x}) |
| 1396 | |
| 1397 | def _eval_numpy_(self, *args): |
| 1398 | """ |
| 1399 | EXAMPLES:: |
| 1400 | sage: import numpy |
| 1401 | sage: z = numpy.array([1,2]) |
| 1402 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 1403 | sage: z3 = numpy.array([1,2,3.]) |
| 1404 | sage: hermite(1,z) |
| 1405 | array([ 2., 4.]) |
| 1406 | sage: hermite(1,z2) |
| 1407 | array([[ 2., 4.], |
| 1408 | [ 2., 4.]]) |
| 1409 | sage: hermite(1,z3) |
| 1410 | array([ 2., 4., 6.]) |
| 1411 | |
| 1412 | """ |
| 1413 | |
| 1414 | #we have to convert the input to float first |
| 1415 | return eval_hermite(nparray(args[0],dtype = float),\ |
| 1416 | nparray(args[-1],dtype = float)) |
| 1417 | |
| 1418 | |
| 1419 | def _derivative_(self, *args, **kwds): |
| 1420 | """ |
| 1421 | Returns the derivative of the hermite polynomial in form of the chebyshev |
| 1422 | Polynomials of the first and second kind |
| 1423 | |
| 1424 | EXAMPLES:: |
| 1425 | sage: var('k') |
| 1426 | k |
| 1427 | sage: derivative(hermite(k,x),x) |
| 1428 | 2*k*hermite(k - 1, x) |
| 1429 | |
| 1430 | """ |
| 1431 | diff_param = kwds['diff_param'] |
| 1432 | if diff_param == 0: |
| 1433 | raise NotImplementedError( |
| 1434 | "Derivative w.r.t. to the index is not supported, yet, and perhaps never will be...") |
| 1435 | else: |
| 1436 | return 2*args[0]*hermite(args[0]-1,args[1]) |
| 1437 | |
| 1438 | hermite = Func_hermite() |
| 1439 | |
| 1440 | |
| 1441 | class Func_jacobi_P(OrthogonalPolynomial): |
552 | | def laguerre(n,x): |
| 1463 | def __init__(self): |
| 1464 | OrthogonalPolynomial.__init__(self,"jacobi_P",nargs = 4, |
| 1465 | conversions =dict(maxima='jacobi_p',mathematica='JacobiP')) |
| 1466 | |
| 1467 | def _clenshaw_method_(self,*args): |
| 1468 | """ |
| 1469 | Clenshaw method for jacobi_P (means use the recursion, |
| 1470 | or sum formula) |
| 1471 | This is much faster for numerical evaluation than maxima! |
| 1472 | See A.S. 227 (p. 782) for details for the recurions. |
| 1473 | Warning: The clanshaw method for the Jacobi Polynomials |
| 1474 | should only used for exact data types, when high orders are |
| 1475 | used, due to weak instabilities of the recursion! |
| 1476 | """ |
| 1477 | k = args[0] |
| 1478 | x = args[-1] |
| 1479 | alpha = args[1] |
| 1480 | beta = args[2] |
| 1481 | |
| 1482 | if k == 0: |
| 1483 | return 1 |
| 1484 | elif k == 1: |
| 1485 | return (alpha-beta + (alpha+beta+2)*x)/2 |
| 1486 | else: |
| 1487 | |
| 1488 | if is_Expression(x) or is_Expression(alpha) or is_Expression(beta): |
| 1489 | #Here we use the sum formula of jacobi_P it seems this is rather |
| 1490 | #optimal for use. |
| 1491 | help1 = gamma(alpha+k+1)/factorial(k)/gamma(alpha+beta+k+1) |
| 1492 | help2 = 0 |
| 1493 | for j in xrange(0,k+1): |
| 1494 | help2 = help2 + binomial(k,j)*gamma(alpha+beta+k+j+1)/\ |
| 1495 | gamma(alpha+j+1)*((x-1)/2)**j |
| 1496 | return help1*help2 |
| 1497 | else: |
| 1498 | help1 = 1 |
| 1499 | help2 = (alpha-beta + (alpha+beta+2)*x)/2 |
| 1500 | |
| 1501 | for j in xrange(1,k): |
| 1502 | a1 = 2*(j+1)*(j+alpha+beta+1)*(2*j+alpha+beta) |
| 1503 | a2 = (2*j+alpha+beta+1)*(alpha**2-beta**2) |
| 1504 | a3 = (2*j+alpha+beta)*(2*j+alpha+beta+1)*(2*j+alpha+beta+2) |
| 1505 | a4 = 2*(j+alpha)*(j+beta)*(2*j+alpha+beta+2) |
| 1506 | help3 = (a2+a3*x)*help2 - a4*help1 |
| 1507 | help3 = help3/a1 |
| 1508 | help1 = help2 |
| 1509 | help2 = help3 |
| 1510 | |
| 1511 | return help3 |
| 1512 | |
| 1513 | def _maxima_init_evaled_(self, *args): |
| 1514 | """ |
| 1515 | Old maxima method. |
| 1516 | """ |
| 1517 | n = args[0] |
| 1518 | a = args[1] |
| 1519 | b = args[2] |
| 1520 | x = args[3] |
| 1521 | return sage_eval(maxima.eval('jacobi_p(%s,%s,%s,x)'%(ZZ(n),a,b)),\ |
| 1522 | locals={'x':x}) |
| 1523 | |
| 1524 | def _evalf_(self, *args,**kwds): |
| 1525 | """ |
| 1526 | Evals jacobi_P |
| 1527 | numerically with mpmath. |
| 1528 | EXAMPLES:: |
| 1529 | sage: jacobi_P(10,2,3,3).n(75) |
| 1530 | 1.322776620000000000000e8 |
| 1531 | """ |
| 1532 | try: |
| 1533 | step_parent = kwds['parent'] |
| 1534 | except KeyError: |
| 1535 | step_parent = parent(args[-1]) |
| 1536 | |
| 1537 | try: |
| 1538 | precision = step_parent.prec() |
| 1539 | except AttributeError: |
| 1540 | precision = RR.prec() |
| 1541 | |
| 1542 | from sage.libs.mpmath.all import call as mpcall |
| 1543 | from sage.libs.mpmath.all import jacobi as mpjacobi |
| 1544 | |
| 1545 | return mpcall(mpjacobi,args[0],args[1],args[2],args[-1],\ |
| 1546 | prec = precision) |
| 1547 | |
| 1548 | def _eval_special_values_(self,*args): |
| 1549 | """ |
| 1550 | Special values known. A.S. 22.4 (p.777) |
| 1551 | EXAMPLES: |
| 1552 | |
| 1553 | sage: var('n k a y') |
| 1554 | (n, k, a, y) |
| 1555 | sage: jacobi_P(n,k,a,1) |
| 1556 | binomial(k + n, n) |
| 1557 | """ |
| 1558 | if args[-1] == 1: |
| 1559 | return binomial(args[0]+args[1],args[0]) |
| 1560 | |
| 1561 | if args[-1] == -1: |
| 1562 | return (-1)**args[0]*binomial(args[0]+args[1],args[0]) |
| 1563 | |
| 1564 | if args[1] == 0 and args[2] == 0: |
| 1565 | return legendre_P(args[0],args[-1]) |
| 1566 | |
| 1567 | if args[1] == -0.5 and args[2] == -0.5: |
| 1568 | try: |
| 1569 | return binomial(2*args[0],args[0])*\ |
| 1570 | chebyshev_T(args[0],args[-1])/4**args[0] |
| 1571 | except TypeError: |
| 1572 | pass |
| 1573 | |
| 1574 | raise ValueError("Value not found") |
| 1575 | |
| 1576 | def _eval_numpy_(self, *args): |
| 1577 | """ |
| 1578 | EXAMPLES:: |
| 1579 | sage: import numpy |
| 1580 | sage: z = numpy.array([1,2]) |
| 1581 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 1582 | sage: z3 = numpy.array([1,2,3.]) |
| 1583 | sage: jacobi_P(3,2,5,z) |
| 1584 | array([ 10. , 183.25]) |
| 1585 | sage: jacobi_P(3,2,z,0) |
| 1586 | array([ -5.00000000e-01, -1.38777878e-16]) |
| 1587 | sage: jacobi_P(3,2,1,z2) |
| 1588 | array([[ 10. , 90.5], |
| 1589 | [ 10. , 90.5]]) |
| 1590 | sage: jacobi_P(3,z3,1,0) |
| 1591 | array([ 0. , -0.5, -1. ]) |
| 1592 | """ |
| 1593 | |
| 1594 | return eval_jacobi(args[0],args[1],args[2],args[-1]) |
| 1595 | |
| 1596 | def _derivative_(self, *args, **kwds): |
| 1597 | """ |
| 1598 | Returns the derivative of jacobi_P in form of jacobi_polynomials |
| 1599 | |
| 1600 | EXAMPLES:: |
| 1601 | sage: var('k a b') |
| 1602 | (k, a, b) |
| 1603 | sage: derivative(jacobi_P(k,a,b,x),x) |
| 1604 | -(2*(b + k)*(a + k)*jacobi_P(k - 1, a, b, x) - ((a + b + 2*k)*x - a + b)*k*jacobi_P(k, a, b, x))/((x^2 - 1)*(a + b + 2*k)) |
| 1605 | sage: derivative(jacobi_P(2,1,3,x),x) |
| 1606 | 14*x - 7/2 |
| 1607 | sage: derivative(jacobi_P(k,a,b,x),a) |
| 1608 | Traceback (most recent call last): |
| 1609 | ... |
| 1610 | NotImplementedError: Derivative w.r.t. to the index is not supported, yet, and perhaps never will be... |
| 1611 | |
| 1612 | """ |
| 1613 | diff_param = kwds['diff_param'] |
| 1614 | if diff_param in [0,1,2]: |
| 1615 | raise NotImplementedError( |
| 1616 | "Derivative w.r.t. to the index is not supported, yet, and perhaps never will be...") |
| 1617 | else: |
| 1618 | return (args[0]*(args[1]-args[2]-(2*args[0]+args[1]+args[2])*args[-1])*\ |
| 1619 | jacobi_P(args[0],args[1],args[2],args[-1])+ 2*(args[0]+args[1])*\ |
| 1620 | (args[0]+args[2])*jacobi_P(args[0]-1,args[1],args[2],args[-1]))/\ |
| 1621 | (2*args[0]+args[1]+args[2])/(1-args[-1]**2) |
| 1622 | |
| 1623 | |
| 1624 | jacobi_P = Func_jacobi_P() |
| 1625 | |
| 1626 | class Func_laguerre(OrthogonalPolynomial): |
573 | | def legendre_P(n,x): |
| 1680 | def _maxima_init_evaled_(self, *args): |
| 1681 | n = args[0] |
| 1682 | x = args[1] |
| 1683 | return sage_eval(maxima.eval('laguerre(%s,x)'%ZZ(n)), locals={'x':x}) |
| 1684 | |
| 1685 | def _evalf_(self, *args,**kwds): |
| 1686 | """ |
| 1687 | Evals laguerre polynomial |
| 1688 | numerically with mpmath. |
| 1689 | EXAMPLES:: |
| 1690 | sage: laguerre(3,5.).n(53) |
| 1691 | 2.66666666666667 |
| 1692 | """ |
| 1693 | try: |
| 1694 | step_parent = kwds['parent'] |
| 1695 | except KeyError: |
| 1696 | step_parent = parent(args[-1]) |
| 1697 | |
| 1698 | try: |
| 1699 | precision = step_parent.prec() |
| 1700 | except AttributeError: |
| 1701 | precision = RR.prec() |
| 1702 | |
| 1703 | from sage.libs.mpmath.all import call as mpcall |
| 1704 | from sage.libs.mpmath.all import laguerre as mplaguerre |
| 1705 | |
| 1706 | return mpcall(mplaguerre,args[0],0,args[-1],prec = precision) |
| 1707 | |
| 1708 | def _eval_special_values_(self,*args): |
| 1709 | """ |
| 1710 | Special values known. |
| 1711 | EXAMPLES: |
| 1712 | |
| 1713 | sage: var('n') |
| 1714 | n |
| 1715 | sage: laguerre(n,0) |
| 1716 | 1 |
| 1717 | """ |
| 1718 | |
| 1719 | if (args[-1] == 0): |
| 1720 | try: |
| 1721 | return 1 |
| 1722 | except TypeError: |
| 1723 | pass |
| 1724 | |
| 1725 | raise ValueError("Value not found") |
| 1726 | |
| 1727 | |
| 1728 | def _eval_numpy_(self, *args): |
| 1729 | """ |
| 1730 | EXAMPLES:: |
| 1731 | sage: import numpy |
| 1732 | sage: z = numpy.array([1,2]) |
| 1733 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 1734 | sage: z3 = numpy.array([1,2,3.]) |
| 1735 | sage: laguerre(1,z) |
| 1736 | array([ 0., -1.]) |
| 1737 | sage: laguerre(1,z2) |
| 1738 | array([[ 0., -1.], |
| 1739 | [ 0., -1.]]) |
| 1740 | sage: laguerre(1,z3) |
| 1741 | array([ 0., -1., -2.]) |
| 1742 | |
| 1743 | """ |
| 1744 | |
| 1745 | return eval_laguerre(args[0],args[1]) |
| 1746 | |
| 1747 | def _derivative_(self,*args,**kwds): |
| 1748 | """return the derivative of laguerre in |
| 1749 | form of the Laguerre Polynomial. |
| 1750 | EXAMPLES:: |
| 1751 | sage: n = var('n') |
| 1752 | sage: derivative(laguerre(3,x),x) |
| 1753 | -1/2*x^2 + 3*x - 3 |
| 1754 | sage: derivative(laguerre(n,x),x) |
| 1755 | -(n*laguerre(n - 1, x) - n*laguerre(n, x))/x |
| 1756 | """ |
| 1757 | diff_param = kwds['diff_param'] |
| 1758 | if diff_param == 0: |
| 1759 | raise NotImplementedError( |
| 1760 | "Derivative w.r.t. to the index is not supported, \ |
| 1761 | yet, and perhaps never will be...") |
| 1762 | else: |
| 1763 | return (args[0]*laguerre(args[0],args[-1])-args[0]*\ |
| 1764 | laguerre(args[0]-1,args[-1]))/args[1] |
| 1765 | |
| 1766 | laguerre = Func_laguerre() |
| 1767 | |
| 1768 | class Func_legendre_P(OrthogonalPolynomial): |
600 | | def legendre_Q(n,x): |
| 1796 | def _clenshaw_method_(self,*args): |
| 1797 | """ |
| 1798 | Clenshaw method for legendre_P (means use the recursion...) |
| 1799 | This is much faster for numerical evaluation than maxima! |
| 1800 | See A.S. 227 (p. 782) for details for the recurions. |
| 1801 | Warning: The clanshaw method for the Legendre Polynomials |
| 1802 | should only used for exact data types, when high orders are |
| 1803 | used, due to weak instabilities of the recursion! |
| 1804 | """ |
| 1805 | k = args[0] |
| 1806 | x = args[-1] |
| 1807 | |
| 1808 | if k == 0: |
| 1809 | return 1 |
| 1810 | elif k == 1: |
| 1811 | return x |
| 1812 | else: |
| 1813 | help1 = 1 |
| 1814 | help2 = x |
| 1815 | if is_Expression(x): |
| 1816 | #raise NotImplementedError("Maxima is faster here...") |
| 1817 | help1 = ZZ(2**k) #Workarround because of segmentation fault... |
| 1818 | help3 = 0 |
| 1819 | for j in xrange(0,floor(k/2)+1): |
| 1820 | help3 = help3 + (-1)**j*x**(k-2*j)*binomial(k,j)*\ |
| 1821 | binomial(2*(k-j),k) |
| 1822 | |
| 1823 | help3 = help3/help1 |
| 1824 | else: |
| 1825 | for j in xrange(1,k): |
| 1826 | help3 = (2*j+1)*x*help2 - j*help1 |
| 1827 | help3 = help3/(j+1) |
| 1828 | help1 = help2 |
| 1829 | help2 = help3 |
| 1830 | |
| 1831 | return help3 |
| 1832 | |
| 1833 | def _maxima_init_evaled_(self, *args): |
| 1834 | n = args[0] |
| 1835 | x = args[1] |
| 1836 | return sage_eval(maxima.eval('legendre_p(%s,x)'%ZZ(n)),\ |
| 1837 | locals={'x':x}) |
| 1838 | |
| 1839 | def _evalf_(self, *args,**kwds): |
| 1840 | """ |
| 1841 | Evals legendre_P |
| 1842 | numerically with mpmath. |
| 1843 | EXAMPLES:: |
| 1844 | sage: legendre_P(10,3).n(75) |
| 1845 | 8.097453000000000000000e6 |
| 1846 | """ |
| 1847 | try: |
| 1848 | step_parent = kwds['parent'] |
| 1849 | except KeyError: |
| 1850 | step_parent = parent(args[-1]) |
| 1851 | |
| 1852 | try: |
| 1853 | precision = step_parent.prec() |
| 1854 | except AttributeError: |
| 1855 | precision = RR.prec() |
| 1856 | |
| 1857 | from sage.libs.mpmath.all import call as mpcall |
| 1858 | from sage.libs.mpmath.all import legendre as mplegendre |
| 1859 | |
| 1860 | return mpcall( |
| 1861 | mplegendre,args[0],args[-1],prec = precision) |
| 1862 | |
| 1863 | |
| 1864 | def _eval_special_values_(self,*args): |
| 1865 | """ |
| 1866 | Special values known. |
| 1867 | EXAMPLES: |
| 1868 | |
| 1869 | sage: var('n') |
| 1870 | n |
| 1871 | sage: legendre_P(n,1) |
| 1872 | 1 |
| 1873 | sage: legendre_P(n,-1) |
| 1874 | (-1)^n |
| 1875 | """ |
| 1876 | if args[-1] == 1: |
| 1877 | return 1 |
| 1878 | |
| 1879 | if args[-1] == -1: |
| 1880 | return (-1)**args[0] |
| 1881 | |
| 1882 | if (args[-1] == 0): |
| 1883 | try: |
| 1884 | return (1+(-1)**args[0])/2*binomial(args[0],args[0]/2)/4**(args[0]/2) |
| 1885 | except TypeError: |
| 1886 | pass |
| 1887 | |
| 1888 | raise ValueError("Value not found") |
| 1889 | |
| 1890 | def _eval_numpy_(self, *args): |
| 1891 | """ |
| 1892 | EXAMPLES:: |
| 1893 | sage: import numpy |
| 1894 | sage: z = numpy.array([1,2]) |
| 1895 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 1896 | sage: z3 = numpy.array([1,2,3.]) |
| 1897 | sage: legendre_P(1,z) |
| 1898 | array([ 1., 2.]) |
| 1899 | sage: legendre_P(1,z2) |
| 1900 | array([[ 1., 2.], |
| 1901 | [ 1., 2.]]) |
| 1902 | sage: legendre_P(1,z3) |
| 1903 | array([ 1., 2., 3.]) |
| 1904 | sage: legendre_P(z3,3) |
| 1905 | array([ 3., 13., 63.]) |
| 1906 | |
| 1907 | """ |
| 1908 | |
| 1909 | return eval_legendre(args[0],args[1]) |
| 1910 | |
| 1911 | def _derivative_(self,*args,**kwds): |
| 1912 | """return the derivative of legendre_P in |
| 1913 | form of the Legendre Polynomial. |
| 1914 | EXAMPLES:: |
| 1915 | sage: n = var('n') |
| 1916 | sage: derivative(legendre_P(n,x),x) |
| 1917 | (n*x*legendre_P(n, x) - n*legendre_P(n - 1, x))/(x^2 - 1) |
| 1918 | sage: derivative(legendre_P(3,x),x) |
| 1919 | 15/2*x^2 - 3/2 |
| 1920 | """ |
| 1921 | diff_param = kwds['diff_param'] |
| 1922 | if diff_param == 0: |
| 1923 | raise NotImplementedError( |
| 1924 | "Derivative w.r.t. to the index is not supported, yet, and perhaps never will be...") |
| 1925 | else: |
| 1926 | return (args[0]*legendre_P(args[0]-1,args[-1])-args[0]*args[-1]*\ |
| 1927 | legendre_P(args[0],args[-1]))/(1-args[-1]**2) |
| 1928 | |
| 1929 | |
| 1930 | legendre_P = Func_legendre_P() |
| 1931 | |
| 1932 | class Func_legendre_Q(OrthogonalPolynomial): |
622 | | def ultraspherical(n,a,x): |
623 | | """ |
624 | | Returns the ultraspherical (or Gegenbauer) polynomial for integers |
625 | | `n > -1`. |
| 1957 | def _maxima_init_evaled_(self, *args): |
| 1958 | """ |
| 1959 | Maxima seems just fine for legendre Q. So we use it here! |
| 1960 | """ |
| 1961 | n = args[0] |
| 1962 | x = args[1] |
| 1963 | return sage_eval(maxima.eval('legendre_q(%s,x)'%ZZ(n)),\ |
| 1964 | locals={'x':x}) |
| 1965 | |
| 1966 | def _clenshaw_method_(self,*args): |
| 1967 | """ |
| 1968 | Clenshaw method for legendre_q (means use the recursion...) |
| 1969 | This is much faster for numerical evaluation than maxima! |
| 1970 | See A.S. 8.5.3 (p. 334) for details for the recurions. |
| 1971 | Warning: The clanshaw method for the Legendre fUNCTIONS |
| 1972 | should only used for exact data types, when high orders are |
| 1973 | used, due to weak instabilities of the recursion! |
| 1974 | """ |
| 1975 | raise NotImplementedError("Function not ready yet...") |
| 1976 | |
| 1977 | k = args[0] |
| 1978 | x = args[-1] |
| 1979 | |
| 1980 | if k == 0: |
| 1981 | return ln((1+x)/(1-x))/2 |
| 1982 | elif k == 1: |
| 1983 | return x/2*ln((1+x)/(1-x))-1 |
| 1984 | else: |
| 1985 | if is_Expression(x): |
| 1986 | raise NotImplementedError("Maxima works fine here!") |
| 1987 | #it seems that the old method just works fine here... |
| 1988 | #raise NotImplementedError("clenshaw does not work well...") |
| 1989 | else: |
| 1990 | help1 = ln((1+x)/(1-x))/2 |
| 1991 | help2 = x/2*ln((1+x)/(1-x))-1 |
| 1992 | |
| 1993 | for j in xrange(1,k): |
| 1994 | help3 = (2*j+1)*x*help2 - j*help1 |
| 1995 | help3 = help3/(j+1) |
| 1996 | help1 = help2 |
| 1997 | help2 = help3 |
| 1998 | |
| 1999 | return help3 |
| 2000 | |
| 2001 | def _eval_special_values_(self,*args): |
| 2002 | """ |
| 2003 | Special values known. |
| 2004 | EXAMPLES: |
| 2005 | |
| 2006 | sage: var('n') |
| 2007 | n |
| 2008 | sage: legendre_Q(n,0) |
| 2009 | -1/2*sqrt(pi)*gamma(1/2*n + 1/2)*sin(1/2*pi*n)/gamma(1/2*n + 1) |
| 2010 | """ |
| 2011 | if args[-1] == 1: |
| 2012 | return NaN |
| 2013 | |
| 2014 | if args[-1] == -1: |
| 2015 | return NaN |
| 2016 | |
| 2017 | if (args[-1] == 0): |
| 2018 | if is_Expression(args[0]): |
| 2019 | try: |
| 2020 | return -(sqrt(SR.pi()))/2*sin(SR.pi()/2*args[0])*\ |
| 2021 | gamma((args[0]+1)/2)/gamma(args[0]/2 + 1) |
| 2022 | except TypeError: |
| 2023 | pass |
| 2024 | else: |
| 2025 | return -(sqrt(math.pi))/2*sin(math.pi/2*args[0])*\ |
| 2026 | gamma((args[0]+1)/2)/gamma(args[0]/2. + 1) |
| 2027 | |
| 2028 | raise ValueError("Value not found") |
| 2029 | |
| 2030 | def _evalf_(self, *args,**kwds): |
| 2031 | """ |
| 2032 | Evals legendre_Q |
| 2033 | numerically with mpmath. |
| 2034 | EXAMPLES:: |
| 2035 | sage: legendre_Q(10,3).n(75) |
| 2036 | 2.079454941572578263731e-9 + 1.271944942879431601408e7*I |
| 2037 | sage: legendre_Q(3, 0.5).n(53) |
| 2038 | -0.198654771479482 |
| 2039 | """ |
| 2040 | |
| 2041 | try: |
| 2042 | step_parent = kwds['parent'] |
| 2043 | except KeyError: |
| 2044 | step_parent = parent(args[-1]) |
| 2045 | |
| 2046 | try: |
| 2047 | precision = step_parent.prec() |
| 2048 | except AttributeError: |
| 2049 | precision = RR.prec() |
| 2050 | |
| 2051 | from sage.libs.mpmath.all import call as mpcall |
| 2052 | from sage.libs.mpmath.all import legenq as mplegenq |
| 2053 | |
| 2054 | return conjugate( |
| 2055 | mpcall(mplegenq,args[0],0,args[-1],prec = precision)) |
| 2056 | #it seems that mpmath uses here a different branch of the logarithm |
| 2057 | |
| 2058 | def _eval_numpy_(self, *args): |
| 2059 | #TODO: numpy_eval with help of the a newer scipy version!!!! |
| 2060 | #Reason scipy supports stable and fast numerical evaluation |
| 2061 | #of ortho polys, but not the current version! |
| 2062 | #Now this only evaluates the array pointwise, and only the first one... |
| 2063 | #if isinstance(arg[0], numpy.ndarray). This is a hack to provide compability! |
| 2064 | """ |
| 2065 | EXAMPLES:: |
| 2066 | sage: import numpy |
| 2067 | sage: z = numpy.array([1,2]) |
| 2068 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 2069 | sage: z3 = numpy.array([1,2,3.]) |
| 2070 | sage: legendre_Q(1,z/5.) |
| 2071 | array([-0.95945349, -0.83054043]) |
| 2072 | sage: legendre_Q(1,z2/5.) |
| 2073 | array([[-0.95945349, -0.83054043], |
| 2074 | [-0.95945349, -0.83054043]]) |
| 2075 | sage: legendre_Q(1,z3/5.) |
| 2076 | array([-0.95945349, -0.83054043, -0.58411169]) |
| 2077 | """ |
| 2078 | #The imports are made here because this isn't optimal anyway |
| 2079 | from numpy import ndarray, zeros |
| 2080 | |
| 2081 | if isinstance(args[0],ndarray): |
| 2082 | raise NotImplementedError( |
| 2083 | "Support for numpy array in first argument(s) is not supported yet!") |
| 2084 | |
| 2085 | result = zeros(args[-1].shape).tolist() |
| 2086 | if isinstance(args[-1][0],int): |
| 2087 | for k in xrange(len(args[-1])): |
| 2088 | result[k] = legendre_Q(args[0],ZZ(args[-1][k])) |
| 2089 | |
| 2090 | if isinstance(args[-1][0],float): |
| 2091 | for k in xrange(len(args[-1])): |
| 2092 | result[k] = legendre_Q(args[0],RR(args[-1][k])) |
| 2093 | |
| 2094 | if isinstance(args[-1][0],ndarray): |
| 2095 | for k in xrange(len(args[-1])): |
| 2096 | result[k] = legendre_Q(args[0],args[-1][k]) |
| 2097 | |
| 2098 | return nparray(result) |
| 2099 | |
| 2100 | def _derivative_(self,*args,**kwds): |
| 2101 | """return the derivative of legendre_Q in |
| 2102 | form of the Legendre Function. |
| 2103 | EXAMPLES:: |
| 2104 | n = var('n') |
| 2105 | derivative(legendre_Q(n,x),x) |
| 2106 | (n*x*legendre_Q(n, x) - n*legendre_Q(n - 1, x))/(x^2 - 1) |
| 2107 | sage: derivative(legendre_Q(3,x),x) |
| 2108 | 5/4*(x - 1)*(1/(x - 1) - (x + 1)/(x - 1)^2)*x^3/(x + 1) + 15/4*x^2*log(-(x + 1)/(x - 1)) - 3/4*(x - 1)*(1/(x - 1) - (x + 1)/(x - 1)^2)*x/(x + 1) - 5*x - 3/4*log(-(x + 1)/(x - 1)) |
| 2109 | """ |
| 2110 | diff_param = kwds['diff_param'] |
| 2111 | if diff_param == 0: |
| 2112 | raise NotImplementedError( |
| 2113 | "Derivative w.r.t. to the index is not supported, yet, and perhaps never will be...") |
| 2114 | else: |
| 2115 | return (args[0]*args[-1]*legendre_Q(args[0],args[-1])-args[0]*\ |
| 2116 | legendre_Q(args[0]-1,args[-1]))/(args[-1]**2-1) |
| 2117 | |
| 2118 | legendre_Q = Func_legendre_Q() |
| 2119 | |
| 2120 | |
| 2121 | class Func_gen_legendre_P(OrthogonalPolynomial): |
| 2122 | |
| 2123 | def __init__(self): |
| 2124 | OrthogonalPolynomial.__init__(self,"gen_legendre_P",nargs = 3, |
| 2125 | conversions =dict(maxima='assoc_legendre_p',mathematica='LegendreP')) |
| 2126 | |
| 2127 | def _evalf_(self, *args,**kwds): |
| 2128 | """ |
| 2129 | Evals gen_legendre_P |
| 2130 | numerically with mpmath. |
| 2131 | EXAMPLES:: |
| 2132 | sage: gen_legendre_P(10,2,3).n(75) |
| 2133 | -7.194963600000000000000e8 |
| 2134 | """ |
| 2135 | |
| 2136 | try: |
| 2137 | step_parent = kwds['parent'] |
| 2138 | except KeyError: |
| 2139 | step_parent = parent(args[-1]) |
| 2140 | |
| 2141 | try: |
| 2142 | precision = step_parent.prec() |
| 2143 | except AttributeError: |
| 2144 | precision = RR.prec() |
| 2145 | |
| 2146 | from sage.libs.mpmath.all import call as mpcall |
| 2147 | from sage.libs.mpmath.all import legenp as mplegenp |
| 2148 | |
| 2149 | return mpcall( |
| 2150 | mplegenp,args[0],args[1],args[-1],prec = precision) |
| 2151 | |
| 2152 | def _eval_special_values_(self,*args): |
| 2153 | """ |
| 2154 | Special values known. |
| 2155 | EXAMPLES: |
| 2156 | |
| 2157 | sage: n, m = var('n m') |
| 2158 | sage: gen_legendre_P(n,m,0) |
| 2159 | 2^m*gamma(1/2*m + 1/2*n + 1/2)*cos(1/2*(m + n)*pi)/(sqrt(pi)*gamma(-1/2*m + 1/2*n + 1)) |
| 2160 | """ |
| 2161 | |
| 2162 | if args[1] == 0: |
| 2163 | return legendre_P(args[0],args[-1]) |
| 2164 | |
| 2165 | if (args[-1] == 0): |
| 2166 | if is_Expression(args[0]): |
| 2167 | try: |
| 2168 | return cos(SR.pi()/2*(args[0]+args[1]))/(sqrt(SR.pi()))*\ |
| 2169 | gamma((args[0]+args[1]+1)/2)/\ |
| 2170 | gamma((args[0]-args[1])/2 + 1)*2**(args[1]) |
| 2171 | except TypeError: |
| 2172 | pass |
| 2173 | else: |
| 2174 | return cos(math.pi/2*(args[0]+args[1]))/(sqrt(math.pi))*\ |
| 2175 | gamma((args[0]+args[1]+1)/2)/\ |
| 2176 | gamma((args[0]-args[1])/2. + 1)*2**args[1] |
| 2177 | |
| 2178 | raise ValueError("Value not found") |
| 2179 | |
| 2180 | def _maxima_init_evaled_(self, *args): |
| 2181 | n = args[0] |
| 2182 | m = args[1] |
| 2183 | x = args[2] |
| 2184 | if is_Expression(n) or is_Expression(m): |
| 2185 | return None |
| 2186 | |
| 2187 | from sage.functions.all import sqrt |
627 | | Computed using Maxima. |
| 2189 | if m.mod(2).is_zero() or m.is_one(): |
| 2190 | return sage_eval(maxima.eval('assoc_legendre_p(%s,%s,x)'\ |
| 2191 | %(ZZ(n),ZZ(m))), locals={'x':x}) |
| 2192 | else: |
| 2193 | return sqrt(1-x**2)*(((n-m+1)*x*gen_legendre_P(n,m-1,x)-\ |
| 2194 | (n+m-1)*gen_legendre_P(n-1,m-1,x))/(1-x**2)) |
| 2195 | |
| 2196 | |
| 2197 | def _eval_numpy_(self, *args): |
| 2198 | """ |
| 2199 | EXAMPLES:: |
| 2200 | sage: import numpy |
| 2201 | sage: z = numpy.array([1,2]) |
| 2202 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 2203 | sage: z3 = numpy.array([1,2,3.]) |
| 2204 | sage: gen_legendre_P(1,1,z/5.) |
| 2205 | array([-0.9797959 , -0.91651514]) |
| 2206 | sage: gen_legendre_P(1,1,z2/5.) |
| 2207 | array([[-0.9797959 , -0.91651514], |
| 2208 | [-0.9797959 , -0.91651514]]) |
| 2209 | sage: gen_legendre_P(1,1,z3/5.) |
| 2210 | array([-0.9797959 , -0.91651514, -0.8 ]) |
| 2211 | """ |
| 2212 | #in scipy arguments 0,1 are in reverse order |
| 2213 | return lpmv(args[1],args[0],args[-1]) |
| 2214 | |
| 2215 | |
| 2216 | def _derivative_(self,*args,**kwds): |
| 2217 | """return the derivative of gen_legendre_P in |
| 2218 | form of the Legendre Function. |
| 2219 | EXAMPLES:: |
| 2220 | sage: n,m = var('n m') |
| 2221 | sage: derivative(gen_legendre_P(n,m,x),x) |
| 2222 | (n*x*gen_legendre_P(n, m, x) - (m + n)*gen_legendre_P(n - 1, m, x))/(x^2 - 1) |
| 2223 | sage: derivative(gen_legendre_P(3,1,x),x) |
| 2224 | -15*sqrt(-x^2 + 1)*x + 3/2*(5*(x - 1)^2 + 10*x - 6)*x/sqrt(-x^2 + 1) |
| 2225 | """ |
| 2226 | diff_param = kwds['diff_param'] |
| 2227 | if diff_param in [0,1]: |
| 2228 | raise NotImplementedError( |
| 2229 | "Derivative w.r.t. to the index is not supported,\ |
| 2230 | yet, and perhaps never will be...") |
| 2231 | else: |
| 2232 | return (args[0]*args[-1]*gen_legendre_P(args[0],args[1],args[-1])\ |
| 2233 | -(args[0]+args[1])*\ |
| 2234 | gen_legendre_P(args[0]-1,args[1],args[-1]))/(args[-1]**2-1) |
| 2235 | |
| 2236 | gen_legendre_P = Func_gen_legendre_P() |
| 2237 | |
| 2238 | class Func_gen_legendre_Q(OrthogonalPolynomial): |
| 2239 | |
| 2240 | def __init__(self): |
| 2241 | OrthogonalPolynomial.__init__(self,"gen_legendre_Q",nargs = 3, |
| 2242 | conversions =dict(maxima='assoc_legendre_q',mathematica='LegendreQ')) |
| 2243 | |
| 2244 | def _evalf_(self, *args,**kwds): |
| 2245 | """ |
| 2246 | Evals gen_legendre_Q |
| 2247 | numerically with mpmath. |
| 2248 | EXAMPLES:: |
| 2249 | sage: gen_legendre_Q(10,2,3).n(75) |
| 2250 | -2.773909528741569374688e-7 - 1.130182239430298584113e9*I |
| 2251 | """ |
| 2252 | |
| 2253 | try: |
| 2254 | step_parent = kwds['parent'] |
| 2255 | except KeyError: |
| 2256 | step_parent = parent(args[-1]) |
| 2257 | |
| 2258 | try: |
| 2259 | precision = step_parent.prec() |
| 2260 | except AttributeError: |
| 2261 | precision = RR.prec() |
| 2262 | |
| 2263 | from sage.libs.mpmath.all import call as mpcall |
| 2264 | from sage.libs.mpmath.all import legenq as mplegenq |
| 2265 | |
| 2266 | return mpcall( |
| 2267 | mplegenq,args[0],args[1],args[-1],prec = precision) |
| 2268 | |
| 2269 | def _eval_special_values_(self,*args): |
| 2270 | """ |
| 2271 | Special values known. |
| 2272 | EXAMPLES: |
| 2273 | |
| 2274 | sage: n, m = var('n m') |
| 2275 | sage: gen_legendre_Q(n,m,0) |
| 2276 | -sqrt(pi)*2^(m - 1)*gamma(1/2*m + 1/2*n + 1/2)*sin(1/2*(m + n)*pi)/gamma(-1/2*m + 1/2*n + 1) |
| 2277 | """ |
| 2278 | |
| 2279 | if args[1] == 0: |
| 2280 | return legendre_Q(args[0],args[-1]) |
| 2281 | |
| 2282 | if (args[-1] == 0): |
| 2283 | if is_Expression(args[0]): |
| 2284 | try: |
| 2285 | return -(sqrt(SR.pi()))*sin(SR.pi()/2*(args[0]+args[1]))\ |
| 2286 | *gamma((args[0]+args[1]+1)/2)/\ |
| 2287 | gamma((args[0]-args[1])/2 + 1)*2**(args[1]-1) |
| 2288 | except TypeError: |
| 2289 | pass |
| 2290 | else: |
| 2291 | return -(sqrt(math.pi))/2*sin(math.pi/2*(args[0]+args[1]))\ |
| 2292 | *gamma((args[0]+args[1]+1)/2)/\ |
| 2293 | gamma((args[0]-args[1])/2. + 1)*2**args[1] |
| 2294 | |
| 2295 | raise ValueError("Value not found") |
| 2296 | |
| 2297 | def _maxima_init_evaled_(self, *args): |
| 2298 | n = args[0] |
| 2299 | m = args[1] |
| 2300 | x = args[2] |
| 2301 | if is_Expression(n) or is_Expression(m): |
| 2302 | return None |
| 2303 | |
| 2304 | from sage.functions.all import sqrt |
649 | | gegenbauer = ultraspherical |
| 2323 | def _eval_numpy_(self, *args): |
| 2324 | #TODO: numpy_eval with help of a new scipy version!!!! |
| 2325 | #Now this only evaluates the array pointwise, and only the first one... |
| 2326 | #if isinstance(arg[0], numpy.ndarray). |
| 2327 | #This is a hack to provide compability with older releases of sage! |
| 2328 | """ |
| 2329 | EXAMPLES:: |
| 2330 | sage: import numpy |
| 2331 | sage: z = numpy.array([1,2]) |
| 2332 | sage: z2 = numpy.array([[1,2],[1,2]]) |
| 2333 | sage: z3 = numpy.array([1,2,3.]) |
| 2334 | sage: gen_legendre_Q(1,1,z/5.) |
| 2335 | array([-0.40276067, -0.82471644]) |
| 2336 | sage: gen_legendre_Q(1,1,z2/5.) |
| 2337 | array([[-0.40276067, -0.82471644], |
| 2338 | [-0.40276067, -0.82471644]]) |
| 2339 | sage: gen_legendre_Q(1,1,z3/5.) |
| 2340 | array([-0.40276067, -0.82471644, -1.30451774]) |
| 2341 | """ |
| 2342 | #imports are included here, because this isn't optimal anyway |
| 2343 | from numpy import ndarray, zeros |
| 2344 | |
| 2345 | if isinstance(args[0],ndarray) or isinstance(args[1],ndarray): |
| 2346 | raise NotImplementedError( |
| 2347 | "Support for numpy array in first argument(s) is not supported yet!") |
| 2348 | |
| 2349 | result = zeros(args[-1].shape).tolist() |
| 2350 | if isinstance(args[-1][0],int): |
| 2351 | for k in xrange(len(args[-1])): |
| 2352 | result[k] = gen_legendre_Q(args[0],args[1],ZZ(args[-1][k])) |
| 2353 | |
| 2354 | if isinstance(args[-1][0],float): |
| 2355 | for k in xrange(len(args[-1])): |
| 2356 | result[k] = gen_legendre_Q(args[0],args[1],RR(args[-1][k])) |
| 2357 | |
| 2358 | if isinstance(args[-1][0],ndarray): |
| 2359 | for k in xrange(len(args[-1])): |
| 2360 | result[k] = gen_legendre_Q(args[0],args[1],args[-1][k]) |
| 2361 | |
| 2362 | return nparray(result) |
| 2363 | |
| 2364 | def _derivative_(self,*args,**kwds): |
| 2365 | """return the derivative of gen_legendre_Q in |
| 2366 | form of the Legendre Function. |
| 2367 | EXAMPLES:: |
| 2368 | sage: n,m = var('n m') |
| 2369 | sage: derivative(gen_legendre_Q(n,m,x),x) |
| 2370 | (n*x*gen_legendre_Q(n, m, x) - (m + n)*gen_legendre_Q(n - 1, m, x))/(x^2 - 1) |
| 2371 | sage: derivative(gen_legendre_Q(0,1,x),x) |
| 2372 | -x/(-x^2 + 1)^(3/2) |
| 2373 | """ |
| 2374 | diff_param = kwds['diff_param'] |
| 2375 | if diff_param in [0,1]: |
| 2376 | raise NotImplementedError("Derivative w.r.t. to the index \ |
| 2377 | is not supported, yet, and perhaps never will be...") |
| 2378 | else: |
| 2379 | return (args[0]*args[-1]*gen_legendre_Q(args[0],args[1],args[-1])\ |
| 2380 | -(args[0]+args[1])*\ |
| 2381 | gen_legendre_Q(args[0]-1,args[1],args[-1]))/(args[-1]**2-1) |
| 2382 | |
| 2383 | |
| 2384 | gen_legendre_Q = Func_gen_legendre_Q() |
| 2385 | |
| 2386 | |
| 2387 | |
| 2388 | |
| 2389 | |
| 2390 | |
| 2391 | |