ó
î&]\c           @` s  d  Z  d d l m Z m Z m Z d d l m Z m Z m Z m	 Z	 d d l
 m Z d d l Z d d l
 Z y d d l m Z e Z Wn# e k
 r« d d l Z e Z n Xd d l Z d d l m Z d d	 g Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d d d d d „ Z d S(   s1   Basic linear factorizations needed by the solver.i    (   t   divisiont   print_functiont   absolute_import(   t   bmatt
   csc_matrixt   eyet   issparse(   t   LinearOperatorN(   t   cholesky_AAt(   t   warnt   orthogonalityt   projectionsc         C` s   t  j j | ƒ } t |  ƒ r< t j j j |  d d ƒ} n t  j j |  d d ƒ} | d k sl | d k rp d St  j j |  j | ƒ ƒ } | | | } | S(   s  Measure orthogonality between a vector and the null space of a matrix.

    Compute a measure of orthogonality between the null space
    of the (possibly sparse) matrix ``A`` and a given vector ``g``.

    The formula is a simplified (and cheaper) version of formula (3.13)
    from [1]_.
    ``orth =  norm(A g, ord=2)/(norm(A, ord='fro')*norm(g, ord=2))``.

    References
    ----------
    .. [1] Gould, Nicholas IM, Mary E. Hribar, and Jorge Nocedal.
           "On the solution of equality constrained quadratic
            programming problems arising in optimization."
            SIAM Journal on Scientific Computing 23.4 (2001): 1376-1395.
    t   ordt   froi    (   t   npt   linalgt   normR   t   scipyt   sparset   dot(   t   At   gt   norm_gt   norm_At   norm_A_gt   orth(    (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR
      s    c   	      ` sU   t  ˆ  ƒ ‰ ‡  ‡ ‡ ‡ f d †  } ‡  ‡ f d †  } ‡  ‡ f d †  } | | | f S(   sL   Return linear operators for matrix A using ``NormalEquation`` approach.
    c         ` s–   ˆ ˆ  j  |  ƒ ƒ } |  ˆ  j j  | ƒ } d } x^ t ˆ  | ƒ ˆ k r‘ | ˆ k rY Pn  ˆ ˆ  j  | ƒ ƒ } | ˆ  j j  | ƒ } | d 7} q4 W| S(   Ni    i   (   R   t   TR
   (   t   xt   vt   zt   k(   R   t   factort	   max_refint   orth_tol(    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyt
   null_spaceA   s    c         ` s   ˆ ˆ  j  |  ƒ ƒ S(   N(   R   (   R   (   R   R   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyt   least_squaresS   s    c         ` s   ˆ  j  j ˆ |  ƒ ƒ S(   N(   R   R   (   R   (   R   R   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyt	   row_spaceW   s    (   R   (	   R   t   mt   nR!   R    t   tolR"   R#   R$   (    (   R   R   R    R!   sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyt   normal_equation_projections:   s
    c   	      ` sÕ   t  t t ˆ ƒ ˆ  j g ˆ  d g g ƒ ƒ ‰ y t j j j ˆ ƒ ‰ Wn7 t	 k
 r t
 d ƒ t ˆ  j ƒ  ˆ ˆ ˆ ˆ | ƒ SX‡  ‡ ‡ ‡ ‡ ‡ ‡ f d †  } ‡ ‡ ‡ f d †  } ‡ ‡ f d †  } | | | f S(   s;   Return linear operators for matrix A - ``AugmentedSystem``.sV   Singular Jacobian matrix. Using dense SVD decomposition to perform the factorizations.c         ` s§   t  j |  t  j ˆ ƒ g ƒ } ˆ | ƒ } | ˆ  } d } xf t ˆ  | ƒ ˆ k r¢ | ˆ k rb Pn  | ˆ j | ƒ } ˆ | ƒ } | | 7} | ˆ  } | d 7} q= W| S(   Ni    i   (   R   t   hstackt   zerosR
   R   (   R   R   t   lu_solR   R   t   new_vt	   lu_update(   R   t   KR%   R    R&   R!   t   solve(    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR"   r   s    


c         ` s9   t  j |  t  j ˆ  ƒ g ƒ } ˆ | ƒ } | ˆ ˆ  ˆ !S(   N(   R   R)   R*   (   R   R   R+   (   R%   R&   R/   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR#   ”   s    c         ` s2   t  j t  j ˆ  ƒ |  g ƒ } ˆ | ƒ } | ˆ   S(   N(   R   R)   R*   (   R   R   R+   (   R&   R/   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR$   ¢   s    N(   R   R   R   R   t   NoneR   R   R   t
   factorizedt   RuntimeErrorR	   t   svd_factorization_projectionst   toarray(	   R   R%   R&   R!   R    R'   R"   R#   R$   (    (   R   R.   R%   R    R&   R!   R/   sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyt   augmented_system_projections]   s    -
	!"
c   	      ` sÚ   t  j j ˆ  j d t d d ƒ\ ‰ ‰ ‰ t j j ˆ d d d … f t j ƒ | k  r t d d ƒ t	 ˆ  ˆ | ˆ ˆ | ƒ S‡  ‡ ‡ ‡ ‡ ‡ ‡ f d †  } ‡ ‡ ‡ ‡ f d	 †  } ‡ ‡ ‡ f d
 †  } | | | f S(   sM   Return linear operators for matrix A using ``QRFactorization`` approach.
    t   pivotingt   modet   economiciÿÿÿÿNs5   Singular Jacobian matrix. Using SVD decomposition to s   perform the factorizations.c         ` sé   ˆ j  j |  ƒ } t j j ˆ | d t ƒ} t j ˆ ƒ } | | ˆ <|  ˆ  j  j | ƒ } d } x€ t ˆ  | ƒ ˆ k rä | ˆ k rŠ Pn  ˆ j  j | ƒ } t j j ˆ | d t ƒ} | | ˆ <| ˆ  j  j | ƒ } | d 7} qe W| S(   Nt   loweri    i   (	   R   R   R   R   t   solve_triangulart   FalseR   R*   R
   (   R   t   aux1t   aux2R   R   R   (   R   t   Pt   Qt   RR%   R    R!   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR"   ¾   s    

c         ` sJ   ˆ j  j |  ƒ } t j j ˆ | d t ƒ} t j ˆ ƒ } | | ˆ  <| S(   NR9   (   R   R   R   R   R:   R;   R   R*   (   R   R<   R=   R   (   R>   R?   R@   R%   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR#   ×   s
    
c         ` s>   |  ˆ  } t  j j ˆ | d t d d ƒ} ˆ j | ƒ } | S(   NR9   t   transR   (   R   R   R:   R;   R   (   R   R<   R=   R   (   R>   R?   R@   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR$   à   s    
	(
   R   R   t   qrR   t   TrueR   R   t   infR	   R3   (	   R   R%   R&   R!   R    R'   R"   R#   R$   (    (   R   R>   R?   R@   R%   R    R!   sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyt   qr_factorization_projections¯   s    *.!		c   	      ` s¾   t  j j ˆ  d t ƒ\ ‰ ‰ ‰ ˆ d d … ˆ | k f ‰ ˆ ˆ | k d d … f ‰ ˆ ˆ | k ‰ ‡  ‡ ‡ ‡ ‡ ‡ f d †  } ‡ ‡ ‡ f d †  } ‡ ‡ ‡ f d †  } | | | f S(   sN   Return linear operators for matrix A using ``SVDFactorization`` approach.
    t   full_matricesNc         ` sÄ   ˆ j  |  ƒ } d ˆ | } ˆ j  | ƒ } |  ˆ  j j  | ƒ } d } xu t ˆ  | ƒ ˆ k r¿ | ˆ k rp Pn  ˆ j  | ƒ } d ˆ | } ˆ j  | ƒ } | ˆ  j j  | ƒ } | d 7} qK W| S(   Ni   i    (   R   R   R
   (   R   R<   R=   R   R   R   (   R   t   Ut   VtR    R!   t   s(    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR"   ø   s    c         ` s0   ˆ j  |  ƒ } d ˆ | } ˆ  j  | ƒ } | S(   Ni   (   R   (   R   R<   R=   R   (   RG   RH   RI   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR#     s    c         ` s6   ˆ  j  j |  ƒ } d ˆ | } ˆ j  j | ƒ } | S(   Ni   (   R   R   (   R   R<   R=   R   (   RG   RH   RI   (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR$     s    (   R   R   t   svdR;   (	   R   R%   R&   R!   R    R'   R"   R#   R$   (    (   R   RG   RH   R    R!   RI   sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR3   ì   s    !gê-™—q=i   gVçž¯Ò<c         C` sç  t  j |  ƒ \ } } | | d k r4 t |  ƒ }  n  t |  ƒ rŸ | d	 k rU d } n  | d
 k rp t d ƒ ‚ n  | d k rÏ t rÏ t j d t	 ƒ d } qÏ n0 | d	 k r´ d } n  | d k rÏ t d ƒ ‚ n  | d k rt
 |  | | | | | ƒ \ } } }	 n™ | d k r5t |  | | | | | ƒ \ } } }	 nf | d k rht |  | | | | | ƒ \ } } }	 n3 | d k r›t |  | | | | | ƒ \ } } }	 n  t | | f | ƒ }
 t | | f | ƒ } t | | f |	 ƒ } |
 | | f S(   s  Return three linear operators related with a given matrix A.

    Parameters
    ----------
    A : sparse matrix (or ndarray), shape (m, n)
        Matrix ``A`` used in the projection.
    method : string, optional
        Method used for compute the given linear
        operators. Should be one of:

            - 'NormalEquation': The operators
               will be computed using the
               so-called normal equation approach
               explained in [1]_. In order to do
               so the Cholesky factorization of
               ``(A A.T)`` is computed. Exclusive
               for sparse matrices.
            - 'AugmentedSystem': The operators
               will be computed using the
               so-called augmented system approach
               explained in [1]_. Exclusive
               for sparse matrices.
            - 'QRFactorization': Compute projections
               using QR factorization. Exclusive for
               dense matrices.
            - 'SVDFactorization': Compute projections
               using SVD factorization. Exclusive for
               dense matrices.

    orth_tol : float, optional
        Tolerance for iterative refinements.
    max_refin : int, optional
        Maximum number of iterative refinements
    tol : float, optional
        Tolerance for singular values

    Returns
    -------
    Z : LinearOperator, shape (n, n)
        Null-space operator. For a given vector ``x``,
        the null space operator is equivalent to apply
        a projection matrix ``P = I - A.T inv(A A.T) A``
        to the vector. It can be shown that this is
        equivalent to project ``x`` into the null space
        of A.
    LS : LinearOperator, shape (m, n)
        Least-Square operator. For a given vector ``x``,
        the least-square operator is equivalent to apply a
        pseudoinverse matrix ``pinv(A.T) = inv(A A.T) A``
        to the vector. It can be shown that this vector
        ``pinv(A.T) x`` is the least_square solution to
        ``A.T y = x``.
    Y : LinearOperator, shape (n, m)
        Row-space operator. For a given vector ``x``,
        the row-space operator is equivalent to apply a
        projection matrix ``Q = A.T inv(A A.T)``
        to the vector.  It can be shown that this
        vector ``y = Q x``  the minimum norm solution
        of ``A y = x``.

    Notes
    -----
    Uses iterative refinements described in [1]
    during the computation of ``Z`` in order to
    cope with the possibility of large roundoff errors.

    References
    ----------
    .. [1] Gould, Nicholas IM, Mary E. Hribar, and Jorge Nocedal.
        "On the solution of equality constrained quadratic
        programming problems arising in optimization."
        SIAM Journal on Scientific Computing 23.4 (2001): 1376-1395.
    i    t   AugmentedSystemt   NormalEquations%   Method not allowed for sparse matrix.sm   Only accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.t   QRFactorizationt   SVDFactorizations#   Method not allowed for dense array.N(   RL   RK   (   RM   RN   (   R   t   shapeR   R   R0   t
   ValueErrort   sksparse_availablet   warningsR	   t   ImportWarningR(   R5   RE   R3   R   (   R   t   methodR!   R    R'   R%   R&   R"   R#   R$   t   Zt   LSt   Y(    (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyR   "  s8    J			''''(    t   __doc__t
   __future__R    R   R   t   scipy.sparseR   R   R   R   t   scipy.sparse.linalgR   t   scipy.linalgR   t   sksparse.cholmodR   RC   RQ   t   ImportErrorRR   R;   t   numpyR   R	   t   __all__R
   R(   R5   RE   R3   R0   R   (    (    (    sM   lib/python2.7/site-packages/scipy/optimize/_trustregion_constr/projections.pyt   <module>   s*   "

		#	#	R	=	6