
    sg2                     `    d Z ddlZddlZddlZddlZddlmZ ddlZddl	m
Z
mZ  G d d      Zy)z:Base classes for low memory simplicial complex structures.    N)cache   )VertexCacheFieldVertexCacheIndexc                       e Zd ZdZ	 	 ddZd ZddZ	 	 ddZddZddZ	dd	Z
d
 Zed        Zd Zd ZddZddZddZy)Complexaz  
    Base class for a simplicial complex described as a cache of vertices
    together with their connections.

    Important methods:
        Domain triangulation:
                Complex.triangulate, Complex.split_generation
        Triangulating arbitrary points (must be traingulable,
            may exist outside domain):
                Complex.triangulate(sample_set)
        Converting another simplicial complex structure data type to the
            structure used in Complex (ex. OBJ wavefront)
                Complex.convert(datatype, data)

    Important objects:
        HC.V: The cache of vertices and their connection
        HC.H: Storage structure of all vertex groups

    Parameters
    ----------
    dim : int
        Spatial dimensionality of the complex R^dim
    domain : list of tuples, optional
        The bounds [x_l, x_u]^dim of the hyperrectangle space
        ex. The default domain is the hyperrectangle [0, 1]^dim
        Note: The domain must be convex, non-convex spaces can be cut
              away from this domain using the non-linear
              g_cons functions to define any arbitrary domain
              (these domains may also be disconnected from each other)
    sfield :
        A scalar function defined in the associated domain f: R^dim --> R
    sfield_args : tuple
        Additional arguments to be passed to `sfield`
    vfield :
        A scalar function defined in the associated domain
                       f: R^dim --> R^m
                   (for example a gradient function of the scalar field)
    vfield_args : tuple
        Additional arguments to be passed to vfield
    symmetry : None or list
            Specify if the objective function contains symmetric variables.
            The search space (and therefore performance) is decreased by up to
            O(n!) times in the fully symmetric case.

            E.g.  f(x) = (x_1 + x_2 + x_3) + (x_4)**2 + (x_5)**2 + (x_6)**2

            In this equation x_2 and x_3 are symmetric to x_1, while x_5 and
             x_6 are symmetric to x_4, this can be specified to the solver as:

            symmetry = [0,  # Variable 1
                        0,  # symmetric to variable 1
                        0,  # symmetric to variable 1
                        3,  # Variable 4
                        3,  # symmetric to variable 4
                        3,  # symmetric to variable 4
                        ]

    constraints : dict or sequence of dict, optional
        Constraints definition.
        Function(s) ``R**n`` in the form::

            g(x) <= 0 applied as g : R^n -> R^m
            h(x) == 0 applied as h : R^n -> R^p

        Each constraint is defined in a dictionary with fields:

            type : str
                Constraint type: 'eq' for equality, 'ineq' for inequality.
            fun : callable
                The function defining the constraint.
            jac : callable, optional
                The Jacobian of `fun` (only for SLSQP).
            args : sequence, optional
                Extra arguments to be passed to the function and Jacobian.

        Equality constraint means that the constraint function result is to
        be zero whereas inequality means that it is to be
        non-negative.constraints : dict or sequence of dict, optional
        Constraints definition.
        Function(s) ``R**n`` in the form::

            g(x) <= 0 applied as g : R^n -> R^m
            h(x) == 0 applied as h : R^n -> R^p

        Each constraint is defined in a dictionary with fields:

            type : str
                Constraint type: 'eq' for equality, 'ineq' for inequality.
            fun : callable
                The function defining the constraint.
            jac : callable, optional
                The Jacobian of `fun` (unused).
            args : sequence, optional
                Extra arguments to be passed to the function and Jacobian.

        Equality constraint means that the constraint function result is to
        be zero whereas inequality means that it is to be non-negative.

    workers : int  optional
        Uses `multiprocessing.Pool <multiprocessing>`) to compute the field
         functions in parallel.
    Nc                    || _         || _        |dg|z  | _        n|| _        || _        || _        || _        ||| _        g | _        g | _        t        |t        t        f      s|f}|D ]G  }|d   dv s| j                  j                  |d          	 | j                  j                  |d          I t        | j                        | _        t        | j                        | _        nd | _        d | _        d| _        d| _        g | _        || j                  p|1t#        ||| j                  | j                  |      | _        g | _        y | j                  @t#        ||| j                  | j                  |      | _        g | _        y t'               | _        g | _        y # t        $ r | j                  j                  d       Y ^w xY w)	N)        g      ?typeineqfunargs r   )field
field_argsg_consg_cons_argsworkers)dimdomainboundssymmetrysfieldsfield_argsmin_consr   g_args
isinstancetuplelistappendKeyErrorgen
perm_cycleHr   Vr   
V_non_symm)	selfr   r   r   r   r   constraintsr   conss	            T/var/www/html/venv/lib/python3.12/site-packages/scipy/optimize/_shgo_lib/_complex.py__init__zComplex.__init__t   s    >%.3.DK DK  &
 "'DMDKDKkE4=9*n# /<F+KK&&tE{3/**4<8	/  ,DK,DKDKDK   DKK$; !);156:kk29;  ();156:kk29;  &'DFK $ /**2./s   F""$G
	G
c                     | j                   S N)r$   )r'   s    r*   __call__zComplex.__call__   s    vv    c              #     K   t        |      }t        |      }| j                  |    | j                  |   }|j                   | j                  |   j                  | j                  |          | | j                  |   gg}t	        j                  t        |            }	|d   |	d<   | j                  t        |	         }	| j                  |   j                  |	       |	j                   |	gg}
g }t        |dd       D ]  \  }}|j                  g        |
j                  g        	 |d    |d|dz    D cg c]  }|dd 	 }}|
d|dz    D cg c]  }|dd 	 }}t        t        ||            D ]  \  }\  }}t        t        ||            D ]v  \  }\  }}t        |j                        }t        |j                        }||dz      ||dz   <   ||dz      ||dz   <   | j                  t        |         }|j                  |       |j                   | j                  t        |         }|j                  |       |j                  |       |j                  |       |j                  ||f       ||dz      j                  |       ||dz      j                  |       |
|dz      j                  |       |
|dz      j                  |       ||   j                  |       |
|   j                  |       |j                   y  t	        j                  |      }|D ]  }t        |d   j                        }t        |d   j                        }||dz      ||dz   <   ||dz      ||dz   <   | j                  t        |         }| j                  t        |         }|d   j                  |       |j                  |       |j                  |d   |f       |j                  ||f         	 ~~~
~~~|r}| j                  |   }| j                  |   }|j                  |       | j                  ||      }|j                  D ]  }|j                  |        |j                   |j                  S | |S c c}w c c}w # t        $ r ||   }|
|   }||}}t        t        ||            D ]p  \  }\  }}t        |j                        }||dz      ||dz   <   | j                  t        |         }|j                  |       |j                  |       |j                  ||f       ||dz      j                  |       |
|dz      j                  |       |j                  | j                  |          |j                   t	        j                  |      }|D ]  }|d   j                  |   ||   k(  st        |d   j                        }||dz      ||dz   <   | j                  t        |         }|d   j                  |       |j                  |d   |f        s Y 3w xY w# t        $ r Y Cw xY ww)z3Generate initial triangulation using cyclic productr   r   N)r   r%   xconnectcopyr   	enumerater    zip
IndexErrorUnboundLocalError
disconnect
split_edgenn)r'   r   originsupremumcentroidvotvutvoC0xa_voC1xab_Cir1   cC0xcC1xjVLVUkvlvua_vla_vuab_Ccvpb_vab_vvsvcvs                                  r*   cyclic_productzComplex.cyclic_product   s     FmHosVVC[dd
sDFF3K(	 s}oyyf&a&QvveDk"sD!ffvh fQRj) l	7DAqJJrNJJrNg7 !&)&1q5k2!22&)&1q5k2!22#,St_#= '%KAxB'0R'= &%8B#BDDz#BDDz&)!a%jQU&)!a%jQU#vveDk2 

4("ff#vveDk2 

4( T* 

4(RJ/ AE
))"-AE
))"-AE
))$/AE
))$/ Ad+Ad+ #ffM&%'%V 		$ -Br!uww-C1=D!$QUCAJ"%a!e*DQK&&s,C66%+.DqEMM$'KK% KKA.KKd,-ul	7^	
 BBMM"c*BUU 		"$$J44KIJy 32z  #71v1vtB#,SR[#9 7KAxB:D"%a!e*DQK  66%+.D JJt$
 JJt$KKT
+AJ%%b)AJ%%d+LL-&&L IIdOE# 	7a5771:Q/#'1=D*-a!e*DQK#'66%+#6DqEMM$/ !KKA6	7-7	#7Z ! 		sv   D*Y
-R<RRR JR9Y
=X: BY

RD3X7A-X73Y
6X77Y
:	YY
YY
c                    || j                   }| j                  D cg c]  }|d   	 }}|| _        | j                  D cg c]  }|d   	 }}|| _        || j                  }nt	        j                  | j                        }t        |      D ]  \  }}	||	us| j                  ||      d   g||<   | j                  ||      d   g||<   | j                  ||      | j                  ||	      usbt        j                  d| d|	 d| d| j                  ||       d|	 d| j                  ||	       d	       | j                  ||	      ||<    |q| j                  ||||      | _	        | j                  D ]  }|  	 | j                  j                  t        | j                        t        | j                        f       nh	 | j                   	 t        | j                   j"                        |k  r8t%        | j                         t        | j                   j"                        |k  r8|r8| j                   j"                  D ]  }
| j                   |
   j)                          ! yc c}w c c}w # t        t        f$ r3 t        | j                        t        | j                        fg| _
        Y w xY w# t        t        f$ r | j                  ||||      | _	        Y w xY w# t&        $ r 	 | j                  j                  t        | j                        t        | j                        f       nE# t        t        f$ r3 t        | j                        t        | j                        fg| _
        Y nw xY wY Ow xY w)
a  
        Triangulate the initial domain, if n is not None then a limited number
        of points will be generated

        Parameters
        ----------
        n : int, Number of points to be sampled.
        symmetry :

            Ex. Dictionary/hashtable
            f(x) = (x_1 + x_2 + x_3) + (x_4)**2 + (x_5)**2 + (x_6)**2

            symmetry = symmetry[0]: 0,  # Variable 1
                       symmetry[1]: 0,  # symmetric to variable 1
                       symmetry[2]: 0,  # symmetric to variable 1
                       symmetry[3]: 3,  # Variable 4
                       symmetry[4]: 3,  # symmetric to variable 4
                       symmetry[5]: 3,  # symmetric to variable 4
                        }
        centroid : bool, if True add a central point to the hypercube
        printout : bool, if True print out results

        NOTES:
        ------
        Rather than using the combinatorial algorithm to connect vertices we
        make the following observation:

        The bound pairs are similar a C2 cyclic group and the structure is
        formed using the cartesian product:

        H = C2 x C2 x C2 ... x C2 (dim times)

        So construct any normal subgroup N and consider H/N first, we connect
        all vertices within N (ex. N is C2 (the first dimension), then we move
        to a left coset aN (an operation moving around the defined H/N group by
        for example moving from the lower bound in C2 (dimension 2) to the
        higher bound in C2. During this operation connection all the vertices.
        Now repeat the N connections. Note that these elements can be connected
        in parallel.
        Nr   r   z	Variable z* was specified as symmetetric to variable z, however, the bounds z = z and zE do not match, the mismatch was ignored in the initial triangulation.)r   r   r;   r<   r3   r4   loggingwarningrW   cptriangulated_vectorsr    r   AttributeErrorr!   lenr%   r   nextStopIteration	print_out)r'   nr   r=   printoutrE   r;   r<   cboundsrH   rV   s              r*   triangulatezComplex.triangulatef  sp   V }}H $,1!A$,,"&++.QAaD.. kkGii,G!(+ >1A:"&++hqk":1"=!>GAJ #'++hqk":1"=!>GAJHQK0 KK45)A3 7CCD# F889s ;,,0KK,D+EU1#+,,0KK,D+E FE)F G &*[[!%=
#>& 9))'68XNDGWW E))00%2D272F2H I8
	I$&&,,'!+M $&&,,'!+  VV\\ &q	##%& 	G -.H #H- E.3DKK.@.3DMM.B.D -E)E #H- 8--gvx.688 ! II--44eDKK6H6;DMM6J6L M&1 I272D272F2H 1ID-I	Ish   I% I*>AI/ J4 AK# /?J10J14(K K #	M:-AL10M:1?M30M:2M33M:9M:c                    |	 | j                    | j                          y t        | j                  j                        |z   }t        | j                  j                        |k  rF	 | j                    	 t        | j                         t        | j                  j                        |k  rFy # t        $ r5}t        |      dk(  r!| j	                  | j
                         Y d }~y  d }~ww xY w# t        t        t        f$ rH | j                   d   } | j                  |d| j                  i| _
        t        | j                         Y w xY w# t        t        f$ r | j	                  || j
                         Y w xY w)N8'Complex' object has no attribute 'triangulated_vectors')r   r   r   )r\   
refine_allr]   strre   r   r^   r%   r   r_   rlsr`   r!   refine_local_spacer   )r'   rb   aentrQ   s        r*   refinezComplex.refine  sG   9
))! " $&&,,"$4 ))#N $&&,,"$* 	A " r7 7 7$$dmm$<* 'x@ #2215B6t66O4;;ODHN#
 #H- 4
   T]]34sN   B, &E 3C- ,	C*5*C%$C%%C*-AE	E E		E +E:9E:c                 r   	 | j                    t        j                  | j                         }t        |      D ]9  \  }} | j                  |d| j                  i| _        | j
                  D ]  }|  ; 	 y# t        $ r7}t        |      dk(  r| j                  | j                  |       n Y d}~yd}~ww xY w)z0Refine the entire domain of the current complex.r   rg   )r   r=   N)
