σ
Κ½χXc           @` sα   d  Z  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
 m Z d d l m Z m Z d e f d	     YZ d d
 d d d d e d d d d d d  Z d d  Z d d  Z d   Z d   Z d S(   s,   
Spectral Algorithm for Nonlinear Equations
i    (   t   divisiont   absolute_importt   print_functionN(   t   OptimizeResult(   t   _check_unknown_optionsi   (   t   _nonmonotone_line_search_cruzt   _nonmonotone_line_search_chengt   _NoConvergencec           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyR      s   g:0βyE>gYσψΒn₯iθ  i
   g»½ΧΩί|Ϋ=g      π?t   cruzc   *      ` s:  t  |  | d k r, t d | f   n  d  |
 d k rP   f d   }
 n  | d k rq   f d   } n   f d   } d g } t |  | | | | |  \ } } }  } } d }    | } | |  } t j  g |	  } d	 }   } t } d
 } xέt rΰ| |  } | r6t d | | | f  n  | d k	 rR| | |  n  | | | | k  rvd } t } Pn  t	 |  d | k r¦d | t
 j |  } n t	 |  | k  rΑ| } n  | | } |
 | | |  }  y| | d k rt | | | | d |  \ }! }" }# }$ nB | d k rYt | | |  | | d |  \ }! }" }# }$ } } n  Wn t k
 rnPn X|" | }% |$ | }& t
 j |% |%  t
 j |% |&  } |" } |$ } |#  | d k rΣ| j |#  n  | d 7} qWt | | d | }' t | |  }( t d |' d | d | d |( d | d d |  }) |) S(   s  
    Solve nonlinear equation with the DF-SANE method

    Options
    -------
    ftol : float, optional
        Relative norm tolerance.
    fatol : float, optional
        Absolute norm tolerance.
        Algorithm terminates when ``||func(x)|| < fatol + ftol ||func(x_0)||``.
    fnorm : callable, optional
        Norm to use in the convergence check. If None, 2-norm is used.
    maxfev : int, optional
        Maximum number of function evaluations.
    disp : bool, optional
        Whether to print convergence process to stdout.
    eta_strategy : callable, optional
        Choice of the ``eta_k`` parameter, which gives slack for growth
        of ``||F||**2``.  Called as ``eta_k = eta_strategy(k, x, F)`` with
        `k` the iteration number, `x` the current iterate and `F` the current
        residual. Should satisfy ``eta_k > 0`` and ``sum(eta, k=0..inf) < inf``.
        Default: ``||F||**2 / (1 + k)**2``.
    sigma_eps : float, optional
        The spectral coefficient is constrained to ``sigma_eps < sigma < 1/sigma_eps``.
        Default: 1e-10
    sigma_0 : float, optional
        Initial spectral coefficient.
        Default: 1.0
    M : int, optional
        Number of iterates to include in the nonmonotonic line search.
        Default: 10
    line_search : {'cruz', 'cheng'}
        Type of line search to employ. 'cruz' is the original one defined in
        [Martinez & Raydan. Math. Comp. 75, 1429 (2006)], 'cheng' is
        a modified search defined in [Cheng & Li. IMA J. Numer. Anal. 29, 814 (2009)].
        Default: 'cruz'

    References
    ----------
    .. [1] "Spectral residual method without gradient information for solving
           large-scale nonlinear systems of equations." W. La Cruz,
           J.M. Martinez, M. Raydan. Math. Comp. **75**, 1429 (2006).
    .. [2] W. La Cruz, Opt. Meth. Software, 29, 24 (2014).
    .. [3] W. Cheng, D.-H. Li. IMA J. Numer. Anal. **29**, 814 (2009).

    t   chengR
   s"   Invalid value %r for 'line_search'i   c         ` s     d |  d S(   Ni   i   (    (   t   kt   xt   F(   t   f_0(    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyt   eta_strategyL   s    c         ` s     d  S(   Ng      π?(    (   R   (   t   f_kt   nexp(    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyt   fnormQ   s    c         ` s   t  j j |     S(   N(   t   npt   linalgt   norm(   R   (   R   (    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyt   fmeritU   s    i    g      π?s&   too many function evaluations requireds   iter %d: ||F|| = %g, sigma = %gs   successful convergencei   t   etat   shapeR   t   successt   messaget   funt   nfevt   nit(   R   R
   N(   R   t
   ValueErrort   Nonet
   _wrap_funct   collectionst   dequet   Falset   Truet   printt   absR   t   signR   R   R   t   vdott   appendt   _wrap_resultR   (*   t   funct   x0t   argst   ftolt   fatolt   maxfevR   t   callbackt   dispt   MR   t	   sigma_epst   sigma_0t   line_searcht   unknown_optionsR   R   t   ft   x_kt   x_shapet   F_kt
   is_complexR   t   sigma_kt   F_0_normt   prev_fst   Qt   Ct	   convergedR   t   F_k_normt   dR   t   alphat   xpt   fpt   Fpt   s_kt   y_kR   R   t   result(    (   R   R   R   s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyt   _root_df_sane   st    1
	-		*:

"c   	      ` sγ   t  j |  } | j  t  j  |     j   } t  j |  pQ t  j |  } | j   } d  d <| r©        f d   } t |  } t |  } n        f d   } | |   |  | | f S(   s«  
    Wrap a function and an initial value so that (i) complex values
    are wrapped to reals, and (ii) value for a merit function
    fmerit(x, f) is computed at the same time, (iii) iteration count
    is maintained and an exception is raised if it is exceeded.

    Parameters
    ----------
    func : callable
        Function to wrap
    x0 : ndarray
        Initial value
    fmerit : callable
        Merit function fmerit(f) for computing merit value from residual.
    nfev_list : list
        List to store number of evaluations in. Should be [0] in the beginning.
    maxfev : int
        Maximum number of evaluations before _NoConvergence is raised.
    args : tuple
        Extra arguments to func

    Returns
    -------
    wrap_func : callable
        Wrapped function, to be called as
        ``F, fp = wrap_func(x0)``
    x0_wrap : ndarray of float
        Wrapped initial value; raveled to 1D and complex
        values mapped to reals.
    x0_shape : tuple
        Shape of the initial value array
    f : float
        Merit function at F
    F : ndarray of float
        Residual at x0_wrap
    is_complex : bool
        Whether complex values were mapped to reals

    i   i    c         ` s    d  k r t     n   d c d 7<t |   j   } t j  |     j   } t |  }  |  } | | f S(   Ni    i   (   R   t   _real2complext   reshapeR   t   asarrayt   ravelt   _complex2real(   R   t   zt   vR   R9   (   R.   R   R,   R1   t	   nfev_listt   x0_shape(    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyt	   wrap_funcΨ   s    c         ` so    d  k r t     n   d c d 7<|  j   }  t j  |      j   }  |  } | | f S(   Ni    i   (   R   RO   R   RP   RQ   (   R   R   R9   (   R.   R   R,   R1   RU   RV   (    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyRW   ε   s    (   R   RP   R   RQ   t   iscomplexobjRR   (	   R,   R-   R   RU   R1   R.   R   R=   RW   (    (   R.   R   R,   R1   RU   RV   s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyR!   §   s    (	

	c         C` s=   | r t  |   } n |  } | d k	 r9 | j |  } n  | S(   sA   
    Convert from real to complex and reshape result arrays.
    N(   RN   R    RO   (   RL   R=   R   RS   (    (    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyR+   ρ   s    c         C` s   t  j |  d t j t  j  S(   Nt   dtype(   R   t   ascontiguousarrayt   floatt   viewt
   complex128(   R   (    (    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyRN   ώ   s    c         C` s   t  j |  d t j t  j  S(   NRY   (   R   RZ   t   complexR\   t   float64(   RS   (    (    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyRR     s    (    (    (   t   __doc__t
   __future__R    R   R   R"   t   numpyR   t   scipy.optimizeR   t   scipy.optimize.optimizeR   t
   linesearchR   R   t	   ExceptionR   R    R$   RM   R!   R+   RN   RR   (    (    (    s7   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_spectral.pyt   <module>   s   J	