o
    8Va%                     @   s   d Z ddl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mZmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZ dd Zdd Z dd Z!dd Z"dd Z#dS )aO  
This module contains the implementation of the 2nd_hypergeometric hint for
dsolve. This is an incomplete implementation of the algorithm described in [1].
The algorithm solves 2nd order linear ODEs of the form

.. math:: y'' + A(x) y' + B(x) y = 0\text{,}

where `A` and `B` are rational functions. The algorithm should find any
solution of the form

.. math:: y = P(x) _pF_q(..; ..;\frac{\alpha x^k + \beta}{\gamma x^k + \delta})\text{,}

where pFq is any of 2F1, 1F1 or 0F1 and `P` is an "arbitrary function".
Currently only the 2F1 case is implemented in SymPy but the other cases are
described in the paper and could be implemented in future (contributions
welcome!).

References
==========

.. [1] L. Chan, E.S. Cheb-Terrab, Non-Liouvillian solutions for second order
       linear ODEs, (2004).
       https://arxiv.org/abs/math-ph/0402063
    )SPow)expand)Eq)SymbolWild)expsqrthyper)Integral)rootsgcd)cancelfactor)collectsimplify
logcombine)	powdenest)get_numbered_constantsc                    sN  |j d }||}td|||||dgd}td|||||dgd}td|||||dgd}|||d ||  ||  }t| ||d|||g|  rt fdd D s|  \}}	t|} t| ||d|||g|  r | dkrt |  |  }
t |  |  }|
|gS g S )	Nr   a3   )Zexcludeb3c3c                    s   g | ]} |   qS  )Zis_polynomial).0keyrr   B/usr/lib/python3/dist-packages/sympy/solvers/ode/hypergeometric.py
<listcomp>1   s    z,match_2nd_hypergeometric.<locals>.<listcomp>)	argsdiffr   r   matchallas_numer_denomr   r   )eqfuncxZdfr   r   r   ZdeqndABr   r   r   match_2nd_hypergeometric'   s*   

    $r,   c              	      s  |j d tt| d | d d  | }ttd | tdd  }| \}}tt|}tt|} fdd  |f} |f}|| |}	t	|	}
tt
t||
d  tdd  |
 d  dd}ttt|td|
  dd}| \}}t |f}|j }g }g }|D ]?}|rt|tr|| d  |tt| d  d  q|| d  |tt| d  q|  t||d	kr||
|d	d
S d S )Nr   r         c                    sx   dh}| D ]4}| r9t|tr#| d kr#|| d  q|kr1|| d  q| |j q|S )Nr   r.   )has
isinstancer   as_base_expaddupdater    )num_powval_power_countingr'   r   r   r8   N   s   
z3equivalence_hypergeometric.<locals>._power_countingTZforce2F1)I0k
sing_pointtype)r    r   r   r!   r   r$   r   r   r3   r   r   subsmaxr/   r0   r   appendr1   listr   keyssortequivalence)r*   r+   r&   ZI1ZJ1r4   ZdemZpow_numZpow_demr5   r<   r;   max_num_powZdem_argsr=   dem_powargr   r7   r   equivalence_hypergeometric>   s>   
& 


4(

&rI   c           "      C   s  |j d }td}td}td}td}td}	td}
td}td	}td
}td}|| d || d  |d  dd| | | d| |   |  ||d   d|d  |d d   }|ddgkrg }| | | | || ||  g}tdD ]!}|t|k r|t|| ||  q|td||  d q| |d  }| |d  }|}t|dkr||d |  |d |d   }|| | || |  }|||}|||}|||}t|}|||  || |  }t|||||||}n|}|}| ||} | ||d  } t	| } |d d|dddi}|
 \}}|d |	d d |dd|
  | |
|	 |
|	   d||d  i}|ttt| | |d |gdd g }|d |dfD ]}|t|| ||  qjdt	td|d j  }|tsttt|d |}t	t|d jd }|t	t|d |d  |d j d|   }|| d }|| d } t|t| t|||dd}!|!S )Nr   abctsr   alphabetagammadeltar.   r   r-      F)Zevaluater:   )rJ   rK   rL   r<   mobiusr>   )r    r   rangelenrA   r   r?   r   r!   r   r$   r3   r   r   r	   Zlhsr/   r   minrB   r   r   )"Ir<   r=   r&   r'   rJ   rK   rL   rM   rN   r   rO   rP   rQ   rR   r;   ZeqsZsing_eqsiZ_betaZ_deltaZ_gammaZmobZdict_IZI0_numZI0_demZdict_I0r   Z_cZ_sZ_rZ_aZ_bZrnr   r   r   match_2nd_2F1_hypergeometric   sh   
h" "@(.rZ   c              	   C   s   | dkr|ddgg dfv rdS d S | dkr*|g dg dddgddgfv r(dS d S | dkrH|g dddgg dddgdgddgddgfv rHdS d S )Nr   )r   r   r   r:   r.   )r.   r   r   r   )r.   r.   r   r   )rF   rG   r   r   r   rE      s    0rE   c                 C   s  |j d }ddlm} ddlm} t| dd\}}|d }|d }	|d }
|d	 }d }|
jd
krW|t||	g|
g| |t||
 d |	|
 d gd|
 g| |d|
    }n|
dkrtt	t||	 d  | |
 |d |  ||t||	g|
g|d  |t||	g|
g| }|t||	g|
g| ||  }n=|
| |	 jd
kr|t||	gd| |	 |
 gd|  |t|
| |
|	 gd|
 | |	 gd|  d| |
| |	    }|r|d }t
d|| }||	 d | |
 ||| }|||d | |||||   }||d | |||d  }t	ttt|d|  |dd}|||d }||||d  }||||d  }|jsvt|d |}t	t|dd}t|| ||d  d d   | }t||}|S t|||d  d d   | }t||}|S )Nr   )hyperexpand)r   r   )r4   rJ   rK   rL   r*   Fr.   rT   Tr9   r<   )r    Zsympy.simplify.hyperexpandr[   Zsympyr   r   
is_integerr
   r   r   r   r!   r?   r   r   Zis_zeror   )r%   r&   Zmatch_objectr'   r[   r   ZC0ZC1rJ   rK   rL   r*   ZsolZy2r?   ZdtdxZ_BZ_AeZe1r   r   r   get_sol_2F1_hypergeometric   sF   

N^ h * "&
"
r^   N)$__doc__Z
sympy.corer   r   Zsympy.core.functionr   Zsympy.core.relationalr   Zsympy.core.symbolr   r   Zsympy.functionsr   r	   r
   Zsympy.integralsr   Zsympy.polysr   r   Zsympy.polys.polytoolsr   r   Zsympy.simplifyr   r   r   Zsympy.simplify.powsimpr   Zsympy.solvers.ode.oder   r,   rI   rZ   rE   r^   r   r   r   r   <module>   s"    AK