
    sg6                         d dl Z d dlmZmZ d dlZd dlmZ  G d de      Z G d de      Z	 G d d	e      Z
 G d
 d      Z G d de      Z G d de      Z G d de      Z G d d      Z G d d      Zy)    N)ABCabstractmethod)
MapWrapperc                   J    e Zd ZdZd	dZd Zd Zed        Zed        Z	d Z
y)

VertexBasez"
    Base class for a vertex.
    Nc                     || _         t        | j                         | _        |t        |      | _        || _        yt               | _        || _        y)a  
        Initiation of a vertex object.

        Parameters
        ----------
        x : tuple or vector
            The geometric location (domain).
        nn : list, optional
            Nearest neighbour list.
        index : int, optional
            Index of vertex.
        N)xhashsetnnindex)selfr	   r   r   s       S/var/www/html/venv/lib/python3.12/site-packages/scipy/optimize/_shgo_lib/_vertex.py__init__zVertexBase.__init__   sE     L	>"gDG 
 eDG
    c                     | j                   S N)r
   r   s    r   __hash__zVertexBase.__hash__$   s    yyr   c                     |dvrt        t        |        d| d      |dk(  r0t        j                  | j                        | _        | j
                  S y )N)x_az object has no attribute ''r   )AttributeErrortypenparrayr	   r   )r   items     r   __getattr__zVertexBase.__getattr__'   sY    w DJ< 0%%)F!"- . .5=xx'DH88O r   c                     t        d      NzKThis method is only implemented with an associated child of the base class.NotImplementedErrorr   vs     r   connectzVertexBase.connect/       ! #H I 	Ir   c                     t        d      r    r!   r#   s     r   
disconnectzVertexBase.disconnect4   r&   r   c                 r    | j                   | _        | j                  j                  |        | j                  S )a  Returns the star domain ``st(v)`` of the vertex.

        Parameters
        ----------
        v :
            The vertex ``v`` in ``st(v)``

        Returns
        -------
        st : set
            A set containing all the vertices in ``st(v)``
        )r   staddr   s    r   starzVertexBase.star9   s(     ''Dwwr   NN)__name__
__module____qualname____doc__r   r   r   r   r%   r(   r,    r   r   r   r   	   sG    . I I I Ir   r   c                   @     e Zd ZdZ	 	 d fd	Zd Zd Zd Zd Z xZ	S )VertexScalarFieldz
    Add homology properties of a scalar field f: R^n --> R associated with
    the geometry built from the VertexBase class
    c                 F    t         |   |||       d| _        d| _        y)aY  
        Parameters
        ----------
        x : tuple,
            vector of vertex coordinates
        field : callable, optional
            a scalar field f: R^n --> R associated with the geometry
        nn : list, optional
            list of nearest neighbours
        index : int, optional
            index of the vertex
        field_args : tuple, optional
            additional arguments to be passed to field
        g_cons : callable, optional
            constraints on the vertex
        g_cons_args : tuple, optional
            additional arguments to be passed to g_cons

        r   r   TN)superr   	check_min	check_max)	r   r	   fieldr   r   
