σ
ΰΖχXc           @` s5  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m	 Z	 m
 Z
 m Z d  d l m Z d  d l Z d  d l Z d  d l m Z 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 m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# d  d l$ m% Z% d  d l& m' Z' d  d l( m) Z) y d  d l* Z+ e, Z- Wn e. k
 re/ Z- n Xd   Z0 d   Z1 d   Z2 d   Z3 e% d  d    Z4 d   Z5 d   Z6 d e j7 f d     YZ8 d   Z9 d   Z: d   Z; d   Z< d e j7 f d     YZ= d S(   i    (   t   absolute_importt   print_functiont   divisionN(   t   assert_array_almost_equal(   t   dect   assert_array_equalt   assert_allclose(   t   inf(   t   tensort   function(   t	   _allclose(   t   break_op(   t   unittest_tools(   t   config(
   t   Choleskyt   choleskyt   CholeskyGradt   Solvet   solvet   Eigvalsht   EigvalshGradt   eigvalsht   expmt   kron(   t   attr(   t   SkipTest(   t   assert_raisesc         C` s­   | |   } | d |  j  d d f d k s3 t  | |  j  d d d f d k sZ t  t j t j | | j  |   s t  t j t j | j |  |   s© t  d  S(   Ni    i   (   t   shapet   AssertionErrort   numpyt   allcloset   dott   T(   t   pdt   ch_ft   ch(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   check_lower_triangular"   s
    '''c         C` s   | |   } | d d k s" t   | d d k s8 t   t j t j | j |  |   s_ t   t j t j | | j  |   s t   d  S(   Ni   i    (   i   i    (   i    i   (   R   R   R   R   R    (   R!   R"   R#   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   check_upper_triangular*   s
    'c          c` s  t  s t d   n  t j j t j    }  |  j d d  j t	 j
  } t j | | j  } t j   } t |  } t | g |  } t | | f Vt d t  |  } t | g |  } t | | f Vt d t  |  } t | g |  } t | | f Vd  S(   Ns!   Scipy needed for the Cholesky op.i   t   lower(   t   imported_scipyR   R   t   randomt   RandomStatet   uttt
   fetch_seedt   randnt   astypeR   t   floatXR   R    R   t   matrixR   R	   R$   R   t   Truet   FalseR%   (   t   rngt   rR!   t   xt   cholR"   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_cholesky2   s    c           #` s   t  s t d   n  t j j t j      j d d  j t	 j
       f d   V   f d   V   f d   Vd  S(   Ns!   Scipy needed for the Cholesky op.i   c           ` s   t  j d     g d   S(   Nc         S` s   t  |  j |  j   S(   N(   R   R   R    (   R3   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   <lambda>Q   s    i   (   R*   t   verify_grad(    (   R3   R2   (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyR7   Q   s   c           ` s   t  j d     g d   S(   Nc         S` s   t  d t  |  j |  j   S(   NR&   (   R   R0   R   R    (   R3   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyR7   T   s    i   (   R*   R8   (    (   R3   R2   (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyR7   T   s   c           ` s   t  j d     g d   S(   Nc         S` s   t  d t  |  j |  j   S(   NR&   (   R   R1   R   R    (   R3   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyR7   W   s    i   (   R*   R8   (    (   R3   R2   (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyR7   W   s   (   R'   R   R   R(   R)   R*   R+   R,   R-   R   R.   (    (    (   R3   R2   s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_cholesky_gradH   s    t   slowc          c` sμ  t  s t d   n  t j j t j    }  t j   } x¬t	 |  t
 d t  |  t
 d t  |  f D]w} t j | g | j  } t j | j   |  } t j | g | j  } | j j j   } | j j j   } t j d k r_t g  | D] } | j j t
 k ^ qψ  d k s%t  t g  | D] } | j j t k ^ q/ d k s_t  n  x d d d g D]q }	 t j |  j |	 |	 d   j t j  }
 t j j  | |
  |	 |	 f f Vt j j  | |
  |	 |	 f f VqoWqm Wd  S(	   Ns!   Scipy needed for the Cholesky op.R&   t   FAST_COMPILEi    i   i   i   i
   (!   R'   R   R   R(   R)   R*   R+   R   R/   R   R   R0   R1   t   theanoR	   R   t   gradt   sumt   makert   fgrapht   toposortR   t   modet   opt	   __class__R   R   t   covR,   R-   R.   t   testingt   assert_equal(   R2   R4   t   lt   f_cholt   gt
   f_cholgradt	   topo_cholt   topo_cholgradt   nodet   shpt   m(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt%   test_cholesky_and_cholesky_grad_shape[   s&    :14+ c    	      C` sj  t  s t d   n  d d  l }  t j j d  } t j j d  } t | | g t | |   } t j	 j
 t j    } | j d d  } | | j } xh d t j d d  | j d d  g D]= } | | |  } |  j j | |  } t j j | |  qΔ Wd  } t j j } t | g t | |   } | |  } |  j j | |  } t j j | |  d  S(   Ns"   Scipy needed for the geigvalsh op.i    t   at   bi   i
   (   R'   R   t   scipy.linalgR<   R   t   dmatrixR	   R   R   R(   R)   R*   R+   R,   R    t   eyet   linalgRF   R   t   Nonet	   NoneConst(	   t   scipyt   At   Bt   fR2   RR   RS   t   wt   refw(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_eigvalshs   s&    0c          C` s€   t  s t d   n  d d  l }  t j j t j    } | j d d  } | | j	 } d t j
 d d  | j d d  } t j d   | | g d t j d  S(   Ns"   Scipy needed for the geigvalsh op.i    i   i
   c         S` s%   t  |  |  j d d d d d g  S(   Ni   i   i   i   i   (   R   R   (   RR   RS   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyR7      s    R2   (   R'   R   RT   R   R(   R)   R*   R+   R,   R    RV   R   R8   (   RZ   R2   RR   RS   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_eigvalsh_grad   s    &t
   test_Solvec           B` s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C` s,   t  t |   j   t |  _ t   |  _ d  S(   N(   t   superRb   t   setUpR   t   op_classRC   (   t   self(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyRd      s    	c         C` sn  t  s t d   n  t j j t j    } t j j	   } t j j	   } |  j
 | | g |  j | |  g t j | j d d  d t j t j | j d d  d t j g |  j d t t j j t j    } t j j	   } t j j   } |  j
 | | g |  j | |  g t j | j d d  d t j t j | j d  d t j g |  j d t d  S(   Ns   Scipy needed for the Solve op.i   t   dtypei   t   warn(   R'   R   R   R(   R)   R*   R+   R<   R   R/   t   _compile_and_checkRC   t   asarrayt   randR   R.   Re   R1   t   vector(   Rf   R2   R[   RS   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_infer_shape‘   s0    				c         C` s/  t  s t d   n  t j j t j    } t j j	   } t j j	   } |  j
 | |  } t j | | g |  } t d t  } | |  } |  j
 | |  } t j | | g |  }	 t d t  }
 |
 |  } |  j
 | |  } t j | | g |  } t j | j d d  d t j } t j | j d d  d t j } t j | j   |  } t j t j j | |  | | |   st  t j j | d t } t j t j j | | d t |	 | |   sέt  t j j | d t } t j t j j | | d t | | |   s+t  d  S(   Ns,   Scipy needed for the Cholesky and Solve ops.R&   i   i   Rg   (   R'   R   R   R(   R)   R*   R+   R<   R   R/   RC   R	   R   R0   R1   Rj   Rk   R   R.   R   t	   transposeR   RZ   RW   R   R   R   t   solve_triangular(   Rf   R2   R[   RS   t   yt   gen_solve_funct   cholesky_lowert   Lt   y_lowert   lower_solve_funct   cholesky_uppert   Ut   y_uppert   upper_solve_funct   b_valt   A_valt   L_valt   U_val(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_solve_correctness½   s4    $$c         C` s  t  s t d   n  d d d d d d d d	 d
 d d g } t j d  } t j d  } x‘ t j | |  D] \ } } t j d |  } t j d |  } t	 | |  } t
 | | g |  }	 |	 | j |  | j |   }
 | j |
 j k sm t  qm Wd  S(   Ns   Scipy needed for the Solve op.t   uint8t   uint16t   uint32t   uint64t   int8t   int16t   int32t   int64t   float16t   float32t   float64i   i   Rg   (   i   i   (   R'   R   R   RV   t   onest	   itertoolst   productR   R/   R   R	   R-   Rg   R   (   Rf   t   dtypesR{   Rz   t   A_dtypet   b_dtypeR[   RS   R4   t   fnt   x_result(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_solve_dtypeγ   s    !c   
      C` s  | j  d | | f  d t j |  j t j  } | d k rS t j |  } n | d k rq t j |  } n  | d  k r | j  d |  j t j  } n$ | j  d | | f  j t j  } d  } t j d k rΰ d } n  t	 d | d |  }	 t
 j |	 | | g d	 | d
 | d  S(   Nt   sizeg      ΰ?t   lower_triangulart   upper_triangularR   g:0βyU>t   A_structureR&   i   t   eps(   t   normalR   RV   R-   R   R.   t   trilt   triuRX   R   R*   R8   (
   Rf   RP   t   nR   R&   R2   R{   Rz   R   t   solve_op(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   verify_solve_gradψ   s    !$	c         C` sΗ   t  s t d   n  t j j t j    } d d d g } xe | D]] } | d k } |  j d d  | | |  |  j d d | | |  |  j d d	 | | |  qC W|  j d d	 d d
 t	 d | d  S(   Ns   Scipy needed for the Solve op.t   generalR   R   i   i   i   i   i   R&   R2   (
   R'   R   R   R(   R)   R*   R+   R   RX   R0   (   Rf   R2   t
   structuresR   R&   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_solve_grad  s    (   t   __name__t
   __module__Rd   Rm   R~   R   R   R    (    (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyRb      s   			&		c          C` sͺ   t  s t d   n  t j j t j    }  |  j d d  j t	 j
  } t j j |  } t j   } t |  } t | g |  } | |  } t j j | |  d  S(   Ns   Scipy needed for the expm op.i   (   R'   R   R   R(   R)   R*   R+   R,   R-   R   R.   RZ   RW   R   R   R/   R	   RF   R   (   R2   R[   t   refR4   RP   t   expm_ft   val(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt	   test_expm  s    c          C` si   t  s t d   n  t j j t j    }  |  j d d  } | | j } t	 j
 t | g d |  d  S(   Ns   Scipy needed for the expm op.i   R2   (   R'   R   R   R(   R)   R*   R+   R,   R    R   R8   R   (   R2   R[   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_expm_grad_1*  s    c          C` sΔ   t  s t d   n  t j j t j    }  |  j d d  } |  j d  d } t j | d  j	 | | j
  j	 t j | d   } t j | | j
  s§ t  t j t | g d |  d  S(   Ns   Scipy needed for the expm op.i   i   g      ΰ?g      ΰΏR2   (   R'   R   R   R(   R)   R*   R+   R,   t   diagR   R    R   R   R   R8   R   (   R2   R[   R^   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_expm_grad_26  s    9c          C` s\   t  s t d   n  t j j t j    }  |  j d d  } t j	 t
 | g d |  d  S(   Ns   Scipy needed for the expm op.i   R2   (   R'   R   R   R(   R)   R*   R+   R,   R   R8   R   (   R2   R[   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_expm_grad_3D  s
    t   TestKronc           B` s5   e  Z e j j d   Z d   Z d   Z d   Z RS(   i+   c         C` s    t  t |   j   t |  _ d  S(   N(   Rc   R«   Rd   R   RC   (   Rf   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyRd   S  s    c   
   	   C` s£  t  s t d   n  xd d d d g D]s} t j d d d t f t |   } t j |  j j |    j	 t
 j  } xd d d d g D]
} t |  t |  d k r΅ q n  t j d d d t f t |   } t | | g t | |   } |  j j |   j	 t
 j  } | | |  } t |  t |  d k rrt j j | t j d  d   f |  j   }	 n t j j | |  }	 t j | |	  q Wq( Wd  S(   Ns1   kron tests need the scipy package to be installedi   i   i   i   Rg   R.   t   broadcastablei   i   i   i	   (   i   (   i   i   (   i   i   i   (   i   i   i   i   (   i   (   i   i   (   i   i   i   (   i   i   i   i	   (   R'   R   R   R1   t   lenR   Rj   R2   Rk   R-   R   R.   R	   R   RZ   RW   t   newaxist   flattenR*   R   (
   Rf   t   shp0R4   RR   t   shp1Rp   R]   RS   t   outt	   scipy_val(    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_performW  s&    '	(c   	   	   C` s.  x'd g D]} t  j  d d d t f t |   } t j |  j j |    j t j	  } xΗ d	 g D]Ό } t |  t |  d k r qf n  t  j  d d d t f t |   } t
 | | g t | |   } |  j j |   j t j	  } | | |  } t j | t j | |   sf t  qf Wq
 Wd  S(
   Ni   i   Rg   R.   R¬   i   i   (   i   i   (   i   i   (   R   R1   R­   R   Rj   R2   Rk   R-   R   R.   R	   R   R   R   (	   Rf   R°   R4   RR   R±   Rp   R]   RS   R²   (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   test_numpy_2dp  s    '(	   R‘   R’   R   R(   R)   R2   Rd   R΄   R΅   (    (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyR«   O  s   		(>   t
   __future__R    R   R   t   unittestR   t   numpy.linalgt   numpy.testingR   R   R   R   R   R   R<   R   R	   t   theano.tensor.basicR
   t   theano.tests.test_ropR   t   theano.testsR   R*   R   t   theano.tensor.slinalgR   R   R   R   R   R   R   R   R   R   t   theano.tests.unittest_toolsR   t   nose.plugins.skipR   t
   nose.toolsR   RT   RZ   R0   R'   t   ImportErrorR1   R$   R%   R6   R9   RQ   R`   Ra   t   InferShapeTesterRb   R¦   R§   R©   Rͺ   R«   (    (    (    s@   /tmp/pip-build-X4mzal/theano/theano/tensor/tests/test_slinalg.pyt   <module>   sF   F

										