
    sg                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ d d	lZ ed
      Z G d dee      Zd Zy	)    )version_tuple)Iterable)Mul)S)Sqrt)import_module)
PRECEDENCE)AbstractPythonCodePrinterArrayPrinterN
tensorflowc                       e Zd ZdZdZi ej                  dej                  dej                  dej                  dej                  dej                  ded	ej                  d
ej                  dej                  dej                   dej"                  dej$                  dej&                  dej(                  dej*                  dej,                  di ej.                  dej0                  dej2                  dej4                  dej6                  dej8                  dej:                  dej<                  dej>                  dej@                  dejB                  dejD                  dejF                  d ejH                  d!ejJ                  d"ejL                  d#ejN                  d$ejP                  d%ejR                  d&ejT                  d'ejV                  d(ejX                  d)ejZ                  d*iZ. e/e0jb                  d+,      Z1d> fd-	Z2 fd.Z3e3Z4e3Z5e3Z6e3Z7e3Z8e3Z9e3Z:e3Z;e3Z<e3Z=d/ Z>d0 Z?d1 Z@d2 ZAd3 ZBd4 ZCd5 ZDd6 ZEd7 ZFd8ZGd9ZHd:ZId;ZJd<ZKd=ZL xZMS )?TensorflowPrinterz
    Tensorflow printer which handles vectorized piecewise functions,
    logical operators, max/min, and relational operators.
    _tensorflowcodeztensorflow.math.absztensorflow.math.signztensorflow.math.ceilztensorflow.math.floorztensorflow.math.logztensorflow.math.exptensorflow.math.sqrtztensorflow.math.cosztensorflow.math.acosztensorflow.math.sinztensorflow.math.asinztensorflow.math.tanztensorflow.math.atanztensorflow.math.atan2ztensorflow.math.coshztensorflow.math.acoshztensorflow.math.sinhztensorflow.math.asinhztensorflow.math.tanhztensorflow.math.atanhztensorflow.math.realztensorflow.math.imagztensorflow.math.angleztensorflow.math.erfztensorflow.math.lgammaztensorflow.math.equalztensorflow.math.not_equalztensorflow.math.greaterztensorflow.math.lessztensorflow.math.less_equalztensorflow.math.greater_equalztensorflow.math.logical_andztensorflow.math.logical_orztensorflow.math.logical_notztensorflow.math.maximumztensorflow.math.minimumztensorflow.math.addztensorflow.math.multiplyztensorflow.linalg.traceztensorflow.linalg.detN)tensorflow_versionc                     t         |   |       | j                  d   }|t        rt        j                  }|| _        y )Nr   )super__init__	_settingsr   __version__r   )selfsettingsversion	__class__s      L/var/www/html/venv/lib/python3.12/site-packages/sympy/printing/tensorflow.pyr   zTensorflowPrinter.__init__Q   s9    "..!56?z ,,G")    c                 >   | j                   j                  t        |      d       }|t        |   |      S |j
                  D cg c]  }| j                  |       }}t        |      dk(  r| j                  |      d|d   dS | j                  ||      S c c}w )N   (r   ))
mappinggettyper   _print_Basicargs_printlen_module_format_expand_fold_binary_op)r   expropargchildrenr   s        r   _print_Functionz!TensorflowPrinter._print_FunctionY   s    \\d4j$/:7'--04		:DKK$::x=A##B' 
 ..r8<< ;s   Bc                 z    | j                  d      }dj                  || j                  |j                              S )Nztensorflow.linalg.inv{}({}))r(   formatr&   r,   )r   r*   r+   s      r   _print_Inversez TensorflowPrinter._print_Inverser   s1      !89r4;;txx#899r   c                     | j                   }|r)t        |      t        d      k  r| j                  d      }n| j                  d      }dj                  || j	                  |j
                              S )Nz1.14ztensorflow.matrix_transposez"tensorflow.linalg.matrix_transposer0   )r   r   r(   r1   r&   r,   )r   r*   r   r+   s       r   _print_Transposez"TensorflowPrinter._print_Transposev   sa    ))}W-f0EE$$%BCB$$%IJBr4;;txx#899r   c                      |j                   }t        d |D              rt        d       fd |j                  |      S )Nc              3   <   K   | ]  }t        |t                y wN)