field_argsg_consg_cons_args	__class__s	           r   r   zVertexScalarField.__init__Q   s)    * 	r/ r   c                     || urb|| j                   vrS| j                   j                  |       |j                   j                  |        d| _        d| _        d|_        d|_        yyy)zConnects self to another vertex object v.

        Parameters
        ----------
        v : VertexBase or VertexScalarField object
        TN)r   r+   r8   r9   r#   s     r   r%   zVertexScalarField.connectt   sY     D=Qdgg-GGKKNDDHHTN "DN!DNAKAK .=r   c                     || j                   v rS| j                   j                  |       |j                   j                  |        d| _        d| _        d|_        d|_        y y )NT)r   remover8   r9   r#   s     r   r(   zVertexScalarField.disconnect   sP    <GGNN1DDKK "DN!DNAKAK r   c                       j                   r*t         fd j                  D               _        d _          j                  S )zMCheck whether this vertex is strictly less than all its
           neighboursc              3   P   K   | ]  }j                   |j                   k    y wr   f.0r$   r   s     r   	<genexpr>z.VertexScalarField.minimiser.<locals>.<genexpr>        :QDFFQSSL:   #&F)r8   allr   _minr   s   `r   	minimiserzVertexScalarField.minimiser   s3     >>:$''::DI"DNyyr   c                       j                   r*t         fd j                  D               _        d _          j                  S )z`
        Check whether this vertex is strictly greater than all its
        neighbours.
        c              3   P   K   | ]  }j                   |j                   kD    y wr   rD   rF   s     r   rH   z.VertexScalarField.maximiser.<locals>.<genexpr>   rI   rJ   F)r9   rK   r   _maxr   s   `r   	maximiserzVertexScalarField.maximiser   s3    
 >>:$''::DI"DNyyr   )NNNr2   Nr2   )
r.   r/   r0   r1   r   r%   r(   rM   rQ   __classcell__r>   s   @r   r4   r4   K   s,    
 GI*,!F"		r   r4   c                   *     e Zd ZdZ	 	 	 d fd	Z xZS )VertexVectorFieldz
    Add homology properties of a scalar field f: R^n --> R^m associated with
    the geometry built from the VertexBase class.
    c
                 >    t         
|   |||	       t        d      )Nr6   z&This class is still a work in progress)r7   r   r"   )r   r	   sfieldvfieldr;   vfield_argsr<   r=   r   r   r>   s             r   r   zVertexVectorField.__init__   s%     	r/!"JKKr   )NNr2   r2   Nr2   NN)r.   r/   r0   r1   r   rR   rS   s   @r   rU   rU      s!    
 @B(,04L Lr   rU   c                   (    e Zd ZdZd Zd Zd Zd Zy)VertexCacheBasez7Base class for a vertex cache for a simplicial complex.c                 R    t        j                         | _        d| _        d| _        y )Nr   )collectionsOrderedDictcachenfevr   r   s    r   r   zVertexCacheBase.__init__   s      ,,.
	
r   c              #   P   K   | j                   D ]  }| j                   |     y wr   )r`   r#   s     r   __iter__zVertexCacheBase.__iter__   s*      	 A**Q-	 s   $&c                      | j                   dz   S )z%Returns the size of the vertex cache.   r   r   s    r   sizezVertexCacheBase.size   s    zzA~r   c                     t        dt        | j                         d      }t        d|z         t        dt        | j                         d       t        d|z         | j                  D ]  }| j                  |   j                          ! y )NzVertex cache of size: :=)lenr`   print	print_out)r   headlenr$   s      r   rm   zVertexCacheBase.print_out   sy    .s4::.?qABcGm&s4::&7q9:cGm 	&AJJqM##%	&r   N)r.   r/   r0   r1   r   rc   rg   rm   r2   r   r   r[   r[      s    A
&r   r[   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )
VertexCubezVertex class to be used for a pure simplicial complex with no associated
    differential geometry (single level domain that exists in R^n)c                 *    t         |   |||       y )Nr6   )r7   r   )r   r	   r   r   r>   s       r   r   zVertexCube.__init__   s    r/r   c                     || urF|| j                   vr7| j                   j                  |       |j                   j                  |        y y y r   )r   r+   r#   s     r   r%   zVertexCube.connect   s9    D=Qdgg-GGKKNDDHHTN .=r   c                     || j                   v r7| j                   j                  |       |j                   j                  |        y y r   )r   rA   r#   s     r   r(   zVertexCube.disconnect   s2    <GGNN1DDKK r   r-   )r.   r/   r0   r1   r   r%   r(   rR   rS   s   @r   rp   rp      s    F0
r   rp   c                   &     e Zd Z fdZddZ xZS )VertexCacheIndexc                 8    t         |           t        | _        y)z
        Class for a vertex cache for a simplicial complex without an associated
        field. Useful only for building and visualising a domain complex.

        Parameters
        ----------
        N)r7   r   rp   Vertex)r   r>   s    r   r   zVertexCacheIndex.__init__   s     	 r   c                     	 | j                   |   S # t        $ rS | xj                  dz  c_        | j                  || j                        }|| j                   |<   | j                   |   cY S w xY w)Nre   rf   )r`   KeyErrorr   rw   r   r	   r   xvals       r   __getitem__zVertexCacheIndex.__getitem__   sd    		!::a=  	!JJ!OJ;;q

