
    wgg                        d dl mZ d dl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mZmZmZ d dl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 m!Z!m"Z"m#Z#m$Z$ d dl%m&Z& d dl'm(Z(m)Z) d dl*m+Z+ dddefdZ,ddZ- ed      Z.d Z/y)    )defaultdict)reduce)prod)
expand_log	count_ops_coeff_isneg)	sympifyBasicDummySAddMulPow
expand_mulfactor_terms)ordereddefault_sort_key)IntegerRational)_keep_coeff)	Transform)	exp_polarexplogrootpolarify
unpolarify)MatrixSymbol)lcmgcd)multiplicityFallc                   789:; fd}t        |       } t        | t              r4t        | t              s$| j                  s| t        d      t        d      fv r| S s*| j                  s| j                  r?t        | j                  vr- | j                  | j                  D cg c]
  } ||       c} } | j                  r || t        z  d      t        z  S | j                  s| S dv rt        t              }g }g }	t        j                  }
| j                  D ]"  }|j                   r|
|z  }
|j                  rt#        |      }|j$                  rv|j'                         \  }}r||fD cg c]
  } ||       c}\  }}|j                  st        |t(              r||z  t        j                  }}||   j+                  |       |ra|d   j'                         \  }}|j'                         \  }}||k(  r3|j$                  r'|j$                  rt-        |t/        ||            |d<   |j+                  |       % t1        t3        |j5                                     D ]  \  }}|r|j                   rt7        d |D              sm|
t        j                  ur[|t        j                  t        j8                  fvr9t;        t=        |      t=        |
            }|r|j+                  |       |
||z  z  }
t/        | ||<    |
t        j                  ur3|
|v r||
xx   t        j                  z  cc<   nt        j                  ||
<   t?        |      }t        |j5                               }tA               }|D ]  \  }}||v r|jB                  xs |jD                  }|s(d|z  }||k7  s3||v s8|jG                         d   t        j                  u r|jI                  |       ||xx   |z  cc<   z|jK                  |       |jI                  |      }||xx   |z  cc<    t        |j5                               }t        j8                  }|D ]  \  }}|jL                  s|j                  s| |v s%||v s*|jB                  |jN                  sC|jN                  s|jP                  r|| xx   |jI                  |      z  cc<   n|jI                  |       }||xx   |z  cc<   ||v r||xx   |z  cc<   |||<    |j5                         D cg c]  \  }}|s	||f }}}9fd	;d9fd
	989:fd}i :g }g 8|D ]P  \  }} 9||      \  }}|:v r:|   |z   :|<   n|:|<   |d   dk7  s0|d   j                  s@8j+                  |       R 8jS                  tT               8jS                  d       8D ]  }|:vr	|\  }}d}d}	 |}|} g }!g }"tW        jX                  |      D ]F  }# 9|#      \  }$}%|$:vs:|$   |%k  rg x}"}! n)|"j+                  |%:|$   g       |!j+                  |$       H |"r|"d   d   |"d   d   z  }&t[        dt]        |"            D ]&  }|"|   d   |"|   d   z  }'|'dk  r n^t_        |&|'      }&( t[        t]        |!            D ]&  }:|!|   xx   |&|"|   d   z  z  cc<    ||!|          ( :|xx   |&| z  |z  z  cc<   |s t]        :      dk(  st7        d :D              rnmta        tW        jX                  |      D #cg c]
  }# ;|#       c}#      }|dk(  rn5||z  }|| z  }t7        ;fdtW        jX                  |      D              rd}|\  }}(|j+                  |:jI                  |      tc        d|(      z  f        |}:j5                         D ]  \  \  }}(}|j                  st        |t(              rD|(t        j                  ur2|j(                  j                   s|j'                         \  }}|||(z  z  }nte        ||(      }|j+                  ||f        t]        |      })t?        |      }t]        |      |)k(  sJ  | j                  |	|j5                         D cg c]  \  }}t-        ||       c}}z    }	dk(  r| j                  |	 | j                  |       S  | | j                  | d       ||	d      z  S dk(  rg }g }| j                  D ]H  }|j$                  r)|j+                  t        |j'                                      8|j+                  |       J t[        t]        |            D ]  }||   \  }}t7        d |jG                         D              s|jN                  ss|jD                  sF|jg                  d      \  }*}+|*t        j                  usn|+t        j                  ust-        ||*      |+g||<    t        t              },|D ]e  \  }}r ||      }|j                  r6|jB                  s|jN                  rti        |      }tk        |      r| }d|z  }|,|   j+                  |       g ~t        t              }|,D ]  }|,|   8t]        8      dk(  r8d   }-n|jN                  sr | j                  8 }-ng }.g }/g }08D ]m  }#|#jP                  r|0j+                  |#       !|#jl                  r|/j+                  |#       ?|#jD                  r|/j+                  |#       ]|.j+                  |#       o t]        |.      dk(  r|0rt]        |0      dk(  r|.s|/jo                  |.|0z          g x}.}0n|0rd}1|j                   rd}1n-|jG                         \  }2}3|2jN                  r|3jN                  rd}1|1r=|0D cg c]  }|  }0}|.jo                  t        j8                  gt]        |0      z         n|.jo                  |0       g }0~1|.D ]  }||   j+                  |         | j                  |/|0z    }-7fd7tq        |-d      }4t]        t/        jX                  |4             7|-      k  rti        |4      }-||-   j+                  |        |j5                         D 5cg c]  \  }}|D ]  }5t-        ||5        }6}}}5 | j                  |6|z    S ts        d      c c}w c c}w c c}}w c c}#w c c}}w c c}w c c}5}}w )a
  
    Reduce expression by combining powers with similar bases and exponents.

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

    If ``deep`` is ``True`` then powsimp() will also simplify arguments of
    functions. By default ``deep`` is set to ``False``.

    If ``force`` is ``True`` then bases will be combined without checking for
    assumptions, e.g. sqrt(x)*sqrt(y) -> sqrt(x*y) which is not true
    if x and y are both negative.

    You can make powsimp() only combine bases or only combine exponents by
    changing combine='base' or combine='exp'.  By default, combine='all',
    which does both.  combine='base' will only combine::

         a   a          a                          2x      x
        x * y  =>  (x*y)   as well as things like 2   =>  4

    and combine='exp' will only combine
    ::

         a   b      (a + b)
        x * x  =>  x

    combine='exp' will strictly only combine exponents in the way that used
    to be automatic.  Also use deep=True if you need the old behavior.

    When combine='all', 'exp' is evaluated first.  Consider the first
    example below for when there could be an ambiguity relating to this.
    This is done so things like the second example can be completely
    combined.  If you want 'base' combined first, do something like
    powsimp(powsimp(expr, combine='base'), combine='exp').

    Examples
    ========

    >>> from sympy import powsimp, exp, log, symbols
    >>> from sympy.abc import x, y, z, n
    >>> powsimp(x**y*x**z*y**z, combine='all')
    x**(y + z)*y**z
    >>> powsimp(x**y*x**z*y**z, combine='exp')
    x**(y + z)*y**z
    >>> powsimp(x**y*x**z*y**z, combine='base', force=True)
    x**y*(x*y)**z

    >>> powsimp(x**z*x**y*n**z*n**y, combine='all', force=True)
    (n*x)**(y + z)
    >>> powsimp(x**z*x**y*n**z*n**y, combine='exp')
    n**(y + z)*x**(y + z)
    >>> powsimp(x**z*x**y*n**z*n**y, combine='base', force=True)
    (n*x)**y*(n*x)**z

    >>> x, y = symbols('x y', positive=True)
    >>> powsimp(log(exp(x)*exp(y)))
    log(exp(x)*exp(y))
    >>> powsimp(log(exp(x)*exp(y)), deep=True)
    x + y

    Radicals with Mul bases will be combined if combine='exp'

    >>> from sympy import sqrt
    >>> x, y = symbols('x y')

    Two radicals are automatically joined through Mul:

    >>> a=sqrt(x*sqrt(y))
    >>> a*a**3 == a**4
    True

    But if an integer power of that radical has been
    autoexpanded then Mul does not join the resulting factors:

    >>> a**4 # auto expands to a Mul, no longer a Pow
    x**2*y
    >>> _*a # so Mul doesn't combine them
    x**2*y*sqrt(x*sqrt(y))
    >>> powsimp(_) # but powsimp will
    (x*sqrt(y))**(5/2)
    >>> powsimp(x*y*a) # but won't when doing so would violate assumptions
    x*y*sqrt(x*sqrt(y))

    c                     |j                  d      }|j                  d      }|j                  d      }|j                  d	      }t        | ||||      S )Ndeepcombineforcemeasure)getpowsimp)
