
    sgai                       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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/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF ddlGmHZH ddlImJZJ ddlKmLZL ddlMmNZN ddlOmPZP ddlQmRZR dd lSmTZTmUZUmVZV dd!lWmXZXmYZYmZZZm[Z[ d" Z\d# Z] G d$ d%      Z^ G d& d'      Z_ G d( d)      Z`d=d+Zadd*d,e=fd-Zb ed.      Zcd/add/aedd0lfmgZg d>d1Zhd?d2Zid3 Zjd4 Zkd5 Zld6 Zmd7 Znd@d8Zodd/d/e=d,fd9Zpd,e=fd:Zqe=fd;Zrd< Zsy/)AzL
This module implements Holonomic Functions and
various operations on them.
    )AddMulPow)NaNInfinityNegativeInfinityFloatIpiequal_valued
int_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)r1   onesdomain)shapers    L/var/www/html/venv/lib/python3.12/site-packages/sympy/holonomic/holonomic.py<lambda>z(_find_nonzero_solution.<locals>.<lambda>+   s    **5!((;     r   r8   )_solverE   	transpose)rF   homosysrC   
particular	nullspacenullitynullpartsols   `       rG   _find_nonzero_solutionrR   *   sT    ;DHHW-J	ooa GQL!I-H 
+
+
-CJrI   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      rG   DifferentialOperatorsrY   4   s"    D 'tY7D$**++rI   c                   &    e Zd ZdZd Zd ZeZd Zy)rT   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)	rV   DifferentialOperatorzeroonerU   r   
gen_symbol
isinstancestr)selfrV   rW   s      rG   __init__z$DifferentialOperatorAlgebra.__init__   sl    	#7YY!4$)  $Tu=DO)S)"("FIv."+ /rI   c                 r    dt        | j                        z   dz   | j                  j                         z   }|S )Nz9Univariate Differential Operator Algebra in intermediate z over the base ring )r2   ra   rV   __str__)rd   strings     rG   rg   z#DifferentialOperatorAlgebra.__str__   s<    L4??#$&<=YY!" rI   c                 j    | j                   |j                   k(  xr | j                  |j                  k(  S rB   )rV   ra   rd   others     rG   __eq__z"DifferentialOperatorAlgebra.__eq__   s.    yyEJJ& 3%"2"22	3rI   N)__name__
__module____qualname____doc__re   rg   __repr__rl    rI   rG   rT   rT   Z   s    $L, H3rI   rT   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   r8   N)parentrV   rb   gensr   x	enumeratedtype
from_sympyr   to_sympy
listofpolylenorder)rd   list_of_polyrv   rV   ijs         rG   re   zDifferentialOperator.__init__   s     {{!+DIIaL&!A1tyyQR|TU
 l+ 	DDAqa,"&//'!*"=Q"&//$--2B"CQ		D ')A-
rI   c                      j                   }t        |t              r|j                   }n]t        | j                  j                  j
                        r|g}n/ j                  j                  j                  t        |            g}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                 ^    t        |t              r|D cg c]  }|| z  	 c}S | |z  gS c c}w rB   )rb   list)blistofotherr   s      rG   _mul_dmp_diffopz5DifferentialOperator.__mul__.<locals>._mul_dmp_diffop   s3    +t,'23!A33O$$ 4s   *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 rB   )	rv   rV   r_   rb   r   appenddiffr{   
_add_lists)r   sol1sol2r   rd   s       rG   
_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))rI   r8   )r}   rb   r^   rv   rV   rz   r{   r   ranger~   r   )rd   rk   
listofselfr   r   rQ   r   r   s   `       rG   __mul__zDifferentialOperator.__mul__   s     __
e12**Kt{{//556 'K;;++66wu~FGK	%
 jm[9	* q#j/* 	OA$[1KS/*Q-"MNC		O $C55rI   c                 B   t        |t              st        || j                  j                  j                        s.| j                  j                  j                  t        |            }| j                  D cg c]  }||z  	 }}t        || j                        S y c c}w rB   )rb   r^   rv   rV   rz   r{   r   r}   )rd   rk   r   rQ   s       rG   __rmul__zDifferentialOperator.__rmul__  s{    %!56eT[[%5%5%;%;<))55genE&*oo65196C6'T[[99 7
 7s   7Bc                    t        |t              r6t        | j                  |j                        }t        || j                        S | j                  }t        || j                  j
                  j                        s0| j                  j
                  j                  t        |            g}n|g}|d   |d   z   g|dd  z   }t        || j                        S )Nr   r8   )	rb   r^   r   r}   rv   rV   rz   r{   r   )rd   rk   rQ   	list_self
