ó
àÆ÷Xc           @` s*  d  Z  d d l m Z m Z m Z d d l m Z d d l Z d d l j	 j
 Z d d l Z d d l Z d d l m Z d d l m Z m Z d d l m Z d d l Z d" d „ Z d	 e j f d
 „  ƒ  YZ e ƒ  Z d e f d „  ƒ  YZ e ƒ  Z d# d „ Z d e j f d „  ƒ  YZ e ƒ  Z d$ d „ Z d e j f d „  ƒ  YZ  e  ƒ  Z! e! Z" d e j f d „  ƒ  YZ# d d „ Z% d „  Z& i  e& _' d% d „ Z( d& Z) d „  Z* d e j f d „  ƒ  YZ+ d d „ Z, d' d „ Z- d e j f d „  ƒ  YZ. d( d d „ Z/ d  e j f d! „  ƒ  YZ0 e0 ƒ  Z1 d S()   sº   
This file contains auxiliary Ops, used during the compilation phase and Ops
building class (:class:`FromFunctionOp`) and decorator (:func:`as_op`) that
help make new Ops more rapidly.

i    (   t   absolute_importt   print_functiont   division(   t   OrderedDictN(   t   gof(   t	   iteritemst   integer_types(   t   xrangec         C` s   | | f t  j |  <d S(   s°  
    Tell ViewOp how to generate C code for a Theano Type.

    Parameters
    ----------
    type : Theano type
        It must be the Theano class itself and not an instance of the class.
    code : C code
        Returns a view for the Theano type 'type'. Use %(iname)s and %(oname)s
        for the input and output C variable names respectively.
    version
        A number indicating the version of the code, for cache.

    N(   t   ViewOpt   c_code_and_version(   t   typet   codet   version(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   register_view_op_c_code   s    R   c           B` si   e  Z d  Z i d g d 6Z i  Z d	 Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(
   sK   
    Returns an inplace view of the input. Used internally by Theano.

    i    c         C` s   t  j |  | g | j ƒ  g ƒ S(   N(   R   t   ApplyR
   (   t   selft   x(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt	   make_node6   s    c         C` s    | \ } | \ } | | d <d  S(   Ni    (    (   R   t   nodet   inpt   outR   t   z(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   perform9   s    		c         C` s   d |  j  j S(   Ns   %s(   t	   __class__t   __name__(   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   __str__>   s    c         C` s~   | \ } | \ } | d } | j  d j j }	 |	 |  j k r\ |  j |	 \ }
 } |
 t ƒ  St t |  ƒ j | | | | | ƒ S(   Nt   faili    (   t   inputsR
   R   R	   t   localst   superR   t   c_code(   R   R   t   nodenameR   R   t   subt   inamet   onameR   t   itypeR   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   A   s    		
c         C` s   g  } xl t  t |  j ƒ d d „  ƒD]L \ } \ } } | sX t j d | d d ƒd S| j t | ƒ | f ƒ q% Wt | ƒ S(   Nt   keyc         S` s   t  |  d ƒ S(   Ni    (   t   str(   t   pair(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   <lambda>S   s    s‚   Type %s has C code for ViewOp, but it has no version. You should add a 'version' keyword arg when calling register_view_op_c_code.t
   stackleveli   (    (   t   sortedR   R	   t   warningst   warnt   appendR%   t   tuple(   R   R   t   tt   ct   v(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   c_code_cache_versionN   s    	c         C` s   | S(   N(    (   R   R   t   input_shapes(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   infer_shape^   s    c         C` s   | S(   N(    (   R   t   argst   g_outs(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   grada   s    (    (   R   t
   __module__t   __doc__t   view_mapR	   t	   __props__R   R   R   R   R1   R3   R6   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   )   s   						t   OutputGuardc           B` s$   e  Z d  Z i d g d 6Z e Z RS(   sm  
    This op is used only internally by Theano.

    Only the AddDestroyHandler optimizer tries to insert them in the graph.

    This Op is declared as destructive while it is not destroying anything.
    It returns a view. This is used to prevent destruction of the output
    variables of a Theano function.

    There is a mechanism in Theano that should prevent this, but the use
    of OutputGuard adds a safeguard: it may be possible for some optimization
    run before the add_destroy_handler phase to bypass this mechanism, by
    making in-place optimizations.

    TODO: find a current full explanation.

    i    (   R   R7   R8   t   destroy_mapt   Falset   check_input(    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR;   g   s   c         C` s   | | f t  j |  <d S(   sª  
    Tell DeepCopyOp how to generate C code for a Theano Type.

    Parameters
    ----------
    typ : Theano type
        It must be the Theano class itself and not an instance of the class.
    code: C code
        Deep copies the Theano type 'typ'. Use %(iname)s and %(oname)s for the
        input and output C variable names respectively.
    version
        A number indicating the version of the code, for cache.

    N(   t
   DeepCopyOpR	   (   t   typR   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   register_deep_copy_op_c_code€   s    R?   c           B` sG   e  Z i  Z e Z d Z d  „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   c         C` s   d  S(   N(    (   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   __init__›   s    c         C` s   t  j |  | g | j ƒ  g ƒ S(   N(   R   R   R
   (   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ž   s    c         C` sM   t  | d d ƒ r. | d j ƒ  | d d <n t j | d ƒ | d d <d  S(   Ni    t   copy(   t   hasattrRC   t   deepcopy(   R   R   R4   t   outs(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ¡   s    c         C` s•   g  } xl t  t |  j ƒ d d „  ƒD]L \ } \ } } | sX t j d | d d ƒd S| j t | ƒ | f ƒ q% W| r‹ | j d ƒ n  t | ƒ S(   NR$   c         S` s   t  |  d ƒ S(   Ni    (   R%   (   R&   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR'   °   s    s‹   Type %s has C code for DeepCopyOp, but it has no version. You should add a 'version' keyword arg when calling register_deep_copy_op_c_code.R(   i   i   (    (   R)   R   R	   R*   R+   R,   R%   R-   (   R   R   R.   R/   R0   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR1   «   s    	c         C` s~   | \ } | \ } | d } | j  d j j }	 |	 |  j k r\ |  j |	 \ }
 } |
 t ƒ  St t |  ƒ j | | | | | ƒ S(   NR   i    (   R   R
   R   R	   R   R   R?   R   (   R   R   t   namet   inamest   onamesR    R!   R"   R   R#   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ¾   s    		
(    (   R   R7   R	   R=   R>   R:   RB   R   R   R1   R   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR?   ’   s   			
	c         C` s   | | f t  j |  <d S(   sÑ  
    Tell Shape Op how to generate C code for a Theano Type.

    Parameters
    ----------
    typ : Theano type
        It must be the Theano class itself and not an instance of the class.
    code : C code
        Returns a vector representing the shape for the Theano type 'typ'.
        Use %(iname)s and %(oname)s for the input and output C variable names
        respectively.
    version
        A number indicating the version of the code, for cache.

    N(   t   ShapeR	   (   R
   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   register_shape_c_codeÏ   s    RJ   c           B` sn   e  Z d  Z e Z i  Z e Z d	 Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(
   s^   
    L{Op} to return the shape of a matrix.

    Notes
    -----
    Non-differentiable.

    c         C` sI   t  | t j ƒ s' t j j | ƒ } n  t j |  | g t j j ƒ  g ƒ S(   N(   t
   isinstancet   theanot   Variablet   tensort   as_tensor_variableR   R   t   lvector(   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ö   s    c         C` s2   | \ } | \ } t  j | j d d ƒ| d <d  S(   Nt   dtypet   int64i    (   RM   t   _asarrayt   shape(   R   R   R   t   out_R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ý   s    		c         C` s   t  | d ƒ g g S(   Ni    (   t   len(   R   R   t	   in_shapes(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR3     s    c         C` s
   t  g g S(   N(   R=   (   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   connection_pattern  s    c         C` s   t  j j ƒ  ƒ  g S(   N(   RM   t   gradientt   DisconnectedType(   R   R   t   grads(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR6     s    c         C` s   d  g S(   N(   t   None(   R   R   t   eval_points(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   R_op  s    c         C` s~   | \ } | \ } | d } | j  d j j }	 |	 |  j k r\ |  j |	 \ }
 } |
 t ƒ  St t |  ƒ j | | | | | ƒ S(   NR   i    (   R   R
   R   R	   R   R   RJ   R   (   R   R   RG   RH   RI   R    R!   R"   R   R#   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR     s    		
c         C` s•   g  } xl t  t |  j ƒ d d „  ƒD]L \ } \ } } | sX t j d | d d ƒd S| j t | ƒ | f ƒ q% W| r‹ | j d ƒ n  t | ƒ S(   NR$   c         S` s   t  |  d ƒ S(   Ni    (   R%   (   R&   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR'   *  s    s   Type %s has C code for Shape, but it has no version. You should add a 'version' keyword arg when calling register_shape_c_code.R(   i   i   (    (   R)   R   R	   R*   R+   R,   R%   R-   (   R   R   R.   R/   R0   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR1   %  s    	(    (   R   R7   R8   t   Truet   _f16_okR	   R=   R>   R:   R   R   R3   RY   R6   R_   R   R1   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRJ   â   s   							t   Shape_ic           B` sw   e  Z d  Z e Z i  Z e Z d Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z RS(   s^   
    L{Op} to return the shape of a matrix.

    Notes
    -----
    Non-differentiable.

    t   ic         C` sa   t  | t j ƒ r0 | j t j j k s0 t ‚ n  | t | ƒ k sH t ‚ t | ƒ } | |  _	 d  S(   N(
   RL   t   npt   ndarrayRR   RM   RO   t   integer_dtypest   AssertionErrort   intRc   (   R   Rc   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRB   R  s
    c         C` s   d |  j  j |  j f S(   Ns   %s{%i}(   R   R   Rc   (   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   [  s    c         C` ss   t  | t j ƒ s$ t d | ƒ ‚ n  | j |  j k rQ t d | |  j f ƒ ‚ n  t j |  | g t j j ƒ  g ƒ S(   Ns&   x must be Variable with ndim attributes$   x has too few dimensions for Shape_i(	   RL   RM   RN   t	   TypeErrort   ndimRc   R   RO   t   lscalar(   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ^  s    c         C` sd   | \ } | \ } | d d  k rH t j | j |  j d d ƒ| d <n | j |  j | d d <d  S(   Ni    RR   RS   .(   R]   RM   RT   RU   Rc   (   R   R   R   RV   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   i  s
    		&c         C` s˜   g  } xo t  t |  j ƒ d d „  ƒD]O \ } \ } } } | s[ t j d | d d ƒd S| j t | ƒ | f ƒ q% W| rŽ | j d ƒ n  t | ƒ S(   NR$   c         S` s   t  |  d ƒ S(   Ni    (   R%   (   R&   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR'   v  s    sƒ   Type %s has C code for Shape_i, but it has no version. You should add a 'version' keyword arg when calling register_shape_i_c_code.R(   i   i   (    (   R)   R   R	   R*   R+   R,   R%   R-   (   R   R   R.   R/   t   ciR0   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR1   q  s    	c         C` sŽ   | \ } | \ } | d } |  j  }	 | j d j j }
 |
 |  j k rl |  j |
 \ } } } | | t ƒ  St t |  ƒ j | | | | | ƒ S(   NR   i    (	   Rc   R   R
   R   R	   R   R   Rb   R   (   R   R   RG   RH   RI   R    R!   R"   R   Rc   R#   R   R>   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   „  s    		
	c         C` s   d g S(   N(    (    (   R   R   R2   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR3   ’  s    c         C` s
   t  g g S(   N(   R=   (   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRY   •  s    c      	   C` s,   t  j j d |  d d d | d d d ƒ g S(   Nt   opt   x_posi    R   t   comments5   No gradient for the shape of a matrix is implemented.(   RM   RZ   t   grad_not_implemented(   R   R   R\   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR6     s    (   Rc   (   R   R7   R8   R`   Ra   R	   R=   R>   R:   RB   R   R   R   R1   R   R3   RY   R6   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRb   =  s   									c         ` s™   ˆ  d k r' t |  d ƒ r' |  j ‰  n  ˆ  rŽ t ˆ  d ƒ rŽ ˆ  j ‰ ˆ j ‰ ‡  ‡ ‡ ‡ f d †  ‰ |  ˆ k r‚ ˆ |  j ƒ n  ˆ |  | S|  j | S(   si  
    Equivalent of var.shape[i], but apply if possible the shape feature
    optimization.

    This is useful in optimization that need to get the shape. This
    remove the need of the following shape_feature optimization that
    convert it. So this speed up optimization and remove Equilibrium
    max iteration problems.

    Parameters
    ----------
    var
        The variable we want to take the shape of.
    i
        The shape dimensions we want
    fgraph : optional
        If var.fgraph do not exist, the fgraph that have the shape_feature to
        introduce var in to get the optimized shape.

    t   fgrapht   shape_featurec         ` sZ   |  j  d ˆ k rV x* |  j D] } | j r ˆ | j ƒ q q Wˆ j ˆ  |  d ƒ n  d  S(   Ni    s   gof.ops.shape_i(   t   outputsR   t   ownert	   on_import(   R   R   (   Rq   t   recurRr   t   shape_of(    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRv   ¿  s    	N(   R]   RD   Rq   Rr   Rw   Rt   RU   (   t   varRc   Rq   (    (   Rq   Rv   Rr   Rw   s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   shape_i¤  s    			c         C` s6   |  } | t  j k r+ t |  ƒ t  j | <n  t  j | S(   N(   t
   shape_i_opt   cacheRb   (   Rc   R$   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRz   Ò  s    c         C` s   | | | f t  j |  <d S(   sË  
    Tell Shape_i how to generate C code for a Theano Type.

    Parameters
    ----------
    typ : Theano type
        It must be the Theano class itself and not an instance of the class.
    code : C code
        Gets the shape of dimensions %(i)s for the Theano type 'typ'.
        Use %(iname)s and %(oname)s for the input and output C variable names
        respectively.
    version
        A number indicating the version of the code, for cache.

    N(   Rb   R	   (   R@   R   R>   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   register_shape_i_c_codeÚ  s    c         C` s6   t  |  ƒ d d  l } | j |  } t | | ƒ } | S(   Ni    (   t
   __import__t   syst   modulest   getattr(   t   modRG   R~   t   modulet   obj(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt	   load_backò  s
    
t   FromFunctionOpc           B` sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sç  
    Build a basic Theano Op around a function.

    Since the resulting Op is very basic and is missing most of the
    optional functionalities, some optimizations may not apply.  If you
    want to help, you can supply an infer_shape function that computes
    the shapes of the output given the shapes of the inputs.

    Also the gradient is undefined in the resulting op and Theano will
    raise an error if you attempt to get the gradient of a graph
    containing this op.

    c         C` sF   | |  _  | |  _ | |  _ | |  _ |  j d  k	 rB |  j |  _ n  d  S(   N(   t   _FromFunctionOp__fnt   itypest   otypest   _FromFunctionOp__infer_shapeR]   t   _infer_shapeR3   (   R   t   fnR‡   Rˆ   R3   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRB   	  s    				c         C` s(   t  |  ƒ t  | ƒ k o' |  j | j k S(   N(   R
   R†   (   R   t   other(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   __eq__  s    c         C` s   t  t |  ƒ ƒ t  |  j ƒ AS(   N(   t   hashR
   R†   (   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   __hash__  s    c         C` s   d |  j  j S(   Ns   FromFunctionOp{%s}(   R†   R   (   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR     s    c         C` s   |  j  | Œ  } t | t t f ƒ s0 | f } n  t | ƒ t | ƒ k sN t ‚ x, t t | ƒ ƒ D] } | | | | d <qa Wd  S(   Ni    (   R†   RL   t   listR-   RW   Rg   t   range(   R   R   R   Rs   RF   Rc   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR     s    c         C` s™   |  j  j } |  j  j } y t | | ƒ } Wn3 t t t f k
 r` t j d | | f ƒ ‚ n) X| |  k	 r‰ t j d | | f ƒ ‚ n  t | | f f S(   Ns(   Can't pickle as_op(), not found as %s.%ss-   Can't pickle as_op(), not the object at %s.%s(	   R†   R7   R   R„   t   ImportErrort   KeyErrort   AttributeErrort   picklet   PicklingError(   R   R   RG   Rƒ   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt
   __reduce__#  s    c         C` s   |  j  | | ƒ S(   N(   R‰   (   R   R   R2   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRŠ   3  s    (
   R   R7   R8   RB   R   R   R   R   R—   RŠ   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR…   ú  s   						c         ` så   t  ˆ t t f ƒ s! ˆ g ‰ n  t d „  ˆ Dƒ ƒ rF t d ƒ ‚ n  t  ˆ t t f ƒ sg ˆ g ‰ n  t d „  ˆ Dƒ ƒ rŒ t d ƒ ‚ n  t ˆ ƒ ‰ t ˆ ƒ ‰ ˆ  d k	 rÌ t ˆ  ƒ rÌ t d ƒ ‚ n  ‡  ‡ ‡ f d †  } | S(   s(  
    Decorator that converts a function into a basic Theano op that will call
    the supplied function as its implementation.

    It takes an optional infer_shape parameter that should be a callable with
    this signature:

        def infer_shape(node, input_shapes):
            ...
            return output_shapes

    Here `input_shapes` and `output_shapes` are lists of tuples that represent
    the shape of the corresponding inputs/outputs.

    This should not be used when performance is a concern since the very basic
    nature of the resulting Op may interfere with certain graph optimizations.

    Examples
    --------
    @as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix],
           otypes=[theano.tensor.fmatrix])
    def numpy_dot(a, b):
        return numpy.dot(a, b)

    c         s` s"   |  ] } t  | t j ƒ Vq d  S(   N(   RL   RM   t   Type(   t   .0R.   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pys	   <genexpr>S  s    s'   itypes has to be a list of Theano typesc         s` s"   |  ] } t  | t j ƒ Vq d  S(   N(   RL   RM   R˜   (   R™   R.   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pys	   <genexpr>W  s    s'   otypes has to be a list of Theano typess"   infer_shape needs to be a callablec         ` s   t  |  ˆ ˆ ˆ  ƒ S(   N(   R…   (   R‹   (   R3   R‡   Rˆ   (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   make_opa  s    N(   RL   R   R-   t   anyRi   R]   t   callable(   R‡   Rˆ   R3   Rš   (    (   R3   R‡   Rˆ   s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   as_op7  s    c         C` s   | | f t  j |  <d S(   s$  
    Tell Rebroadcast how to generate C code for a Theano Type.

    typ : Theano type
        It must be the Theano class itself and not an instance of the class.
    code : C code
        That checks if the dimension %(axis)s is of shape 1 for the Theano type
        'typ'. Use %(iname)s and %(oname)s for the input and output C variable
        names respectively, and %(axis)s for the axis that we need to check.
        This code is put in a loop for all axes.
    version
        A number indicating the version of the code, for cache.

    N(   t   RebroadcastR	   (   R@   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   register_rebroadcast_c_codef  s    Rž   c           B` s   e  Z d  Z i d g d 6Z e Z i  Z e Z d Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   sÏ  
    Change the input's broadcastable fields in some predetermined way.

    See Also
    --------
    unbroadcast <theano.tensor.unbroadcast>
    addbroadcast <theano.tensor.addbroadcast>
    patternbroadcast <theano.tensor.patternbroadcast>

    Notes
    -----
    Works inplace and works for CudaNdarrayType.

    Example
    -------
    `Rebroadcast((0, True), (1, False))(x)` would make `x` broadcastable in
    axis 0 and not broadcastable in axis 1.

    i    t   axisc         G` sŸ   t  | ƒ } t | ƒ |  _ x} t |  j ƒ D]l \ } } t | t j t f ƒ sg t d j	 | ƒ ƒ ‚ n  t | t j
 t f ƒ s+ t d j	 | ƒ ƒ ‚ q+ q+ Wd  S(   Ns&   Rebroadcast needs integer axes. Got {}s8   Rebroadcast needs bool for new broadcast pattern. Got {}(   R)   R   R    R   RL   Rd   t   integerR   Ri   t   formatt   bool_t   bool(   R   R    t   itemst   broad(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRB   —  s    		c         C` s1   t  t |  j ƒ ƒ } t t |  ƒ t | ƒ f ƒ S(   N(   R)   R   R    RŽ   R
   R-   (   R   R¥   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ¤  s    c         C` s£   t  |  j ƒ d k r g  } n2 g  t d t |  j j ƒ  ƒ ƒ D] } d ^ q> } x3 t |  j ƒ D]" \ } } t t | ƒ ƒ | | <q` Wd |  j j	 d j
 | ƒ f S(   Ni    i   t   ?s   %s{%s}t   ,(   RW   R    R   t   maxt   keysR   R%   Rh   R   R   t   join(   R   t   broadcast_patternRc   t   kR0   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ª  s    	/c         C` s¡   |  j  j ƒ  r< | j t |  j  j ƒ  ƒ k r< t d ƒ ‚ n  | j j d g  t | j j ƒ D]! \ } } |  j  j	 | | ƒ ^ q[ ƒ } t
 j |  | g | ƒ  g ƒ S(   Ns,   Trying to rebroadcast non-existent dimensiont   broadcastable(   R    Rª   Rj   R©   t
   ValueErrorR
   t   clonet	   enumerateR®   t   getR   R   (   R   R   Rc   t   bR.   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   µ  s    -:c         C` sy   | \ } | \ } xV t  |  j ƒ D]E \ } } | r" | j | d k r" t d | | j | f ƒ ‚ q" q" W| | d <d  S(   Ni   sI   Dimension %s in Rebroadcast's input was supposed to be 1 (got %s instead)i    (   R   R    RU   R¯   (   R   R   R   RV   R   R   R    t   value(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ½  s    		c         C` sW   | \ } | \ } t  g  t |  j ƒ D]" \ } } | | j j | f ^ q% Œ  | ƒ f S(   N(   Rž   R   R    R
   R®   (   R   R   R\   R   t   gzR    R´   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR6   Ç  s    		c         C` s›   t  | ƒ d k s t ‚ g  } t j j j d ƒ } xX t t  | d ƒ ƒ D]@ } |  j j | t	 ƒ ru | j
 | ƒ qJ | j
 | d | ƒ qJ Wt | ƒ g S(   Ni   i    (   RW   Rg   RM   RO   t   basict   constantR   R    R²   R=   R,   R-   (   R   R   t   ishapest   lt   onet   ax(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR3   Î  s    c         C` s-   | d d  k r d  g S|  | t d t ƒ Ž  S(   Ni    t   return_list(   R]   t   dictR`   (   R   R   R^   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR_   Ú  s    c         C` sÂ   | \ } | \ } | d } | j  d j j }	 |	 |  j k r  |  j |	 \ }
 } d } x7 t |  j ƒ D]& \ } } | rg | |
 t ƒ  7} qg qg W| d t ƒ  St t |  ƒ j	 | | | | | ƒ S(   NR   i    t    sv   
            Py_XDECREF(%(oname)s);
            %(oname)s = %(iname)s;
            Py_XINCREF(%(oname)s);
            (
   R   R
   R   R	   R   R    R   R   Rž   R   (   R   R   R   R   R   R    R!   R"   R   R#   R   R   t
   final_codeR    R´   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   ß  s    		
c         C` s•   g  } xl t  t |  j ƒ d d „  ƒD]L \ } \ } } | sX t j d | d d ƒd S| j t | ƒ | f ƒ q% W| r‹ | j d ƒ n  t | ƒ S(   NR$   c         S` s   t  |  d ƒ S(   Ni    (   R%   (   R&   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR'   ÷  s    s‹   Type %s has C code for Rebroadcast, but it has no version. You should add a 'version' keyword arg when calling register_rebroadcast_c_code.R(   i   i   (    (   R)   R   R	   R*   R+   R,   R%   R-   (   R   R   R.   R/   R0   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR1   ò  s    	(   s   axis(   R   R7   R8   R9   R`   Ra   R	   R=   R>   R:   RB   R   R   R   R   R6   R3   R_   R   R1   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRž   x  s    					
				c         C` s   | | | f t  j |  <d S(   sT  
    Tell SpecifyShape how to generate C code for a Theano Type.

    Parameters
    ----------
    typ : Theano type
        It must be the Theano class itself and not an instance of the class.
    code : C code
        Checks the shape and returns a view for the Theano type 'typ'.
        Use %(iname)s and %(oname)s for the input and output C variable names
        respectively. %(shape)s is the vector of shape of %(iname)s.
        Check that its length is good.
    version
        A number indicating the version of the code, for cache.
    c_support_code_apply
        Extra code.

    N(   t   SpecifyShapeR	   (   R@   R   R   t   c_support_code_apply(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   register_specify_shape_c_code  s    RÀ   c           B` s{   e  Z d  Z i d g d 6Z i  Z d Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z RS(   sB  
    L{Op} that puts into the graph the user-provided shape.

    In the case where this op stays in the final graph, we assert the shape.
    For this the output of this op must be used in the graph. This is not
    the case most of the time if we only take the shape of the output.
    Maybe there are other optimizations that will mess with this.

    Notes
    -----
    Maybe in the future we will never do the assert!

    We currently don't support specifying partial shape information.

    TODO : test this op with sparse and cuda ndarray. Do C code for them too.

    i    c         C` s¾   t  | t j ƒ s' t j j | ƒ } n  t j j | ƒ } | j d k sN t ‚ | j t j j	 k si t ‚ t  | t j j
 ƒ rœ | j j | j k sœ t ‚ n  t j |  | | g | j ƒ  g ƒ S(   Ni   (   RL   R   RN   RM   RO   RP   Rj   Rg   RR   Rf   t   TensorConstantt   datat   sizeR   R
   (   R   R   RU   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   8  s    c         C` sn   | \ } } | \ } | j  | j k s- t ‚ t j | j | k ƒ s` t d | j d | f ƒ ‚ | | d <d  S(   Ns	   got shapet   expectedi    (   Rj   RÅ   Rg   Rd   t   allRU   (   R   R   R   RV   R   RU   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   B  s    	$c         C` sÊ   | \ } } g  } x t  | j d j ƒ D]x } y@ t j j | j d | ƒ } t j j | ƒ } | j | ƒ Wq) t j j k
 r  | j | j d | ƒ q) Xq) Wt	 | ƒ t	 | ƒ k sÃ t
 ‚ | g S(   Ni    i   (   R   R   Rj   RM   RO   t   get_scalar_constant_valueRP   R,   t   NotScalarConstantErrorRW   Rg   (   R   R   t   shapest   xshapet   sshapet	   new_shapet   dimt   s(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR3   J  s    	 c         C` s   t  g t g g S(   N(   R`   R=   (   R   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRY   Y  s    c         C` sJ   | \ } } | \ } | t  j j ƒ  ƒ  g St | | ƒ t  j j ƒ  ƒ  g S(   N(   RM   RZ   R[   t   specify_shape(   R   R   R\   R   RÏ   Rµ   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR6   \  s    	c         C` s2   | d d  k r d  g S|  j | d | d Œ j S(   Ni    i   (   R]   R   Rs   (   R   R   R^   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR_   g  s    c         C` s^   | j  d j j } | |  j k rE |  j | \ } } } | rE | Sn  t t |  ƒ j | | ƒ S(   Ni    (   R   R
   R   R	   R   RÀ   RÁ   (   R   R   RG   R#   t   _t   support_code(    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRÁ   n  s    c         C` s„   | \ } } | \ } | d }	 | j  d j j }
 |
 |  j k rb |  j |
 \ } } } | t ƒ  St t |  ƒ j | | | | | ƒ S(   NR   i    (   R   R
   R   R	   R   R   RÀ   R   (   R   R   RG   RH   RI   R    R!   RU   R"   R   R#   R   R   RÑ   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR   v  s    	
c         C` s‚   g  } xo t  t |  j ƒ d d „  ƒD]O \ } \ } } } | s[ t j d | d d ƒd S| j t | ƒ | f ƒ q% Wt | ƒ S(   NR$   c         S` s   t  |  d ƒ S(   Ni    (   R%   (   R&   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR'   ˆ  s    sŽ   Type %s has C code for SpecifyShape, but it has no version. You should add a 'version' keyword arg when calling register_specify_shape_c_code.R(   i   (    (   R)   R   R	   R*   R+   R,   R%   R-   (   R   R   R.   R/   R0   RÑ   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyR1   ƒ  s    	(    (   R   R7   R8   R9   R	   R:   R   R   R3   RY   R6   R_   RÁ   R   R1   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyRÀ     s   	
							(    (    (    (    (    (    (    (2   R8   t
   __future__R    R   R   t   collectionsR   RC   t   six.moves.cPicklet   movest   cPickleR•   R*   RM   R   t   sixR   R   t	   six.movesR   t   numpyRd   R   t   OpR   t   view_opR;   t   _output_guardRA   R?   t   deep_copy_opRK   RJ   RU   t   _shapeRb   R]   Ry   Rz   R{   R|   t   expandable_typesR„   R…   R   RŸ   Rž   RÂ   RÀ   RÐ   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/compile/ops.pyt   <module>   sJ   ;		:	W	g.			=/Žw