
    wg                     j   d Z ddlmZ ddlmZmZ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 ddlmZmZmZm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#m$Z$ ddl%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Z0d Z1d Z2d"dZ3d#dZ4d#dZ5d#dZ6d#dZ7d#dZ8d#dZ9d#dZ:d#dZ;d#d Z<d! Z=y)$a  
This module contains the implementation of the internal helper functions for the lie_group hint for
dsolve. These helper functions apply different heuristics on the given equation
and return the solution. These functions are used by :py:meth:`sympy.solvers.ode.single.LieGroup`

References
=========

- `abaco1_simple`, `function_sum` and `chi`  are referenced from E.S Cheb-Terrab, L.G.S Duarte
and L.A,C.P da Mota, Computer Algebra Solving of First Order ODEs Using
Symmetry Methods, pp. 7 - pp. 8

- `abaco1_product`, `abaco2_similar`, `abaco2_unique_unknown`, `linear`  and `abaco2_unique_general`
are referenced from E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order
ODE Patterns, pp. 7 - pp. 12

- `bivariate` from Lie Groups and Differential Equations pp. 327 - pp. 329

    )islice)AddSMulPow)factor_terms)FunctionAppliedUndefexpand)EqualityEq)SymbolWildDummysymbols)explog)	integrate)Poly)canceldiv)collectpowsimpseparatevarssimplify)solve)pdsolve)numbered_symbols)_preprocess	ode_order   )checkinfsol)	abaco1_simpleabaco1_productabaco2_similarabaco2_unique_unknownabaco2_unique_generallinearfunction_sum	bivariatechic                    t        d      }t        d      }|j                  }|j                  d   }|d   }	|d   }
g }|s	 t        | ||d|      }|D ]  }| |||         j                  ||	      }| |||         j                  ||	      }t        ||z        |
k(  rN |||	      j                  |      |z   |||	      j                  |	      |z  z   }t        | |||	            j                  }t        |dz
   |||	            j                  }||fD cg c]  }t        |       }}t        d	      }t        d
      }t        d      }|d   }|d   }	 t        ||z
  ||z
  g||	d      }|g k(  r(	 |d   }||   }||	   }t        |j                  |      |j                  |	      |
z  z         }t        |j                  |      |j                  |	      |
z  z         }|r|rt        ||z  j                  ||f|	|fg            }t        |||gd      }|st        d||   z  |      |z   t        |d   ||   z  |      z
  }|j                  ||f||fg      }	 t        ||	      }|D cg c]  }t!         ||      |       c}c S |r%t!         ||      t        ||z
  |	      d         gc S |smt!         ||      t        ||z
  |	      d         gc S  |r0|D cg c]$  }t!        |j                  |	 ||            d      & c}S y # t        $ r Y y w xY wc c}w c c}w # t"        $ r |j%                  |       Y w xY w# t"        $ r Y w xY wc c}w )Nxietar   yhr!   )hintfuncordermatch)r2   rsC1T)dict)r   r9   coeff)r	   r2   argsinfinitesimals