isinstancer   ).0is     r   	<genexpr>z6TensorflowPrinter._print_Derivative.<locals>.<genexpr>   s     :1z!X&:s   z1derivation by multiple variables is not supportedc                     |sj                  |       S j                  d      d | |d d       dj                  |d         dS )Nztensorflow.gradientsr   , z)[0])r&   r(   )r*   r%   r   unfolds     r   r?   z3TensorflowPrinter._print_Derivative.<locals>.unfold   sN    {{4((''(>?4cr+KKR) r   )	variablesanyNotImplementedErrorr*   )r   r*   r@   r?   s   `  @r   _print_Derivativez#TensorflowPrinter._print_Derivative~   s>    NN	:	::%&YZZ	 dii++r   c                    | j                   }|rt        |      t        d      k  rd}nd}ddlm} |j                  d   j                  \  }}t        |j                        dk(  rAdj                  | j                  |      | j                  |      | j                  |      d      S dj                  | j                  |      | j                  |      | j                  |      | j                   ||j                  dd               S )Nz1.0ztensorflow.selectztensorflow.wherer   )	Piecewiser   z{}({}, {}, {}))	r   r   $sympy.functions.elementary.piecewiserE   r%   r'   r1   r(   r&   )r   r*   r   tensorflow_piecewiserE   econds          r   _print_Piecewisez"TensorflowPrinter._print_Piecewise   s    ))}W-e0DD#6 #5 B))A,##4tyy>Q#**##$89D!A	   && 45KKKKNKK	499QR=12	4 	4r   c                 :   |j                   \  }}|j                  t        j                  k(  r0dj	                  | j                  d      | j                  |            S dj	                  | j                  d      | j                  |      | j                  |            S )Nr0   r   z
{}({}, {})ztensorflow.math.pow)r%   expr   Halfr1   r(   r&   )r   r*   baserL   s       r   
_print_PowzTensorflowPrinter._print_Pow   s     II	c88qvv??##$:;T[[=NP P"" 56KKt{{3/1 	1r   c                 4   |j                   rdnd}ddj                  |j                         D cg c]6  }ddj                  |D cg c]  }| j                  |       c}      z   dz   8 c}}      z   dz   }| j	                  |      d|dS c c}w c c}}w )Nztensorflow.Variableztensorflow.constant[r>   ]r   r    )free_symbolsjointolistr&   r(   )r   r*   tensorflow_fr:   jdatas         r   _print_MatrixBasez#TensorflowPrinter._print_MatrixBase   s    040A0A,G\499VZVaVaVcdQRc$))Q,GT[[^,G"HHLdeefii-
 	
 -Hds   BBBBc                 T   ddl m} |j                  D cg c]  }t        ||      s| }}|j                  D cg c]	  }||vs| }}|rA| j	                  t        j                  |      t        d         d| j                  d|      S | j                  d|      S c c}w c c}w )Nr   )
MatrixExprr   *tensorflow.linalg.matmul)	sympy.matrices.expressionsr[   r%   r8   parenthesizer   fromiterr	   r)   )r   r*   r[   r,   mat_argsr%   s         r   _print_MatMulzTensorflowPrinter._print_MatMul   s    9#'99LC
3
0KCLL#yy@Cx,?@@!!#,,t"4j6GH++.:  ..*H6 6 M@s   B B 	B%B%c                 V    | j                  d|j                  g|j                  z        S )Nr]   )r)   rN   rL   )r   r*   s     r   _print_MatPowzTensorflowPrinter._print_MatPow   s)    **&DHH(<> 	>r   c                     g }|j                   D ]"  }|j                  | j                  |             $ dj                  |      S )N
)r%   appendr&   rT   )r   r*   retsubexprs       r   _print_CodeBlockz"TensorflowPrinter._print_CodeBlock   s=    yy 	-GJJt{{7+,	-yy~r   r   zlinalg.einsumzmath.add	transposeoneszerosr7   )N__name__
__module____qualname____doc__printmethodsympyAbssignceilingfloorlogrL   r   cosacossinasintanatanatan2coshacoshsinhasinhtanhatanhreimr,   erfloggammaEqNeStrictGreaterThanStrictLessThanLessThanGreaterThanAndOrNotMaxMinMatAddHadamardProductTraceDeterminantr!   dictr
   _default_settingsr   r.   _print_Expr_print_Application_print_MatrixExpr_print_Relational
_print_Not
_print_And	_print_Or_print_HadamardProduct_print_Trace_print_Determinantr2   r4   rC   rJ   rO   rY   rb   rd   rj   _module_einsum_add
_transpose_ones_zeros__classcell__)r   s   @r   r   r      s_    $K5		(5

*5
 	-5 	,5 			(5 			(5 	$5 			(5 	

*5 			(5 	

*5 			(5 	

*5  	,!5$ 	

*%5& 	,'5( 	

*)5* 	,+5, 	

*-5. 	,/52 	(354 	(556 			*75< 			(=5> 	0?5B 	)C5D 	-E5F 	!:G5H 	4I5J 	4K5L 	:M5P 			0Q5R 	.S5T 			0U5V 			,		, 	+9. 	3i5Gn !33
*= "K('' J JI,"L(::,4,	1
6> GGDJEFr   r   c                 :    t        |      }|j                  |       S r7   )r   doprint)r*   r   printers      r   tensorflow_coder      s    )G??4  r   )sympy.external.importtoolsr   collections.abcr   sympy.core.mulr   sympy.core.singletonr   sympy.codegen.cfunctionsr   sympy.externalr   sympy.printing.precedencer	   sympy.printing.pycoder
   r   rs   r   r   r    r   r   <module>r      sA    4 $  " ) ( 0 I <(
E&? EP!r   