
    MZdr                       d Z ddlmZmZmZ ddlmZmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZmZmZ dd	lmZmZmZ dd
lmZm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZG ddlHmIZI ddlJmKZK ddlLmMZM ddlNmOZO ddlPmQZQ dd lRmSZSmTZTmUZU dd!lVmWZWmXZXmYZYmZZZ d" Z[d# Z\ G d$ d%      Z] G d& d'      Z^ G d( d)      Z_d=d+Z`dd*d,e<fd-Za ed.      Zbd/acd/addd0lemfZf d>d1Zgd?d2Zhd3 Zid4 Zjd5 Zkd6 Zld7 Zmd@d8Zndd/d/e<d,fd9Zod,e<fd:Zpe<fd;Zqd< Zry/)AzL
This module implements Holonomic Functions and
various operations on them.
    )AddMulPow)NaNInfinityNegativeInfinityFloatIpiequal_valued)S)ordered)DummySymbol)sympify)binomial	factorialrf)	exp_polarexplog)coshsinh)sqrt)cossinsinc)CiShiSierferfcerfi)gamma)hypermeijerg)	meijerint)Matrix)PolyElement)FracElement)QQRR)DMF)roots)Poly)DomainMatrix)sstr)limit)Order)hyperexpand)	nsimplify)solve   )HolonomicSequenceRecurrenceOperatorRecurrenceOperators)NotPowerSeriesErrorNotHyperSeriesErrorSingularityErrorNotHolonomicErrorc                       fd} j                  |      \  }}|j                  d   } |d|f      |z  }||z   j                         }|S )Nc                 D    t        j                  | j                        S N)r0   onesdomain)shapers    ;/usr/lib/python3/dist-packages/sympy/holonomic/holonomic.py<lambda>z(_find_nonzero_solution.<locals>.<lambda>+   s    **5!((;     r   r7   )_solverD   	transpose)rE   homosysrB   
particular	nullspacenullitynullpartsols   `       rF   _find_nonzero_solutionrQ   *   sT    ;DHHW-J	ooa GQL!I-H 
+
+
-CJrH   c                 6    t        | |      }||j                  fS )a  
    This function is used to create annihilators using ``Dx``.

    Explanation
    ===========

    Returns an Algebra of Differential Operators also called Weyl Algebra
    and the operator for differentiation i.e. the ``Dx`` operator.

    Parameters
    ==========

    base:
        Base polynomial ring for the algebra.
        The base polynomial ring is the ring of polynomials in :math:`x` that
        will appear as coefficients in the operators.
    generator:
        Generator of the algebra which can
        be either a noncommutative ``Symbol`` or a string. e.g. "Dx" or "D".

    Examples
    ========

    >>> from sympy import ZZ
    >>> from sympy.abc import x
    >>> from sympy.holonomic.holonomic import DifferentialOperators
    >>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    >>> R
    Univariate Differential Operator Algebra in intermediate Dx over the base ring ZZ[x]
    >>> Dx*x
    (1) + (x)*Dx
    )DifferentialOperatorAlgebraderivative_operator)base	generatorrings      rF   DifferentialOperatorsrX   4   s"    D 'tY7D$**++rH   c                   &    e Zd ZdZd Zd ZeZd Zy)rS   a  
    An Ore Algebra is a set of noncommutative polynomials in the
    intermediate ``Dx`` and coefficients in a base polynomial ring :math:`A`.
    It follows the commutation rule:

    .. math ::
       Dxa = \sigma(a)Dx + \delta(a)

    for :math:`a \subset A`.

    Where :math:`\sigma: A \Rightarrow A` is an endomorphism and :math:`\delta: A \rightarrow A`
    is a skew-derivation i.e. :math:`\delta(ab) = \delta(a) b + \sigma(a) \delta(b)`.

    If one takes the sigma as identity map and delta as the standard derivation
    then it becomes the algebra of Differential Operators also called
    a Weyl Algebra i.e. an algebra whose elements are Differential Operators.

    This class represents a Weyl Algebra and serves as the parent ring for
    Differential Operators.

    Examples
    ========

    >>> from sympy import ZZ
    >>> from sympy import symbols
    >>> from sympy.holonomic.holonomic import DifferentialOperators
    >>> x = symbols('x')
    >>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    >>> R
    Univariate Differential Operator Algebra in intermediate Dx over the base ring
    ZZ[x]

    See Also
    ========

    DifferentialOperator
    c                     || _         t        |j                  |j                  g|       | _        |t        dd      | _        y t        |t              rt        |d      | _        y t        |t
              r|| _        y y )NDxF)commutative)	rU   DifferentialOperatorzeroonerT   r   
gen_symbol
isinstancestr)selfrU   rV   s      rF   __init__z$DifferentialOperatorAlgebra.__init__   sl    	#7YY!4$)  $Tu=DO)S)"("FIv."+ /rH   c                 r    dt        | j                        z   dz   | j                  j                         z   }|S )Nz9Univariate Differential Operator Algebra in intermediate z over the base ring )r1   r`   rU   __str__)rc   strings     rF   rf   z#DifferentialOperatorAlgebra.__str__   s<    L4??#$&<=YY!" rH   c                 j    | j                   |j                   k(  r| j                  |j                  k(  ryyNTF)rU   r`   rc   others     rF   __eq__z"DifferentialOperatorAlgebra.__eq__   s)    99

"t%:J:J'JrH   N)__name__
__module____qualname____doc__rd   rf   __repr__rl    rH   rF   rS   rS   Z   s    $L, HrH   rS   c                   d    e Zd ZdZdZd Zd Zd Zd ZeZ	d Z
d Zd	 Zd
 Zd Zd ZeZd Zd Zy)r]   a  
    Differential Operators are elements of Weyl Algebra. The Operators
    are defined by a list of polynomials in the base ring and the
    parent ring of the Operator i.e. the algebra it belongs to.

    Explanation
    ===========

    Takes a list of polynomials for each power of ``Dx`` and the
    parent ring which must be an instance of DifferentialOperatorAlgebra.

    A Differential Operator can be created easily using
    the operator ``Dx``. See examples below.

    Examples
    ========

    >>> from sympy.holonomic.holonomic import DifferentialOperator, DifferentialOperators
    >>> from sympy import ZZ
    >>> from sympy import symbols
    >>> x = symbols('x')
    >>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')

    >>> DifferentialOperator([0, 1, x**2], R)
    (1)*Dx + (x**2)*Dx**2

    >>> (x*Dx*x + 1 - Dx**2)**2
    (2*x**2 + 2*x + 1) + (4*x**3 + 2*x**2 - 4)*Dx + (x**4 - 6*x - 2)*Dx**2 + (-2*x**2)*Dx**3 + (1)*Dx**4

    See Also
    ========

    DifferentialOperatorAlgebra
       c                    || _         | j                   j                  }t        |j                  d   t              r|j                  d   n|j                  d   d   | _        t        |      D ]\  \  }}t        ||j                        s|j                  t        |            ||<   :|j                  |j                  |            ||<   ^ || _        t        | j                        dz
  | _        y)z
        Parameters
        ==========

        list_of_poly:
            List of polynomials belonging to the base ring of the algebra.
        parent:
            Parent algebra of the operator.
        r   r7   N)parentrU   ra   gensr   x	enumeratedtype
from_sympyr   to_sympy
listofpolylenorder)rc   list_of_polyrv   rU   ijs         rF   rd   zDifferentialOperator.__init__   s     {{!+DIIaL&!A1tyyQR|TU
 l+ 	DDAqa,"&//'!*"=Q"&//$--2B"CQ		D ')A-
rH   c                      j                   }t        |t              s^t        | j                  j                  j
                        s0 j                  j                  j                  t        |            g}n|g}n|j                   }d } ||d   |      } fd}t        dt        |            D ]   } ||      }t        | |||   |            }" t        | j                        S )z
        Multiplies two DifferentialOperator and returns another
        DifferentialOperator instance using the commutation rule
        Dx*a = a*Dx + a'
        c                 l    t        |t              rg }|D ]  }|j                  || z          |S | |z  gS rA   )ra   listappend)blistofotherrP   r   s       rF   _mul_dmp_diffopz5DifferentialOperator.__mul__.<locals>._mul_dmp_diffop   sB    +t,$ &AJJq1u%&