r\   r3   r4   rk   r   rj   r]   ri   re   r   )r'   	centroidstvsrE   rQ   rl   s         r*   rh   zComplex.refine_all  s    	%%))D556C"3 22422BKt{{K A 	  	2w 3 3  $--) L M 		s   A2A6 6	B6?-B11B6c           
   #   .  K   t        j                   |      }t        j                   |      }d\  }}}	t        |      }
t        |      }t        |      }t        |      }t        |      D ]!  \  }}|
|   ||   kD  s||   ||<   |
|   ||<   # t        |      }t        |      }| j                  |   }| j                  |   }| j                  |j                  |j                        }t        j                   |j                        }t        j                   t        |            }|d   |d<   t        |      | j                  j                  vr| j                  |   }| j                  |   }| j                  |j                  |j                        }t        j                   |j                        }t        j                   t        |            }|d   |d<   | j                  t        |         }n| j                  t        |         }| j                  |j                  |j                        }|j                  |       |j                   |gg}|gg}|gg}g }g }t        |dd        D ]  \  }}|j                  g        |j                  g        |j                  g        	 t        |      }||dz      ||dz   <   |d |dz    D cg c]  }|d d  	 }}|d |dz    D cg c]  }|d d  	 } }|d |dz    D cg c]  }|d d  	 }!}t        j                   |      }"t        j                   |      }#t        |      | j                  j                  vrt        t        |      }$||dz      |$|dz   <   t        |$      | j                  j                  vrt        |#D ]  }%t        |%d   j                        }&t        |%d   j                        }'t        |%d   j                        }(t        |%d   j                        })||dz      |&|dz   <   ||dz      |'|dz   <   ||dz      |(|dz   <   ||dz      |)|dz   <   | j                  t        |&         }&|&j                  |       |& | j                  |%d   j                  |&j                        }*|*j                  |&       |*j                  |%d          |*j                  |%d          |*j                  |%d          |*j                   | j                  t        |'         }'|&j                  |'       |*j                  |'       |' | j                  t        |(         }(|&j                  |(       |*j                  |(       | j                  |(j                  |'j                        }+|&j                  |+       |( | j                  t        |)         })|&j                  |)       |*j                  |)       | j                  |%d   j                  |)j                        },| j                  |'j                  |)j                        }-| j                  |(j                  |)j                        }.|&j                  |.       |,j                   |-j                   |) | j                  |%d   j                  |&j                        }*|*j                  |       |*j                   | j                  |%d   j                  |'j                        }/|*j                  |       |*j                  |/       |/j                   | j                  |%d   j                  |(j                        }0|*j                  |       |*j                  |0       |0j                   | j                  |%d   j                  |)j                        }1|*j                  |       |*j                  |1       |1 |||	|'|(|)g}2t        j                  |2d      }3|3D ].  }4| j                  |4d   j                  |4d   j                         0 |j                  |*|%d   |'|	|)f       |j                  |*||'|	|)f        |"D ]]  }%t        |%d   j                        }&t        |%d   j                        }'t        |%d   j                        }(t        |%d   j                        }5t        |%d   j                        })||dz      |&|dz   <   ||dz      |'|dz   <   ||dz      |(|dz   <   ||dz      |5|dz   <   ||dz      |)|dz   <   | j                  t        |&         }&|&j                  |       |& | j                  |%d   j                  |&j                        }*|*j                  |&       |*j                  |%d          |*j                  |%d          |*j                  |%d          |*j                  |%d          |*j                   | j                  t        |'         }'|&j                  |'       |*j                  |'       |' | j                  t        |(         }(|&j                  |(       |*j                  |(       |( | j                  t        |5         }5|&j                  |5       |*j                  |5       | j                  |(j                  |5j                         |5 | j                  t        |)         })|&j                  |)       |*j                  |)       | j                  |%d   j                  |)j                        },| j                  |'j                  |)j                        }-|,j                   |-j                   |) | j                  |%d   j                  |&j                        }*|*j                  |       |*j                   | j                  |%d   j                  |'j                        }/|*j                  |       |*j                  |/       |/j                   | j                  |%d   j                  |(j                        }0|*j                  |       |*j                  |0       |0j                   | j                  |%d   j                  |5j                        }6|*j                  |       |*j                  |6       |6 | j                  |%d   j                  |)j                        }1|*j                  |       |*j                  |1       |1 |%\  }7}}}}	||||	|'|(|5|)g}2t        j                  |2d      }3|3D ].  }4| j                  |4d   j                  |4d   j                         0 |j                  |&|'|(|5|)f       |j                  |*|/|0|6|1f       |j                  |*|%d   |'|	|)f       |j                  |*||(||5f       ` t        t        || |!            D ]  \  }8\  }9}:};t        t        |9|:|;            D ]  \  }<\  }}=}t        |j                        }t        |j                        }	||dz      ||dz   <   ||dz      |	|dz   <   | j                  t        |         }| j                  t        |	         }	| j                  |j                  |	j                        }7| j                  |j                  |j                         |7j                  |       |7j                  |=       |7j                  |       |7j                  |       |7j                  |       |7j                  |	       |7j                   | j                  |j                  |j                        }>|>j                  |       |7j                  |>       |>j                   | j                  |j                  |	j                        }?|?j                  |       |7j                  |?       |?j                   | j                  |j                  |	j                        }@|@j                  |       |@j                  |7       |j                  |7||||	f       ||dz      j                  |       ||dz      j                  |=       ||dz      j                  |       ||dz      j                  |>       ||dz      j                  |7       ||dz      j                  |?       ||dz      j                  |       ||dz      j                  |@       ||dz      j                  |	       ||8   j                  |>       ||8   j                  |       ||8   j                  |7       ||8   j                  |@       ||8   j                  |?       ||8   j                  |	       |@j                      	 ~~~~~"	 | j                   j#                  t        |      t        |      f       |D ]E  }| j                   j                  t        |j                        t        |j                        f       G |rt'               }Bg }C|D ]K  }| j)                  |j                  |j                        }D	 Dj#                  B       Cj                  D       M CD ]  }D	 Dj#                  B        t        |C      D ]t  \  }}D| j                  |j                  |j                        }EBj-                  |E       	 Dj#                  B       DD ]  }FEj                  |F        Ej                   v n	 | y c c}w c c}w c c}w # t        $ r "D ]  }%t        |%d   j                        }5t        |%d   j                        })||dz      |5|dz   <   ||dz      |)|dz   <   | j                  t        |)         })|) | j                  |%d   j                  |)j                        }*|) |*j                  |%d          |*j                  |%d          |*j                  |%d          |*j                  |%d          |*j                   | j                  t        |5         }5|5 | j                  |%d   j                  |5j                        }6| j                  |%d   j                  |)j                        }1| j                  |6j                  |1j                        }A|6 |1 |A |%\  }7}}}}	||||	|5|)g}2t        j                  |2d      }3|3D ].  }4| j                  |4d   j                  |4d   j                         0  ||   }||   } ||   }!|| |!};}:}9t        t        |9|:|;            D ]  \  }<\  }}=}t        |j                        }	||dz      |	|dz   <   | j                  t        |	         }	|j                   | j                  |j                  |	j                        }7| j                  |j                  |j                         |7j                  |       |7j                  |=       |7j                  |       |7j                  |       |7j                  |	       |7j                   | j                  |j                  |	j                        }?|?j                  |       |7j                  |?       |?j                   ||dz      j                  |       ||dz      j                  |?       ||dz      j                  |	       |j                  |7|||	g       |	j                    Y w xY w# t        $ r Y w xY w# t$        $ r Y kw xY w# t*        $ r Y w xY w# t*        $ r Y w xY w# t*        $ r Y w xY ww)N)NNNr   r            )r3   r   r4   r   r%   r9   r1   r:   r   r2   r    r6   	itertoolscombinationsr5   r7   r\   remove
ValueErrorsetvpoolr!   add)Gr'   r;   r<   r   r=   origin_c
supremum_crL   rM   rO   s_ovs_origins_sv
s_supremumrE   vir>   r?   r@   rT   vcosup_setrN   c_vCoxCcxCuxrD   s_ab_Cr1   t_a_vlcCoxcCcxcCuxrP   s_ab_Cct_a_vuvectorsbc_vcb_vlb_vuba_vud_bc_vcb_vl_cos_vss_vb_vu_cd_b_vld_b_vud_ba_vucomb	comb_itervecsba_vld_ba_vlc_vcrH   rI   VCrJ   rK   rU   c_vlc_vua_vcd_ba_vcvcn_set
c_nn_listsc_nnvcnvnnsG                                                                          r*   rk   zComplex.refine_local_space  s    99V$YYx(
 (B F|<H~(^
x( 	(EArAwa "1g $Q
1	(
 HoJVVC[VVC[oobddBDD) ))CFF# yyc#a&Q;dffll*BB//"$$-C ii'G99T#Y'D!fDG66%+&D66%+&DoobddDFF+Ceetfugvh fQRj) B	!DAqJJrNJJrNJJrN|!c #AE
q1u
 '*&1q5k2!22&)&1q5k2!22&)&1q5k2!22 		$ ))F+
 =4$$c #AE
q1u=4$$& KBG .E
-D
-D .E#&q1u:E!a%L"%a!e*DQK"%a!e*DQK#&q1u:E!a%L FF5<0EMM#&K #oogajllEGGDGOOE*OOGAJ/OOGAJ/OOGAJ/!))O66%+.DMM$'OOD)J66%+.DMM$'OOD)!__TVVTVV<FMM&)J FF5<0EMM%(OOE*  ??71:<<AD??466577;D!__TVVUWW=FMM&)&&L&&LK #oogajllEGGDGOOC(!))O!__WQZ\\466BFOOC(OOF+ ((N!__WQZ\\466BFOOC(OOF+ ((N"oogajllEGGDGOOC(OOG,!M D $/D ) 6 6tQ ?I ) >Q		4799=> KK'!*dD% HIKK"dD% @AWKBZ  % NBG .E
-D
-D .E .E#&q1u:E!a%L"%a!e*DQK"%a!e*DQK#&q1u:E!a%L#&q1u:E!a%L FF5<0EMM#&K #oogajllEGGDGOOE*OOGAJ/OOGAJ/OOGAJ/OOGAJ/!))O66%+.DMM$'OOD)J66%+.DMM$'OOD)J FF5<0EMM%(OOE*OODFFEGG4K FF5<0EMM%(OOE*??71:<<AD??466577;D&&L&&LK #oogajllEGGDGOOC(!))O!__WQZ\\466BFOOC(OOF+ ((N!__WQZ\\466BFOOC(OOF+ ((N"oogajllEGGDGOOC(OOG,!M"oogajllEGGDGOOC(OOG,!M/6,D"b$D$ $u6D ) 6 6tQ ?I ) >Q		4799=> KKdE5 ABKK&&'7 KLKK'!*dD% HIKK"dD% @A]NB` (1T41F'G ;%OA|B+4SR_+E :%<BB#BDDz#BDDz&)!a%jQU&)!a%jQU#vveDk2#vveDk2  $rttTVV<bdd3S)R(R(R(T*T*"ff#rttTVV<S)T*"ff#rttTVV<S)T*"ff#tvvtvv>S)T* T2r4$>? AE
))"-AE
))"-AE
))"-AE
))$/AE
))$/AE
))$/AE
))$/AE
))$/AE
))$/ Ad+Ad+Ad+Ad+Ad+Ad+ #ffu:%;%AB	!J
		%%,,eHo.3J.?.A B  	JB%%,,eCEElE"$$K-HI	J eGJ (zz#%%.KK( !!$'( # KK(  4 
