
Xc           @` s  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l m Z d  d l Z d  d l	 m
 Z
 d  d l	 m Z d  d l m Z m Z d  d l m Z d  d l m Z d  d	 l m Z m Z e
 Z d
 e j f d     YZ e   Z d e j f d     YZ d d d  Z d e j f d     YZ d d  Z d d  Z d e j f d     YZ d e j f d     YZ d e j f d     YZ  d d d  Z! d d e" d  Z# d   Z$ d d  Z% d  e j f d!     YZ& d d"  Z' d# e j f d$     YZ( e(   Z) d%   Z* d& e j f d'     YZ+ e+   Z, d(   Z- d) e j f d*     YZ. e.   Z/ d+   Z0 d d,  Z1 d- e j f d.     YZ2 d S(/   i    (   t   absolute_importt   print_functiont   divisionN(   t   xrange(   t   basic(   t   nlinalg(   t   goft   scalar(   t   Generic(   t   gradient(   t   DisconnectedTypet   disconnected_typet   CpuContiguousc           B` sQ   e  Z d  Z d Z i d g d 6Z d   Z d   Z d   Z d   Z d   Z	 RS(   sn   
    Check to see if the input is c-contiguous,
    if it is, do nothing, else return a contiguous array.
    i    c         C` s1   t  j j |  } t  j |  | g | j   g  S(   N(   t   theanot   tensort   as_tensor_variablet   Applyt   type(   t   selft   xt   x_(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt	   make_node   s    c         C` sP   | \ } | d } | j  d s/ | j   } n  | j  d sB t  | | d <d  S(   Ni    t   C_CONTIGUOUS(   t   flagst   copyt   AssertionError(   R   t   nodet   inputst   output_storageR   t   y(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   perform   s    	
c         C` s   t  j j | d  g S(   Ni    (   R   R   R   (   R   R   t   dout(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   grad'   s    c   	      C` s#   | \ } | \ } d t    } | S(   Ns  
            if (!PyArray_CHKFLAGS(%(x)s, NPY_ARRAY_C_CONTIGUOUS)){
                // check to see if output is contiguous first
                if (%(y)s != NULL &&
                    PyArray_CompareLists(PyArray_DIMS(%(y)s), PyArray_DIMS(%(x)s), PyArray_NDIM(%(x)s)) &&
                    PyArray_CHKFLAGS(%(y)s, NPY_ARRAY_C_CONTIGUOUS)){
                    PyArray_CopyInto(%(y)s, %(x)s);
                }
                else{
                    Py_XDECREF(%(y)s);
                    %(y)s = PyArray_GETCONTIGUOUS(%(x)s);
                }
            }
            else{
                Py_XINCREF(%(x)s);
                Py_XDECREF(%(y)s);
                %(y)s = %(x)s;
            }
            (   t   locals(	   R   R   t   namet   inamest   onamest   subR   R   t   code(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   c_code*   s    		c         C` s   d S(   Ni   (   i   (    (   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   c_code_cache_versionB   s    (    (
   t   __name__t
   __module__t   __doc__t	   __props__t   view_mapR   R   R    R'   R(   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR      s   		
		t   SearchsortedOpc           B` s}   e  Z d  Z e   Z d Z d d  Z d   Z d d  Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z RS(   s   Wrapper of numpy.searchsorted.

    For full documentation, see :func:`searchsorted`.

    See Also
    --------
    searchsorted : numpy-like function to use the SearchsortedOp

    t   sidet   leftc         C` s;   | d k s | d k r$ | |  _  n t d t     d  S(   NR0   t   rights1   '%(side)s' is an invalid value for keyword 'side'(   R/   t
   ValueErrorR!   (   R   R/   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   __init__V   s    c         C` s   |  j  S(   N(   R/   (   R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt
   get_params]   s    c         C` s   t  j | d d } t  j |  } | j j d d  } | d  k rd t j |  | | g |   g  St  j | d d } t j j   d k r | j	 d k r t
 d   n  | j t  j k r t
 d | j   n  t j |  | | | g |   g  Sd  S(   Nt   ndimi   t   dtypet   int64i    sF   numpy.searchsorted with Python 32bit do not support a sorter of int64.s    sorter must be an integer vector(   R   t	   as_tensorR   t   clonet   NoneR   R   t   configdefaultst   python_int_bitwidthR6   t	   TypeErrort   int_vector_types(   R   R   t   vt   sortert   out_type(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   `   s    c         C` s   | d g S(   Ni   (    (   R   R   t   shapes(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   infer_shaper   s    c   	      C` s   | d } | d } t  | j  d k r6 | d } n d  } | d } t j | | d | d | j | j d j  | d <d  S(   Ni    i   i   i   R/   R@   (   t   lenR   R:   t   npt   searchsortedt   astypet   outputsR6   (	   R   R   R   R   t   paramsR   R?   R@   t   z(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   u   s    


c         C` s   d t    S(   Ns)   
            int right_%(name)s;
        (   R!   (   R   R   R"   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   c_support_code_struct   s    c         C` s   | d } | d } d t    S(   NRI   t   fails   
            PyObject* tmp_%(name)s = PyUnicode_FromString("right");
            if (tmp_%(name)s == NULL)
                %(fail)s;
            right_%(name)s = PyUnicode_Compare(%(side)s, tmp_%(name)s);
            Py_DECREF(tmp_%(name)s);
        (   R!   (   R   R   R"   R%   R/   RL   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   c_init_code_struct   s    

c         C` sf   d  } t | j  d k r- | \ } } } n | \ } } | sH d } n  | \ }	 | d }
 d t   S(   Ni   t   NULLRL   s  
            Py_XDECREF(%(z)s);
            %(z)s = (PyArrayObject*) PyArray_SearchSorted(%(x)s, (PyObject*) %(v)s,
                                                          right_%(name)s ? NPY_SEARCHLEFT : NPY_SEARCHRIGHT, (PyObject*) %(sorter)s);
            if (!%(z)s)
                %(fail)s;
            if (PyArray_TYPE(%(z)s) != NPY_INT64){
                PyObject * tmp = PyArray_Cast(%(z)s, NPY_INT64);
                Py_XDECREF(%(z)s);
                %(z)s = (PyArrayObject*) tmp;
            }
        (   R:   RD   R   R!   (   R   R   R"   R#   R$   R%   R@   R   R?   RJ   RL   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR'      s    		
c         C` s   d S(   Ni   (   i   (    (   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR(      s    c   	      C` s~   t  |  } | d k r* | \ } } } n | \ } } t j |  } t j |  } | d k rp | | t   g S| | g Sd  S(   Ni   (   RD   R	   t   _float_zeros_likeR   (	   R   R   t   output_gradientst   num_insR   R?   R@   t   x_gradt   v_grad(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR       s    (   s   sideN(   R)   R*   R+   R   t   params_typeR,   R3   R4   R:   R   RC   R   RK   RM   R'   R(   R    (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR.   H   s   									R0   c         C` s   t  d |  |  | |  S(   sg  Find indices where elements should be inserted to maintain order.

    Wrapping of numpy.searchsorted. Find the indices into a sorted array
    `x` such that, if the corresponding elements in `v` were inserted
    before the indices, the order of `x` would be preserved.

    Parameters
    ----------
    x: 1-D tensor (array-like)
        Input array. If `sorter` is None, then it must be sorted in
        ascending order, otherwise `sorter` must be an array of indices
        which sorts it.
    v: tensor (array-like)
        Contains the values to be inserted into `x`.
    side: {'left', 'right'}, optional.
        If 'left' (default), the index of the first suitable
        location found is given. If 'right', return the last such index. If
        there is no suitable index, return either 0 or N (where N is the length
        of `x`).
    sorter: 1-D tensor of integers (array-like), optional
        Contains indices that sort array `x` into ascending order.
        They are typically the result of argsort.

    Returns
    -------
    indices : tensor of integers (int64)
        Array of insertion points with the same shape as `v`.

    See Also
    --------
    `numpy.searchsorted <https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.searchsorted.html>`_

    Notes
    -----
    * Binary search is used to find the required insertion points.
    * This Op is working **only on CPU** currently.

    Examples
    --------
    >>> from theano import tensor
    >>> x = tensor.dvector()
    >>> idx = x.searchsorted(3)
    >>> idx.eval({x: [1,2,3,4,5]})
    array(2)
    >>> tensor.extra_ops.searchsorted([1,2,3,4,5], 3).eval()
    array(2)
    >>> tensor.extra_ops.searchsorted([1,2,3,4,5], 3, side='right').eval()
    array(3)
    >>> tensor.extra_ops.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]).eval()
    array([0, 5, 1, 2])

    .. versionadded:: 0.9

    R/   (   R.   (   R   R?   R/   R@   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRF      s    7t   CumOpc           B` s\   e  Z d Z d d d  Z d   Z d   Z d   Z d   Z d   Z	 d	   Z
 d
   Z RS(   t   axist   modet   addc         C` sD   | d k r. t  d t |   j | f   n  | |  _ | |  _ d  S(   NRX   t   muls   %s: Unknown mode "%s"(   s   adds   mul(   R2   R   R)   RV   RW   (   R   RV   RW   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR3      s    "	c         C` s   t  j |  } | j   } |  j d  k rE t j j d | j  } n@ |  j | j	 k sj |  j | j	 k  r t
 d j |  j    n  t j |  | g | g  S(   NR6   s   axis(={0}) out of bounds(   R   R   R   RV   R:   R   R   t   vectorR6   R5   R2   t   formatR   (   R   R   RA   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR      s    %c         C` sL   | d } | d } i t  j d 6t  j d 6|  j | d |  j | d <d  S(   Ni    RX   RY   RV   (   RE   t   cumsumt   cumprodRW   RV   (   R   R   R   R   R   RJ   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    

c         C` s  | \ } | \ } |  j  d  k r |  j d k rc t | d  d  d   d  d  d  j | j  g S|  j d k r t | d |  j  } t | | d  d  d   d  d  d  j | j  | g St d t |   j	 |  j f   n  t
 d  d  d   g | j } t
 d  d  d  | |  j  <t |  } |  j d k rRt | | |  j   | g S|  j d k rt | d |  j  } t | | | |  j   | | g St d t |   j	 |  j f   d  S(   NRX   iRY   RV   s"   %s: unknown gradient for mode "%s"(   RV   R:   RW   R\   t   reshapet   shapeR]   t   NotImplementedErrorR   R)   t   sliceR5   t   tuple(   R   R   RP   R   t   git   fxt   reverse_slicing(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR      s0    		38 c         C` s*   |  j  d  k r& t j | d  f g S| S(   Ni    (   RV   R:   R   t   prod(   R   R   RB   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRC   0  s    c         C` s   | \ } | \ } |  j  } | d }	 t d d d d  |  j }
 |  j  d  k su |  j  d k r | j d j d k r d t   } n d	 t   } | S(
   NRL   RY   t   CumProdRX   t   CumSumi    i   s#  
                npy_intp shape[1] = { PyArray_SIZE(%(x)s) };
                if(!(%(z)s && PyArray_DIMS(%(z)s)[0] == shape[0]))
                {
                    Py_XDECREF(%(z)s);
                    %(z)s = (PyArrayObject*) PyArray_SimpleNew(1, shape, PyArray_TYPE((PyArrayObject*) py_%(x)s));
                }

                if (!%(z)s)
                    %(fail)s;
                {
                    PyObject * t = PyArray_%(func)s(
                        %(x)s, NPY_MAXDIMS,
                        PyArray_TYPE((PyArrayObject*) py_%(x)s), %(z)s);
                    if (!t){
                       %(fail)s;
                    }
                    // Because PyArray_%(func)s returns a newly created reference on t.
                    Py_XDECREF(t);
                }
            s5  
                if(!(%(z)s && PyArray_CompareLists(PyArray_DIMS(%(z)s), PyArray_DIMS(%(x)s), PyArray_NDIM(%(x)s))))
                {
                    Py_XDECREF(%(z)s);
                    %(z)s = (PyArrayObject*) PyArray_SimpleNew(PyArray_NDIM(%(x)s), PyArray_DIMS(%(x)s), PyArray_TYPE((PyArrayObject*) py_%(x)s));
                }

                if (!%(z)s)
                    %(fail)s;
                {

                    PyObject * t = PyArray_%(func)s(
                        %(x)s, %(axis)s,
                        PyArray_TYPE((PyArrayObject*) py_%(x)s), %(z)s);
                    if (!t){
                       %(fail)s;
                    }
                    // Because PyArray_%(func)s returns a newly created reference on t.
                    Py_XDECREF(t);
                }
            (   RV   t   dictRW   R:   R   R5   R!   (   R   R   R"   R#   R$   R%   R   RJ   RV   RL   t   funcR&   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR'   6  s    			
4c         C` s   d S(   Ni   (   i   (    (   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR(   l  s    c         C` s   d |  j  j |  j |  j f S(   Ns
   %s{%s, %s}(   t	   __class__R)   RV   RW   (   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   __str__o  s    (   s   axiss   modeN(   R)   R*   R,   R:   R3   R   R   R    RC   R'   R(   Rl   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRU      s   			 		6	c         C` s   t  d | d d  |   S(   sU  Return the cumulative sum of the elements along a given axis.

    Wraping of numpy.cumsum.

    Parameters
    ----------
    x
        Input tensor variable.
    axis
        The axis along which the cumulative sum is computed.
        The default (None) is to compute the cumsum over the flattened array.


    .. versionadded:: 0.7

    RV   RW   RX   (   RU   (   R   RV   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR\   s  s    c         C` s   t  d | d d  |   S(   s`  Return the cumulative product of the elements along a given axis.

    Wraping of numpy.cumprod.

    Parameters
    ----------
    x
        Input tensor variable.

    axis
        The axis along which the cumulative product is computed.
        The default (None) is to compute the cumprod over the flattened array.


    .. versionadded:: 0.7

    RV   RW   RY   (   RU   (   R   RV   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR]     s    t   CumsumOpc           B` s   e  Z d Z d   Z RS(   RV   c         O` s"   t  j t | |  } d | _ | S(   NRX   (   t   objectt   __new__RU   RW   (   t   typt   argst   kwargst   obj(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRo     s    	(   s   axis(   R)   R*   R,   Ro   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRm     s   t	   CumprodOpc           B` s   e  Z d Z d   Z RS(   RV   c         O` s"   t  j t | |  } d | _ | S(   NRY   (   Rn   Ro   RU   RW   (   Rp   Rq   Rr   Rs   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRo     s    	(   s   axis(   R)   R*   R,   Ro   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRt     s   t   DiffOpc           B` sA   e  Z d	 Z d d d  Z d   Z d   Z d   Z d   Z RS(
   t   nRV   i   ic         C` s8   | |  _  | |  _ | d k r4 i d g d 6|  _ n  d  S(   Ni    (   Rv   RV   R-   (   R   Rv   RV   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR3     s    		c         C` s.   t  j |  } t j |  | g | j   g  S(   N(   R   R   R   R   R   (   R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    c         C` s=   | d } | d } t  j | d |  j d |  j | d <d  S(   Ni    Rv   RV   (   RE   t   diffRv   RV   (   R   R   R   R   R   RJ   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    

c         C` sh   | d } | j  d k r( t d   n  | d } d   } x# t |  j  D] } | |  } qK W| g S(   Ni    i   sH   Grad is not implemented for inputs withnumber of dimension other than 1.c         S` s8   t  j d g |  g  } t  j |  d g g  } | | S(   Ng        (   R   t   concatenate(   RJ   t   pret   app(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   _grad_helper  s    (   R5   R`   t   rangeRv   (   R   R   t   outputs_gradientsRJ   R{   t   k(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR      s    

	c         C` s8   | d } t  |  } | |  j |  j | |  j <| g S(   Ni    (   t   listRV   Rv   (   R   R   t
   ins_shapest	   i0_shapest	   out_shape(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRC     s    
(   Rv   s   axis(   R)   R*   R,   R3   R   R   R    RC   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRu     s   			i   ic         C` s   t  d | d |  |   S(   s  Calculate the n-th order discrete difference along given axis.

    The first order difference is given by out[i] = a[i + 1] - a[i]
    along the given axis, higher order differences are calculated by
    using diff recursively. Wraping of numpy.diff.

    Parameters
    ----------
    x
        Input tensor variable.

    n
        The number of times values are differenced, default is 1.

    axis
        The axis along which the difference is taken, default is the last axis.


    .. versionadded:: 0.6

    Rv   RV   (   Ru   (   R   Rv   RV   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRw     s    c         C` s&  |  j  d k r t d   n  | rd d d l m } | d  } | |  t j j |  d k   }  n  t j j |  j   d d  } | d k	 r t j j
 | |  } n  | d k r t j j | g d |  j } t j j | d |   } n6 t j j | g d | j } t j j | | |   } | S(	   s  Count number of occurrences of each value in array of ints.

    The number of bins (of size 1) is one larger than the largest
    value in x. If minlength is specified, there will be at least
    this number of bins in the output array (though it will be longer
    if necessary, depending on the contents of x). Each bin gives the
    number of occurrences of its index value in x. If weights is
    specified the input array is weighted by it, i.e. if a value n
    is found at position i, out[n] += weight[i] instead of out[n] += 1.

    Parameters
    ----------
    x : 1 dimension, nonnegative ints
    weights : array of the same shape as x with corresponding weights.
        Optional.
    minlength : A minimum number of bins for the output array.
        Optional.
    assert_nonneg : A flag that inserts an assert_op to check if
        every input x is nonnegative.
        Optional.


    .. versionadded:: 0.6

    i   s   Inputs must be of dimension 1.i    (   t   Asserts&   Input to bincount has negative values!R7   R6   N(   R5   R=   t   theano.tensor.optR   R   R   t   allt   castt   maxR:   t   maximumt   zerosR6   t   advanced_inc_subtensor1(   R   t   weightst	   minlengtht   assert_nonnegR   t	   assert_opt	   max_valuet   out(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   bincount  s    $c         C` s<   |  j  g  t |  j  D] } |  j | s | ^ q  } | S(   s  
    Remove broadcastable dimensions from the shape of an array.

    It returns the input array, but with the
    broadcastable dimensions removed. This is
    always `x` itself or a view into `x`.

    .. versionadded:: 0.6

    Parameters
    ----------
    x
        Input data, tensor variable.

    Returns
    -------
    object
        `x` without its broadcastable dimensions.

    (   t
   dimshuffleR|   R5   t   broadcastable(   R   t   it   view(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   squeeze*  s    c         C` s(   t  j j j |   } | j | d | S(   s#  
    Return selected slices of an array along given axis.

    It returns the input tensor, but with selected slices along a given axis
    retained. If no axis is provided, the tensor is flattened.
    Corresponds to numpy.compress

    .. versionadded:: 0.7

    Parameters
    ----------
    x
        Input data, tensor variable.
    condition
         1 dimensional array of non-zero and zero values
         corresponding to indices of slices along a selected axis.

    Returns
    -------
    object
        `x` with selected slices.

    RV   (   R   R   R   t   flatnonzerot   take(   t	   conditionR   RV   t   indices(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   compressD  s    t   RepeatOpc           B` sG   e  Z d Z d d  Z d   Z d   Z d   Z d   Z d   Z	 RS(	   RV   c         C` s   | |  _  d  S(   N(   RV   (   R   RV   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR3   e  s    c         C` s\  t  j |  } t  j |  } | j t j k r? t d   n  t j j   } | d k rc d	 } n  | d k rx d
 } n  | j | k r t d t	 |  | j   n  |  j
 d  k r t g } nd y t  j |  } Wn t  j k
 r d  } n X| d k r	| j } n t | j  } t | |  j
 <t j j | j |  } t j |  | | g |   g  S(   Ns!   repeats.dtype must be an integer.i@   t   uint64i    t   uint32R7   sI   dtypes %s are not supported by numpy.repeat for the 'repeats' parameter, i   (   R   (   R   s   int64R   (   R   R   R6   R   t   integer_dtypesR=   R   R;   t   local_bitwidtht   strRV   R:   t   Falset   get_scalar_constant_valuet   NotScalarConstantErrorR   R   t
   TensorTypeR   (   R   R   t   repeatst   ptr_bitwidtht   numpy_unsupported_dtypesR   t
   const_repsRA   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   h  s2    		
c         C` sD   | d } | d } | d } t  j | d | d |  j | d <d  S(   Ni    i   R   RV   (   RE   t   repeatRV   (   R   R   R   R   R   R   RJ   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    


c         C` s   t  g t g g S(   N(   t   TrueR   (   R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   connection_pattern  s    c   	      C` s  | \ } } | \ } | j  d k r |  j d  k r? | j  } n3 |  j d k r^ |  j d } n |  j | j  d } g  t | j   D] } | j | ^ q } | j | |  | j | | j  d  j d |  t     g S| j  d k r t	    n	 t
    d  S(   Ni    i   RV   (   R5   RV   R:   R|   R_   t   insertR^   t   sumR
   R`   R2   (	   R   R   t   goutR   R   t   gzRV   R~   R_   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR      s    	)"c   	      C` s  | d } | j  d } t |  } d  } | j d k rA d } n  |  j d  k r | j d k r t |  d k r} | g } q d } x | D] } | | } q W| | f } qt j j	 | d | g } nI | j d k r | |  j | | |  j <n t j j	 | d | | |  j <| g S(	   Ni    i   t   uint8t   uint16R   R7   R6   (   R   R   s   uint32(
   R   R   R:   R6   RV   R5   RD   R   R   R   (	   R   R   R   R   R   R   R6   t   rest   d(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRC     s&    
	(   s   axisN(
   R)   R*   R,   R:   R3   R   R   R   R    RC   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   `  s   	'			c         C` s  t  j |  } | j d k r- t d   n  | j d k r` | j d r` t d |  |  |  S| j d k r| | d } n  |  j d k r t d   n  | d	 k r d } |  j	   }  n: | |  j k r t d   n  | d k  r |  j | } n  g  t
 |  j  D] } |  j | ^ q} | } | j | d |  | | | | | <t t j |  j   } | j | d d  t  j |  j |   |  j |  } | Sd	 S(
   sl  Repeat elements of an array.

    It returns an array which has the same shape as `x`, except
    along the given axis. The axis is used to speficy along which
    axis to repeat values. By default, use the flattened input
    array, and return a flat output array.

    The number of repetitions for each element is `repeat`.
    `repeats` is broadcasted to fit the length of the given `axis`.

    Parameters
    ----------
    x
        Input data, tensor variable.
    repeats
        int, scalar or tensor variable
    axis : int, optional

    See Also
    --------
    tensor.tile

    .. versionadded:: 0.6

    i   s-   The dimension of repeats should not exceed 1.i    RV   R   s/   theano.tensor.repeat don't support dtype uint64s    Axis should not exceed x.ndim-1.R   N(   R   R   R5   R2   R   R   R6   R=   R:   t   flattenR   R_   R   R   t   numpyt   aranget   allocR   R^   (   R   R   RV   R   R_   t   shape_t   dims_RJ   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s0    )$t   Bartlettc           B` s2   e  Z d Z d    Z d   Z d   Z d   Z RS(   c         C` s   t  j |  } | j d k r7 t d |  j j   n. | j t j  j k re t d |  j j   n  t	 j
 |  | g t  j   g  S(   Ni    s   %s only works on scalar inputs   %s only works on integer input(   R   R   R5   R=   Rk   R)   R6   R   R   R   R   t   dvector(   R   t   M(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    c         C` s*   | d } | \ } t  j |  | d <d  S(   Ni    (   R   t   bartlett(   R   R   R   t   out_R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   &  s    
	c         C` sG   | j  d } t j t j | d  t j d | j  |  } | g g S(   Ni    (   R   R   t   switcht   ltR   R6   (   R   R   t	   in_shapest   tempR   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRC   +  s
    	c         C` s   g  | D] } d  ^ q S(   N(   R:   (   R   R   t   output_gradsR   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR    2  s    (    (   R)   R*   R,   R   R   RC   R    (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s
   			c         C` s
   t  |   S(   s  
    An instance of this class returns the Bartlett spectral window in the
    time-domain. The Bartlett window is very similar to a triangular window,
    except that the end points are at zero. It is often used in signal
    processing for tapering a signal, without generating too much ripple in
    the frequency domain.

    .. versionadded:: 0.6

    Parameters
    ----------
    M : integer scalar
        Number of points in the output window. If zero or less,
        an empty vector is returned.

    Returns
    -------
    vector of doubles
        The triangular window, with the maximum value normalized to one
        (the value one appears only if the number of samples is odd), with
        the first and last samples equal to zero.

    (   t	   bartlett_(   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   8  s    t   FillDiagonalc           B` s2   e  Z d Z d    Z d   Z d   Z d   Z RS(   c         C` s   | d g S(   Ni    (    (   R   R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRC   W  s    c         C` s   t  j |  } t  j |  } | j d k  rF t d |  j j   n( | j d k rn t d |  j j   n  t  j | d t j | j	 | j	  } | j	 | j	 k r t d |  j j   n  t
 j |  | | g | j   g  S(   Ni   s5   %s: first parameter must have at least two dimensionsi    s%   %s: second parameter must be a scalarR6   s<   %s: type of second parameter must be the same as the first's(   R   R   R5   R=   Rk   R)   R   R   t   upcastR6   R   R   R   (   R   t   at   val(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   Z  s    'c         C` s   | d j    } | d } | j d k rk | j d d } | j d | j d } | | j d  | |  <n t j | |  | | d d <d  S(   Ni    i   i   (   R   R5   R_   t   flatR   t   fill_diagonal(   R   R   R   R   R   R   t   stept   end(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   i  s    
c         C` s   | \ } } | d } | j  j d  r2 d d g S| j d k rZ t d |  j j   n  t | d  } t j	 j
 j |  j   } | | g S(   sg   
        Notes
        -----
        The gradient is currently implemented for matrices only.

        i    t   complexi   s7   %s: gradient is currently implemented for matrices onlyN(   R6   t
   startswithR:   R5   R`   Rk   R)   R   R   R   R   t   diagR   (   R   t   inpt	   cost_gradR   R   R    t   wr_at   wr_val(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR    y  s    

(    (   R)   R*   R,   RC   R   R   R    (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   S  s
   			c         C` s   t  |  |  S(   s&  
    Returns a copy of an array with all
    elements of the main diagonal set to a specified scalar value.

    .. versionadded:: 0.6

    Parameters
    ----------
    a
        Rectangular array of at least two dimensions.
    val
        Scalar value to fill the diagonal whose type must be
        compatible with that of array 'a' (i.e. 'val' cannot be viewed
        as an upcast of 'a').

    Returns
    -------
    array
        An array identical to 'a' except that its main diagonal
        is filled with scalar 'val'. (For an array 'a' with a.ndim >=
        2, the main diagonal is the list of locations a[i, i, ..., i]
        (i.e. with indices all identical).)

    Support rectangular matrix and tensor with more than 2 dimensions
    if the later have all dimensions are equals.



    (   t   fill_diagonal_(   R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    t   FillDiagonalOffsetc           B` s2   e  Z d Z d    Z d   Z d   Z d   Z RS(   c         C` s   | d g S(   Ni    (    (   R   R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRC     s    c         C` sJ  t  j |  } t  j |  } t  j |  } | j d k rU t d |  j j   nP | j d k r} t d |  j j   n( | j d k r t d |  j j   n  t  j | d t j | j	 | j	  } | j	 | j	 k r t d |  j j   n. | j	 t
 j  j k r%t d |  j j   n  t j |  | | | g | j   g  S(	   Ni   s4   %s: first parameter must have exactly two dimensionsi    s%   %s: second parameter must be a scalars$   %s: third parameter must be a scalarR6   s<   %s: type of second parameter must be the same as the first'ss\   %s: type of third parameter must be as integer use theano.tensor.cast( input, 'int32/int64')(   R   R   R5   R=   Rk   R)   R   R   R   R6   R   R   R   R   R   (   R   R   R   t   offset(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s(    'c         C` s   | d j    } | d } | d } | j \ } } | d k rd | }	 t t | |  | |  }
 n. | | j d }	 t t | |  | |  }
 | j d d } |	 | |
 } | | j |	 | |  <| | d d <d  S(   Ni    i   i   (   R   R_   t   minR   (   R   R   R   R   R   R   R   t   heightt   widtht   startt   num_of_stepR   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    

c         C` so  | \ } } } | d } | j  \ } } | j j d  rD d d g St | d |  }	 t j |  }
 t j | d  } t j | d  } t j	 | |  } | | |
 | | } t j	 | | | | | |
  } | j  d d } | | | } t j
 | d  } t j
 | d  } t j
 | d  } | j   | | |  j   } t j j |  d | d  } |	 | | g S(   sf   
        Notes
        -----
        The gradient is currently implemented for matrices only.
        i    R   i   t   int32i   sc   offset is not defined for non-integer offset so fill_diagonal_offset(a,val,offset+eps) is undefinedN(   R_   R6   R   R:   t   fill_diagonal_offsetR   t   abs_t   geR   t   minimumR   R   R   R   R	   t   grad_undefined(   R   R   R   R   R   R   R    R   R   R   t
   offset_abst   pos_offset_flagt   neg_offset_flagt   min_whR   R   R   R   R   t	   wr_offset(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR      s.    

				(    (   R)   R*   R,   RC   R   R   R    (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s
   			c         C` s   t  |  | |  S(   s  
    Returns a copy of an array with all
    elements of the main diagonal set to a specified scalar value.

    Parameters
    ----------
    a
        Rectangular array of two dimensions.
    val
        Scalar value to fill the diagonal whose type must be
        compatible with that of array 'a' (i.e. 'val' cannot be viewed
        as an upcast of 'a').
    offset
        Scalar value Offset of the diagonal from the main
        diagonal. Can be positive or negative integer.

    Returns
    -------
    array
        An array identical to 'a' except that its offset diagonal
        is filled with scalar 'val'. The output is unwrapped.

    (   t   fill_diagonal_offset_(   R   R   R   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s    c         C` s[   t  j j |  j d | f d | } t  j j | t  j j |  j d  |  f d  } | S(   s  
    Return a matrix where each row correspond to the one hot
    encoding of each element in y.

    Parameters
    ----------
    y
        A vector of integer value between 0 and nb_class - 1.
    nb_class : int
        The number of class in y.
    dtype : data-type
        The dtype of the returned matrix. Default floatX.

    Returns
    -------
    object
        A matrix of shape (y.shape[0], nb_class), where each row ``i`` is
        the one hot encoding of the corresponding ``y[i]`` value.

    i    R6   i   (   R   R   R   R_   t   set_subtensorR   (   R   t   nb_classR6   t   ret(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt
   to_one_hot7  s
    	)	t   Uniquec           B` sA   e  Z d  Z d Z e e e d  Z d   Z d   Z d   Z RS(	   s?  
    Wraps numpy.unique. This op is not implemented on the GPU.

    Examples
    --------
    >>> import numpy as np
    >>> import theano

    >>> x = theano.tensor.vector()
    >>> f = theano.function([x], Unique(True, True, False)(x))
    >>> f([1, 2., 3, 4, 3, 2, 1.])
    [array([ 1.,  2.,  3.,  4.]), array([0, 1, 2, 3]), array([0, 1, 2, 3, 2, 1, 0])]

    >>> y = theano.tensor.matrix()
    >>> g = theano.function([y], Unique(True, True, False)(y))
    >>> g([[1, 1, 1.0], (2, 3, 3.0)])
    [array([ 1.,  2.,  3.]), array([0, 3, 4]), array([0, 0, 0, 1, 2, 2])]

    t   return_indext   return_inverset   return_countsc         C` s   | |  _  | |  _ | |  _ g  t j j d  d  D] } t |  ^ q2 } |  j r t | d d g k   r t d t	 j d   n  d  S(   Nt   .i   i   i	   s   Numpy version = s@   . Option 'return_counts=True' works starting from version 1.9.0.(
   R   R   R   R   t   __version__t   splitt   intt   boolt   RuntimeErrorRE   (   R   R   R   R   Rv   t	   numpy_ver(    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR3   j  s    			/!
c         C` s   t  j |  } t  j d t g d | j    g } t  j d t g d d  } |  j rj | j |    n  |  j r | j |    n  |  j r | j |    n  t	 j
 |  | g |  S(   NR   R6   R7   (   R   R   R   R   R6   R   t   appendR   R   R   R   (   R   R   RH   Rp   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   v  s    $			c   	      C` s   | d } | } i  } |  j  r, t | d <n  |  j rB t | d <n  |  j rX t | d <n  t j | |  } |  j r |  j  r |  j r | | d d <n/ x, t t |   D] } | | | | d <q Wd  S(   Ni    R   R   R   (   R   R   R   R   RE   t   uniqueR|   RD   (	   R   R   R   R   R   RJ   t   paramt   outsR   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR     s     
			


c         C` s`   | j  j j | |  } |  j r\ t j | d  f } |  j rN | | d <| S| | d <| S| S(   Ni    i   i   (   t   fgrapht   shape_featuret   default_infer_shapeR   R   Rf   R   (   R   R   R   R   R_   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyRC     s    		

(   s   return_indexs   return_inverses   return_counts(	   R)   R*   R+   R,   R   R3   R   R   RC   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyR   S  s   		(3   t
   __future__R    R   R   R   RE   t	   six.movesR   R   t   theano.tensorR   R   R   R   t
   theano.gofR   R	   t   theano.gradientR
   R   R   t   OpR   t   cpu_contiguousR.   R:   RF   RU   R\   R]   Rm   Rt   Ru   Rw   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/tensor/extra_ops.pyt   <module>   sL   5	s:~		/2	nI		9		!h		