o
    8Va                      @   sl  d Z ddlmZmZmZ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sFG dd dZdZnejZeedrSejjZn	eedr\ej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-d. Z#d/d0 Z$d1d2 Z%d3d4 Z&d5d6 Z'd7d8 Z(d9d: Z)d;d< Z*dS )=z<Utilities to deal with sympy.Matrix, numpy and scipy.sparse.    )
MatrixBaseIExprInteger)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   C/usr/lib/python3/dist-packages/sympy/physics/quantum/matrixutils.pyr	          r	   scipyfromlistsparse)Zimport_kwargsc                   @   r   )r
   Nr   r   r   r   r   r
   #   r   r
   Nbasec                 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   matrixtolistr   	is_Numberis_NumberSymbolr   r%   	TypeErrormoptionsr$   r   r   r   r   1   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%   r/   r0   r   r   r   r   >   s   

r   c                 K   s   t |  S )z0Convert a scipy.sparse matrix to a sympy matrix.)r   todenser1   r2   r   r   r   r   K   s   r   c                 K   s   t | S )z)Convert a numpy matrix to a sympy matrix.)r   r5   r   r   r   r   P   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   r/   r5   r   r   r   r   U   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&   r6   )	r)   r*   r   r   r   r	   r
   r4   r/   r0   r   r   r   r   b   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&   r6   )r)   r*   r   r   r   r	   r!   r(   r3   r
   r/   r0   r   r   r   r   n   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.)   r7   r   )r   r   )r*   r   shaper	   r
   r%   er   r   r   r   |   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.r6   )r*   r   Hr	   r
   	conjugateZ	transposer/   r9   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)Z$sympy.matrices.expressions.kroneckerr=   )Zmatricesr=   r   r   r   _sympy_tensor_product   s   r>   c                  G   s2   t st| d }| dd D ]}t ||}q|S )z6numpy version of tensor product of multiple arguments.r   r7   N)r'   r(   kronproductZansweritemr   r   r   _numpy_tensor_product   s   rC   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   r7   N)r!   r(   r?   r3   r@   r   r   r   _scipy_sparse_tensor_product   s   
rD   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   r>   r	   rC   r
   rD   )rA   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   rG   c                 C   s   t stt j| | ddS )z$scipy.sparse version of complex eye.r%   r&   )r!   r(   r   rE   r   r   r   _scipy_sparse_eye   s   rH   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   rG   rH   NotImplementedError)rF   r2   rI   r   r   r   
matrix_eye   s   rM   c                 K   s&   | dd}ts
ttj| |f|dS )znumpy version of zeros.r$   float64r&   )r)   r'   r(   r   )r1   rF   r2   r$   r   r   r   _numpy_zeros   s   rO   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.spmatrixZcsrr$   rN   Zlilr&   N)r)   r!   r(   Z
lil_matrixr3   )r1   rF   r2   rP   r$   r   r   r   _scipy_sparse_zeros   s   rQ   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.rI   rJ   r   rK   zInvaild format: %r)r)   r   rO   rQ   rL   )r1   rF   r2   rI   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   
rV   c                 C   s.   t st|  }t |}t ||rdS | S )z6Convert a scipy.sparse zero matrix to the zero scalar.rR   )r'   r(   r4   rS   rT   )r:   ZedenserU   r   r   r   _scipy_sparse_matrix_to_zero   s   
rW   c                 C   sP   t | trt| j | krtd} | S t | trt| } | S t | tr&t| } | S )z)Convert a zero matrix to the scalar zero.r   )	r*   r   r   r8   r   r	   rV   r
   rW   r9   r   r   r   matrix_to_zero
  s   


rX   )+__doc__rJ   r   r   r   r   Zsympy.matricesr   r   Zsympy.externalr   __all__r'   r	   Zndarrayr   r
   r!   hasattrr"   rP   r   r   r   r   r   r   r   r   r   r>   rC   rD   r   rG   rH   rM   rO   rQ   r   rV   rW   rX   r   r   r   r   <module>   sP    





