K idZddlmZddlmZddlmZmZmZm Z ddl m Z ddl m Z mZmZmZmZmZddlmZmZdd gZGd deZGd d eZy ) z General binary relations. )Optional)S)AppliedPredicateask PredicateQ) BooleanKind)EqNeGtLtGeLe) conjunctsNotBinaryRelationAppliedBinaryRelationcneZdZUdZdZeeed<dZeeed<dZ e dZ e dZ dZ d d Zy) ra^ Base class for all binary relational predicates. Explanation =========== Binary relation takes two arguments and returns ``AppliedBinaryRelation`` instance. To evaluate it to boolean value, use :obj:`~.ask()` or :obj:`~.refine()` function. You can add support for new types by registering the handler to dispatcher. See :obj:`~.Predicate()` for more information about predicate dispatching. Examples ======== Applying and evaluating to boolean value: >>> from sympy import Q, ask, sin, cos >>> from sympy.abc import x >>> Q.eq(sin(x)**2+cos(x)**2, 1) Q.eq(sin(x)**2 + cos(x)**2, 1) >>> ask(_) True You can define a new binary relation by subclassing and dispatching. Here, we define a relation $R$ such that $x R y$ returns true if $x = y + 1$. >>> from sympy import ask, Number, Q >>> from sympy.assumptions import BinaryRelation >>> class MyRel(BinaryRelation): ... name = "R" ... is_reflexive = False >>> Q.R = MyRel() >>> @Q.R.register(Number, Number) ... def _(n1, n2, assumptions): ... return ask(Q.zero(n1 - n2 - 1), assumptions) >>> Q.R(2, 1) Q.R(2, 1) Now, we can use ``ask()`` to evaluate it to boolean value. >>> ask(Q.R(2, 1)) True >>> ask(Q.R(1, 2)) False ``Q.R`` returns ``False`` with minimum cost if two arguments have same structure because it is antireflexive relation [1] by ``is_reflexive = False``. >>> ask(Q.R(x, x)) False References ========== .. [1] https://en.wikipedia.org/wiki/Reflexive_relation N is_reflexive is_symmetriccdt|dk(stdt|zt|g|S)Nz0Binary relation takes two arguments, but got %s.)len ValueErrorr)selfargss g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/assumptions/relation/binrel.py__call__zBinaryRelation.__call__Ps54yA~ORUVZR[[\ \$T1D11c |jr|SyN)rrs rreversedzBinaryRelation.reversedUs   Krcyr!r"s rnegatedzBinaryRelation.negated[src|tjus|tjury|j}| y|r||k(ry|s||k(ryy)NTF)rNaNr)rlhsrhs reflexives r_compare_reflexivez!BinaryRelation._compare_reflexive_sR !%%<3!%%<%%     C3Js rcH|j|}||S|\}}|j|||}||S|jret|t|f}|jj||jjt |ur|j|||}|S)N) assumptions)r,handlerrtypedispatchr#)rrr.retr)r*typess revalzBinaryRelation.evalqs%d%%t, ?JSll3l= ?J   #YS *E$t||$$e,4IDLL4I4I8TY?4[[ll3lE r)T)__name__ __module__ __qualname____doc__rrbool__annotations__rrpropertyr#r&r,r4r%rrrrs];z$(L(4.'#'L(4.'2  $rcleZdZdZedZedZedZedZedZ dZ dZ y ) rzd The class of expressions resulting from applying ``BinaryRelation`` to the arguments. c |jdS)z#The left-hand side of the relation.r argumentsr"s rr)zAppliedBinaryRelation.lhs~~a  rc |jdS)z$The right-hand side of the relation.r>r"s rr*zAppliedBinaryRelation.rhsr@rcp|jj}||S||j|jS)zE Try to return the relationship with sides reversed. )functionr#r*r)rrevfuncs rr#zAppliedBinaryRelation.reverseds2 --(( ?Ktxx**rc|jj}||Std|jDs||j |j S|S)zE Try to return the relationship with signs reversed. c3@K|]}|jtuywr!)kindr ).0sides r z5AppliedBinaryRelation.reversedsign..sG499 +Gs)rDr#anyr?r)r*rEs r reversedsignz"AppliedBinaryRelation.reversedsignsM --(( ?KGGGDHH9txxi0 0 rcj|jj}| t|dS||jS)NFevaluate)rDr&rr?)rneg_rels rr&zAppliedBinaryRelation.negateds2--'' ?te, ,''rc xtttjttj t tjttjttjttji}t|D]L}|j|vr+j!|t#||j$<j!|Nt'fd||j(fDry|j*|j(j*t-|dt-|j(df}t'fd|Dry|j.j1|j2|}||St5d|j2D}|j.j1||S)Nc3&K|]}|v ywr!r%rJrel conj_assumpss rrLz2AppliedBinaryRelation._eval_ask..sDssl"DTFrPc3&K|]}|v ywr!r%rUs rrLz2AppliedBinaryRelation._eval_ask..s7ssl"7rXc3<K|]}|jywr!)simplify)rJas rrLz2AppliedBinaryRelation._eval_ask..s:aQZZ\:s)setr reqr ner gtr ltrgerlerfuncaddr0rrMr#r&rrDr4r?tuple)rr. binrelpredsr\neg_relsr2rrWs @r _eval_askzAppliedBinaryRelation._eval_asksBu 144QTT2qttRr144QTTR ;' $Avv$  !5T!W!5qvv!>?  #  $ DtT]].CD DLL$--"7"7TE9R   .0 7h7 7mm  = ?J:4>>::}}!!$ 44rc<t|}|td|z|S)Nz"Cannot determine truth value of %s)r TypeError)rr2s r__bool__zAppliedBinaryRelation.__bool__s&$i ;@4GH H rN) r5r6r7r8r;r)r*r#rNr&rirlr%rrrrsu !!!!++  (( 56rN)r8typingrsympy.core.singletonrsympy.assumptionsrrrrsympy.core.kindr sympy.core.relationalr r r r rrsympy.logic.boolalgrr__all__rrr%rrrtsM"AA'88. 4 5uYupM,Mr