;3D !DJJqM::a= 	!s    AA-,A-r   )r.   r/   r0   r   r|   rR   rS   s   @r   ru   ru      s    	!
!r   ru   c                   n     e Zd Z	 	 d fd	ZddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )VertexCacheFieldc                    t         |           d| _        t        | _        || _        || _        t        ||      | _        || _	        || _
        t        ||      | _        t               | _        t               | _        d| _        || _        t%        |      | _        |dk(  r7| j(                  | _        || j,                  | _        y| j0                  | _        y| j2                  | _        || j4                  | _        y| j6                  | _        y)a  
        Class for a vertex cache for a simplicial complex with an associated
        field.

        Parameters
        ----------
        field : callable
            Scalar or vector field callable.
        field_args : tuple, optional
            Any additional fixed parameters needed to completely specify the
            field function
        g_cons : dict or sequence of dict, optional
            Constraints definition.
            Function(s) ``R**n`` in the form::
        g_cons_args : tuple, optional
            Any additional fixed parameters needed to completely specify the
            constraint functions
        workers : int  optional
            Uses `multiprocessing.Pool <multiprocessing>`) to compute the field
             functions in parallel.

        r]   Fre   N)r7   r   r   r4   rw   r:   r;   FieldWrapperwfieldr<   r=   ConstraintWrapperwgconsr   gpoolfpoolsfc_lockworkersr   _mapwrapper
proc_gpoolprocess_gpoolproc_fpool_nogprocess_fpoolproc_fpool_gpproc_gpoolpproc_fpool_nogpproc_fpool_g)r   r:   r;   r<   r=   r   r>   s         r   r   zVertexCacheField.__init__   s    0 	
'
$"5*5&'<U
 U
