o
    h6                     @   s   d dl Z d dlmZmZ d dlZd dlmZ G dd deZG dd deZ	G dd	 d	eZ
G d
d dZG dd deZG dd deZG dd deZG dd dZG dd dZdS )    N)ABCabstractmethod)
MapWrapperc                   @   sJ   e Zd ZdZdddZdd Zdd Zed	d
 Zedd Z	dd Z
dS )
VertexBasez"
    Base class for a vertex.
    Nc                 C   s8   || _ t| j | _|durt|| _nt | _|| _dS )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
    r   T/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/optimize/_shgo_lib/_vertex.py__init__   s   
zVertexBase.__init__c                 C   s   | j S N)r   r   r   r   r   __hash__$   s   zVertexBase.__hash__c                 C   s@   |dvrt t|  d| d|dkrt| j| _| jS d S )N)x_az object has no attribute ''r   )AttributeErrortypenparrayr   r   )r   itemr   r   r   __getattr__'   s   
zVertexBase.__getattr__c                 C      t dNzKThis method is only implemented with an associated child of the base class.NotImplementedErrorr   vr   r   r   connect/      zVertexBase.connectc                 C   r   r   r   r   r   r   r   
disconnect4   r"   zVertexBase.disconnectc                 C   s   | 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   r   r   r   star9   s   zVertexBase.starNN)__name__
__module____qualname____doc__r   r   r   r   r!   r#   r&   r   r   r   r   r   	   s    


r   c                       sF   e Zd ZdZ		d fdd	Zdd Zdd	 Zd
d Zdd Z  Z	S )VertexScalarFieldz
    Add homology properties of a scalar field f: R^n --> R associated with
    the geometry built from the VertexBase class
    Nr   c                    s"   t  j|||d d| _d| _dS )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__r   r   r   Q   s   
zVertexScalarField.__init__c                 C   sN   || ur#|| j vr%| j | |j |  d| _d| _d|_d|_dS dS dS )zConnects self to another vertex object v.

        Parameters
        ----------
        v : VertexBase or VertexScalarField object
        TN)r	   r%   r/   r0   r   r   r   r   r!   t   s   
zVertexScalarField.connectc                 C   sB   || j v r| j | |j |  d| _d| _d|_d|_d S d S )NT)r	   remover/   r0   r   r   r   r   r#      s   