K((rH   r   c                    j                   j                  j                  g}g }t        | t              r8| D ]2  }|j                  |       |j                  |j                                4 nv|j                  j                   j                  j                  |              |j                  j                   j                  j                  |       j                                t        ||      S rA   )	rv   rU   r^   ra   r   r   diffr{   
_add_lists)r   sol1sol2r   rc   s       rF   
_mul_Dxi_bz0DifferentialOperator.__mul__.<locals>._mul_Dxi_b  s    KK$$))*DD!T" *AKKNKK)* DKK,,77:;DKK,,77:??ABdD))rH   r7   )r}   ra   r]   rv   rU   rz   r{   r   ranger~   r   )rc   rk   
listofselfr   r   rP   r   r   s   `       rF   __mul__zDifferentialOperator.__mul__   s     __
%!56eT[[%5%5%;%;<#{{//::75>JK  %g**K	) jm[9	* q#j/* 	OA$[1KS/*Q-"MNC		O $C55rH   c                 N   t        |t              st        || j                  j                  j                        s.| j                  j                  j                  t        |            }g }| j                  D ]  }|j                  ||z          t        || j                        S y rA   )	ra   r]   rv   rU   rz   r{   r   r}   r   )rc   rk   rP   r   s       rF   __rmul__zDifferentialOperator.__rmul__  s    %!56eT[[%5%5%;%;<))55genEC__ &

519%& (T[[99 7rH   c                    t        |t              r6t        | j                  |j                        }t        || j                        S | j                  }t        || j                  j
                  j                        s0| j                  j
                  j                  t        |            g}n|g}g }|j                  |d   |d   z          ||dd  z  }t        || j                        S )Nr   r7   )
ra   r]   r   r}   rv   rU   rz   r{   r   r   )rc   rk   rP   	list_self
list_others        rF   __add__zDifferentialOperator.__add__%  s    e12T__e.>.>?C'T[[99 IeT[[%5%5%;%;< $11==genMN
#W
CJJy|jm349QR= C'T[[99rH   c                     | d|z  z   S Nrr   rj   s     rF   __sub__zDifferentialOperator.__sub__9  s    rUl""rH   c                     d| z  |z   S r   rr   rj   s     rF   __rsub__zDifferentialOperator.__rsub__<  s    d{U""rH   c                     d| z  S r   rr   rc   s    rF   __neg__zDifferentialOperator.__neg__?      DyrH   c                 .    | t         j                  |z  z  S rA   r   Onerj   s     rF   __truediv__z DifferentialOperator.__truediv__B      quuu}%%rH   c                    |dk(  r| S |dk(  r5t        | j                  j                  j                  g| j                        S | j                  | j                  j
                  j                  k(  ri| j                  j                  j                  g|z  }|j                  | j                  j                  j                         t        || j                        S |dz  dk(  r| |dz
  z  }|| z  S |dz  dk(  r| |dz  z  }||z  S y )Nr7   r      )r]   rv   rU   r_   r}   rT   r^   r   )rc   nrP   	powreduces       rF   __pow__zDifferentialOperator.__pow__E  s    6K6')9)9)=)=(>LL ??dkk==HHH;;##(()!+CJJt{{''++,'T[[99 1uz 1q5M	 4''Q! 1q5M	 9,, rH   c                    | j                   }d}t        |      D ]  \  }}|| j                  j                  j                  k(  r*|dk(  r|dt        |      z   dz   z  }D|r|dz  }|dk(  r,|dt        |      z   d| j                  j                  z  z   z  }||dt        |      z   dz   d| j                  j                  z  z   t        |      z   z  } |S )	N r   ()z + r7   z)*%sz*%s**)r}   ry   rv   rU   r^   r1   r`   )rc   r}   	print_strr   r   s        rF   rf   zDifferentialOperator.__str__Z  s    __
	j) 	[DAqDKK$$)))AvS47]S00	U"	AvS47]Vdkk6L6L-MMM	tAw,w9O9O/PPSWXYSZZZI	[" rH   c                 "   t        |t              r4| j                  |j                  k(  r| j                  |j                  k(  ryy| j                  d   |k(  r9| j                  dd  D ]&  }|| j                  j                  j
                  us& y yy)NTFr   r7   )ra   r]   r}   rv   rU   r^   )rc   rk   r   s      rF   rl   zDifferentialOperator.__eq__s  s    e12%"2"22t{{ell7Rq!U*, %A 0 0 5 55$% rH   c                     | j                   j                  }|t        |j                  | j                  d         | j
                        v S )zH
        Checks if the differential equation is singular at x0.
        r   )rv   rU   r.   r|   r}   rx   )rc   x0rU   s      rF   is_singularz DifferentialOperator.is_singular  s;    
 {{U4==)<=tvvFFFrH   N)rm   rn   ro   rp   _op_priorityrd   r   r   r   __radd__r   r   r   r   r   rf   rq   rl   r   rr   rH   rF   r]   r]      s[    !F L.<36j
::$ H##&-*. HGrH   r]   c                       e Zd ZdZdZd!dZd ZeZd Zd Z	d Z
d	 Zd
 Zd"dZd Zd Zd ZeZd Zd Zd Zd Zd Zd Zd Zd#dZd#dZd$dZd Zd%dZd Zd Zd&dZ d Z!d'dZ"d  Z#y)(HolonomicFunctiona
  
    A Holonomic Function is a solution to a linear homogeneous ordinary
    differential equation with polynomial coefficients. This differential
    equation can also be represented by an annihilator i.e. a Differential
    Operator ``L`` such that :math:`L.f = 0`. For uniqueness of these functions,
    initial conditions can also be provided along with the annihilator.

    Explanation
    ===========

    Holonomic functions have closure properties and thus forms a ring.
    Given two Holonomic Functions f and g, their sum, product,
    integral and derivative is also a Holonomic Function.

    For ordinary points initial condition should be a vector of values of
    the derivatives i.e. :math:`[y(x_0), y'(x_0), y''(x_0) ... ]`.

    For regular singular points initial conditions can also be provided in this
    format:
    :math:`{s0: [C_0, C_1, ...], s1: [C^1_0, C^1_1, ...], ...}`
    where s0, s1, ... are the roots of indicial equation and vectors
    :math:`[C_0, C_1, ...], [C^0_0, C^0_1, ...], ...` are the corresponding initial
    terms of the associated power series. See Examples below.

    Examples
    ========

    >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
    >>> from sympy import QQ
    >>> from sympy import symbols, S
    >>> x = symbols('x')
    >>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')

    >>> p = HolonomicFunction(Dx - 1, x, 0, [1])  # e^x
    >>> q = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1])  # sin(x)

    >>> p + q  # annihilator of e^x + sin(x)
    HolonomicFunction((-1) + (1)*Dx + (-1)*Dx**2 + (1)*Dx**3, x, 0, [1, 2, 1])

    >>> p * q  # annihilator of e^x * sin(x)
    HolonomicFunction((2) + (-2)*Dx + (1)*Dx**2, x, 0, [0, 1])

    An example of initial conditions for regular singular points,
    the indicial equation has only one root `1/2`.

    >>> HolonomicFunction(-S(1)/2 + x*Dx, x, 0, {S(1)/2: [1]})
    HolonomicFunction((-1/2) + (x)*Dx, x, 0, {1/2: [1]})

    >>> HolonomicFunction(-S(1)/2 + x*Dx, x, 0, {S(1)/2: [1]}).to_expr()
    sqrt(x)

    To plot a Holonomic Function, one can use `.evalf()` for numerical
    computation. Here's an example on `sin(x)**2/x` using numpy and matplotlib.

    >>> import sympy.holonomic # doctest: +SKIP
    >>> from sympy import var, sin # doctest: +SKIP
    >>> import matplotlib.pyplot as plt # doctest: +SKIP
    >>> import numpy as np # doctest: +SKIP
    >>> var("x") # doctest: +SKIP
    >>> r = np.linspace(1, 5, 100) # doctest: +SKIP
    >>> y = sympy.holonomic.expr_to_holonomic(sin(x)**2/x, x0=1).evalf(r) # doctest: +SKIP
    >>> plt.plot(r, y, label="holonomic function") # doctest: +SKIP
    >>> plt.show() # doctest: +SKIP

    rt   Nc                 <    || _         || _        || _        || _        y)ap  

        Parameters
        ==========

        annihilator:
            Annihilator of the Holonomic Function, represented by a
            `DifferentialOperator` object.
        x:
            Variable of the function.
        x0:
            The point at which initial conditions are stored.
            Generally an integer.
        y0:
            The initial condition. The proper format for the initial condition
            is described in class docstring. To make the function unique,
            length of the vector `y0` should be equal to or greater than the
            order of differential equation.
        N)y0r   annihilatorrx   )rc   r   rx   r   r   s        rF   rd   zHolonomicFunction.__init__  s!    , &rH   c           
      >   | j                         r]dt        | j                        dt        | j                        dt        | j
                        dt        | j                        d	}|S dt        | j                        dt        | j                        d}|S )NzHolonomicFunction(z, r   )_have_init_condrb   r   r1   rx   r   r   )rc   str_sols     rF   rf   zHolonomicFunction.__str__  sw    !=@AQAQ=RTVVd477mT$'']<G  " 699I9I5JTVVG rH   c                 H   | j                   j                  j                  }|j                   j                  j                  }|j                  }|j                  }||k(  r| |fS |j	                  |      j                  | j                        }t        |t        | j                   j                  j                              \  }}| j                   j                  D 	cg c]  }	|j                  |	       }
}	|j                   j                  D 	cg c]  }	|j                  |	       }}	t        |
|      }
t        ||      }t        |
| j                  | j                  | j                        }
t        ||j                  |j                  |j                        }|
|fS c c}	w c c}	w )z^
        Unifies the base polynomial ring of a given two Holonomic
        Functions.
        )r   rv   rU   domunifyold_poly_ringrx   rX   rb   r`   r}   r|   r]   r   r   r   )rc   rk   R1R2dom1dom2R	newparent_r   r   r   s               rF   r   zHolonomicFunction.unify  sJ    $$))%%**vvvv8%= ZZ,,TVV4,QD4D4D4K4K4V4V0WX	1(,(8(8(C(CD1ADD(-(9(9(D(DE1AEE#D)4#D)4 tvvtww@ uww%((Cd| EEs   FFc                 p    t        | j                  t              ryt        | j                  t              ryy)z
        Returns True if the function have singular initial condition
        in the dictionary format.

        Returns False if the function have ordinary initial condition
        in the list format.

        Returns None for all other cases.
        TFN)ra   r   dictr   r   s    rF   is_singularicsz HolonomicFunction.is_singularics  s+     dggt$& 'rH   c                 ,    t        | j                        S )z@
        Checks if the function have initial condition.
        )boolr   r   s    rF   r   z!HolonomicFunction._have_init_cond'  s     DGG}rH   c           
         t        | j                        d   }| j                  |   }t        | j                        dk(  r|t        |      k(  r|dkD  rg }t        |      }t	        |      D ]!  }|j                  t        j                         # |t        |      D cg c]  \  }}|t        ||z         z   c}}z  }t        | j                  | j                  | j                  |      S yyyc c}}w )zP
        Converts a singular initial condition to ordinary if possible.
        r   r7   N)r   r   r~   intr   r   r   Zerory   r   r   r   rx   r   )rc   ar   r   r   r   s         rF   _singularics_to_ordz%HolonomicFunction._singularics_to_ord-  s     M!GGAJtww<1c!fQBAA1X "		!&&!"IaLADAq1yQ''AAB$T%5%5tvvtwwKK 27
 Bs   C.c                    | j                   j                  j                  |j                   j                  j                  k7  r| j                  |      \  }}||z   S | j                   j                  }|j                   j                  }t        ||      }| j                   j                  j                  }|j                         }| j                   g}	|j                   g}
| j                   j                  j                  }t        ||z
        D ]  }||	d   z  }|	j                  |        t        ||z
        D ]  }||
d   z  }|
j                  |        |	|
z   }g }|D ]  }g }t        |dz         D ]m  }|t        |j                        k\  r|j                  |j                         7|j                  |j                  |j                  |   j                               o |j                  |        t        |t        |      |dz   f|      j!                         }t        j"                  |dz   df|      }t%        ||      }|j&                  r4|dz  }||	d   z  }|	j                  |       ||
d   z  }|
j                  |       |	|
z   }g }|D ]  }g }t        |dz         D ]m  }|t        |j                        k\  r|j                  |j                         7|j                  |j                  |j                  |   j                               o |j                  |        t        |t        |      |dz   f|      j!                         }t        j"                  |dz   df|      }t%        ||      }|j&                  r4|j)                         d |dz   |z
   }t+        || j                   j                        }|| j                   z  }t+        |j                  | j                   j                  d      }| j-                         r|j-                         st/        || j0                        S | j3                         dk(  r|j3                         dk(  r| j4                  |j4                  k(  rot7        | |j                        }t7        ||j                        }t9        ||      D cg c]
  \  }}||z    }}}t/        || j0                  | j4                  |      S | j                   j;                  d      }|j                   j;                  d      }| j4                  dk(  r|s|s| |j=                  d      z   S |j4                  dk(  r|s|s| j=                  d      |z   S | j                   j;                  | j4                        }|j                   j;                  | j4                        }|s |s| |j=                  | j4                        z   S | j=                  |j4                        |z   S | j4                  |j4                  k7  rt/        || j0                        S d }d }| j3                         dk(  re|j3                         dk(  rRt?        | j@                        D cg c]  \  }}|tC        |      z   }}}tD        jF                  |i}|j@                  }n| j3                         dk(  re|j3                         dk(  rRt?        |j@                        D cg c]  \  }}|tC        |      z   }}}| j@                  }tD        jF                  |i}n>| j3                         dk(  r+|j3                         dk(  r| j@                  }|j@                  }i }|D ]:  }||v r,t9        ||   ||         D cg c]
  \  }}||z    c}}||<   3||   ||<   < |D ]  }||vs||   ||<    t/        || j0                  | j4                  |      S c c}}w c c}}w c c}}w c c}}w )Nr   r7   Fnegativer   T)$r   rv   rU   r   r   max	get_fieldrT   r   r   r~   r}   r^   newrepr0   rJ   zerosrQ   is_zero_matrixflat
_normalizer   r   rx   r   r   
_extend_y0zipr   
change_icsry   r   r   r   r   )rc   rk   r   r   deg1deg2dimr   Krowsself	rowsothergenr   diff1diff2rowrE   exprprK   rP   r   y1y2r   selfat0otherat0selfatx0	otheratx0r   _y0s                                  rF   r   zHolonomicFunction.__add__=  s   ""''5+<+<+C+C+H+HH::e$DAqq5L%%  &&$o##((KKM$$%&&'	%%99 sTz" 	#A8B<'EOOE"	# sTz" 	$A9R=(EU#	$ "  	DA37^ <DOO,,HHQVV$HHQUU4??1#5#9#9:;	<
 HHQK	 SXs1u-q1;;=$$c!eQZ3$Q0   1HC8B<'EOOE"9R=(EU#Y&CA sQw @AC00(tq'9'='=!>?	@
  QS3q5 115??AA"((#a%Q7G(G4C1   : hhj#'D.)#t//667d&&')9)9)@)@5Q$$&5+@+@+B$S$&&11  E)e.B.B.D.M ww%(("  cii0syy1(+B41a!e44(dffdggrBB **66q9 ,,88;77a<%"2"21"555XX]78??1-55  $//;;DGGDH % 1 1 = =dgg FI#I#e&6&6tww&???  $uxx85@@77ehh$S$&&11  E)e.B.B.D.L09$''0BC11y|#CCC&&#BB  "d*u/C/C/E/N09%((0CD11y|#DCDB&&#B  "d*u/C/C/E/MBB  	A Bw+.r!ube+<=41aQ=111	  	A{11	 !dffdggr::s 5B D E >s   +___._c           	      	   | j                   j                  j                  }| j                         dk(  r| j	                         }|r|j                  ||      S i }| j                  D ]  }| j                  |   }g }t        |      D ]c  \  }	}
|
dk(  r |j                  t        j                         +||	z   dz   dk(  rt        d      |j                  |
t        ||	z   dz         z         e |||dz   <    t        |d      rt        d      t        | j                   |z  | j                  | j                  |      S | j!                         sc|r>t        | j                   |z  | j                  | j                  t        j                  g      S t        | j                   |z  | j                        S t        |d      r9t#        |      dk(  r-|d   | j                  k(  r| j                  }|d   }|d	   }d}nd
}t        j                  g}|| j                  z  }t        | j                   |z  | j                  | j                  |      }s|S k7  r&	 |j%                         }|rI|j+                  | j                  |      }t-        |t.              r.|j1                  | j                  |      }n|j3                  |      }| j                  k(  r0|d   |z
  |d<   t        | j                   |z  | j                  ||      S t        |      j4                  re|rM|j+                  | j                  |      }t-        |t.              r|j1                  | j                  |      }||z
  S |j3                  |      }||z
  S | j                  k(  r%t        | j                   |z  | j                  ||      S t        |      j4                  r~	 t        | j                   |z  | j                  ||      j%                         }|j+                  | j                  |      }t-        |t.              s|S |j1                  | j                  |      S t        | j                   |z  | j                        S # t&        t(        f$ r d}Y w xY w# t&        t(        f$ r7 t        | j                   |z  | j                  ||      j3                  |      cY S w xY w)az  
        Integrates the given holonomic function.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import QQ
        >>> from sympy import symbols
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
        >>> HolonomicFunction(Dx - 1, x, 0, [1]).integrate((x, 0, x))  # e^x - 1
        HolonomicFunction((-1)*Dx + (1)*Dx**2, x, 0, [0, 1])
        >>> HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).integrate((x, 0, x))
        HolonomicFunction((1)*Dx + (1)*Dx**3, x, 0, [0, 1, 0])
        T)initcondr   r7   z1logarithmic terms in the series are not supported__iter__z4Definite integration for singular initial conditions   r   FN)r   rv   rT   r   r   	integrater   ry   r   r   r   NotImplementedErrorhasattrr   rx   r   r   r~   to_exprr<   r;   subsra   r   r2   evalf	is_Number)rc   limitsr   DrE   r   r   cc2r   cjr   r   r   definiteindefinite_integralindefinite_exprloweruppers
indefinites                        rF   r   zHolonomicFunction.integrate  sT   & ##77  D(((*A{{6H{== BWW GGAJ&q\ 	5EArQw		!&&) Qa12eff		"qQ|"34	5 1q5	 vz*)*`aa$T%5%5%9466477BOO ##%()9)9A)=tvvtwwQRQWQWPXYY$T%5%5%9466BB
 6:&6{aF1I$7WW1I1I HffX
dgg/0@0@10DdffdggWYZ&& 7'"5"="="? ',,TVVQ7eS)+11$&&!<E+11!4DFF{11()9)9A)=tvvr2NN1"+00;E!%- / 5 5dffa @ u}$ 055a8Eu}$ ;$T%5%5%94661bII qT^^	W%d&6&6&:DFFA	 VVDFFA.
!*c2%%774661-- !!1!1A!5tvv>>U ()<= '"&'N ()<= W()9)9A)=tvvq"MSSTUVVWs,   Q   A Q8 !Q8  Q54Q58AR>=R>c                    |j                  dd       |r[|d   | j                  k7  rt        j                  S t	        |      dk(  r+| }t        |d         D ]  }|j                  |d         } |S | j                  }|j                  d   |j                  j                  j                  k(  r|j                  dk(  rt        j                  S |j                  d   |j                  j                  j                  k(  rt        |j                  dd |j                        }| j                         rX| j                         dk(  r/t!        || j                  | j"                  | j$                  dd       S t!        || j                        S t!        || j                        S |j                  j                  }|j'                         }|j                  D cg c]  }|j)                  |j*                         }}|dd D cg c]
  }||d   z   }	}|	j-                  d|j                         t/        |	      }t1        ||j                  |j2                  g      }t5        |dd | j                  j                  d      }| j                         r| j                         dk(  rt!        || j                        S t7        | |j                  dz         dd }