list_others        rG   __add__zDifferentialOperator.__add__  s    e12T__e.>.>?C'T[[99OO	%!1!1!7!78 KK--99'%.IJJJ|jm+,y}<#C55rI   c                     | d|z  z   S Nrr   rj   s     rG   __sub__zDifferentialOperator.__sub__)  s    rUl""rI   c                     d| z  |z   S r   rr   rj   s     rG   __rsub__zDifferentialOperator.__rsub__,  s    d{U""rI   c                     d| z  S r   rr   rd   s    rG   __neg__zDifferentialOperator.__neg__/      DyrI   c                 .    | t         j                  |z  z  S rB   r   Onerj   s     rG   __truediv__z DifferentialOperator.__truediv__2      quuu}%%rI   c                    |dk(  r| S t        | j                  j                  j                  g| j                        }|dk(  r|S | j                  | j                  j
                  j                  k(  r\| j                  j                  j                  g|z  | j                  j                  j                  gz   }t        || j                        S | }	 |dz  r||z  }|dz  }|s	 |S ||z  })Nr8   r      )r^   rv   rV   r`   r}   rU   r_   )rd   nresultrQ   rx   s        rG   __pow__zDifferentialOperator.__pow__5  s    6K%t{{'7'7';';&<dkkJ6M??dkk==HHH;;##(()!+t{{/?/?/C/C.DDC'T[[991u!!GA FA rI   c                    | j                   }d}t        |      D ]  \  }}|| j                  j                  j                  k(  r*| j                  j                  j                  |      }|dk(  r|dt        |      z   dz   z  }i|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 + r8   z)*%sz*%s**)r}   ry   rv   rV   r_   r|   r2   ra   )rd   r}   	print_strr   r   s        rG   rg   zDifferentialOperator.__str__I  s    __
	j) 	[DAqDKK$$)))  ))!,AAvS47]S00	U"	AvS47]Vdkk6L6L-MMM	tAw,w9O9O/PPSWXYSZZZI#	[& rI   c                      t        |t              r4 j                  |j                  k(  xr  j                  |j                  k(  S  j                  d   |k(  xr! t	         fd j                  dd  D              S )Nr   c              3   b   K   | ]&  }|j                   j                  j                  u  ( y wrB   rv   rV   r_   ).0r   rd   s     rG   	<genexpr>z.DifferentialOperator.__eq__.<locals>.<genexpr>i  s&     HqT[[%%***Hs   ,/r8   )rb   r^   r}   rv   allrj   s   ` rG   rl   zDifferentialOperator.__eq__d  sp    e12??e&6&66 /;;%,,./q!U* IHDOOAB4GHH	IrI   c                     | j                   j                  }|t        |j                  | j                  d         | j
                        v S )zH
        Checks if the differential equation is singular at x0.
        r   )rv   rV   r/   r|   r}   rx   )rd   x0rV   s      rG   is_singularz DifferentialOperator.is_singulark  s;    
 {{U4==)<=tvvFFFrI   N)rm   rn   ro   rp   _op_priorityre   r   r   r   __radd__r   r   r   r   r   rg   rq   rl   r   rr   rI   rG   r^   r^      s\    !F L.<,6\:6 H##&(2 HIGrI   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   )rd   r   rx   r   r   s        rG   re   zHolonomicFunction.__init__  s!    , &rI   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_condrc   r   r2   rx   r   r   )rd   str_sols     rG   rg   zHolonomicFunction.__str__  sw    !=@AQAQ=RTVVd477mT$'']<G  " 699I9I5JTVVG rI   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   rV   domunifyold_poly_ringrx   rY   rc   ra   r}   r|   r^   r   r   r   )rd   rk   R1R2dom1dom2R	newparent_r   r   r   s               rG   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)rb   r   dictr   r   s    rG   is_singularicsz HolonomicFunction.is_singularics   s+     dggt$& 'rI   c                 ,    t        | j                        S )z@
        Checks if the function have initial condition.
        )boolr   r   s    rG   r   z!HolonomicFunction._have_init_cond  s     DGG}rI   c           
         t        | j                        d   }| j                  |   }t        | j                        dk(  r|t        |      k(  r|dkD  r{t        |      }t        j
                  g|z  }|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   r8   N)r   r   r~   intr   Zerory   r   r   r   rx   r   )rd   ar   r   r   r   s         rG   _singularics_to_ordz%HolonomicFunction._singularics_to_ord  s     M!GGAJtww<1c!fQAA&&AB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 ]q  }|t        |j                        k\  r|j                  |j                         7|j                  |j                  |j                  |   j                                      s |j                  |        t        |t        |      |dz   f|      j!                         }t        j"                  |dz   df|      }t%        ||      }|j&                  r8|dz  }||	d   z  }|	j                  |       ||
d   z  }|
j                  |       |	|
z   }g }|D ]  }g }t        |dz         D ]q  }|t        |j                        k\  r|j                  |j                         7|j                  |j                  |j                  |   j                                      s |j                  |        t        |t        |      |dz   f|      j!                         }t        j"                  |dz   df|      }t%        ||      }|j&                  r8|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   r8   Fnegativer   T)$r   rv   rV   r   r   max	get_fieldrU   r   r   r~   r}   r_   newto_listr1   rK   zerosrR   is_zero_matrixflat
_normalizer   r   rx   r   r   
_extend_y0zipr   
change_icsry   r   r   r   r   )rd   rk   r   r   deg1deg2dimr   Krowsself	rowsothergenr   diff1diff2rowrF   exprprL   rQ   r   y1y2r   selfat0otherat0selfatx0	otheratx0r   _y0s                                  rG   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^ BDOO,,HHQVV$HHQUU4??1#5#=#=#?@A	B
 HHQK	 SXs1u-q1;;=$$c!eQZ3$Q0   1HC8B<'EOOE"9R=(EU#Y&CA sQw FAC00(tq'9'A'A'C!DE	F
  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 &&2215G((44Q7Hww!|GHe..q111xx1}WXq)E11''33DGG<H))55dgg>IIe..tww777??588,u44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::e 54 D E >s   3_&__6_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   r8   z1logarithmic terms in the series are not supported__iter__z4Definite integration for singular initial conditions   r   FN)r   rv   rU   r   r   	integrater   ry   r   r   r   NotImplementedErrorhasattrr   rx   r   r   r~   to_exprr=   r<   subsrb   r   r3   evalf	is_Number)rd   limitsr   DrF   r   r   cc2r   cjr   r   r   definiteindefinite_integralindefinite_exprloweruppers
