
    Џkh(7                         d Z ddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZ dededed	ed
edefdZddZdej"                  dej"                  ded
efdZy)a  
This module provides subroutines that ensure the returned X is optimal among all the calculated
points in the sense that no other point achieves both lower function value and lower constraint
violation at the same time. This module is needed only in the constrained case.

Translated from Zaikun Zhang's modern-Fortran reference implementation in PRIMA.

Dedicated to late Professor M. J. D. Powell FRS (1936--2015).

Python translation by Nickolai Belakovski.
    N   )	DEBUGGINGEPS	CONSTRMAXREALMAXFUNCMAX)presentf1c1f2c2ctolreturnc           	      r   t         rt        t        j                  | |g      t        j                  ||g      z        rJ t        t        j                  ||g      t        j                  ||g      z        rJ |dk\  r|dk\  sJ |dk\  sJ d}|xss t        t        j                  | |g      t        j                  | |g      z        xr9 t        t        j                  ||g      t        j                  ||g      z         }|xs | |k  xr ||k  }|xs | |k  xr ||k  }dt        t        t        |dt        z              z  }|xs8 | t        k  xr- ||k  xr& |t        ||      kD  xs t        j                  |      }t         r*|r| |k\  r||k\  rJ |s| |k  r||k  rJ |s| |k  r||k  rJ |S )a  
    This function compares whether FC1 = (F1, C1) is (strictly) better than FC2 = (F2, C2), which
    basically means that (F1 < F2 and C1 <= C2) or (F1 <= F2 and C1 < C2).
    It takes care of the cases where some of these values are NaN or Inf, even though some cases
    should never happen due to the moderated extreme barrier.
    At return, BETTER = TRUE if and only if (F1, C1) is better than (F2, C2).
    Here, C means constraint violation, which is a nonnegative number.
    r   F
   g{Gz?)
r   anynpisnanisposinfmaxr   minr   r   )r
   r   r   r   r   	is_bettercrefs          ]/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/_lib/pyprima/common/selectx.pyisbetterr      s    rxxR)BKKR,AABBBrxxR)BKKR,AABBBQw27""qyy I   Gc"((B8"4r{{B87L"LM  FVYZ\ZbZbdfhjckZloqozoz|~  AC  |D  pE  [E  WF  SFI3b2g2"(I3bBh227I
 CT6I#5677Deb7ldrTzdrCdO?S?cWYW_W_`bWcI "(rRx88rb2g66bR2X66    c                 >    t        |	      rt        |	      }nd}t        |      }t        |      }t        r|dk\  sJ |dk\  sJ dk\  sJ |dk\  r||k  sJ |dk\  sJ t        j                  |d      |k(  rt        j                  |d      |k(  sJ t        j                  |      |k(  sJ t        j
                  |ddd|f         j                         rJ t        t        j
                  |d|       t        j                  |d|       z        rJ t        |d| dt        j
                  |d|       z  t        j                  |d|       z  k        rJ t        t        j
                  |            rJ t        j
                        t        j                        z  rJ  dt        j
                         z  t        j                         z  k  rJ t        |	      t        |
      k(  sJ t        |	      rt        t        j
                  |	      t        j                  |	      z        rJ t        j                  |
d      |k(  rt        j                  |
d      |k(  sJ t        j
                  |
ddd|f         t        j                  |
ddd|f         z  j                         rJ t         fdt        |d| |d|       D              s+t        t        j                  |d| k  |d|  k              r|||||
fS t        j                  t        |d| |d|       D cg c]  \  }}t         ||       c}}      }t        |      |k(  rt        j                  |z
  d      }|dk  r|}nWt        j                  |      r|}n?t        j                  |t               }t        j                   |t               }|||z  z  }t#        |      }t#        |||k\           }t#        |||k\           }t        j$                  j'                  |||kD        j)                         }|dk  s|t        |      k\  rd}d||<   t        |      }t        j*                  |      d   }|dd|f   |ddd|f<   ||   |d| ||   |d| |
|	|
dd|f   |
ddd|f<   ||dd|f<   ||<    ||<   |
|		|	|
dd|f<   |dz  }t        r)|dk\  r||k  sJ t        j                  |d      |k(  rt        j                  |d      |k(  sJ t        j                  |      |k(  sJ t        j                  |      |k(  sJ t        j
                  |ddd|f         j                         rJ t        t        j
                  |d|       t        j                  |d|       z        rJ t        |d| dt        j
                  |d|       z  t        j                  |d|       z  k        rJ t        t        |d| |d|       D cg c]  \  }}t        ||        c}}      rJ t        t        |d| |d|       D cg c]  \  }}t         ||       c}}      rJ t        |
      rt        j                  |
