K iTpddlZddlZddlmZmZddlmZmZmZmZm Z m Z ddl m Z m Z ddlmZmZmZmZmZmZmZddlmZddlmZmZmZddlmZmZmZdd l m!Z!m"Z"dd l#m$Z$dd l%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,dd l-m.Z.m/Z/m0Z0m1Z1m2Z2dd l-m3Z3m4Z4m5Z5m6Z6m7Z7ddl8m9Z9ddl:m;Z;ddlZ?ddl@mAZAddlBmCZCddlDmEZEddlFmGZGmHZHmIZImJZJmKZKGdde9ZL d#dZMdejee fdeLdejePgdffdZQdedeLdejePgdffdZRddde d ejeTd!eTfd"ZUy)$N)AddMul)SymbolExprFloatRationalIntegerBasic)UndefinedFunctionFunction) Relational UnequalityEqualityLessThan GreaterThanStrictLessThanStrictGreaterThan)Abs)explogPow)sinhcoshtanh)MinMax) Piecewise)sincostanasinacosatanatan2)AndOrXorImpliesBoolean) BooleanTrue BooleanFalseBooleanFunctionNotITE)Printer)Interval) prec_to_dpsto_str)AppliedPredicate)AppliedBinaryRelation)Q)StrictGreaterThanPredicateStrictLessThanPredicateGreaterThanPredicateLessThanPredicateEqualityPredicateceZdZUdZdededediiiedede de d e d e d e d eded ed ed ed ed edededediedededededededede de!de"de#de$de%de&d e'd!e(d"d#Z)e*e+d$<e*e+d%< dEd&e,jZe*fd'Z.d(e/fd)Z0d*e/d+e,jbe2e3fd,e/fd-Z4d.Z5d/e6fd0Z7d/e8fd1Z9d/e:fd2Z;d/efd4Z?d/e@fd5ZAd/eBfd6ZCd/eBfd7ZDd8eEfd9ZFd8eGfd:ZHd8eIfd;ZJd8efd<ZKd8eLfd=ZMd8eNfd>ZOd8efd?ZPd8eQfd@ZRdAZSdBZTdCZUdDZVy)F SMTLibPrinter_smtlibNBoolIntReal+*=z<=z>=<>rrabsrrr arcsinarccosarctanarctan2rrrminmaxpowandorxornotitez=>) precision known_typesknown_constantsknown_functions_default_settings symbol_tablesettingsc|xsi}|xsi|_tj|||jd|_t |jd|_t |jd|_t |jd|_|j jD]}|j|rJ|jjD]}|j|rJy)NrSrTrUrV) rXr/__init__ _settings _precisiondict _known_types_known_constants_known_functionsvalues_is_legal_name)selfrYrX_s [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/printing/smtlib.pyr[zSMTLibPrinter.__init__Ts>r(.Bx(..5  !>? $T^^4E%F G $T^^4E%F G""))+JAD4G4G4J-J4JJ&&--/NA8K8KA8N1N8NNscT|sy|djrytd|DS)NFrc3JK|]}|jxs|dk(yw)reN)isalnum).0res rf z/SMTLibPrinter._is_legal_name..es"6q199;*!s(*6s!#) isnumericall)rdrhs rfrczSMTLibPrinter._is_legal_namebs( Q4>> E6A666rgopargsreturncJdjfd|D}d|d|dS)N c3dK|]'}t|tr|nj|)ywN) isinstancestr_print)rlards rfrmz(SMTLibPrinter._s_expr..hs4 As#AQ  s-0())join)rdrprqargs_strs` rf_s_exprzSMTLibPrinter._s_exprgs788    2$az##rgc||jvr|j|}nt||jvr|jt|}ntt|tk(r |j}nlt |t rM|j |jvr5|j|j }|j||jS|j|}|j||jSrv) ratyper namerwr4functionr argumentsrq)rderps rf_print_FunctionzSMTLibPrinter._print_Functionos %% %&&q)B !W-- -&&tAw/B $q']/ /B 0 1ajjDDYDY6Y&&qzz2B<<AKK0 0&&q)B||B''rgrc$|j|Srvrrdrs rf_print_RelationalzSMTLibPrinter._print_Relational~##A&&rgc$|j|Srvrrs rf_print_BooleanFunctionz$SMTLibPrinter._print_BooleanFunctionrrgc$|j|Srvrrs rf _print_ExprzSMTLibPrinter._print_Exprrrgct||jvr|j|S|jt}|jt}|j ||j ||j gSrv)rrarrr-rrq)rdreq_opnot_ops rf_print_UnequalityzSMTLibPrinter._print_Unequalitysh 7d++ +))!, ,))(3E**3/F<<eQVV)D(EF Frgcrdtjttfffd |jS)Nrqc |d\}}t|dk(r'|dust|tsJj|Sjt }j ||||ddgS)NrT)lenrwr*ryrar.r)rqrcrR_print_Piecewise_recursiverds rfrzBSMTLibPrinter._print_Piecewise.._print_Piecewise_recursivesz7DAq4yA~T jK&@@@{{1~%++C0||Cq4T!"X>*rg)typingUnionlisttuplerq)rdrrs` @rf_print_PiecewisezSMTLibPrinter._print_Piecewises- V\\$+-F *!&&11rgc|jjr|jjry|jj|jjk7rtd|dd|jd|jdS)NzOne-sided intervals (`z`) are not supported in SMT.[z, ])start is_infiniteend ValueErrorrs rf_print_IntervalzSMTLibPrinter._print_Intervalsm 77  155#4#4 WW AEE$5$5 55aS8TUV Vqwwir!%%* *rgcR|jtjk(r%tj|jdd}nU|jtj k(r%tj |jdd}n|jtjk(r$tj|jdd}n|jtjk(r$tj|jdd}n|jtjk(r$tj|jdd}nP|jtjk(r$tj|jdd}ntd|d|j!|S)Nrz Predicate (`z`) is not handled.)rr5positivegtrnegativeltzeroeq nonpositivele nonnegativegenonzeroner_print_AppliedBinaryRelation)rdrrels rf_print_AppliedPredicatez%SMTLibPrinter._print_AppliedPredicates :: #$$q{{1~a(C ZZ1:: %$$q{{1~q)C ZZ166 !$$q{{1~q)C ZZ1== ($$q{{1~q)C ZZ1== ($$q{{1~q)C ZZ199 $$$q{{1~q)C|A3.@AB B0055rgc|jtjk(r!|jt |j S|j |Srv)rr5rrrrrrs rfrz*SMTLibPrinter._print_AppliedBinaryRelations= :: ))*akk*BC C''* *rgxcy)Ntruerdrs rf_print_BooleanTruez SMTLibPrinter._print_BooleanTruesrgcy)Nfalserrs rf_print_BooleanFalsez!SMTLibPrinter._print_BooleanFalsesrgc 6t|j}t|j|ddd}d|vrV|j d\}}|ddk(r|dd}|j t }|j t}d|d|d |d |d S|d vr td |S)NT) strip_zeros min_fixed max_fixedrrrArr{rtz (z 10 z)))z+infz-infz)Infinite values are not supported in SMT.) r1_prec mlib_to_str_mpf_splitrarrr)rdrdpsstr_realmantrmulrMs rf _print_FloatzSMTLibPrinter._print_Floats!''"qwwY]^ (?"..-KT31v}!"g'',C'',C,/sC@ @ ) )HI IOrgc6|jt|Srv)ryrrs rf _print_floatzSMTLibPrinter._print_floats{{58$$rgcR|jd|j|jgS)N/)rpqrs rf_print_RationalzSMTLibPrinter._print_Rationals||C!##qss,,rgcN|jdk(sJt|jS)Nr)rrxrrs rf_print_IntegerzSMTLibPrinter._print_Integersssaxx133xrgct|Srv)rxrs rf _print_intzSMTLibPrinter._print_ints 1v rgcT|j|jsJ|jSrvrcrrs rf _print_SymbolzSMTLibPrinter._print_Symbol#""166***vv rgc|jj|}|r|S|jr|j|jn|j}|j |Srv)r`getr]evalfr)rdrrfs rf_print_NumberSymbolz!SMTLibPrinter._print_NumberSymbolsQ$$((+ K,0OO(A$$Q' 'rgcT|j|jsJ|jSrvrrs rf_print_UndefinedFunctionz&SMTLibPrinter._print_UndefinedFunctionrrgct|jvr|jtddS|j|S)NrF)evaluate)rrarrrs rf _print_Exp1zSMTLibPrinter._print_Exp1sCd+++  Q!7 8   $ $Q ' rgcJtdt|dt|d)NzCannot convert `z ` of type `z ` to SMT.)NotImplementedErrorreprr)rdexprs rf emptyPrinterzSMTLibPrinter.emptyPrinters'!$4T$ZL DQUJ$3$ (':''''T'G:G 2) 2++6)96$+.>+&K\e&%e%--Cv( crgr<c | xsd} t|ts|g}|D cgc]} tj| ddd}} |si}t |d|i}i} |r|| d<~|r|| d<~|r|| d<~|r|| d <~|sg}| sg} t | |} ~|D]}|j ttD]}|jr;|| jvr-|| jvr| d |d t| j|<|jsWt|| jvsot|| jvs|j rt#d |d g}|r|Dcic].}|j$D]}|| jvr |j&|0}}}|Dcic]L}|j tD]2}t|| jvr|j s |j&|4N}}}|j)Dcgc]}t+|| | c}|j)Dcgc]}t+|| | c}z}|Dcgc]}|s| }}|r|Dcgc]}t-|| | }}dj/g|Dcgc]%}t|t0r|n| j3|'c}t5d|D|Dcgc]%}t|t0r|n| j3|'c}| Dcgc]%}t|t0r|n| j3|'c}Scc} wcc}}wcc}}wcc}wcc}wcc}wcc}wcc}wcc}wcc}w)aConverts ``expr`` to a string of smtlib code. Parameters ========== expr : Expr | List[Expr] A SymPy expression or system to be converted. auto_assert : bool, optional If false, do not modify expr and produce only the S-Expression equivalent of expr. If true, assume expr is a system and assert each boolean element. auto_declare : bool, optional If false, do not produce declarations for the symbols used in expr. If true, prepend all necessary declarations for variables used in expr based on symbol_table. precision : integer, optional The ``evalf(..)`` precision for numbers such as pi. symbol_table : dict, optional A dictionary where keys are ``Symbol`` or ``Function`` instances and values are their Python type i.e. ``bool``, ``int``, ``float``, or ``Callable[...]``. If incomplete, an attempt will be made to infer types from ``expr``. known_types: dict, optional A dictionary where keys are ``bool``, ``int``, ``float`` etc. and values are their corresponding SMT type names. If not given, a partial listing compatible with several solvers will be used. known_functions : dict, optional A dictionary where keys are ``Function``, ``Relational``, ``BooleanFunction``, or ``Expr`` instances and values are their SMT string representations. If not given, a partial listing optimized for dReal solver (but compatible with others) will be used. known_constants: dict, optional A dictionary where keys are ``NumberSymbol`` instances and values are their SMT variable names. When using this feature, extra caution must be taken to avoid naming collisions between user symbols and listed constants. If not given, constants will be expanded inline i.e. ``3.14159`` instead of ``MY_SMT_VARIABLE_FOR_PI``. prefix_expressions: list, optional A list of lists of ``str`` and/or expressions to convert into SMTLib and prefix to the output. suffix_expressions: list, optional A list of lists of ``str`` and/or expressions to convert into SMTLib and postfix to the output. log_warn: lambda function, optional A function to record all warnings during potentially risky operations. Soundness is a core value in SMT solving, so it is good to log all assumptions made. Examples ======== >>> from sympy import smtlib_code, symbols, sin, Eq >>> x = symbols('x') >>> smtlib_code(sin(x).series(x).removeO(), log_warn=print) Could not infer type of `x`. Defaulting to float. Non-Boolean expression `x**5/120 - x**3/6 + x` will not be asserted. Converting to SMTLib verbatim. '(declare-const x Real)\n(+ x (* (/ -1 6) (pow x 3)) (* (/ 1 120) (pow x 5)))' >>> from sympy import Rational >>> x, y, tau = symbols("x, y, tau") >>> smtlib_code((2*tau)**Rational(7, 2), log_warn=print) Could not infer type of `tau`. Defaulting to float. Non-Boolean expression `8*sqrt(2)*tau**(7/2)` will not be asserted. Converting to SMTLib verbatim. '(declare-const tau Real)\n(* 8 (pow 2 (/ 1 2)) (pow tau (/ 7 2)))' ``Piecewise`` expressions are implemented with ``ite`` expressions by default. Note that if the ``Piecewise`` lacks a default term, represented by ``(expr, True)`` then an error will be thrown. This is to prevent generating an expression that may not evaluate to anything. >>> from sympy import Piecewise >>> pw = Piecewise((x + 1, x > 0), (x, True)) >>> smtlib_code(Eq(pw, 3), symbol_table={x: float}, log_warn=print) '(declare-const x Real)\n(assert (= (ite (> x 0) (+ 1 x) x) 3))' Custom printing can be defined for certain types by passing a dictionary of PythonType : "SMT Name" to the ``known_types``, ``known_constants``, and ``known_functions`` kwargs. >>> from typing import Callable >>> from sympy import Function, Add >>> f = Function('f') >>> g = Function('g') >>> smt_builtin_funcs = { # functions our SMT solver will understand ... f: "existing_smtlib_fcn", ... Add: "sum", ... } >>> user_def_funcs = { # functions defined by the user must have their types specified explicitly ... g: Callable[[int], float], ... } >>> smtlib_code(f(x) + g(x), symbol_table=user_def_funcs, known_functions=smt_builtin_funcs, log_warn=print) Non-Boolean expression `f(x) + g(x)` will not be asserted. Converting to SMTLib verbatim. '(declare-const x Int)\n(declare-fun g (Int) Real)\n(sum (existing_smtlib_fcn x) (g x))' cyrvr)res rfzsmtlib_code..dsrgTF)strictr convert_xorrXrSrTrVrUCould not infer type of `z`. Defaulting to float.z$Unknown type of undefined function `z^`. Must be mapped to ``str`` in known_functions or mapped to ``Callable[..]`` in symbol_table. c3 K|]}|ywrvr)rlrs rfrmzsmtlib_code..s(a(s )rwrsympysympify_auto_infer_smtlib_typesr<atomsrr is_Symbolr`rXr is_Functionrra is_Piecewise TypeError free_symbolsrrb_auto_declare_smtlib_auto_assert_smtlibr}rxdoprintsorted)r auto_assert auto_declarerSrXrTrUrVprefix_expressionssuffix_expressionslog_warnrerYrrsym declarations constantsfnc functionsdecls rf smtlib_coder sr+NH dD !4&4    au%H D  + (L H)(;'kH]+o!23o!23 B1 B1h -A7768, C 1---1>>)4SE9PQR&+s#S !3!33S /$$6se> >=AB#Aq(3BB 99( As#A1 5  (<( ( As#A1 5 &( As#A1 5 # O j7V ?C    sB L3L AL&6L,L1;L6L6L;5*M 8*M )*M rrrc |jrD|j|}t|tsJ|j|}|j d||gS|j r|jt|}t|sJ|jDcgc]}|j|}}t|dkDsJddj|ddd}|d}|j dt|||gS|d|d ycc}w) Nz declare-constrr{rtr|z declare-funzNon-Symbol/Function `z` will not be declared.) rrXrwrr_rrcallable__args__rr})rrrtype_signaturereparams_signaturereturn_signatures rfrrs }},.$///7yy3*?@@ S 2'''5C5L5LM!..+MM>"Q&&&sxxs(;<=Q?)"-yyc4DFV(WXX (-DEFNsDrcJt|tss||jvr|j|tk(sO|jrVt ||jvr?|jt |j dtk(r|jd|gS|d|d|S)NrassertzNon-Boolean expression `z6` will not be asserted. Converting to SMTLib verbatim.)rwr)rXrrrrr)rrrs rfrrs!W Q^^q 1T 9  Q1>>! tAw((,4yyA3''+A3.defrg)rXexprsrXrrc|r t|nidtffd }||Dchc]}|jr|c}t||Dchc];}|j t D]!}|j D]}|jr|#=c}}}t||Dchc]t}|j tD]Z}t|vrKt|j t|jD]\}}|jr |tk(r|\vc}}}}t||Dchc]t}|j tD]Z}t|vrKt|j t|jD]\}}|jr |tk(r|\vc}}}}t||Dchc]*}|j tD]}|jr|,c}}t||Dchc]6}|j tD]}|jr|js|8c}}t|D cgc]}|j t D]} |  } }} | D cgc]1} | j"js| j"| j$f3c} | D cgc]1} | j$js| j$| j"f3c} z} | D]\} } | vr| n| vr| nw| j&r(t| vrt| jdnC| j(rtn1| js | j*rtn| jrtnd}|s|| h|Scc}wcc}}}wcc}}}}wcc}}}}wcc}}wcc}}wcc} }wcc} wcc} w)Nsymsc |D]<}|jsJj||x}|k7s)td|d|d|dy)Nrz`. Apparently both `z` and `z`?)r setdefaultr)r"infrhold_type_symbolss rf safe_updatez-_auto_infer_smtlib_types..safe_updatesb mA;; ;$//377C?";A3>RS[R\\cdgchhj kll mrgr)r^setrrrr,rqr rziprrr is_integeris_realrrlhsrhsr is_Boolean is_Integer)rXr r(rboolfuncsymbolparamintfuncrrrels_eqrelsinferreltd inferencer's @rfrrs"&2tL!rHm#m ;;    0 mm          )  >X % h0H0Q0QR  FE       wwx(  =H $ xW /F/O/OP  FE       ggfo         ggfo  >>&"3"3    %Et 80DEsEsEGE-4 &)8I8I! .5 &)8I8I!  D  6 u$0HUO$0HUO  T%[H%< T%[ ! * *2 .$$D##u'7'7C]]E   k5'95 6 OI F  sDN AN #A9N#4A9N+/N3 ;N9 #N?=OO9O O ) TTNNNNNNNN)Vrr sympy.corerrrrrrr r sympy.core.functionr r sympy.core.relationalr rrrrrr$sympy.functions.elementary.complexesr&sympy.functions.elementary.exponentialrrr%sympy.functions.elementary.hyperbolicrrr(sympy.functions.elementary.miscellaneousrr$sympy.functions.elementary.piecewiser(sympy.functions.elementary.trigonometricrrr r!r"r#r$sympy.logic.boolalgr%r&r'r(r)r*r+r,r-r.sympy.printing.printerr/ sympy.setsr0mpmath.libmp.libmpfr1r2rsympy.assumptions.assumer3!sympy.assumptions.relation.binrelr4sympy.assumptions.askr5#sympy.assumptions.relation.equalityr6r7r8r9r:r<rrCallablerxrrrr^rrrgrfrLs- DD;|||4@@BB=:[[[>>TT*B5C#``ocGoch$(<@04 xvfll68+;<Z`ZiZiknjoqujuZv* 4 M V__cUTX[=Y  +/^ ^//$'^ ^rg