o
    Ö­jÈ±  ã                   @   s„   d dl Z d dlZd dlmZ ddlmZ e e¡j	Z
e e¡jZdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )é    N)Úqré   )Úget_arrays_tolc           6      K   sÄ  |rft | tjƒr| jdksJ ‚t |¡ | ¡sJ ‚t |tjƒr%|j| jks'J ‚t |tjƒr3|j| jks5J ‚t |tƒs<J ‚t |t	ƒsCJ ‚t
||ƒ}t ||k¡sQJ ‚t || k¡s[J ‚t |¡rd|dksfJ ‚t |d¡}t |d¡}| j}t | ¡} t | ¡}	|dk | dk B |dk| dkB @ }
t | ¡}t |¡}| |
  ||
< d}d}d}|t |
¡k �r6| | }|dt | tdtj | ¡ƒ krÅ�nqzt|||ƒ}W n
 ty×   Y �n_w | | d| krã�nS||ƒ}|| }|tt|ƒ krüt| | dƒ}ntj}t||ƒ}| |d| |   d| k�r�n |tj k|t t || ¡ k @ }|tjk |tt || ¡ k@ }t |tj¡}t |tj¡}t || ||  ||  d¡||< t || ||  ||  d¡||< t |¡}t |¡}t||ƒ}t||ƒ}|dk�rªt ||
 |||
   ||
 ||
 ¡||
< | || 7 } |||d| |   8 }|t||ƒk �rÒ| |
 ||
  | }|||
  | |
  ||
< d||
 < |d7 }n]||k �r||k�rèt |¡}|| ||< nt |¡}|| ||< d|
|< | |
  ||
< d||
 < d}n)||k�rt |ƒ}|| ||< d|
|< ||k�r,t |ƒ}|| ||< d|
|< d	}n|t |
¡k s®| !d
d	¡�r<|�r<t |¡}|	| d| ||ƒ  } t |
¡dk�r+||
 ||
  }!| |
 | |
  }"| |
 ||
  }#t "t|!|" |#d  dƒ¡ }|#||
  |!| |
   ||
< d||
 < |d| k�sªt #|t t ||
 ¡ k¡�r¬�n||
  |   < t $|¡}$t $|¡}%||
 d ||
 d  ||
 d  |$|
< ||
 d ||
 d  ||
 d  |%|
< t "|$|$dk ¡||$dk  |$|$dk< t "|%|%dk ¡||%dk  |%|%dk< t || d¡}&t || d¡}'|$t|& k}|%t|' k}t %|¡}(t %|¡})t |(| |&| |$|  ¡|(|< t |)| |'| |%|  ¡|)|< t |(¡}*t |)¡}+t|*|+ƒ},||ƒ}-||ƒ}||- }.|| }|| }/d}0t&|0d |, d ƒ}0t '|,|0 |,|0¡}1d|1 d|1d   }2|2|#|1 | |1|.  |2|1|/ d||.      }3t |3dk¡�r¶nut (|3¡}4d|1|4 d  d|1|4 d   }5|5||
  |2|4 ||
   ||
< | |5d |- |2|4 |  7 } ||3|4 7 }|,dk �r"|4|0d k�r"|*|,k�rt |(ƒ}|| ||< d|
|< |+|,k�r!t |)ƒ}|| ||< d|
|< nnt |
¡dk�sY|	| d| ||ƒ  | k�r<|}|�r`t ||k¡�sIJ ‚t ||k¡�sSJ ‚tj |¡d| k �s`J ‚|S )ad  
    Minimize approximately a quadratic function subject to bound constraints in
    a trust region.

    This function solves approximately

    .. math::

        \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2}
        s^{\mathsf{T}} H s \quad \text{s.t.} \quad
        \left\{ \begin{array}{l}
            l \le s \le u\\
            \lVert s \rVert \le \Delta,
        \end{array} \right.

    using an active-set variation of the truncated conjugate gradient method.

    Parameters
    ----------
    grad : `numpy.ndarray`, shape (n,)
        Gradient :math:`g` as shown above.
    hess_prod : callable
        Product of the Hessian matrix :math:`H` with any vector.

            ``hess_prod(s) -> `numpy.ndarray`, shape (n,)``

        returns the product :math:`H s`.
    xl : `numpy.ndarray`, shape (n,)
        Lower bounds :math:`l` as shown above.
    xu : `numpy.ndarray`, shape (n,)
        Upper bounds :math:`u` as shown above.
    delta : float
        Trust-region radius :math:`\Delta` as shown above.
    debug : bool
        Whether to make debugging tests during the execution.

    Returns
    -------
    `numpy.ndarray`, shape (n,)
        Approximate solution :math:`s`.

    Other Parameters
    ----------------
    improve_tcg : bool, optional
        If True, a solution generated by the truncated conjugate gradient
        method that is on the boundary of the trust region is improved by
        moving around the trust-region boundary on the two-dimensional space
        spanned by the solution and the gradient of the quadratic function at
        the solution (default is True).

    Notes
    -----
    This function implements Algorithm 6.2 of [1]_. It is assumed that the
    origin is feasible with respect to the bound constraints and that `delta`
    is finite and positive.

    References
    ----------
    .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
       and Software*. PhD thesis, Department of Applied Mathematics, The Hong
       Kong Polytechnic University, Hong Kong, China, 2022. URL:
       https://theses.lib.polyu.edu.hk/handle/200/12294.
    é   ç        r   Fç      $Àç      ð?ç:Œ0âŽyE>ç      à?TÚimprove_tcgç       @ç:Œ0âŽyE¾é   é   çš™™™™™ñ?))Ú