indefinites                        rG   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   r8   NFr   )
setdefaultrx   r   r   r~   r   r   r   r}   rv   rV   r_   r   r^   r   r   r   r   r   r   r   r   insert_derivate_diff_eqr   r`   r   r   )rd   argskwargsrQ   r   annr   r   seq_dmfrhsr   s              rG   r   zHolonomicFunction.diff4  ss   , 	*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/2~~>!155%>> (/qr{3!q71:~33

1aff  Q' qvvquuo.QR$"2"2"9"9EJ##%)<)<)>$)F$S$&&11cii!m,QR0 dffdggr::% ? 4s   &K6?K;c                    | j                   |j                   k7  s| j                  |j                  k7  ry| j                         rD|j                         r4| j                  |j                  k(  xr | j                  |j                  k(  S y)NFT)r   rx   r   r   r   rj   s     rG   rl   zHolonomicFunction.__eq__  sg    u000DFFegg4E!e&;&;&=77ehh&>477ehh+>>rI   c                 $    | j                   }t        |t              st        |      }|j	                  | j
                        rt        d      | j                         s| S t        | |j                        }|D cg c]/  }t        j                  || j
                        |z  j                  1 c}t        || j
                  | j                        S | j                   j                  j                  |j                   j                  j                  k7  r| j!                  |      \  }|z  S |j                   }|j                  |j                  }|j                  j                  }|j#                         }|j$                  D cg c]!  }|j                  |j'                               # }	}|j$                  D cg c]!  }|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;        |      |      |   <   j|      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 |jA                  t)              D cg c]  }t)        |      D ]
  }||   |     c}}       t/        |tC        |      |z  f|      j1                         }t5        ||      }|j6                  rtE        |jG                         | j                   j                  d      }| j                         r|j                         st        || j
                        S | jI                         dk(  r|jI                         dk(  ru| j                  |j                  k(  rMt        | |j                        }t        ||j                        }|d   |d   z  g}t)        dtK        tC        |      tC        |                  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tM              |   |<    2 d}t)        dz         D ],  t)        dz         D ]  }||   |   |   z  ||   z  z  } . |jA                  |        t        || j
                  | j                  |      S | j                   jO                  d      }|j                   jO                  d      }| j                  dk(  r|s|s| |jQ                  d      z  S |j                  dk(  r|s|s| jQ                  d      |z  S | j                   jO                  | j                        }|j                   jO                  | j                        }|s |s| |jQ                  | j                        z  S | jQ                  |j                        |z  S | j                  |j                  k7  rt        || j
                        S d d  | jI                         dk(  re|jI                         dk(  rRtS        | jT                        D cg c]  \  }}|tW        |      z   }}}tX        jZ                  |i|jT                   n| jI                         dk(  re|jI                         dk(  rRtS        |jT                        D cg c]  \  }}|tW        |      z   }}}| jT                  tX        jZ                  |i n>| jI                         dk(  r+|jI                         dk(  r| jT                  |jT                   i }D ]   D ]  tK        tC                 tC                        }t)        |      D cg c]6  t]         fdt)        dz         D        tX        jZ                  	      8 }}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 c c}}w c c}}w c c}w c c}}w )
Nz> Can't multiply a HolonomicFunction and expressions/functions.r8   r   r   Fr   Tc              3   F   K   | ]  }   |      |z
     z    y wrB   rr   )r   r   r   r   r   r   r   s     rG   r   z,HolonomicFunction.__mul__.<locals>.<genexpr>!  s*     Ha"Q%(RU1q5\1Hs   !start)0r   rb   r   r   hasrx   r   r   r   r   r0   r   repr   rv   rV   r   r   r}   r   r   r_   r`   r1   rK   r   rR   r   rz   DMFdiffr   is_zeror   r~   r   r   r   minr   r   r   ry   r   r   r   r   sumr   )!rd   rk   ann_selfr   r   r   	ann_otherr   r   r   r   r   self_red	other_red	coeff_mullin_sys_elementslin_syshomo_sysrQ   sol_anny0_selfy0_othercoeffkr   r   r   r   r   r   r  r   r   s!       `      `                 ` @@rG   r   zHolonomicFunction.__mul__  s	   ##%!23ENEyy )*jkk'')D(..1B=?@488Atvv&.33@B$XtvvtwwCC""''5+<+<+C+C+H+HH::e$DAqq5L%%	NNOOOO  KKM191D1DEAQUU199;'E	E2;2F2FGQaeeAIIK(G
G ;@(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/1*E	!Q*3A,q/*>*>tvv*F	!QGG 1q5\ )Q<?**q FAaLOy|il1o'EEOF"#&&	!Q) 1X )Q<?a'q EAaLOx{Yq\!_'DDOE"#&&	!Q) ##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 &&2215G((44Q7Hww!|GHe..q111xx1}WXq)E11''33DGG<H))55dgg>IIe..tww777??588,u44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/05a:+, H5Q<H vv' : :1u{ !Bq1uI36q"QU)3D E41aQ EBq1uIF	F !$&&$''2>>g A FG DF 4J RH %Z. 7ML D E:
 !Fsr   ?4i
&i &i5ii=ii%ii%i+
i6.	i17i62i;*j ;jj
i1i6c                     | |dz  z   S r   rr   rj   s     rG   r   zHolonomicFunction.__sub__+  s    ebj  rI   c                     | dz  |z   S r   rr   rj   s     rG   r   zHolonomicFunction.__rsub__.  s    by5  rI   c                     d| z  S r   rr   r   s    rG   r   zHolonomicFunction.__neg__1  r   rI   c                 .    | t         j                  |z  z  S rB   r   rj   s     rG   r   zHolonomicFunction.__truediv__4  r   rI   c                    | 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      | j                   j                  j                   }
t        |
| j                  t"        j$                  t"        j&                  g      }|dk(  r|S | }	 |dz  r||z  }|dz  }|s	 |S ||z  }c c}w )Nr8   r   z&Negative Power on a Holonomic Functionr   )r   r   rv   r   r   r}   r0   r   rx   r"  rV   r|   r^   r   r   r?   rU   r   r   r   )rd   r   r  rv   r   p0p1r   rQ   ddr\   r   rx   s                rG   r   zHolonomicFunction.__pow__7  sm   !!Q&""CZZFww477mA&!+,"B"B((2tvv&*//B57H=q6;;''*=C=%c62B$R"==q5#$LMM$$88"2tvvqvvw?6M1u!!GA FA  >s   ."F	c                 N    t        d | j                  j                  D              S )zF
        Returns the highest power of `x` in the annihilator.
        c              3   <   K   | ]  }|j                           y wrB   degreer   r   s     rG   r   z+HolonomicFunction.degree.<locals>.<genexpr>]  s     C!188:C   )r   r   r}   r   s    rG   r?  zHolonomicFunction.degreeY  s!     Ct'7'7'B'BCCCrI   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   Tr8   r   NFr   )r   rv   r   r   rx   r}   ry   rb   rV   rz   r|   r  r   r   r   r   r)   rK   r   gauss_jordan_solver   r   r   r   )rd   r   r  r  r   r   r   r}   r   r   rF   r  coeffssystemhomogeneousr   coeffs_nextrQ   taustaus                       rG   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 ]k  |v rFt-        fd|j/                         D        t0        j2                  	      }|j5                  |       M|j5                  t0        j2                         m t7        ||      }|j8                  }t;        |j                  j#                  |j                  d
         d      }|j=                         }|rt)        |      dz   }t)        ||      }||z  }t?        | |      }t        |      D cg c]  \  }}|tA        |      z   }}}t        |      |k  r t!        |      D ]  }t0        j2                  }|D ]  |d   z   dk  rt0        j2                  ||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||   jC                  |      ||d   z      z  z  } |j5                  |        tE        |g| }tG        |tH              rt!        t        |      |      D ]J  }||vrt        d|z        ||<   ||   |v r|j5                  |||             7|j5                  ||          L |rtK        ||      |fgS tK        ||      gS t!        t        |      |      D ]X  }||vrt        d|z        ||<   d}|D ]#  ||   v s|j5                  ||             d}% |rE|j5                  ||          Z |rtK        ||      |fgS tK        ||      gS c c}w 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Snr8   c              3   ^   K   | ]$  \  }}|d    k(  r|j                  z
         & ywr   Nr  r   r3  vr   r  r   s      rG   r   z0HolonomicFunction.to_sequence.<locals>.<genexpr>  s8      C#q!!	 FF1a%i0 C   *-r  r   ZfilterC_%sF)&r   shift_xto_sequencer   r   
_frobeniusr   rv   rV   r   r;   r   ry   r}   
all_coeffsr~   r   r|   r   r%  r   r?  r&  itemsr   r   r   r:   r   r/   keysr   r   r  r7   rb   r   r9   )rd   rL  dict1r   r   r   r   	listofdmpr?  r3  r2  rQ   keylistr  
smallest_ndummyseqsunknownstempr   	all_rootsmax_rootr   r   u0eqsoleqsr  r  r   s                          `    @@rG   r[  zHolonomicFunction.to_sequence  sf   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| C',{{}C!") 

4 

166"	# !a( 		!&&//#..*<=qM	NN$	9~)HXz2Je$+4R=941aa)A,99 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*++K (J :s    T;3U c                 p  ,- | 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 ]`  }t        |      dk(  r|j                  |g       $|D ]&  t        d   |z
        sj                  |        N |j                  |g       b t        d |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|D cg c]  }t        |      |k(  r| }}ne|
