
    _-Ph:                     l    d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ d Z G d de
          ZdS )zTest clustering.    N)load_example)TestCaseassert_equalassert_array_equalc                     t          j        |           } | j        | j        k    }| j        |         | _        | j        |         | _        | j        |         | _        d| j        d d <   | S )N   )sparse
coo_matrixrowcoldata)Gmasks     [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/tests/test_clustering.pycanonical_graphr   
   s\     	!A5AE>DE$KAEE$KAEVD\AFAF111IH    c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )TestClusteringc                 r   d t          d          D             }d t          d          D             }d t          d          D             }d t          d          D             }d t          d          D             }d t          d          D             }d t          d          D             }d	 t          d          D             }d
 t          d          D             }	d t          d          D             }
d t          d          D             }t          j        ddgddgddgddgddgddgg          }~t          j        d          }d|dg df<   d|dg df<   d|dg df<   d|dg df<   d|dg df<   d|dg df<   g d|g dg df<   t	          j        |          }||d<   t          j        g dt          j                  }t          j        g dt          j                  }t          j        g dt          j                  }t          j        g dt          j                  }d|d |d<   |||d!|d<   ddgdt          j        g dt          j                  t          j        g dt          j                  t          j        g dt          j                  t          j        g dt          j                  d"|d<   ddg|d<   d#ddgi|d<   t          j        g d$t          j                  t          j        g d%|j                  d&|d<   d#t          j        ddgt          j                  i|d<   t          j        g d$t          j                  t          j        g d'|j                  t          j        ddgt          j                  d(|	d<   d#t          j        ddgt          j                  i|
d<   t          j        g d)t          j                  t          j        g d*|j                  t          j        ddgt          j                  d(|d<   t          j        ddgddgddgddgddgddgddgddgddgddgddgddgg          }~t          j        d+          }d|dddgf<   d|dg d,f<   d|dg d-f<   d|dddgf<   d|ddd.gf<   d|dg d/f<   d|d0g d1f<   d|d2dd0gf<   d|d3d0d4gf<   d|d4g d5f<   d|d.g d6f<   d|d7d4d.gf<   g d8|t          j        d9          t          j        d9          f<   t	          j        |          }|	                    |           t          j        g d:t          j                  }t          j        g d;t          j                  }t          j        g d<t          j                  }t          j        g d=t          j                  }|	                    d|d            |	                    |||d!           ddgdt          j        g dt          j                  t          j        g dt          j                  t          j        g dt          j                  t          j        g dt          j                  d"|d<   ddg|d<   t          j
                            d>           t          j
                            t          |j                            dz  |j        d d <   |	                    |           t          j        ddgddgddgddgddgddgddgddgddgddgddgddgg          }~t          j        d+          }d|ddgf<   d|ddd2gf<   d|dddgf<   d|ddgf<   d|dd.gf<   d|ddd0gf<   d|d0dgf<   d|d2d0gf<   d|d3d0gf<   d|d4dd3gf<   d|d.d4d7gf<   d|d7d4gf<   t	          j        |          }t          j
                            d?           t          j
                            t          |j                            dz  |j        d d <   |	                    |           |	                    t          d@          dA                    || _        || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _        || _        d S )BNc                     g | ]}d S N .0is     r   