ValueErrorsubsr   diffr   rhs_lie_group_remover   r   r   r   r   r   NotImplementedErrorappend) eq	heuristicr2   r4   infr-   r.   fxr/   r0   tempsolinfsimxiinfetainfrpder5   r6   coordnewcoordr7   rcoordscoordsolxsubysubnumdenomdiffeqsepdeqsdeqs                                    `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/solvers/ode/lie_group.py_ode_lie_group_try_heuristicr\   6   s   	$B
5/C		A		!Ac
Ac
AG	 )$auUC  2<1d$**43Q&,,T15
 F5L!Q&Aw||Au$qAw||Av'==DqAw'++D1H1Q7+//;<a&A%e,AA#J#JD\!" 	<VQZ0!QTBCby 
 a&Cq6Dq6D6;;q>FKKN1,<<=CV[[^fkk!nQ.>>?Eu!3u9"2"2QI4y3I"JK"6Aq6E#Qs1vX2R7)CLQTUVQWDWYZ:[[C((QK!V#=>C?$S!} :>>#1Q4>> 1Q4v{A!6q!9:;;1Q4v{A!6q!9:;;e2<j 4;<S388Aqt$a(<<s  		 B>  ? / ,s+,% # 		@ =sH   L L(M'L27L-,)M#	L%$L%2MM	M M c                 |   t         }i }|j                  }|j                  d   }|j                  |      }t	        d      }	t	        d      }
|d   }|d   }|j                  dd       }|rt        ||d      ||d      z         }|}nt        d      }| j                  ||      }|i|g |	| ||            t        |       |
| ||            t        |      ig}t        t        ||       | ||      d      d   d   rd	gt        |      z   }||d
}d }|D ]!  }t        t        ||       ||||      }|s|c S  |S )Nr   r-   r.   r/   der!   )r2   r3   user_defined)r0   r/   )lie_heuristicsr2   r;   r?   r	   popr   r   r>   r   r"   r   listr\   )r6   r2   r3   r4   
heuristicsrF   rG   rH   dfr-   r.   xisetasr/   r0   rR   rE   s                    r[   _ode_lie_grouprh   ~   sY   J
C		A		!A	1B	$B
5/C
+C<D		#tAeE#J'eCj(99::#JFF4O
4+1adQsVSAaD\1T7;<r"ay#AaD:1=a@()D,<<J!E C 	*2b!9iucRJ J    Nc                    t        | t              r| j                  | j                  z
  } |st	        |       \  } }|j
                  }t        |      dk7  rt        d      |d   }|st        | |      }|dk7  rt        d      |j                  |      }t        d|g      }t        d|g      }	|r|d   }
|d	   }nqt        t        |       |      j                  ||z  |	z         }|rt        ||	   ||   z         }
n	 t!        | |      }|d   }
t#        d	      }|
j%                  ||      }
t#        d      }|
j                  |      }|
j                  |      }d|
z  j%                  ||f||fg      j%                  ||      }|
|||||d}|dk(  r]g }t&        D ]C  }t)               d|z      } ||d      }|s |j+                  |D cg c]	  }||vs| c}       E |r|S t        d      |dk(  r6t&        D ]"  }t)               d|z      } ||d      }|s |c S  t        d      |t&        vrt        d|z         t)               d|z      } ||d      }|r|S t        d      # t        $ r t        d
      w xY wc c}w )a}  
    The infinitesimal functions of an ordinary differential equation, `\xi(x,y)`
    and `\eta(x,y)`, are the infinitesimals of the Lie group of point transformations
    for which the differential equation is invariant. So, the ODE `y'=f(x,y)`
    would admit a Lie group `x^*=X(x,y;\varepsilon)=x+\varepsilon\xi(x,y)`,
    `y^*=Y(x,y;\varepsilon)=y+\varepsilon\eta(x,y)` such that `(y^*)'=f(x^*, y^*)`.
    A change of coordinates, to `r(x,y)` and `s(x,y)`, can be performed so this Lie group
    becomes the translation group, `r^*=r` and `s^*=s+\varepsilon`.
    They are tangents to the coordinate curves of the new system.

    Consider the transformation `(x, y) \to (X, Y)` such that the
    differential equation remains invariant. `\xi` and `\eta` are the tangents to
    the transformed coordinates `X` and `Y`, at `\varepsilon=0`.

    .. math:: \left(\frac{\partial X(x,y;\varepsilon)}{\partial\varepsilon
                }\right)|_{\varepsilon=0} = \xi,
              \left(\frac{\partial Y(x,y;\varepsilon)}{\partial\varepsilon
                }\right)|_{\varepsilon=0} = \eta,

    The infinitesimals can be found by solving the following PDE:

        >>> from sympy import Function, Eq, pprint
        >>> from sympy.abc import x, y
        >>> xi, eta, h = map(Function, ['xi', 'eta', 'h'])
        >>> h = h(x, y)  # dy/dx = h
        >>> eta = eta(x, y)
        >>> xi = xi(x, y)
        >>> genform = Eq(eta.diff(x) + (eta.diff(y) - xi.diff(x))*h
        ... - (xi.diff(y))*h**2 - xi*(h.diff(x)) - eta*(h.diff(y)), 0)
        >>> pprint(genform)
        /d               d           \                     d              2       d                       d             d
        |--(eta(x, y)) - --(xi(x, y))|*h(x, y) - eta(x, y)*--(h(x, y)) - h (x, y)*--(xi(x, y)) - xi(x, y)*--(h(x, y)) + --(eta(x, y)) = 0
        \dy              dx          /                     dy                     dy                      dx            dx

    Solving the above mentioned PDE is not trivial, and can be solved only by
    making intelligent assumptions for `\xi` and `\eta` (heuristics). Once an
    infinitesimal is found, the attempt to find more heuristics stops. This is done to
    optimise the speed of solving the differential equation. If a list of all the
    infinitesimals is needed, ``hint`` should be flagged as ``all``, which gives
    the complete list of infinitesimals. If the infinitesimals for a particular
    heuristic needs to be found, it can be passed as a flag to ``hint``.

    Examples
    ========

    >>> from sympy import Function
    >>> from sympy.solvers.ode.lie_group import infinitesimals
    >>> from sympy.abc import x
    >>> f = Function('f')
    >>> eq = f(x).diff(x) - x**2*f(x)
    >>> infinitesimals(eq)
    [{eta(x, f(x)): exp(x**3/3), xi(x, f(x)): 0}]

    References
    ==========

    - Solving differential equations by Symmetry Groups,
      John Starrett, pp. 1 - pp. 14

    r!   z(ODE's have only one independent variabler   z?Infinitesimals for only first order ODE's have been implementedaexcludebr0   r/   z9Infinitesimals for the first order ODE could not be foundu)r0   r2   hxhyr/   hinvalllie_heuristic_T)compz3Infinitesimals could not be found for the given ODEdefaultFzHeuristic not recognized: z;Infinitesimals could not be found using the given heuristic)
isinstancer   lhsr@   r   r;   lenr=   r    rB   r?   r   r   r   r4   r   r   r   r>   ra   globalsextend)rD   r2   r3   r1   r4   	variablesrH   re   rk   rn   r0   r/   rR   ro   rp   rq   rr   xietarE   functioninflistrF   s                         r[   r<   r<      s   | "hVVbff_r?D		I
9~CDDaLb$'EA:% ': ; ; 1BSRD)ASRD)A#J#Jr
B/55adQh?!%(58"344A##Bm
  F#JFF4Oc
ABBqSJJAA/066q!<DT2AtTEu}!/ SI&y)9I)EFH&u48GW%Qc5@Pc%QR	S
  L- /( ) ) "!/ %I&y)9I)EFH$U7E$	% * +% & & ^+!">"EFF $I&6&=>!%d3!\% ', - -[ / B1 3A B BB& &Rs   ?I) 		J
