ó
àÆ÷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 m Z d  d l m Z d  d l	 m Z e j e ƒ Z d e f d	 „  ƒ  YZ e ƒ  Z d
 e f d „  ƒ  YZ e ƒ  Z d „  Z d e f d „  ƒ  YZ e ƒ  Z d e f d „  ƒ  YZ e ƒ  Z d „  Z d „  Z d e f d „  ƒ  YZ  e  ƒ  Z! d e f d „  ƒ  YZ" e" ƒ  Z# d e" f d „  ƒ  YZ$ d „  Z% d e f d „  ƒ  YZ& d d „ Z' d e f d „  ƒ  YZ( d  e f d! „  ƒ  YZ) d" d# „ Z* d$ e f d% „  ƒ  YZ+ d& d& d' „ Z, d( e f d) „  ƒ  YZ- d* „  Z. d+ „  Z/ d, e f d- „  ƒ  YZ0 d. d/ „ Z1 d0 e f d1 „  ƒ  YZ2 d d2 „ Z4 d S(3   i    (   t   absolute_importt   print_functiont   divisionN(   t   xrange(   t   as_tensor_variable(   t   Opt   Apply(   t   DisconnectedType(   t   basict
   MatrixPinvc           B` s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   s¹  Computes the pseudo-inverse of a matrix :math:`A`.

    The pseudo-inverse of a matrix :math:`A`, denoted :math:`A^+`, is
    defined as: "the matrix that 'solves' [the least-squares problem]
    :math:`Ax = b`," i.e., if :math:`\bar{x}` is said solution, then
    :math:`A^+` is that matrix such that :math:`\bar{x} = A^+b`.

    Note that :math:`Ax=AA^+b`, so :math:`AA^+` is close to the identity matrix.
    This method is not faster than `matrix_inverse`. Its strength comes from
    that it works for non-square matrices.
    If you have a square matrix though, `matrix_inverse` can be both more
    exact and faster to compute. Also this op does not get optimized into a
    solve op.

    c         C` s   d  S(   N(    (   t   self(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   __init__$   s    c         C` s=   t  | ƒ } | j d k s! t ‚ t |  | g | j ƒ  g ƒ S(   Ni   (   R   t   ndimt   AssertionErrorR   t   type(   R
   t   x(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt	   make_node'   s    c         C` s8   | \ } | \ } t  j j | ƒ j | j ƒ | d <d  S(   Ni    (   t   numpyt   linalgt   pinvt   astypet   dtype(   R
   t   nodet   inputst   outputsR   t   z(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   perform,   s    		(    (   t   __name__t
   __module__t   __doc__t	   __props__R   R   R   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR	      s
   		t   MatrixInversec           B` sJ   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sz  Computes the inverse of a matrix :math:`A`.

    Given a square matrix :math:`A`, ``matrix_inverse`` returns a square
    matrix :math:`A_{inv}` such that the dot product :math:`A \cdot A_{inv}`
    and :math:`A_{inv} \cdot A` equals the identity matrix :math:`I`.

    Notes
    -----
    When possible, the call to this op will be optimized to the call
    of ``solve``.

    c         C` s   d  S(   N(    (   R
   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   D   s    c         C` s=   t  | ƒ } | j d k s! t ‚ t |  | g | j ƒ  g ƒ S(   Ni   (   R   R   R   R   R   (   R
   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   G   s    c         C` s8   | \ } | \ } t  j j | ƒ j | j ƒ | d <d  S(   Ni    (   R   R   t   invR   R   (   R
   R   R   R   R   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   L   s    		c         C` s8   | \ } |  | ƒ } | \ } t  | | j | ƒ j g S(   s£  The gradient function should return

            .. math:: V\frac{\partial X^{-1}}{\partial X},

        where :math:`V` corresponds to ``g_outputs`` and :math:`X` to
        ``inputs``. Using the `matrix cookbook
        <http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=3274>`_,
        one can deduce that the relation corresponds to

            .. math:: (X^{-1} \cdot V^{T} \cdot X^{-1})^T.

        (   t
   matrix_dott   T(   R
   R   t	   g_outputsR   t   xit   gz(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   gradQ   s    		c         C` sE   | \ } |  | ƒ } | \ } | d k r1 d g St | | | ƒ g S(   s›  The gradient function should return

            .. math:: \frac{\partial X^{-1}}{\partial X}V,

        where :math:`V` corresponds to ``g_outputs`` and :math:`X` to
        ``inputs``. Using the `matrix cookbook
        <http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=3274>`_,
        one can deduce that the relation corresponds to

            .. math:: X^{-1} \cdot V \cdot X^{-1}.

        N(   t   NoneR!   (   R
   R   t   eval_pointsR   R$   t   ev(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   R_opd   s    		c         C` s   | S(   N(    (   R
   R   t   shapes(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   infer_shapex   s    (    (
   R   R   R   R   R   R   R   R&   R*   R,   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   4   s   					c          G` s8   |  d } x' |  d D] } t  j j | | ƒ } q W| S(   sþ    Shorthand for product between several dots.

    Given :math:`N` matrices :math:`A_0, A_1, .., A_N`, ``matrix_dot`` will
    generate the matrix product between all in the given order, namely
    :math:`A_0 \cdot A_1 \cdot A_2 \cdot .. \cdot A_N`.

    i    i   (   t   theanot   tensort   dot(   t   argst   rvalt   a(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR!   ~   s    
t	   AllocDiagc           B` s8   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z RS(   sJ   
    Allocates a square matrix with the given vector as its diagonal.
    c         C` s[   t  | ƒ } | j j d k r0 t d | ƒ ‚ n  t |  | g t j j d | j j ƒ g ƒ S(   Ni   s   AllocDiag only works on vectorsR   (	   R   R   R   t	   TypeErrorR   R-   R.   t   matrixR   (   R
   t   _xR   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   “   s    c         C` s   t  | d ƒ g S(   Ni    (   t   extract_diag(   R
   R   R#   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR&   ™   s    c         C` sG   | \ } | \ } | j  d k r0 t | ƒ ‚ n  t j | ƒ | d <d  S(   Ni   i    (   R   R4   R   t   diag(   R
   R   R   R   R   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   œ   s
    		c         C` s   | \ } | d | d f g S(   Ni    (    (   R
   R   R+   t   x_s(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR,   £   s    	(    (   R   R   R   R   R   R&   R   R,   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR3   Œ   s   			t   ExtractDiagc           B` sM   e  Z d  Z d Z e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   sQ   Return the diagonal of a matrix.

    Notes
    -----
    Works on the GPU.

    t   viewc         C` s,   | |  _  |  j  r( i d g d 6|  _ n  d  S(   Ni    (   R;   t   view_map(   R
   R;   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   µ   s    		c         C` s|   t  | t j ƒ s! t | ƒ } n | } | j j d k rK t d | ƒ ‚ n  | j j d t f ƒ ƒ  } t	 |  | g | g ƒ S(   Ni   s"   ExtractDiag only works on matricest   broadcastable(
   t
   isinstanceR-   t   VariableR   R   R   R4   t   clonet   FalseR   (   R
   R6   R   t   y(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   º   s    c         C` sÞ   | \ } | \ } | j  d d k s8 | j  d d k rY | j d j j d ƒ | d <d S| j  d | j  d k  rŒ | d d … d f } n
 | d } | j d | j d f | _ |  j rÊ | | d <n | j ƒ  | d <d S(   sR    For some reason numpy.diag(x) is really slow, so we
        implemented our own. i    i   N(   i    (   t   shapeR   R   t   value_zerost   stridesR;   t   copy(   R
   R   t   inst   outsR   R   R1   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   Å   s    		&
	c         C` s   d |  j  S(   Ns   ExtractDiag{view=%s}(   R;   (   R
   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   __str__Ú   s    c         C` s`   t  j j | d ƒ } t | d ƒ } t  j j | d  | j d … d  | j d … f | ƒ g S(   Ni    i   (   R-   R.   t
   zeros_liket
   alloc_diagt   set_subtensorRC   (   R
   R   R#   R   t   xdiag(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR&   Ý   s
    	'c         C` s/   | \ } t  j j | j d j ƒ } | f g S(   Ni    (   R-   R.   t   minR   RC   (   R
   R   R+   R9   t   shp(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR,   ä   s    	(   s   view(   R   R   R   R   RA   R   R   R   RI   R&   R,   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR:   ª   s   				c         C` sW   t  |  ƒ } | j j d k r( t | ƒ S| j j d k rD t | ƒ St d |  ƒ ‚ d S(   sÛ   
    Numpy-compatibility method
    If `x` is a matrix, return its diagonal.
    If `x` is a vector return a matrix with it as its diagonal.

    * This method does not support the `k` argument that numpy supports.
    i   i   s'   diag requires vector or matrix argumentN(   R   R   R   RK   R7   R4   (   R   t   xx(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR8   í   s    

c         C` s   t  |  ƒ j ƒ  S(   sp   
    Returns the sum of diagonal elements of matrix X.

    Notes
    -----
    Works on GPU since 0.6rc4.

    (   R7   t   sum(   t   X(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   traceþ   s    	t   Detc           B` sA   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s?   
    Matrix determinant. Input should be a square matrix.

    c         C` sO   t  | ƒ } | j d k s! t ‚ t j j d | j ƒ } t |  | g | g ƒ S(   Ni   R   (   R   R   R   R-   R.   t   scalarR   R   (   R
   R   t   o(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s    c         C` sf   | \ } | \ } y, t  j t  j j | ƒ d | j ƒ| d <Wn! t k
 ra t d | ƒ ‚  n Xd  S(   NR   i    s   Failed to compute determinant(   R   t   asarrayR   t   detR   t	   Exceptiont   print(   R
   R   R   R   R   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s    		,c         C` s0   | \ } | \ } | |  | ƒ t  | ƒ j g S(   N(   t   matrix_inverseR"   (   R
   R   R#   R%   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR&   !  s    		c         C` s   d g S(   N(    (    (   R
   R   R+   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR,   &  s    c         C` s   d S(   NRT   (    (   R
   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRI   )  s    (    (	   R   R   R   R   R   R   R&   R,   RI   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRT   
  s   					t   Eigc           B` sA   e  Z d  Z e e j j ƒ Z d Z d „  Z	 d „  Z
 d „  Z RS(   sL   
    Compute the eigenvalues and right eigenvectors of a square array.

    c         C` sj   t  | ƒ } | j d k s! t ‚ t j j d | j ƒ } t j j d | j ƒ } t |  | g | | g ƒ S(   Ni   R   (	   R   R   R   R-   R.   t   vectorR   R5   R   (   R
   R   t   wt   v(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   7  s
    c         C` sU   | \ } | \ } } g  |  j  | ƒ D] } | j | j ƒ ^ q% \ | d <| d <d  S(   Ni    (   t   _numopR   R   (   R
   R   R   R   R   R^   R_   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   >  s    	c         C` s!   | d d } | f | | f g S(   Ni    (    (   R
   R   R+   t   n(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR,   C  s    (    (   R   R   R   t   staticmethodR   R   t   eigR`   R   R   R   R,   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR\   .  s   		t   Eighc           B` sM   e  Z d  Z e e j j ƒ Z d Z d d „ Z	 d „  Z
 d „  Z d „  Z RS(   sV   
    Return the eigenvalues and eigenvectors of a Hermitian or symmetric matrix.

    t   UPLOt   Lc         C` s   | d k s t  ‚ | |  _ d  S(   NRf   t   U(   Rf   Rg   (   R   Re   (   R
   Re   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   S  s    c         C` s˜   t  | ƒ } | j d k s! t ‚ |  j t j | j ƒ j ƒ  g g ƒ d j j } t j	 j
 d | ƒ } t j	 j d | j ƒ } t |  | g | | g ƒ S(   Ni   i    R   (   R   R   R   R`   R   R   R   t   nameR-   R.   R]   R5   R   (   R
   R   t   w_dtypeR^   R_   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   W  s    1c         C` s<   | \ } | \ } } |  j  | |  j ƒ \ | d <| d <d  S(   Ni    (   R`   Re   (   R
   R   R   R   R   R^   R_   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   d  s    	c         C` sX   | \ } |  | ƒ \ } } t  | | g | ƒ \ } } t |  j ƒ | | | | | ƒ g S(   sð  The gradient function should return

           .. math:: \sum_n\left(W_n\frac{\partial\,w_n}
                           {\partial a_{ij}} +
                     \sum_k V_{nk}\frac{\partial\,v_{nk}}
                           {\partial a_{ij}}\right),

        where [:math:`W`, :math:`V`] corresponds to ``g_outputs``,
        :math:`a` to ``inputs``, and  :math:`(w, v)=\mbox{eig}(a)`.

        Analytic formulae for eigensystem gradients are well-known in
        perturbation theory:

           .. math:: \frac{\partial\,w_n}
                          {\partial a_{ij}} = v_{in}\,v_{jn}


           .. math:: \frac{\partial\,v_{kn}}
                          {\partial a_{ij}} =
                \sum_{m\ne n}\frac{v_{km}v_{jn}}{w_n-w_m}

        (   t   _zero_disconnectedt   EighGradRe   (   R
   R   R#   R   R^   R_   t   gwt   gv(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR&   i  s    	(   s   UPLO(   R   R   R   Rb   R   R   t   eighR`   R   R   R   R   R&   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRd   J  s   		c         C` s_   g  } xR t  |  | ƒ D]A \ } } t | j t ƒ rJ | j | j ƒ  ƒ q | j | ƒ q W| S(   N(   t   zipR>   R   R   t   appendRJ   (   R   t   gradst   lRV   t   g(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRj   ˆ  s    Rk   c           B` s;   e  Z d  Z d Z d d „ Z d „  Z d „  Z d „  Z RS(   s<   
    Gradient of an eigensystem of a Hermitian matrix.

    Re   Rf   c         C` s^   | d k s t  ‚ | |  _ | d k rB t j |  _ d „  |  _ n t j |  _ d „  |  _ d  S(   NRf   Rg   c         S` s   t  j |  d ƒ S(   Ni   (   R   t   triu(   R2   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   <lambda>Ÿ  s    c         S` s   t  j |  d ƒ S(   Niÿÿÿÿ(   R   t   tril(   R2   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRu   ¢  s    (   Rf   Rg   (   R   Re   R   Rv   t   tri0t   tri1Rt   (   R
   Re   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   š  s    	c         C` sú   t  t | | | | | f ƒ \ } } } } } | j d k sB t ‚ | j d k sW t ‚ | j d k sl t ‚ | j d k s t ‚ | j d k s– t ‚ t j j | j | j | j | j | j ƒ } t j j	 d | ƒ } t
 |  | | | | | g | g ƒ S(   Ni   i   R   (   t   mapR   R   R   R-   RU   t   upcastR   R.   R5   R   (   R
   R   R^   R_   Rl   Rm   t	   out_dtypet   out(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   ¤  s    -c         ` s¸   | \ } ‰ ‰ ‰ ‰ | j  d ‰ t j ‰ ‡ ‡ ‡ ‡ f d †  ‰  t ‡  ‡ ‡ ‡ f d †  t ˆ ƒ Dƒ ƒ } |  j | ƒ |  j | ƒ j } t j | d | j	 d j
 ƒ| d d <d S(   si   
        Implements the "reverse-mode" gradient for the eigensystem of
        a square matrix.

        i    c         ` s)   t  ‡ ‡  ‡ ‡ f d †  t ˆ ƒ Dƒ ƒ S(   Nc         3` se   |  ][ } | ˆ k r ˆ d  d  … | f ˆ  j  ˆ j ˆ d  d  … | f ƒ ˆ ˆ ˆ | Vq d  S(   N(   R"   R/   (   t   .0t   m(   t   VRa   R_   R^   (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pys	   <genexpr>»  s   (   RQ   R   (   Ra   (   t   NR   R_   R^   (   Ra   s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   Gº  s    c         3` sP   |  ]F } ˆ ˆ d  d  … | f ˆ d  d  … | f ˆ | ˆ  | ƒ ƒ Vq d  S(   N(    (   R}   Ra   (   R   t   Wt   outerR_   (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pys	   <genexpr>¾  s   R   N(   RC   R   Rƒ   RQ   R   Rw   Rx   R"   RW   R   R   (   R
   R   R   R   R   Rs   R|   (    (   R   R€   R   R‚   Rƒ   R_   R^   s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   °  s    	
c         C` s   | d g S(   Ni    (    (   R
   R   R+   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR,   Ï  s    (   s   UPLO(   R   R   R   R   R   R   R   R,   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRk   ’  s   
		Rf   c         C` s   t  | ƒ |  ƒ S(   N(   Rd   (   R2   Re   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRn   Ó  s    t   QRFullc           B` sA   e  Z d  Z e e j j ƒ Z d Z d „  Z	 d „  Z
 d „  Z RS(   s¦   
    Full QR Decomposition.

    Computes the QR decomposition of a matrix.
    Factor the matrix a as qr, where q is orthonormal
    and r is upper-triangular.

    t   modec         C` s   | |  _  d  S(   N(   R…   (   R
   R…   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   ä  s    c         C` sš   t  | ƒ } | j d k s' t d ƒ ‚ t j j d | j ƒ } |  j d k ri t j j d | j ƒ } n t j j d | j ƒ } t	 |  | g | | g ƒ S(   Ni   s,   The input of qr function should be a matrix.R   t   raw(
   R   R   R   R-   R.   R5   R   R…   R]   R   (   R
   R   t   qt   r(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   ç  s    c         C` sW   | \ } | \ } } | j  d k s0 t d ƒ ‚ |  j | |  j ƒ \ | d <| d <d  S(   Ni   s,   The input of qr function should be a matrix.i    (   R   R   R`   R…   (   R
   R   R   R   R   R‡   Rˆ   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   ò  s    	(   s   mode(   R   R   R   Rb   R   R   t   qrR`   R   R   R   R   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR„   ×  s   		t   QRIncompletec           B` sA   e  Z d  Z e e j j ƒ Z d Z d „  Z	 d „  Z
 d „  Z RS(   s‘   
    Incomplete QR Decomposition.

    Computes the QR decomposition of a matrix.
    Factor the matrix a as qr and return a single matrix.

    R…   c         C` s   | |  _  d  S(   N(   R…   (   R
   R…   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s    c         C` sU   t  | ƒ } | j d k s' t d ƒ ‚ t j j d | j ƒ } t |  | g | g ƒ S(   Ni   s,   The input of qr function should be a matrix.R   (   R   R   R   R-   R.   R5   R   R   (   R
   R   R‡   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s    c         C` sJ   | \ } | \ } | j  d k s- t d ƒ ‚ |  j | |  j ƒ | d <d  S(   Ni   s,   The input of qr function should be a matrix.i    (   R   R   R`   R…   (   R
   R   R   R   R   R‡   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s
    			(   s   mode(   R   R   R   Rb   R   R   R‰   R`   R   R   R   R   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRŠ   ù  s   		t   reducedc         C` sZ   d d g d d g g } t  t j j | | ƒ t ƒ rF t | ƒ |  ƒ St | ƒ |  ƒ Sd S(   sV  
    Computes the QR decomposition of a matrix.
    Factor the matrix a as qr, where q
    is orthonormal and r is upper-triangular.

    Parameters
    ----------
    a : array_like, shape (M, N)
        Matrix to be factored.

    mode : {'reduced', 'complete', 'r', 'raw'}, optional
        If K = min(M, N), then

        'reduced'
          returns q, r with dimensions (M, K), (K, N)

        'complete'
           returns q, r with dimensions (M, M), (M, N)

        'r'
          returns r only with dimensions (K, N)

        'raw'
          returns h, tau with dimensions (N, M), (K,)

        Note that array h returned in 'raw' mode is
        transposed for calling Fortran.

        Default mode is 'reduced'

    Returns
    -------
    q : matrix of float or complex, optional
        A matrix with orthonormal columns. When mode = 'complete' the
        result is an orthogonal/unitary matrix depending on whether or
        not a is real/complex. The determinant may be either +/- 1 in
        that case.
    r : matrix of float or complex, optional
        The upper-triangular matrix.

    i   i   i   i   N(   R>   R   R   R‰   t   tupleR„   RŠ   (   R2   R…   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR‰     s    +t   SVDc           B` sG   e  Z d  Z e e j j ƒ Z d Z e	 e	 d „ Z
 d „  Z d „  Z RS(   sy  

    Parameters
    ----------
    full_matrices : bool, optional
        If True (default), u and v have the shapes (M, M) and (N, N),
        respectively.
        Otherwise, the shapes are (M, K) and (K, N), respectively,
        where K = min(M, N).
    compute_uv : bool, optional
        Whether or not to compute u and v in addition to s.
        True by default.

    t   full_matricest
   compute_uvc         C` s   | |  _  | |  _ d  S(   N(   RŽ   R   (   R
   RŽ   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   \  s    	c         C` s‹   t  | ƒ } | j d k s' t d ƒ ‚ t j j d | j ƒ } t j j d | j ƒ } t j j d | j ƒ } t |  | g | | | g ƒ S(   Ni   s-   The input of svd function should be a matrix.R   (	   R   R   R   R-   R.   R5   R   R]   R   (   R
   R   R^   t   uR_   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   `  s    c         C` sg   | \ } | \ } } } | j  d k s3 t d ƒ ‚ |  j | |  j |  j ƒ \ | d <| d <| d <d  S(   Ni   s-   The input of svd function should be a matrix.i    (   R   R   R`   RŽ   R   (   R
   R   R   R   R   R^   R   R_   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   h  s    		(   s   full_matricess
   compute_uv(   R   R   R   Rb   R   R   t   svdR`   R   t   TrueR   R   R   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   H  s   	i   c         C` s   t  | | ƒ |  ƒ S(   sÕ  
    This function performs the SVD on CPU.

    Parameters
    ----------
    full_matrices : bool, optional
        If True (default), u and v have the shapes (M, M) and (N, N),
        respectively.
        Otherwise, the shapes are (M, K) and (K, N), respectively,
        where K = min(M, N).
    compute_uv : bool, optional
        Whether or not to compute u and v in addition to s.
        True by default.

    Returns
    -------
    U, V,  D : matrices

    (   R   (   R2   RŽ   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR‘   q  s    t   lstsqc           B` s    e  Z d Z d  „  Z d „  Z RS(   c         C` s‚   t  j j | ƒ } t  j j | ƒ } t  j j | ƒ } t  j |  | | | g t  j j ƒ  t  j j ƒ  t  j j ƒ  t  j j ƒ  g ƒ S(   N(   R-   R.   R   R   R5   t   dvectort   lscalar(   R
   R   RB   t   rcond(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   Œ  s    c         C` sy   t  j j | d | d | d ƒ } | d | d d <| d | d d <t  j | d ƒ | d d <| d | d d <d  S(   Ni    i   i   i   (   R   R   R“   t   array(   R
   R   R   R   t   zz(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   ”  s
    $(    (   R   R   R   R   R   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR“   ˆ  s   	c         C` s3   d } x& t  | ƒ D] } t j | |  ƒ } q W| S(   sƒ   
    Raise a square matrix to the (integer) power n.

    Parameters
    ----------
    M : Tensor variable
    n : Python int
    i   (   R   R-   R/   (   t   MRa   t   resultt   i(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   matrix_powerœ  s    	c         C` s!  t  |  ƒ }  |  j } | d k r0 t d ƒ ‚ ní| d k r| d  k r] t j |  d ƒ d S| d k r| t j t |  ƒ ƒ S| d k r› t j t |  ƒ ƒ S| d k r¼ |  |  j	 ƒ  j
 d Sy% t j t |  | ƒ ƒ d | } Wn t k
 r t d	 ƒ ‚ n X| Sn| d k r| d  k s,| d
 k rGt j t |  d ƒ ƒ d S| d k rrt j t j t |  ƒ d ƒ ƒ S| d k rt j t j t |  ƒ d ƒ ƒ S| d k rÈt j t j t |  ƒ d ƒ ƒ S| d k rót j t j t |  ƒ d ƒ ƒ St d ƒ ‚ n | d k rt d ƒ ‚ n  d  S(   Ni    s   'axis' entry is out of bounds.i   i   g      à?t   infs   -infg      ð?s   Invalid norm order for vectors.t   froiÿÿÿÿs#   We don't support norm witn ndim > 2(   R   R   t
   ValueErrorR'   R.   RQ   t   maxt   absRN   t   nonzeroRC   R4   t   NotImplementedError(   R   t   ordR   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   norm«  s@    	%t	   TensorInvc           B` sM   e  Z d  Z e e j j ƒ Z d Z d d „ Z	 d „  Z
 d „  Z d „  Z RS(   sc   
    Class wrapper for tensorinv() function;
    Theano utilization of numpy.linalg.tensorinv;
    t   indi   c         C` s   | |  _  d  S(   N(   R§   (   R
   R§   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   Ø  s    c         C` s.   t  | ƒ } | j ƒ  } t |  | g | g ƒ S(   N(   R   R   R   (   R
   R2   R|   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   Û  s    c         C` s/   | \ } | \ } |  j  | |  j ƒ | d <d  S(   Ni    (   R`   R§   (   R
   R   R   R   R2   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR   à  s    		c         C` s'   | d |  j  | d |  j   } | g S(   Ni    (   R§   (   R
   R   R+   t   sp(    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR,   å  s     (   s   ind(   R   R   R   Rb   R   R   t	   tensorinvR`   R   R   R   R   R,   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR¦   Ð  s   		i   c         C` s   t  | ƒ |  ƒ S(   s‰  
    Does not run on GPU;
    Theano utilization of numpy.linalg.tensorinv;

    Compute the 'inverse' of an N-dimensional array.
    The result is an inverse for `a` relative to the tensordot operation
    ``tensordot(a, b, ind)``, i. e., up to floating-point accuracy,
    ``tensordot(tensorinv(a), a, ind)`` is the "identity" tensor for the
    tensordot operation.

    Parameters
    ----------
    a : array_like
        Tensor to 'invert'. Its shape must be 'square', i. e.,
        ``prod(a.shape[:ind]) == prod(a.shape[ind:])``.
    ind : int, optional
        Number of first indices that are involved in the inverse sum.
        Must be a positive integer, default is 2.

    Returns
    -------
    b : ndarray
        `a`'s tensordot inverse, shape ``a.shape[ind:] + a.shape[:ind]``.

    Raises
    ------
    LinAlgError
        If `a` is singular or not 'square' (in the above sense).
    (   R¦   (   R2   R§   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR©   ê  s    t   TensorSolvec           B` sD   e  Z d  Z e e j j ƒ Z d Z d d „ Z
 d „  Z d „  Z RS(   se   
    Theano utilization of numpy.linalg.tensorsolve
    Class wrapper for tensorsolve function.

    t   axesc         C` s   | |  _  d  S(   N(   R«   (   R
   R«   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s    c         C` sa   t  | ƒ } t  | ƒ } t j j | j | j ƒ } t j j d | ƒ } t |  | | g | g ƒ S(   NR   (   R   R-   RU   Rz   R   R.   R5   R   (   R
   R2   t   bR{   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s
    c         C` s5   | \ } } | \ } |  j  | | |  j ƒ | d <d  S(   Ni    (   R`   R«   (   R
   R   R   R   R2   R¬   R   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR     s    	(   s   axesN(   R   R   R   Rb   R   R   t   tensorsolveR`   R   R'   R   R   R   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyRª     s   	c         C` s   t  | ƒ |  | ƒ S(   s  
    Theano utilization of numpy.linalg.tensorsolve. Does not run on GPU!

    Solve the tensor equation ``a x = b`` for x.
    It is assumed that all indices of `x` are summed over in the product,
    together with the rightmost indices of `a`, as is done in, for example,
    ``tensordot(a, x, axes=len(b.shape))``.

    Parameters
    ----------
    a : array_like
        Coefficient tensor, of shape ``b.shape + Q``. `Q`, a tuple, equals
        the shape of that sub-tensor of `a` consisting of the appropriate
        number of its rightmost indices, and must be such that
        ``prod(Q) == prod(b.shape)`` (in which sense `a` is said to be
        'square').
    b : array_like
        Right-hand tensor, which can be of any shape.
    axes : tuple of ints, optional
        Axes in `a` to reorder to the right, before inversion.
        If None (default), no reordering is done.
    Returns
    -------
    x : ndarray, shape Q
    Raises
    ------
    LinAlgError
        If `a` is singular or not 'square' (in the above sense).
    (   Rª   (   R2   R¬   R«   (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyR­   $  s    (5   t
   __future__R    R   R   t   loggingR   t	   six.movesR   R-   t   theano.tensorR   t
   theano.gofR   R   t   theano.gradientR   R   R.   t	   getLoggerR   t   loggerR	   R   R   R[   R!   R3   RK   R:   R7   R8   RS   RT   RX   R\   Rc   Rd   Rj   Rk   Rn   R„   RŠ   R‰   R   R‘   R“   Rœ   R¥   R¦   R©   Rª   R'   R­   (    (    (    s5   /tmp/pip-build-X4mzal/theano/theano/tensor/nlinalg.pyt   <module>   sP    	G			?			!		>	
A"2)		%!