ó
ąĘ÷Xc           @` s®  d  Z  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 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 m Z m Z m Z m Z m Z 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) m* Z* m+ Z+ m, Z, m- Z- m. Z. m/ Z/ m0 Z0 m1 Z1 m2 Z2 m3 Z3 m4 Z4 m5 Z5 m6 Z6 m7 Z7 m8 Z8 m9 Z9 m: Z: m; Z; m< Z< m= Z= m> Z> m? Z? m@ Z@ mA ZA d	   ZB d
 e jC f d     YZD d   ZE d e jC f d     YZF d e jC f d     YZG d eH f d     YZI d e jC f d     YZJ d e jC f d     YZK d   ZL d   ZM d   ZN d   ZO d   ZP d   ZQ eR d k rŖe jS   n  d S(   sŠ  
These routines are not well-tested. They are also old.
OB says that it is not important to test them well because Scalar Ops
are rarely used by themselves, instead they are the basis for Tensor Ops
(which should be checked thoroughly). Moreover, Scalar will be changed
to use numpy's scalar routines.
If you do want to rewrite these tests, bear in mind:
  * You don't need to use Composite.
  * FunctionGraph and DualLinker are old, use compile.function instead.
i    (   t   absolute_importt   print_functiont   divisionN(   t   FunctionGraph(   t   gof(   t   unittest_tools(   t   floatst   float16t   float32t   float64t   intst   int8t   int32t	   complex64t   uint8t   ComplexErrort   IntDivt   TrueDivt	   Compositet   addt	   div_proxyt   and_t   eqt   neqt   invertt   mult   Scalart   InRanget   castt   constantt   switch(   t   true_divt   invt   logt   log2t   log10t   log1pt   expt   exp2t   expm1t   sqrtt   deg2radt   rad2degt   cost   arccost   sint   arcsint   tant   arctant   arctan2t   cosht   arccosht   sinht   arcsinht   tanht   arctanhc           C` s
   t  d  S(   Nt   xyz(   R   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   inputs#   s    t   test_ScalarOpsc           B` s   e  Z d    Z d   Z RS(   c         C` s   t    \ } } } t t | |  t | |   } t | | g | g  } t j   j |  j   } | d d  d k s t	  d  S(   Ng      š?g       @g      ų?(
   R9   R   R   R   R   R   t
   DualLinkert   acceptt   make_functiont   AssertionError(   t   selft   xt   yt   zt   et   gt   fn(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_straightforward)   s
    !c         C` s«   t  d  \ } } t j   j t | | g | | g   j   } xa d d d d d d d d d d d d d f D]2 \ } } |  j | | |  | | k | f  qq Wd S(   sŅ   
        We add this test as not all language and C implementation give the same
        signe to the result. This check that the c_code of `Mod` is implemented
        as Python. That is what we want.
        t   xyi    i   i’’’’i   iž’’’i   i   iū’’’iż’’’N(   i    i   (   i   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;   R<   R   R=   t
   assertTrue(   R?   R@   RA   RE   t   at   b(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   tes_mod7   s    1(   t   __name__t
   __module__RF   RK   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyR:   '   s   	c         C` s$   t  d   |  j j D  r  t St S(   Nc         s` s   |  ] } | j  t k Vq d  S(   N(   t   typeR   (   t   .0t   v(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pys	   <genexpr>G   s    (   t   anyt   fgrapht	   variablest   Truet   False(   t   comp(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   has_f16F   s    t   test_compositec           B` sG   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   c   	      C` s  t    } t   } t   } t | | g t | t | d   g  } t | | | g | | |  | | |  d t | d  t | d  t | d  t t j d   g  } t |  sĒ t	  | j
   } t |  sę t	  t   } t   } t   } t   } t   } t | | | | | g t | t | | |  |  g  } t |  s^t	  | j
   } t |  s}t	  d  S(   NR   i   t   int16R   g      š?(   R   R   R   R   R6   R   R   t   npRW   R>   t   clone_float32R   R   R   (	   R?   t   wR@   RA   t   czt   ct   ncRP   RB   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_composite_clone_float32M   s&    			+D					9c   	      C` s²   t    \ } } } t t | |  t | |   } t | | g | g  } | j | |  } t | | g | j g  } t j	   j
 |  j   } | d d  d k s® t  d  S(   Ng      š?g       @g      ų?(   R9   R   R   R   R   t	   make_nodeR   t   outR   R;   R<   R=   R>   (	   R?   R@   RA   RB   RC   t   CR^   RD   RE   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyRF   f   s    !c         C` sŌ   t    \ } } } t | | g | | g  } t | | g | | | |  g  } t | j d j j t  sv t  t | | | g | | | |  | | | |  g  } t | j d j j t  sŠ t  d  S(   Ni    (   R9   R   t
   isinstancet   outputst   ownert   opR>   (   R?   R@   RA   RB   Rc   t   CC(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_flattenp   s    %#8c   	   	   C` sī   t    \ } } } t t d |  t | |   } t | | g | g  } | j | |  } d | j j | d d d g d g t d d   k s t	  t
 | | g | j g  } t j   j |  j   } | d	 d
  d k sź t	  d  S(   Ng     Q@s   70.0t   dummyR@   RA   RB   t   idi    g      š?g       @g      B@(   R9   R   R   R   R   Ra   Rg   t   c_codet   dictR>   R   Rb   R   R;   R<   R=   (	   R?   R@   RA   RB   RC   Rc   R^   RD   RE   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_with_constants|   s    !<c         C` sĻ   t    \ } } } | | | } | | | } | | } t | | | g | | | g  } | j | | |  } t | | | g | j  }	 t j   j |	  j   }
 |
 d d d  d d d g k sĖ t	  d  S(   Ng      š?g       @g      @g      @g      @g      ą?(
   R9   R   Ra   R   Re   R   R;   R<   R=   R>   (   R?   R@   RA   RB   t   e0t   e1t   e2Rc   R^   RD   RE   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_many_outputs   s    
!c      
   C` s  t  d  \ } } } | | | } | | | } | | } | d } | } | | }	 | | | }
 | d } t | | | g | | | | | |	 |
 | g  } | j | | |  } t | | | g | j  } t j   j |  j   t	 |  d k s t
  d  S(   NR8   i   i   sĖ   [*1 -> Composite{((i0 + i1) + i2), (i0 + (i1 * i2)), (i0 / i1), (i0 // Constant{5}), (-i0), (i0 - i1), ((i0 ** i1) + (-i2)), (i0 % Constant{3})}(x, y, z), *1::1, *1::2, *1::3, *1::4, *1::5, *1::6, *1::7](   R   R   Ra   R   Re   R   R;   R<   R=   t   strR>   (   R?   R@   RA   RB   Ro   Rp   Rq   t   e3t   e4t   e5t   e6t   e7Rc   R^   RD   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_composite_printing   s    



0c         C` s³   t  j j   } t  j j   } t  j j   } | | | } t  j j | | | g | g  } t  j j   } t  j j   } t  j j   } t  j j   } | j | | | |  d  S(   N(   t   theanot   scalarR   R   R   Ra   (   R?   t   si0t   si1t   si2t   soutt   sopt   si3(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_make_node_continue_graph©   s    (	   RL   RM   R`   RF   Ri   Rn   Rr   Ry   R   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyRX   L   s   		
				t   test_logicalc           B` sb   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z RS(
   c         C` s   t    \ } } } t j   j t | | g | | k g   j   } x? d d d f D]. \ } } |  j | | |  | | k k  qU Wd  S(   Ng      @i	   i   gĶĢĢĢĢĢģ?(   g      @i	   (   i   gĶĢĢĢĢĢģ?(   i   i   (   R9   R   R;   R<   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_gtæ   s    3c         C` s   t    \ } } } t j   j t | | g | | k  g   j   } x? d d d f D]. \ } } |  j | | |  | | k  k  qU Wd  S(   Ng      @i	   i   gĶĢĢĢĢĢģ?(   g      @i	   (   i   gĶĢĢĢĢĢģ?(   i   i   (   R9   R   R;   R<   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_ltÅ   s    3c         C` s   t    \ } } } t j   j t | | g | | k g   j   } x? d d d f D]. \ } } |  j | | |  | | k k  qU Wd  S(   Ng      @i	   i   gĶĢĢĢĢĢģ?(   g      @i	   (   i   gĶĢĢĢĢĢģ?(   i   i   (   R9   R   R;   R<   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_leĖ   s    3c         C` s   t    \ } } } t j   j t | | g | | k g   j   } x? d d d f D]. \ } } |  j | | |  | | k k  qU Wd  S(   Ng      @i	   i   gĶĢĢĢĢĢģ?(   g      @i	   (   i   gĶĢĢĢĢĢģ?(   i   i   (   R9   R   R;   R<   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_geŃ   s    3c         C` s   t    \ } } } t j   j t | | g t | |  g   j   } x? d d d f D]. \ } } |  j | | |  | | k k  qX Wd  S(   Ng      @i	   i   gĶĢĢĢĢĢģ?(   g      @i	   (   i   gĶĢĢĢĢĢģ?(   i   i   (   R9   R   R;   R<   R   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_eq×   s    6c         C` s   t    \ } } } t j   j t | | g t | |  g   j   } x? d d d f D]. \ } } |  j | | |  | | k k  qX Wd  S(   Ng      @i	   i   gĶĢĢĢĢĢģ?(   g      @i	   (   i   gĶĢĢĢĢĢģ?(   i   i   (   R9   R   R;   R<   R   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_neqŻ   s    6c         C` s   t  d  \ } } } t j   j t | | g | | Bg   j   } xI d d d d f D]5 \ } } |  j | | |  | | Bk | | f  qY Wd  S(   NR8   i    i   (   i    i   (   i    i    (   i   i    (   i   i   (   R
   R   R;   R<   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_orć   s    1c         C` s   t  d  \ } } } t j   j t | | g | | Ag   j   } xI d d d d f D]5 \ } } |  j | | |  | | Ak | | f  qY Wd  S(   NR8   i    i   (   i    i   (   i    i    (   i   i    (   i   i   (   R
   R   R;   R<   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_xoré   s    1c         C` s-  t  d  \ } } } t j   j t | | g t | |  g   j   } xI d d d d f D]5 \ } } |  j | | |  | | @k | | f  q^ Wt  d  \ } } } t j   j t | | g | | @g   j   } xI d d	 d
 d f D]5 \ } } |  j | | |  | | @k | | f  qš Wd  S(   NR8   i    i   (   i    i   (   i    i    (   i   i    (   i   i   (   i    i   (   i    i    (   i   i    (   i   i   (   R
   R   R;   R<   R   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_andļ   s    6-1c         C` s  t  d  \ } } } t j   j t | | g t |  g   j   } xC d d d d f D]/ \ } } |  j | | |  | k | f  q[ Wt  d  \ } } } t j   j t | | g | g   j   } xC d d	 d
 d f D]/ \ } } |  j | | |  | k | f  qä Wd  S(   NR8   i    i   (   i    i   (   i    i    (   i   i    (   i   i   (   i    i   (   i    i    (   i   i    (   i   i   (   R
   R   R;   R<   R   R   R=   RH   (   R?   R@   RA   RB   RE   RI   RJ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_notś   s    3'.(   RL   RM   R   R   R   R   R   R   R   R   R   R   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyR   ¾   s   									t   test_upgrade_to_floatc           B` s¦  e  Z e e e d  d   e e d d   f e e e d d   f e e e d d   f e e e d d   f e e e d d   f e	 e e d d   f e
 e e d  d   f e e e d  d   f e e e d  d   f e e e d  d   f e e e d  d   f e e e d  d   f e e e d d   f e e e d d	   f e e e d d   f e e e d  d   f e e e d d   f e e e d d	   f e e e d  d   f e e e d
 d   f e e e d  d   f e e e d  d   f e d g f g Z e e e d  d   e e d  d   f g Z e d    Z e d    Z  d   Z! d   Z" d   Z# RS(   i’’’i    i   i   i   iY   i’’’’i   i§’’’iZ   iż’’’i   c         C` sĆ   t  d  } t d  } |  |  } t j | g |  } |  |  } t j | g |  } xb | D]Z } | |  }	 | |  }
 |	 j |
 j k s t d   t j |	 |
  sa t d   qa Wd  S(   Nt   xit   xfs   incorrect dtypes   insufficient precision(   R   R   Rz   t   functiont   dtypeR>   RZ   t   allclose(   t   unary_opt   x_rangeR   R   t   eit   fit   eft   fft   x_valt   outit   outf(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   _test_unary-  s    c         C` sž   t  d  } t  d  } t d  } t d  } |  | |  } t j | | g |  } |  | |  }	 t j | | g |	  }
 xy | D]q } xh | D]` } | | |  } |
 | |  } | j | j k sŌ t d   t j | |  s t d   q Wq Wd  S(   NR   t   yiR   t   yfs   incorrect dtypes   insufficient precision(   R   R   Rz   R   R   R>   RZ   R   (   t	   binary_opR   t   y_rangeR   R   R   R   R   R   R   R   R   t   y_valR   R   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   _test_binary?  s    c         C` sS  t  t d d   } t  t d d   t  t d d   } t d  } t d  } t t j j  d  } t t j j  d	  } t | |  } t j | | g |  } t | |  }	 t j | | g |	  }
 xy | D]q } xh | D]` } | | |  } |
 | |  } | j	 | j	 k s)t
 d
   t j | |  sē t
 d   qē WqŚ Wd  S(   Ni’’’i   i    i   i   R   R   R   R   s   incorrect dtypes   insufficient precision(   t   listt   rangeR   R   Rz   t   configt   floatXR   R   R   R>   RZ   R   (   R?   R   R”   R   R   R   R   R   R   R   R   R   R¢   R   R   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_true_divT  s     (c         #` sN   xG   j  D]< \   d  j }     f d   } | | _ | Vq
 Wd  S(   Ns   test_%sc           ` s     j     d  S(   N(   R   (    (   R?   R   R   (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   testr  s    (   t   unary_ops_valst   namet   description(   R?   t	   test_nameR©   (    (   R?   R   R   s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt
   test_unarym  s
    	c         #` sT   xM  j  D]B \     d   j }      f d   } | | _ | Vq
 Wd  S(   Ns   test_%sc           ` s    j       d  S(   N(   R£   (    (   R    R?   R   R”   (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyR©   |  s    (   t   binary_ops_valsR«   R¬   (   R?   R­   R©   (    (   R    R?   R   R”   s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_binaryw  s
    	($   RL   RM   R    R¤   R„   R(   R!   R"   R#   R$   R%   R&   R'   R)   R*   R+   R,   R2   R3   R-   R.   R4   R5   R/   R0   R6   R7   RŖ   R1   RÆ   t   staticmethodR   R£   RØ   R®   R°   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyR     s:   +0		
t   test_complex_modc           B` s   e  Z d  Z d   Z RS(   s%   Make sure % fails on complex numbers.c         C` sB   t    } t   } y | | t s) t  Wn t k
 r= n Xd  S(   N(   R   R   RU   R>   R   (   R?   R@   RA   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt	   test_fail  s    		(   RL   RM   t   __doc__R³   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyR²     s   t   test_divc           B` s   e  Z d    Z RS(   c         C` sH  t    } t   } t   } t   } t   } t | | j j t  sL t	  t | | j j t  sk t	  t | | j j t
  s t	  t | | j j t
  s© t	  t | | j j t
  sČ t	  t | | j j t
  sē t	  t | | j j t
  st	  t | | j j t
  s%t	  t | | j j t
  sDt	  d  S(   N(   R   R   R   R	   R   Rd   Rf   Rg   R   R>   R   (   R?   RI   RJ   R^   t   dt   f(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_0  s    					(   RL   RM   Rø   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyRµ     s   c          C` se   t  d d  }  t  d d  } |  | k } t j j | |  } | j i d | 6 d k sa t  d  S(   NR«   R@   RA   g      š?g        (   R   Rz   t   gradientt   gradt   evalR>   (   R@   RA   RB   RD   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_grad_gt¢  s
    c          C` sY   t  j j   }  t  j j   } t  j j | |  d  } | j   } t  j j | |   d  S(   Ni    (   Rz   t   tensort   matrixR   t   sumR¹   Rŗ   (   R@   R^   t   st   l(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_grad_switchŖ  s
    c          C` sS   t  j j d  }  t  j j |   } | j d t  j j  } t  j j | |   d  S(   NR@   R   (	   Rz   R½   t   imatrixt   tensor_copyRæ   R¦   R§   R¹   Rŗ   (   R@   RA   RĮ   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_grad_identityŗ  s    c    
      C` s  xxt  t  f t t f g D]^}  t |    } t j j d  } t j j d  } t j j d  } | | | |  } t j j | | | | g  \ } } } t j | | | g | | | g  }	 t j	 |	 d d d  d d d g  t j	 |	 d d d  d d d g  t j	 |	 d d d  d d d g  t j	 |	 d d d  d d d g  t j	 |	 d d d  d d d g  q Wd  S(	   NR@   t   lowt   highi    i   i   i   i   (
   RT   RU   R   Rz   R½   t   fscalarRŗ   R   t   uttt   assert_allclose(
   t   bound_definitionRg   R@   RĘ   RĒ   Rb   t   gxt   glowt   ghighR·   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_grad_inrangeĆ  s    '
$%%%%c          C` s   t  j j d  }  t  j j j |   } t  j | |   } t  j |  g | d t  j d d   } | d  } | d k s t	 |   d  S(   NRI   t   modet	   optimizerg        g      ą?(
   Rz   R½   RČ   t   nnett   reluRŗ   R   t   Modet   NoneR>   (   RI   RJ   R^   R·   t   ret(    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_grad_absŻ  s    'c          C` s|   t  d d d }  |  j d k s' t  |  j d k s< t  t  d d d }  |  j d  k sc t  |  j d k sx t  d  S(   Ni   R«   RI   R   R   R   (   R   R«   R>   R   RÕ   (   R^   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   test_constantė  s    t   __main__(T   R“   t
   __future__R    R   R   t   unittestt   numpyRZ   Rz   t
   theano.gofR   R   t   theano.testsR   RÉ   t   theano.scalar.basicR   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   R0   R1   R2   R3   R4   R5   R6   R7   R9   t   TestCaseR:   RW   RX   R   t   objectR   R²   Rµ   R¼   RĀ   RÅ   RĻ   R×   RŲ   RL   t   main(    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/scalar/tests/test_basic.pyt   <module>
   s2     		rMw								