t!        || j                  | j"                  |
      S c c}w c c}w )	aK  
        Differentiation of the given Holonomic function.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import ZZ
        >>> from sympy import symbols
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')
        >>> HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]).diff().to_expr()
        cos(x)
        >>> HolonomicFunction(Dx - 2, x, 0, [1]).diff().to_expr()
        2*exp(2*x)

        See Also
        ========

        integrate
        evaluateTr   r   r7   NFr   )
setdefaultrx   r   r   r~   r   r   r   r}   rv   rU   r^   r   r]   r   r   r   r   r   r   r   r   insert_derivate_diff_eqr   r_   r   r   )rc   argskwargsrP   r   annr   r   seq_dmfrhsr   s              rF   r   zHolonomicFunction.diffT  sn   , 	*d+Aw$&& vvTatAw ,A((47+C,
 >>!

 4 44a66M ^^A#**//"6"66&s~~ab'93::FC##%&&(E1,S$&&$''47712;OO(dff55(dff55 JJOOKKM),8A155<88 (/qr{3!q71:~33

1aff  $ qvvquuo.QR$"2"2"9"9EJ##%)<)<)>$)F$S$&&11cii!m,QR0 dffdggr::% 9 4s   "K1;K6c                    | j                   |j                   k(  ro| j                  |j                  k(  rU| j                         rD|j                         r4| j                  |j                  k(  r| j                  |j                  k(  ryyyyyri   )r   rx   r   r   r   rj   s     rF   rl   zHolonomicFunction.__eq__  sk    u000vv '')e.C.C.Eww%((*tww%((/B#$rH   c           	      D   | j                   }t        |t              st        |      }|j	                  | j
                        rt        d      | j                         s| S t        | |j                        }g }|D ]>  }|j                  t        j                  || j
                        |z  j                         @ t        || j
                  | j                  |      S | j                   j                  j                   |j                   j                  j                   k7  r| j#                  |      \  }}||z  S |j                   }g }	g }
|j                  }|j                  }|j                  j                   }|j%                         }|j&                  D ],  }|	j                  |j                  |j                               . |j&                  D ],  }|
j                  |j                  |j                               . t)        |      D cg c]  }|	|    |	|   z   }}t)        |      D cg c]  }|
|    |
|   z   }}t)        |dz         D cg c](  }t)        |dz         D cg c]  }|j*                   c}* }}}|j,                  |d   d<   t)        |      D cg c]  }t)        |      D ]
  }||   |     c}}g}t/        |d||z  f|      j1                         }t/        j2                  ||z  df|      }t5        ||      }|j6                  rt)        |dz
  dd      D ]  }t)        |dz
  dd      D ]  }||   |dz   xx   ||   |   z  cc<   ||dz      |xx   ||   |   z  cc<   t        ||   |   |j8                        rt;        ||   |         ||   |<   i||   |   j=                  | j
                        ||   |<     t)        |dz         D ]S  }||   |   j>                  rt)        |      D ]  }||   |xx   ||   ||   |   z  z  cc<     |j*                  ||   |<   U t)        |      D ]L  }||   |   dk(  rt)        |      D ]  }||   |xx   ||   ||   |   z  z  cc<     |j*                  ||   |<   N |j                  t)        |      D cg c]  }t)        |      D ]
  }||   |     c}}       t/        |tA        |      ||z  f|      j1                         }t5        ||      }|j6                  rtC        |jE                         | j                   j                  d      }| j                         r|j                         st        || j
                        S | jG                         dk(  r|jG                         dk(  ru| j                  |j                  k(  rMt        | |j                        }t        ||j                        }|d   |d   z  g}t)        dtI        tA        |      tA        |                  D ]  }t)        |dz         D cg c]  }t)        dz         D cg c]  }d c}  }}t)        dz         D ]0  }t)        |dz         D ]  }||z   |k(  stK        ||      ||   |<    2 d}t)        |dz         D ],  }t)        |dz         D ]  }|||   |   ||   z  ||   z  z  } . |j                  |        t        || j
                  | j                  |      S | j                   jM                  d      }|j                   jM                  d      }| j                  dk(  r|s|s| |jO                  d      z  S |j                  dk(  r|s|s| jO                  d      |z  S | j                   jM                  | j                        }|j                   jM                  | j                        }|s |s| |jO                  | j                        z  S | jO                  |j                        |z  S | j                  |j                  k7  rt        || j
                        S d }d }| jG                         dk(  re|jG                         dk(  rRtQ        | jR                        D cg c]  \  }}|tU        |      z   }}}tV        jX                  |i}|jR                  }n| jG                         dk(  re|jG                         dk(  rRtQ        |jR                        D cg c]  \  }}|tU        |      z   }}}| jR                  }tV        jX                  |i}n>| jG                         dk(  r+|jG                         dk(  r| jR                  }|jR                  }i }|D ]  }|D ]  }tI        tA        ||         tA        ||               }g } t)        |      D ]M  }tV        jX                  }!t)        |dz         D ]  }|!||   |   ||   ||z
     z  z  }! | j                  |!       O ||z   |vr	| |||z   <   t[        | |||z            D cg c]
  \  }}||z    c}}|||z   <     t        || j
                  | j                  |      S c c}w c c}w c c}w c c}}w c c}}w c c}}w c c}w c c}w c c}}w c c}}w c c}}w )Nz> Can't multiply a HolonomicFunction and expressions/functions.r7   r   r   Fr   T).r   ra   r   r   hasrx   r   r   r   r   r   r/   r   r   r   rv   rU   r   r   r}   r   r^   r_   r0   rJ   r   rQ   r   rz   DMFdiffr   is_zeror~   r   r   r   minr   r   r   ry   r   r   r   r   r   )"rc   rk   ann_selfr   r   r   r   r   	ann_otherr   r   r   r   r   self_red	other_red	coeff_mullin_sys_elementslin_syshomo_sysrP   sol_anny0_selfy0_othercoeffkr   r   r   r   r   r   r  r  s"                                     rF   r   zHolonomicFunction.__mul__  s
   ##%!23ENEyy )*jkk'')hnn5 AAIItxx4662U:??@A )466477BGG""''5+<+<+C+C+H+HH::e$DAqq5L%%		