J
)I>c                 B   g }| d   }| d   }| d   }|j                   d   }| d   }| d   } t        d      ||      }	 t        d      ||      }
|j                  }||vrG	 t        t	        ||            }|	t
        j                  |
|i}|s|gS |r||vr|j                  |       	 ||z  }|j                  }||vrW	 t        t	        ||            }|	t
        j                  |
|j                  ||      i}|s|gS |r||vr|j                  |       	 | |z  }|j                  }||vrG	 t        t	        ||            }|	||
t
        j                  i}|s|gS |r||vr|j                  |       	 | |d	z  z  }|j                  }||vrW	 t        t	        ||            }|	|j                  ||      |
t
        j                  i}|s|gS |r||vr|j                  |       	 |r|S y
# t        $ r Y Jw xY w# t        $ r Y w xY w# t        $ r Y w xY w# t        $ r Y >w xY w)aD  
    The first heuristic uses the following four sets of
    assumptions on `\xi` and `\eta`

    .. math:: \xi = 0, \eta = f(x)

    .. math:: \xi = 0, \eta = f(y)

    .. math:: \xi = f(x), \eta = 0

    .. math:: \xi = f(y), \eta = 0

    The success of this heuristic is determined by algebraic factorisation.
    For the first assumption `\xi = 0` and `\eta` to be a function of `x`, the PDE

    .. math:: \frac{\partial \eta}{\partial x} + (\frac{\partial \eta}{\partial y}
                - \frac{\partial \xi}{\partial x})*h
                - \frac{\partial \xi}{\partial y}*h^{2}
                - \xi*\frac{\partial h}{\partial x} - \eta*\frac{\partial h}{\partial y} = 0

    reduces to `f'(x) - f\frac{\partial h}{\partial y} = 0`
    If `\frac{\partial h}{\partial y}` is a function of `x`, then this can usually
    be integrated easily. A similar idea is applied to the other 3 assumptions as well.


    References
    ==========

    - E.S Cheb-Terrab, L.G.S Duarte and L.A,C.P da Mota, Computer Algebra
      Solving of First Order ODEs Using Symmetry Methods, pp. 8


    r/   r0   r2   r   rp   rq   r-   r.      N)
r;   r	   free_symbolsr   r   r   ZerorC   rB   r>   )r4   ru   r}   r/   r0   r2   rH   rp   rq   r-   r.   hysymfxrF   factorfacsymfys                    r[   lie_heuristic_abaco1_simpler   /  sj   F Ec
Ac
A=D		!A	tB	tB	$4	 B
(5/!T
"COOE~		"Yr1%&B qvvsB'Cu5(S!TF  F		"Yvq)*B qvvsBGGAt$45Cu5(S!SUF  F		"Yvq)*B r3'Cu5(S!S!Q$ZF  F		"Yvq)*B rwwq$'aff5Cu5(S! g # 		 # 		 # 		 # 		sH    G$ <G4 )H 	H $	G10G14	H ?H 	HH	HHc                 V   g }| d   }| d   }| d   }| d   }|j                   d   } t        d      ||      } t        d      ||      }	t        t        |      j	                  |      j	                  |      |dz  z  d	||g
      }
|
r|
d   r|
|   }t        |d||z  z  j	                  |      z        }|j                  }||vrXt        t        ||            }|	t        j                  |||z  j                  ||      i}
|s|
gS |r|
|vr|j                  |
       t        d      }t        t        |      j	                  |      j	                  |      |dz  z  d	||g
      }
|
r|
d   r|
|   }t        |d||z  z  j	                  |      z        }|j                  }||vrt        t        ||            }||z  }|j                  ||f||fg      j                  ||      }|	|j                  ||      |t        j                  i}
|s|
gS |r|
|vr|j                  |
       |r|S y)a  
    The second heuristic uses the following two assumptions on `\xi` and `\eta`

    .. math:: \eta = 0, \xi = f(x)*g(y)

    .. math:: \eta = f(x)*g(y), \xi = 0

    The first assumption of this heuristic holds good if
    `\frac{1}{h^{2}}\frac{\partial^2}{\partial x \partial y}\log(h)` is
    separable in `x` and `y`, then the separated factors containing `x`
    is `f(x)`, and `g(y)` is obtained by

    .. math:: e^{\int f\frac{\partial}{\partial x}\left(\frac{1}{f*h}\right)\,dy}

    provided `f\frac{\partial}{\partial x}\left(\frac{1}{f*h}\right)` is a function
    of `y` only.

    The second assumption holds good if `\frac{dy}{dx} = h(x, y)` is rewritten as
    `\frac{dy}{dx} = \frac{1}{h(y, x)}` and the same properties of the first assumption
    satisfies. After obtaining `f(x)` and `g(y)`, the coordinates are again
    interchanged, to get `\eta` as `f(x)*g(y)`


    References
    ==========
    - E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order
      ODE Patterns, pp. 7 - pp. 8

    r/   r0   rr   r2   r   r-   r.   r   Tr9   r   r:   r!   u1N)r;   r	   r   r   r?   r   r   r   r   r   r   r>   rC   r   )r4   ru   r}   r/   r0   rr   r2   rH   r-   r.   rF   r   gygysymsr   etavals                   r[   lie_heuristic_abaco1_productr     s5   > Ec
Ac
A=D=D		!A	$4	 B
(5/!T
"C QQ--a0!Q$6TAq6
RC
s7|Vb1bd8//!,-.F?Yr1%&BRULLD$9:Cu5(S!	tB
T*003T1W<4RSUVQW
XC
s7|Vb1bg;,,Q/01F?Yr1%&BUFkkAr7QF"34::2qAFAt,b!&&9Cu5(S! ri   c                 J   | d   }| d   }| d   }| d   }|j                   d   }| d   } t        d      ||      } t        d      ||      }	|j                         r<t        d	      \  }
}}}}}|
||z
  |z  z   ||d