argkwargs_deep_combine_force_measurer&   r%   r'   r(   s
         [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/simplify/powsimp.pyrecursezpowsimp.<locals>.recurseg   sV    

64(::i1GU+::i1sE8VX>>    r      F)r%   )r   r"   c              3   4   K   | ]  }|j                     y wN)	is_Number).0eis     r1   	<genexpr>zpowsimp.<locals>.<genexpr>   s     /I/I   c                       |       d   d   S )zLReturn Rational part of x's exponent as it appears in the bkey.
            r   r4    )xbkeys    r1   ratqzpowsimp.<locals>.ratq   s     71:a= r3   c                 L   ||j                   r| t        j                  f|fS |j                  r,| t	        |j
                        ft	        |j                        fS |j                  d      \  }}|t        j                  urj|j                  r/| t	        |j
                        f|t	        |j                        z  fS | |z  t	        |j
                        ft	        |j                        fS | |z  t        j                  ft        j                  fS  | j                          S )a@  Return (b**s, c.q), c.p where e -> c*s. If e is not given then
            it will be taken by using as_base_exp() on the input b.
            e.g.
                x**3/2 -> (x, 2), 3
                x**y -> (x**y, 1), 1
                x**(2*y/3) -> (x**y, 3), 2
                exp(x/2) -> (exp(a), 2), 1

            Trational)