%g.a<!%D~%)%8%8"%)%6%6"!%!1!1D~%)%9%9"%)%7%7"r   c           
         	 | j                   |   S # t        $ r | xj                  dz  c_        | j                  || j                  || j                  | j
                  | j                  | j                        }|| j                   |<   | j                  j                  |       | j                  j                  |       | j                   |   cY S w xY w)Nre   )r:   r   r   r;   r<   r=   )r`   ry   r   rw   r:   r;   r<   r=   r   r+   r   rz   s       r   r|   zVertexCacheField.__getitem__0  s    	!::a=  
	!JJ!OJ;;q

r*.//&*kk+/+;+;  =D
 !DJJqMJJNN4 JJNN4 ::a= 
	!s    B<CCc                 @    | j                   j                         }|d= |S )Npool)__dict__copy)r   	self_dicts     r   __getstate__zVertexCacheField.__getstate__?  s"    MM&&(	fr   c                 |    | j                   | j                          | j                          | j                          y r   )r<   r   r   proc_minimisersr   s    r   process_poolszVertexCacheField.process_poolsD  s0    ;;" r   c                     d|_         t        | j                  | j                        D ]L  \  }}t	        j
                   ||j                  g| dk        s0t        j                  |_        d|_          y  y NTg        F)	feasiblezipr<   r=   r   anyr   infrE   )r   r$   gargss       r   feasibility_checkz"VertexCacheField.feasibility_checkJ  s_    
4;;(8(89 	GAtvvaoo+,ff"
	r   c                 :   	  | j                   |j                  g| j                   |_        | xj                  dz  c_        t        j                  |j                        rt        j                  |_        yy# t
        $ r t        j                  |_        Y Vw xY w)zCompute the scalar field values of a vertex object `v`.

        Parameters
        ----------
        v : VertexBase or VertexScalarField object
        re   N)	r:   r   r;   rE   ra   r   r   r   isnanr#   s     r   compute_sfieldzVertexCacheField.compute_sfieldS  sp    	$**QUU5T__5ACIINI 88ACC=&&AC   	&&AC	s   AA9 9BBc                 ~    | j                   "| j                  D ]  }| j                  |        t               | _        y)zProcess all constraints.N)r<   r   r   r   r#   s     r   r   zVertexCacheField.proc_gpoolc  s8    ;;"ZZ *&&q)* U
r   c                     g }| j                   D ]  }|j                  |j                          | j                  | j                  j
                  |      }t        | j                   |      D ]  \  }}||_         y)z$Process all constraints in parallel.N)r   appendr   r   r   gconsr   r   )r   gpool_lr$   Gr   s        r   r   zVertexCacheField.pproc_gpoolk  sm     	"ANN155!	" T[[..8

A& 	DAqAJ	r   c                     | j                   D ]   }|j                  s| j                  |       " t               | _         y)z6Process all field functions with constraints supplied.N)r   r   r   r   r#   s     r   r   zVertexCacheField.proc_fpool_gu  s5     	'Azz##A&	' U
r   c                 f    | j                   D ]  }| j                  |        t               | _         y)z9Process all field functions with no constraints supplied.N)r   r   r   r#   s     r   r   zVertexCacheField.proc_fpool_nog}  s.     	#A"	# U
r   c                    | j                   j                   g }| j                  D ]?  }|j                  r|j	                  |j
                         +t        j                  |_        A | j                  | j                   j                  |      }t        ||      D ]/  \  }}t        |      }|| |   _        | xj                  dz  c_        1 t               | _        y)zT
        Process all field functions with constraints supplied in parallel.
        re   N)r   funcr   r   r   r   r   r   rE   r   r   tuplera   r   r   fpool_lr$   FvarE   vts          r   r   zVertexCacheField.pproc_fpool_g  s     	 	Azzquu%ff		
 T[[--w7!_ 	EBrBDHJIINI	
 U
r   c                 r   | j                   j                   g }| j                  D ]  }|j                  |j                          | j                  | j                   j                  |      }t        ||      D ]/  \  }}t        |      }|| |   _        | xj                  dz  c_	        1 t               | _        y)zW
        Process all field functions with no constraints supplied in parallel.
        re   N)r   r   r   r   r   r   r   r   rE   ra   r   r   s          r   r   z VertexCacheField.pproc_fpool_nog  s     	 	"ANN155!	"T[[--w7!_ 	EBrBDHJIINI	
 U
r   c                 R    | D ]"  }|j                          |j                          $ y)zCheck for minimisers.N)rM   rQ   r#   s     r   r   z VertexCacheField.proc_minimisers  s#     	AKKMKKM	r   )Nr2   Nr2   re   r   )r.   r/   r0   r   r|   r   r   r   r   r   r   r   r   r   r   r   rR   rS   s   @r   r~   r~      sK    KM68p!
 & r   r~   c                       e Zd ZdZd Zd Zy)r   z=Object to wrap constraints to pass to `multiprocessing.Pool`.c                      || _         || _        y r   )r<   r=   )r   r<   r=   s      r   r   zConstraintWrapper.__init__  s    &r   c                     d}t        | j                  | j                        D ])  \  }}t        j                   ||g| dk        s&d} |S  |S r   )r   r<   r=   r   r   )r   v_x_a	vfeasibler   r   s        r   r   zConstraintWrapper.gcons  sZ    	4;;(8(89 	GAtvvaoo+,!		
 r   N)r.   r/   r0   r1   r   r   r2   r   r   r   r     s    G'r   r   c                       e Zd ZdZd Zd Zy)r   z7Object to wrap field to pass to `multiprocessing.Pool`.c                      || _         || _        y r   )r:   r;   )r   r:   r;   s      r   r   zFieldWrapper.__init__  s    
$r   c                     	  | j                   |g| j                   }t        j
                  |      rt        j                  }|S # t        $ r t        j                  }Y Bw xY wr   )r:   r;   	Exceptionr   r   r   )r   r   v_fs      r   r   zFieldWrapper.func  sU    	$**U5T__5C 88C=&&C
  	&&C	s   A A"!A"N)r.   r/   r0   r1   r   r   r2   r   r   r   r     s    A%r   r   )r^   abcr   r   numpyr   scipy._lib._utilr   r   r4   rU   r[   rp   ru   r~   r   r   r2   r   r   <module>r      s     #  '? ?DW
 WtL
 L& &4 "! !2t tn   r   