
    Ed                         d 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mZ g dZ ed	          Zes G d
 d          Znej        Z edddgi          Zes G d d          ZdZnej        Zej        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'd% Z(d& Z)d' Z*d( Z+dS ))z<Utilities to deal with sympy.Matrix, numpy and scipy.sparse.    )Expr)I)S
MatrixBase)eyezeros)import_module)numpy_ndarrayscipy_sparse_matrixsympy_to_numpysympy_to_scipy_sparsenumpy_to_sympyscipy_sparse_to_sympyflatten_scalarmatrix_daggerto_sympyto_numpyto_scipy_sparsematrix_tensor_productmatrix_zerosnumpyc                       e Zd ZdS )r   N__name__
__module____qualname__     Alib/python3.11/site-packages/sympy/physics/quantum/matrixutils.pyr   r              r   r   scipyfromlistsparse)import_kwargsc                       e Zd ZdS )r   Nr   r   r   r    r   r   &   r!   r   r   Nc                 n   t           st          |                    dd          }t          | t                    r.t                               |                                 |          S t          | t                    r(| j        s| j	        s| t          k    rt          |           S t          d| z            zBConvert a SymPy Matrix/complex number to a numpy matrix or scalar.dtypecomplexr)   z.Expected MatrixBase or complex scalar, got: %r)npImportErrorget
isinstancer   arraytolistr   	is_Numberis_NumberSymbolr   r*   	TypeErrormoptionsr)   s      r    r   r   .   s     KK++E!Z   xx

