K iJ UdZddlmZddlZddlmZddlmZddlm Z ddl m Z ddl Z ddl mZdd lmZdd lmZd d giZed Zerkej*Zej.Zdd lmZie j8ej:e j<ej>e j@ejBe jDejFe jHejJe jLejLe jNejNe jPejPe jRejRe jTejTe jVejXe jZejZe j\ej^e j`ej`e jbejde jfejhe jjejjie jlejne jpejpe jrejte jvejve jxejze j|ej~e jeje jeje jeje jeje jeje jeje jeje jeje jeje jeje jeje jeje jeje jeje jeje jeje jjjde jejej:e jejej>e jeje jeje jeje jejdddgi ZlGdde ZmiZndeod<ddZpddZqedddddddZry)z .. deprecated:: 1.8 ``sympy.printing.theanocode`` is deprecated. Theano has been renamed to Aesara. Use ``sympy.printing.aesaracode`` instead. See :ref:`theanocode-deprecated` for more information. ) annotationsN)Any) import_module)Printer) is_sequence)partial)doctest_depends_on)sympy_deprecation_warning)theano_functiontheano)linalgc.tjddSNr)ttcomplex_/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/printing/theanocode.pyrJsRZZ!_r)FFrceZdZdZdZfdZddZddZdZdZ dZ d Z d Z d Z e xZZd Zd ZdZdZdZdZdZdZdZdZdZdZdZddZxZS) TheanoPrintera Code printer which creates Theano symbolic expression graphs. Parameters ========== cache : dict Cache dictionary to use. If None (default) will use the global cache. To create a printer which does not depend on or alter global state pass an empty dictionary. Note: the dictionary is not copied on initialization of the printer and will be updated in-place, so using the same dict object when creating multiple printers or making multiple calls to :func:`.theano_code` or :func:`.theano_function` means the cache is shared between all these applications. Attributes ========== cache : dict A cache of Theano variables which have been created for SymPy symbol-like objects (e.g. :class:`sympy.core.symbol.Symbol` or :class:`sympy.matrices.expressions.MatrixSymbol`). This is used to ensure that all references to a given symbol in an expression (or multiple expressions) are printed as the same Theano variable, which is created only once. Symbols are differentiated only by name and type. The format of the cache's contents should be considered opaque to the user. _theanocR|jdi|_t| |i|y)Ncache)poprsuper__init__)selfargskwargs __class__s rrzTheanoPrinter.__init__rs'ZZ,  $)&)rcR| |j}|t||j||fS)z Get the cache key for a SymPy object. Parameters ========== s : sympy.core.basic.Basic SymPy object to get key for. name : str Name of object, if it does not have a ``name`` attribute. )nametyper )rsr$dtype broadcastables r_get_keyzTheanoPrinter._get_keyvs+ <66Dd1gqvvum<))D3KDKK.v.DD8}EsAc 4t|jSr>)floatevalf)rnr!s r _print_NumberzTheanoPrinter._print_NumbersQWWYrc l|jdij|}|j||dS)Nr3NNr,r5)rXr!r's r_print_MatrixSymbolz!TheanoPrinter._print_MatrixSymbols5 8R(,,Q/""1E"NNrc ttds tdtj|j Dcgc]#}|Dcgc]}|j |fi|c}%c}}Scc}wcc}}w)N stacklistsz>Matrix translation not yet supported in this version of Theano)hasattrrNotImplementedErrorrPtolistr@)rrMr!LrCs r_print_DenseMatrixz TheanoPrinter._print_DenseMatrixspr<(%OQ Q}}XXZ 45 5C[T[[ ' ' 5   5 s A3 A.!A3 .A3 c |jDcgc]}|j|fi|}}|d}|ddD]}tj||}|Scc}wr)r r@rdot)rrAr!rCrDresultchilds r _print_MatMulzTheanoPrinter._print_MatMulsb:>))D3KDKK.v.DD!ab\ +EVVFE*F + EsAc  |jDcgc]}|j|fi|}}d}t|dtr6|ddkDr.t |dD]}t j ||d}|Stdcc}w)Nrrz^Only non-negative integer powers of matrices can be handled by Theano at the moment)r r@ isinstanceintrangerrWrR)rrAr!rCrDrXis r _print_MatPowzTheanoPrinter._print_MatPows:>))D3KDKK.v.DD hqk3 'HQK!O8A;' 5 4 5  &'HI I EsBc |j|jfi|}|jt|jfi|}|jt|jfi|}|||fSr>)r@parentslicerowslicecolslice)rrAr!rbrdres r_print_MatrixSlicez TheanoPrinter._print_MatrixSlicesgT[[3F34;;udmm4??4;;udmm4??h())rc f|jj\}}t|Dcgc];}t|Dcgc]$}|j|j||ffi|&c}=}}}t j dg|Dcgc]}t j dg|c}Scc}wcc}}wcc}wr)blocksshaper^r@rjoin) rrAr!nrowsncolsrcrhrows r_print_BlockMatrixz TheanoPrinter._print_BlockMatrixs{{(( u"'u/"'u/4;;t{{1a40;F;///wwq@?BGGA,,?@@//@sB()B# #B(B. #B(c t|j|j|jfDcgc]1}t |t j r|j|fi|n|3c}Scc}wr>)rcstartstopstepr\sympyBasicr@)rrAr!r_s r _print_slicezTheanoPrinter._print_slicesj"&**dii!CE&a5#t{{1//;<=EF FEs6A%c "tjSr>)mathpirrAr!s r _print_PizTheanoPrinter._print_Pis wwrc ,tjdS)Nr)tsexpr{s r _print_Exp1zTheanoPrinter._print_Exp1svvayrc ddl}|jdj\}}|j|fi|}|j|fi|}t|jdk(r!tj |||j S|jtj|jddfi|}tj |||Sr) numpyr r@lenrswitchnanru Piecewise) rrAr!npecondp_condp_e p_remainings r_print_PiecewisezTheanoPrinter._print_Piecewises))A,##4T,V,dkk!&v& tyy>Q 99VS"&&1 1"dkk%//499QR="ALVL yyk22rc tj|j|jfi||j|jfi|Sr>)rtrue_divr@pqr{s r_print_RationalzTheanoPrinter._print_Rationals@{{;4;;tvv88&4;;tvv88: :rc |jSr>)rr{s r_print_IntegerzTheanoPrinter._print_Integers vv rc n|jtj|jddzfi|Sr)r@rugammar r{s r_print_factorialzTheanoPrinter._print_factorials.t{{5;;tyy|a'78CFCCrc |j|jfi|}|jD]?}|j|fi|}tj ||tj |}A|Sr>)r@rA variablesrRop ones_like)rderivr!rvvars r_print_DerivativezTheanoPrinter._print_Derivative se T[[ .v .?? 4C$++c,V,CCc!23B 4 rc|Sr>r)rrAs r emptyPrinterzTheanoPrinter.emptyPrinters rc:|i}|i}|j|||S)a Convert a SymPy expression to a Theano graph variable. The ``dtypes`` and ``broadcastables`` arguments are used to specify the data type, dimension, and broadcasting behavior of the Theano variables corresponding to the free symbols in ``expr``. Each is a mapping from SymPy symbols to the value of the corresponding argument to ``theano.tensor.Tensor``. See the corresponding `documentation page`__ for more information on broadcasting in Theano. .. __: http://deeplearning.net/software/theano/tutorial/broadcasting.html Parameters ========== expr : sympy.core.expr.Expr SymPy expression to print. dtypes : dict Mapping from SymPy symbols to Theano datatypes to use when creating new Theano variables for those symbols. Corresponds to the ``dtype`` argument to ``theano.tensor.Tensor``. Defaults to ``'floatX'`` for symbols not included in the mapping. broadcastables : dict Mapping from SymPy symbols to the value of the ``broadcastable`` argument to ``theano.tensor.Tensor`` to use when creating Theano variables for those symbols. Defaults to the empty tuple for symbols not included in the mapping (resulting in a scalar). Returns ======= theano.gof.graph.Variable A variable corresponding to the expression's value in a Theano symbolic expression graph. )r3r4)r@)rrAr3r4s rdoprintzTheanoPrinter.doprints/P >F  !N{{4~{NNrNNNrL)__name__ __module__ __qualname____doc__ printmethodrr)r1r8r<rErJrNrU_print_ImmutableMatrix_print_ImmutableDenseMatrixrZr`rfrprwr|rrrrrrrr __classcell__)r"s@rrrUs4K*=$.E P   O `). Defaults to the module-level global cache. dtypes : dict Passed to :meth:`.TheanoPrinter.doprint`. broadcastables : dict Passed to :meth:`.TheanoPrinter.doprint`. Returns ======= theano.gof.graph.Variable A variable corresponding to the expression's value in a Theano symbolic expression graph. z sympy.printing.theanocode is deprecated. Theano has been renamed to Aesara. Use sympy.printing.aesaracode instead.1.8theanocode-deprecateddeprecated_since_versionactive_deprecations_targetz"theano is required for theano_code)rsettings)r r ImportErrorrrr)rArr!s r theano_coderFsTD :"'6 8 >?? } :=ur 2 : :4 J6 JJrc |tj|d|zS|It|j}|j Dcic]\}}|d|zd||z zzc}}S||SiScc}}w)a6 Get value of ``broadcastables`` argument to :func:`.theano_code` from keyword arguments to :func:`.theano_function`. Included for backwards compatibility. Parameters ========== inputs Sequence of input symbols. dim : int Common number of dimensions for all inputs. Overrides other arguments if given. dims : dict Mapping from input symbols to number of dimensions. Overrides ``broadcastables`` argument if given. broadcastables : dict Explicit value of ``broadcastables`` argument to :meth:`.TheanoPrinter.doprint`. If not None function will return this value unchanged. Returns ======= dict Dictionary mapping elements of ``inputs`` to their "broadcastable" values (tuple of ``bool``\ s). F)T)dictfromkeysmaxvaluesitems)inputsdimdimsr4maxdimr&ds r dim_handlingrxs> }}VX^44 T[[]#  1 x!|g!44 4  ! I s A-)r )modulesrrr4c tdddts td|jdi}|jdi}t ||||}t t ||| } tt| |} tt| |} | D cgc]3} t| tjr| ntj| 5} } t| d k(r| d } tj| | fi|jD cgc]$} t| j j"d k(&c} |r t%s _Sfd }|_j*|_|_|Scc} wcc} w) aN Create a Theano function from SymPy expressions. .. deprecated:: 1.8 ``sympy.printing.theanocode`` is deprecated. Theano has been renamed to Aesara. Use ``sympy.printing.aesaracode`` instead. See :ref:`theanocode-deprecated` for more information. The inputs and outputs are converted to Theano variables using :func:`.theano_code` and then passed to ``theano.function``. Parameters ========== inputs Sequence of symbols which constitute the inputs of the function. outputs Sequence of expressions which constitute the outputs(s) of the function. The free symbols of each expression must be a subset of ``inputs``. scalar : bool Convert 0-dimensional arrays in output to scalars. This will return a Python wrapper function around the Theano function object. cache : dict Cached Theano variables (see :class:`TheanoPrinter.cache `). Defaults to the module-level global cache. dtypes : dict Passed to :meth:`.TheanoPrinter.doprint`. broadcastables : dict Passed to :meth:`.TheanoPrinter.doprint`. dims : dict Alternative to ``broadcastables`` argument. Mapping from elements of ``inputs`` to integers indicating the dimension of their associated arrays/tensors. Overrides ``broadcastables`` argument if given. dim : int Another alternative to the ``broadcastables`` argument. Common number of dimensions to use for all arrays/tensors. ``theano_function([x, y], [...], dim=2)`` is equivalent to using ``broadcastables={x: (False, False), y: (False, False)}``. Returns ======= callable A callable object which takes values of ``inputs`` as positional arguments and returns an output array for each of the expressions in ``outputs``. If ``outputs`` is a single expression the function will return a Numpy array, if it is a list of multiple expressions the function will return a list of arrays. See description of the ``squeeze`` argument above for the behavior when a single output is passed in a list. The returned object will either be an instance of ``theano.compile.function_module.Function`` or a Python wrapper function around one. In both cases, the returned value will have a ``theano_function`` attribute which points to the return value of ``theano.function``. Examples ======== >>> from sympy.abc import x, y, z >>> from sympy.printing.theanocode import theano_function A simple function with one input and one output: >>> f1 = theano_function([x], [x**2 - 1], scalar=True) >>> f1(3) 8.0 A function with multiple inputs and one output: >>> f2 = theano_function([x, y, z], [(x**z + y**z)**(1/z)], scalar=True) >>> f2(3, 4, 2) 5.0 A function with multiple inputs and multiple outputs: >>> f3 = theano_function([x, y], [x**2 + y**2, x**2 - y**2], scalar=True) >>> f3(2, 3) [13.0, -5.0] See also ======== dim_handling zz sympy.printing.theanocode is deprecated. Theano has been renamed to Aesara. Use sympy.printing.aesaracode insteadrrrz&theano is required for theano_functionrr3r)rr3r4rrc|}t|r(t|Dcgc]\}}|r|dn|c}}S|dScc}}w)Nr)r enumerate)r outr_ofuncis_0ds rwrapperz theano_function..wrapper/sMDk s 9B3HAU1XAbE1,H Hr7NIs?)r r rrrrrlistmapr\Variableras_tensor_variablerfunctionoutputsvariabler(anyr __wrapped__r)rrscalarrrr4r!rr3codetinputstoutputsoutputrrrrs @@rr r sl@ }!&6 8 BCC JJw #E ZZ" %F!Cd>N ;eF"0 2D3tV$%GCg&'Hpxxek*VV__=2CXCXY_C``xHx 8}A; ??7H 7 7D9= FAS)) *a / FE U# GllGO"G N9y Gs 8E1)E6r>rr)sr __future__rrytypingrsympy.externalrsympy.printing.printerrsympy.utilities.iterablesrru functoolsrsympy.utilities.decoratorr sympy.utilities.exceptionsr __doctest_requires__r rr~r.rtheano.sandboxr tlinalgAddaddMulmulAbsabs_signsgnceilingceilfloorlogrsqrtcosacosarccossinasinarcsintanatanarctanatan2arctan2coshacosharccoshsinhasinharcsinhtanhatanharctanhrerealimimagrCangleerfrloggammagammalnPowpowEqeqStrictGreaterThangtStrictLessThanltLessThanle GreaterThangeAndand_Oror_MaxmaximumMinminimum conjugateconjcorenumbers ImaginaryUnitMatAddElemwiseHadamardProductTracetrace DeterminantdetInversematrix_inverse Transpose DimShuffler?rr__annotations__rrr rrrr5s:# (*1 8@-xj9 x  B B00 IIrvv0 IIrvv0 IIrww0 JJ 0 MM277 0 KK 0 IIrvv0 IIrvv0 JJ0 IIrvv0 JJ 0 IIrvv0 JJ 0 IIrvv0 JJ 0 KK!0" JJ#0$ KK%0& JJ'0( KK)0* JJ+0, KK-0. HHbgg/00 HHbgg102 IIrxx304 IIrvv506 KK708 NNBJJ90: IIrvv;0< HHbee=0>  # #RUU?0@  "%%A0B NNBEEC0D   ruuE0F IIrww HHbff IIrzz IIrzz OORWW JJ   , ,.D LL"++bff-  ! !2;;rvv#6 KK    MM711 OOR]]>Aq6B_0GfkOGkO\ " n!/Kd,^K(S tDS)Sr