<listcomp>z(TestClustering.setUp.<locals>.<listcomp>   s    (((!(((r      c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>   s    '?'?'?'?'?'?r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>   s    (@(@(@!(@(@(@r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>       777777r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>    s     8 8 8! 8 8 8r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>"   s    555qd555r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>#       666t666r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>(   r%   r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>)   r!   r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>+   s    $<$<$<aT$<$<$<r   c                     g | ]}d S r   r   r   s     r   r   z(TestClustering.setUp.<locals>.<listcomp>,   s    %=%=%=qd%=%=%=r   r   r      )   r+   )r         )r   r*   r,   r-   r   )r   r-   r   r,   )r   r   r-   r-   )r   r   r*   r,   r   )r   r*   r-   )r,   r   r,   r,   r   r,   )r   r   r*   r,   r-   r   )r   r   r   r   r   r   dtype)r   r,   r+   )r   r,   r-   r   r*   r   )r   r   r   r   r*   r*   )num_clusterscm)ICpICiL)ar0   r1   r2   r3   r4   seeds)r   r   r   r   r   r   )              ?r8   r8   r8   r7   )r1   d)r8   r7   r7   r8   r7   r7   )r1   r9   c)r   r   r   r   r   r   )r   r   r   r   r   r   )   r;   )r   r*      )r   r   r,   r+   
   )r,   r-   r+   	   r+   )r*   r   r<      r<   r?   r>   )r   r?   r=      )r-   r>   r@   r@   )r*   r,   r-   r*   r*   r-   r-   r*   r*   r-   r,   r*   r;   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r+   r;   )r   r   r*   r,   r+   r<   r-   r   r?   r>   r=   r@   )r   r   r*   r,   r   r   r-   r   r*   r,   r-   r   l   e\ i=2cunit_squareA)rangenparrayzerosr	   
csr_matrixint32r/   arangeappendrandomseedrandlenr   r   casescluster_node_incidence_inputcluster_node_incidence_outputcluster_center_inputcluster_center_outputbellman_ford_inputbellman_ford_outputlloyd_cluster_inputlloyd_cluster_outputlloyd_cluster_exact_inputlloyd_cluster_exact_output)selfrO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   xyr   r1   r2   r3   r4   s                     r   setUpzTestClustering.setUp   s*
   ((uQxx((('?'?eAhh'?'?'?$(@(@uQxx(@(@(@%77eAhh777 8 8uQxx 8 8 855E!HH55566U1XX666
 76U1XX66677eAhh777$<$<588$<$<$<!%=%=E!HH%=%=%=" X1v1v1v1v1v1v     HV!YYY, !!___
!YYY,!YYY, !!___
!YYY,4F4F4F


000
01a  aX(((999hyyy111h))):::H'''rx888;<13+5 +5$Q'363612,4 ,4%a( *+A34)+2D2D2DBH)U)U)U*,(999BH*M*M*M*,(3E3E3ERX*V*V*V(*1C1C1C28(T(T(T#V #VQ %&q6a !(1a& 11(*1C1C1C28(T(T(T')x0H0H0HPQPW'X'X'X"Z "ZA #*28QF"(+K+K+K!LA)+2D2D2DBH)U)U)U(*1I1I1IQRQX(Y(Y(Y(*!Qrx(H(H(H#J #JQ )01a&1Q1Q1Q'R!!$/1x8J8J8JRTRZ/[/[/[.0h7I7I7IQRQX.Y.Y.Y.0h1vRX.N.N.N)P )P"1%  X1v1v1v1v1v1v1v1v1v1v1v1v     HX!aV)!YYY,!\\\/!aV)!aW*!\\\/!\\\/!aV)!aV) !^^^
"jjj."q"g+*N*N*N")B--2
&'a  QX:::"(KKKhzzz222h===RXNNNH999JJJ$++Q35-7 -7 	8 	8 	8%,,S5834.6 .6 	7 	7 	7 *+A34)+2D2D2DBH)U)U)U*,(999BH*M*M*M*,(3E3E3ERX*V*V*V(*1C1C1C28(T(T(T#V #VQ %&q6a 
	z"""INN3qv;;//!3qqq	Q X1v1v1v1v1v1v1v1v1v1v1v1v     HX!aS&	!aV)!aV)!aS&	!bT'
!aV)!aS&	!aS&	!aS&	!aV)"q"g+"qc'
a  
	z"""INN3qv;;//!3qqq	Q 	\-005666
,H)-J*$8!%:""4#6  $7 $8!)B&*D'''r   c           	          t          | j        | j        | j                  D ]\  }}}||j        d         }|d         }|d         }dt          j        |dz   t
          j                  z  }dt          j        |t
          j                  z  }dt          j        |t
          j                  z  }	t          j	        ||||||	           t          ||d                    t          ||d                    t          |	|d	                    d S )
Nr   r0   r1   r   r.   r2   r3   r4   )ziprO   rP   rQ   shaperD   onesrH   amg_corecluster_node_incidencer   )
rZ   rB   arginargout	num_nodesr0   r1   r2   r3   r4   s
             r   test_cluster_node_incidencez*TestClustering.test_cluster_node_incidence   s    #DJ$($E$($F!H !H 	3 	3Auf  GAJ	$^44[arx@@@@"(;;;;rwy9999/	<SRUWXYYY"3u666"3u666"1fSk222!	3 	3r   c                 r   t          | j        | j        | j                  D ]\  }}}|t          |d         |          D ]v\  }}|j        d         }|d         }|d         }|d         }	|d         }
