K iSdZddlZddlmZddlmZmZmZejdZ e jejedZ e jeje rgddlZe j Ze j$ZdDcgc]}ej!|dc}\ZZZd Dcgc]}ej%dd | c}\ZZZnd ZddlZdd lmZddlm Z m!Z!m"Z"m#Z#ddl$m%Z%m&Z&m'Z'd Dcgc]}ejP|ddc}\Z)Z*Z+ejXde#Z-dZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5edZ6dZ7dZ8dZ9dZ:dZ;d Zd#Z?d$Z@d%ZAd&ZBd'ZCd(ZDd)ZEd*ZFd+ZGd,ZHd-ZId.ZJd/ZKd0ZLd1ZMd2ZNd3ZOed4ZPd5ZQd6ZRd7ZSd8ZTd9ZUd:ZVd;ZWd<ZXd=ZYd>ZZd?Z[ycc}wcc}wcc}w)@a Important note on tests in this module - the Theano printing functions use a global cache by default, which means that tests using it will modify global state and thus not be independent from each other. Instead of using the "cache" keyword argument each time, this module uses the theano_code_ and theano_function_ functions defined below which default to using a new, empty cache instead. N) import_module)raisesSKIPwarns_deprecated_sympyztheano.configdefaultstheanoxyzfloatXXYZFF)nameT)S)xyzt) theano_code dim_handlingtheano_functionfc ~|jdit5t|fi|cdddS#1swYyxYw)zB Wrapper for theano_code that uses a new, empty cache by default. cacheN) setdefaultrr)exprkwargss j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/printing/tests/test_theanocode.py theano_code_r.s: gr"  !+4*6*+++s 3<c |jdit5t||fi|cdddS#1swYyxYw)zF Wrapper for theano_function that uses a new, empty cache by default. rN)rrr)inputsoutputsrs rtheano_function_r!4s< gr"  !:vw9&9:::s 4=c ttt|}tjj j |}tjj j||\}}tjj||S)z Transform SymPy expressions into Theano Computation. Parameters ========== exprs SymPy expressions Returns ======= theano.gof.FunctionGraph ) listmaprrgofgraphrclone FunctionGraph)exprsoutsinss r fgraph_ofr,;sf L%( )D **   ! !$ 'C   &&sD1IC :: # #C ..ctjjjd}|j }|j j ||S)z Simplify a Theano Computation. Parameters ========== fgraph : theano.gof.FunctionGraph Returns ======= theano.gof.FunctionGraph fusion)rcompileget_default_mode excludingr' optimizeroptimize)fgraphmodes rtheano_simplifyr7MsC >> * * , 6 6x @D \\^FNNF# Mr-c |tttjf}t ||}t ||}|s|r |r|sy||k(St |t t f}t |t t f}|s|rO|r|rt|t|k7ryt tt|t tt|k(Stjj|d}tjj|d}d||fd||ffD])\} } } | dk(s td| d| d t| d ||k(S) a Test two Theano objects for equality. Also accepts numeric types and lists/tuples of supported types. Note - debugprint() has a bug where it will accept numeric types but does not respect the "file" argument and in this case and instead prints the number to stdout and returns an empty string. This can lead to tests passing where they should fail because any two numbers will always compare as equal. To prevent this we treat numbers as a separate case. Fstr)fileabztheano.printing.debugprint(z) returned empty string (z is instance of ))intfloatnpnumber isinstancetupler#typer$theqrprinting debugprint TypeError) r;r< numeric_typesa_is_numb_is_numa_is_seqb_is_seqastrbstrargnameargvalargstrs rrFrF^s2%+M!]+H!]+H8XAv !eT]+H!eT]+H8X$q'T!W*<CaL!T#dA,%777 ?? % %ae % 4D ?? % %ae % 4D&)!TNS!TN#C R<GT&\3  4<r-cxttttsJtttt sJtt ttsJttttsJttttsJttttsJy)z Check that the example symbols in this module print to their Theano equivalents, as many of the other tests depend on this. N)rFxtrrytrztrXtXYtYZtZr-rtest_example_symbolsr_s| LO $$ $ LO $$ $ LO $$ $ LO $$ $ LO $$ $ LO $$ $r-chtt}t|tjt j fsJ|jdk(sJ|jtjk(sJtttdi}|jdk(sJ|jtjk(sJy)z. Test printing a Symbol to a theano variable. r^FbroadcastablesN) rrrCttTensorVariabletsScalarVariable broadcastabler )xxxx2s r test_Symbolrks aB b2,,b.?.?@ AA A  r !! ! 77aff   q!X 7C    (( ( 88qvv  r-c|tt}t|tjsJ|j dk(sJy)z4 Test printing a MatrixSymbol to a theano variable. r N)rrYrCrdrerh)XXs rtest_MatrixSymbolrns5 aB b"++ ,, ,  ~ -- -r-cgd}|D]1}tt5ttt|iddd3y#1swY>xYw)z/ Test MatrixSymbol with invalid broadcastable. )r^raTTFFT)TTrbN)r ValueErrorrrY)bcsbcs rtest_MatrixSymbol_wrong_dimsrvsI OC4 J  4 Ar7 3 4 44 4 4s <A ctt}t|tjsJ|j dk(sJ|j dk(sJy)zG Test printing AppliedUndef instance, which works similarly to Symbol. r^f_tN)rrxrCrdrerhr )ftts rtest_AppliedUndefrzsH s C c2,, -- -    "" " 88u  r-cttz}t|}|jjt j jk(sJyN)rrrowneroprtensoraddrcomps rtest_addrs5 q5D  D ::==FMM-- -- -r-ctttjtt jt sJtttjtt jt sJyr|)rFrsysinrrdrUtanr^r-r test_trigrsL  RVVAY' 44 4  RVVAY' 44 4r-ctjtdztjtztj dt zz}t|}tjtdztjtztj dtzz}t||sJy)z; Test printing a complex expression with multiple symbols. N) rexprcosrlogrrrdrUrVrWrF)rrexpecteds r test_manyrs| 66!Q$" #bffQqSk 1D  Dvvb!ebffRj()BFF1R4L8H h  r-cdD]2}ttt|ijj|k(r2JtttdijjdvsJttdztdijjdk(sJtttztdtdijjdk(sJy) zA Test specifying specific data types through the dtype argument. )float32float64int8int16int32int64dtypesr )rrrrN)rrrEdtyper)rs r test_dtypersJFAq%j166<<EEEF 1h- 0 5 5 ; ;?U UU U Aq)n 5 : : @ @I MM M Aq)Q &B C H H N NR[ [[ [r-cpttfD]'}dD] }t|||ij|k(r J)y)zG Test the "broadcastables" argument when printing symbol-like objects. )r^rarpr rqrbN)rrxrrh)srus rtest_broadcastablesrsIXOH OB1b':HHBN NN OOr-cttz}gdgdgdgdgdg}|D]0\}}}t|t|t|i}|j|k(r0Jy)zG Test "broadcastable" attribute after applying element-wise binary op. )r^r^r^)rarara)rprara)rrr r )rqr r rbN)rrrrh)rcasesbc1bc2bc3rs rtest_broadcastingrse q5D &%77  E) S#D!S!S1AB!!S((()r-cttztz}t|}t |j j tjsJt|tjtjtsJyr|)rYr[r]rrCr}r~rdDotrFrXdotrZr\)rexpr_ts r test_MatMulrsV Q3q5D $ F flloorvv .. . r r* ++ +r-ctttjjj t jsJyr|)rCrrYTr}r~rd DimShuffler^r-rtest_Transposers+ l133'--00"-- @@ @r-cttztz}tt |j j tjsJyr|) rYr[r]rCrr}r~rdElemwisers r test_MatAddrs4 Q3q5D l4(..112;; ?? ?r-ctttjddz tj ddsJttt jtj ddsJy)Nrr)rFrrIntegerrdtrue_divr Halfr^r-rtest_RationalsrsS  RZZ]Q./Q1B CC C  QVV$bkk!Q&7 88 8r-cJttjddk(sJy)Nr)rrrr^r-r test_Integersrs  1 &! ++ +r-cntjd}ttj|sJy)Nn)rSymbolr factorial)rs rtest_factorialr s% #A  Q (( (r-c d}t|ttjtjt t d|t jtj ttsJy)Nc*tt|Sr|)r7r,rs rz!test_Derivative..s $8r-Fevaluate) rFrr DerivativerrrgradrdrU)simps rtest_DerivativersU 8D \"--q 1u"MNOV[[R01 33 3r-cdtttgttzg}|dddk(sJy)z, Test theano_function() with single output. rrNr!rr)rs rtest_theano_function_simplers*!Q!A#'A Q7a<)rrN) r!rrrAlinalgnormasarrayarangeastype)rriyys rtest_theano_function_numpyr!s!Q!A#A!"Iq) < >A 99>>!QFQF+bjj!Q.@@ AD HH H!Q!A#9a/K A 1  Y 'B 299Q<  y ) )B 99>>!B)a ! n4 5 << *+Axx#sc?%;<=H!QQC(AJJqc3/:!QQC5AJJqc3/:!QQF+A aS#&R 11 1JJqc3/2H=JJqc3/2H=r-cttgdtdik(sJtttgtdtditdtdik(sJttgtditdik(sJy) Nr)rr r)dimsrrrarb)rrrr^r-rtest_dim_handlingr:st  #>': :: : AaAq\ 2q-78.7J JJ J QM :q(m KK Kr-c ddl}ttttgttzgddtdtdtdi}|j j |ddgdd gddg|jd d gz d ksJttttgttzgtdtdtdidd }|jdjd}d|jdjdz}d|jdjdz}|j j ||||d|jdzz d ksJy) zU Test passing additional kwargs from theano_function() to theano.function(). rNrignorer)ron_unused_inputrrrrrr)rrr) numpyr!rrrrrrrr)rArrirzzs rtest_theano_function_kwargsr@s8!QQqSEq(y!Y9= ?A 99>>!QFQFQF3jbjj!Q6HH ID PP P!QQqSE !9aAyIx 9A 1  Y 'B 9299Q<  y ) )B 9299Q<  y ) )B 99>>!BB-!IBIIaL.8 9D @@ @r-c >ttgttzgddgfttgttzgddgfttgttzgtdtdidgfttgttzttz gdddgfttttgttzttzgdddgfg}|D]N\}}}}dD]@}t ||||}t |j tjjjsJ|j jDcgc]=}tj|jjDcgc]}|rdnd c}?} }}|| } t | t s| g} t#|t#| k(sJt%|| D]V\} } |r"| dk(rt | tj&r(Jt | tj(sJ| j*| k(rVJCQycc}wcc}}w)z2 Test the "scalar" argument to theano_function(). Nrrrrr)rrr)rrrYr[r!rCrrr0function_moduleFunction input_storagerAonesrErhr#lenziprBndarrayndim) argsrr in_dimsout_dimsrriru in_values out_valuesdvalues rtest_theano_function_scalarrQs Q!a%$$ Q!a%$$ Q!a%1aA,, Q!a%Q1v. Q1Aq1u~taV4  D/3+*(# +F wvNAa//1O1O1X1XY YY **88!&&2F2FGBbaGHIIJj$/(\ x=C O3 33*5 +5a1f%eRYY777&eRZZ888 ::?*? +% ++Hs2,H  H+ H H c&ttdy)za Passing an unknown keyword argument to theano_function() should raise an exception. c8ttgtdzgdS)Nrr)foobar)r!rr^r-rrz0test_theano_function_bad_kwarg..s/acU1Er-N)r Exceptionr^r-rtest_theano_function_bad_kwargr{s  9EFr-c Nttdddtdddk(sJd}tdtdi}|tttt|ttt sJ|ttdtd|tdtdsJy)NrrrcvdD]4}t||}t||}|||"|%yt||r4yy)N)startstopstepFT)getattrrF)s1s2attra1a2s r theq_sliceztest_slice..theq_slicesO- DT"BT"BzRZ bj "b\ r-rr)rslicerrrUrV)rrs r test_slicers aA '5Aq> 99 9 !W %F l5A;v>b" NN N l5Aq>&A5BPQ? SS Sr-c| ddlmi}tjdd}tjd||}|ddd d d d f}t ||  t jd t jjjt t fk(sJ jjdt || k(sJt fdtddDsJtjd}t ||did |d}}}||||}t ||d|di y)Nr)ConstantrTintegerrYrrrrrrrrc3|K|]3}jj|j|5ywr|)r}requals).0rrrZrs r z#test_MatrixSlice..s/NQrxxq!((!Q8Ns9<krr)rrrr MatrixSymbolrrfScalarrDr}r~idx_listrrallrange) rrrYr[rrr r rrZrs @@@rtest_MatrixSlicer$s! E #t$A Q"A !Aa%1Q,A au %B 'A %% &5Aq>5Aq>*J JJ J 88??1 au!= == = N%1+N NN N #AAw<(1a4E %T/A aGQ 8 9Br-c @ tjdd}dDcgc]}tj|||c}\}}}}tt||||f\}}}} tj ||g||gg} t |  t jdt jd||t jd|| t jdt jd||t jd|| g} t fd| DsJycc}w)NrTrABCDrrc36K|]}t|ywr|)rF)rsolutionBlockts rrz#test_BlockMatrix..s@(tFH%@s) rrrr$r BlockMatrixrdjoinany) rr ABCDAtBtCtDtBlock solutionsr)s @rtest_BlockMatrixr7s #t$A:@A$"//$1-AJAq!Q1a|4NBB NNQFQF+ ,E % FBGGAr2.2r0BCBGGAr2.2r0BCEI @i@ @@ @ BsDc  d\}}d}tjd||}tjd||}||f}|j|z}||dz|dzf|dz|dzfg||dz|dzf|dz|dzfgi}|Dcgc]}tj|g||} }|j t t || } t t ||gt|z} t||g| i} t|tj| g| i} |Dcgc]7}tjj|jj|9}}tj||zj!|jj|tj"|j|g}|dxxtj$|jdzz cc<tj&| || |d sJycc}wcc}w) N)rrrr-r.r)rrrgh㈵>)rtol)rrIblockcutsubsdictrrr!block_collapserArandomrandshaperrreshapeeyerallclose)rrrr-r.routputcutsizesr cutinputs cutoutputrrfblockedrninputss r"test_BlockMatrix_Inverse_executionrKs DAq E Q"A Q"A TF SSUFQT1a4L1a4A,/QT1a4L1a4A,/1H7=>!Q-!->I> DVY!789I #fugc&k12 3F&&CA):):9)E(F'-R9H@FF!ryy~~qww'..u5FGFyy1~%%agg.55e<vvay&(G AJ"''!''"4''J ;;q'{Hg$6T BB B?Gs 7 H>+,- .#g, >> >52 # % 5q."4 44 5 55 5 5s CC( ctjttd}t|}t |t t zsJt |t ttzrJy)zf Test that a symbol is cached and reused when it appears in an expression more than once. FrN)rAddrrrFrUrs rtest_symbols_are_created_oncerhsP 66!Q 'D  D b2g  D"|A.// //r-c|tdzttjtz tjt ttzz zz}|j Dchc]}|j}}t|}t}tjjj|gD]y}|jt|tjjj r?|j|vsJ|j|vsJ|j#|j{||k(sJycc}w)zf Test caching on a complicated expression with multiple symbols appearing multiple times. rN)rrrrrr free_symbolsr rr`rr%r& ancestorsr}rCrr)rr symbol_namesrseenvs rtest_cache_complexro's 6Q]bffQQY&77 7D$($5$56qAFF6L6 $ F 5D ZZ   ' ' 1 77?:a1A1A1J1J#K66\) ))66% %% HHQVV   <  7s!D9c .tjdtdkfttdkfd}t|}|jj t jk(sJt jtdkdt jtdktd}t||sJtjttdkf}t|}t jtdkttj}t||sJtjdtjtdkDtdkfttjtdkDtdkf}t|}t jt jtdkDtdkdt jt jtdkDtdkttj}t||sJy)Nrr)rTr)r Piecewiserrr}r~rdswitchrUrFrAnanAndOrand_or_)rresultrs rtest_Piecewisery?sq <<AaC1ac(I 6D $ F <7-  !NL9Q<0"bggi.AAAL$*BCS$q)9K0K,LMMMNN ee NNs/CA4C(C%(C1ctt5tgdg}ddddk(sJy#1swYxYw)Ny??)rr!)tfs rtest_constantfunctionsris9  !) b$ () 4::))s.7c2tjd}tj}tjj t |tjtjj t |tjt|}tjj t ||jt|}tjj t ||jy)zP Test that exp(1) prints without error and evaluates close to SymPy's E rN) rrErArrr@ereval)e_ae_brs r test_Exp1ros &&)C $$CJJuSz2440JJuSz2440SAJJuSz16684SAJJuSz16684r-)\__doc__loggingsympy.externalrsympy.testing.pytestrrr getLogger theanologgersetLevelCRITICALrWARNINGrrArrfrrdrUrVrWrXrZr\disabledsympyrsympy.core.singletonr sympy.abcrrrrrYrrrrrYr[r]rrxrr!r,r7rFr_rkrnrvrzrrrrrrrrrrrrrrrrrrrrrrr$r7rKrRrVr]rerhroryr}rrr)r rs00rrs8(EE w  !89 g&&' x  goo&  B B8=>"))D(+>JBBGLM!"))Hn1)=MJBBH"  .3 3?2??1a # 31akbkk#q+ :/$"-` % . 44. 5  \O)$, A@ 9,)3   = >L A"(+TGT$:2ACC2'3.$$50 0 0",0N 5m?M 4s G-G 6G