isinstanceÚnpÚndarrayÚndimÚinspectÚ	signatureÚbindÚshapeÚfloatÚboolr   ÚallÚisfiniteÚminimumÚmaximumÚsizeÚcopyÚ
zeros_likeÚcount_nonzeroÚEPSÚmaxÚlinalgÚnormÚ	_alpha_trÚZeroDivisionErrorÚTINYÚabsÚinfÚminÚ	full_likeÚclipÚargminÚ_argminÚgetÚsqrtÚanyÚzerosÚonesÚintÚlinspaceÚargmax)6ÚgradÚ	hess_prodÚxlÚxuÚdeltaÚdebugÚkwargsÚtolÚnÚ	grad_origÚfree_bdÚstepÚsdÚkÚreductÚboundary_reachedÚgrad_sdÚalpha_trÚhess_sdÚcurv_sdÚ
alpha_quadÚalphaÚi_xlÚi_xuÚall_alpha_xlÚall_alpha_xuÚalpha_xlÚalpha_xuÚalpha_bdÚbetaÚi_newÚ	step_baseÚstep_comparatorÚstep_sqÚgrad_sqÚ	grad_stepÚtemp_xlÚtemp_xuÚdist_xlÚdist_xuÚall_t_xlÚall_t_xuÚt_xlÚt_xuÚt_bdÚ	hess_stepÚ	curv_stepÚcurv_step_sdÚ	n_samplesÚ	t_samplesÚ
sin_valuesÚ
all_reductÚi_maxÚ	cos_value© ro   ú_/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/optim.pyÚtangential_byrd_omojokun   sr  @


 

"ÿ
 $ þþ




ý