is_Integerr   Oneis_Rationalr   qpas_coeff_Mul
is_integeras_base_exp)becmr@   s       r1   r@   zpowsimp.<locals>.bkey   s     }<<quu:q=(]]wqss|,gaccl::>>4>8DAq~<<$%wqss|#4an#DD !1gaccl3WQSS\AA !1aee}aee33Q]]_--r3   c                    t        |    | d         \  }}|spj                  |        |r\t        j                  | d   |z        D ]=  } |      \  } }| vrd| <   | xx   |z  cc<   | d   dk7  s-j	                  |        ? yyy)aL  Decide what to do with base, b. If its exponent is now an
            integer multiple of the Rational denominator, then remove it
            and put the factors of its base in the common_b dictionary or
            update the existing bases if necessary. If it has been zeroed
            out, simply remove the base.
            r4   r   N)divmodpopr   	make_argsappend)rM   newerrP   rN   basesr@   common_bs        r1   updatezpowsimp.<locals>.update   s     Xa[!A$/GD!Q ]]1Q4:6 ,#Aw1H,*+HQK q(Q419!LLO,  r3   )keyT)r[   reversec              3   ,   K   | ]  }|d    d k(    ywr4   Nr>   )r9   ks     r1   r;   zpowsimp.<locals>.<genexpr>K  s     71Q4197s   c              3   4   K   | ]  } |      d k(    ywr^   r>   )r9   birA   s     r1   r;   zpowsimp.<locals>.<genexpr>X  s     @tBx1}@s   r   base)r&   c              3   4   K   | ]  }|j                     y wr7   )is_nonnegative)r9   r?   s     r1   r;   zpowsimp.<locals>.<genexpr>  s     EQ((Er<   rC   c                     | j                   rt        fd| j                  D              S | j                  r)t	        | j                  D cg c]
  } |       c}      S yc c}w )Nc              3   .   K   | ]  } |        y wr7   r>   )r9   ai_termss     r1   r;   z*powsimp.<locals>._terms.<locals>.<genexpr>  s     "?"6":"?s   r4   )is_Addsumargsis_Mulr   )rN   mirh   s     r1   rh   zpowsimp.<locals>._terms  sO     xx""?"???xx#!&&$ABVBZ$ABB %Bs   A"z.combine must be one of ('all', 'exp', 'base').r7   ):r	   