sc| j!                         r%t#        | j                  d         j$                  d	k(  rt        |      g}n!|D cg c]
  }|dk\  s	| }}t        |      g}t'        d
d      -| j(                  j*                  j,                  j.                  }t1        |j3                  -      d      \  }}g }t5        d      }D ]  }i }t7        | j(                  j8                        D ]  \  }j;                         }t        |      dz
  }t=        |dz         D ]  }|||z
     }|dk(  r||z
  ||z
  f|v r;|||z
  ||z
  fxx   |j?                  |      tA        -|z
  dz   |z   |      z  z  cc<   X|j?                  |      tA        -|z
  dz   |z   |      z  |||z
  ||z
  f<     g }|D cg c]  }|d   	 }}t        |      ,tC        |      }tC        d |D              }t        d |D              },|z   }i }g } g }!t=        ,|dz         D ]k  |v rFtE        ,-fd|jG                         D        t"        jH                        }"|j                  |"       M|j                  t"        jH                         m tK        ||      }|jL                  }#tO        |j,                  j?                  |j8                  d         -d      }$|$j                         }$|$rtC        |$      dz   }%tC        |%|      }|#|z  }#g }&| j                         dk(  r| j                  |   }&n| j                         d	k(  r|dk\  rt        |      |k(  rwt        |      dk(  ritQ        | |#t        |      z         }'t        |'      t        |      kD  r:t=        t        |      t        |'            D cg c]  }|'|   tS        |      z   }&}t        |&      |#k  rNt=        ||      D ]  }t"        jH                  }(|D ]  |d   z   dk  rt"        jH                  ||d   z   <   nx|d   z   t        |&      k  r|&|d   z      ||d   z   <   nO|d   z   |vrEtU        |      d|d   z   z  z   })t'        |)      ||d   z   <   |!j                  ||d   z             d   |k  s|(|   jW                  -|      ||d   z      z  z  }( | j                  |(        tY        | g|! }*t[        |*t\              rt=        t        |&      |#      D ]X  }||vrtU        |      d|z  z   })t'        |)      ||<   ||   |*v r|&j                  |*||             E|&j                  ||          Z |r |j                  t_        ||&      ||f       |j                  t_        ||&      |f       t=        t        |&      |#      D ]f  }||vrtU        |      d|z  z   })t'        |)      ||<   d	}+|*D ]#  ||   v s|&j                  ||             d}+% |+rS|&j                  ||          h |r|j                  t_        ||&      ||f       n|j                  t_        ||&      |f       |dz  } |S c c}w c c}w c c}w c c}w c c}w c c}w )Nc                     | d   S )Nr8   rr   rx   s    rG   rH   z.HolonomicFunction._frobenius.<locals>.<lambda>j  
    !A$ rI   )keyc                     | d   S Nr   rr   ro  s    rG   rH   z.HolonomicFunction._frobenius.<locals>.<lambda>k  rp  rI   r   c              3   8   K   | ]  }t        |      d k(    ywr8   N)r~   r@  s     rG   r   z/HolonomicFunction._frobenius.<locals>.<genexpr>~  s     3!#a&A+3s   c              3   &   K   | ]	  }|d k\    ywrQ  rr   r@  s     rG   r   z/HolonomicFunction._frobenius.<locals>.<genexpr>  s     +Q!V+   c              3   2   K   | ]  }t        |        y wrB   )r   r@  s     rG   r   z/HolonomicFunction._frobenius.<locals>.<genexpr>  s     2qZ]2s   TFr   rM  rO  Cr8   c              3   &   K   | ]	  }|d      ywru  rr   r@  s     rG   r   z/HolonomicFunction._frobenius.<locals>.<genexpr>  s     -!1-rw  c              3   &   K   | ]	  }|d      ywru  rr   r@  s     rG   r   z/HolonomicFunction._frobenius.<locals>.<genexpr>  s     .1!A$.rw  c              3   ^   K   | ]$  \  }}|d    k(  r|j                  z
         & ywrQ  rR  rS  s      rG   r   z/HolonomicFunction._frobenius.<locals>.<genexpr>  s8       G#'1aAaDAI !"q!e) 4  GrU  r  r   rV  rW  z_%s)0	_indicialr   r_  is_realextendas_real_imagsortr~   r   r   r   r   r   r   r%  r   r   r   	is_finiter   r   rv   rV   r   r;   r   ordry   r}   r]  r   r|   r   r   r&  r^  r   r:   r   r/   r   r   chrr  r7   rb   r   r9   ).rd   rL  indicialrootsrealscomplr   r   r   grpindependentallposallintrootstoconsiderr   posrootsr   r   r   finalsolcharr   r`  ra  r?  r3  r2  rQ   rb  r  degree2rc  rd  re  rf  rg  r   rh  ri  rj  r   rk  letterrl  r  r  r   s.                `                              @@rG   r\  zHolonomicFunction._frobenius\  s   (++-. 	=AyyaS=#334~~'1q!Qi[=+;;<	= 	