NNOOOO  KKM$$ 	+AQUU155\*	+ %% 	,AaeeAEEl+	, ;@(CQYq\MIaL0CC=B1XFjm^jm3F	F >C1q5\JeAEl3aff3J	J%%	!Q 7<AhQaQ1Yq\!_Q_QR/!QqS1=GGI%%qsAh2$Wh7    1q5"b) Gq1ub"- GAaLQ'9Q<?:'a!e$Q'9Q<?:'!)A,q/177;*1)A,q/*B	!Q*3A,q/*>*>tvv*F	!QGG 1q5\ - |A.."1X ,!!Q9Q<%aLO,, ,, '(ffIaLO- 1X - |A!+"1X ,!!Q8A;%aLO,, ,, '(ffIaLO- ##eAh$YPUVWPX$Y1Yq\!_$Y_$YZ"#3c:J6KQqS5QSTU__aG((;C?   B SXXZ)9)9)@)@5Q$$&5+@+@+B$Wdff55 E)e.B.B.D.M ww%((" %T7==9%eW]];aj8A;./ q#c'lCM"BC #A@Ea!eM1q1u6Aa6MEM"1q5\ =!&q1u =A 1uz.6q!na==
 C"1q5\ I!&q1u IA58A;
#:Xa[#HHCII IIcN# )$&&$''2FF
 **66q9 ,,88;77a<%"2"21"555XX]78??1-55  $//;;DGGDH % 1 1 = =dgg FI#I#e&6&6tww&???  $uxx85@@77ehh$Wdff55  E)e.B.B.D.L09$''0BC11y|#CCC&&#BB  "d*u/C/C/E/N09%((0CD11y|#DCDB&&#B  "d*u/C/C/E/MBB 	FA FBqE
C1J/q  AA"1q5\ 5RU1X1a!e445HHQK	 
 1u{ !Bq1uI36q"QU)3D E41aQ EBq1uIF	F !$&&$''2>>W DF 4J RH %Z. 7MZ D E* !FsZ   i%*i*i4%i/8i4 i:&j 
+j 	j	jj<j&j
/i4jc                     | |dz  z   S r   rr   rj   s     rF   r   zHolonomicFunction.__sub__i  s    ebj  rH   c                     | dz  |z   S r   rr   rj   s     rF   r   zHolonomicFunction.__rsub__l  s    by5  rH   c                     d| z  S r   rr   r   s    rF   r   zHolonomicFunction.__neg__o  r   rH   c                 .    | t         j                  |z  z  S rA   r   rj   s     rF   r   zHolonomicFunction.__truediv__r  r   rH   c                 B   | j                   j                  dk  r| j                   }|j                  }| j                  d }nt	        | j                        d   |z  g}|j
                  d   }|j
                  d   }t        j                  || j                        |z  j                  }||fD cg c]  }|j                  j                  |       }}t        ||      }	t        |	| j                  | j                  |      S |dk  rt        d      |dk(  rU| j                   j                  j                   }
t        |
| j                  t"        j$                  t"        j&                  g      S |dk(  r| S |dz  dk(  r| |dz
  z  }|| z  S |dz  dk(  r| |dz  z  }||z  S y c c}w )Nr7   r   z&Negative Power on a Holonomic Functionr   )r   r   rv   r   r   r}   r/   r   rx   r   rU   r|   r]   r   r   r>   rT   r   r   r   )rc   r   r  rv   r   p0p1r   rP   ddr[   r   s               rF   r   zHolonomicFunction.__pow__u  s   !!Q&""CZZFww477mA&!+,"B"B((2tvv&*//B57H=q6;;''*=C=%c62B$R"==q5#$LMM6!!((<<B$R!%%AA6K1uz 1q5M	 4''Q! 1q5M	 9,,  >s   ."Fc                     | j                   j                  D cg c]  }|j                          }}t        |      S c c}w )zF
        Returns the highest power of `x` in the annihilator.
        )r   r}   degreer   )rc   r   rP   s      rF   r3  zHolonomicFunction.degree  s8     $(#3#3#>#>?aqxxz??3x @s   =c                    | j                   j                  }| j                   j                  }|j                  | j                        }| j                   j
                  }t        |      D ]l  \  }}	t        |	| j                   j                  j                  j                        s;| j                   j                  j                  j                  |	      ||<   n ||   j                  | j                  |i      }
t        |      D cg c]&  }||   j                  | j                  |i       |
z  ( }}t        |      D cg c]  }t        j                   }}t        j                  |d<   |g}t!        t        |      D cg c]  }t        j                   c}g      j#                         }	 |D cg c]  }|j                  | j                         }}t        |dz
        D ]  }||dz   xx   ||   |z  z  cc<    t        |      D ]  }||xx   |d   ||   z  |z  z  cc<    |}|j%                  |       t!        |      j#                         j'                  |      \  }}|j(                  durnt+        |      d   }|j                  |d      }t-        |dd |d      }|rt/        || j                  |d   |d         S t/        || j                        S c c}w c c}w c c}w c c}w )a`  
        Returns function after composition of a holonomic
        function with an algebraic function. The method cannot compute
        initial conditions for the result by itself, so they can be also be
        provided.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import QQ
        >>> from sympy import symbols
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
        >>> HolonomicFunction(Dx - 1, x).composition(x**2, 0, [1])  # e^(x**2)
        HolonomicFunction((-2*x) + (1)*Dx, x, 0, [1])
        >>> HolonomicFunction(Dx**2 + 1, x).composition(x**2 - 1, 1, [1, 0])
        HolonomicFunction((4*x**3) + (-1)*Dx + (x)*Dx**2, x, 1, [1, 0])

        See Also
        ========

        from_hyper
        r   Tr7   r   NFr   )r   rv   r   r   rx   r}   ry   ra   rU   rz   r|   r   r   r   r   r   r(   rJ   r   gauss_jordan_solver   r   r   r   )rc   r   r  r  r   r   r   r}   r   r   rE   r   coeffssystemhomogeneousr   coeffs_nextrP   taustaus                       rF   compositionzHolonomicFunction.composition  s   4 ##""yy %%00
j) 	IDAq!T--4499??@ $ 0 0 7 7 < < E Ea H
1	I qMt}-@Eq	J1A##TVVDM22Q6JJ"'(+Q!&&++EEq	uQx8!qvv89:DDF39:a166$&&>:K:1q5\ 9AE"vay4'78"91X @A6":Q#7$#>?@ FMM&!113$$[1 C!!-  4jmhhsAQR!e4 $S$&&$q'47CC dff--5 K+ 9:s   +K?K" K'2"K,c           
      
   | j                   dk7  r)| j                  | j                         j                         S | j                  j	                  | j                         r| j                  |      S i }t        dd      }| j                  j                  j                  j                  }t        |j                  |      d      \  }}t        | j                  j                        D ]  \  }}|j                         }	t        |	      dz
  }
t!        |
dz         D ]z  }|	|
|z
     }|dk(  r||z
  |f|v r5|||z
  |fxx   |j#                  |      t%        ||z
  dz   |      z  z  cc<   O|j#                  |      t%        ||z
  dz   |      z  |||z
  |f<   |  g }|D cg c]  }|d   	 }}t'        |      }t)        |      }| j+                         }
||
z   }i }g }g }t!        ||dz         D ]  }||v r[t,        j.                  }|j1                         D ]&  }|d   |k(  s|||   j3                  |||z
        z  }( |j5                  |       b|j5                  t,        j.                          t7        ||      }|j8                  }t;        |j                  j#                  |j                  d         |d	
      }|j1                         }|rt)        |      dz   }t)        ||      }||z  }t=        | |      }g }t        |      D ]"  \  }}|j5                  |t?        |      z         $ t        |      |k  r t!        |
      D ]  }t,        j.                  }|D ]  }||d   z   dk  rt,        j.                  |||d   z   <   nj||d   z   t        |      k  r|||d   z      |||d   z   <   nA||d   z   |vr7t        d||d   z   z        |||d   z   <   |j5                  |||d   z             |d   |k  s|||   j3                  ||      |||d   z      z  z  } |j5                  |        tA        |g| }tC        |tD              rt!        t        |      |      D ]J  }||vrt        d|z        ||<   ||   |v r|j5                  |||             7|j5                  ||          L |rtG        ||      |fgS tG        ||      gS t!        t        |      |      D ]X  }||vrt        d|z        ||<   d}|D ]#  }||   |v s|j5                  |||             d}% |rE|j5                  ||          Z |rtG        ||      |fgS tG        ||      gS c c}w )aG  
        Finds recurrence relation for the coefficients in the series expansion
        of the function about :math:`x_0`, where :math:`x_0` is the point at
        which the initial condition is stored.

        Explanation
        ===========

        If the point :math:`x_0` is ordinary, solution of the form :math:`[(R, n_0)]`
        is returned. Where :math:`R` is the recurrence relation and :math:`n_0` is the
        smallest ``n`` for which the recurrence holds true.

        If the point :math:`x_0` is regular singular, a list of solutions in
        the format :math:`(R, p, n_0)` is returned, i.e. `[(R, p, n_0), ... ]`.
        Each tuple in this vector represents a recurrence relation :math:`R`
        associated with a root of the indicial equation ``p``. Conditions of
        a different format can also be provided in this case, see the
        docstring of HolonomicFunction class.

        If it's not possible to numerically compute a initial condition,
        it is returned as a symbol :math:`C_j`, denoting the coefficient of
        :math:`(x - x_0)^j` in the power series about :math:`x_0`.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import QQ
        >>> from sympy import symbols, S
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
        >>> HolonomicFunction(Dx - 1, x, 0, [1]).to_sequence()
        [(HolonomicSequence((-1) + (n + 1)Sn, n), u(0) = 1, 0)]
        >>> HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1]).to_sequence()
        [(HolonomicSequence((n**2) + (n**2 + n)Sn, n), u(0) = 0, u(1) = 1, u(2) = -1/2, 2)]
        >>> HolonomicFunction(-S(1)/2 + x*Dx, x, 0, {S(1)/2: [1]}).to_sequence()
        [(HolonomicSequence((n), n), u(0) = 1, 1/2, 1)]

        See Also
        ========

        HolonomicFunction.series

        References
        ==========

        .. [1] https://hal.inria.fr/inria-00070025/document
        .. [2] https://www3.risc.jku.at/publications/download/risc_2244/DIPLFORM.pdf

        r   )lbr   TintegerSnr7   r   ZfilterC_%sF)$r   shift_xto_sequencer   r   
_frobeniusr   rv   rU   r   r:   r   ry   r}   
all_coeffsr~   r   r|   r   r  r   r3  r   r   keysr   r   r9   r   r.   r   r   r6   ra   r   r8   )rc   r>  dict1r   r   r   r   r   r   	listofdmpr3  r)  r(  rP   keylistr	  r
  
smallest_ndummyseqsunknownstempr   	all_rootsmax_rootr   u0eqsoleqsr  s                                 rF   rG  zHolonomicFunction.to_sequence  s   h 77a<<<(4466 ''0??b?))3%%%**.."3#4#4Q#7>1 d..99: 	QDAqI^a'F6A:& 	Q!&1*-A:E1:&1q5!*%#,,u*=1q519a@P*PQ%),e)<r!a%!)Q?O)OE1q5!*%	Q	Q" !&'A1Q4''GG V^
 ueai( 	#AG|vv <AtqyaaU ;;< 

4 

166"	# !a( 		!&&//#..*<=qM	NN$	9~)HXz2Je$ bM 	(DAqIIa)A,&'	( r7U?6] VV EA1Q4x!|+,66q1Q4x(QqTCG++-a!A$h<q1Q4x(1X/+1&1qt82D+Eq1Q4x( q1Q4x(89tqyeAhmmAq1F1qt84DDDE 

2%* 3**F&$'s2w. 	-A$*619$5q	ayF*		&"34 		&),	- .sB7DEE)#r2333r7E* )F? &vqy 1F1I !AayA~		!F1I,/ ! IIfQi() &sB/<==!#r*++S (s   Uc                    | j                         }g }g }t        |j                               D ]U  }|j                  r|j	                  |g||   z         (|j                         \  }}|j	                  |||fg||   z         W |j                  d        |j                  d        |j                          g }|D ]p  }d}	t        |      dk(  r|j                  |g       &|D ]1  }
t        |
d   |z
        |
d   |z
  k(  s|
j                  |       d}	 n |	r_|j                  |g       r t        d |D              rdnd}t        d |D              }t        d	 |D              }| j                         dk(  rgg }t        | j                  j                               D ]>  }t        |j                               D ]   }
t        |
|      s|j                  |       " @ n|r|rt        |      g}n|r(|D cg c]  }|d   	 c}|D 
cg c]  }
|
d   	 c}
z   }n|s*g }|D ]"  }t        |      |k(  r|j                  |       $ np|sn| j                         r%t!        | j                  d         j"                  dk(  rt        |      g}n,g }|D ]  }|dk\  s	|j                  |        t        |      g}t%        d
d      }| j&                  j(                  j*                  j,                  }t/        |j1                  |      d      \  }}g }t3        d      }D ]  }i }t5        | j&                  j6                        D ]  \  }}
|
j9                         }t        |      dz
  }t;        |dz         D ]  }|||z
     }|dk(  r||z
  ||z
  f|v r;|||z
  ||z
  fxx   |j=                  |      t?        ||z
  dz   |z   |      z  z  cc<   X|j=                  |      t?        ||z
  dz   |z   |      z  |||z
  ||z
  f<     g }|D cg c]  }|d   	 }}t        |      }tA        |      }tA        |D cg c]  }|d   	 c}      }t        |D cg c]  }|d   	 c}      } ||z   }!i }"g }#g }$t;        ||dz         D ]  }