|d         }t          j        ||||j        |j        |j	        ||	|
|
  
        }t          ||           wd S )Nr5   r   r0   r1   r2   r3   r4   )r_   rO   rR   rS   r`   rb   cluster_centerindptrindicesr   r   )rZ   rB   rd   re   r5   ccorrectrf   r0   r1   r2   r3   r4   r:   s                r   test_cluster_centerz"TestClustering.test_cluster_center   s     #DJ$($=$($>!@ !@ 	. 	.Auf  #&uSz6#:#: . .KAx !
I#(#8LtB,C,Cc
A /09<01!)QV0203S!	= =A !H----%	. 	.r   c           	         t          | j        | j        | j                  D ]H\  }}}|>|d         }|j        d         }t          j        |j                  j        }|t          j	        ||j                  z  }d||<   dt          j	        |t
          j
                  z  }t          j        t          |                    ||<   t          j        |          }	d}
d}||
|k     rU||	d d <   t          j        ||j        |j        |j        ||           |	|k                                    rn|
dz  }
|O|
|k     Ut)          ||d                    t)          ||d                    Jd S )	Nr6   r   r.   r^   r=   r   r9   r1   )r_   rO   rT   rU   r`   rD   finfor/   maxra   rH   rI   rN   
empty_likerb   bellman_fordrj   rk   r   allr   )rZ   rB   rd   re   r6   rf   mvr9   r1   old_ditermaxiters               r   test_bellman_fordz TestClustering.test_bellman_ford  s    #DJ$($;$($<!> !>  	5  	5Auf  gGAJ	Xag&&*!'::::%"')28<<<<Ic%jj115	a((o E!!!H))*+(AIqv*+R1 1 1 
'')) AID o #1fSk222"2vd|444A 	5  	5r   c                 x   t          | j        | j        | j                  D ]\  }}}||d         }|j        d         }|                                }t          |          }t          j        |j	                  j
        }|t          j        ||j	                  z  }	d|	|<   dt          j        |t          j                  z  }
||
|<   t          j        ||j        |j        |j        ||	|
|           t%          |	|d                    t%          |
|d                    t%          ||d                    d S Nr6   r   r.   r^   r9   r1   r:   )r_   rO   rV   rW   r`   copyrN   rD   ro   r/   rp   ra   rH   rb   lloyd_clusterrj   rk   r   r   rZ   rB   rd   re   r6   rf   r:   r0   rt   r9   r1   s              r   test_lloyd_clusterz!TestClustering.test_lloyd_clusterI  s9    #DJ$($<$($=!? !? 	3 	3Auf  gGAJ	JJLL"5zzXag&&*!'::::%"')28<<<<!5	&y'(xAF'3'("a1 1 1
 #1fSk222"2vd|444"1fSk2221	3 	3r   c                 x   t          | j        | j        | j                  D ]\  }}}||d         }|j        d         }|                                }t          |          }t          j        |j	                  j
        }|t          j        ||j	                  z  }	d|	|<   dt          j        |t          j                  z  }
||
|<   t          j        ||j        |j        |j        ||	|
|           t%          |	|d                    t%          |
|d                    t%          ||d                    d S rz   )r_   rO   rX   rY   r`   r{   rN   rD   ro   r/   rp   ra   rH   rb   lloyd_cluster_exactrj   rk   r   r   r}   s              r   test_lloyd_cluster_exactz'TestClustering.test_lloyd_cluster_exactd  s;    #DJ$($B$($C!E !E 	3 	3Auf  gGAJ	JJLL"5zzXag&&*!'::::%"')28<<<<!5	,Y-.Xqy!&-9-.A7 7 7
 #1fSk222"2vd|444"1fSk2221	3 	3r   N)	__name__
__module____qualname__r\   rg   rm   rx   r~   r   r   r   r   r   r      sv        KE KE KEZ3 3 3&. . .*!5 !5 !5v3 3 363 3 3 3 3r   r   )__doc__numpyrD   pyamg.amg_corerb   pyamg.galleryr   scipy.sparser	   numpy.testingr   r   r   r   r   r   r   r   <module>r      s          ! ! ! ! ! ! & & & & & &       D D D D D D D D D D  e3 e3 e3 e3 e3X e3 e3 e3 e3 e3r   