£`
ÿ
ÿ

"ÿ"ÿ
ÿ
ÿ

þþ


ÿþÿýÿ
ÿÿ

€œhrq   c	           H      K   sJ  |r¯t | tjƒr| jdksJ ‚t |¡ | ¡sJ ‚t |tjƒr%|j| jks'J ‚t |tjƒr3|j| jks5J ‚t |tjƒrH|jdkrH|jd | jksJJ ‚t |tjƒr]|jdkr]|j|jd ks_J ‚t |tjƒrr|jdkrr|jd | jkstJ ‚t |t	ƒs{J ‚t |t
ƒs‚J ‚t||ƒ}
t ||
k¡s�J ‚t ||
 k¡sšJ ‚t ||
 k¡s¤J ‚t |¡r­|dks¯J ‚t |d¡}t |d¡}t |d¡}| j}t | ¡} t | ¡}|dk | dk B }|dk| dkB }|dk||  dkB }t|||||ƒ\}}t | ¡}|dd…|d…f  |dd…|d…f j|   }t |¡}d}d}d}||| k �r#| | }|dt | tdtj | ¡ƒ k�r9�nêzt|||ƒ}W n t�yL   Y �n×w | | d	| k�rY�nÊ||ƒ}|| }|tt|ƒ k�rst| | dƒ}ntj}t||ƒ}| |d
| |   d	| k�r��n–||tj k@ |t t || ¡ k @ }||tjk @ |tt || ¡ k@ }t |tj¡} t |tj¡}!t || ||  ||  d¡| |< t || ||  ||  d¡|!|< t | ¡}"t |!¡}#t|"|#ƒ}$|| }%||%tt |¡ k@ }&t |tj¡}'||& |%|&  |'|&< tj|'tjd�}(t||$|(ƒ}|dk�rNt |||  ||¡}| || 7 } t d|||%  ¡}|||d
| |   8 }|t||$|(ƒk �r|dd…|d…f |dd…|d…f j|   })|)| | }*|*| |) }|d7 }n�||k �rÜ|"|k�r™t  | ¡}+||+ ||+< d||+< n|#|k�r®t  |!¡}+||+ ||+< d||+< n	t  |'¡}+d||+< t|||||ƒ\}}|dd…|d…f  |dd…|d…f j|   }d}n@|"|k�rït!| ƒ}+||+ ||+< d||+< |#|k�rt!|!ƒ}+||+ ||+< d||+< |(|k�rt!|'ƒ}+d||+< t|||||ƒ\}}d}n||| k �s!|	 "dd¡�rÝ|�rÝ||k �rÝt |¡},||k �rÂ|dd…|d…f |dd…|d…f j|  }-|dd…|d…f |dd…|d…f j|   })|-|- }.|)|) }/|)|- }0t #t|.|/ |0d  dƒ¡ }|dd…|d…f |dd…|d…f j|0| |.|     }|d| k�s·t $|t t |¡ k¡�r¹�n	||  }t %|¡}1t %|¡}2t || d¡}3t || d¡}4|| d |3| |3| d|-|     |1|< || d |4| |4| d|-|     |2|< t #|1|1dk ¡||1dk  |1|1dk< t #|2|2dk ¡||2dk  |2|2dk< |1t|3 k}|2t|4 k}t &|¡}5t &|¡}6t |5| |3| |1|  ¡|5|< t |6| |4| |2|  ¡|6|< t |5¡}7t |6¡}8t|7|8ƒ}9t |¡}:||- };|| }%|%| d || || d|;|     |:|< t #|:|:dk ¡|%|:dk  |:|:dk< |:t| k}&t '|¡}<t |<|& ||& |:|&  ¡|<|&< tj|<dd�}=t|9|=ƒ}>||-ƒ}?||ƒ}|-|? }@|| }|-| }Ad}Bt(|Bd |> d ƒ}Bt )|>|B |>|B¡}Cd|C d|Cd   }D|D|0|C | |Dd
|Cd  |@ d|C |A  d
|     }Et |Edk¡�r$nžt *|E¡}Fd|C|F d  d|C|F d   }Gt ||Gd |-  |D|F |  ||¡}| |Gd |? |D|F |  7 } t d||Gd |;  |D|F |%  ¡}||E|F 7 }|>dk �r¼|F|Bd k�r¼|7|>k�r‘t!|5ƒ}+||+ ||+< d||+< |8|>k�r¤t!|6ƒ}+||+ ||+< d||+< |=|>k�r±t!|<ƒ}+d||+< t|||||ƒ\}}nn||k �s<|| d
| ||ƒ  ||, d
|, ||,ƒ  k�rÝ|,}|�r#t||ƒ}
t ||k¡�sïJ ‚t ||k¡�sùJ ‚t || ||
 k¡�sJ ‚t t || ¡|
k¡�sJ ‚tj |¡d| k �s#J ‚|S )af
  
    Minimize approximately a quadratic function subject to bound and linear
    constraints in a trust region.

    This function solves approximately

    .. math::

        \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2}
        s^{\mathsf{T}} H s \quad \text{s.t.} \quad
        \left\{ \begin{array}{l}
            l \le s \le u,\\
            A_{\scriptscriptstyle I} s \le b_{\scriptscriptstyle I},\\
            A_{\scriptscriptstyle E} s = 0,\\
            \lVert s \rVert \le \Delta,
        \end{array} \right.

    using an active-set variation of the truncated conjugate gradient method.

    Parameters
    ----------
    grad : `numpy.ndarray`, shape (n,)
        Gradient :math:`g` as shown above.
    hess_prod : callable
        Product of the Hessian matrix :math:`H` with any vector.

            ``hess_prod(s) -> `numpy.ndarray`, shape (n,)``

        returns the product :math:`H s`.
    xl : `numpy.ndarray`, shape (n,)
        Lower bounds :math:`l` as shown above.
    xu : `numpy.ndarray`, shape (n,)
        Upper bounds :math:`u` as shown above.
    aub : `numpy.ndarray`, shape (m_linear_ub, n)
        Coefficient matrix :math:`A_{\scriptscriptstyle I}` as shown above.
    bub : `numpy.ndarray`, shape (m_linear_ub,)
        Right-hand side :math:`b_{\scriptscriptstyle I}` as shown above.
    aeq : `numpy.ndarray`, shape (m_linear_eq, n)
        Coefficient matrix :math:`A_{\scriptscriptstyle E}` as shown above.
    delta : float
        Trust-region radius :math:`\Delta` as shown above.
    debug : bool
        Whether to make debugging tests during the execution.

    Returns
    -------
    `numpy.ndarray`, shape (n,)
        Approximate solution :math:`s`.

    Other Parameters
    ----------------
    improve_tcg : bool, optional
        If True, a solution generated by the truncated conjugate gradient
        method that is on the boundary of the trust region is improved by
        moving around the trust-region boundary on the two-dimensional space
        spanned by the solution and the gradient of the quadratic function at
        the solution (default is True).

    Notes
    -----
    This function implements Algorithm 6.3 of [1]_. It is assumed that the
    origin is feasible with respect to the bound and linear constraints, and
    that `delta` is finite and positive.

    References
    ----------
    .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
       and Software*. PhD thesis, Department of Applied Mathematics, The Hong
       Kong Polytechnic University, Hong Kong, China, 2022. URL:
       https://theses.lib.polyu.edu.hk/handle/200/12294.
    r   r   r   r   NFr   r   r	   r
   ©ÚinitialTr   r   r   r   r   r   )+r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    Úqr_tangential_byrd_omojokunr!   ÚTr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   Ú	ones_liker6   r7   r8   )Hr9   r:   r;   r<   ÚaubÚbubÚaeqr=   r>   r?   r@   rA   rB   Úfree_xlÚfree_xuÚfree_ubÚn_actÚqrD   rE   ÚresidrF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   Úaub_sdÚi_ubÚall_alpha_ubÚalpha_ubÚ	grad_projrV   rW   rX   Ú	step_projrZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   Útemp_ubÚaub_stepÚall_t_ubÚt_ubÚt_minrf   rg   rh   ri   rj   rk   rl   rm   rn   ro   ro   rp   Ú$constrained_tangential_byrd_omojokunC  s"  S
ÿ

ÿ

ÿ




0
$ÿ
 ($þþ



.








û0


û‹x

..$ÿÿ



ÿ
ÿ
ÿ
ÿ

þþ




ÿ
ÿ
þ
ÿ
ÿþþþÿ

ÿý
ÿ
þþ



û �
î ÿþ
r‹   c           N      K   sî  |r£t | tjƒr| jdksJ ‚t |tjƒr"|jdkr"|j| jd ks$J ‚t |tjƒr9|jdkr9|jd | jd ks;J ‚t |tjƒrN|jdkrN|j|jd ksPJ ‚t |tjƒr_|j| jd fksaJ ‚t |tjƒrp|j| jd fksrJ ‚t |tƒsyJ ‚t |tƒs€J ‚t||ƒ}	t 	||	k¡sŽJ ‚t 	||	 k¡s˜J ‚t 
|¡r¡|dks£J ‚t |d¡}t |d¡}| j\}
}tj|j|  t d| ¡f }|dk |d|… dk B }|dk|d|… dkB }|dk }|dk| |d|…  ||d…  dkB }t| ||||ƒ\}}t || ||d… ||d…   ¡}t |¡}|dd…|d…f  |dd…|d…f j|  }|||d…  }d}d}d}|||
 | k �rÎ|| }|dt | tdtj |¡ƒ k�r]�nqzt||d|… |ƒ}W n t�yv   tj}Y nw zt|t||d… ||d… |ƒƒ}W n
 t�y”   Y nw | | d	| k�r¡�n-tj|j||d|…   ||d… f }|| }|tt|ƒ k�rËt| | dƒ}ntj}t||ƒ}| |d
| |   d	| k�rå�né||tj k@ |d|… t t || ¡ k @ } ||tjk @ |d|… tt || ¡ k@ }!|||d… t t ||d… ¡ k @ }"t |tj¡}#t |tj¡}$t |tj¡}%t ||  ||   |d|… |   d¡|#| < t ||! ||!  |d|… |!  d¡|$|!< t ||d… |"  ||d… |"  d¡|%|"< t |#¡}&t |$¡}'tj|%tjd�}(t|&|'|(ƒ})| |d|…  ||d…  }*||*tt |¡ k@ }+t |tj¡},||+ |*|+  |,|+< tj|,tjd�}-t||)|-ƒ}|dk�rÿt |||d|…   ||¡}||| 7 }t d|||*  ¡}|||d
| |   8 }|t||)|-ƒk �r0|dd…|d…f |dd…|d…f j|  }.|.| | }/|/| |. }|d7 }n•||k �rœ|&|k�rJt |#¡}0||0 ||0< d||0< n-|'|k�r_t |$¡}0||0 ||0< d||0< n|(|k�rnt |%¡}0d||0< n	t |,¡}0d||0< t| ||||ƒ\}}|dd…|d…f  |dd…|d…f j|  }d}n)|&|k�r¯t|#ƒ}0||0 ||0< d||0< |'|k�rÂt|$ƒ}0||0 ||0< d||0< d}n	|||
 | k �sE|  dd¡�rQ|�rQt !|¡}1||@ }2| jt | | | d¡ |j|| |   }t |¡}t "|2¡dk�r||2 ||2  }3||2 ||2  }4||2 ||2  }5t t|3|4 |5d  dƒ¡ }|5||2  |3||2   ||2< d||2 < |d| k�sUt #|t t ||2 ¡ k¡�rW�nÇ||2  |   < t |¡}6t |¡}7||2 d ||2 d  ||2 d  |6|2< ||2 d ||2 d  ||2 d  |7|2< t |6|6dk ¡||6dk  |6|6dk< t |7|7dk ¡||7dk  |7|7dk< t || d¡}8t || d¡}9|6t|8 k} |7t|9 k}!t $|¡}:t $|¡};t |:|  |8|  |6|   ¡|:| < t |;|! |9|! |7|!  ¡|;|!< t |:¡}<t |;¡}=t|<|=ƒ}>d}?t%|?d |> d ƒ}?t &|>|? |>|?¡}@t | | | d¡}A|| | }Bt !|¡}Cd|C|2 < t '|?¡}Dt(|?ƒD ]F}Ed|@|E  d|@|E d   }Ft ||F||@|E |C    ||¡}Gt | |G | d¡}H||G | }Id
|A|A |B|B  |H|H  |I|I   |D|E< �qEt 	|Ddk¡�r•n‰t )|D¡}Jd|@|J d  d|@|J d   }Kd|@|J  d|@|J d   }F|K||2  |F||2   ||2< | jt | | | d¡ |j|| |   }||D|J 7 }|>dk �r|J|?d k�r|<|>k�rt|:ƒ}0||0 ||0< d|2|0< |=|>k�rt|;ƒ}0||0 ||0< d|2|0< nnt "|2¡dk�st | | | d¡}At | |1 | d¡}L|| | }B||1 | }M|A|A |B|B  |L|L |M|M  k�rQ|1}|�rut 	||k¡�s^J ‚t 	||k¡�shJ ‚tj |¡d| k �suJ ‚|S )a­	  
    Minimize approximately a linear constraint violation subject to bound
    constraints in a trust region.

    This function solves approximately

    .. math::

        \min_{s \in \mathbb{R}^n} \quad \frac{1}{2} \big( \lVert \max \{
        A_{\scriptscriptstyle I} s - b_{\scriptscriptstyle I}, 0 \} \rVert^2 +
        \lVert A_{\scriptscriptstyle E} s - b_{\scriptscriptstyle E} \rVert^2
        \big) \quad \text{s.t.}
        \quad
        \left\{ \begin{array}{l}
            l \le s \le u,\\
            \lVert s \rVert \le \Delta,
        \end{array} \right.

    using a variation of the truncated conjugate gradient method.

    Parameters
    ----------
    aub : `numpy.ndarray`, shape (m_linear_ub, n)
        Matrix :math:`A_{\scriptscriptstyle I}` as shown above.
    bub : `numpy.ndarray`, shape (m_linear_ub,)
        Vector :math:`b_{\scriptscriptstyle I}` as shown above.
    aeq : `numpy.ndarray`, shape (m_linear_eq, n)
        Matrix :math:`A_{\scriptscriptstyle E}` as shown above.
    beq : `numpy.ndarray`, shape (m_linear_eq,)
        Vector :math:`b_{\scriptscriptstyle E}` as shown above.
    xl : `numpy.ndarray`, shape (n,)
        Lower bounds :math:`l` as shown above.
    xu : `numpy.ndarray`, shape (n,)
        Upper bounds :math:`u` as shown above.
    delta : float
        Trust-region radius :math:`\Delta` as shown above.
    debug : bool
        Whether to make debugging tests during the execution.

    Returns
    -------
    `numpy.ndarray`, shape (n,)
        Approximate solution :math:`s`.

    Other Parameters
    ----------------
    improve_tcg : bool, optional
        If True, a solution generated by the truncated conjugate gradient
        method that is on the boundary of the trust region is improved by
        moving around the trust-region boundary on the two-dimensional space
        spanned by the solution and the gradient of the quadratic function at
        the solution (default is True).

    Notes
    -----
    This function implements Algorithm 6.4 of [1]_. It is assumed that the
    origin is feasible with respect to the bound constraints and that `delta`
    is finite and positive.

    References
    ----------
    .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
       and Software*. PhD thesis, Department of Applied Mathematics, The Hong
       Kong Polytechnic University, Hong Kong, China, 2022. URL:
       https://theses.lib.polyu.edu.hk/handle/200/12294.
    r   r   r   r   NFr   r   r	   r
   rr   Tr   r   r   r   r   r   )*r   r   r   r   r   r   r   r   r   r   r   r   r   Úr_ru   Úqr_normal_byrd_omojokunr2   r4   r#   r$   r%   r&   r'   r(   r+   r,   r)   r*   r-   r.   r/   r0   r1   r    r"   r3   r5   r6   r7   ÚemptyÚranger8   )Nrw   rx   ry   Úbeqr;   r<   r=   r>   r?   r@   Úm_linear_ubrA   r9   rz   r{   Ú
free_slackr|   r}   r~   Údelta_slackrD   rE   r   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   Úi_slackrQ   rR   Úall_alpha_slackrS   rT   Úalpha_slackrU   r€   r�   r‚   rƒ   r„   rV   rW   rX   rC   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   ri   rj   Úresid_ubÚresid_eqr…   rl   ÚiÚ	sin_valueÚstep_altÚresid_ub_altÚresid_eq_altrm   rn   Úresid_ub_baseÚresid_eq_basero   ro   rp   Únormal_byrd_omojokunæ  sî  C
ÿ

ÿ

ÿ
""

 (û&
0$
ÿ&ÿ(
 0,(þþ þ


.












ÿ0

ˆ{

ÿ

ÿ

"ÿ"ÿ
ÿ
ÿ

þþ





ýÿþýÿ
ÿ
ÿ
ÿ

€•oÿr    c                 C   sÀ   |j }t |¡}tt |g| | d d …f g|| d d …f  g|| d d …f gg¡jdd�\}}}	t t t |¡¡dt	 | tj
j|d t |j¡…d t |j¡…f dd� k¡}
|
|fS ©NT)Úpivotingg      $@r   )Úaxis)r   r   Úeyer   Úblockru   r"   r*   Údiagr#   r%   r&   r,   r   )rw   ry   rz   r{   r|   rA   Úidentityr~   ÚrÚ_r}   ro   ro   rp   rt   c  s2   
üÿ÷ÿþ.ýÿÿrt   c                 C   s6  | j \}}t |¡}t |¡}tt | | d d …f || d d …f  gt |t |¡ |f¡|| d d …f  g|| d d …f  t |t |¡ |f¡g|| d d …f t |t |¡ |f¡gg¡jdd�\}	}
}t t t 	|
¡¡dt
 ||  tjj|
d t |
j ¡…d t |
j ¡…f dd� k¡}||	fS r¡   )r   r   r¤   r   r¥   r4   r"   ru   r*   r¦   r#   r%   r&   r,   )rw   rz   r{   r’   r|   r‘   rA   Ú
identity_nÚ
identity_mr~   r¨   r©   r}   ro   ro   rp   r�   {  sD   


þþþþóÿëÿþ.ýÿÿr�   c                 C   sœ   | | }|| }|d | |   }t  t|d ||  dƒ¡}|dkr7|tt|| ƒ kr7t|| | dƒ}|S t|| ƒt| krLt|||  dƒ}|S t‚)Nr   r   )r   r2   r$   r)   r*   r(   )rD   rE   r=   Ústep_sdÚsd_sqÚ
dist_tr_sqÚtemprJ   ro   ro   rp   r'      s   üÿr'   c                 C   s   t  | t  | ¡k¡S ©N)r   Úflatnonzeror$   ©Úxro   ro   rp   Ú_argmax®  ó   r´   c                 C   s   t  | t  | ¡k¡S r°   )r   r±   r,   r²   ro   ro   rp   r0   ²  rµ   r0   )r   Únumpyr   Úscipy.linalgr   Úutilsr   Úfinfor   Útinyr)   Úepsr#   rq   r‹   r    rt   r�   r'   r´   r0   ro   ro   ro   rp   Ú<module>   s*      8   &  %