
    wg7G                     V   d 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mZ ddlmZmZ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 m!Z!m"Z"m#Z# ddl$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/ ddl0m1Z1 ddl2m3Z3 d Z4d Z5ddZ6d Z7d Z8dejr                  fdZ:d Z;y)a  
This File contains helper functions for nth_linear_constant_coeff_undetermined_coefficients,
nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients,
nth_linear_constant_coeff_variation_of_parameters,
and nth_linear_euler_eq_nonhomogeneous_variation_of_parameters.

All the functions in this file are used by more than one solvers so, instead of creating
instances in other classes for using them it is better to keep it here as separate helpers.

    )defaultdict)AddS)diffexpand_mexpand
expand_mul)Eq)default_sort_key)DummyWild)
expcoscoshimlogresinsinhatan2	conjugate)Integral)PolyRootOfrootofroots)collectsimplifyseparatevarspowsimptrigsimp)numbered_symbols)solve)	wronskian   )sub_func_doit)get_numbered_constantsc                 P   |j                   d   }|j                  }|dk  rt        d      | dk(  ry|dk(  r|| j                  v ryy| j                  r)| j                   ||            ry||z  | j                   v S | j                  r| j                         ||fk(  S |dk(  r|| k(  S y)a   
    Linear Euler ODEs have the form  K*x**order*diff(y(x), x, order) = F(x),
    where K is independent of x and y(x), order>= 0.
    So we need to check that for each term, coeff == K*x**order from
    some K.  We have a few cases, since coeff may have several
    different types.
    r   zorder should be greater than 0TFr%   )argsfunc
ValueErrorfree_symbolsis_Mulhasis_Powas_base_exp)coeffr*   orderxfs        e/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/solvers/ode/nonhomogeneous.py
_test_termr6      s     			!A		Aqy9::zz"""||99QqT?%x5::%%	  "q%j00	!Ez    c                 8   |j                   d   }|j                  }t        j                  t	        d      }}|D ]6  }|dk\  s	|||   t        ||z  ||      z  || z  z  j                         z  }8 t        ||      }t        |j                               D cg c]  }t        ||       }	}g }
t        t        | |j                         dz              }|j                          t        t              }|	D ]  }||xx   dz  cc<    t        j                  }t         }|j#                         D ]  \  }}t        |      D ]	  }t%        |t&              r3|||z  |j)                         z  z  }|dk7  rt+        d      d|dfg|
z   }
G|j,                  r/| ||      |z  ||z  z  |j)                         z  z  }||dfg|
z   }
t/        |      }t1        |      }| ||      |z  ||z  z  |j)                         t3        t5        |       ||      z        z  |j)                         t7        | ||      z        z  z   z  z  }|||fg|
z   }
 ! t9         ||      |      }g }|
D ]  \  }}}|dk(  r!|j;                   ||      |z  ||z  z         - ||      |z  ||z  z  t3        t5        |       ||      z        z  }||v r8 ||      |z  ||z  z  t7        | ||      z        z  }|j;                  |       |j;                  |        ||fS c c}w )a0  
    Returns the solution of homogeneous part of the linear euler ODE and
    the list of roots of characteristic equation.

    The parameter ``match_obj`` is a dict of order:coeff terms, where order is the order
    of the derivative on each term, and coeff is the coefficient of that derivative.

    r   r3      )numr%   zValue should be 1)r)   r*   r   Zeror   r   r   r   rangedegreer   listr'   reverser   intr   items
isinstancer   popr+   is_realr   r   r   absr   r
   append)eqr*   	match_objr3   r4   chareqsymbolikchareqrootscollectterms	constants	charrootsrootgsollnmultiplicityrerootimrootgensolssin_formcos_forms                         r5   !_get_euler_characteristic_eq_solsrZ   :   s0    			!A		A VVU3ZFF O6y|DFAq$99!fW*DLLNNFO &&!F.3FMMO.DE6&!$EKEL +BFMMOA4EFGI C I $166D	B'oo/ Dl|$ 	DA$'DIMMO331$$%899!"D!~<1q!T'*Y]]_<<!"D!~<DD1qAvI.MMOc#f+be*;&<<mmoF2a5L(99:; ; "#FF 34|C	DD$ adD>DG) 	)66Q;NN2a5!8AvI-.!uax6	)#c&k"Q%.?*@@H7"a5!8AvI-c&A,.??x(x(	) =[ Fs   Lc                    |j                   }|j                  d   }|}	d}
t        ||      }|rt        |      }t	        |dd      }|s,t        dt        |      z   dz   dz   t        |       z   dz         t        |      |k7  r,t        dt        |      z   dz   dz   t        |       z   dz         t        j                  |z  }|D ]I  }|
|t        t        |D cg c]
  }||k7  s	| c}|      |	d	   z  |z  |      z  |z  |	|   z  z  }
|d	z  }K |rt        |
      }
t	        |
d
      }
t         ||      |j                  |
z         S c c}w )a  
    Helper function for the method of variation of parameters and nonhomogeneous euler eq.

    See the
    :py:meth:`~sympy.solvers.ode.single.NthLinearConstantCoeffVariationOfParameters`
    docstring for more information on this method.

    The parameter are ``match_obj`` should be a dictionary that has the following
    keys:

    ``list``
    A list of solutions to the homogeneous equation.

    ``sol``
    The general solution.

    r   T)deep	recursiveCannot find z: solutions to the homogeneous equation necessary to apply zvariation of parameters to z (Wronskian == 0) (number of terms != order))r\   )r*   r)   r$   r   r!   NotImplementedErrorstrlenr   NegativeOner   r
   rhs)rG   r*   r   homogen_solr2   rH   simplify_flagr4   r3   rpsolwr
negonetermrK   sols                  r5   _solve_variation_of_parametersrm   ~   s   $ 			A		!AAD	5!	Bb\ btt4 ".3u:"=D#E%#&(+B#02E#F G 	G 5zU!.3u:"=D#E%#& 	B# 0#0 1 	1 'J 
8Ie.Pssaxs.PRS$TUVWYUZ$Z[]$]_`aabccdefkdlllb
 ~4(adKOOd*++ /Qs   
E	"E	c           
      V   |j                   d   }t        j                  t        d      }}| j	                         D ]&  }t        |t              s|dk  r|| |   ||z  z  z  }( t        ||      }t        |d      }t        |      |k7  r0t        |j                               D cg c]  }t        ||       }}t        d |j                         D               }	t        t               }
|D ]  }|
|xx   dz  cc<    g }g }g }|D ]  }||
vr	|
j#                  |      }t        |      D ]o  }|	r-|j%                  ||z  t'        ||z        z         ||dfg|z   }3t)        |      }t+        |      }|j-                  t.              rB|j-                  t.              r-|j%                  ||z  t'        ||z        z         ||dfg|z   }||v r
|||fg|z   }|dk(  r-|j%                  ||z  t'        ||z        z         ||dfg|z   }|j%                  t1        |             |j%                  ||z  t'        ||z        z  t3        t5        |      |z        z         |j%                  ||z  t'        ||z        z  t7        ||z        z         |||fg|z   }r  ||fS c c}w )ae  
    Returns the roots of characteristic equation of constant coefficient
    linear ODE and list of collectterms which is later on used by simplification
    to use collect on solution.

    The parameter `r` is a dict of order:coeff terms, where order is the order of the
    derivative on each term, and coeff is the coefficient of that derivative.

    r   r3   T)multiplec              3   4   K   | ]  }|j                     y wN)rD   ).0rK   s     r5   	<genexpr>z4_get_const_characteristic_eq_sols.<locals>.<genexpr>   s     Ga		Gs   r%   )r)   r   r;   r   keysrB   rb   r   r   rc   r<   r=   r   all
all_coeffsr   r@   rC   rF   r   r   r   r.   r   r   r   rE   r   )rh   r*   r2   r3   rI   rJ   rK   rM   rL   chareq_is_complexrP   rQ   rN   rW   conjugate_rootsrT   rU   rV   s                     r5   !_get_const_characteristic_eq_solsry      s    			!AVVU3ZFFVVX %aQad619n$F	% &&!F .K
;5 272HIQvfa(IIG63D3D3FGGG C I $1 LGO Dy  }}T*|$ 	DA q!tCQK/0!"D!~<XFXFzz% VZZ%6 q!tCQK/0!"D!~<?*%&$7#8<#GLQ;NN1a4F1H#56%&N#3l#BL&&y7q!tCqM1CFa4HHIq!tCqM1CFaK4HHI "#FF 34|C3	DD> L  [ Js   &J&c           
      p   |j                   }|j                  d   }|j                  t               |j	                          t        |       dk(  r| d   j                   ||      k(  sJ | d   j                  } t        |       } |D ]i  \  }}}t        | ||z  t        ||z        z  t        t        |      |z        z        } t        | ||z  t        ||z        z  t        ||z        z        } k |D ]$  \  }}}t        | ||z  t        ||z        z        } & t        |       } t         ||      |       S )ah  
    Helper function which collects the solution on
    collectterms. Ideally this should be handled by odesimp.It is used
    only when the simplify is set to True in dsolve.

    The parameter ``collectterms`` is a list of tuple (i, reroot, imroot) where `i` is
    the multiplicity of the root, reroot is real part and imroot being the imaginary part.

    r   )keyr%   )r*   r)   sortr   r?   rc   lhsre   r	   r   r   r   rE   r   r    r
   )rl   r*   rN   r4   r3   rK   rU   rV   s           r5   _get_simplified_solr~      s/    			A		!A*+s8q=SVZZ1Q4///
a&**C
S/C) =66c1a4F1H-c#f+a-.@@Ac1a4F1H-c&(m;<= * /66c1a4F1H-./
#,CadC=r7   Nc                   	
 t        d|g      t        d|g      t        | d      } i }
fd
t               f	fd		fd 
| |      |d	<   |d	   rt               }t        j                  |       D ]i  } 	||      }t
        j                  ur;t        fd
|D              r'|D ch c]  }||z  	 }}t        fd
|D              r'|j                  |      }k ||d<   |S c c}w )a  
    Returns a trial function match if undetermined coefficients can be applied
    to ``expr``, and ``None`` otherwise.

    A trial expression can be found for an expression for use with the method
    of undetermined coefficients if the expression is an
    additive/multiplicative combination of constants, polynomials in `x` (the
    independent variable of expr), `\sin(a x + b)`, `\cos(a x + b)`, and
    `e^{a x}` terms (in other words, it has a finite number of linearly
    independent derivatives).

    Note that you may still need to multiply each term returned here by
    sufficient `x` to make it linearly independent with the solutions to the
    homogeneous equation.

    This is intended for internal use by ``undetermined_coefficients`` hints.

    SymPy currently has no way to convert `\sin^n(x) \cos^m(y)` into a sum of
    only `\sin(a x)` and `\cos(b x)` terms, so these are not implemented.  So,
    for example, you will need to manually convert `\sin^2(x)` into `[1 +
    \cos(2 x)]/2` to properly apply the method of undetermined coefficients on
    it.

    Examples
    ========

    >>> from sympy import log, exp
    >>> from sympy.solvers.ode.nonhomogeneous import _undetermined_coefficients_match
    >>> from sympy.abc import x
    >>> _undetermined_coefficients_match(9*x*exp(x) + exp(-x), x)
    {'test': True, 'trialset': {x*exp(x), exp(-x), exp(x)}}
    >>> _undetermined_coefficients_match(log(x), x)
    {'test': False}

    a)excludebr   )combinec                 t   | j                        sy| j                  rt        fd| j                  D              S | j                  rm| j                  t
        t              r4d}| j                  D ]#  }|j                  t
        t              s|r yd}% t        fd| j                  D              S | j                  rN| j                  t
        t        t        t        t        fv r&| j                  d   j                  z  z         ryyy| j                  r<| j                  j                  r&| j                  j                   r| j                  dk\  ry| j                  r9| j                  j"                  r#| j                  j                  z  z         ryy| j                  s| j"                  ryy)zV
        Test if ``expr`` fits the proper form for undetermined coefficients.
        Tc              3   0   K   | ]  } |        y wrq    rr   rK   r6   r3   s     r5   rs   zG_undetermined_coefficients_match.<locals>._test_term.<locals>.<genexpr>F       ;Az!Q';   Fc              3   0   K   | ]  } |        y wrq   r   r   s     r5   rs   zG_undetermined_coefficients_match.<locals>._test_term.<locals>.<genexpr>R  r   r   r   )r.   is_Addru   r)   r-   r   r   is_Functionr*   r   r   r   matchr/   base	is_Symbol
is_Integer	is_number)exprr3   	foundtrigrK   r6   r   r   s    `  r5   r6   z4_undetermined_coefficients_match.<locals>._test_term?  sE    xx{[[;;;;[[xxS!!	  -AuuS#$#((,I- ;;;;yyS#sD$7799Q<%%acAg. [[TYY00TXX5H5HA[[TYY00xx~~acAg&^^t~~r7   c                    d }t        |       } | j                  rP| j                  D ]?  } |||      |v r|j                   |||             |j	                   |||            }A |S  || |      }|j	                  |h      }t               }||k7  rg|j                         }| j                  |      }  || |      }|j                  r|j	                   |||            }n|j                  |       ||k7  rg|}|S )aq  
        Returns a set of trial terms for undetermined coefficients.

        The idea behind undetermined coefficients is that the terms expression
        repeat themselves after a finite number of derivatives, except for the
        coefficients (they are linearly dependent).  So if we collect these,
        we should have the terms of our trial function.
        c                     t         j                  }| j                  r*| j                  D ]  }|j	                  |      s||z  } |S | j	                  |      r| }|S )z
            Returns the expression without a coefficient.

            Similar to expr.as_independent(x)[1], except it only works
            multiplicatively.
            )r   Oner-   r)   r.   )r   r3   termrK   s       r5   _remove_coefficientzU_undetermined_coefficients_match.<locals>._get_trial_set.<locals>._remove_coefficientq  sY     55D{{ "AuuQx	"
 K !Kr7   )r	   r   r)   addunionsetcopyr   )r   r3   exprsr   r   tmpsetoldset_get_trial_sets          r5   r   z8_undetermined_coefficients_match.<locals>._get_trial_seth  s   	  $;;		 H&tQ/58II1$:;!KKtQ(FGEH*  'tQ/D[[$(FUFF"  yy|*43;;#\\.q&*IJFJJt$ F" Er7   c                 D    t        t        |             j                  S )zf This function checks whether the given trialset contains any root
            of homogeneous equation)r   r&   is_zero)r   eq_homogeneousr*   s    r5   is_homogeneous_solutionzA_undetermined_coefficients_match.<locals>.is_homogeneous_solution  s     mND$?@HHHr7   testc              3   .   K   | ]  } |        y wrq   r   )rr   tsr   s     r5   rs   z3_undetermined_coefficients_match.<locals>.<genexpr>  s     D"1"5Ds   trialset)	r   r    r   r   	make_argsr   r;   anyr   )r   r3   r*   r   retdicttemp_setrK   actr   r   r6   r   r   r   s     ``     @@@@@r5    _undetermined_coefficients_matchr     s    H 	S1#AS1#A4'DG'R '*e 0dI
 !q)GFOv 5t$ 	+A A&CQVV+DDD*-.B1R4.C. DDD~~c*H	+ '
N	 /s   5C2c                 Z   |}t        dt              }g }|d   }|d   }	|j                  }
|j                  d   }t	        |      |k7  r,t        dt        |      z   dz   dz   t        |       z   d	z         d}|D ]&  }t        |      }|j                  |       |||z  z  }( t        |  |
|      |      }t        t        t        |dgt	        |      d
z   z                    }t        |      }t        j                  |      D ]D  }t!        |d|g      }|j#                  ||         r|||   xx   |d   z  cc<   :|d   |||   <   F t%        t        |j'                               |      }|st        d| z        |j)                  |      }t+         |
|      |	j,                  |z         S )a#  
    Helper function for the method of undetermined coefficients.

    See the
    :py:meth:`~sympy.solvers.ode.single.NthLinearConstantCoeffUndeterminedCoefficients`
    docstring for more information on this method.

    The parameter ``trialset`` is the set of trial functions as returned by
    ``_undetermined_coefficients_match()['trialset']``.

    The parameter ``match`` should be a dictionary that has the following
    keys:

    ``list``
    A list of solutions to the homogeneous equation.

    ``sol``
    The general solution.

    r   )clsr>   rl   r   r^   z9 solutions to the homogeneous equation necessary to applyz undetermined coefficients to r_   r%   T)dictsymbolsr1   zfCould not solve `%s` using the method of undetermined coefficients (unable to solve for coefficients).)r"   r   r*   r)   rc   ra   rb   nextrF   r&   r   r>   zipr   r   r   r   getr#   valuessubsr
   re   )rG   r*   r2   r   r   rh   coeffs	coefflistrW   rR   r4   r3   	trialfuncrK   ceqs
coeffsdicts	coeffvalsri   s                       r5    _solve_undetermined_coefficientsr     s   * 	Acu-FIiGU8D		A		!A
7|u!.3u:"=C#D(#)+.r7#3 	&#& ' 	'
 I LQqS	
 AaD)
,Cd3x!c(ma.?)@ABCJ
3-C]]3 *s3>>!A$qt'
* zJqt* d:,,./;I!24678 	8
 >>)$DadDHHtO$$r7   )T)<__doc__collectionsr   
sympy.corer   r   sympy.core.functionr   r   r   r	   sympy.core.relationalr
   sympy.core.sortingr   sympy.core.symbolr   r   sympy.functionsr   r   r   r   r   r   r   r   r   r   sympy.integralsr   sympy.polysr   r   r   r   sympy.simplifyr   r   r   r    r!   sympy.utilitiesr"   sympy.solvers.solversr#   sympy.matricesr$   	subscheckr&   sympy.solvers.ode.oder'   r6   rZ   rm   ry   r~   r;   r   r   r   r7   r5   <module>r      s   	 $  B B $ / )   $ 5 5 M M , ' $ $ 8:AH0,fF!V4 48 Zz@%r7   