z  z  z
  ||z  z
  ||z  z
  }t	        |      j                         \  }}t        |||      j                         } t        d      ||      } t        d      ||      }|j                  |      |j                  |      |j                  |      z
  |z  z   |j                  |      |d
z  z  z
  ||z  z
  ||z  z
  }t        d      }t        d      }t        |dz         D ]*  }|r|t        t        |dz         D cg c]:  }t        dt        |      z   dz   t        ||z
        z         ||z  z  |||z
  z  z  < c} z  }|t        t        |dz         D cg c]:  }t        dt        |      z   dz   t        ||z
        z         ||z  z  |||z
  z  z  < c} z  }|j                  ||||i      j                         j                         \  }}t        |      }|j!                  ||      r'|j"                  rt        |||      j%                         }s8|j&                  j)                  |j&                        ||hz
  }t+        |j-                         g| }t/        |t0              r|d   }t3        |j-                               s|j                  |      }|j                  |      }t4        j7                  |d      } |	|j                  |       j                  ||      ||j                  |       j                  ||      i}!|!gc S  yyc c}w c c}w )a  
    The third heuristic assumes the infinitesimals `\xi` and `\eta`
    to be bi-variate polynomials in `x` and `y`. The assumption made here
    for the logic below is that `h` is a rational function in `x` and `y`
    though that may not be necessary for the infinitesimals to be
    bivariate polynomials. The coefficients of the infinitesimals
    are found out by substituting them in the PDE and grouping similar terms
    that are polynomials and since they form a linear system, solve and check
    for non trivial solutions. The degree of the assumed bivariates
    are increased till a certain maximum value.

    References
    ==========
    - Lie Groups and Differential Equations
      pp. 327 - pp. 329

    r0   rp   rq   r2   r   r/   r-   r.   zetax etay etad xix xiy xidr   detadxixi0eta0r!   xi__eta_N)r;   r	   is_rational_functionr   r   as_numer_denomr   total_degreer?   r   ranger   strr>   doitr   is_polynomialis_Addas_dictr   unionr   valuesrw   rc   anyr9   fromkeys)"r4   ru   r0   rp   rq   r2   rH   r/   r-   r.   etaxetayetadxixxiyxidipderU   rV   degr   r   xieqetaeqipowerpdenpolyysymsetsoldictxiredetareddict_rF   s"                                     r[   lie_heuristic_bivariater     s   & 	c
A	tB	tB=D		!Ac
A	$4	 B
(5/!T
"C +22N*O'dD#sCtcz1n$s1a4x/#b&847BD\002
U31o**,x1%huoa#		!		!sxx{ :A==!aQRd@RR"fBwe}vsQw 	!A!&q1u/ 53u:-3c!e)nDEahNqSTW\S\~]/ 0 0 !&q1u/ 6CJ.4s1u9~EFq%xOPQTUX]T]P^^/ 0 0  99c4u%=>CCEUUWKD%$<D !!!Q'DKKT1a(002**001C1CD1vM88gt,%ajGw~~'( IIg.E"ZZ0F !MM&!4EE 2 7 74 @EJJu-221d;=C5L9	!   //s   ?N"?N c                    | d   }| d   }| d   }|j                   d   }| d   } t        d      ||      } t        d      ||      }|j                         rct        d      \  }	}
}|
||z  z   ||	z  z
  }t	        |      j                         \  }}t        |||      j                         } t        d	      ||      }|j                  |      }|j                  |      }|||z  z   ||z  z
  }t        d	      }t        d
