o
    Xi#                     @   sf   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	Z
ddlmZ dgZd	d
 ZddefddZdS )z&Byrd-Omojokun Trust-Region SQP method.    )eye   )projections)modified_doglegprojected_cgbox_intersectionsN)normequality_constrained_sqpc                 C   s   t | \}t|S )N)npshapespeye)xn r   b/tmp/pip-target-1s0edx8b/lib/python/scipy/optimize/_trustregion_constr/equality_constrained_sqp.pydefault_scaling   s   r   c           O   
   C   s  d}d}d}d}d}d}d}d}d}d}d}t |\}|d	u r(t |t j }|d	u r3t |t j}t |}|}|
}|}|} |}!|}"||}#z
t|"|\}$}%}&W n tyk }' zt|'d
kretd|'|'d	}'~'ww |%|  }(|||(})t	| |"j
|( t j}*t|!dkrt	|!t jnd}+dddd},d}-||	||-|*|+|||,s>t|"|&|!|| || || }.|)|.|  }/t |!}0t |d t j	|.d  }1||. }2||. }3t|)|/|$|&|0|1|2|3\}4},|.|4 }5d|)|5|5 | j
|5 }6|"|5|! }7t	|!t	|7 }8td|8}8|}9|6dkr |6d| |8  }:t||:}|6 ||8  };||t	|!  }<||#|5 }=| |=\}>}?|>|t	|?  }@|<|@ }A|A|; }B|B|k rt	|.|t	|4 kr|&|? }Ct|5|C||\}D}E}F||#|5|E|C   }G| |G\}H}I|H|t	|I  }J|<|J }K|K|; }L|Fr|L|kr|G}=|H}>|I}?|L}B|B|krt|t	|5 |}n<|B|krt|t	|5 |}n-|B|k rd| d|B  }M|Mt	|5 }N|N|| kr||9 }n|N|| kr|N}n||9 }|B|kr/|=}|>|?}}!||\} }"||}#t|"|\}$}%}&|%|  }(|||(})d}-t	| |"j
|( t j}*t|!dkr,t	|!t jnd}+n|9}d}-||	||-|*|+|||,r||	fS )au  Solve nonlinear equality-constrained problem using trust-region SQP.

    Solve optimization problem:

        minimize fun(x)
        subject to: constr(x) = 0

    using Byrd-Omojokun Trust-Region SQP method described in [1]_. Several
    implementation details are based on [2]_ and [3]_, p. 549.

    References
    ----------
    .. [1] Lalee, Marucha, Jorge Nocedal, and Todd Plantenga. "On the
           implementation of an algorithm for large-scale equality
           constrained optimization." SIAM Journal on
           Optimization 8.3 (1998): 682-706.
    .. [2] Byrd, Richard H., Mary E. Hribar, and Jorge Nocedal.
           "An interior point algorithm for large-scale nonlinear
           programming." SIAM Journal on Optimization 9.4 (1999): 877-900.
    .. [3] Nocedal, Jorge, and Stephen J. Wright. "Numerical optimization"
           Second Edition (2006).
    g333333?g?g:0yE>g      @g       @g      ?g?g?Nzexpected square matrixzThe 'expected square matrix' error can occur if there are more equality constraints than independent variables. Consider how your constraints are set up, or use factorization_method='SVDFactorization'.r   F)niter	stop_condhits_boundary   gؗҜ<r   T)r
   r   fullinfcopyr   
ValueErrorstrdotr   Tlenr   
zeros_likesqrtlinalgr   maxr   )Ofun_and_constrgrad_and_jac	lagr_hessx0fun0grad0constr0jac0stop_criteriastateinitial_penaltyinitial_trust_radiusfactorization_methodtrust_lbtrust_ubscalingPENALTY_FACTORLARGE_REDUCTION_RATIOINTERMEDIARY_REDUCTION_RATIOSUFFICIENT_REDUCTION_RATIOTRUST_ENLARGEMENT_FACTOR_LTRUST_ENLARGEMENT_FACTOR_SMAX_TRUST_REDUCTIONMIN_TRUST_REDUCTIONSOC_THRESHOLD	TR_FACTOR
BOX_FACTORr   r   trust_radiuspenaltyfcbASZLSYevH
optimalityconstr_violationcg_infolast_iteration_faileddnc_tb_ttrust_radius_tlb_tub_tdtdquadratic_modellinearized_constrvpredprevious_penaltynew_penaltypredicted_reductionmerit_functionx_nextf_nextb_nextmerit_function_nextactual_reductionreduction_ratioy_t	intersectx_socf_socb_socmerit_function_socactual_reduction_socreduction_ratio_soctrust_reductionnew_trust_radiusr   r   r   r	      s    


 










 |)__doc__scipy.sparser   r   r   qp_subproblemr   r   r   numpyr
   numpy.linalgr   __all__r   r	   r   r   r   r   <module>   s    