'


'

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

A3  adQh'HHQK 
 

A3		  3s33+U++2E22  D( OTWW\\^, 2 !3!3!56 2A#Aq)'..q122
 "5zlO-01qt154IaQqT4IIO*/CQs1v{qCOC'')Qtwwqz]-D-D-N#&u:, (-7!QA77#&x=/3%%%**.."3#4#4Q#7>13x  @	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-u--F...GJFCH5%!), '<  G+0;;= G%&VV-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#8=c!fc"g8NO1"Q%)A,.OBO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 ?@AIDA@	B m 24I D 8< ,T Ps0   -b?bb$+b$:
b)b)&b.b3c                    || j                         }n|}t        |t              rt        |      dk(  r|d   }dnt        |t              rt        |      dk(  r|d   |d   }nzt        |      dk(  rt        |d         dk(  r|d   d   }dnPt        |      dk(  r"t        |d         dk(  r|d   d   |d   d   }n |D cg c]  }| j	                  |       c}S |t              z
  }t        |j                        dz
  }|j                  j                  }| j                  | j                  }	|j                  j                  }
|j                  j                  j                  }|j                         }|
D cg c]!  }|j                  |j!                               # }}t#        |      D cg c]  }||    ||   z   c}t%        |j                        |dz   |k  rZt#        |dz   |z
  ||z
        D ]B  t'        fdt#        |      D        t(        j*                        }j-                  |       D |rS t'        fdt/              D        t(        j*                        }|r|t1        |t              z   z        z  }|	dk7  r|j3                  |	z
        S |S c c}w c c}w 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   r8   )_recurc              3   ^   K   | ]$  }|z   d k\  rt        |         |z      z   & ywrQ  )DMFsubs)r   r   r   rQ   subs     rG   r   z+HolonomicFunction.series.<locals>.<genexpr>i  s;      = !Q! %SVQ/#a!e*< =rU  r  c              3   :   K   | ]  \  }}|z   z  |z    y wrB   rr   )r   r   r   constantpowerrx   s      rG   r   z+HolonomicFunction.series.<locals>.<genexpr>p  s$     I$!Q1q=()A-I   )r[  rb   tupler~   seriesr   rj  
