o
    jg                      @   sd  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 dZnejZedddgidZesRG dd dZdZnejZejZdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Z d)d* Z!d+d, Z"d-d. Z#d/d0 Z$d1d2 Z%d3d4 Z&d5d6 Z'd7d8 Z(d9d: Z)d;d< Z*d=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__ r   r   Y/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/quantum/matrixutils.pyr
          r
   scipyfromlistsparse)import_kwargsc                   @   r   )r   Nr   r   r   r   r   r   &   r   r   Nc                 K   sb   t st|dd}t| trt j|  |dS t| tr+| js'| j	s'| t
kr+t| S td|  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%   r   r   r   r   .   s   

r   c                 K   sl   t rtst|dd}t| trtt j|  |dS t| t	r0| j
s,| js,| tkr0t| S td|  r$   )r(   r"   r)   r*   r+   r   
csr_matrixr,   r-   r   r.   r/   r   r&   r0   r1   r   r   r   r   ;   s   

r   c                 K   s   t |  S )z0Convert a scipy.sparse matrix to a SymPy matrix.)r   todenser2   r3   r   r   r   r   H   s   r   c                 K   s   t | S )z)Convert a numpy matrix to a SymPy matrix.r   r6   r   r   r   r   M   s   r   c                 K   sL   t | tr| S t | trt| S t | trt| S t | tr | S td|  )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   r0   r6   r   r   r   r   R   s   



r   c                 K   sR   | dd}t| ttfrt| |dS t| tr| S t| tr#|  S td|  )z6Convert a sympy/scipy.sparse matrix to a numpy matrix.r%   r&   r'   r7   )	r*   r+   r   r   r   r
   r   r5   r0   r1   r   r   r   r   _   s   

r   c                 K   s\   | dd}t| ttfrt| |dS t| tr!tstt| S t| t	r(| S t
d|  )z6Convert a sympy/numpy matrix to a scipy.sparse matrix.r%   r&   r'   r7   )r*   r+   r   r   r   r
   r"   r)   r4   r   r0   r1   r   r   r   r   k   s   


r   c                 C   sD   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.)   r8   r   )r   r   )r+   r   shaper
   r   r&   er   r   r   r   y   s   


r   c                 C   s6   t | tr| jS t | ttfr|   S td|  )z7Return the dagger of a sympy/numpy/scipy.sparse matrix.r7   )r+   r   Hr
   r   	conjugate	transposer0   r:   r   r   r   r      s
   
r   c                  G   s   ddl m} ||  S )zCCompute the kronecker product of a sequence of SymPy Matrices.
    r   )matrix_kronecker_product)$sympy.matrices.expressions.kroneckerr?   )matricesr?   r   r   r   _sympy_tensor_product   s   rB   c                  G   s2   t st| d }| dd D ]}t ||}q|S )z6numpy version of tensor product of multiple arguments.r   r8   N)r(   r)   kronproductansweritemr   r   r   _numpy_tensor_product   s   rH   c                  G   s8   t st| d }| dd D ]}t ||}qt |S )z=scipy.sparse version of tensor product of multiple arguments.r   r8   N)r"   r)   rC   r4   rD   r   r   r   _scipy_sparse_tensor_product   s   
rI   c                  G   sF   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   rB   r
   rH   r   rI   )rE   r   r   r   r      s   r   c                 C   s   t stt t j| ddS )znumpy version of complex eye.r&   r'   )r(   r)   r,   r   nr   r   r   
_numpy_eye   s   rL   c                 C   s   t stt j| | ddS )z$scipy.sparse version of complex eye.r&   r'   )r"   r)   r   rJ   r   r   r   _scipy_sparse_eye   s   rM   c                 K   sH   | dd}|dkrt| S |dkrt| S |dkrt| S td| )z=Get the version of eye and tensor_product for a given format.formatsympyr   scipy.sparsezInvalid format: %r)r*   r   rL   rM   NotImplementedError)rK   r3   rN   r   r   r   
matrix_eye   s   rR   c                 K   s&   | dd}ts
ttj| |f|dS )znumpy version of zeros.r%   float64r'   )r*   r(   r)   r   )r2   rK   r3   r%   r   r   r   _numpy_zeros   s   rT   c                 K   sX   | dd}| dd}tst|dkrtj| |f|dS |dkr*tj| |f|dS dS )zscipy.sparse version of zeros.spmatrixcsrr%   rS   lilr'   N)r*   r"   r)   
lil_matrixr4   )r2   rK   r3   rU   r%   r   r   r   _scipy_sparse_zeros   s   rY   c                 K   s^   | dd}|dkrt| |S |dkrt| |fi |S |dkr)t| |fi |S td| )z'"Get a zeros matrix for a given format.rN   rO   r   rP   zInvaild format: %r)r*   r   rT   rY   rQ   )r2   rK   r3   rN   r   r   r   r      s   
r   c                 C   s&   t stt | }t | |rdS | S )z/Convert a numpy zero matrix to the zero scalar.        )r(   r)   
zeros_likeallclose)r;   testr   r   r   _numpy_matrix_to_zero   s   
r^   c                 C   s.   t st|  }t |}t ||rdS | S )z6Convert a scipy.sparse zero matrix to the zero scalar.rZ   )r(   r)   r5   r[   r\   )r;   edenser]   r   r   r   _scipy_sparse_matrix_to_zero   s   
r`   c                 C   sN   t | trt| j | krtj} | S t | trt| } | S t | tr%t	| } | S )z)Convert a zero matrix to the scalar zero.)
r+   r   r   r9   r   Zeror
   r^   r   r`   r:   r   r   r   matrix_to_zero  s   


rb   ),__doc__sympy.core.exprr   sympy.core.numbersr   sympy.core.singletonr   sympy.matrices.matrixbaser   sympy.matricesr   r   sympy.externalr	   __all__r(   r
   ndarrayr    r   r"   rU   r   r   r   r   r   r   r   r   r   rB   rH   rI   r   rL   rM   rR   rT   rY   r   r^   r`   rb   r   r   r   r   <module>   sP    


