
    ph                     V    S r SSKrSSKJrJr  SSKJr  SSKJr	  S r
S rS	 rS
 rS rg)zB
Functions for computing distances between various PyPhi objects.
    N   )configutils)emd)system_repertoire_distancec                    [        [        U R                  R                  UR                  R                  -   5      5      n[        [        U R                  R                  UR                  R                  -   5      5      n[        U R                  U5      UR                  U5      5      [        U R                  U5      UR                  U5      5      -   $ )zReturn the distance between two concepts in concept space.

Args:
    c1 (Concept): The first concept.
    c2 (Concept): The second concept.

Returns:
    float: The distance between the two concepts in concept space.
)tuplesetcausepurvieweffectrepertoire_distanceexpand_cause_repertoireexpand_effect_repertoire)c1c2cause_purvieweffect_purviews       P/home/james-whalen/.local/lib/python3.13/site-packages/pyphi/compute/distance.pyconcept_distancer      s     #bhh..1A1AABCM3ryy002993D3DDEFN : := I " : := IK ; ;N K " ; ;N KMM N    c                    ^ [        U5      [        U 5      :  a  XpU  V^s/ s H!  m[        U4S jU 5       5      (       a  M  TPM#     nn[        S U 5       5      $ s  snf )zReturn the distance between two cause-effect structures.

Assumes the only difference between them is that some concepts have
disappeared.
c              3   F   >#    U  H  nTR                  U5      v   M     g 7fNemd_eq.0r   r   s     r   	<genexpr>'_ces_distance_simple.<locals>.<genexpr>/   s     +GBbBIIbMMB   !c              3   z   #    U  H1  nUR                   [        XR                  R                  5      -  v   M3     g 7fr   )phir   	subsystemnull_conceptr   cs     r   r   r    0   s0      #!1 uu';;+C+CDD!s   9;)lenanysum)C1C2r   	destroyeds     ` r   _ces_distance_simpler.   &   s[     2wRB Hb+GB+G(GbIH #!# # # Is   AAc                    [         R                  " U  VVs/ s H  o! Vs/ s H  n[        X#5      PM     snPM      snn5      n[         R                  " X4 VVs/ s H+  oU  H"  n[        XfR                  R                  5      PM$     M-     snn5      n[        U 5      [        U5      p[         R                  " X-   S-   /S-  5      n
[         R                  " U5      S-   U
SS& XJSU2US24'   UR                  XS2SU24'   XzSSS24'   UR                  U
SS2S4'   SU
S'   U  Vs/ s H  ofR                  PM     snS/U	-  -   S/-   nS/U-  U Vs/ s H  ofR                  PM     sn-   S/-   n[        U5      [        U5      -
  US'   [        R                  " [        U5      [        U5      5      (       d   e[        [         R                  " U5      [         R                  " U5      U
5      $ s  snf s  snnf s  snnf s  snf s  snf )zTReturn the distance between two cause-effect structures.

Uses the generalized EMD.
   r   Nr   )r1   r1   )nparrayr   r$   r%   r(   emptymaxTr#   r*   r   eqr   )	unique_C1	unique_C2ij	distancescesr'   distances_to_nullNMdistance_matrixd1d2s                r   _ces_distance_emdrD   4   s    =F=F3A	!	3Y I )")C3a 	KK44536 	6)" 4 y>3y>qhh	{Q/O	*Q.OA )BQB"H )ObD"1"H0BG022OCRCGOF #	#A%%	#qcAg	-	3B
q9-9aEE9-	-	3BWs2wBrF88CGSW%%%%rxx|RXXb\?;;u 	4"T 
$-s'   	G3
G.G3
2G9
%G?H.G3
c                   ^^ [         R                  (       a#  [        [        X5      [         R                  5      $ U  V^s/ s H!  m[        U4S jU 5       5      (       a  M  TPM#     nnU V^s/ s H!  m[        U4S jU  5       5      (       a  M  TPM#     nnU(       a  U(       d  [        X5      nO[        X55      n[        U[         R                  5      $ s  snf s  snf )a#  Return the distance between two cause-effect structures.

Args:
    C1 (CauseEffectStructure): The first |CauseEffectStructure|.
    C2 (CauseEffectStructure): The second |CauseEffectStructure|.

Returns:
    float: The distance between the two cause-effect structures in concept
    space.
c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   r   r   s     r   r   ces_distance.<locals>.<genexpr>        ">2R299R==2r!   c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   r   )r   r   r   s     r   r   rG      rH   r!   )r   )USE_SMALL_PHI_DIFFERENCE_FOR_CES_DISTANCEroundsmall_phi_ces_distance	PRECISIONr)   r.   rD   )r+   r,   r   concepts_only_in_C1r   concepts_only_in_C2dists     ` `  r   ces_distancerQ   y   s     77+B3V5E5EFF @rs">2">>R  @ @rs">2">>R  @ &9#B+ !4Jv''((@@s    C"C/CCc                 L    [        S U  5       5      [        S U 5       5      -
  $ )zDReturn the difference in |small_phi| between |CauseEffectStructure|.c              3   8   #    U  H  oR                   v   M     g 7fr   r#   r&   s     r   r   )small_phi_ces_distance.<locals>.<genexpr>   s     !buub   c              3   8   #    U  H  oR                   v   M     g 7fr   rT   r&   s     r   r   rU      s     ':r!rrV   )r*   )r+   r,   s     r   rL   rL      s#    !b!!C':r':$:::r   )__doc__numpyr2    r   r   distancer   r   r   r   r.   rD   rQ   rL    r   r   <module>r]      s4   
    HN,#B<J)8;r   