%x000	At		 ; 	!+ 	qAv 	1::
DqH
I
IIr   c                    t           rt          st          |                    dd          }t	          | t
                    rFt                              t                               |                                 |                    S t	          | t                    r(| j
        s| j        s| t          k    rt          |           S t          d| z            r(   )r,   r$   r-   r.   r/   r   
csr_matrixr0   r1   r   r2   r3   r   r*   r4   r5   s      r    r   r   ;   s     V KK++E!Z     !((**E!B!BCCC	At		 ; 	!+ 	qAv 	1::
DqH
I
IIr   c                 D    t          |                                           S )z0Convert a scipy.sparse matrix to a SymPy matrix.)r   todenser6   r7   s     r    r   r   H   s    aiikk"""r   c                      t          |           S )z)Convert a numpy matrix to a SymPy matrix.r   r<   s     r    r   r   M   s    a==r   c                    t          | t                    r| S t          | t                    rt          |           S t          | t                    rt          |           S t          | t                    r| S t          d| z            )z6Convert a numpy/scipy.sparse matrix to a SymPy matrix.1Expected sympy/numpy/scipy.sparse matrix, got: %r)r/   r   r   r   r   r   r   r4   r<   s     r    r   r   R   s    !Z   	A}	%	% a   	A*	+	+ $Q'''	At		 
G!K
L
LLr   c                 ,   |                     dd          }t          | t          t          f          rt	          | |          S t          | t
                    r| S t          | t                    r|                                 S t          d| z            )z6Convert a sympy/scipy.sparse matrix to a numpy matrix.r)   r*   r+   r?   )	r.   r/   r   r   r   r   r   r;   r4   r5   s      r    r   r   _   s    KK++E!j$'(( au----	A}	%	% 	A*	+	+ yy{{
G!K
L
LLr   c                 T   |                     dd          }t          | t          t          f          rt	          | |          S t          | t
                    r(t          st          t                              |           S t          | t                    r| S t          d| z            )z6Convert a sympy/numpy matrix to a scipy.sparse matrix.r)   r*   r+   r?   )r.   r/   r   r   r   r   r$   r-   r9   r   r4   r5   s      r    r   r   k   s    KK++E!j$'(( $Qe4444	A}	%	%  	  ###	A*	+	+ 
G!K
L
LLr   c                     t          | t                    r| j        dk    r| d         } t          | t          t          f          r | j        dk    rt          | d                   } | S )zCFlatten a 1x1 matrix to a scalar, return larger matrices unchanged.)   rC   r   )r   r   )r/   r   shaper   r   r*   es    r    r   r   y   si    !Z   7f 	!A!m%89:: !7f 	!$  AHr   c                     t          | t                    r| j        S t          | t          t          f          r&|                                                                 S t          d| z            )z7Return the dagger of a sympy/numpy/scipy.sparse matrix.r?   )r/   r   Hr   r   	conjugate	transposer4   rE   s    r    r   r      sa    !Z   )s
	A':;	<	< ){{}}&&(((
G!K
L
LLr   c                      ddl m}  ||  S )zCCompute the kronecker product of a sequence of SymPy Matrices.
    r   )matrix_kronecker_product)$sympy.matrices.expressions.kroneckerrL   )matricesrL   s     r    _sympy_tensor_productrO      s%     NMMMMM##X..r   c                      t           st          | d         }| dd         D ]}t                               ||          }|S )z6numpy version of tensor product of multiple arguments.r   rC   N)r,   r-   kronproductansweritems      r    _numpy_tensor_productrV      sJ     QZF ' '&&Mr   c                      t           st          | d         }| dd         D ]}t                               ||          }t                               |          S )z=scipy.sparse version of tensor product of multiple arguments.r   rC   N)r$   r-   rQ   r9   rR   s      r    _scipy_sparse_tensor_productrX      s[     QZF + +VT** V$$$r   c                      t          | d         t                    r	t          |  S t          | d         t                    r	t	          |  S t          | d         t
                    r	t          |  S dS )zGCompute the matrix tensor product of sympy/numpy/scipy.sparse matrices.r   N)r/   r   rO   r   rV   r   rX   )rS   s    r    r   r      sp    '!*j)) 6$g..	GAJ	.	. 6$g..	GAJ 3	4	4 6+W556 6r   c                     t           st          t                               t                               | d                    S )znumpy version of complex eye.r*   r+   )r,   r-   r0   r   ns    r    
_numpy_eyer]      s2     88BFF1IF..///r   c                 X    t           st          t                               | | d          S )z$scipy.sparse version of complex eye.r*   r+   )r$   r-   r   r[   s    r    _scipy_sparse_eyer_      s(     ::a):,,,r   c                     |                     dd          }|dk    rt          |           S |dk    rt          |           S |dk    rt          |           S t	          d|z            )z=Get the version of eye and tensor_product for a given format.formatsympyr   scipy.sparsezInvalid format: %r)r.   r   r]   r_   NotImplementedError)r\   r7   ra   s      r    
matrix_eyere      su    [[7++F $1vv	7	 $!}}	>	! $ ###
2V;
<
<<r   c                     |                     dd          }t          st          t                              | |f|          S )znumpy version of zeros.r)   float64r+   )r.   r,   r-   r	   )r6   r\   r7   r)   s       r    _numpy_zerosrh      s=    KK++E 88QF%8(((r   c                 
   |                     dd          }|                     dd          }t          st          |dk    rt                              | |f|          S |dk    rt                              | |f|          S dS )zscipy.sparse version of zeros.spmatrixcsrr)   rg   lilr+   N)r.   r$   r-   
lil_matrixr9   )r6   r\   r7   rj   r)   s        r    _scipy_sparse_zerosrn      s    {{:u--HKK++E 5 6  !Qu 555	U	 6  !Qu 5556 6r   c                     |                     dd          }|dk    rt          | |          S |dk    rt          | |fi |S |dk    rt          | |fi |S t	          d|z            )z'"Get a zeros matrix for a given format.ra   rb   r   rc   zInvaild format: %r)r.   r	   rh   rn   rd   )r6   r\   r7   ra   s       r    r   r      s    [[7++F 4Q{{	7	 4Aq,,G,,,	>	! 4"1a337333
2V;
<
<<r   c                     t           st          t                               |           }t                               | |          rdS | S )z/Convert a numpy zero matrix to the zero scalar.        )r,   r-   
zeros_likeallclose)rF   tests     r    _numpy_matrix_to_zeroru      sA     ==D	{{1d sr   c                     t           st          |                                 }t                               |          }t                               ||          rdS | S )z6Convert a scipy.sparse zero matrix to the zero scalar.rq   )r,   r-   r;   rr   rs   )rF   edensert   s      r    _scipy_sparse_matrix_to_zerorx      sN     YY[[F==  D	{{64   sr   c                     t          | t                    rt          | j         | k    rt          j        } nIt          | t                    rt          |           } n$t          | t                    rt          |           } | S )z)Convert a zero matrix to the scalar zero.)
r/   r   r	   rD   r   Zeror   ru   r   rx   rE   s    r    matrix_to_zeror{     su    !Z   ,!'?a 	A	A}	%	% ,!!$$	A*	+	+ ,(++Hr   ),__doc__sympy.core.exprr   sympy.core.numbersr   sympy.core.singletonr   sympy.matrices.matricesr   sympy.matricesr   r	   sympy.externalr
   __all__r,   r   ndarrayr"   r   r$   rj   r   r   r   r   r   r   r   r   r   rO   rV   rX   r   r]   r_   re   rh   rn   r   ru   rx   r{   r   r   r    <module>r      s   B B                         " " " " " " . . . . . . % % % % % % % % ( ( ( ( ( (  & ]7	          JMgj8*-EFFF *       FF\F /
J 
J 
J
J 
J 
J# # #
  

M 
M 
M	M 	M 	MM M M  M M M/ / /  	% 	% 	%6 6 60 0 0- - -	= 	= 	=) ) )	6 	6 	6	= 	= 	=  	 	 		 	 	 	 	r   