|d
z         D ]  }|t        t        |d
z         D cg c]:  }t        dt        |      z   dz   t        ||z
        z         ||z  z  |||z
  z  z  < c} z  }t	        |j                  ||i      j                               j                         \  }}t        |      }|j!                  ||      s|j"                  st        |||      j%                         }|s|j&                  ||hz
  }t)        |j+                         g| }t-        |t.              r|d   }t1        |j+                               s>|j                  |      }t2        j5                  |d
      }|j                  |      }t7        ||      \  }}||j                  ||      ||j                  ||       i}|gc S  yyc c}w )a  
    The aim of the fourth heuristic is to find the function `\chi(x, y)`
    that satisfies the PDE `\frac{d\chi}{dx} + h\frac{d\chi}{dx}
    - \frac{\partial h}{\partial y}\chi = 0`.

    This assumes `\chi` to be a bivariate polynomial in `x` and `y`. By intuition,
    `h` should be a rational function in `x` and `y`. The method used here is
    to substitute a general binomial for `\chi` up to a certain maximum degree
    is reached. The coefficients of the polynomials, are calculated by by collecting
    terms of the same order in `x` and `y`.

    After finding `\chi`, the next step is to use `\eta = \xi*h + \chi`, to
    determine `\xi` and `\eta`. This can be done by dividing `\chi` by `h`
    which would give `-\xi` as the quotient and `\eta` as the remainder.


    References
    ==========
    - E.S Cheb-Terrab, L.G.S Duarte and L.A,C.P da Mota, Computer Algebra
      Solving of First Order ODEs Using Symmetry Methods, pp. 8

    r0   rq   r2   r   r/   r-   r.   zschi, schix, schiyr+   r!   chi_r   N)r;   r	   r   r   r   r   r   r   r?   r   r   r   r   r>   r   r   r   r   r   r   r   r   rw   rc   r   r9   r   r   )r4   ru   r0   rq   r2   rH   r/   r-   r.   schischixschiycpderU   rV   r   r+   chixchiychieqr   r   cnumcdencpolysolsymsr   r   xicetacrF   s                                  r[   lie_heuristic_chir   '  s   0 	c
A	tB=D		!Ac
A	$4	 B
(5/!T
"C$%9:eUqwD(D\002
U31o**,huoa#xx{xx{af}r#v%uq#'" 	%AS"1q5\+ vE
*S03q5y>AB1e8KAPQTYPYNZ+ , ,E  		3- 8 = = ?@OOQJD$$<D!!!Q'DKKT1a(002#00Aq69G#ELLN=W=G!'40")!*7>>+, %

7 3 $gq 9 %

5 1 %(qM	T"DIIa$6SXXa=N<NO #u5	%  +s   #?J=c           
         g }| d   }| d   }| d   }|j                   d   }| d   } t        d      ||      } t        d      ||      }	||fD ]  }
|
d|
z  j                  |d	      z  }t        d|z  j                  |      d
||g      }|r|d   r||   j	                  |      r||   j	                  |      rt        d      }	 |t        ||   |      z  }d|||   z  |d   z  z  }t        ||z  |z
        }t        |j                  |d	      |z
        }|r'|s|j                  |d      }||z  }n0t        ||      }|r |d   }|j                  ||      }||z  |z  }n|
|k(  r$|j                  ||      }|j                  ||      }t        ||z         }|j                  r3t        |j                   D cg c]  }|j	                  ||      s| c} }|
|k(  r%|	|j                  ||      |t        j                  i}n$||j                  ||      |	t        j                  i}|s|gc S |j                  |       	 |s|c S  yc c}w # t         $ r Y w xY w)a/  
    This heuristic uses the following two assumptions on `\xi` and `\eta`

    .. math:: \eta = 0, \xi = f(x) + g(y)

    .. math:: \eta = f(x) + g(y), \xi = 0

    The first assumption of this heuristic holds good if

    .. math:: \frac{\partial}{\partial y}[(h\frac{\partial^{2}}{
                \partial x^{2}}(h^{-1}))^{-1}]

    is separable in `x` and `y`,

    1. The separated factors containing `y` is `\frac{\partial g}{\partial y}`.
       From this `g(y)` can be determined.
    2. The separated factors containing `x` is `f''(x)`.
    3. `h\frac{\partial^{2}}{\partial x^{2}}(h^{-1})` equals
       `\frac{f''(x)}{f(x) + g(y)}`. From this `f(x)` can be determined.

    The second assumption holds good if `\frac{dy}{dx} = h(x, y)` is rewritten as
    `\frac{dy}{dx} = \frac{1}{h(y, x)}` and the same properties of the first
    assumption satisfies. After obtaining `f(x)` and `g(y)`, the coordinates
    are again interchanged, to get `\eta` as `f(x) + g(y)`.

    For both assumptions, the constant factors are separated among `g(y)`
    and `f''(x)`, such that `f''(x)` obtained from 3] is the same as that
    obtained from 2]. If not possible, then this heuristic fails.


    References
    ==========
    - E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order
      ODE Patterns, pp. 7 - pp. 8

    r0   r2   rr   r   r/   r-   r.   r!   r   Tr   r:   kN)r;   r	   r?   r   hasr   r   r   r>   r   r   is_Mulr   r   r   rC   rB   )r4   ru   r}   r0   r2   rr   rH   r/   r-   r.   odefacr   rX   r   r   fddr   checkrR   r   argrF   s                         r[   lie_heuristic_function_sumr   n  sh   L Ec
A=D=D		!Ac
A	$4	 B
(5/!T
"Cd) )!F(A./AfH??1-D1a&I3w<CFJJqMc!fjjmc
A!*yQ++ 3q6#g,./c&j2o. A!45 WWQ] d#E1o"%a&C!#CB"$Q$B$~WWQ]WWQ])"r'2F}}!$fkk&SsSWWQPQ]s&S!T~"FKK4$8"qvvF!6;;q$#7qvvF #uS)LS)< 'T/ ' s   I I
I
	I"!I"c                    | d   }| d   }| d   }| d   }| d   }|j                   d   }| d   } t        d      ||      }	 t        d	      ||      }