recurrencer   rx   r   r}   rv   rV   r   r   r   r   r   r&  r   r   r   ry   r4   r  )rd   r   coefficientr   r  r  r   lr3  r   seq_dmpr   r   r   seqr2  serr  rQ   r  rx   s         `          @@@@rG   r  zHolonomicFunction.series(  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)J3=>aDKKqK)>>M""
"!!''FFWW''22!!((--KKM+23aquuQYY[!33).q2AAwQ2:==!q5191q519a!e, " =%*1X=DEFFL

5!"
 JI)C.I 5Q]!334a88C788Aq2v&&
= ? 42s   J2&J77J<c                   	
 | j                   dk7  r)| j                  | j                         j                         S | j                  j                  }| j                  j
                  j                  	| j                  	j                  }	j                  }	fd
t        d |D              }dt        d|      t        d| j                  j                        z   z  
fdt        fdt        |      D              }t        |      D ]\  \  }}|j                         }t        |      dz
  }d||z   cxk  r|k  rn n||||z
  |z
     |z  z   }|	j!                  |z
        z  }^ t#        	j%                  |            S )z:
        Computes roots of the Indicial equation.
        r   c                 n    t        j                  |       d      }d|j                         v r|d   S y)NrV  rW  r   )r/   r|   r_  )polyroot_allr   rx   s     rG   _pole_degreez1HolonomicFunction._indicial.<locals>._pole_degree  s5    QZZ-q=HHMMO#{"rI   c              3   <   K   | ]  }|j                           y wrB   r>  )r   r   s     rG   r   z.HolonomicFunction._indicial.<locals>.<genexpr>  s     4AQXXZ4rA  
   r8   c                 0    | j                   rS  |       S rB   )r$  )qr  infs    rG   rH   z-HolonomicFunction._indicial.<locals>.<lambda>  s    qyy l1o rI   c              3   :   K   | ]  \  }} |      |z
    y wrB   rr   )r   r   r  degs      rG   r   z.HolonomicFunction._indicial.<locals>.<genexpr>  s     =tq!A
=r  )r   rZ  r}  r   r}   rv   rV   rx   r_   r`   r   r   r%  ry   r]  r~   r{   r/   r|   )rd   
list_coeffr  yr?  r   r   r   ra  r   r  r  r  rx   s            @@@@@rG   r}  zHolonomicFunction._indicialx  sT   
 77a<<<(2244%%00
##((FFFFEE	 444C6NSD,<,<,B,B%CCD==y'<==j) 	%DAqI^a'FAE#V#	&1*q.1A55a!e$$A	% QZZ]A&&rI   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   r8   )sympy.holonomic.numericalr  r  r   r   r  r   r   r   r   r/   r   rv   rV   r|   r}   rx   r>   )rd   pointsr  hr  r  lpr   r   r   r   s              rG   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KrI   c                 j   | j                   j                  j                  j                  }|j	                  |      }t        |d      \  }}| j                   j                  D cg c]  } ||j                                }}t        ||      }t        ||| j                  | j                        S c c}w )z
        Changes only the variable of Holonomic Function, for internal
        purposes. For composition use HolonomicFunction.composition()
        r\   )r   rv   rV   r   r   rY   r}   r   r^   r   r   r   )rd   zr   r   rv   r   r   rQ   s           rG   change_xzHolonomicFunction.change_x  s     %%**..a )!T2	'+'7'7'B'BC!q~CC#C0 a$'':: Ds   #B0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   rV   r{   r|   r  r^   r   r   r   r   )rd   r   rx   listaftershiftrV   r   rQ   r   s           rG   rZ  zHolonomicFunction.shift_x  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                    || 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'        |      st)        |      }|t        |      k  r>t        ||   t*        t,        f      r||   j/                         ||<   |||   |	|z  z  z  }n|t1        d	|z        |	|z  z  z  } t        |t*        t,        f      r|j/                         |	|z  z  }n||	|z  z  }|r |
dk7  r|j3                  |	|	|
z
        fgS |fgS |
dk7  r|j3                  |	|	|
z
        S |S ||z   t        |      kD  rt5        d
      t7        fdj                  dd D              rt9        | | j                        j                  d   }j                  d   }t        |j;                         t*        t,        f      ryt!        |j;                         j/                               ||j=                         z  z   t!        |j;                         j/                               ||j=                         z  z  z  }n\t!        |j;                               ||j=                         z  z   t!        |j;                               ||j=                         z  z  z  }d}t        j                  j                  j                  |      |j                        }t        j                  j                  j                  |      |j                        }|rg }t?        ||z         D ]  }||k  rU|r;jA                  t!        ||         |	||z   z  z  j3                  |	|	|
z
        f       n|t!        ||         |	|z  z  z  }^t!        ||         dk(  rpg }g }tC        |jE                               D ])  }|jG                  tI        ||z
  |z        g||   z         + tC        |jE                               D ])  }|jG                  tI        ||z
  |z        g||   z         + d|v r|jK                  d       n|jA                  d       |rajA                  t!        ||         |	||z   z  z  j3                  |	|	|