d      |k(  rt        j                  |
d      |k(  sJ t        j
                  |
ddd|f         t        j                  |
ddd|f         z  j                         rJ |||||
fS c c}}w c c}}w c c}}w )a  
    This subroutine saves X, F, and CSTRV in XFILT, FFILT, and CFILT (and CONSTR in CONFILT
    if they are present), unless a vector in XFILT[:, :NFILT] is better than X.
    If X is better than some vectors in XFILT[:, :NFILT] then these vectors will be
    removed. If X is not better than any of XFILT[:, :NFILT], but NFILT == MAXFILT,
    then we remove a column from XFILT according to the merit function
    PHI = FFILT + CWEIGHT * max(CFILT - CTOL, 0)
    N.B.:
    1. Only XFILT[:, :NFILT] and FFILT[:, :NFILT] etc contains valid information,
    while XFILT[:, NFILT+1:MAXFILT] and FFILT[:, NFILT+1:MAXFILT] etc are not
    initialized yet.
    2. We decide whether and X is better than another by the ISBETTER function
    r   r   Nc              3   B   K   | ]  \  }}t        ||        y w)N)r   ).0ffilt_icfilt_icstrvr   fs      r   	<genexpr>zsavefilt.<locals>.<genexpr>~   s#     q;K7GHWgq%6qs   )nanmaskF)r	   lenr   r   sizer   r   r   isneginfziplogical_andlogical_notr   summaximumr   
nan_to_numr   maarrayargmaxwhere)r"   r   cweightr#   xnfiltcfiltffiltxfiltconstrconfiltnum_constraintsnum_varsmaxfiltr    r!   keepcfilt_shiftedphiphimaxr   frefkworstindex_to_keeps   `` `                    r   savefiltrG   B   s     vf+1vH%jG 1}}!||qyyzew...!||wwua H,1Bg1MMMwwu~(((HHU1fuf9-.33555rxxfu.U6E]1KKLLLuVe}q288E&5M+B'BR[[QVW]X]Q^E_'__``` rxx{###HHQK"++a.011A/"++e2DDDEEv''"22226?288F+bkk&.AABBB777A&/9bgggq>QU\>\\\FUF!34r{{71fuf9CU7VV[[]]] qsSXY_Z_S`bghninboOpqrBNN5%=A-uVe}/EFGeUE722 >>^abghninboqvw}x}q~^  AJZ'SZ8AugwE  A  BD 4yG

54<3a<C[[!C **UWH-C--'2C7]**C S=/05$./0U%$,8??AA:3t9,FV IEHHTN1%MQ-.E!VeV)-(E&5M-(E&5Mv1$Q%566E6	 E!U(OE%LE%Lv1"5	QJE zew...wwua H,1Bg1MMMwwu~(((wwu~(((HHU1fuf9-.33555rxxfu.U6E]1KKLLLuVe}q288E&5M+B'BR[[QVW]X]Q^E_'__```[^_dekfk_lnstzuzn{[|}GWwPW'1eTB}~~~[^_dekfk_lnstzuzn{[|}GWwPWE7GTB}~~~7777A&/9bgggq>QU\>\\\FUF!34r{{71fuf9CU7VV[[]]] %w..U AD ~}s   6\
\
\
fhistchistr5   c                 r   t        |       }t        r|dk\  sJ t        j                  |      |k(  sJ t	        t        j
                  |       t        j                  |       z        rJ t	        |dt        j
                  |      z  t        j                  |      z  k        rJ |dk\  sJ |dk\  sJ t	        t        j                  | t        k  |t        k              rt        }t        }nt	        t        j                  | t        k  |t        k              rt        }t        }nFt	        t        j                  | t        k  |t        k              rt        }t        }nt        }t        }t	        t        j                  | |k  ||k              s|dz
  }nCt        j                  ||z
  d      }t        j                  || |k           }	t        j                  t        d|	z        }|dk  r| }
n9t        j                  |      r|}
n!t        j                  | t               ||z  z   }
t        j                  |
t        j                  | |k  ||k                 }t        j                  |t        j                  | |k  |
|k                 }t        j                  | ||k           }t        j                  j                  || |kD        j!                         }t        rO|dk\  r||k  sJ t	        t#        | d| |d|       D cg c]  \  }}t%        ||| |   ||   |       c}}      rJ |S c c}}w )aS  
    This subroutine selects X according to FHIST and CHIST, which represents (a part of) history
    of F and CSTRV. Normally, FHIST and CHIST are not the full history but only a filter, e.g. ffilt
    and CFILT generated by SAVEFILT. However, we name them as FHIST and CHIST because the [F, CSTRV]
    in a filter should not dominate each other, but this subroutine does NOT assume such a property.
    N.B.: CTOL is the tolerance of the constraint violation (CSTRV). A point is considered feasible if
    its constraint violation is at most CTOL. Not that CTOL is absolute, not relative.
    r   r      r&   N)r(   r   r   r)   r   r   r   r,   r   r   r   r/   r   r   r1   r2   argminr+   r   )rH   rI   r5   r   nhistrD   r   koptchist_shiftedcminrB   phiminfhistichistis                 r   selectxrT      s    JE zzwwu~&&&rxxU);;<<<uq288E?2R[[5GGGHHH!||qyy 2>>%'/59+<=>	R^^EGOUY->?	@	R^^EGOUW_=	>r~~edlEDL9:qy 

54<3vvmEDL12 zz#qv&a<C[[!C **UWH--0GGC BNN54<$9NOPQvvmBNN54<$OPQvveMT123uu{{5{6==? qyTE\))gjkpqwrwkxz  AG  BG  {H  hI  JUcU[]cteDk4P  J  K  	K  KK Js   L3
)NN)__doc__numpyr   numpy.typingtypingnptconstsr   r   r   r   r   r	   floatboolr   rG   NDArrayrT    r   r   <module>r_      s   
   ? ? - -E -u -% -u - -`K/\X3;; Xs{{ XU X% Xr   