isinstancer
   r   is_Atomr   ri   rl   _yrk   funcis_Powr   listr   rF   rG   _denest_powis_commutativerL   r   rU   r   r   r   iteritemsr"   NegativeOner!   absdictsetis_positiveis_polaras_numer_denomrS   add	is_SymbolrK   is_negativesortr   r   rT   rangelenminr   r   r   rJ   r   r   rd   extendr   
ValueError)<exprr%   r&   r'   r(   r2   wc_powersnc_partnewexprcoefftermrM   rN   ib1e1b2e2rP   beskipbposbinv_nrZ   donerb   exponentlastqlcmbstartqstartbbeera   bibbiemin1ratrH   checkexp_cexp_tc_expnew_baseunknonnegnegisratrI   d	xnew_baser:   c_partrh   rX   r@   rY   rA   s<    ````                                                  @@@@@r1   r*   r*      s0   j? 4=DtU#z$'ELLDYq\9Q<$@@t{{dkkb		.Atyytyy9!71:9:{{tBwU+B..;; .  t$II 	%D{{"4(""'')112A71GAJ7DAq88z!S1 a4qA""1% $R[446FB!--/FBb--"2C2C&)"c"bk&: t$3	%8 D!123 	"DAq ammC/Iq/I,I&aeeQ]]33 QU4HHQKQTMEq'HQK	"  155("#%% > (.."#u 	)DAqDy==.AJJDs9!1'')!,5 Q !+$LL. q(	)  (.."#]] 	)DAqqxxaR8^XMM-||q}} !Q7$LL!, q(X~ )'(	) (0~~'7=tq!1QF==	!
	.4	,*  	 DAq1:DAqH}&qkAotqyQqT[[Q	  	

'
(

w
- ;	@D8#KAxDD--* #B#BxHC(*hsmc.A"$RIIsHSM23IIcN#  a58RU1X-D"1c"g. ? eAh1a07!"4~	? "'s2w *A$RUOtBqE!H}<O"2a5M* !$v+h*>>8})7h77 s}}V/DEDHEF19DL@s}}Q/?@@Dc h DAqKKHLL.x1~=>?w;	@|  ") 	$IFQAJq#.QUUN155+<+<21IAJOOQF#	$ H>8}%%% $))gx~~?O(Ptq!Q(PPRe99Widii&9::9499g.?01 1 
F	 II 	%D""T%5%5%7 89t$		% s8}% 	5AA;DAqE!2B2B2DEEY^bcblbl>>4>8LE5AEE!e155&8"1e}e4	5 D! 	DAqAJxxQ]]all O?A!A!HOOA	  t$ B	)A!HE 5zQ 8$499e,  	'B~~

2**b)  

2	' s8q=CAcMM#), "NC# "E}} $ //11<<ALL$(E+./ar//

AMM?3s8#;<

3   *AQK&&q)* %499v|5 'xe<	s}}Y/06(3CC+I6HX%%a(EB	)J *2)9FFAAFb#a*F*FF tyy6G+-- IJJk :2 8V >t F< )Qj 0< Gs0   )s6s
s$$s$s*
s/3
s5s:c                 $  
 ddl m} |r
fd
g }| j                  t        t              D ]M  }t        |j                  t        t        f      s$ 
|j                   \  }}|dus;|j                  ||f       O |r| j                  |      }  ||       \  } }t        | d|      j                  |      S |r.t        |       \  } }t        t        t        | d            |      S t        |       }	|	j                  t        t         d 	            S )
a
  
    Collect exponents on powers as assumptions allow.

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

    Given ``(bb**be)**e``, this can be simplified as follows:
        * if ``bb`` is positive, or
        * ``e`` is an integer, or
        * ``|be| < 1`` then this simplifies to ``bb**(be*e)``

    Given a product of powers raised to a power, ``(bb1**be1 *
    bb2**be2...)**e``, simplification can be done as follows:

    - if e is positive, the gcd of all bei can be joined with e;
    - all non-negative bb can be separated from those that are negative
      and their gcd can be joined with e; autosimplification already
      handles this separation.
    - integer factors from powers that have integers in the denominator
      of the exponent can be removed from any term and the gcd of such
      integers can be joined with e

    Setting ``force`` to ``True`` will make symbols that are not explicitly
    negative behave as though they are positive, resulting in more
    denesting.

    Setting ``polar`` to ``True`` will do simplifications on the Riemann surface of
    the logarithm, also resulting in more denestings.

    When there are sums of logs in exp() then a product of powers may be
    obtained e.g. ``exp(3*(log(a) + 2*log(b)))`` - > ``a**3*b**6``.

    Examples
    ========

    >>> from sympy.abc import a, b, x, y, z
    >>> from sympy import Symbol, exp, log, sqrt, symbols, powdenest

    >>> powdenest((x**(2*a/3))**(3*x))
    (x**(2*a/3))**(3*x)
    >>> powdenest(exp(3*x*log(2)))
    2**(3*x)

    Assumptions may prevent expansion:

    >>> powdenest(sqrt(x**2))
    sqrt(x**2)

    >>> p = symbols('p', positive=True)
    >>> powdenest(sqrt(p**2))
    p

    No other expansion is done.

    >>> i, j = symbols('i,j', integer=True)
    >>> powdenest((x**x)**(i + j)) # -X-> (x**x)**i*(x**x)**j
    x**(x*(i + j))

    But exp() will be denested by moving all non-log terms outside of
    the function; this may result in the collapsing of the exp to a power
    with a different base:

    >>> powdenest(exp(3*y*log(x)))
    x**(3*y)
    >>> powdenest(exp(y*(log(a) + log(b))))
    (a*b)**y
    >>> powdenest(exp(3*(log(a) + log(b))))
    a**3*b**3

    If assumptions allow, symbols can also be moved to the outermost exponent:

    >>> i = Symbol('i', integer=True)
    >>> powdenest(((x**(2*i))**(3*y))**x)
    ((x**(2*i))**(3*y))**x
    >>> powdenest(((x**(2*i))**(3*y))**x, force=True)
    x**(6*i*x*y)

    >>> powdenest(((x**(2*a/3))**(3*y/i))**x)
    ((x**(2*a/3))**(3*y/i))**x
    >>> powdenest((x**(2*i)*y**(4*i))**z, force=True)
    (x*y**2)**(2*i*z)

    >>> n = Symbol('n', negative=True)

    >>> powdenest((x**i)**y, force=True)
    x**(i*y)
    >>> powdenest((n**i)**x, force=True)
    (n**i)**x

    r   )posifyc                     t        | t        t        f      s| j                  t        | |d      fS  | j                  | j                  |z        S )NF)evaluate)rn   r   r   r|   rb   )rM   rN   _denests     r1   r   zpowdenest.<locals>._denestK  sB    a#s,}}c!Q&???1661557++r3   F)r'   polarT)exponents_onlyc                 >    | j                   xs t        | t              S r7   )rr   rn   r   )rP   s    r1   <lambda>zpowdenest.<locals>.<lambda>`  s    ahh&D*Q2D r3   )filter)sympy.simplify.simplifyr   atomsr   r   rn   rb   rk   rU   subs	powdenestxreplacer   r   r*   r   rt   )eqr'   r   r   repsrI   okdprepnewr   s             @r1   r   r     s    v /	, #s# 	)A!&&3*- !&&)BU?KKB(		)
 B":D56??EE2,C)Jr$$GH#NN