DooceeRTT2C KK(   %CKK$%ee
 	[ 322\  E!$ >G .E .E#&q1u:E!a%L#&q1u:E!a%L FF5<0EK"oogajllEGGDGKOOGAJ/OOGAJ/OOGAJ/OOGAJ/!))O FF5<0EK"oogajllEGGDG"oogajllEGGDG"oogiiCG!M!M!M/6,D"b$D$!!#D !* 6 6tQ ?I ) >Q		4799=>7>> 1v1v1v!4B'0RR'A !!OA|B:D"%a!e*DQK  66%+.D&&L??2448DOOBDD"$$/LL%LL$LL$LL$LL&66N??244+/663DLL%LL&66N AJ%%b)AJ%%d+AJ%%d+ MM4R"67&&LC!!IE!\ ! 		  		          s\  A9A]<H)A]&#AL	AL
AL!AL-AL9ALzALFA]F
A\ F/A\ F?BA]IA\.IA]I-A\>I>AA]K
A]K/A]L
ALLO.A\\A]\
A\\A]\	A\\A]\A\\A]\	A\+\'A]\*A\+\+A]\.	A\;\7A]\:A\;\;A]\>	A]]A]]
A]]A]]	A]]A]]A]]A]c                 (   t        j                   |j                        }g }t               }|D ]0  }|j                  t        j                   |j                               2 t	        ||      D ]  \  }}|j                  |      }| j                  |j                  |j                        }|D ]  }|j                  |        |j                  |       |D ]9  }	| j                  |j                  |	j                        }
|j                  |
       ;  y)z'Refine the star domain of a vertex `v`.N)