|
|v r[t         jB                  }%|j                         D ]&  }|d   |
k(  s|%||   jE                  |||z
        z  }%( |j                  |%       b|j                  t         jB                          tG        ||      }|jH                  }&tK        |j*                  j=                  |j6                  d         |d      }'|'j                         }'|'rtA        |'      dz   }(tA        |(|!      }!|&|!z  }&g })| j                         dk(  r| j                  |   })n| j                         dk(  r|dk\  rt        |      |k(  rt        |      dk(  rrtM        | |&t        |      z         }*t        |*      t        |      kD  rCt;        t        |      t        |*            D ]"  }|)j                  |*|   tO        |      z         $ t        |)      |&k  rNt;        | |      D ]  }t         jB                  }+|D ]  }
||
d   z   dk  rt         jB                  |"||
d   z   <   nx||
d   z   t        |)      k  r|)||
d   z      |"||
d   z   <   nO||
d   z   |"vrEtQ        |      d||
d   z   z  z   },t%        |,      |"||
d   z   <   |$j                  |"||
d   z             |
d   |k  s|+||
   jE                  ||      |"||
d   z      z  z  }+ |#j                  |+        tS        |#g|$ }-tU        |-tV              rt;        t        |)      |&      D ]X  }||"vrtQ        |      d|z  z   },t%        |,      |"|<   |"|   |-v r|)j                  |-|"|             E|)j                  |"|          Z |r |j                  tY        ||)      ||!f       |j                  tY        ||)      |f       t;        t        |)      |&      D ]f  }||"vrtQ        |      d|z  z   },t%        |,      |"|<   d}.|-D ]#  }
|"|   |
v s|)j                  |
|"|             d}.% |.rS|)j                  |"|          h |r|j                  tY        ||)      ||!f       n|j                  tY        ||)      |f       |dz  } |S c c}w c c}
w c c}w c c}w c c}w )Nc                     | d   S )Nr7   rr   rx   s    rF   rG   z.HolonomicFunction._frobenius.<locals>.<lambda>  
    !A$ rH   )keyc                     | d   S Nr   rr   rZ  s    rF   rG   z.HolonomicFunction._frobenius.<locals>.<lambda>  r[  rH   Fr   Tc              3   8   K   | ]  }t        |      d k(    ywr7   N)r~   .0r   s     rF   	<genexpr>z/HolonomicFunction._frobenius.<locals>.<genexpr>  s     !;!#a&A+!;   c              3   &   K   | ]	  }|d k\    ywr   Nrr   ra  s     rF   rc  z/HolonomicFunction._frobenius.<locals>.<genexpr>  s     +Q!V+   c              3   8   K   | ]  }t        |      |k(    y wrA   r   ra  s     rF   rc  z/HolonomicFunction._frobenius.<locals>.<genexpr>  s     0QSVq[0rd  r   r?  rA  Cr7   r   rB  rC  z_%s)-	_indicialr   rJ  is_realextendas_real_imagsortr~   r   r   allr   r   r   r  r   r   	is_finiter   r   rv   rU   r   r:   r   ordry   r}   rI  r   r|   r   r   r   r   r9   r   r.   r   r   chrr6   ra   r   r8   )/rc   r>  indicialrootsrealscomplr   r   r   grpintdiffr   independentallposallintrootstoconsiderposrootsr   r   r   r   finalsolcharr   rK  rL  r3  r)  r(  rP   rM  r	  r
  degree2rN  rO  rP  rQ  rR  r   rS  rT  rU  r   rV  letterrW  r  s/                                                  rF   rH  zHolonomicFunction._frobenius  s	   (++-. 	=AyyaS=#334~~'1q!Qi[=+;;<	= 	


'


'

  	 AG3x1}

A3 qtax=AaD1H,HHQK"G	
 

A3	  "!;s!;;d+U++0%00  D( OTWW\\^, 2 !3!3!56 2A#Aq)'..q122
 "5zlO-01qt154IaQqT4IIO O .1v{#**1-. '')Qtwwqz]-D-D-N#&u:,  +AAv *+ $'x=/3%%%**.."3#4#4Q#7>13x  B	AE!$"2"2"="=> ]1LLN	Y!+vz* 	]A%fqj1Ez Aq1u~.q1ua!en-#,,u2E1q5ST9WX=Z[H\2\]-14e1Dr!a%RS)VW-YZG[1[q1ua!en-	]]  C%*+qt+G+LELE.1!A$./F/A1Q4/0GJFCH5%!), '<66D"ZZ\ @Q419 E!HMM!QY$??D@ JJt$JJqvv&' %S!,C IIEaffoocnnR.@A1SQI!(Iy>A- :6
ZEB""$,WWQZ$$&%/AFs1v{sSbOcghOhec!fn5r7SV#"3q63r73 8		"Q%)A,"678 2ww/ #AB" Iqt8a</0vvF1qt8,1XB//1!ad(|F1qt8,!"QqTV!3%(YQqT1B%BF/5f~F1qt8,$OOF1qt8,<=Q419%(--1"5q1Q4x8H"HHBI JJrN%#* s.X.fd+"3r7E2 
1F?%(Y%9F(.vF1I!!9.IIfVAY&78 IIfQi0
1  ):3)CQ
(ST  ):3)CQ(GH s2w. -A!$TUAX!5$*6Nq	A# %!!9>IIaq	l3 $A% 		&),- !23!;Q
 KL !23!;Q ?@AIDEB	F } 24I^ , //s   >c3c8c=:d
d
c                    || j                         }n|}t        |t              rt        |      dk(  r|d   }d}nt        |t              rt        |      dk(  r|d   }|d   }nt        |      dk(  rt        |d         dk(  r|d   d   }d}n\t        |      dk(  r"t        |d         dk(  r|d   d   }|d   d   }n,g }|D ]#  }|j	                  | j                  |             % |S |t        |      z
  }t        |j                        dz
  }	|j                  j                  }
| j                  }| j                  }|j                  j                  }|j                  j                  j                  }|j                         }g }t!        |      D ]/  \  }}|j	                  |j#                  |j$                               1 t'        |
      D cg c]  }||    ||
   z   }}t)        |j                        }|	dz   |k\  rnot'        |	dz   |
z
  ||
z
        D ]W  }t*        j,                  }t'        |
      D ]&  }||z   dk\  s|t/        ||   |      |||z      z  z  }( |j	                  |       Y |r|S t*        j,                  }t!        |      D ]  \  }}||||z   z  |z  z  } |r|t1        ||t        |      z   z  |      z  }|dk7  r|j3                  |||z
        S |S c c}w )a  
        Finds the power series expansion of given holonomic function about :math:`x_0`.

        Explanation
        ===========

        A list of series might be returned if :math:`x_0` is a regular point with
        multiple roots of the indicial equation.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import QQ
        >>> from sympy import symbols
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')
        >>> HolonomicFunction(Dx - 1, x, 0, [1]).series()  # e^x
        1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + O(x**6)
        >>> HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]).series(n=8)  # sin(x)
        x - x**3/6 + x**5/120 - x**7/5040 + O(x**8)

        See Also
        ========

        HolonomicFunction.to_sequence
        r   r   r   r7   )_recur)rG  ra   tupler~   r   seriesr   rU  
recurrencer   rx   r   r}   rv   rU   r   ry   r   r   r   r   r   r   DMFsubsr3   r   )rc   r   coefficientr   r  r  constantpowerrP   r   lr)  rx   r   seq_dmpr   r   seqr   subr(  sers                        rF   r  zHolonomicFunction.seriest  s   : >))+JJj%(S_-A#AJM
E*s:!/C&qMM#AJ_!c*Q-&8A&=#Aq)JM_!c*Q-&8A&=&qM!,M#Aq)JC 2

4;;a;012JM""
"!!''FFWW''22!!((--KKMg& 	%DAqJJquuQUU|$	% */q2AAwQ22:==!q5A: 1q519a!e, "q AA1uzQ!3c!a%j!@@A 

5!" JffcN 	.DAq1q=()A--C	.5Q]!334a88C788Aq2v&&
1 3s   K$c                 l  
 | j                   dk7  r)| j                  | j                         j                         S | j                  j                  }| j                  j
                  j                  
| j                  
j                  }
j                  }
fd|D cg c]  }|j                          }}t        |      }dt        d|      t        d| j                  j                        z   z  fd} ||d         }t        dt        |            D ]  }t        | |||         |z
        } t!        |      D ]P  \  }}|j#                         }	t        |	      dz
  }| |z
  dk  r||z
  |z
  dk\  r||	||z
  |z
     |z  z   }||z
  z  }R t%        
j'                  |            S c c}w )z:
        Computes roots of the Indicial equation.
        r   c                 n    t        j                  |       d      }d|j                         v r|d   S y)NrB  rC  r   )r.   r|   rJ  )polyroot_allr   rx   s     rF   _pole_degreez1HolonomicFunction._indicial.<locals>._pole_degree  s5    QZZ-q=HHMMO#{"rH   
   r7   c                 0    | j                   rS  |       S rA   )r  )qr  infs    rF   rG   z-HolonomicFunction._indicial.<locals>.<lambda>  s    qyy l1o rH   )r   rF  rk  r   r}   rv   rU   rx   r^   r_   r3  r   r   r   r~   r  ry   rI  r.   r|   )rc   
list_coeffr  yr   r3  degr   r   rL  r   r  r  rx   s             @@@@rF   rk  zHolonomicFunction._indicial  s   
 77a<<<(2244%%00
##((FFFFEE	 '11!((*11VC6NSD,<,<,B,B%CCD=
1q#j/* 	/AAs:a=)A-.A	/ j) 	DAqI^a'FsQw!|
Q! 3	&1*q.1A55QJA	 QZZ]A&&# 2s   !F1c                    ddl m} d}t        |d      sd}t        |      }| j                  |k(  r || |g||      d   S |j
                  st        | j                  }||kD  r| }t        ||z
  |z        }	||z   g}t        |	dz
        D ]  }