t        |j                  |      |j                  |d
      z        }|j                  |      }|j                  |      }|j	                  |      s|j	                  |      st        d|g      }t        d|g      }t        d||g      }|j                  ||t        ||z        z  z         } 	 t        t        | |   | |   z         |      | |   z  }| |   |z  }|	||
|igS t        ||z        }|j	                  |      s]t        ||z  |j                  |      z
  |z
  ||z   z        }|j	                  |      s#	 t        t        ||            }| |z  }|	||
|igS t        |j                  |      |j                  |d
      z        }|j                  |      }|j                  |      }|j	                  |      s|j	                  |      st        d|g      }t        d|g      }t        d||g      }|j                  ||t        ||z        z  z         } 	 t        t        | |   | |   z         |      | |   z  }| |   |z  }|
|j                  ||      |	|j                  ||      igS t        ||z        }|j	                  |      st        ||j                  |      z  |j                  |      z
  |j                  |      z
  ||z   z        }|j	                  |      sC	 t        t        ||            }| |z  }|
|j                  ||      |	|j                  ||      igS yy# t        $ r Y w xY w# t        $ r Y w xY w# t        $ r Y yw xY w# t        $ r Y yw xY w)aW  
    This heuristic uses the following two assumptions on `\xi` and `\eta`

    .. math:: \eta = g(x), \xi = f(x)

    .. math:: \eta = f(y), \xi = g(y)

    For the first assumption,

    1. First `\frac{\frac{\partial h}{\partial y}}{\frac{\partial^{2} h}{
       \partial yy}}` is calculated. Let us say this value is A

    2. If this is constant, then `h` is matched to the form `A(x) + B(x)e^{
       \frac{y}{C}}` then, `\frac{e^{\int \frac{A(x)}{C} \,dx}}{B(x)}` gives `f(x)`
       and `A(x)*f(x)` gives `g(x)`

    3. Otherwise `\frac{\frac{\partial A}{\partial X}}{\frac{\partial A}{
       \partial Y}} = \gamma` is calculated. If

       a] `\gamma` is a function of `x` alone

       b] `\frac{\gamma\frac{\partial h}{\partial y} - \gamma'(x) - \frac{
       \partial h}{\partial x}}{h + \gamma} = G` is a function of `x` alone.
       then, `e^{\int G \,dx}` gives `f(x)` and `-\gamma*f(x)` gives `g(x)`

    The second assumption holds good if `\frac{dy}{dx} = h(x, y)` is rewritten as
    `\frac{dy}{dx} = \frac{1}{h(y, x)}` and the same properties of the first assumption
    satisfies. After obtaining `f(x)` and `g(x)`, the coordinates are again
    interchanged, to get `\xi` as `f(x^*)` and `\eta` as `g(y^*)`

    References
    ==========
    - E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order
      ODE Patterns, pp. 10 - pp. 12

    r0   rp   rq   r2   rr   r   r/   r-   r.   r   Arl   BCN)r;   r	   r   r?   r   r   r4   r   r   rB   r>   )r4   ru   r0   rp   rq   r2   rr   rH   r/   r-   r.   r   factorxfactoryr   r   r   taugxgammatauints                        r[   lie_heuristic_abaco2_similarr     s   L 	c
A	tB	tB=D=D		!Ac
A	$4	 B
(5/!T
"CAFF1IaffQl*+Fkk!nGkk!nG::a=Aqc"qc"q!f%Ac!A#hJ'	(yq%(!233Q7a@C q#Bc2&'' ww'yy|U2X

15:QYGHF::a=0i23C eBc2.//DIIaL1a01Fkk!nGkk!nG::a=Aqc"qc"q!f%Ac!A#hJ'	Dyq%(!233Q7a@C q#B#((1d+RD1ABCC ww'yy|U499Q</%**Q-?$))A,Nu F::a=Li23C eB #((1d"3RD9IJKK ! K # 		 +   # 		 + sH   %N  N" %N2 O 	NN"	N/.N/2	N>=N>	OOc                     | d   }| d   }| d   }| d   }|j                   d   }| d   } t        d      ||      } t        d      ||      }	g }
|j                  t              D ]W  }|j	                         \  }}|j                  |      s(|j                  |      s:|j                  rG|
j                  |       Y |j                  t              D ])  }|j                  }||v s||v s|
j                  |       + |
D ]  }t        |j                  |      |j                  |      z        }t        |d	||g
      }|r|d   r||   }d||   |d   z  z  }|j                  |      |z  |j                  |      |z  z
  ||z  z
  ||z  z
  }t        |      s|||	|j                  ||      igc S d|z  }d|z  }|j                  |      |j                  |      |dz  z  z
  ||z  z
  ||z  z
  }t        t        |            r||j                  ||      |	|igc S d|z  }|j                  |      |j                  |      |z  z   |z
  ||z  z
  }t        |      s'|t         j"                  |	|j                  ||      igc S | }|j                  |       |z  |j                  |      |dz  z  z
  ||z  z
  |z
  }t        t        |            r||j                  ||      |	t         j"                  igc S  y)a  
    This heuristic assumes the presence of unknown functions or known functions
    with non-integer powers.

    1. A list of all functions and non-integer powers containing x and y
    2. Loop over each element `f` in the list, find `\frac{\frac{\partial f}{\partial x}}{
       \frac{\partial f}{\partial x}} = R`

       If it is separable in `x` and `y`, let `X` be the factors containing `x`. Then

       a] Check if `\xi = X` and `\eta = -\frac{X}{R}` satisfy the PDE. If yes, then return
          `\xi` and `\eta`
       b] Check if `\xi = \frac{-R}{X}` and `\eta = -\frac{1}{X}` satisfy the PDE.
           If yes, then return `\xi` and `\eta`

       If not, then check if

       a] :math:`\xi = -R,\eta = 1`

       b] :math:`\xi = 1, \eta = -\frac{1}{R}`

       are solutions.

    References
    ==========
    - E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order
      ODE Patterns, pp. 10 - pp. 12

    r0   rp   rq   r2   r   r/   r-   r.   Tr   r:   r8   r!   r   N)r;   r	   atomsr   as_base_expr   