r3   r:   rz   r    r5   intersectionr9   r1   r2   r|   )r'   rV   r   v1nn
d_v0v1_setv1vnnud_v0v1o_d_v0v1v2d_v1v2s              r*   refine_starzComplex.refine_star  s     iioU
 	*BKK		"%%()	* C 		'HB$$S)D__QSS"$$/F& )x()NN6" 'rtt4v&'		' 	r/   c                    | j                   |   }| j                   |   }|j                  |       	 |j                  |j                  z
  dz  |j                  z   }| j                   t        |         }|j                  |       |j                  |       |S # t        $ r? |j                  |j                  z
  t	        j
                  d      z  |j                  z   }Y w xY w)Ng       @)r%   r8   x_a	TypeErrordecimalDecimalr   r2   )r'   r   r   vctrU   s        r*   r9   zComplex.split_edge  s    VVBZVVBZ
b	D66BFF?c)BFF2C VVE#J


2


2	  	D66BFF?gooc&::RVVCC	Ds   )B ACCc                 n   t        |      }t        |      }| j                  |   }| j                  |   }t        |      }t        |      }t        t	        ||            D ]#  \  }	\  }
}||	   |kD  r|||	<   ||	   |
k  s|
||	<   % t               }|j                  |j                         |j                  |j                         t        j                  |      }|D ]F  }t        |j                        D ],  \  }	}||	   |cxk  r||	   k  rn n	 |j                  |       . H |S # t        $ r Y >w xY wr-   )r   r%   r   r4   r5   rz   updater:   r3   r1   rx   r!   )r'   r;   r<   r>   vstr@   rT   blburE   voivsivn_poolcvn_poolvnxis                   r*   r{   zComplex.vpool  s1   FmHoVVC[VVC[
 #Y#Y&s3}5 	MAzS!us{1!us{1		 %ruuruu99W% 	B"244 2a5B'"Q%'r*	  $ s   D((	D43D4c           
         | j                   dkD  rx|D ]r  }t        j                  || j                         }|D ]K  }| j                  t	        ||d               j                  | j                  t	        ||d                      M t y|D ]K  }| j                  t	        ||d               j                  | j                  t	        ||d                      M y)z
        Convert a vertex-face mesh to a vertex-vertex mesh used by this class

        Parameters
        ----------
        vertices : list
            Vertices
        simplices : list
            Simplices
        r   r   N)r   rv   rw   r%   r   r2   )r'   vertices	simplicessedgeses         r*   vf_to_vvzComplex.vf_to_vv  s     88a< 7!..q$((; 7AFF5!A$0199uXad^45777 	  3uXad^,-55FF5!A$0133 	r/   c           	         || j                   }n|}t        |      | j                   j                  v r| j                   |   | j                  v rny| j                   |    d}g }|D ]  }|j	                  |j
                          t        j                  |      }t        j                  |      t        j                  |      z
  }t        j                  t        |j                  d         | j                  dz         D ]   }d}	t        j                  |d      D ]  }
