K i0dZddlmZmZmZddlmZmZddlm Z m Z m Z m Z m Z mZddlmZddlmZmZmZmZddlmZmZmZmZmZmZGdd ZGd d ZGd d ZddZdZ GddZ!GddZ"y)a  The classes used here are for the internal use of assumptions system only and should not be used anywhere else as these do not possess the signatures common to SymPy objects. For general use of logic constructs please refer to sympy.logic classes And, Or, Not, etc. ) combinationsproduct zip_longest)AppliedPredicate Predicate)EqNeGtLtGeLe)S)OrAndNotXnor) EquivalentITEImpliesNandNorXorcVeZdZdZd fd ZedZdZdZdZ e Z dZ dZ xZ S) Literala{ The smallest element of a CNF object. Parameters ========== lit : Boolean expression is_Not : bool Examples ======== >>> from sympy import Q >>> from sympy.assumptions.cnf import Literal >>> from sympy.abc import x >>> Literal(Q.even(x)) Literal(Q.even(x), False) >>> Literal(~Q.even(x)) Literal(Q.even(x), True) ct|tr|jd}d}n"t|ttt fr|r|S|St ||}||_||_ |S)NrT) isinstancerargsANDORrsuper__new__litis_Not)clsr"r#obj __class__s [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/assumptions/cnf.pyr!zLiteral.__new__&s` c3 ((1+CF c2w/ 0!C4 *s *goc"  c|jSN)r"selfs r'argz Literal.arg1s xxr(ct|jr|j|}n|jj|}t|||jSr*)callabler"applytyper#)r,exprr"s r'rcallz Literal.rcall5sD DHH ((4.C((..&CtDz#t{{++r(cH|j }t|j|Sr*)r#rr")r,r#s r' __invert__zLiteral.__invert__<s[[txx((r(cvdjt|j|j|jS)Nz {}({}, {}))formatr1__name__r"r#r+s r'__str__zLiteral.__str__@s)""4:#6#6$++NNr(cj|j|jk(xr|j|jk(Sr*)r-r#r,others r'__eq__zLiteral.__eq__Es'xx599$D )DDr(cptt|j|j|jf}|Sr*)hashr1r8r-r#)r,hs r'__hash__zLiteral.__hash__Hs* $t*%%txx= >r()F)r8 __module__ __qualname____doc__r!propertyr-r3r5r9__repr__r=rA __classcell__)r&s@r'rrsC, ,)OHEr(rcHeZdZdZdZedZdZdZdZ dZ dZ e Z y ) rz+ A low-level implementation for Or c||_yr*_argsr,rs r'__init__z OR.__init__Q  r(c8t|jtSN)keysortedrKstrr+s r'rzOR.argsTdjjc**r(cvt||jDcgc]}|j|c}Scc}wr*r1rKr3r,r2r-s r'r3zOR.rcallX<tDz'+zz # IIdO 6cNt|jDcgc]}|c}Scc}wr*)rrKr,r-s r'r5z OR.__invert__]s!TZZ0ccT0110 "cltt|jft|jzSr*r?r1r8tuplerr+s r'rAz OR.__hash__`(T$Z((*U499-==>>r(c4|j|jk(Sr*rr;s r'r=z OR.__eq__cyyEJJ&&r(c ddj|jDcgc] }t|c}zdz}|Scc}w)N( | )joinrrTr,r-ss r'r9z OR.__str__fs7 %**$))<3c#h<= = C=; N) r8rBrCrDrMrErr3r5rAr=r9rFr(r'rrMs@++ 2?'Hr(rcHeZdZdZdZdZedZdZdZ dZ dZ e Z y ) rz, A low-level implementation for And c||_yr*rJrLs r'rMz AND.__init__qrNr(cNt|jDcgc]}|c}Scc}wr*)rrKr\s r'r5zAND.__invert__ts!DJJ/SSD/00/r]c8t|jtSrPrRr+s r'rzAND.argswrUr(cvt||jDcgc]}|j|c}Scc}wr*rWrXs r'r3z AND.rcall{rYrZcltt|jft|jzSr*r_r+s r'rAz AND.__hash__rar(c4|j|jk(Sr*rcr;s r'r=z AND.__eq__rdr(c ddj|jDcgc] }t|c}zdz}|Scc}w)Nrf & rhrirks r'r9z AND.__str__s7   :CH:; ;C ?;rmN) r8rBrCrDrMr5rErr3rAr=r9rFrnr(r'rrms@1++ ?'Hr(rNc . ddlm}|i}t|jt|j t |jt|jt|jt|ji}t||vr|t|}||j}t!|t"r|jd}t%||}|St!|t&r3t)t'j*|Dcgc]}t%||c}St!|t,r3t/t-j*|Dcgc]}t%||c}St!|t0r-t/|jDcgc]}t%||c}}|St!|t2r-t)|jDcgc]}t%||c}}|St!|t4rg}t7dt9|jdzdD]h} t;|j| D]M} |jD cgc]} | | vr t%| |n t%| |!} } |j=t)| Ojt/|St!|t>rg}t7dt9|jdzdD]h} t;|j| D]M} |jD cgc]} | | vr t%| |n t%| |!} } |j=t)| Ojt/|St!|t@r?t%|jd|t%|jd|}} t)| |St!|tBrxg}tE|j|jdd|jdD]9\}}t%||}t%||}|j=t)||;t/|St!|tFrlt%|jd|} t%|jd|}t%|jd|}t/t)| |t)| |St!|tHrE|jJ|jL}}|jO|d}|t%|jP||St!|tRr |jO|d}| t%||StU|Scc}wcc}wcc}wcc}wcc} wcc} w)a Generates the Negation Normal Form of any boolean expression in terms of AND, OR, and Literal objects. Examples ======== >>> from sympy import Q, Eq >>> from sympy.assumptions.cnf import to_NNF >>> from sympy.abc import x, y >>> expr = Q.even(x) & ~Q.positive(x) >>> to_NNF(expr) (Literal(Q.even(x), False) & Literal(Q.positive(x), True)) Supported boolean objects are converted to corresponding predicates. >>> to_NNF(Eq(x, y)) Literal(Q.eq(x, y), False) If ``composite_map`` argument is given, ``to_NNF`` decomposes the specified predicate into a combination of primitive predicates. >>> cmap = {Q.nonpositive: Q.negative | Q.zero} >>> to_NNF(Q.nonpositive, cmap) (Literal(Q.negative, False) | Literal(Q.zero, False)) >>> to_NNF(Q.nonpositive(x), cmap) (Literal(Q.negative(x), False) | Literal(Q.zero(x), False)) r)QN) fillvalue)+sympy.assumptions.askryreqr ner gtr ltr ger ler1rrrto_NNFrr make_argsrrrrrrangelenrappendrrrrrrfunction argumentsgetr3rr)r2 composite_mapry binrelpredspredr-tmpxcnfsinegrlclauseLRabMrnewpreds r'rrso:( qttRr144QTT2qttRNK Dz[ 4:&TYY$iilS-(t $bll46HIF1m,IJJ$s}}T7JK!VA}-KLL$dii@F1m,@At $TYY?6!]+?@t $q#dii.1,a0 )A#DIIq1 )#'99.89Cx6!]33VA}E]].. BK( ) ) Dz$q#dii.1,a0 )A#DIIq1 )#'99.89Cx6!]33VA}E]].. BK( ) ) T {$ diilM2F499Q<4W11"ay$ # 499QR=DIIaLQ #DAqq-(Aq-(A KKA2q " #Dz$ 499Q< / 499Q< / 499Q< /2qb!9bAh''$()]]DNNd##D$/  -'--. > >$ "##D$/  '=1 1 4=yJLA@..s$1S94S>.T+T$$T  $Tct|ttfs0t}|j t |ft |St|tr3t j|jDcgc] }t|c}St|tr3t j|jDcgc] }t|c}Sycc}wcc}w)z Distributes AND over OR in the NNF expression. Returns the result( Conjunctive Normal Form of expression) as a CNF object. N) rrrsetadd frozensetCNFall_orrKdistribute_AND_over_ORall_and)r2rr-s r'rrs dS"I &e  4'"#3x$zz'+zz3 #33734 4${{(, 4!$4C845 534s 4C7CceZdZdZddZdZdZdZdZdZ e d Z d Z d Z d Zd ZdZdZe dZe dZe dZe dZy)ra Class to represent CNF of a Boolean expression. Consists of set of clauses, which themselves are stored as frozenset of Literal objects. Examples ======== >>> from sympy import Q >>> from sympy.assumptions.cnf import CNF >>> from sympy.abc import x >>> cnf = CNF.from_prop(Q.real(x) & ~Q.zero(x)) >>> cnf.clauses {frozenset({Literal(Q.zero(x), True)}), frozenset({Literal(Q.negative(x), False), Literal(Q.positive(x), False), Literal(Q.zero(x), False)})} Nc*|s t}||_yr*)rclausesr,rs r'rMz CNF.__init__ seG r(cdtj|j}|j|yr*)rto_CNFr add_clauses)r,proprs r'rzCNF.add%s$**T"** !r(cdj|jDcgc]0}ddj|Dcgc] }t|c}zdz2c}}}|Scc}wcc}}w)Nrwrfrgrh)rjrrT)r,rr"rls r'r9z CNF.__str__)s] JJ,, (5::6:Cs3x:; ;S @ ( ; (sA AA A c6|D]}|j||Sr*r)r,propsps r'extendz CNF.extend0s! A HHQK  r(c>tt|jSr*)rrrr+s r'copyzCNF.copy5s3t||$%%r(c.|xj|zc_yr*)rrs r'rzCNF.add_clauses8s  r(c6|}|j||Sr*r)r$rress r' from_propz CNF.from_prop;se   r(c<|j|j|Sr*)rrr;s r'__iand__z CNF.__iand__As ' r(ct}|jD]}||Dchc]}|jc}z}!|Scc}wr*)rrr")r, predicatescr-s r'all_predicateszCNF.all_predicatesEs@U  1A a0s3770 0J 11s;ct}t|j|jD];\}}t|}|j||j t |=t |Sr*)rrrupdaterrr)r,cnfrrrrs r'_orzCNF._orKs[%DLL#++6 (DAqa&C JJqM KK # ' (7|r(cb|jj|j}t|Sr*)runionr)r,rrs r'_andzCNF._andSs$,,$$S[[17|r(ct|j}|dDchc]}t|f}}t|}|ddD]6}|Dchc]}t|f}}|j t|}8|Scc}wcc}w)N)listrrrr)r,clssrllrestrs r'_notzCNF._notWsDLL!(,R 11i! 1 1 W"I D+/0aQB5!0A0AB  21s A> Bcg}|jD]7}|Dcgc]}|j|}}|jt|9t |}t |Scc}wr*)rr3rrrr)r,r2 clause_listrr-litss r'r3z CNF.rcallasc ll *F/56CIIdO6D6   r4y ) *K %d++7sAcb|dj}|ddD]}|j|}|SNrrz)rrr$rrrs r'rz CNF.all_oris8 GLLNH Dd A r(cb|dj}|ddD]}|j|}|Sr)rrrs r'rz CNF.all_andps8 GLLNH Dt A r(cJddlm}t||}t|}|S)Nr)get_composite_predicates)sympy.assumptions.factsrrr)r$r2rs r'rz CNF.to_CNFws$Dd467%d+ r(c@dtfd|jDS)zm Converts CNF object to SymPy's boolean expression retaining the form of expression. c\|jrt|jS|jSr*)r#rr")r-s r'remove_literalz&CNF.CNF_to_cnf..remove_literals#&::3sww< :377 :r(c3@K|]}tfd|Dyw)c3.K|] }|ywr*rn).0r-rs r' z+CNF.CNF_to_cnf...s@#.-@sN)r)rrrs r'rz!CNF.CNF_to_cnf..s\fR@@A\s)rr)r$rrs @r' CNF_to_cnfzCNF.CNF_to_cnf~s"  ;\PSP[P[\]]r(r*)r8rBrCrDrMrr9rrr classmethodrrrrrrr3rrrrrnr(r'rrs" " &   ,   ^^r(rc\eZdZdZd dZdZedZedZdZ dZ d Z d Z d Z y) EncodedCNFz0 Class for encoding the CNF expression. Ncl|s|sg}i}||_||_t|j|_yr*)dataencodingrkeys_symbols)r,rrs r'rMzEncodedCNF.__init__s1HDH   X]]_- r(c 2t|j|_t|j}t t |jt d|dz|_|jDcgc]}|j|c}|_ ycc}wNrz) rrrrdictziprrrencoder)r,rnrs r'from_cnfzEncodedCNF.from_cnfshS//12  SaQ@A 7:{{CVT[[(C Cs3Bc|jSr*)rr+s r'symbolszEncodedCNF.symbolss }}r(cFtdt|jdzSr)rrrr+s r' variableszEncodedCNF.variablessQDMM*Q.//r(c|jDcgc] }t|}}t|t|jScc}wr*)rrrrr)r,rnew_datas r'rzEncodedCNF.copys8.2ii8FCK88(D$7889sAcPtj|}|j|yr*)rr add_from_cnf)r,rrs r'add_propzEncodedCNF.add_propsmmD! #r(c|jDcgc]}|j|}}|xj|z c_ycc}wr*)rrr)r,rrrs r'rzEncodedCNF.add_from_cnfs658[[A64;;v&AA W Bs?c|j}|jj|d}|Dt|j}|jj ||dzx}|j|<|j r| S|Sr)r"rrrrrr#)r,r-literalvaluers r' encode_argzEncodedCNF.encode_argsp'' !!'40 =DMM"A MM  )-.U 2EDMM'* ::6MLr(c|Dchc]2}|jtjk(s|j|nd4c}Scc}w)Nr)r"rfalser)r,rr-s r'rzEncodedCNF.encodes3QWX#CGGqww,>$AEXXXs7?)NN)r8rBrCrDrMrrErrrrrrrrnr(r'rrsT.D 009 Yr(rr*)#rD itertoolsrrrsympy.assumptions.assumerrsympy.core.relationalrr r r r r sympy.core.singletonrsympy.logic.boolalgrrrrrrrrrrrrrrrrrrnr(r'rsr 98@88"22JJ;;|@@jZ5(y^y^x3Y3Yr(