"+C<<	DF G Gr3   yc                    ddl m} | j                         \  }}|j                  st	        |t
              r-|dk7  r(|j                  |      }||} |j                         \  }}|t        j                  u r|j                  rg }g }|j                  D ]J  }t        d t        j                  |      D              r|j                  |       :|j                  |       L  |t        |       }t!        t        |      t        |       S |j                         \  }}	|	t        j"                  u r5|j                  s)|j$                  r|j&                  dk7  s|j(                  s| S g g }}
t        j                  |      D ]?  }|j*                  r |
j                  |j                                /|j                  |       A t-        |
      dk(  rB|
d   d   j                  s0t!        |
d   d   |
d   d   |z        t/        t        | |z        z  S |
r?t        |
D cg c]  \  }}t/        |||z  z         c}} t/        t        | |z        z  S |j0                  rOt3        t5        |            }|j                  r/|j                  \  }}||z  }|j                  d   }t!        ||      S |j                  r%t        d t        j                  |      D              r| S d }t3        t5        |            }|j6                  rV|j                  }t9        ||      }|dk7  r9|j;                         \  }}t=        ||t        |D cg c]  }||z  	 c} z        }t	        |t4              s|j                  s|j                  d   j                  st	        |j                  d   t
              rVt?        |j                  d         }tA        |j
                        dk  dk(  r#t!        |jB                  |j
                  |z        S | S g }g }|j                  D ]1  }|j6                  r|j                  |       !|j                  |       3 t!        t         |t        |             |t        | z        S c c}}w c c}w )zr
    Denest powers.

    This is a helper function for powdenest that performs the actual
    transformation.
    r   )