|j                  |d   |z           t        | j                  j                  j                  j                  | j                  j                  d         | j                         D ]!  }
|
| j                  k(  s|
|v st#        | |
       |r || |||      d   S  || |||      S )	a  
        Finds numerical value of a holonomic function using numerical methods.
        (RK4 by default). A set of points (real or complex) must be provided
        which will be the path for the numerical integration.

        Explanation
        ===========

        The path should be given as a list :math:`[x_1, x_2, \dots x_n]`. The numerical
        values will be computed at each point in this order
        :math:`x_1 \rightarrow x_2 \rightarrow x_3 \dots \rightarrow x_n`.

        Returns values of the function at :math:`x_1, x_2, \dots x_n` in a list.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import QQ
        >>> from sympy import symbols
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')

        A straight line on the real axis from (0 to 1)

        >>> r = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]

        Runge-Kutta 4th order on e^x from 0.1 to 1.
        Exact solution at 1 is 2.71828182845905

        >>> HolonomicFunction(Dx - 1, x, 0, [1]).evalf(r)
        [1.10517083333333, 1.22140257085069, 1.34985849706254, 1.49182424008069,
        1.64872063859684, 1.82211796209193, 2.01375162659678, 2.22553956329232,
        2.45960141378007, 2.71827974413517]

        Euler's method for the same

        >>> HolonomicFunction(Dx - 1, x, 0, [1]).evalf(r, method='Euler')
        [1.1, 1.21, 1.331, 1.4641, 1.61051, 1.771561, 1.9487171, 2.14358881,
        2.357947691, 2.5937424601]

        One can also observe that the value obtained using Runge-Kutta 4th order
        is much more accurate than Euler's method.
        r   )_evalfFr   T)methodderivativesr   r7   )sympy.holonomic.numericalr  r   r   r   r   r   r   r   r   r.   r   rv   rU   r|   r}   rx   r=   )rc   pointsr  hr  r  lpr   r   r   r   s              rF   r   zHolonomicFunction.evalf  sP   \ 	5 vz*B&	Aww!|dQCKPQSTT;;))A1uBQUaK A!eWF1q5\ .fRj1n-. t''..33<<T=M=M=X=XY[=\]_c_e_ef 	0ADGG|qF{&tQ//	0 $v;OPRSSdF6{KKrH   c                 n   | j                   j                  j                  j                  }|j	                  |      }t        |d      \  }}g }| j                   j                  D ]#  }|j                   ||j                               % t        ||      }t        ||| j                  | j                        S )z
        Changes only the variable of Holonomic Function, for internal
        purposes. For composition use HolonomicFunction.composition()
        r[   )r   rv   rU   r   r   rX   r}   r   r   r]   r   r   r   )rc   zr   r   rv   r   rP   r   s           rF   change_xzHolonomicFunction.change_xC  s     %%**..a )!T2	!!,, 	!AJJqx 	!#C0 a$''::rH   c           
         | j                   }| j                  j                  }| j                  j                  j                  }|D cg c]5  }|j                  |j                  |      j                  |||z               7 }}t        || j                  j                        }| j                  |z
  }| j                         st        ||      S t        |||| j                        S c c}w )z-
        Substitute `x + a` for `x`.
        )rx   r   r}   rv   rU   r{   r|   r   r]   r   r   r   r   )rc   r   rx   listaftershiftrU   r   rP   r   s           rF   rF  zHolonomicFunction.shift_xR  s    
 FF))44&&++IWXAtt}}Q/44QA>?XX"3(8(8(?(?@WWq[##%$S!,, aTWW55 Ys   :C&c                 t   || j                         }n|}t        |t              rt        |      dk(  r|d   }|d   }d}nt        |t              rt        |      dk(  r|d   }|d   }|d   }nt        |      dk(  r$t        |d         dk(  r|d   d   }|d   d   }d}npt        |      dk(  r*t        |d         dk(  r|d   d   }|d   d   }|d   d   }n8| j	                  ||d         }|dd D ]  }|| j	                  ||      z  } |S |j
                  }|j                  }	| j                  }
