K iddlmZddlmZmZmZmZmZddlm Z m Z ddl m Z ddl mZmZddlmZmZmZddlmZmZmZddlmZmZmZdd lmZdd lmZm Z dd l!m"Z"m#Z#m$Z$dd l%m&Z&m'Z'm(Z(dd l)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0m1Z1d#dZ2dZ3d$dZ4dZ5dddZ6d%dZ7dZ8d&dZ9d&dZ:d&dZ;dZe=Z?e>Z@e|d|}tA||jBSDcgc]}tE|dc}d}|r4|j7}|"|jFrd}n|jI}tj|Dcgc]}tE|d}}tKt,tLjN}}|D]+}|jQd\}}t-t/||z} | Dcgc] }/| }!}d}"D]}#t|#tRr|"rt-tU|!}!|" }"||!|#}$|$8|#jVs tYd|$\}!}%}&}'|'sFg}(|%D]6})|)d|)d}*n |)d|)dz}*|(j[tA|)d |*8t]|(}+n||%}+tE||!d}!tE|+d}+||+j[|!&||z }.|jD cic]\} }| t|}} }|tLjNur||tLj^<| |jD] \},}-|-|z||,<|)|jD,-cic]\},}-|,||-}},}-|r,t|jD,-cgc] \},}-|,|-z c}-},S|Scc}wcc}wcc} wcc}} wcc}} wcc}wcc}wcc}wcc}wcc}wcc}} wcc}-},wcc}-},w)a Collect additive terms of an expression. Explanation =========== This function collects additive terms of an expression with respect to a list of expression up to powers with rational exponents. By the term symbol here are meant arbitrary expressions, which can contain powers, products, sums etc. In other words symbol is a pattern which will be searched for in the expression's terms. The input expression is not expanded by :func:`collect`, so user is expected to provide an expression in an appropriate form. This makes :func:`collect` more predictable as there is no magic happening behind the scenes. However, it is important to note, that powers of products are converted to products of powers using the :func:`~.expand_power_base` function. There are two possible types of output. First, if ``evaluate`` flag is set, this function will return an expression with collected terms or else it will return a dictionary with expressions up to rational powers as keys and collected coefficients as values. Examples ======== >>> from sympy import S, collect, expand, factor, Wild >>> from sympy.abc import a, b, c, x, y This function can collect symbolic coefficients in polynomials or rational expressions. It will manage to find all integer or rational powers of collection variable:: >>> collect(a*x**2 + b*x**2 + a*x - b*x + c, x) c + x**2*(a + b) + x*(a - b) The same result can be achieved in dictionary form:: >>> d = collect(a*x**2 + b*x**2 + a*x - b*x + c, x, evaluate=False) >>> d[x**2] a + b >>> d[x] a - b >>> d[S.One] c You can also work with multivariate polynomials. However, remember that this function is greedy so it will care only about a single symbol at time, in specification order:: >>> collect(x**2 + y*x**2 + x*y + y + a*y, [x, y]) x**2*(y + 1) + x*y + y*(a + 1) Also more complicated expressions can be used as patterns:: >>> from sympy import sin, log >>> collect(a*sin(2*x) + b*sin(2*x), sin(2*x)) (a + b)*sin(2*x) >>> collect(a*x*log(x) + b*(x*log(x)), x*log(x)) x*(a + b)*log(x) You can use wildcards in the pattern:: >>> w = Wild('w1') >>> collect(a*x**y - b*x**y, w**y) x**y*(a - b) It is also possible to work with symbolic powers, although it has more complicated behavior, because in this case power's base and symbolic part of the exponent are treated as a single symbol:: >>> collect(a*x**c + b*x**c, x) a*x**c + b*x**c >>> collect(a*x**c + b*x**c, x**c) x**c*(a + b) However if you incorporate rationals to the exponents, then you will get well known behavior:: >>> collect(a*x**(2*c) + b*x**(2*c), x**c) x**(2*c)*(a + b) Note also that all previously stated facts about :func:`collect` function apply to the exponential function, so you can get:: >>> from sympy import exp >>> collect(a*exp(2*x) + b*exp(2*x), exp(x)) (a + b)*exp(2*x) If you are interested only in collecting specific powers of some symbols then set ``exact`` flag to True:: >>> collect(a*x**7 + b*x**7, x, exact=True) a*x**7 + b*x**7 >>> collect(a*x**7 + b*x**7, x**7, exact=True) x**7*(a + b) If you want to collect on any object containing symbols, set ``exact`` to None: >>> collect(x*exp(x) + sin(x)*y + sin(x)*2 + 3*x, x, exact=None) x*exp(x) + 3*x + (y + 2)*sin(x) >>> collect(a*x*y + x*y + b*x + x, [x, y], exact=None) x*y*(a + 1) + x*(b + 1) You can also apply this function to differential equations, where derivatives of arbitrary order can be collected. Note that if you collect with respect to a function or a derivative of a function, all derivatives of that function will also be collected. Use ``exact=True`` to prevent this from happening:: >>> from sympy import Derivative as D, collect, Function >>> f = Function('f') (x) >>> collect(a*D(f,x) + b*D(f,x), D(f,x)) (a + b)*Derivative(f(x), x) >>> collect(a*D(D(f,x),x) + b*D(D(f,x),x), f) (a + b)*Derivative(f(x), (x, 2)) >>> collect(a*D(D(f,x),x) + b*D(D(f,x),x), D(f,x), exact=True) a*Derivative(f(x), (x, 2)) + b*Derivative(f(x), (x, 2)) >>> collect(a*D(f,x) + b*D(f,x) + a*f + b*f, f) (a + b)*f(x) + (a + b)*Derivative(f(x), x) Or you can even match both derivative order and exponent at the same time:: >>> collect(a*D(D(f,x),x)**2 + b*D(D(f,x),x)**2, D(f,x)) (a + b)*Derivative(f(x), (x, 2))**2 Finally, you can apply a function to each of the collected coefficients. For example you can factorize symbolic coefficients of polynomial:: >>> f = expand((x + a + 1)**3) >>> collect(f, x, factor) x**3 + 3*x**2*(a + 1) + 3*x*(a + 1)**2 + (a + 1)**3 .. note:: Arguments are expected to be in expanded form, so you might have to call :func:`~.expand` prior to calling this function. See Also ======== collect_const, collect_sqrt, rcollect cx|jxs-| jxst|jtSN) is_Symbolboolatomsrxs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/simplify/radsimp.pyzcollect..s.Q[[aRNNd  7T)binary)funcevaluateexactdistribute_order_termNc3VK|] }|jxr|jv"ywr')is_Powbase).0isymss r- zcollect..s%@QQXX0!&&D.0@s&)Fc4g}|D]\}}}}|!|\}}t|D]}t||}|@|tjur|j |Q|j t ||m|j t |||zt |Sr')rangerrOneappendrr) termsproducttermratsymderivvarorder_s r-make_expressionz collect..make_expressions%* 3 !D#sE " Uu1A%dC0D1{!%%<NN4(NN3tS>2s4S12 3G}r/c|j|jdd}}}|jddD]}||k(r|dz }tdt|trt|jdt fd|jDr td|k(r%|j|t |jz}}nnt|trt||t|ffS)Nrr5z(Improve MV Derivative support in collectc3(K|] }|k7 ywr')r9ss0s r-r<z4collect..parse_derivative..s3q173s)expr variablesNotImplementedError isinstanceranylenr)rFrPrErHrNrOs @r-parse_derivativez!collect..parse_derivatives!::uq'915c$ @ACx )>@@  @z*"B3DNN33)>@@Sy"iiT^^1D)Dez*c8E?+++r/ctjd}}|d}}|jrt|jt r|j\}}n |j}|jtj k(r\|j}|jrtj |}}n|jr|jd\}}t||}}n|jjr |j}n|jj\}}|jr||}}n|j}nt|trZ|j}|jrtj |}}nJ|jr>|jd\}}t||}}nt|t r |\}}||||fS)aParses expression expr and outputs tuple (sexpr, rat_expo, sym_expo, deriv) where: - sexpr is the base expression - rat_expo is the rational exponent that sexpr is raised to - sym_expo is the symbolic exponent that sexpr is raised to - deriv contains the derivatives of the expression For example, the output of x would be (x, 1, None, None) the output of 2**x would be (2, 1, x, None). NT)rational) rr?r7rSr8rExp1r is_Rationalis_Mul as_coeff_Mul is_Number) rPrat_exposym_exposexprrFargcoefftailrVs r- parse_termzcollect..parse_termsaUUD(Tu ;;$))Z0/ : u yyAFF"hh??&'ffc8EZZ"%"2"2D"2"AKE4&)$i8E##88"hh335 t??).hH#xxH c "((C"#&&#x!...= t"%d)Ux j )+D1LE5h%//r/cPtj|}t|t|kry|Dcgc] }| }}|dd}gdd}}}|D]\}}}}|jr|dk(r|t t|D]} ||  || \} } } } | d}| j |)| |k(s| 1|4| j |Fdur| |z }||}n||k7rd}n || k7s|| k7rg|j || d|| <y|Dcgc]}|s| c}|||fScc}wcc}w)zParse terms searching for a pattern. Terms is a list of tuples as returned by parse_terms; Pattern is an expression treated as a product of factors. NFr5T)r make_argsrUr]r>matchr@)rApatternelemelems common_expo has_derive_rate_syme_ordjrCt_ratt_symt_ordexpo_fr3rds r-parse_expressionz!collect..parse_expression6s --( u:G $4;>eqjU]s5z*- AQx' 05a-D% ($(  4(4"e^u/@!-!KK.: E>$)5=D*2.2 $/$#623K %~% ( U1X.#'aS- Z g3 j"'-2"B-uk9L Lu=t.sD D#D#r)deep)split_1z%Can not collect noncommutative symbolrM)0rr#r$dictziprr getcollectsubsitemsrSxreplacesetr rfhas_freer[add _new_rawargs as_coeff_mulalllistrrr2is_AddgetOr1argsr7r8rrrhasremoveOrrZeroargs_cncrreversedis_commutativeAttributeErrorr@rr?)0rPr;r1r2r3r4r:condrInonsymsrepsrNrvkvurep_symsgsimplerJrvoarCb order_termsumma collecteddislikedrBcncrrA small_firstsymbolresultrjrkrlmargsrieindexkeyvalrVrds0 ` ` @@r-r}r}s l 4=D)1$dV E1GAJ ED DdD.JAwC7!Ka%"9+a."9!KLM(,-1A- TYYt_d"79"&.A1.."d#;;t$ $!# ,1HHQN++D11::d3CC, , }t$ !A1::t$T 88  ! #ANNNANND$9!$<=D=IIaL !@%@@d75>**4"79 9$--&,400dFMP ;;  qA499!YY2!q&AtT48MN23567D[[499 II'dD$7LM'( ([[ 4tU4IKAq$((# #6: ; ae , ;DJYY[  !z~~t$! ||~7:}}T7J K! qu - KE K%d+QVVxI%    /2GAJ"$(,-1A--  F&*-+Xe_-"-o %eV4F!,,()PQQ7=4uk9!E %67? $QA $QQA Sa!_5 6  KE+E2E)/%*@uM)%e<% ''.9 >  HK% N)2(9:1CG:I:qvv# !%%!) .HC :-IcN . +4??+<>'sCCcN> >Y__->?cSW?@@O F"L-/,H2' < L .H;>@sMWWW  W;WWW#,W(W-=W2 W7=W=-X c |js|j|s|S|j|jDcgc]}t |g|c}}|j r t ||S|Scc}w)aQ Recursively collect sums in an expression. Examples ======== >>> from sympy.simplify import rcollect >>> from sympy.abc import x, y >>> expr = (x**2*y + x*y + x + y)/(x + y) >>> rcollect(expr, y) (x + y*(x**2 + x + 1))/(x + y) See Also ======== collect, collect_const, collect_sqrt )is_Atomr __class__rrcollectrr})rPvarsras r-rrsd( ||8488T? t~~ J 4t 4JK ;;4& &K KsA)ct|tj}|j\}}t}t j |D]}|j dD]n}|js|jr/|jjr|jjdk(s|tjus^|j|pt|g|ddi}||k7}|sd}t!t#t j |} t%| D]\} }|j \} } | D]V} | jr/| jjr| jjdk(s| tjusQ|dz }n| | xx|zcc<|s |s t | g} t'| |fS||zS)aLReturn expr with terms having common square roots collected together. If ``evaluate`` is False a count indicating the number of sqrt-containing terms will be returned and, if non-zero, the terms of the Add will be returned, else the expression itself will be returned as a single term. If ``evaluate`` is True, the expression with any collected terms will be returned. Note: since I = sqrt(-1), it is collected, too. Examples ======== >>> from sympy import sqrt >>> from sympy.simplify.radsimp import collect_sqrt >>> from sympy.abc import a, b >>> r2, r3, r5 = [sqrt(i) for i in [2, 3, 5]] >>> collect_sqrt(a*r2 + b*r2) sqrt(2)*(a + b) >>> collect_sqrt(a*r2 + b*r2 + a*r3 + b*r3) sqrt(2)*(a + b) + sqrt(3)*(a + b) >>> collect_sqrt(a*r2 + b*r2 + a*r3 + b*r5) sqrt(3)*a + sqrt(5)*b + sqrt(2)*(a + b) If evaluate is False then the arguments will be sorted and returned as a list and a count of the number of sqrt-containing terms will be returned: >>> collect_sqrt(a*r2 + b*r2 + a*r3 + b*r5, evaluate=False) ((sqrt(3)*a, sqrt(5)*b, sqrt(2)*(a + b)), 3) >>> collect_sqrt(a*sqrt(2) + b, evaluate=False) ((b, sqrt(2)*a), 1) >>> collect_sqrt(a + b, evaluate=False) ((a + b,), 0) See Also ======== collect, collect_const, rcollect rryNumbersFr5)rr2as_content_primitiverr rfr is_numberr7rrZqr ImaginaryUnitr collect_constrr enumeratetuple)rPr2rbrrmdhitnradrr:rrcis r- collect_sqrtrsR$--++-KE4 5D ]]4 a A{{HH!2!2quuww!|(   d1T151A !)C GCMM!,-.dO DAqJJLEAr 99!3!3A aoo-AID   Gu G tJ>> from sympy.simplify.radsimp import collect_abs >>> from sympy.abc import x >>> collect_abs(abs(x + 1)/abs(x**2 - 1)) Abs((x + 1)/(x**2 - 1)) >>> collect_abs(abs(1/x)) Abs(1/x) cD|j\}}g}g}|D]}t|tr|j|jd2t|t rft|j trL|jjr6|j|j jd|jz|j|t|dkrtd|Ds|St|}t|}|g}|j||jts|j|t|St|ts t|d}||d<t||j|tj|| S)Nrryc3jK|]+}t|ts|jj-ywr')rSrr is_negative)r9r:s r-r<z,collect_abs.._abs..^s$!U*QPSBT!%%"3"3!Us33Fr2)r)rrSr r@rrr8ris_realrUrTrextendrr _from_args) mulrrrrr:absargArs r-_abszcollect_abs.._absSs< 2   A!S!#As# 1663(?AEEMMQ./   q6A:c!UQ!UUJa Ks AuuSz KKO: !S!FU+AQ B~~dr6::r/c"t|tSr')rSrr+s r-r.zcollect_abs..ps *Q$r/c|Sr'rMr,rs r-r.zcollect_abs..qs $q'r/c"t|tSr')rSrr+s r-r.zcollect_abs..rs jC(r/c|Sr'rMrs r-r.zcollect_abs..ss d1gr/)replace)rPrs @r- collect_absrEs1;8 <<$ "7 ( r/)rc |js|Sd}|sVd}t}|jD]:}tj|D] }|j s|j |"<n t|}|s|Dcgc]}|jr|}}tt|}|D]}tt}t|}tj|D]}t|} | j|\} } | jr^| j j#| j t%fdDs#||j'| j)|t*j,j'|g} d} d}t|D]}||}|t*j,ur| j/|,t1|dkDr#t|}d} |r||k7r|j'|n|d}|r8|jr,|jr | j't3||dd}| j'||z| s|r t5| }nt| }|jr|S|Scc}w)aA non-greedy collection of terms with similar number coefficients in an Add expr. If ``vars`` is given then only those constants will be targeted. Although any Number can also be targeted, if this is not desired set ``Numbers=False`` and no Float or Rational will be collected. Parameters ========== expr : SymPy expression This parameter defines the expression the expression from which terms with similar coefficients are to be collected. A non-Add expression is returned as it is. vars : variable length collection of Numbers, optional Specifies the constants to target for collection. Can be multiple in number. Numbers : bool Specifies to target all instance of :class:`sympy.core.numbers.Number` class. If ``Numbers=False``, then no Float or Rational will be collected. Returns ======= expr : Expr Returns an expression with similar coefficient terms collected. Examples ======== >>> from sympy import sqrt >>> from sympy.abc import s, x, y, z >>> from sympy.simplify.radsimp import collect_const >>> collect_const(sqrt(3) + sqrt(3)*(1 + sqrt(2))) sqrt(3)*(sqrt(2) + 2) >>> collect_const(sqrt(3)*s + sqrt(7)*s + sqrt(3) + sqrt(7)) (sqrt(3) + sqrt(7))*(s + 1) >>> s = sqrt(2) + 2 >>> collect_const(sqrt(3)*s + sqrt(3) + sqrt(7)*s + sqrt(7)) (sqrt(2) + 3)*(sqrt(3) + sqrt(7)) >>> collect_const(sqrt(3)*s + sqrt(3) + sqrt(7)*s + sqrt(7), sqrt(3)) sqrt(7) + sqrt(3)*(sqrt(2) + 3) + sqrt(7)*(sqrt(2) + 2) The collection is sign-sensitive, giving higher precedence to the unsigned values: >>> collect_const(x - y - z) x - (y + z) >>> collect_const(-y - z) -(y + z) >>> collect_const(2*x - 2*y - 2*z, 2) 2*(x - y - z) >>> collect_const(2*x - 2*y - 2*z, -2) 2*x - 2*(y + z) See Also ======== collect, collect_sqrt, rcollect FTc3lK|]+}|vxr!|jxr|j -ywr') is_Integer)r9rfnowfwass r-r<z collect_const..sK:/09+a););+Q**A++:s14r5r)sign)rrrrrfrrrr]rrrr r divis_onefactorscopyrTr@as_exprrr?rrUrr )rPrrrecurserrrrAFvfrrrrunevalrrrs @@r-rrvsB| ;; G u A]]1% ;;HHQK  t} 3aq{{33  D 3D! QZt$ #A A559DAqxx yy~~'yy:48::!HOOAIIK0 !%%L   " #  !AaAAEEz A1vzGqDyKKNaD 1;;188 K1489 AaC + !. '.Dz;; Kk3j Kq4s <I;I;c pddlmddlm}t dfdd fd fdt |s3|j |jDcgc]}t| c}S|j\}}|j}t|}t|\}}|||fk7rY|js||f} ||d }||d }tt|d |z } t| jj!D cgc] \} } | | z c} } } t| \}}|||fk(r| \}}t#|}|j$s |j&rtt)t|d |z \} }|j$s!|j+|j+krY| |fDcgc] }|| c}\}}|j,r2|jdj$r|j |j}|t|d |z zScc}wcc} } wcc}w) a Rationalize the denominator by removing square roots. Explanation =========== The expression returned from radsimp must be used with caution since if the denominator contains symbols, it will be possible to make substitutions that violate the assumptions of the simplification process: that for a denominator matching a + b*sqrt(c), a != +/-b*sqrt(c). (If there are no symbols, this assumptions is made valid by collecting terms of sqrt(c) so the match variable ``a`` does not contain ``sqrt(c)``.) If you do not want the simplification to occur for symbolic denominators, set ``symbolic`` to False. If there are more than ``max_terms`` radical terms then the expression is returned unchanged. Examples ======== >>> from sympy import radsimp, sqrt, Symbol, pprint >>> from sympy import factor_terms, fraction, signsimp >>> from sympy.simplify.radsimp import collect_sqrt >>> from sympy.abc import a, b, c >>> radsimp(1/(2 + sqrt(2))) (2 - sqrt(2))/2 >>> x,y = map(Symbol, 'xy') >>> e = ((2 + 2*sqrt(2))*x + (2 + sqrt(8))*y)/(2 + sqrt(2)) >>> radsimp(e) sqrt(2)*(x + y) No simplification beyond removal of the gcd is done. One might want to polish the result a little, however, by collecting square root terms: >>> r2 = sqrt(2) >>> r5 = sqrt(5) >>> ans = radsimp(1/(y*r2 + x*r2 + a*r5 + b*r5)); pprint(ans) ___ ___ ___ ___ \/ 5 *a + \/ 5 *b - \/ 2 *x - \/ 2 *y ------------------------------------------ 2 2 2 2 5*a + 10*a*b + 5*b - 2*x - 4*x*y - 2*y >>> n, d = fraction(ans) >>> pprint(factor_terms(signsimp(collect_sqrt(n))/d, radical=True)) ___ ___ \/ 5 *(a + b) - \/ 2 *(x + y) ------------------------------------------ 2 2 2 2 5*a + 10*a*b + 5*b - 2*x - 4*x*y - 2*y If radicals in the denominator cannot be removed or there is no denominator, the original expression will be returned. >>> radsimp(sqrt(2)*x + sqrt(2)) sqrt(2)*x + sqrt(2) Results with symbols will not always be valid for all substitutions: >>> eq = 1/(a + b*sqrt(c)) >>> eq.subs(a, b*sqrt(c)) 1/(2*b*sqrt(c)) >>> radsimp(eq).subs(a, b*sqrt(c)) nan If ``symbolic=False``, symbolic denominators will not be transformed (but numeric denominators will still be processed): >>> radsimp(eq, symbolic=False) 1/(a + b*sqrt(c)) r)Expr)signsimpza:d A:Dc \}}}}}}}}t|dk(rettt||||g|D cgc] } | D]} |  c} } } t ||zt ||zz j | St|dk(rttt||||||g|D cgc] } | D]} |  c} } } t ||zt ||zzt ||zz dt |zt |z|z|z||dzzz ||dzzz ||dzzzzj | St|dk(rttt||||||||g|D cgc] } | D]} |  c} } } t ||zt ||zzt ||zz t ||zz dt |zt |z|z|z||dzzz ||dzzz dt |zt |z|z|zz ||dzzz||dzzzzdt |zt |zt |zt |z|z|z|z|z|dz|dzzzd|z|z|dzz|dzzz d|z|z|dzz|dzzz d|z|z|dzz|dzzz |dz|dzzzd|z|z|dzz|dzzz d|z|z|dzz|dzzz |dz|dzzzd|z|z|dzz|dzzz |dz|dzzzzj | St|dk(rt |ddSt cc} } wcc} } wcc} } w)Nryir5r)rUrzrr{rrrR) rtermsrrrrrBCDr:rprr;s r-_numzradsimp.._numNs"&1aAq!Q v;! S!Q16/Ma1/Ma/M/MNOPD GAIQ !88D> 2 v;! S!Q1a!35SAQR5SAa5Sa5STUVD !WQYa "T!WQY .47471B11DQ1F1a41O adF2q!tV2 &htn -[A S!Q1aAq!9v;Y!WX;YRSA;YA;YZ[\D!WQYa*T!WQY6aBQtAwYtTUwEVWXEXYZEZAqD&FQT6F"$%d1gId1g$5a$7$9F:<=adFFC!Q$FDGDG+DG3DG;A=a?A!Cad1a4iO!Aad 1a4 "#A#a%1*QT/245aCE!Q$Jq!tODFGd1a4iP!Aad 1a4 "#A#a%1*QT/245qDAI>@A!Aad 1a4P1QT  %HTN  + [A q ! % %% %'0N6T .ispow2hsxx EE ==QSSAXeAh!m A}}CC!H<<AAv#a)..r/c > ddlm}|jr|St|s-|j|j Dcgc] }| c}St |\}}|jr|jr|S|jsB|j|j Dcgc] }| c}}t|d|z S|tjurt|d|z S|jr)t|j Dcgc] }d|z c}Ss|jr|S|rDtt|jt|j z}||k7red|z S|j"rN|j j$s|jj&r"d|jz |j zS|j(s8|s0d|j|j Dcgc] }| c}z Sd}t+|}|jrd|z S|j,r*||}|j.r|j1|rd|z S t3t4}t7j8|D]} g} g} t;j8| D]} | drT| j=| j tj>ur | jn| jd| j zza| tj@ur | j=tjB| j=| |tEtG| j=t;| t5tGt5|jI} | D cgc]\} }t;| t7|f} } }tK| | ddtjurdndz }|dkrn|kDrd}ntK| dkDrjtMd | DrUtOtjt7jP| Dcgc]\}}t||zc}}\}}||z}nd}nmdd l)m*}m+}|| }||z}||z}|t+| }|jYtjZt\t^r|S|jrn||s|St|d|z Scc}wcc}wcc}wcc}wcc}} wcc}}w) Nr) nsimplifyr5T)rryFrc3\K|]$\}}|jxr|dzj&yw)ryN)rrZ)r9r,ys r-r<z*radsimp..handle..s*Ntq!q||:A(:(::Ns*,)powsimp powdenest)force)0sympy.simplify.simplifyrrrSr1rfractionrrr?r[ free_symbolsr"rr8numerrr7 is_integer is_positiverrrr]equalsrrr rfrr@Halfr NegativeOnerrrrUrrad_rationalizersympy.simplify.powsimprrrrrr)rPrrnrd2keep_drrp2otherr:rrprr,rndrrnumrrhandler max_termsrs r-rzradsimp..handlezsB 6 <<KD$'499$))>/56tq!a6489EBGA!D A$v,'C HA HA(1+X6AuuQVVS#& yyadK1Q3''G=4 &C9F=$7s#S?T T T 4TT)rrrr5F)sympy.core.exprrrrrrSr1rradsimp as_coeff_Addnormalrrr rrrrr]rr count_opsr[)rPrrrrrboldrrwasurrn2rr:rrrrr;s `` @@@@@r-rrsX%0 9 D&4$l(l(\ dD !tyyW[W`W`aRS71x9Mabb##%KE4 ;;=D 4.C F4L !DAq q!f}yya&CU+AU+A(AaC01A AIIOO4E"FDAq1a4"FGAA;DAqq!f}1 qM ;;!((i(8AaC(@ABFB|| !++- ?.0"X6 6188q 3 3A #Aqs+ ++1b#G7sH(H- H3c|js||fSt|\}}}|t|z}t||z |z}t|dz|dzz }t ||S)a\ Rationalize ``num/den`` by removing square roots in the denominator; num and den are sum of terms whose squares are positive rationals. Examples ======== >>> from sympy import sqrt >>> from sympy.simplify.radsimp import rad_rationalize >>> rad_rationalize(sqrt(3), 1 + sqrt(2)/3) (-sqrt(3) + sqrt(6)/3, -7/9) ry)r split_surdsrrr)r denrrrs r-rrsf ::Cx#GAq! $q' A AE3; C 1a4!Q$; C 3 $$r/ct|}gg}}tj|D]}|jr9|jst |t r|j\}}|jr|tjur|j|q|r?|jr|jt|| |j||jt|| |jr|j||sH|jr<|j!\}}|dk7r|j||j|7|j|J|j"rS|j$sG|j&dk7r|j|j&|j|j(|j|t|d| it|d| ifS)aReturns a pair with expression's numerator and denominator. If the given expression is not a fraction then this function will return the tuple (expr, 1). This function will not make any attempt to simplify nested fractions or to do any term rewriting at all. If only one of the numerator/denominator pair is needed then use numer(expr) or denom(expr) functions respectively. >>> from sympy import fraction, Rational, Symbol >>> from sympy.abc import x, y >>> fraction(x/y) (x, y) >>> fraction(x) (x, 1) >>> fraction(1/y**2) (1, y**2) >>> fraction(x*y/2) (x*y, 2) >>> fraction(Rational(1, 2)) (1, 2) This function will also work fine with assumptions: >>> k = Symbol('k', negative=True) >>> fraction(x * y**k) (x, y**(-k)) If we know nothing about sign of some exponent and ``exact`` flag is unset, then the exponent's structure will be analyzed and pretty fraction will be returned: >>> from sympy import exp, Mul >>> fraction(2*x**(-y)) (2, x**y) >>> fraction(exp(-x)) (1, exp(x)) >>> fraction(exp(-x), exact=True) (exp(-x), 1) The ``exact`` flag will also keep any unevaluated Muls from being evaluated: >>> u = Mul(2, x + 1, evaluate=False) >>> fraction(u) (2*x + 2, 1) >>> fraction(u, exact=True) (2*(x + 1), 1) r5r2)rrrfrr7rSr as_base_exprrrr@ is_constantrrr[as_numer_denomrZrpr) rPr3rrrCrexrrs r-rrszp 4=Dr5E d#   DKK:dC3H$$&EAr~~&LLO~~' SRC[1 T*LLQ- T"ryy**,16LLO Q T"   doovv{ TVV$ LL LL 78  *E *C,KU,K KKr/c"t||dS)Nr3rrrPr3s r-rru D &q ))r/c"t||dS)Nr$r5r%r&s r-rryr'r/c *|jdddi|S)NfracTrM)expand)rPhintss r-fraction_expandr-}s 4;; *D *E **r/c pt||jdd\}}|jdddi||z S)Nr3Fr$rTrMrr|r+rPr,rrs r- numer_expandr1s; D '5 9 :DAq 188 ($ (% (1 ,,r/c pt||jdd\}}||jdddi|z S)Nr3Fr$rTrMr/r0s r- denom_expandr3s; D '5 9 :DAq xqxx,d,e, ,,r/ct|jt}|Dcgc]}|j}}|Dcgc]}|djs|ddz}}|j tt |\}}}|}|sFt|dk\r8|Dcgc]}||z  } }| Dcgc] }|dk7s | } }t | \} } }|| z}gg} } |D]\} }|jrc|jtjk(rF|j}||vr!| j| t||z z`| j| |zu| j| |zt| }t| }|||fScc}wcc}wcc}wcc}w)a Split an expression with terms whose squares are positive rationals into a sum of terms whose surds squared have gcd equal to g and a sum of terms with surds squared prime with g. Examples ======== >>> from sympy import sqrt >>> from sympy.simplify.radsimp import split_surds >>> split_surds(3*sqrt(3) + sqrt(5)/7 + sqrt(6) + sqrt(10) + sqrt(15)) (3, sqrt(2) + sqrt(5) + 3, sqrt(5)/7 + sqrt(10)) )rr5ry)sortedrrr\r7sort _split_gcdrUrrrr8r@rr )rPrr, coeff_mulssurdsrb1b2g2b1ng1a1va2vrrNs1rrs r-rrst $))!1 2D,01q!.."1J1( 8AaDKKQqT1W 8E 8 JJ#J$E"IAr2 B #b'Q,qqs(Qaq(( #& C rT2C1 88BRx 1T"R%[=) 1Q3 JJqsO S A S A q!8O/2 8  (s"E0E5 E5 E:( E?3E?c|d}|g}g}|ddD]8}t||}|dk(r|j|&|}|j|:|||fS)a` Split the list of integers ``a`` into a list of integers, ``a1`` having ``g = gcd(a1)``, and a list ``a2`` whose elements are not divisible by ``g``. Returns ``g, a1, a2``. Examples ======== >>> from sympy.simplify.radsimp import _split_gcd >>> _split_gcd(55, 35, 22, 14, 77, 10) (5, [55, 35, 10], [22, 14, 77]) rr5N)r!r@)rrr:r;r,r>s r-r7r7si !A B B qrU AY 7 IIaLA IIaL  b"9r/)NNFTr')Trr)D collectionsr sympy.corerrrrrsympy.core.addr r sympy.core.assumptionsr sympy.core.exprtoolsr r sympy.core.functionrrrsympy.core.mulrrrsympy.core.numbersrrrsympy.core.parametersrsympy.core.sortingrrsympy.core.symbolrrrsympy.functionsrrr$sympy.functions.elementary.complexesr sympy.polysr!sympy.simplify.sqrtdenestr"sympy.utilities.iterablesr#r$r}rrrrrrrrrr-r1r3 expand_numer expand_denomexpand_fractionrr7rMr/r-rVs#770.3GGBB113822**404 ~B>L^.b(,EPC,L%,XLv**+- -   !&Rr/