K i|)ddlmZmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZddl mZdd l Z e d ZGd d eeZdd Zy ))AbstractPythonCodePrinter ArrayPrinter) MatrixExpr)Mul) PRECEDENCE) import_module)Sqrt)S)IntegerNtorchc eZdZdZiej dej dejdejdejdejde dejd ejd ejd ejd ej d ej"dej$dej&dej(dej*diej,dej.dej0dej2dej4dej6dej8dej:dej<dej>dej@dejBdejDdejFd ejHd!ejJd"ejLd#ejNd$ejPd%ejRd&ejTd'ejVd(ejXd)ejZd*iZ.e/e0jbd+d,d-.Z1dJfd/ Z2fd0Z3e3Z4e3Z5e3Z6e3Z7e3Z8e3Z9e3Z:e3Z;e3Zd2Z?d3Z@d4ZAd5ZBd6ZCd7ZDd8ZEd9ZFd:ZGd;ZHd<ZId=ZJd>ZKd?ZLd@ZMdAZNdBZOdCZPdDZQdEZRdFZSdGZTdHZUdIZVxZWS)K TorchPrinter _torchcodez torch.absz torch.signz torch.ceilz torch.floorz torch.logz torch.exp torch.sqrtz torch.cosz torch.acosz torch.sinz torch.asinz torch.tanz torch.atanz torch.atan2z torch.coshz torch.acoshz torch.sinhz torch.asinhz torch.tanhz torch.atanh torch.powz torch.realz torch.imagz torch.anglez torch.erfz torch.lgammaztorch.eqztorch.neztorch.gtztorch.ltztorch.leztorch.geztorch.logical_andztorch.logical_orztorch.logical_notz torch.maxz torch.minz torch.addz torch.mulz torch.tracez torch.detNF torch.float64) torch_version requires_graddtypect|||jd}|jd|_|jd|_|t rt j }||_y)Nrrr)super__init__ _settingsrrr __version__r)selfsettingsversion __class__s \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/printing/pytorch.pyrzTorchPrinter.__init__SsY "..1!^^O<^^G, ?u''G$c>|jjt|d}|t||S|j Dcgc]}|j |}}t|dk(r|j|d|ddS|j||Scc}w)N(r)) mappinggettyper _print_Basicargs_printlen_module_format_expand_fold_binary_op)rexpropargchildrenrs r_print_FunctionzTorchPrinter._print_Function]s \\  d4j$ / :7'- -04 :DKK$:: x=A ##B'   ..r8< <;sBc|dj|jd|j|jdS)N{}({})ztorch.linalg.invr)formatr,r*r)rr.s r_print_InversezTorchPrinter._print_Inversews5t223EF#{{499Q<8: :r c |jjr^|jjd|jjdk(r+djd|j |jSdj|j |jdj t t|jjDcgc] }t|c}dddScc}w)Nrr"z {}({}).t()ztorch.transposez{}.permute({}), ) r0 is_Matrixshaper5r*joinranger+str)rr.is r_print_TransposezTorchPrinter._print_Transpose{s 88  $((.."3txx~~a7H"H&&'8$++dhh:OP P$** DHH% 5TXX^^1D+EFa3q6FG"M FsC2c|j|jddjd|jjDdS)Nz .permute(r9c32K|]}t|ywN)r?).0r@s r z2TorchPrinter._print_PermuteDims..sBc!fBsr$)r*r.r= permutation array_formr6s r_print_PermuteDimszTorchPrinter._print_PermuteDimss: KK " IIBd&6&6&A&AB B  r c|j}|j}t|dkDs8t|dk(rt|dts|j |ddkDr|j |}i}|D]H}t|tr|\}}|j|d|z||<1|j|ddz||<J|jD]6\}}t|D]#} dj||j |}%8|St|dk(r|d} t| trrt| dk(rd| \}}t|ts td|j |}t|D]#} dj||j |}%|Sdj|j ||j | S|j |S)Nr"rz1torch.autograd.grad({}, {}, create_graph=True)[0]z!Only integer orders are supportedztorch.autograd.grad({}, {})[0]) variablesr.r+ isinstancetuplecountr*r&itemsr>r5r NotImplementedError) rr.rLexpr_argresult var_groupsvarbase_varorder_variables r_print_DerivativezTorchPrinter._print_DerivativesNN 99 y>A   Na  9Q<(GIOO\efg\hLilmLm[[*FJ! Ac5)&)OHe+5>>(A+F+NJx(&0nnS!&Q  |H(E*s8}/A"*%!%19LMp9q3qX.uwAPWWX^`d`k`klt`uvFw 3::4;;x;PRVR]R]^fRgh h{{8$$r c ddlm}|jdj\}}t|jdk(rAdj |j d|j ||j |dSdj |j d|j ||j ||j ||jddS)Nr) Piecewiser"z{}({}, {}, {})z torch.where)sympyr\r)r+r5r,r*)rr.r\econds r_print_PiecewisezTorchPrinter._print_Piecewises#))A,##4 tyy>Q #**##M2 D! A    &&    . KK  KKN KK 499QR=1 2 4 4r c:|j\}}|jtjk(r0dj |j d|j |Sdj |j d|j ||j |S)Nr4r {}({}, {})r)r)expr Halfr5r,r*)rr.basercs r _print_PowzTorchPrinter._print_PowsII c 88qvv ??##L14;;t3DF F""    , KK t{{3/1 1r cP|jDcgc]}t|ts|}}|jDcgc] }||vs| }}|rA|jt j |t dd|jd|S|jd|Scc}wcc}w)Nr*z torch.matmul)r)rMr parenthesizerfromiterrr-)rr.r0mat_argsr)s r _print_MatMulzTorchPrinter._print_MatMuls#'99LC 3 0KCLL#yy@Cx,?@@ !!#,,t"4j6GH++NHE  ..~xH HM@sBB B#B#cV|jd|jg|jzS)Nztorch.mm)r-rercr6s r _print_MatPowzTorchPrinter._print_MatPows$**: {4887KLLr cddj|jDcgc]6}ddj|Dcgc]}|j|c}zdz8c}}zdz}t|g}|j d|j |j r|j ddj|jddj|Scc}wcc}}w)N[r9]zdtype=zrequires_grad=Truer4z torch.tensor) r=tolistr*r?appendrrr5r,)rr.r@jdataparamss r_print_MatrixBasezTorchPrinter._print_MatrixBasesTYY\`\g\g\ijWXdii0KAQ0K&L Ls Rjkknqqd) tzzl+,    MM. /    / IIf   1LjsC$ C C$ C$ cFd|j|jddS)Nz torch.isnan(rr$r*r)r6s r _print_isnanzTorchPrinter._print_isnan#dkk$))A,78::r cFd|j|jddS)Nz torch.isinf(rr$ryr6s r _print_isinfzTorchPrinter._print_isinfr{r chtd|jDr=dj|jd|j |jdSdj|jd|j |jd|j |jdS)Nc34K|]}|jywrD) is_Integer)rEdims rrFz/TorchPrinter._print_Identity..s4#s~~4sr4z torch.eyerrbr")allr<r5r,r*r6s r_print_IdentityzTorchPrinter._print_Identitys 44 4??##K0 DJJqM*   &&##K0 DJJqM* DJJqM* r cvdj|jd|j|jS)Nr4z torch.zerosr5r,r*r<r6s r_print_ZeroMatrixzTorchPrinter._print_ZeroMatrixs1    . KK #  r cvdj|jd|j|jS)Nr4z torch.onesrr6s r_print_OneMatrixzTorchPrinter._print_OneMatrixs1    - KK #  r ch|jdd|j|jddS)Nz torch.conjr#rr$r,r*r)r6s r_print_conjugatezTorchPrinter._print_conjugate s4%%l34Adkk$))A,6O5PPQRRr cy)N1jr6s r_print_ImaginaryUnitz!TorchPrinter._print_ImaginaryUnitsr c|j|jddg}t|jdkDr!|j|jd|d<|jdd|dd|ddS)Nrz0.5r"ztorch.heavisider#r9r$)r*r)r+r,)rr.r)s r_print_HeavisidezTorchPrinter._print_Heavisidesu DIIaL)51 tyy>A kk$))A,/DG%%&78947)2d1gYaPPr ch|jdd|j|jddS)Nztorch.special.gammar#rr$rr6s r _print_gammazTorchPrinter._print_gammas6%%&;<=Qt{{499UVYYZ[[r c|jdtjk(r3|jdd|j |jddSt d)Nrztorch.special.digammar#r"r$z3PyTorch only supports digamma (0th order polygamma))r)r Zeror,r*rQr6s r_print_polygammazTorchPrinter._print_polygammasZ 99Q<166 !))*ABC1T[[QUQZQZ[\Q]E^D__`a a%&[\ \r r einsumaddtoneszerosrD)X__name__ __module__ __qualname__ printmethodr]Abssignceilingfloorlogrcr cosacossinasintanatanatan2coshacoshsinhasinhtanhatanhPowreimr0erfloggammaEqNeStrictGreaterThanStrictLessThanLessThan GreaterThanAndOrNotMaxMinMatAddHadamardProductTrace Determinantr%dictr_default_settingsrr2 _print_Expr_print_Application_print_MatrixExpr_print_Relational _print_Not _print_And _print_Or_print_HadamardProduct _print_Trace_print_Determinantr7rArIrZr`rfrlrnrwrzr}rrrrrrrr_module_einsum_add _transpose_ones_zeros __classcell__)rs@rrrs{K6 ;6 L6  | 6  ] 6  ;6  ;6 l6  ;6  L6  ;6  L6  ;6  L6  ]!6$  L%6&  ]'6(  L)6*  ]+6,  L-6.  ]/60  ;164 ,566 ,768  =96>  ;?6@ A6D *E6F *G6H I6J jK6L  M6N :O6R  &S6T $U6V  & ; ;  k { ] ;k6Gp!33 % ="K('' J JI,"L(:  %%N4 1 IM  ;;   SQ \] GG DJ E Fr rc Ft||d}|j|fi|S)N)rr)r)rdoprint)r.rrrprinters r torch_coder's'me$TUG 7??4 ,8 ,,r )Fr)sympy.printing.pycoderrsympy.matrices.expressionsrsympy.core.mulrsympy.printing.precedencersympy.externalrsympy.codegen.cfunctionsr r]r r r rrrr rrsAI10() gU