| j                  }|	j                  }|dk(  r`t        |	j                  j                  j                  |	j                  d         |j                  d      }t         j"                  }t%        |      D ]  \  }}|dk  st'        |      |k7  rt'        |      }|t        |      k  r>t        ||   t(        t*        f      r||   j-                         ||<   |||   |
|z  z  z  }q|t/        d	|z        |
|z  z  z  } t        |t(        t*        f      r|j-                         |
|z  z  }n||
|z  z  }|r |dk7  r|j1                  |
|
|z
        fgS |fgS |dk7  r|j1                  |
|
|z
        S |S ||z   t        |      kD  rt3        d
      d}t5        dt        |	j                        dz
        D ]6  }|	j                  |   |	j                  j                  j6                  k7  s4d} n |st9        | | j                        |	j                  d   }|	j                  d   }t        |j:                  d   t(        t*        f      rwt!        |j:                  d   j-                               ||j=                         z  z   t!        |j:                  d   j-                               ||j=                         z  z  z  }nZt!        |j:                  d         ||j=                         z  z   t!        |j:                  d         ||j=                         z  z  z  }d}t        |	j                  j                  j                  |      |j                        }t        |	j                  j                  j                  |      |j                        }|rg }t5        ||z         D ]  }||k  rU|r;j?                  t!        ||         |
||z   z  z  j1                  |
|
|z
        f       n|t!        ||         |
|z  z  z  }^t!        ||         dk(  rpg }g }tA        |jC                               D ])  }|jE                  tG        ||z
  |z        g||   z         + tA        |jC                               D ])  }|jE                  tG        ||z
  |z        g||   z         + d|v r|jI                  d       n|j?                  d       |raj?                  t!        ||         |
||z   z  z  j1                  |
|
|z
        tK        ||||
|z  z        j1                  |
|
|z
        f       |t!        ||         tK        ||||
|z  z        z  |
|z  z  z  } |rS ||
|z  z  }|dk7  r|j1                  |
|
|z
        S |S )a  
        Returns a hypergeometric function (or linear combination of them)
        representing the given holonomic function.

        Explanation
        ===========

        Returns an answer of the form:
        `a_1 \cdot x^{b_1} \cdot{hyper()} + a_2 \cdot x^{b_2} \cdot{hyper()} \dots`

        This is very useful as one can now use ``hyperexpand`` to find the
        symbolic expressions/functions.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import ZZ
        >>> from sympy import symbols
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')
        >>> # sin(x)
        >>> HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]).to_hyper()
        x*hyper((), (3/2,), -x**2/4)
        >>> # exp(x)
        >>> HolonomicFunction(Dx - 1, x, 0, [1]).to_hyper()
        hyper((), (), x)

        See Also
        ========

        from_hyper, from_meijerg
        Nr   r7   r   r   )as_listr  r   rC  rE  z+Can't compute sufficient Initial ConditionsTFr   )&rG  ra   r  r~   to_hyperrU  r  rx   r   r   r.   rv   rU   r|   r}   r   r   r   ry   r   r)   r*   as_exprr   r   r   r   r^   r<   r   r3  r   r   rJ  rm  r5   remover%   )rc   r  r  r  rN  r  rP   r   rU  rE   rx   r   mnonzerotermsr   is_hyperr   r   r  arg1arg2	listofsolapbqr)  s                            rF   r  zHolonomicFunction.to_hyperb  s   F >))+JJj%(S_-A#AJ#AJM
E*s:!/C#AJ&qMM#AJ_!c*Q-&8A&=#Aq)J#Aq)JM_!c*Q-&8A&=#Aq)J&qM!,M#Aq)J--
1-FC^ @t}}WQ}??@J]]!!FFWW GG 6 !7!7Q!H*,,_bcL&&C!,/ 41q5CFaKFs2w;!"Q%+{)CD "112a51a4<'C 6&!),q!t33C4 #[9:kkma&66A},,7 XXaR0344y Qwxx1r6**J>CG#%&STT q#all+A-. 	A||A!((--"4"44 	
 %dDGG44LLOLL aeeAhk :;QUU1X%%'(1qxxz?:;qqAQAQAS?TWX[\[c[c[eWf?fgAQUU1X;QXXZ01QquuQx[1qxxz?5RSAQXX]]++A.
=QXX]]++A.
= IzA~& %	AA :~$$qAx!ao2F'F&L&LQPQRTPT&U%XY1RU8ad?*C
 Ax1}BB TYY[) >		9a!eq[12T!W<=> TYY[) >		9a!eq[12T!W<=> Bw		!		!  1RU8A-,@#@"F"Fq!B$"ORWXZ\^`abcefbf`fRgQmQmnoqrsuquQv!wxqAx%BAqD"99AqD@@K%	AL A}$$788Aq2v&&
rH   c                 P    t        | j                               j                         S )a_  
        Converts a Holonomic Function back to elementary functions.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy import ZZ
        >>> from sympy import symbols, S
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(ZZ.old_poly_ring(x),'Dx')
        >>> HolonomicFunction(x**2*Dx**2 + x*Dx + (x**2 - 1), x, 0, [0, S(1)/2]).to_expr()
        besselj(1, x)
        >>> HolonomicFunction((1 + x)*Dx**3 + Dx**2, x, 0, [1, 1, 1]).to_expr()
        x*log(x + 1) + log(x + 1) + 1

        )r4   r  simplifyr   s    rF   r   zHolonomicFunction.to_expr  s    & 4==?+4466rH   c                    d}|At        | j                        | j                  j                  kD  rt        | j                        }| j                  j                  j
                  j                  }	 t        | j                         | j                  |||      }|rj                  |k(  r|S | j                  |d      }t        | j                  | j                  ||      S # t        t        f$ r d}Y [w xY w)a  
        Changes the point `x0` to ``b`` for initial conditions.

        Examples
        ========

        >>> from sympy.holonomic import expr_to_holonomic
        >>> from sympy import symbols, sin, exp
        >>> x = symbols('x')

        >>> expr_to_holonomic(sin(x)).change_ics(1)
        HolonomicFunction((1) + (1)*Dx**2, x, 1, [sin(1), cos(1)])

        >>> expr_to_holonomic(exp(x)).change_ics(2)
        HolonomicFunction((-1) + (1)*Dx, x, 2, [exp(2)])
        T)rx   r   lenicsrC   F)r  )r~   r   r   r   rv   rU   rC   expr_to_holonomicr   rx   r;   r<   r   r   r   )rc   r   r  symbolicr   rP   r   s          rF   r   zHolonomicFunction.change_ics.  s    $ >c$''lT-=-=-C-CC\F%%**11	#DLLNdff6Z]^C !JZZtZ, !1!14661bAA $%89 	H	s   1(C! !C54C5c                     | j                  d      }t        j                  }|D ]?  }t        |      dk(  r	||d   z  }t        |      dk(  s)||d   t	        |d         z  z  }A |S )a  
        Returns a linear combination of Meijer G-functions.

        Examples
        ========

        >>> from sympy.holonomic import expr_to_holonomic
        >>> from sympy import sin, cos, hyperexpand, log, symbols
        >>> x = symbols('x')
        >>> hyperexpand(expr_to_holonomic(cos(x) + sin(x)).to_meijerg())
        sin(x) + cos(x)
        >>> hyperexpand(expr_to_holonomic(log(x)).to_meijerg()).simplify()
        log(x)

        See Also
        ========

        to_hyper
        T)r  r7   r   r   )r  r   r   r~   _hyper_to_meijerg)rc   r   rP   r   s       rF   
to_meijergzHolonomicFunction.to_meijergQ  ss    , mmDm)ff 	6A1v{qtQ1qt/!555	6 
rH   rf  FT)   FTN)RK4g?F)FNrA   )$rm   rn   ro   rp   r   rd   rf   rq   r   r   r   r   r   r   r   rl   r   r   r   r   r   r   r   r3  r<  rG  rH  r  rk  r   r  rF  r  r   r   r  rr   rH   rF   r   r     s    @D L: H< L V;p}?~K;Zu?n H!!&-B>.@,BTlZx&'PILV;6 un7*!BF rH   r   Fc                 n   | j                   }| j                  }| j                  d   }|j                  t              j                         }t        t        j                  |      d      \  }}||z  }	d}
|D ]
  }|
|	|z   z  }
 |	dz
  }|}|D ]
  }|||z   z  } |
|z
  }t        |       }|t        t        fv rt        ||      j                  |      S dd}t        |t              sQ |||||j                         }|s|dz  } |||||j                         }|st        ||      j                  |||      S t        |t              rUd} |||||j                   |      }|s|dz  } |||||j                   |      }|st        ||      j                  |||      S t        ||      j                  |      S )a  
    Converts a hypergeometric function to holonomic.
    ``func`` is the Hypergeometric Function and ``x0`` is the point at
    which initial conditions are required.

    Examples
    ========

    >>> from sympy.holonomic.holonomic import from_hyper
    >>> from sympy import symbols, hyper, S
    >>> x = symbols('x')
    >>> from_hyper(hyper([], [S(3)/2], x**2/4))
    HolonomicFunction((-x) + (2)*Dx + (x)*Dx**2, x, 1, [sinh(1), -sinh(1) + cosh(1)])
    r   r[   r7   c                    g }t        |      D ]y  }|r!| j                  ||      j                         }n| j                  ||      }|j                  du st	        |t
              r y |j                  |       | j                  |      } { |S NFr   r   r   rq  ra   r   r   r   simprx   r   r   r   r   r   vals           rF   _find_conditionsz$from_hyper.<locals>._find_conditions  s}    u 		 Aii2&,,.ii2&}}%C)=IIcN99Q<D		  	rH   r  )r  r  r  atomsr   poprX   r+   r   r4   r   r   r   r<  ra   r%   r   )funcr   r   r   r   r  rx   r   r[   xDxr1aixDx_1r2birP   r  r  r   s                      rF   
from_hyperr  t  s     	AA		!A	A!""2"21"5t<EAr B$C	
B 
cBh!GE	B 
ebj
r'CtD*++ a(44Q77 dE"dAr3995 !GB!$2syy9B  !a(44QB??$dAr399e<!GB!$2syy%@B  !a(44QB??S!$0033rH   Tc                 "   | j                   }| j                  }t        | j                        }t        | j                        }t        |      }	| j
                  d   }
|
j                  t              j                         }t        |j                  |      d      \  }}||z  }|dz   }|d||z   |	z
  z  z  }|D ]
  }|||z
  z  } d}|D ]
  }|||z
  z  } ||z
  }|st        ||      j                  |
      S t        |       }|t        t        fv rt        ||      j                  |
      S dd}t!        |t"              sQ |||||j$                        }|s|dz  } |||||j$                        }|st        ||      j                  |
||      S t!        |t"              rUd} |||||j$                  |      }|s|dz  } |||||j$                  |      }|st        ||      j                  |
||      S t        ||      j                  |
      S )a  
    Converts a Meijer G-function to Holonomic.
    ``func`` is the G-Function and ``x0`` is the point at
    which initial conditions are required.

    Examples
    ========

    >>> from sympy.holonomic.holonomic import from_meijerg
    >>> from sympy import symbols, meijerg, S
    >>> x = symbols('x')
    >>> from_meijerg(meijerg(([], []), ([S(1)/2], [0]), x**2/4))
    HolonomicFunction((1) + (1)*Dx**2, x, 0, [0, 1/sqrt(pi)])
    r   r[   r7   r   c                    g }t        |      D ]y  }|r!| j                  ||      j                         }n| j                  ||      }|j                  du st	        |t
              r y |j                  |       | j                  |      } { |S r  r  r  s           rF   r  z&from_meijerg.<locals>._find_conditions  s}    u 	 Aii2&,,.ii2&}}%C)=IIcN99Q<D	  	rH   r  )r  r  r~   anbmr  r  r   r  rX   r   r   r<  r4   r   r   ra   r&   r   )r  r   r   r   rC   r   r   r   r  r   r  rx   r   r[   r  xDx1r  r  r  r  rP   r  r  r   s                           rF   from_meijergr    s     	AADGGADGGAAA		!A	A!&"6"6q"94@EAr B$C7D	
B!a%!)	B 
dRi 
B 
cBh
r'C a(44Q77tD*++ a(44Q77 dG$dAr3995!GB!$2syy9B  !a(44QB??$ dAr399e<!GB!$2syy%@B  !a(44QB??S!$0033rH   x_1N)_mytypec           	         t        |       } | j                  }|s*t        |      dk(  r|j                         }n t	        d      ||v r|j                  |       t        |      }|C| j                  t              rt        }nt        }t        |      dk7  r||   j                         }t        | ||||||      }	|	r|	S t        s|ai at        t        |       n|t        k7  r|ai at        t        |       | j                   rd| j#                  |t$              }
t'        |
t$              }|t        v r!t        |   }|d   d   j)                  |      }nt+        | |d|      }|st,        |r||_        |s|s	||_        |S |s|j2                  j4                  }t7        | |||      }|s|dz  }t7        | |||      }|st9        |j2                  |||      S |s|s0|j;                  | j<                  d         }|r||_        ||_        |S |s|j2                  j4                  }t7        | |||      }|s|dz  }t7        | |||      }|s|j;                  | j<                  d   ||      S | j<                  }| j>                  }
tA        |d   |d|      }|
tB        u r0tE        dt        |            D ]  }|tA        ||   |d|      z  } nH|
tF        u r0tE        dt        |            D ]  }|tA        ||   |d|      z  } n|
tH        u r||d   z  }||_        |st,        |r||_        |s|s|S |j.                  r|S |s|j2                  j4                  }|j2                  jK                  |      r|jM                         }t        |      }t        |      dk(  r|||d      tN        jP                  k(  rc|d   }| ||z
  |z  z  }t7        ||||      }tS        |      D cg c]  \  }}|tU        |      z   }}}||i}t9        |j2                  |||      S t7        | |||      }|s|dz  }t7        | |||      }|st9        |j2                  |||      S c c}}w )	a  
    Converts a function or an expression to a holonomic function.

    Parameters
    ==========

    func:
        The expression to be converted.
    x:
        variable for the function.
    x0:
        point at which initial condition must be computed.
    y0:
        One can optionally provide initial condition if the method
        is not able to do it automatically.
    lenics:
        Number of terms in the initial condition. By default it is
        equal to the order of the annihilator.
    domain:
        Ground domain for the polynomials in ``x`` appearing as coefficients
        in the annihilator.
    initcond:
        Set it false if you do not want the initial conditions to be computed.

    Examples
    ========

    >>> from sympy.holonomic.holonomic import expr_to_holonomic
    >>> from sympy import sin, exp, symbols
    >>> x = symbols('x')
    >>> expr_to_holonomic(sin(x))
    HolonomicFunction((1) + (1)*Dx**2, x, 0, [0, 1])
    >>> expr_to_holonomic(exp(x))
    HolonomicFunction((-1) + (1)*Dx, x, 0, [1])

    See Also
    ========

    sympy.integrals.meijerint._rewrite1, _convert_poly_rat_alg, _create_table
    r7   z%Specify the variable for the functionr   )r   r   r  rC   r   )rC   Fr   rC   )rx   r   rC   )+r   free_symbolsr~   r  
ValueErrorr  r   r  r	   r,   r+   r   _convert_poly_rat_alg_lookup_tabledomain_for_table_create_tableis_Functionr   r  r  r  _convert_meijerintr   r   r   r   r   r  r   r<  r  r  r  r   r   r   r   r   rk  r   r   ry   r   )r  rx   r   r   r  rC   r   syms
extra_symssolpolyftr  rP   r   r  r   rE   gsingular_icsr   s                        rF   r  r  	  sX   R 4=DDt9>xxzADEE	
dAdJ~88E?FFz?aJ'113F $D!r&QWbjkG !mF3	#	#!mF3 IIaAsOa AA$q'""1%C$T1uVLC))
.."4B7Ca&tQF;  %S__aSAAX//$))A,/CCFJ__**FtQF3!GB"4B7C  tyy|R55 99D		A
DGq5
HCCxq#d)$ 	RA$T!WE&QQC	R 
cq#d)$ 	RA$T!WE&QQC	R 
c47lCF!!		

vv
&&
""2&MMOGq6Q;1QqT7aee+!AB{"A+Aq"f=L9B<9PQAA	!,QLQL!B$S__aR@@
4B
/C
atQF3  S__aS99 Rs   !Qc                    g }g }|j                   }|j                         }|j                  t        j                        }g }t        |       D ]  \  }	}
t        |
|j                        r+|j                  |j                  |
j                               nQt        |
|j                        s*|j                  |j                  t        |
                   n|j                  |
       |j                  ||	   j                                |j                  ||	   j                                 |D ]  }	|	j                  |      } |r| }|j                  |j                        }t        |      D ]  \  }	}
|
|z  ||	<     ||d   j                         |d   j                         z  j                        }|D ]  }	|	j                  |      } |j                  |j                        }t        |      D ]>  \  }	}
|
|z  } ||j                         |j                         z  j                        ||	<   @ t!        ||      S )z'
    Normalize a given annihilator
    r   )rU   r   r{   r   r   ry   ra   rz   r   r   r   r   numerdenomlcmgcdr]   )list_ofrv   r   numr  rU   r   	lcm_denomlist_of_coeffr   r   	gcd_numerfrac_anss                rF   r   r   	  s   
 CE;;DA&IM '" /1a$  quu.Aqww'  gaj!9:  # 	

=#))+, 	]1%++-./  %EE)$	% J	imm$I -( )1y=a) mB'--/-2C2I2I2KKPPQI  %EE)$	% imm$I -( K1y=!1HNN4D!D I IJaK  v66rH   c                     g }t        |       dz
  }|j                  t        | d                t        | dd       D ]%  \  }}|j                  t        |      | |   z          ' |j                  | |          |S )a*  
    Let a differential equation a0(x)y(x) + a1(x)y'(x) + ... = 0
    where a0, a1,... are polynomials or rational functions. The function
    returns b0, b1, b2... such that the differential equation
    b0(x)y(x) + b1(x)y'(x) +... = 0 is formed after differentiating the
    former equation.
    r7   r   N)r~   r   r  ry   )r}   rP   r   r   r   s        rF   r  r  	  sz     CJ!AJJwz!}%&*QR.) /1

71:
1-./ JJz!}JrH   c                 r   | j                   }| j                  }t        d |D              }|rt        |       S | j                  d   }d |D        }d}t
        j                  f}d |D        }t
        j                  }	|D ]  }
|	t        |
      z  }	 |D ]  }
|	t        |
      z  }	 |	t        |||||       z  S )z(
    Converts a `hyper` to meijerg.
    c              3   F   K   | ]  }|d k  xr t        |      |k(    ywrf  ri  ra  s     rF   rc  z$_hyper_to_meijerg.<locals>.<genexpr>
  s$     4Aa'CFaK'4s   !r   c              3   &   K   | ]	  }d |z
    ywr`  rr   ra  s     rF   rc  z$_hyper_to_meijerg.<locals>.<genexpr>
  s     	A!a%	rg  rr   c              3   &   K   | ]	  }d |z
    ywr`  rr   ra  s     rF   rc  z$_hyper_to_meijerg.<locals>.<genexpr>
  s     
Q1q5
rg  )
r  r  anyr4   r  r   r   r   r$   r&   )r  r  r  ispolyr  r  anpr  bmqr)  r   s              rF   r  r  
  s     
B	B444F4  		!A 
	B
C
&&B
"
C	A aL  aL wr3C!,,,rH   c                    t        |       t        |      k  r3t        | |      D cg c]
  \  }}||z    c}}|t        |       d z   }|S t        | |      D cg c]
  \  }}||z    c}}| t        |      d z   }|S c c}}w c c}}w )zvTakes polynomial sequences of two annihilators a and b and returns
    the list of polynomials of sum of a and b.
    N)r~   r   )list1list2r   r   rP   s        rF   r   r   &
  s     5zSZ!$UE!23Aq1u3eCJK6HH J "%UE!23Aq1u3eCJK6HHJ 43s   A>Bc                 ^   | j                   j                  | j                        s| j                         dk(  r| j                  S | j                   }|j
                  }g }| j                  }|j                  j                  }|j                         }t        |j                        D ]Z  \  }}	t        |	|j                  j                  j                        s1|j                  |j                  |	j                               \ t!        |      |k  s|t!        |      k  r|S t#        |      D cg c]  }||    ||   z   }
}t!        |      |kD  rt#        |      D cg c]  }||   	 }}nt%        |      }t#        ||z
        D ]  }d}t'        ||
      D ]\  \  }}t)        || j                        }t+        |dd      s|c c S t        |t,        t.        f      r|j1                         }|||z  z  }^ |j                  |       t3        |
      }
 ||t!        |      d z   S c c}w c c}w )zy
    Tries to find more initial conditions by substituting the initial
    value point in the differential equation.
    Tr   rq  N)r   r   r   r   r   r   rv   rU   r   ry   r}   ra   rz   r   r   r   r~   r   r   r   r  getattrr)   r*   r  r  )	Holonomicr   r   r   r}   r   r   r   r   r   list_redr   rP   r   rE   s                  rF   r   r   1
  s    ((6):R:R:TX\:\||''KAJ	BA	A+001 01![//44::;!!!%%,/0 2w{a3r7l	 #1X'  ]NZ]2 ' 'r7Q;!&q*A"Q%*B*bBq1u 
	3ACB) 1Ay||,q+t4Ia+{!;<		Aq1u IIcN(2H
	3 Bs2wxL  %' +s   .H%H*c                 4   t        | t              s| j                         S | j                  }|j	                  |       }|j                  |       }| |j                         z  ||j                         z  z   }|dz  } ||j                  |j                  f      S r^  )ra   r-   r   rW   r  r  r   )fracr   r   r  sol_num	sol_denoms         rF   r  r  _
  s|    dC yy{		A	A	AcAFFHnq1668|+G1Igkk9==)**rH   c                 |   t        | t              s| S | j                  }| j                  }t        j
                  }t        j
                  }|rddlm} t        t        |            D ]6  \  }}	|r$t        |	      j                  j                        }	||	||z  z  z  }8 t        t        |            D ]6  \  }}	|r$t        |	      j                  j                        }	||	||z  z  z  }8 t        |t        t        f      r|j                         }t        |t        t        f      r|j                         }||z  S )Nr   )mp)ra   r-   r  denr   r   mpmathr  ry   reversedr   
_to_mpmathprecr)   r*   r  )
r	  r   mpmr   r  sol_psol_qr  r   r   s
             rF   r  r  l
  s   dC AAFFEFFE
(1+& 1
%%bgg.ARU
 (1+& 1
%%bgg.ARU
 %+{34%+{345=rH   c                    | j                         }|s| j                         }nd}|sj|sh| j                         \  }	}
|	j                         rB|
j                  r6t	        |
t
              rt        |
      }
|
j                  |
j                  }}d}nd}nd}|s|s|sy|j                  |      }t        |d      \  }}| j                  |      st        ||d| g      S |r| |z  | j                  |      z
  }t        |j                  |j                   d      }|j#                  |      }|"|dk(  r|r|j%                  |       j&                  }t)        t+        |            D ]%  \  }}|dk(  rt-        t+        |            |d }|} n t)              D ]/  \  }}t	        |t.        t0        f      s|j3                         ||<   1 t5        |      i}nv|ri| j7                         \  }}||z  |z  ||j                  |      z  z   ||j                  |      z  z
  }t        |j                  |j                   d      }n|r|z  z  |z  dz
  }t        ||      j9                  	      j:                  }|j#                  |      }||dk(  r|r||dk  r|j%                  |	      j&                  }t)        t+        |            D ]N  \  }}|dk(  rt	        |t.        t0        f      r|j3                         }t5        |      
z  }t5        |      |
z  } n t	        t.        t0        f      r|j3                         }t5        |g      i}|s|st        |||      S |sj<                  }j#                  |      rt        |||      j?                         }t-        |      }tA        |      dk(  rr||d      t4        jB                  k(  rY|d   }| ||z
  |z  z  }tE        ||||      }t)        |      D cg c]  \  }}|tG        |      z   }}}||i}t        ||||      S tE        | |||      }|s|dz  }tE        | |||      }|st        ||||      S c c}}w )zO
    Converts polynomials, rationals and algebraic functions to holonomic.
    TFNr[   r   r   r7   )$is_polynomialis_rational_functionas_base_expr   ra   r	   r5   r   r  r   rX   r  r   r   r   r}   rv   r   r{   r   ry   r  r   r)   r*   r  r   as_numer_denomr<  r   r   rk  r~   r   r  r   )r  rx   r   r   r  rC   r   r  isratbasepolyratexpr  r   is_algr   r   r[   rP   r   r   r   r   r(  indicialr   r  rE   r  r  r  s                                 rF   r  r  
  s4   
 !F))+e++-&!!#(8(8&%("6*88VXXqAFFevQA!!T*EAr 88A; QD622Ri$))A,&eDoob) :"'k,,t$((C!(3-0 16 #/3E H "%( +1a+{!;< yy{E!H+ AeH%B	""$1!ebj1qvvay=(1qvvay=8eD	1q5kB"Q'33H=IIoob) :"'k^v{,,x(,,C!(3-0 16a+{!;<		A!fQ4&= %+{!;<AugJ'B	 aR00
rc1b)335Gq6Q;1QqT7aee+!AB{"A+Aq"f=L9B<9PQAA	!,QLQL!B$S!R44	$2v	.B
adAr62  S!R,, Rs   6Qc                 "   t        j                  |       }|r|\  }}}}ny |D 	cg c]
  }	||	d   z   }
}	|j                         }|d   k(  r|d   nt        j                  }|D 	cg c]
  }	||	d   z    }}	|D 	cg c]  }	|	d   	 }}	fd} ||d   |d         \  }}|
d   |z  t        |||      z  }t        dt        |            D ].  }	 |||	   ||	         \  }}||
|	   |z  t        |||      z  z  }0 |S c c}	w c c}	w c c}	w )Nr   r7   r   c                 8   | j                   d   }|j                  t              r|j                  t        t
              }|j                  d      }t        |      d   }||   }|j                         }|d   k(  r|d   nt        j                  }||z  fd| j                   d   d   D        }fd| j                   d   d   D        }fd| j                   d   d   D        }	fd	| j                   d   d   D        }
| z  t        ||f|	|
f|      fS )
Nr   F)r  r   r7   c              3   (   K   | ]	  }|z     y wrA   rr   rb  r   rE   s     rF   rc  z5_convert_meijerint.<locals>._shift.<locals>.<genexpr>       -a!e-   c              3   (   K   | ]	  }|z     y wrA   rr   r#  s     rF   rc  z5_convert_meijerint.<locals>._shift.<locals>.<genexpr>  r$  r%  c              3   (   K   | ]	  }|z     y wrA   rr   r#  s     rF   rc  z5_convert_meijerint.<locals>._shift.<locals>.<genexpr>  r$  r%  c              3   (   K   | ]	  }|z     y wrA   rr   r#  s     rF   rc  z5_convert_meijerint.<locals>._shift.<locals>.<genexpr>  r$  r%  )r  r  r
   r   r   r   collectr   r  r   r   r&   )r  r  r  dr   r   r  r  r  r  r  rE   rx   s              @rF   _shiftz"_convert_meijerint.<locals>._shift  s    IIbM558y#&AIIa%I(GAJaDMMOaDAIAaD166E-TYYq\!_--TYYq\!_--TYYq\!_--TYYq\!_-1"ugr2hR!444rH   r  )r'   	_rewrite1r  r   r   r  r   r~   )r  rx   r   rC   r  facpor  r   r   fac_listr  r  po_listG_listr+  r(  r  rP   s    `                 rF   r  r  
  sC   tQ'DRA %&&qad