z
        tM        ||||	|z  z        j3                  |	|	|
z
        f       |t!        ||         tM        ||||	|z  z        z  |	|z  z  z  } |rS ||	|z  z  }|
dk7  r|j3                  |	|	|
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   r8   r   r   )as_listr  r   rW  rY  z+Can't compute sufficient Initial Conditionsc              3   d   K   | ]'  }|j                   j                  j                  k7   ) y wrB   r   r   r   rF   s     rG   r   z-HolonomicFunction.to_hyper.<locals>.<genexpr>m  s$     C1qAHHMM&&&Cs   -0r   )'r[  rb   r  r~   to_hyperrj  r  rx   r   r   r/   rv   rV   r|   r}   r   r   r   ry   r   r   r*   r+   as_exprr   r  r   anyr=   LCr?  r   r   r   r_  r  r6   remover&   )rd   r  r  r  rc  r  rQ   r   rj  rx   r   mnonzerotermsr   r   r   r  arg1arg2	listofsolapbqr3  rF   s                          @rG   r  zHolonomicFunction.to_hyper  sK   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q5
1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 CQr0BCC%dDGG44LLOLL addf{K89QTTV^^%&QXXZ89Qqttv~~?O=PSTWXW_W_WaSb=bcAQTTV9q188:./1QTTV9q188:3NOA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&&
rI   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

        )r5   r  simplifyr   s    rG   r  zHolonomicFunction.to_expr  s    & 4==?+4466rI   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   lenicsrD   F)r  )r~   r   r   r   rv   rV   rD   expr_to_holonomicr  rx   r<   r=   r   r  r   )rd   r   r  symbolicr   rQ   r   s          rG   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  r8   r   r   )r  r   r   r~   _hyper_to_meijerg)rd   r"  rQ   r   s       rG   
to_meijergzHolonomicFunction.to_meijerg  ss    , mmDm)ff 	6A1v{qtQ1qt/!555	6 
rI   rQ  FT)   FTN)RK4g?F)FNrB   )$rm   rn   ro   rp   r   re   rg   rq   r   r   r   r   r   r   r   rl   r   r   r   r   r   r   r   r?  rJ  r[  r\  r  r}  r  r  rZ  r  r  r   r  rr   rI   rG   r   r   t  s    @D L: H< LO;b}?~K;Z_?B H!!& DD>.@{,zJXN`"'HILV;6 n`7*!BF rI   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\   r8   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  r  rb   r   r   r   simprx   r   r   r  r   r   vals           rG   _find_conditionsz$from_hyper.<locals>._find_conditions6  s}    u 		 Aii2&,,.ii2&}}%C)=IIcN99Q<D		  	rI   r  )r  r  r  atomsr   poprY   r,   r   r5   r   r   r   rJ  rb   r&   r   )funcr   r  r   r   r  rx   r   r\   xDxr1aixDx_1r2birQ   r  r  r   s                      rG   
from_hyperr    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3rI   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\   r8   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           rG   r  z&from_meijerg.<locals>._find_conditions  s}    u 	 Aii2&,,.ii2&}}%C)=IIcN99Q<D	  	rI   r  )r  r  r~   anbmr  r  r   r  rY   r   r   rJ  r5   r   r   rb   r'   r   )r  r   r  r   rD   r   r   r   r  r   r  rx   r   r\   r  xDx1r  r  r  r  rQ   r  r  r   s                           rG   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3rI   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
    r8   z%Specify the variable for the functionr   )r   r   r  rD   r   )rD   Fr   rD   )rx   r   rD   )+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   rJ  r  r  r  r   r   r   r   r   r}  r   r   ry   r   )r  rx   r   r   r  rD   r   syms
extra_symssolpolyftr  rQ   r   r  r   rF   gsingular_icsr   s                        rG   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 ]B  \  }	}
|
|z  } ||j                         |j                         z  j                               ||	<   D t!        ||      S )z'
    Normalize a given annihilator
    r   )rV   r   r{   r   r   ry   rb   rz   r   r   r   r   numerdenomlcmgcdr^   )list_ofrv   r   numr  rV   r   	lcm_denomlist_of_coeffr   r   	gcd_numerfrac_anss                rG   r   r   V	  s%   
 CE;;DA&IM '" /1a$  qyy{!34Aqww'  gaj!9:  # 	

=#))+, 	]1%++-./  %EE)$	% J	i'')*I -( )1y=a) mB'--/-2C2I2I2KKTTVWI  %EE)$	% i'')*I -( Q1y=!1HNN4D!D M M OPaQ  v66rI   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.
    r8   r   N)r~   r   r#  ry   )r}   r   rQ   r   r   r   s         rG   r  r  	  s~     CJ!AJJwz!}a()*QR.) 21

71a=:a=012 JJz!}JrI   c                 n   | 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rQ  )r   r@  s     rG   r   z$_hyper_to_meijerg.<locals>.<genexpr>	  s$     
.a16!c!fk!
.s   !r   c              3   &   K   | ]	  }d |z
    ywru  rr   r@  s     rG   r   z$_hyper_to_meijerg.<locals>.<genexpr>	  s     	A!a%	rw  rr   c              3   &   K   | ]	  }d |z
    ywru  rr   r@  s     rG   r   z$_hyper_to_meijerg.<locals>.<genexpr>	  s     
