ó
~9­\c           @  s%  d  Z  d d l m Z m Z d d l m Z m Z m Z m Z d d l	 m
 Z
 d d l m Z m Z d d l m Z d d d	 d
 d d d d d d d d d g Z e d ƒ Z e sÌ d e f d „  ƒ  YZ n	 e j Z e d d i d g d 6ƒZ e sd e f d „  ƒ  YZ d2 Z nE e j Z e e d ƒ r=e j j Z n e e d ƒ r[e j j Z n  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/ d0 „  Z0 d1 „  Z1 d2 S(3   s<   Utilities to deal with sympy.Matrix, numpy and scipy.sparse.iÿÿÿÿ(   t   print_functiont   division(   t
   MatrixBaset   It   Exprt   Integer(   t   range(   t   eyet   zeros(   t   import_modulet   numpy_ndarrayt   scipy_sparse_matrixt   sympy_to_numpyt   sympy_to_scipy_sparset   numpy_to_sympyt   scipy_sparse_to_sympyt   flatten_scalart   matrix_daggert   to_sympyt   to_numpyt   to_scipy_sparset   matrix_tensor_productt   matrix_zerost   numpyc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR
      s   t   scipyt   __import__kwargst   sparset   fromlistc           B  s   e  Z RS(    (   R   R   (    (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   &   s   t   basec         K  s—   t  s t ‚ n  | j d d ƒ } t |  t ƒ rI t  j |  j ƒ  d | ƒSt |  t ƒ rƒ |  j sv |  j	 sv |  t
 k rƒ t |  ƒ Sn  t d |  ƒ ‚ d S(   sB   Convert a sympy Matrix/complex number to a numpy matrix or scalar.t   dtypet   complexs.   Expected MatrixBase or complex scalar, got: %rN(   t   npt   ImportErrort   gett
   isinstanceR   t   matrixt   tolistR   t	   is_Numbert   is_NumberSymbolR   R    t	   TypeError(   t   mt   optionsR   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   4   s    	c         K  s¨   t  s t r t ‚ n  | j d d ƒ } t |  t ƒ rZ t j t  j |  j ƒ  d | ƒƒ St |  t	 ƒ r” |  j
 s‡ |  j s‡ |  t k r” t |  ƒ Sn  t d |  ƒ ‚ d S(   sB   Convert a sympy Matrix/complex number to a numpy matrix or scalar.R   R    s.   Expected MatrixBase or complex scalar, got: %rN(   R!   R   R"   R#   R$   R   t
   csr_matrixR%   R&   R   R'   R(   R   R    R)   (   R*   R+   R   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   A   s    	"c         K  s   t  |  j ƒ  ƒ S(   s0   Convert a scipy.sparse matrix to a sympy matrix.(   R   t   todense(   R*   R+   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   N   s    c         K  s
   t  |  ƒ S(   s)   Convert a numpy matrix to a sympy matrix.(   R   (   R*   R+   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   S   s    c         K  sl   t  |  t ƒ r |  St  |  t ƒ r, t |  ƒ St  |  t ƒ rE t |  ƒ St  |  t ƒ rX |  St d |  ƒ ‚ d S(   s6   Convert a numpy/scipy.sparse matrix to a sympy matrix.s1   Expected sympy/numpy/scipy.sparse matrix, got: %rN(   R$   R   R
   R   R   R   R   R)   (   R*   R+   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   X   s    

c         K  sw   | j  d d ƒ } t |  t t f ƒ r7 t |  d | ƒSt |  t ƒ rJ |  St |  t ƒ rc |  j ƒ  St d |  ƒ ‚ d S(   s6   Convert a sympy/scipy.sparse matrix to a numpy matrix.R   R    s1   Expected sympy/numpy/scipy.sparse matrix, got: %rN(	   R#   R$   R   R   R   R
   R   R-   R)   (   R*   R+   R   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   e   s    
c         K  s‰   | j  d d ƒ } t |  t t f ƒ r7 t |  d | ƒSt |  t ƒ rb t sU t ‚ n  t j |  ƒ St |  t	 ƒ ru |  St
 d |  ƒ ‚ d S(   s6   Convert a sympy/numpy matrix to a scipy.sparse matrix.R   R    s1   Expected sympy/numpy/scipy.sparse matrix, got: %rN(   R#   R$   R   R   R   R
   R   R"   R,   R   R)   (   R*   R+   R   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   q   s    	c         C  sl   t  |  t ƒ r. |  j d k r. |  d }  q. n  t  |  t t f ƒ rh |  j d k rh t |  d ƒ }  qh n  |  S(   sC   Flatten a 1x1 matrix to a scalar, return larger matrices unchanged.i   i    (   i   i   (   i   i   (   i    i    (   R$   R   t   shapeR
   R   R    (   t   e(    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR      s    c         C  sO   t  |  t ƒ r |  j St  |  t t f ƒ r; |  j ƒ  j ƒ  St d |  ƒ ‚ d S(   s7   Return the dagger of a sympy/numpy/scipy.sparse matrix.s1   Expected sympy/numpy/scipy.sparse matrix, got: %rN(   R$   R   t   HR
   R   t	   conjugatet	   transposeR)   (   R/   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   Š   s
    c          G  s   d d l  m } | |  Œ  S(   sC   Compute the kronecker product of a sequence of sympy Matrices.
    iÿÿÿÿ(   t   matrix_kronecker_product(   t$   sympy.matrices.expressions.kroneckerR3   (   t   matricesR3   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _sympy_tensor_product”   s    c          G  sD   t  s t ‚ n  |  d } x$ |  d D] } t  j | | ƒ } q$ W| S(   s6   numpy version of tensor product of multiple arguments.i    i   (   R!   R"   t   kron(   t   productt   answert   item(    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _numpy_tensor_productœ   s    	
c          G  sM   t  s t ‚ n  |  d } x$ |  d D] } t  j | | ƒ } q$ Wt  j | ƒ S(   s=   scipy.sparse version of tensor product of multiple arguments.i    i   (   R   R"   R7   R,   (   R8   R9   R:   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _scipy_sparse_tensor_product¦   s    	
c          G  s[   t  |  d t ƒ r t |  Œ  St  |  d t ƒ r: t |  Œ  St  |  d t ƒ rW t |  Œ  Sd S(   sG   Compute the matrix tensor product of sympy/numpy/scipy.sparse matrices.i    N(   R$   R   R6   R
   R;   R   R<   (   R8   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   ²   s    

c         C  s+   t  s t ‚ n  t  j t  j |  d d ƒƒ S(   s   numpy version of complex eye.R   R    (   R!   R"   R%   R   (   t   n(    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt
   _numpy_eye¼   s    	c         C  s%   t  s t ‚ n  t  j |  |  d d ƒS(   s$   scipy.sparse version of complex eye.R   R    (   R   R"   R   (   R=   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _scipy_sparse_eyeÃ   s    	c         K  sh   | j  d d ƒ } | d k r( t |  ƒ S| d k r> t |  ƒ S| d k rT t |  ƒ St d | ƒ ‚ d S(   s=   Get the version of eye and tensor_product for a given format.t   formatt   sympyR   s   scipy.sparses   Invalid format: %rN(   R#   R   R>   R?   t   NotImplementedError(   R=   R+   R@   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt
   matrix_eyeÊ   s    


c         K  s:   | j  d d ƒ } t s! t ‚ n  t j |  | f d | ƒS(   s   numpy version of zeros.R   t   float64(   R#   R!   R"   R   (   R*   R=   R+   R   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _numpy_zerosÖ   s    	c         K  s   | j  d d ƒ } | j  d d ƒ } t s3 t ‚ n  | d k rX t j |  | f d | ƒS| d k r} t j |  | f d | ƒSd S(   s   scipy.sparse version of zeros.t   spmatrixt   csrR   RD   t   lilN(   R#   R   R"   t
   lil_matrixR,   (   R*   R=   R+   RF   R   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _scipy_sparse_zerosÞ   s    	c         K  s›   | j  d d ƒ } | j  d d ƒ } | j  d d ƒ } | d k rO t |  | ƒ S| d k rk t |  | |  S| d k r‡ t |  | |  St d	 | ƒ ‚ d
 S(   s'   "Get a zeros matrix for a given format.R@   RA   R   RD   RF   RG   R   s   scipy.sparses   Invaild format: %rN(   R#   R   RE   RJ   RB   (   R*   R=   R+   R@   R   RF   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyR   ê   s    c         C  s<   t  s t ‚ n  t  j |  ƒ } t  j |  | ƒ r4 d S|  Sd S(   s/   Convert a numpy zero matrix to the zero scalar.g        N(   R!   R"   t
   zeros_liket   allclose(   R/   t   test(    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _numpy_matrix_to_zeroø   s    	c         C  sH   t  s t ‚ n  |  j ƒ  } t  j | ƒ } t  j | | ƒ r@ d S|  Sd S(   s6   Convert a scipy.sparse zero matrix to the zero scalar.g        N(   R!   R"   R-   RK   RL   (   R/   t   edenseRM   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   _scipy_sparse_matrix_to_zero  s    	c         C  sv   t  |  t ƒ r6 t |  j Œ  |  k rr t d ƒ }  qr n< t  |  t ƒ rT t |  ƒ }  n t  |  t ƒ rr t |  ƒ }  n  |  S(   s)   Convert a zero matrix to the scalar zero.i    (	   R$   R   R   R.   R   R
   RN   R   RP   (   R/   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   matrix_to_zero  s    N(2   t   __doc__t
   __future__R    R   RA   R   R   R   R   t   sympy.core.compatibilityR   t   sympy.matricesR   R   t   sympy.externalR	   t   __all__R!   t   objectR
   t   ndarrayR   R   t   NoneR   t   hasattrR   RF   R   R   R   R   R   R   R   R   R   R6   R;   R<   R   R>   R?   RC   RE   RJ   R   RN   RP   RQ   (    (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/matrixutils.pyt   <module>   sj   "													
		
		
								