zVertexScalarField.disconnectc                    ,    j rt fdd jD  _d _  jS )zMCheck whether this vertex is strictly less than all its
           neighboursc                 3   s    | ]	} j |j k V  qd S r   f.0r    r   r   r   	<genexpr>       z.VertexScalarField.minimiser.<locals>.<genexpr>F)r/   allr	   _minr   r   r   r   	minimiser   s   zVertexScalarField.minimiserc                    r8   )z`
        Check whether this vertex is strictly greater than all its
        neighbours.
        c                 3   s    | ]	} j |j kV  qd S r   r9   r;   r   r   r   r=      r>   z.VertexScalarField.maximiser.<locals>.<genexpr>F)r0   r?   r	   _maxr   r   r   r   	maximiser   s   zVertexScalarField.maximiser)NNNr   Nr   )
r(   r)   r*   r+   r   r!   r#   rA   rC   __classcell__r   r   r5   r   r,   K   s    #	r,   c                       s(   e Zd ZdZ			d fdd	Z  ZS )VertexVectorFieldz
    Add homology properties of a scalar field f: R^n --> R^m associated with
    the geometry built from the VertexBase class.
    Nr   c
           
         s   t  j|||	d td)Nr-   z&This class is still a work in progress)r.   r   r   )
r   r   sfieldvfieldr2   vfield_argsr3   r4   r	   r
   r5   r   r   r      s   zVertexVectorField.__init__)NNr   r   Nr   NN)r(   r)   r*   r+   r   rD   r   r   r5   r   rE      s    rE   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )VertexCacheBasez7Base class for a vertex cache for a simplicial complex.c                 C   s   t  | _d| _d| _d S )Nr   )collectionsOrderedDictcachenfevr
   r   r   r   r   r      s   

zVertexCacheBase.__init__c                 c   s    | j D ]}| j | V  qd S r   )rM   r   r   r   r   __iter__   s   
zVertexCacheBase.__iter__c                 C   s
   | j d S )z%Returns the size of the vertex cache.   r
   r   r   r   r   size   s   
zVertexCacheBase.sizec                 C   sb   t dt | j d}td|  tdt | j d td|  | jD ]	}| j|   q%d S )NzVertex cache of size: :=)lenrM   print	print_out)r   headlenr    r   r   r   rW      s   
zVertexCacheBase.print_outN)r(   r)   r*   r+   r   rO   rR   rW   r   r   r   r   rI      s    rI   c                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  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)Nc                    s   t  j|||d d S )Nr-   )r.   r   r   r5   r   r   r      s   zVertexCube.__init__c                 C   s6   || ur|| j vr| j | |j |  d S d S d S r   )r	   r%   r   r   r   r   r!      s   zVertexCube.connectc                 C   s*   || j v r| j | |j |  d S d S r   )r	   r7   r   r   r   r   r#      s   
zVertexCube.disconnectr'   )r(   r)   r*   r+   r   r!   r#   rD   r   r   r5   r   rY      s
    rY   c                       s&   e Zd Z fddZdddZ  ZS )VertexCacheIndexc                    s   t    t| _dS )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)r.   r   rY   Vertexr   r5   r   r   r      s   

zVertexCacheIndex.__init__Nc                 C   sR   z| j | W S  ty(   |  jd7  _| j|| jd}|| j |< | j |  Y S w )NrP   rQ   )rM   KeyErrorr
   r[   r   r   r	   xvalr   r   r   __getitem__   s   
zVertexCacheIndex.__getitem__r   )r(   r)   r*   r   r_   rD   r   r   r5   r   rZ      s    rZ   c                       s   e Zd Z		d fdd	Zd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d Zdd Zdd Zdd Z  ZS ) VertexCacheFieldNr   rP   c                    s   t    d| _t| _|| _|| _t||| _|| _	|| _
t||| _t | _t | _d| _|| _t|| _|dkrN| j| _|du rH| j| _dS | j| _dS | j| _|du r\| j| _dS | j| _dS )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.

        rJ   FrP   N)r.   r   r
   r,   r[   r1   r2   FieldWrapperwfieldr3   r4   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   r1   r2   r3   r4   rh   r5   r   r   r      s.   

zVertexCacheField.__init__c                 C   s|   z| j | W S  ty=   |  jd7  _| j|| j|| j| j| j| jd}|| j |< | j	| | j
	| | j |  Y S w )NrP   )r1   r	   r
   r2   r3   r4   )rM   r\   r
   r[   r1   r2   r3   r4   re   r%   rf   r]   r   r   r   r_   0  s   
zVertexCacheField.__getitem__c                 C   s   | j  }|d= |S )Npool)__dict__copy)r   	self_dictr   r   r   __getstate__?  s   
zVertexCacheField.__getstate__c                 C   s&   | j d ur	|   |   |   d S r   )r3   rk   rm   proc_minimisersr   r   r   r   process_poolsD  s   
zVertexCacheField.process_poolsc                 C   sR   d|_ t| j| jD ]\}}t||jg|R  dk r&tj|_d|_  d S q
d S NTg        F)	feasiblezipr3   r4   r   anyr   infr:   )r   r    gargsr   r   r   feasibility_checkJ  s   z"VertexCacheField.feasibility_checkc                 C   sb   z| j |jg| jR  |_|  jd7  _W n ty"   tj|_Y nw t|jr/tj|_dS dS )zCompute the scalar field values of a vertex object `v`.

        Parameters
        ----------
        v : VertexBase or VertexScalarField object
        rP   N)	r1   r   r2   r:   rN   r   r   r}   isnanr   r   r   r   compute_sfieldS  s   zVertexCacheField.compute_sfieldc                 C   s,   | j dur| jD ]}| | qt | _dS )zProcess all constraints.N)r3   re   r   r   r   r   r   r   rj   c  s   

zVertexCacheField.proc_gpoolc                 C   sL   g }| j D ]}||j q| | jj|}t| j |D ]\}}||_qdS )z$Process all constraints in parallel.N)re   appendr   ri   rd   gconsr{   rz   )r   gpool_lr    Gr~   r   r   r   ro   k  s   
zVertexCacheField.pproc_gpoolc                 C   s(   | j D ]
}|jr| | qt | _ dS )z6Process all field functions with constraints supplied.N)rf   rz   r   r   r   r   r   r   rn   u  s
   

zVertexCacheField.proc_fpool_gc                 C   s"   | j D ]}| | qt | _ dS )z9Process all field functions with no constraints supplied.N)rf   r   r   r   r   r   r   rl   }  s   
zVertexCacheField.proc_fpool_nogc                 C   s   | j j g }| jD ]}|jr||j q	tj|_q	| 	| j j|}t
||D ]\}}t|}|| | _|  jd7  _q't | _dS )zT
        Process all field functions with constraints supplied in parallel.
        rP   N)rb   funcrf   rz   r   r   r   r}   r:   ri   r{   tuplerN   r   r   fpool_lr    Fvar:   vtr   r   r   rq     s   


zVertexCacheField.pproc_fpool_gc                 C   st   | j j g }| jD ]}||j q	| | j j|}t||D ]\}}t|}|| | _|  j	d7  _	qt
 | _dS )zW
        Process all field functions with no constraints supplied in parallel.
        rP   N)rb   r   rf   r   r   ri   r{   r   r:   rN   r   r   r   r   r   rp     s   

z VertexCacheField.pproc_fpool_nogc                 C   s   | D ]
}|   |  qdS )zCheck for minimisers.N)rA   rC   r   r   r   r   rw     s   
z VertexCacheField.proc_minimisers)Nr   Nr   rP   r   )r(   r)   r*   r   r_   rv   rx   r   r   rj   ro   rn   rl   rq   rp   rw   rD   r   r   r5   r   r`      s     
8	
r`   c                   @       e Zd ZdZdd Zdd ZdS )rc   z=Object to wrap constraints to pass to `multiprocessing.Pool`.c                 C      || _ || _d S r   )r3   r4   )r   r3   r4   r   r   r   r        
zConstraintWrapper.__init__c                 C   sD   d}t | j| jD ]\}}t||g|R  dk rd} |S q	|S ry   )r{   r3   r4   r   r|   )r   v_x_a	vfeasibler~   r   r   r   r   r     s   zConstraintWrapper.gconsN)r(   r)   r*   r+   r   r   r   r   r   r   rc         rc   c                   @   r   )ra   z7Object to wrap field to pass to `multiprocessing.Pool`.c                 C   r   r   )r1   r2   )r   r1   r2   r   r   r   r     r   zFieldWrapper.__init__c                 C   sF   z| j |g| jR  }W n ty   tj}Y nw t|r!tj}|S r   )r1   r2   	Exceptionr   r}   r   )r   r   v_fr   r   r   r     s   

zFieldWrapper.funcN)r(   r)   r*   r+   r   r   r   r   r   r   ra     r   ra   )rK   abcr   r   numpyr   scipy._lib._utilr   r   r,   rE   rI   rY   rZ   r`   rc   ra   r   r   r   r   <module>   s    BZ 8