| j                   t        ||
d               | j                   t        ||
d               j                  vsJ| j                   t        ||
d               | j                   t        ||
d               j                  vsd}	 n |t        |g         }|	r| j                  |d      rd}	|	s|t        |g         }| j                  |||      sd} n |r>D ]9  }
| j                   |   j!                  | j                   t        ||
                   ; | j                  j	                  | j                   |          |S )	a  
        Adds a vertex at coords v_x to the complex that is not symmetric to the
        initial triangulation and sub-triangulation.

        If near is specified (for example; a star domain or collections of
        cells known to contain v) then only those simplices containd in near
        will be searched, this greatly speeds up the process.

        If near is not specified this method will search the entire simplicial
        complex structure.

        Parameters
        ----------
        v_x : tuple
            Coordinates of non-symmetric vertex
        near : set or list
            List of vertices, these are points near v to check for
        NFr   r   )rTrs   )proj)r%   r   r   r&   r    r1   nparrayrv   rw   rangeshaper   r:   deg_simplex
in_simplexr2   )r'   v_xnearstarfound_nnS_rowsrV   As_ivalid_simplexrE   SA_j0s                r*   connect_vertex_non_symmzComplex.connect_vertex_non_symm3  s5   & <66DD:%vvc{doo-s 	AMM!##	 &!HHVrxx},))%Q*@,0HHqL: 	C !M++C15 	 VVE&1,/0uVAaD\23667fQqTl 34fQqTl 34778$)M	 ucU|$A##AD#1$)M u??1c40#H7	<  >s##DFF5+;$<=> 	tvvc{+r/   c           
         t        j                  |dd      |d   z
  }t        j                  t         j                  j	                  |            }|dk(  rd}|||z
  }t        | j                  dz         D ]X  }d|z  |z  }t        j                  t         j                  j	                  t        j                  ||d                  }||k(  rX y y)a  Check if a vector v_x is in simplex `S`.

        Parameters
        ----------
        S : array_like
            Array containing simplex entries of vertices as rows
        v_x :
            A candidate vertex
        A_j0 : array, optional,
            Allows for A_j0 to be pre-calculated

        Returns
        -------
        res : boolean
            True if `v_x` is in `S`
        r   r   FT)r   deletesignlinalgdetr   r   )	r'   r   r   r   A_11sign_det_A_11ddet_A_jjsign_det_A_j0s	            r*   r   zComplex.in_simplex  s    " yyAq!AaD(		d 34A M <s7Dtxx!|$ 	AQw.H GGBIIMM"))D!EF3H %I JM =(	 r/   c                 d    ||dd |d   z
  }t         j                  j                  |      dk(  ryy)aF  Test a simplex S for degeneracy (linear dependence in R^dim).

        Parameters
        ----------
        S : np.array
            Simplex with rows as vertex vectors
        proj : array, optional,
            If the projection S[1:] - S[0] is already
            computed it can be added as an optional argument.
        Nr   r   r
   TF)r   r   r   )r'   r   r   s      r*   r   zComplex.deg_simplex  s9     <QR51Q4<D 99==#%r/   )NNr   NNr   )T)NNTF)r   r-   )__name__
__module____qualname____doc__r+   r.   rW   re   rn   rh   rk   r   r   r9   r{   r   r   r   r   r   r/   r*   r   r      s}    eL CE:;ENeN ;?"qf&P*yv*  ""H.KZ.`r/   r   )r   r3   rY   rv   r   	functoolsr   numpyr   _vertexr   r   r   r   r/   r*   <module>r      s)    @       9| |r/   