is_IntegerrC   r
   r   r   r?   r   r   r>   r   r   One)r4   ru   r0   rp   rq   r2   rH   r/   r-   r.   funclistatombaser   r~   symsrG   fracrX   xitry1etatry1pde1xitry2etatry2pde2etatrypdexitrys                               r[   #lie_heuristic_abaco2_unique_unknownr   4  s   > 	c
A	tB	tB=D		!Ac
A	$4	 B
(5/!T
"CH &$$&	c88A;488A;>>%	& GGL) &$$9dOOH%&
  ?affQiq	)*4dQF;3w<VF#a&W-.G<<?1$v{{1~a'77&)CgbjPDD>VS',,q$*?@AAwYFhG<<?fkk!nad%::VBYFQSSDF4L)V[[D13@AA WF++a.6;;q>!#33b86"9DCC=QUUCQ)=>??EE::a=."EJJqM!Q$$66rA2ECF3K(UZZ40#quu=>>/?ri   c                 $   | d   }| d   }| d   }|j                   d   }| d   } t        d      ||      } t        d      ||      }|j                  |      }	|j                  |      |dz  z   }
|j                  |      |dz  z
  }|	r|
r|sy	|	j                  |      }|	j                  |      }|j                  |      }|j                  |      }|j                  |      }t        d|z  ||z  z   ||z  z
  ||z  d|	z  z   |	z  z         |	z  d
|z  |z  z
  }|st        d
|dz  z  |dz  d|z  z   |	z  d|z  z
  |	z  z         }|rt        d|z  d|
z  |dz  z
  |	z  z   |	z  d
|dz  z  z
        }|st        |d|z  d|z  |	z  z   |	d
z  z  |||	z  |z   z  z
  z  |j                  |      dz  |	dz  z  z
        }|s}t	        d|	d
z  z  |||	z  z
  z  |||	z  |z
  z  z   t        d      |	z  |z  z        }||vr@	 t        t        ||            }d|	d
z  z  |z  |z  }||vr||||j                  ||      igS y	y	y	y	y	t        d|z  d|
z  |dz  z
  |	z  z   |	z  d
|dz  z  z
        }|rt        d|	d
z  z  |z  |dz  z
  |d|z  |dz  d|z  z   |	z  z
  |	z  d
|dz  z  z
  z  z         }|st        |	|z   |j                  |      z  |j                  |      ||z  z
  |	z  d
|z  |z  z   |	|z  d
|z  z
  |z  z   |z  z         }|srt	        |	|z  |z
  |z  ||	|z  z   |z  z
  t        d      |	z  |z  z        }||vr;	 t        t        ||            }| |z  |z  }||vr||||j                  ||      igS y	y	y	y	y	# t        $ r Y y	w xY w# t        $ r Y y	w xY w)aQ  
    This heuristic finds if infinitesimals of the form `\eta = f(x)`, `\xi = g(y)`
    without making any assumptions on `h`.

    The complete sequence of steps is given in the paper mentioned below.

    References
    ==========
    - E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order
      ODE Patterns, pp. 10 - pp. 12

    rp   rq   r2   r   r/   r-   r.   r   N            )
r;   r	   r?   r   r   r   r   r   r>   rB   )r4   ru   rp   rq   r2   rH   r/   r-   r.   r   r   r   AxAyAxyAxxAyyDE1E2E3r   xivals                          r[   #lie_heuristic_abaco2_unique_generalr    sv    
tB	tB=D		!Ac
A	$4	 B
(5/!T
"C

A

RUA

RUA!	
B	
B
''!*C
''!*C
''!*C3BB&"R%!A#+q89!;ad2gEAaAg"a%!A#+q1S5!8! ;;<1S5AaC"a%K?2A5"a%?@BB2aA-BqD2I>?"''!*Q,qRStBSSU#Qq!tVR"Q$Y%7"bdRi.%H1Q4PQ6RT9$UVFP%(61)=%>F %'q!tGFN2$5E ~)+UCQ9M(N'O O  . '   $ qu!b!eQ.1Ab!eG;<!Q$q1a4"quA!Q'>&AAb!eG&K"LLNBqS6"''!*$bd):A(="Q(FrTAbD["$)%&(() )* #adRi^rAbDy!m%Cad1fQh$OPFP%(61)=%>F &(CJqLE ~)+UCQ9M(N'O O  . ' 	    3 ! !*  3 ! !s$   7M4 6N 4	N ?N 	NNc           
         | d   }| d   }| d   }| d   }|j                   d   }| d   } t        d      ||      } t        d      ||      }	i }
