
Xc           @` sl  d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l Z d d l m	 Z	 d d l
 m Z m 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 d Z d e j f d     YZ e j j e d d  e   Z d e j f d     YZ d   Z d   Z d d d d d d  Z d d d d d d  Z  d d d d d d d  Z! d   Z" d d d d d d  Z# d   Z$ d d e% d d d d  Z& d d d d d   Z' d!   Z( d d d d d"  Z) d#   Z* d d d d g d d d$  Z+ e j, e g  d%    Z- e j. d& e j/ e- d' e% d( d) d*  d+ e0 f d,     YZ1 d S(-   sH   Define random number Type (`RandomStateType`) and Op (`RandomFunction`).i    (   t   absolute_importt   print_functiont   divisionN(   t   copy(   t   string_types(   t   reducet   xrange(   t   tensor(   t   opt(   t   gof(   t   optdbs   restructuredtext ent   RandomStateTypec           B` sY   e  Z d  Z d   Z e d d  Z d   Z d   Z d   Z	 d   Z
 e d    Z RS(	   s1  
    A Type wrapper for numpy.random.RandomState.

    The reason this exists (and `Generic` doesn't suffice) is that
    RandomState objects that would appear to be equal do not compare
    equal with the '==' operator.  This Type exists to provide an equals
    function that is used by DebugMode.

    c         C` s   d S(   NR   (    (   t   self(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   __str__   s    c         C` s    |  j  |  r | St    d  S(   N(   t   is_valid_valuet	   TypeError(   R   t   datat   strictt   allow_downcast(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   filter"   s    c         C` s   t  |  t j j k S(   N(   t   typet   numpyt   randomt   RandomState(   R   t   a(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR   (   s    c         C` s   | j    } | j    } | d | d k r0 t St j | d | d k  sQ t S| d | d k ri t S| d | d k r t S| d d k r | d | d k r t Sn  t S(   Ni    i   i   i   i   (   t	   get_statet   FalseR   t   allt   True(   R   R   t   bt   sat   sb(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt	   values_eq+   s    c         C` s   d  S(   N(   t   None(   R   t   obj(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   get_shape_infoA   s    c         C` s   t  j j   j   } d } x | D] } t | t  rJ | t |  7} q" t | t  j  rs | | j | j	 7} q" t | t
  r | t  j d  j	 7} q" t | t  r | t  j d  j	 7} q" t    q" W| S(   Ni    t   intt   float(   R   R   R   R   t
   isinstancet   strt   lent   ndarrayt   sizet   itemsizeR$   t   dtypeR%   t   NotImplementedError(   R   t
   shape_infot   stateR*   t   elem(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   get_sizeD   s    c         C` s
   |  | k S(   N(    (   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   may_share_memoryU   s    N(   t   __name__t
   __module__t   __doc__R   R   R!   R   R   R    R#   R1   t   staticmethodR2   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR      s   						sV   
    Py_XDECREF(%(oname)s);
    %(oname)s = %(iname)s;
    Py_XINCREF(%(oname)s);
    i   t   RandomFunctionc           B` sk   e  Z d  Z d Z e d d  Z d   Z d   Z d	   Z d
   Z	 d   Z
 d   Z d   Z d   Z RS(   s  
    Op that draws random numbers from a numpy.random.RandomState object.

    Parameters
    ----------
    fn : string or function reference
        A member function of numpy.random.RandomState. A string will
        be interpreted as the name of a member function of
        numpy.random.RandomState.
        Technically, any function with a signature like the ones in
        numpy.random.RandomState will do. This function must accept
        the shape (sometimes called size) of the output as the last
        positional argument.
    outtype
        The theano Type of the output.
    args
        A list of default arguments for the function
        kwargs
        If the 'inplace' key is there, its value will be used to
        determine if the op operates inplace or not.
        If the 'ndim_added' key is there, its value indicates how
        many more dimensions this op will add to the output, in
        addition to the shape's dimensions (used in multinomial and
        permutation).

    t   fnt   outtypet   inplacet
   ndim_addedi    c         C` s   |  j  | | | | g  d  S(   N(   t   __setstate__(   R   R8   R9   R:   R;   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   __init__   s    c         C` s0   t  |  j  } | d =d | k r, | d =n  | S(   Nt   exec_fnt   destroy_map(   t   dictt   __dict__(   R   t   d(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   __getstate__   s
    
c         C` s   t  | t  rD | d | d | d | d g } |  j j |  n | } | \ } } } } | |  _ t  | t  r t t j j	 |  |  _
 n	 | |  _
 | |  _ | |  _ |  j r i d g d 6|  _ n  | |  _ d  S(   NR8   R9   R:   R;   i    (   R&   R@   RA   t   updateR8   R   t   getattrR   R   R   R>   R9   R:   R?   R;   (   R   t   dctR/   R8   R9   R:   R;   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR<      s"    					c         C` s   d |  j  j S(   Ns   RandomFunction{%s}(   R>   R3   (   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR      s    c         G` s   t  j | d d } | d k r3 | j d  } n | } | j j d k sQ t  | j j d k s{ | j j d k s{ t  t | j t  s t	 d d t
 j n  t t t  j |   } t j |  | | g | | j   |  j   g  S(   sV  
        Parameters
        ----------
        r
            A numpy.random.RandomState instance, or a Variable of Type
            RandomStateType that will contain a RandomState instance.
        shape
            An lvector with a shape defining how many samples
            to draw.  In the case of scalar distributions, it is the shape
            of the tensor output by this Op.  In that case, at runtime, the
            value associated with this lvector must have a length equal to
            the number of dimensions promised by `self.outtype`.
            In a more general case, the number of output dimensions,
            len(self.outtype), is equal to len(shape)+self.ndim_added.
            The special case where len(shape) == 0 means that the smallest
            shape compatible with the argument's shape will be used.
        args
            The values associated with these variables will be passed to the
            RandomState function during perform as extra "*args"-style
            arguments. These should be castable to variables of Type TensorType.

        Returns
        -------
        Apply
            Apply with two outputs. The first output is a gof.generic Variable
            from which to draw further random numbers.
            The second output is the outtype() instance holding the random
            draw.

        t   ndimi   t   int64t   int32s;   WARNING: RandomState instances should be in RandomStateTypet   file(    (   R   t   as_tensor_variablet   astypeR   RG   t   AssertionErrorR,   R&   R   t   printt   syst   stderrt   listt   mapR	   t   ApplyR9   (   R   t   rt   shapet   argst   shape_(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt	   make_node   s    *	c         C` s   | j  d d !\ } } t t | d d d d g   d k } |  j d k r\ | r\ | } n t j    d  g  t | j d j	  D] } | | ^ q g S(   Ni    i   R   i   (
   t   inputsR(   RE   R;   R   t
   ShapeErrorR!   R   t   outputsRG   (   R   t   nodet   i_shapesRT   t   shpt   unknown_shapet
   sample_shpt   i(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   infer_shape   s    '	c   
      C` s  | \ } } | d | d | d } } } t  |  t j j k s\ t t  |  | f   t |  d k rw d  } n t |  } | d  k	 r |  j j	 t |  |  j
 k r t d |  j j	 t |  |  j
 f   n  |  j s t |  } n  | | d <|  j | | | g  }	 t |	 t j  sIt |	 j  | j d j  j k rnt j |	 d | j d j  j }	 n  | d  k r|	 j	 d k r|  j j	 d k r|	 j d g |  j j	  }	 n  t |	 j  |  j j	 k rt d |  j j	 t |	 j  f   n  | d  k	 r|  j
 d k rC| |	 j k rCt d | |	 j f   q|  j
 d k r| |	 j |  j
  k rt d | |  j
 |	 j f   qn  |	 | d <d  S(	   Ni    i   i   sP   Shape mismatch: self.outtype.ndim (%i) != len(shape) (%i) + self.ndim_added (%i)R,   sd   Shape mismatch: "out" should have dimension %i, but the value produced by "perform" has dimension %is\   Shape mismatch: "out" should have shape %s, but the value produced by "perform" has shape %ss   Shape mismatch: "out" should have shape starting with %s (plus %i extra dimensions), but the value produced by "perform" has shape %s(   R   R   R   R   RM   R(   R!   t   tupleR9   RG   R;   t
   ValueErrorR:   R   R>   R&   R)   R'   R,   R[   t   theanot   _asarrayt   reshapeRU   (
   R   R\   RY   t   out_t   routt   outRT   RU   RV   t   rval(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   perform   sB     0	%	
"%-"c         C` s8   g  t  |  D]' \ } } t j j |  | | d  ^ q S(   Ns1   No gradient defined through raw random numbers op(   t	   enumerateRe   t   gradientt   grad_undefined(   R   RY   R[   t   kt   inp(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   grad(  s    c         C` s   g  | D] } d  ^ q S(   N(   R!   (   R   RY   t   eval_pointsRa   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   R_op-  s    (   s   fns   outtypes   inplaces
   ndim_added(   R3   R4   R5   t	   __props__R   R=   RC   R<   R   RX   Rb   Rl   Rr   Rt   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR7   f   s   				6		;	c         G` sz  | r t  d   | D  } n d } t | t t f  r#t |  } |  d k r[ | }  n+ | |  k r t d d |  | | f   n  g  } g  } xRt |  D]D\ } } t | d  r | j	 t
  | j	 |  q | d k r| j	 t j |   | j	 | d k  q | d k rd }	 x | D]i }
 | |  |
 j k r(|	 d 7}	 | |
 j |  } |
 j | s| j	 |
 j |  | j	 t
  Pqq(q(W|	 d k rt d   q| j	 t j d   | j	 t  q t d	 |  q Wt |  d k rt j g  d
 d } qt j |  } n | d k rt| sDt d   n  t d   |  } | j } | j } | j }  n\ t j |  } | j d k rt d |   n  |  d k rt j |  }  n  t
 g |  } | j d k rt d |   n  | j t j j k rt d | j   n  | |  k rCt d |  | f |   n  |  t |  k s[t  |  t j | d  t |  f S(   s  
    Infer the number of dimensions from the shape or the other arguments.

    Returns
    -------
    (int, variable, tuple) triple, where the variable is an integer vector,
    and the tuple contains Booleans
        The first element returned is the inferred number of dimensions.
        The second element is the shape inferred (combining symbolic and
        constant informations from shape and args).
        The third element is a broadcasting pattern corresponding to that shape.

    c         s` s   |  ] } | j  Vq d  S(   N(   RG   (   t   .0t   arg(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pys	   <genexpr>B  s    i    s(   ndim should be equal to len(shape), but
s&   ndim = %s, len(shape) = %s, shape = %sR   i   isM   Auto-shape of -1 must overlapwith the shape of one of the broadcastableinputss   negative shapeR,   RH   sA   _infer_ndim_bcast cannot infer shape without either shape or argsc         S` s   |  | S(   N(    (   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   <lambda>  s    s2   shape must be a vector or list of scalar, got '%s's'   shape must be an integer vector or lists8   ndim should be at least as big as required by args valueN(   t   maxR&   Rc   RQ   R(   R!   Rd   Rm   t   hasattrt   appendR   R   RK   RG   t   broadcastableRU   R   t   constantt   stackR   R   t   get_vector_lengthR,   Re   t   integer_dtypesRM   t   cast(   RG   RU   RV   t	   args_ndimt
   shape_ndimt   bcastt   pre_v_shapeRa   t   st   n_a_iR   t   a_it   v_shapet   template(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   _infer_ndim_bcast1  s    	
		c      	   G` s  |  f | } g  | D] } d g ^ q } xPt  t |    D]<} g  | D] } g  ^ qI } t t |  |   } | g } x | D] } | | |  | k r | j |  q | | d k r | j d g |  |  q t d | | | | |  | f | |  |   q Wxh t |   D]Z }	 xQ t |   D]C }
 x: t  t |   D]& } | | j |	 | |
 | f  q@Wq'WqW| } q< W| S(   s  
    Return indices over each shape that broadcast them to match out_shape.

    The first returned list is equivalent to numpy.ndindex(out_shape),
    the other returned lists are indices corresponding to the other shapes,
    such that looping over these indices produce tensors of shape out_shape.
    In particular, the indices over broadcasted dimensions should all be 0.

    The shapes should have the same length as out_shape. If they are longer,
    the right-most dimensions are ignored.

    i   i    s<   shape[%i] (%i) should be equal to out_shape[%i] (%i) or to 1(    (   R   R(   RQ   t   rangeR{   Rd   t   zip(   t	   out_shapet   shapest
   all_shapesRU   t   ret_indicest   dimt   _ret_indicest	   out_ranget   rangest
   prev_indext	   dim_indexRa   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   _generate_broadcasting_indices  s*    	,
g        g      ?c         C` s   t  j |  } t  j |  } | d k rQ t  j j t j j | j | j  } n  t	 | | | |  \ } } } t
 d t  j d | d |   } | |  | | |  S(   s  
    Sample from a uniform distribution between low and high.

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, the output shape will be determined by the shapes
    of low and high.

    If dtype is not specified, it will be inferred from the dtype of
    low and high, but will be at least as precise as floatX.

    t   uniformR,   R|   N(   R   RK   R!   t   scalt   upcastRe   t   configt   floatXR,   R   R7   t
   TensorType(   t   random_stateR*   t   lowt   highRG   R,   R   t   op(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    'c         C` s   t  j |  } t  j |  } | d k rQ t  j j t j j | j | j  } n  t	 | | | |  \ } } } t
 d t  j d | d |   } | |  | | |  S(   s  
    Sample from a normal distribution centered on avg with
    the specified standard deviation (std).

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, the output shape will be determined by the shapes
    of avg and std.

    If dtype is not specified, it will be inferred from the dtype of
    avg and std, but will be at least as precise as floatX.

    t   normalR,   R|   N(   R   RK   R!   R   R   Re   R   R   R,   R   R7   R   (   R   R*   t   avgt   stdRG   R,   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    'g      ?RH   c   	      C` s$  | d k	 r( | } t d d t j n  t j |  } t j |  } t | | | |  \ } } } | j d k r yM t j	 j
 d t j d d d g d d d	 t j d
 d d g d d  Wq t k
 r t j | d  } q Xn  t d t j d | d t f |   } | |  | | |  S(   sT  
    Sample n times with probability of success prob for each trial,
    return the number of successes.

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, the output shape will be determined by the shapes
    of n and prob.

    sq   DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as numpy.RJ   RH   t   ni   i   i   R,   t   pg?g?g333333?t   float64RI   t   binomialR|   N(   R!   RN   RO   RP   R   RK   R   R,   R   R   R   t   asarrayR   R   R7   R   R   (	   R   R*   R   R   RG   R,   t   probR   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    Mc         C` s  | d k	 r t |  } n t | j | j  } | j | k ra t d | j | f | |   n  | j | k  r | j d | | j | j  } n  | j | k r t d | j | f | |   n  | j | k  r | j d | | j | j  } n  | d k	 rt |  } nG d	 } x> t |  D]0 } t | j | | j |  } | | f } q&Wt	 j
 |  } t | | j | j  }	 xE t |	   D]7 \ }
 } } |  j d | | d | | d  | |
 <qW| S(
   s   
    Helper function to draw random integers.

    This is a generalization of numpy.random.random_integers to the case where
    low and high are tensors.

    Since random_integers is deprecated it calls randint() instead.

    s6   low.ndim (%i) should not be larger than len(size) (%i)i   s7   high.ndim (%i) should not be larger than len(size) (%i)R   R   N(   i   (   i   (    (   R!   R(   Ry   RG   Rd   Rg   RU   Rc   R   R   R)   R   R   t   randint(   R   R   R   R*   t   out_ndimt   out_sizeR   t   dim_lenRj   t   broadcast_indt   oit   lit   hi(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   random_integers_helper"  s8    $$,c         C` sp   t  j |  } t  j |  } t | | | |  \ } } } t t t  j d | d |   } | |  | | |  S(   s0  
    Sample a random integer between low and high, both inclusive.

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, the output shape will be determined by the shapes
    of low and high.

    R,   R|   (   R   RK   R   R7   R   R   (   R   R*   R   R   RG   R,   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   random_integersT  s    c         C` s   | j  d k r% t d | j    n  | j  d k rO | j d k rb d } qb n t d | j    t |  } |  j | | | |  S(   s   
    Helper function to draw random numbers using numpy's choice function.

    This is a generalization of numpy.random.choice that coerces
    `replace` to a bool and replaces `p` with None when p is a vector
    of 0 elements.

    i   s   a.ndim (%i) must be 0 or 1i    s   p.ndim (%i) must be 1N(   RG   Rd   R*   R!   t   boolt   choice(   R   R   t   replaceR   R*   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   choice_helperh  s    	i   c   	      C` s   t  j |  } t | t  r6 t  j | d d } n t  j |  } t  j | pT g   } t | |  \ } } } t t t  j d | d |   } | |  | | | |  S(   ss  
    Choose values from `a` with or without replacement. `a` can be a 1-D array
    or a positive scalar. If `a` is a scalar, the samples are drawn from the
    range 0,...,a-1.

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, a scalar will be returned.

    R,   t   int8R|   (	   R   RK   R&   R   R}   R   R7   R   R   (	   R   R*   R   R   R   RG   R,   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR   |  s    c         C` sX   t  j |  } t | |  \ } } } t d t  j d | d |   } | |  | |  S(   s"  
    Draw samples from a Poisson distribution.

    The Poisson distribution is the limit of the Binomial distribution for
    large N.

    Parameters
    ----------
    lam : float or ndarray-like of the same shape as size parameter
        Expectation of interval, should be >= 0.
    size: int or tuple of ints, optional
        Output shape. If the given shape is, e.g., (m, n, k), then m * n * k
        samples are drawn.
    dtype
        The dtype of the return value (which will represent counts).

    size or ndim must be given.

    t   poissonR,   R|   (   R   RK   R   R7   R   (   R   R*   t   lamRG   R,   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s
    c         C` s   | j  d k s t  t | j    } | d k r< d } n  t |  } | j |  t j | t  } x* t j	 |   D] } |  j
 |  | | <qw W| S(   sd  
    Helper function to generate permutations from integers.

    permutation_helper(random_state, n, (1,)) will generate a permutation of
    integers 0..n-1.
    In general, it will generate as many such permutation as required by shape.
    For instance, if shape=(p,q), p*q permutations will be generated, and the
    output shape will be (p,q,n), because each permutation is of size n.

    If you wish to perform a permutation of the elements of an existing vector,
    see shuffle_row_elements.

    This is a generalization of numpy.random.permutation to tensors.
    Otherwise it behaves the same.

    (    N(    (   RU   RM   R$   t   itemR!   RQ   R{   R   t   emptyt   ndindext   permutation(   R   R   RU   R   Rj   Ra   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   permutation_helper  s    	c         C` s   | d k s | d k rT | d k p- | d k s? t d   n  d } d } d	 } n t | |  \ } } } t t t j d | d | t f  d d } | |  | |  S(
   s
  
    Return permutations of the integers between 0 and n-1.

    Returns them as many times as required by size. For instance, if size=(p,q),
    p*q permutations will be generated, and the output shape will be (p,q,n),
    because each permutation is of size n.

    Theano tries to infer the number of dimensions from the length of
    the size argument and the shape of n, but you may always specify it
    with the `ndim` parameter.

    Notes
    -----
    Note that the output will then be of dimension ndim+1.

    i   sH   You asked for just one permutation but asked for more then 1 dimensions.R,   R|   R;   N(    (    (    (   R!   R   R   R7   R   R   R   R   (   R   R*   R   RG   R,   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    		c         C` s  | d k	 r t |  } n t | j | j d  } | j | k re t d | j | f | |   n  | j | k  r | j d | | j | j  } n  | j d | k r t d | j d | f | |   n  | j d | k  r| j d | | j d | j  } n  | d k	 r't |  } nG d } x> t |  D]0 } t | j | | j |  } | | f } q:W| | j d f } t	 j
 |  } t | | j | j d   }	 | j   d k st  x t |	   D] \ }
 } } | | } t	 j |  } d | k  od k  n r2| d } t	 j |  } n, | d d k  r^| d } t	 j |  } n  | d k svt |   |  j d	 | | d
 | j d   | |
 <qW| S(   s   
    Helper function drawing from multinomial distributions.

    This is a generalization of numpy.random.multinomial to the case where
    n and pvals are tensors.

    i   s4   n.ndim (%i) should not be larger than len(size) (%i)s:   pvals.ndim-1 (%i) should not be larger than len(size) (%i)ii    g      ?gh㈵>g-C6
?R   t   pvalsR   N(   i   (   i   (    grZ|
 ?gSt$?gSt$?(   R!   R(   Ry   RG   Rd   Rg   RU   Rc   R   R   R)   R   t   minRM   R   t   sumt   multinomialRL   (   R   R   R   R*   RG   R   R   R   Rj   R   t   mit   nit   pit   pvit   pisum(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   multinomial_helper  sN    $(


c   	      C` s   t  j |  } t  j |  } | j d j } t | | | |  \ } } } | | j j d f } t t t  j d | d |  d d } | |  | | |  S(   s  
    Sample from one or more multinomial distributions defined by
    one-dimensional slices in pvals.

    Parameters
    ----------
    pvals
        A tensor of shape "nmulti+(L,)" describing each multinomial
        distribution.  This tensor must have the property that
        numpy.allclose(pvals.sum(axis=-1), 1) is true.
    size
        A vector of shape information for the output; this can also
        specify the "nmulti" part of pvals' shape.  A -1 in the k'th position
        from the right means to borrow the k'th position from the
        right in nmulti. (See examples below.)
        Default ``None`` means size=nmulti.
    n
        The number of experiments to simulate for each
        multinomial. This can be a scalar, or tensor, it will be
        broadcasted to have shape "nmulti".
    dtype
        The dtype of the return value (which will represent counts)

    Returns
    -------
    tensor
        Tensor of len(size)+1 dimensions, and shape[-1]==L, with
        the specified ``dtype``, with the experiment counts. See
        examples to understand the shape of the return value, which is
        derived from both size and pvals.shape. In return value rval,
        "numpy.allclose(rval.sum(axis=-1), n)" will be true.

    Extended Summary
    ----------------
    For example, to simulate n experiments from each multinomial in a batch of
    size B:

        size=None, pvals.shape=(B,L) --> rval.shape=[B,L]

        rval[i,j] is the count of possibility j in the i'th distribution (row)
        in pvals.

    Using size:

        size=(1,-1), pvals.shape=(A,B,L)
        --> rval.shape=[1,B,L], and requires that A==1.

        rval[k,i,j] is the count of possibility j in the distribution specified
        by pvals[k,i].

    Using size for broadcasting of pvals:

        size=(10, 1, -1), pvals.shape=(A, B, L)
        --> rval.shape=[10,1,B,L], and requires that A==1.

        rval[l,k,i,j] is the count of possibility j in the
        distribution specified by pvals[k,i], in the l'th of 10
        draws.

    i    iR,   R|   R;   i   (	   R   RK   t   TR   R   R|   R7   R   R   (	   R   R*   R   R   RG   R,   t   tmpR   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR   B  s    >		c         C` sl   |  j  } t | t  rh | j rh | j   \ } } } } t | | d t d | } | j |  j   j St	 S(   NR:   R;   (
   R   R&   R7   R:   t   _propsR   RX   RY   R[   R   (   R\   R   t   op_fnt
   op_outtypet
   op_inplacet   op_ndim_addedt   new_op(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   random_make_inplace  s    		R   t   ignore_newtreesic   t   fast_runR:   t   RandomStreamsBasec           B` s   e  Z d d  d d d d d  Z d d d d d d  Z d d d d d d  Z d d d  d d d	  Z d d
 e d d d d  Z d d d d d  Z	 d d  d d d  Z
 d d  d d g d d d  Z d   Z RS(   i   g      ?RH   c      	   C` sJ   | d k	 r( | } t d d t j n  |  j t | | | d | d | S(   s  
        Sample n times with probability of success p for each trial and
        return the number of successes.

        If the size argument is ambiguous on the number of dimensions,
        ndim may be a plain integer to supplement the missing information.

        sq   DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as numpy.RJ   RG   R,   N(   R!   RN   RO   RP   t   genR   (   R   R*   R   R   RG   R,   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    
g        g      ?c      	   C` s"   |  j  t | | | d | d | S(   s  
        Sample a tensor of given size whose element from a uniform
        distribution between low and high.

        If the size argument is ambiguous on the number of dimensions,
        ndim may be a plain integer to supplement the missing information.

        RG   R,   (   R   R   (   R   R*   R   R   RG   R,   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    	c      	   C` s"   |  j  t | | | d | d | S(   s  
        Sample from a normal distribution centered on avg with
        the specified standard deviation (std).

        If the size argument is ambiguous on the number of dimensions,
        ndim may be a plain integer to supplement the missing information.

        RG   R,   (   R   R   (   R   R*   R   R   RG   R,   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    	i    c      	   C` s"   |  j  t | | | d | d | S(   s   
        Sample a random integer between low and high, both inclusive.

        If the size argument is ambiguous on the number of dimensions,
        ndim may be a plain integer to supplement the missing information.

        RG   R,   (   R   R   (   R   R*   R   R   RG   R,   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    	i   c      
   C` s%   |  j  t | | | | d | d | S(   s[  
        Choose values from `a` with or without replacement.

        `a` can be a 1-D array or a positive scalar.
        If `a` is a scalar, the samples are drawn from the range 0,...,a-1.

        If the size argument is ambiguous on the number of dimensions,
        ndim may be a plain integer to supplement the missing information.

        RG   R,   (   R   R   (   R   R*   R   R   R   RG   R,   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    c         C` s   |  j  t | | d | d | S(   s0  
        Draw samples from a Poisson distribution.

        The Poisson distribution is the limit of the Binomial distribution for
        large N.

        If the size argument is ambiguous on the number of dimensions,
        ndim may be a plain integer to supplement the missing information.

        RG   R,   (   R   R   (   R   R*   R   RG   R,   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    c         C` s   |  j  t | | d | d | S(   s>  
        Return permutations of the integers between 0 and n-1.

        Returns them as many times as required by size. For instance,
        if size=(p,q), p*q permutations will be generated,
        and the output shape will be (p,q,n), because each
        permutation is of size n.

        Theano tries to infer the number of dimensions from the length
        of the size argument and the shape of n, but you may always
        specify it with the `ndim` parameter.

        Notes
        -----
        Note that the output will then be of dimension ndim+1.

        RG   R,   (   R   R   (   R   R*   R   RG   R,   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s    c      	   C` s"   |  j  t | | | d | d | S(   s$  
        Sample n times from a multinomial distribution defined by
        probabilities pvals, as many times as required by size. For
        instance, if size=(p,q), p*q samples will be drawn, and the
        output shape will be (p,q,len(pvals)).

        Theano tries to infer the number of dimensions from the length
        of the size argument and the shapes of n and pvals, but you may
        always specify it with the `ndim` parameter.

        Notes
        -----
        Note that the output will then be of dimension ndim+1.

        RG   R,   (   R   R   (   R   R*   R   R   RG   R,   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR      s    c         C` sR   |  j  d | j d  d | j d d | j d  } t j | |  } | | _  | S(   s   
        Return a variable with every row (rightmost index) shuffled.

        This uses permutation random variable internally, available via
        the ``.permutation`` attribute of the return value.

        R*   iR   RG   i   (   R   RU   RG   R   t   permute_row_elements(   R   t   inputt   permt   shuffled(    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   shuffle_row_elements  s
    #	N(   R3   R4   R!   R   R   R   R   R   R   R   R   R   R   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyR     s   (2   R5   t
   __future__R    R   R   RO   R   R   t   sixR   t	   six.movesR   R   Re   R   t   theano.tensorR   R	   t   theano.compileR
   t   __docformat__t   TypeR   t   compilet   register_view_op_c_codet   random_state_typet   OpR7   R   R   R!   R   R   R   R   R   R   R   R   R   R   R   R   R   t   local_optimizerR   t   registert   in2outt   objectR   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/tensor/raw_random.pyt   <module>   sT   E			r	/	2		$"	IJ