K ihddlmZddlmZddlmZddlmZmZm Z ddl m Z m Z m Z mZmZmZmZmZmZddlmZmZddlmZmZmZddlmZdd lmZdd lm Z m!Z!m"Z"m#Z#m$Z$m%Z%dd l&m'Z'dd l(m)Z)m*Z*dd l+m,Z,dddefdZ-ddZ.e dZ/dZ0y)) defaultdict)reduce)prod) expand_log count_ops _coeff_isneg) sympifyBasicDummySAddMulPow expand_mul factor_terms)ordereddefault_sort_key)IntegerRational equal_valued) _keep_coeff) Transform) exp_polarexplogrootpolarify unpolarify) MatrixSymbol)lcmgcd) multiplicityFallc Z789:;fd}t|}t|tr4t|ts$|js|t dt dfvr|Ss*|j s|jr?t|jvr-|j|jDcgc] }|| c}}|jr||tzdtz S|js|Sdvr$tt}g}g} tj} |jD]"} | j r| | z} | jr t#| } | j$rv| j'\} } r| | fDcgc] }|| c}\} } | jst| t(r| | ztj} } || j+| |ra|dj'\}}| j'\}}||k(r3|j$r'|j$rt-|t/|||d<|j+| %t1t3|j5D]\} } | r| j rt7d| Dsm| tjur[| tjtj8fvr9t;t=| t=| }|r| j+|| | |zz} t/| || <| tjur3| |vr|| xxtjz cc<ntj|| <t?|}t|j5}tA}|D]\} } | |vr | jBxs | jD}|s(d| z }| jFr tI| drtj|| <Y| |k7s_||vsd| jKdtjur|jM| ||xx| zcc<|jO||jM|} || xx| zcc<t|j5}tj8}|D]\} } | jPs | j s| |vs%| |vs*| jB | jRsC| jRs | jTr|| xx|jM| z cc<n|jM| } || xx| z cc<||vr||xx| z cc<| ||<|j5D cgc] \} } | s | | f}} } 9fd ;d9fd 989:fd }i:g}g8|D]P\} } 9| | \} } | :vr :| | z:| <n| :| <| ddk7s0| djs@8j+| R8jWtX 8jWd 8D]}|:vr |\} }d}d} | }|} g}!g}"t[j\| D]F}#9|#\}$}%|$:vs:|$|%krgx}"}!n)|"j+|%:|$g|!j+|$H|"r|"dd|"ddz}&t_dta|"D]&}|"|d|"|dz}'|'dkrn^tc|&|'}&(t_ta|!D]&}:|!|xx|&|"|dzzcc<||!|(:|xx|&| z|zz cc<|s ta:dk(st7d:Drnmtet[j\|D#cgc] }#;|# c}#}|dk(rn5||z} || z}t7;fdt[j\| Drd }|\} }(|j+| :jM|tgd|(zf|}:j5D]\\} }(} | jst| t(rD|(tjur2| j(j s| j'\} }| ||(z z} n ti| |(} |j+| | fta|})t?|}ta||)k(sJ|j| |j5D cgc]\} } t-| | c} } z} dk(r|j| |j|S||j|d|| dzSdk(rg}g}|jD]H} | j$r)|j+t| j'8|j+| Jt_ta|D]}||\} } t7d| jKDs| jRss | jDsF| jkd \}*}+|*tjusn|+tjust-| |*|+g||<tt},|D]e\} } r|| } | j r6| jBs | jRrtm| } to| r| } d| z } |,| j+| g~tt}|,D]} |,| 8ta8dk(r8d}-n| jRsr|j8}-ng}.g}/g}08D]m}#|#jTr|0j+|#!|#jpr|/j+|#?|#jDr|/j+|#]|.j+|#ota|.dk(r|0rta|0dk(r|.s|/js|.|0zgx}.}0n|0rd}1| j rd }1n-| jK\}2}3|2jRr|3jRrd }1|1r=|0Dcgc]}| }0}|.jstj8gta|0zn|.js|0g}0~1|.D]} || j+| |j|/|0z}-7fd7tu|-d}4tat/j\|47|-kr tm|4}-||-j+| |j5D 5cgc]\} } | D]}5t-| |5}6} } }5|j|6|zStwdcc}wcc}wcc} } wcc}#wcc} } wcc}wcc}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|jd}|jd}|jd}|jd }t|||||S)Ndeepcombineforcemeasure)getpowsimp) argkwargs_deep_combine_force_measurer'r&r(r)s \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/simplify/powsimp.pyrecursezpowsimp..recursegsV 64(::i1GU+::i1sE8VX>>rF)r&)rr#c34K|]}|jywN) is_Number).0eis r2 zpowsimp..s/I /Ic |ddS)zLReturn Rational part of x's exponent as it appears in the bkey. rr5)xbkeys r2ratqzpowsimp..ratqs71:a= r4cL||jr|tjf|fS|jr,|t |j ft |j fS|jd\}}|tjurj|jr/|t |j f|t |j zfS||zt |j ft |j fS||ztjftjfS|jS)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 Trational) is_Integerr One is_Rationalrqp as_coeff_Mul is_integer as_base_exp)becmrAs r2rAzpowsimp..bkeys}<<quu:q=(]]wqss|,gaccl::>>4>8DAq~<<$%wqss|#4a n#DD !1gaccl3WQSS\AA !1aee}aee33Q]]_--r4ct||d\}}|spj||r\tj|d|zD]=}|\}}|vrd|<|xx|z cc<|ddk7s-j |?yyy)aLDecide 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. r5rN)divmodpopr make_argsappend)rNnewerrQrObasesrAcommon_bs r2updatezpowsimp..updatesXa[!A$/GD! Q ]]1Q4:6,#Aw1H,*+HQK  q( Q419!LLO ,r4)keyT)r\reversec3,K|] }|ddk(ywr5Nr?)r:ks r2r<zpowsimp..Os71Q4197sc34K|]}|dk(ywr_r?)r:birBs r2r<zpowsimp..\s@tBx1}@srbase)r'c34K|]}|jywr8)is_nonnegative)r:r@s r2r<zpowsimp..sEQ((Er=rDc|jrtfd|jDS|jr)t |jDcgc] }| c}Sycc}w)Nc3.K|] }|ywr8r?)r:ai_termss r2r<z*powsimp.._terms..s"?"6":"?sr5)is_Addsumargsis_Mulr)rOmiris r2rizpowsimp.._termssOxx""?"???xx#!&&$ABVBZ$ABB%Bs A"z.combine must be one of ('all', 'exp', 'base').r8)(.." #u )DAqDy==.AJJDs::,q!"4"#%%HQK9!1'')!,5 Q !+$LL.  q( # )((.." # ]] )DAq qxxaR8^X MM-||q}} !  Q7 $LL!,  q( X~  ) '(  )(0~~'7=tq!1QF== !  .4 ,* DAq1:DAqH}&qkAo  tqyQqT[[ Q   ' ( w -; @D8#KAxDD--*#B#BxHC(*hsmc.A"$ RIIsHSM23IIcN #a58RU1X-D"1c"g.? eAh1a07!"4~ ?"'s2w*A$RUOtBqE!H}>8})7h77 s}}V/DEDHEF19DL@s}}Q/?@@DchDAq KKHLL.x1~=> ?w; @|") $IFQAJq#.QUUN155+<+< 21IAJ OOQF # $H >8}%%%$))gx~~?O(Ptq!Q(PPR e 99Widii&9: :9499g.?01 1 F II %D""T%5%5%7 89t$  %s8}% 5AA;DAqE!2B2B2DEEY^bcblbl>>4>8LE5AEE!e155&8"1e}e4  5D! DAqAJxxQ]]all O?A!A !HOOA   t$B )A!HE5zQ 8$499e, 'B~~ 2** b)   2 's8q=CA cMM#), "NC#"E}} $ //11<tF<)Qj0<Gs0)t6t  tt t 4t  t!t&c$ ddlm}|r fd g}|jttD]M}t |j ttfs$ |j\}}|dus;|j||fO|r|j|}||\}}t|d|j|S|r.t|\}}ttt|d|St|} | jtt 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)posifyct|ttfs|jt||dfS|j|j|zS)NF)evaluate)rorrr}rc)rNrO_denests r2rzpowdenest.._denestOsBa#s,}}c!Q&???1661557+ +r4F)r(polarT)exponents_onlyc>|jxst|tSr8)rsror)rQs r2zpowdenest..dsahh&D*Q2Dr4)filter)sympy.simplify.simplifyratomsrrrorcrlrVsubs powdenestxreplacerrr+rru) eqr(rrrepsrJokdprepnewrs @r2rrsv/  ,#s# )A!&&3*- !&&)BU?KKB(  ) B":D56??EE 2,C)Jr$$GH#NN "+C << DF GGr4yc ddlm}|j\}}|jst |t r-|dk7r(|j |}||}|j\}}|tjur|jrg}g}|jD]J}tdtj|Dr|j|:|j|L|t|}t!t |t|S|j\}} | tj"ur5|js)|j$r|j&dk7s|j(s|Sgg} } tj|D]?} | j*r | j| j/| j| At-| dk(rB| ddjs0t!| dd| dd|zt/t| |zzS| r?t| D cgc]\} } t/| | |zzc} } t/t| |zzS|j0rOt3t5|}|jr/|j\}}||z}|jd}t!||S|jr%tdtj|Dr|Sd}t3t5|}|j6rV|j}t9||}|dk7r9|j;\}}t=||t|Dcgc]}||z  c}z}t |t4s |js|jdjst |jdt rVt?|jd}tA|j dkdk(r#t!|jB|j |zS|Sg}g}|jD]1}|j6r|j|!|j|3t!t |t||t|zScc} } wcc}w)zr Denest powers. This is a helper function for powdenest that performs the actual transformation. r) logcombiner5c3<K|]}t|tywr8)ror)r:rhs r2r<z_denest_pow..~sC2:b#&Csc34K|]}|jywr8)rp)r:ss r2r<z_denest_pow..s?199?r=c||fDcgc]}|jc}\}}t|d|djd}t|dj dd|dj ddz}t ||Scc}w)Nrr5T)cset)rKr!rrargs_cncr)aarrarNrPgs r2nc_gcdz_denest_pow..nc_gcds+-r(3Q 31 !adO * * ,Q / !A$--T-*1-! 4 0H0KK M1a  4sB T)"rrrMrsror _eval_powerr Exp1rmrlanyr rUrVrrrGrHrIr}r~rrrFrrrjrrKrrurzrc)rrrNrOrlogsotherr;_rpolars nonpolarsrrlogbrPrcrglogbrlrcgrgrrs r2ruruis3 >> DAqxx:a%!q&mmA ?B??$DAq AFF{qxx&& !BCr1BCC B R  ! #t*%3t9c5k** MMOEAr QUU{AHHMMaccQhMM BIFmmA! ;; MM"..* +   R !  6{aq ! 3 36!9Q<1a03 ?A;M1NNN 6BxBYrBqDz*BC sI) *+ + ||#a&! ;;iiGAt FA99Q?? ! s1v E ||zz 64  6^^%FBBs$,?QQqS,?'@$@AE%U\\ ::a=  :ejjmS#A 1 .EEII"t+5::uyy{33  C E ZZ 88 JJqM LLO  s:c3i()1S%[= 99mCH-@s Q, > Q2N)FF)1 collectionsr functoolsrmathrsympy.core.functionrrr sympy.corer r r r r rrrrsympy.core.sortingrrsympy.core.numbersrrrsympy.core.mulrsympy.core.rulesrsympy.functionsrrrrrr"sympy.matrices.expressions.matexprr sympy.polysr r!sympy.ntheory.factor_r"r+rrqrur?r4r2rsg#CCXXX8>>&&KK; .e5)\K~sGj 3Ze:r4