t        d	t        
      }t	        |d      D cg c]  }t        |       }}|\  }}}}}}|||z
  |z  z   ||z  ||z  z   |z   |z  z
  ||z  ||z  z   |z   |z  z
  ||dz  z  z
  }|j                         \  }}t        t        |            }|j                  r|j                   }|D ]  }|j                  rPt        |j                   D cg c]  }|j                  ||      r| c} }||z  }||
vr||
|<   Q|
|xx   |z  cc<   _||
vrt        j                  |
|<   w|
|xx   t        j                  z  cc<    |
j                         }t!        ||      }|rt#        |t$              r|d   }|j                         }t'        d |D              rt)        t+        |dgdz              }||z  ||z  z   |z   }||z  ||z  z   |z   } |j-                  |      }| j-                  |      } |j-                  |      }| j-                  |      } |||	| igS yyc c}w c c}w )a  
    This heuristic assumes

    1. `\xi = ax + by + c` and
    2. `\eta = fx + gy + h`

    After substituting the following assumptions in the determining PDE, it
    reduces to

    .. math:: f + (g - a)h - bh^{2} - (ax + by + c)\frac{\partial h}{\partial x}
                 - (fx + gy + c)\frac{\partial h}{\partial y}

    Solving the reduced PDE obtained, using the method of characteristics, becomes
    impractical. The method followed is grouping similar terms and solving the system
    of linear equations obtained. The difference between the bivariate heuristic is that
    `h` need not be a rational function in this case.

    References
    ==========
    - E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order
      ODE Patterns, pp. 10 - pp. 12

    r0   rp   rq   r2   r   r/   r-   r.   c)cls   r   c              3       K   | ]  }|  y w)N ).0ts     r[   	<genexpr>z'lie_heuristic_linear.<locals>.<genexpr>  s     !Qq!s   r!   N)r;   r	   r   r   r   nextr   r   r   r   r   r   r   r   r   r   r   rw   rc   r   r9   zipr>   )!r4   ru   r0   rp   rq   r2   rH   r/   r-   r.   	coeffdictr   r   symlistC0r7   C2C3C4C5r   rV   termstermmremxypartsollistr   subvalonedictr  r   s!                                    r[   lie_heuristic_linearr    s   0 	c
A	tB	tB=D		!Ac
A	$4	 B
(5/!T
"CIs.G&,Wa&89tG}9G9$BBB
R{
bdRTkB.2
2bdRTkB6F5J
JRPQSTPTW
TC##%JC
&+
C
zz 	-D{{tyyD!aADEc*(+If%f%,%y(&'eeIdOdOquu,O	-  GGW%Ggt$ajG!!&!!3wA./GqD2d7NR'ETBtG^b(FJJw'E[[)FJJw'E[[)FV,-- "	 / : Es   .I/&I4
>I4
c                    t        | t              r| j                  d   S | j                  r2| j	                  t              }|r|D ]  }| j                  |d      }  | S | j                  r.| j                         \  }}t        |      }t        |      }||z  S | j                  rHg }| j                  }|D ]-  }t        | t              r|j                  t        |             / t        | S | S )a  
    This function is strictly meant for internal use by the Lie group ODE solving
    method. It replaces arbitrary functions returned by pdsolve as follows:

    1] If coords is an arbitrary function, then its argument is returned.
    2] An arbitrary function in an Add object is replaced by zero.
    3] An arbitrary function in a Mul object is replaced by one.
    4] If there is no arbitrary function coords is returned unchanged.

    Examples
    ========

    >>> from sympy.solvers.ode.lie_group import _lie_group_remove
    >>> from sympy import Function
    >>> from sympy.abc import x, y
    >>> F = Function("F")
    >>> eq = x**2*y
    >>> _lie_group_remove(eq)
    x**2*y
    >>> eq = F(x**2*y)
    >>> _lie_group_remove(eq)
    x**2*y
    >>> eq = x*y**2 + F(x**3)
    >>> _lie_group_remove(eq)
    x*y**2
    >>> eq = (F(x**3) + y)*x**4
    >>> _lie_group_remove(eq)
    x**4*y

    r   )rw   r
   r;   r   r   r>   is_Powr   rA   r   rC   r   )coordssubfuncr2   r   exprmulargs	coordargsr   s           r[   rA   rA     s    > &,'{{1~	,,|, .T1-.	'')
d & &Tz	KK	 	7Cfl3056	7 G}Mri   )NNrv   N)F)>__doc__	itertoolsr   
sympy.corer   r   r   r   sympy.core.exprtoolsr   sympy.core.functionr	   r
   r   sympy.core.relationalr   r   sympy.core.symbolr   r   r   r   sympy.functionsr   r   sympy.integrals.integralsr   sympy.polysr   sympy.polys.polytoolsr   r   sympy.simplifyr   r   r   r   sympy.solversr   sympy.solvers.pder   sympy.utilitiesr   sympy.solvers.deutilsr   r    oder"   ra   r\   rh   r<   r   r   r   r   r   r   r   r  r  rA   r  ri   r[   <module>r6     s   &  ' ' - > > . : : $ /  -   % , 8 
EP"JI-XeNGRF!PE%NXtiLXK?\HPVF.R3ri   