&H&
A!	!qvvA!"#Aq1Q4x#G#qadF5& fQi,HE1
1+
Q& Q
QC 1c&k" W&)WQZ0qx{U"\!hv%VVVW JE ' $s   D)D>Dc                 J    d	 fd	}|j                  t              }t        |d      \  }} |t        t              |dz  dz   t        dddg        |t	        t              |dz  dz   t        dddg        |t        t              |dz
  t        dd        |t        t              |t        |dz  z  z   t        dddg        |t        t              dt        z  |z  |dz  z   t        dddt        t              z  g        |t        t              dt        z  |z  |dz  z   t        dddt        t              z  g        |t        t              dt        z  |z  |dz  z   t        dddt        t              z  g        |t        t              |dz  dz
  t        dddg        |t        t              |dz  dz
  t        dddg        |t        t              t        d|z  z   t        |dz  z  z   t                |t        t              t        |z  d|dz  z  z   t        |dz  z  z   t                |t!        t              t        |z  d|dz  z  z   t        |dz  z  z   t                |t#        t              t         |z  d|dz  z  z   t        |dz  z  z   t               y)
zi
    Creates the look-up table. For a similar implementation
    see meijerint._create_lookup_table.
    r   c           	          j                  t        | t              g       j                  | t	        ||||      f       y)z2
        Adds a formula in the dictionary
        N)r  r  r  r   r   )formular   argr   r   tables        rF   addz_create_table.<locals>.add*  s<     	#.3::Gk3B7<9 	:rH   r[   r   r7   r   N)r   rr   )r   r  rX   r   r   r   r   r!   r   r   r"   r#   r   r   r   r    r   r   )r6  rC   r7  r   r   r[   s   `     rF   r  r  $  s-   : 	S!A!!T*EAr C"a%!)S!aV,C"a%!)S!aV,C"q&#q!$C"s2q5y.#q1a&1C!C%(RU"CQ$r(
O<S	1S58b!e#S!aDH-=>S	2c6"9r1u$c1q!DH*o>S	2q519c1q!f-S	2q519c1q!f-S	32:BE	)3/3R!BE'!CAI-s33R!BE'!CAI-s3C3$r'Ab!eG#c"a%i/5rH   c                    g }t        |      D ]s  }| j                  ||      }t        |t              rt	        | ||      }|j
                  du st        |t              r y |j                  |       | j                  |      } u |S r  )r   r   ra   r   r2   rq  r   r   )r  rx   r   r   r   r   r  s          rF   r  r  I  sy    	B5\ ii2c3a$C==E!ZS%9
		#yy| IrH   )r   F)Nr   NNNTr  r  )srp   
sympy.corer   r   r   sympy.core.numbersr   r   r   r	   r
   r   r   sympy.core.singletonr   sympy.core.sortingr   sympy.core.symbolr   r   sympy.core.sympifyr   (sympy.functions.combinatorial.factorialsr   r   r   &sympy.functions.elementary.exponentialr   r   r   %sympy.functions.elementary.hyperbolicr   r   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r   r   'sympy.functions.special.error_functionsr   r   r    r!   r"   r#   'sympy.functions.special.gamma_functionsr$   sympy.functions.special.hyperr%   r&   sympy.integralsr'   sympy.matricesr(   sympy.polys.ringsr)   sympy.polys.fieldsr*   sympy.polys.domainsr+   r,   sympy.polys.polyclassesr-   sympy.polys.polyrootsr.   sympy.polys.polytoolsr/   sympy.polys.matricesr0   sympy.printingr1   sympy.series.limitsr2   sympy.series.orderr3   sympy.simplify.hyperexpandr4   sympy.simplify.simplifyr5   sympy.solvers.solversr6   r  r8   r9   r:   holonomicerrorsr;   r<   r=   r>   rQ   rX   rS   r]   r   r  r  r  r  r  sympy.integrals.meijerintr  r  r   r  r  r   r   r  r  r  r  r  r  rr   rH   rF   <module>rY     sR  
 % $   " & + & L L F F > 9 E E R R 9 8 % ! ) * & ' ' & -  % $ 2 - ' R R) )#,LC CLhG hGVf fR7J4Z 54 M4` El  -_:H67r(-<+!\
+> '(DbSW h-V *.b +\ !# "6J
rH   