Q1q5
rw  )
r  r  r  r5   r  r   r   r   r%   r'   )
r  r  r  r  r  anpr  bmqr3  r   s
             rG   r  r  	  s     
B	B

.2
..4  		!A 
	B
C
&&B
"
C	A aL  aL wr3C!,,,rI   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   rQ   s        rG   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                         }|j                  D ][  }t        ||j                  j                  j                        s.|j                  |j                  |j                                      ] t        |      |k  s|t        |      k  r|S t!        |      D 	cg c]  }	||	    ||   z   }
}	|dt#        t        |      |       }t!        ||z
        D ]  }d}t%        ||
      D ]\  \  }}t'        || j                        }t)        |dd      s|c c S t        |t*        t,        f      r|j/                         }|||z  z  }^ |j                  |       t1        |
|      }
 ||t        |      d z   S c c}	w )zy
    Tries to find more initial conditions by substituting the initial
    value point in the differential equation.
    TNr   r  )r   r   r   r   r   r   rv   rV   r   r}   rb   rz   r   r   r   r~   r   r%  r   r  getattrr*   r+   r  r  )	Holonomicr   r   r   r}   r   r   r   r   r   list_redr   r   rQ   r   rF   s                   rG   r   r   	  s    ((6):R:R:TX\:\||''KAJ	BA	A## 2a++00667aeeAIIK012 2w{a3r7l	q# AA. #H #	SR!_	B1q5\ 
2H% 	DAq9<<(A1k40	!k;78IIK1q5LC	 			#$Xq1
2 3r78#s   &Hc                 ,   t        | t              s| j                         S |j                  |       }|j	                  |       }| |j                         z  ||j                         z  z   }|dz  } ||j                         |j                         f      S rs  )rb   r.   r   r  r  r   )fracr   r   r  sol_num	sol_denoms         rG   r#  r#  	  s{    dC yy{	A	AcAFFHnq1668|+G1Igoo!2!2!4566rI   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)rb   r.   r  denr   r   mpmathr#  ry   reversedr   
_to_mpmathprecr*   r+   r  )
r  r   mpmr   r  sol_psol_qr#  r   r   s
             rG   r  r   
  s   dC AAFFEFFE
(1+& 1
%%bgg.ARU
 (1+& 1
%%bgg.ARU
 %+{34%+{345=rI   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}nz|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   r8   )$is_polynomialis_rational_functionas_base_expr  rb   r	   r6   r   r  r   rY   r!  r   r   r   r}   rv   r   r{   r   ry   r&  r   r*   r+   r  r   as_numer_denomrJ  r   r   r}  r~   r   r  r   )r  rx   r   r   r  rD   r   ispolyisratbasepolyratexpr  r   is_algr   r   r\   rQ   r   r"  r   r   r2  indicialr   r  rF   r  r  r  s                                 rG   r  r  
  s:   
 !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Xc]+AB/ "%( +1a+{!;< yy{E!H+ AeH%B	""$1!ebj1qvvay=(1qvvay=8eD	1q5kB"Q'33H=IIoob) :"'k^v{,,x(002C!(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   >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   r8   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   r8   c              3   (   K   | ]	  }|z     y wrB   rr   r  s     rG   r   z5_convert_meijerint.<locals>._shift.<locals>.<genexpr>
       -a!e-   c              3   (   K   | ]	  }|z     y wrB   rr   r  s     rG   r   z5_convert_meijerint.<locals>._shift.<locals>.<genexpr>
  r:  r;  c              3   (   K   | ]	  }|z     y wrB   rr   r  s     rG   r   z5_convert_meijerint.<locals>._shift.<locals>.<genexpr>
  r:  r;  c              3   (   K   | ]	  }|z     y wrB   rr   r  s     rG   r   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  rF   rx   s              @rG   _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4rI   r  )r(   	_rewrite1r/  r   r   r  r   r~   )r  rx   r   rD   r  facpor  r   r   fac_listr  r  po_listG_listrA  r2  r  rQ   s    `                 rG   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        rG   addz_create_table.<locals>.add
  s<     	#.3::Gk3B7<9 	:rI   r\   r   r8   r   N)r   rr   )r   r  rY   r   r   r   r   r"   r   r   r#   r$   r   r   r   r!   r   r    )rL  rD   rM  r   r   r\   s   `     rG   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/5rI   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  rb   r   r3   r  r   r   )r  rx   r   r   r   r   r  s          rG   r  r  
  sy    	B5\ ii2c3a$C==E!ZS%9
		#yy| IrI   )r   F)Nr   NNNTr  r  )trp   
sympy.corer   r   r   sympy.core.numbersr   r   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.polytoolsr0   sympy.polys.matricesr1   sympy.printingr2   sympy.series.limitsr3   sympy.series.orderr4   sympy.simplify.hyperexpandr5   sympy.simplify.simplifyr6   sympy.solvers.solversr7   r  r9   r:   r;   holonomicerrorsr<   r=   r>   r?   rR   rY   rT   r^   r   r  r  r  r  r  sympy.integrals.meijerintr  r  r   r  r  r   r   r#  r  r  r  r  r  rr   rI   rG   <module>ro     sR  
 % $" " " " & + & L L F F > 9 E E R R 9 8 % ! ) * & ' ' & -  % $ 2 - ' R R) )#,LA3 A3HSG SGlX Xv4J4Z 54 M4` El  -_:H67r(-:&R	7> '(DbSW g-T *.b +\ !# "6J
rI   