logcombiner4   c              3   <   K   | ]  }t        |t                y wr7   )rn   r   )r9   rg   s     r1   r;   z_denest_pow.<locals>.<genexpr>z  s     C2:b#&Cs   c              3   4   K   | ]  }|j                     y wr7   )ro   )r9   ss     r1   r;   z_denest_pow.<locals>.<genexpr>  s     ?199?r<   c                    | |fD cg c]  }|j                          c}\  }}t        |d   |d         j                         d   }t        |d   j	                  d      d   |d   j	                  d      d   z   }t        ||      S c c}w )Nr   r4   T)cset)rJ   r    r~   r   args_cncr   )aar   r   arM   rO   gs          r1   nc_gcdz_denest_pow.<locals>.nc_gcd  s    +-r(3Q 31!adO**,Q/!A$--T-*1-!40H0KKM1a   4s   B
T)"r   r   rL   rr   rn   r   _eval_powerr   Exp1rl   rk   anyr   rT   rU   r   r   rF   rG   rH   r|   r}   r   r   rE   r   r   ri   r   rJ   r   rt   ry   rb   )r   r   rM   rN   r   logsotherr:   _r   polars	nonpolarsr   r   logbrO   rb   r   glogbrk   r   cgrgr   r   s                            r1   rt   rt   e  s    3>>DAqxx:a%!q&mmA?B??$DAq 	AFF{qxx&& 	!BCr1BCCBR 		!
 #t*%3t9c5k**MMOEAr	QUU{AHHMMaccQhMM	 BIFmmA !;;MM"..*+R 	!
 6{aq	! 3 36!9Q<1a03	?A;M1NNN	6BxBYrBqDz*BCsI)*+ 	+ 	||#a&!;;iiGAtFA99Q<DtQ< 88s?cmmA.>??	! s1vE||zz64 6^^%FBBs$,?QQqS,?'@$@AE %U\\::a=:ejjmS#A

1.EEII"t+5::uyy{33	 CEZZ 88JJqMLLO	
 s:c3i()1S%[=99m CH -@s   Q,
>Q2N)FF)0collectionsr   	functoolsr   mathr   sympy.core.functionr   r   r   
sympy.corer	   r
   r   r   r   r   r   r   r   sympy.core.sortingr   r   sympy.core.numbersr   r   sympy.core.mulr   sympy.core.rulesr   sympy.functionsr   r   r   r   r   r   "sympy.matrices.expressions.matexprr   sympy.polysr   r    sympy.ntheory.factor_r!   r*   r   rp   rt   r>   r3   r1   <module>r      sd    #   C C X X X 8 0 & & K K ;   . e5) XKvsGj 3Ze:r3   