
    RiN                        d Z ddlZddlmZ ddlZd Zd Zd ZeZ	d Z
d Zd	 Zd
 Z G d d      Z	 dededej"                  fdZdedej"                  fdZdedej"                  fdZdedej"                  fdZdedededej"                  fdZdedededej"                  fdZdedej"                  fdZdededefdZdej"                  deeeef   fdZ ej8                  dej:                         Z ej8                  dej:                         Z ej8                  dej:                         Z  ej8                  dej:                         Z!	 d+d!ej"                  d"ej"                  d#ej"                  d$e"deej"                  ej"                  dz  f   f
d%Z#dej"                  dej"                  fd&Z$dej"                  dej"                  fd'Z%d+d(ej"                  d)e&d$e"dej"                  fd*Z'y),z3Vector class, including rotation-related functions.    N)Optionalc                    d}t        | d   | d   z
        |k  rt        | d   | d   z
        |k  rt        | d   | d   z
        |k  ryt        | d   | d   z         |k  rQt        | d   | d   z         |k  r:t        | d   | d   z         |k  r#t        | d   | d	   z   | d
   z   dz
        |k  rd}nYt        j                  }nHdt        j                  |       dz
  z  }t	        d|      }t        d|      }t        j                  |      }|dk  rdt        ddd      fS |t        j                  k  rB| d   | d   z
  }| d   | d   z
  }| d   | d   z
  }t        |||      }|j                          ||fS | d   }| d	   }	| d
   }
||	kD  r:||
kD  r5t        j                  ||	z
  |
z
  dz         }| d   d|z  z  }| d   d|z  z  }ns|	|kD  r:|	|
kD  r5t        j                  |	|z
  |
z
  dz         }| d   d|z  z  }| d   d|z  z  }n4t        j                  |
|z
  |	z
  dz         }| d   d|z  z  }| d   d|z  z  }t        |||      }|j                          t        j                  |fS )zReturn angles, axis pair that corresponds to rotation matrix m.

    The case where ``m`` is the identity matrix corresponds to a singularity
    where any rotation axis is valid. In that case, ``Vector([1, 0, 0])``,
    is returned.
    h㈵>r      r   r   r      r
   r   r   r
   r
   r   r   r   r   r   r
   r
      r   g      ?r   gV瞯<        r
   )
absnppitracemaxminarccosVector	normalizesqrt)mepsangletxyzaxism00m11m22s              G/home/agent/.friday_env/lib/python3.12/site-packages/Bio/PDB/vectors.py	m2rotaxisr*      s    C
 	AdGag$$!D'!"S($!D'!"S( $!D'!"S(AdGag%&,AdGag%&,AdGag%$/!34s:EEEE 288A;?#AJ1I		!u}F1aO##	dGagdGagdGagaAd{ ggg9sc	C#-.A$1q5!A$1q5!A3Y39c	C#-.A$1q5!A$1q5!Ac	C#-.A$1q5!A$1q5!AaAuud{    c                     | j                         } |j                         }| j                  |      }|| |t        j                  |      z  z  z
  S )a2  Vector to axis method.

    Return the vector between a point and
    the closest point on a line (ie. the perpendicular
    projection of the point on the line).

    :type line: L{Vector}
    :param line: vector defining a line

    :type point: L{Vector}
    :param point: vector defining the point
    )
normalizednormr    mathcos)linepoint
norm_pointr    s       r)   vector_to_axisr4   S   sF     ??DJJJuE4J%8999r+   c                    |j                         }t        j                  |       }t        j                  |       }d|z
  }|j	                         \  }}}t        j
                  d      }||z  |z  |z   |d<   ||z  |z  ||z  z
  |d<   ||z  |z  ||z  z   |d<   ||z  |z  ||z  z   |d<   ||z  |z  |z   |d<   ||z  |z  ||z  z
  |d<   ||z  |z  ||z  z
  |d	<   ||z  |z  ||z  z   |d
<   ||z  |z  |z   |d<   |S )aG  Calculate left multiplying rotation matrix.

    Calculate a left multiplying rotation matrix that rotates
    theta rad around vector.

    :type theta: float
    :param theta: the rotation angle

    :type vector: L{Vector}
    :param vector: the rotation axis

    :return: The rotation matrix, a 3x3 NumPy array.

    Examples
    --------
    >>> from numpy import pi
    >>> from Bio.PDB.vectors import rotaxis2m
    >>> from Bio.PDB.vectors import Vector
    >>> m = rotaxis2m(pi, Vector(1, 0, 0))
    >>> Vector(1, 2, 3).left_multiply(m)
    <Vector 1.00, -2.00, -3.00>

    r   )r   r   r   r   r	   r   r   r   r   r   r   )r-   r   r0   sin	get_arrayzeros)	thetavectorcsr!   r"   r#   r$   rots	            r)   	rotaxis2mr>   f   s7   0  F
uA
uA	AA GAq!
((6
CA	ACIA	AE!CIA	AE!CIA	AE!CIA	ACIA	AE!CIA	AE!CIA	AE!CIA	ACIJr+   c                 |   | j                         } |j                         }| |z
  j                         dk  rt        j                  d      S | |z
  }|j	                          |j                         }d|_        t        j                  d      }|dt        j                  |t        j                  |            z  z
  }|S )a  Return a (left multiplying) matrix that mirrors p onto q.

    :type p,q: L{Vector}
    :return: The mirror operation, a 3x3 NumPy array.

    Examples
    --------
    >>> from Bio.PDB.vectors import refmat
    >>> p, q = Vector(1, 2, 3), Vector(2, 3, 5)
    >>> mirror = refmat(p, q)
    >>> qq = p.left_multiply(mirror)
    >>> print(q)
    <Vector 2.00, 3.00, 5.00>
    >>> print(qq)
    <Vector 1.21, 1.82, 3.03>

    r   r   )r   r   r
   )	r-   r.   r   identityr   r7   shapedot	transpose)pqpqbirefs         r)   refmatrJ      s    $ 	
A	A	A||~{{1~	
QBLLN
AAG
AA
a"&&BLLO,,
,CJr+   c                 ^    t        j                  t        ||        t        | |              }|S )a  Return a (left multiplying) matrix that rotates p onto q.

    :param p: moving vector
    :type p: L{Vector}

    :param q: fixed vector
    :type q: L{Vector}

    :return: rotation matrix that rotates p onto q
    :rtype: 3x3 NumPy array

    Examples
    --------
    >>> from Bio.PDB.vectors import rotmat
    >>> p, q = Vector(1, 2, 3), Vector(2, 3, 5)
    >>> r = rotmat(p, q)
    >>> print(q)
    <Vector 2.00, 3.00, 5.00>
    >>> print(p)
    <Vector 1.00, 2.00, 3.00>
    >>> p.left_multiply(r)
    <Vector 1.21, 1.82, 3.03>

    )r   rB   rJ   )rD   rE   r=   s      r)   rotmatrL      s)    2 &&A2q1"
.CJr+   c                 8    | |z
  } ||z
  }| j                  |      S )zCalculate angle method.

    Calculate the angle between 3 vectors
    representing 3 connected points.

    :param v1, v2, v3: the tree points that define the angle
    :type v1, v2, v3: L{Vector}

    :return: angle
    :rtype: float
    )r    )v1v2v3s      r)   
calc_anglerQ      s$     
bB	bB88B<r+   c                     | |z
  }||z
  }||z
  }||z  }||z  }||z  }	|j                  |      }
	 |j                  |	      dkD  r|
 }
|
S # t        $ r Y |
S w xY w)a  Calculate dihedral angle method.

    Calculate the dihedral angle between 4 vectors
    representing 4 connected points. The angle is in
    ]-pi, pi].

    :param v1, v2, v3, v4: the four points that define the dihedral angle
    :type v1, v2, v3, v4: L{Vector}
    gMbP?)r    ZeroDivisionError)rN   rO   rP   v4abcbdbuvwr    s              r)   calc_dihedralr[      s     
bB	bB	bB
BA
BA	1AGGAJE88A;FE L  Ls   A
 
	AAc                       e Zd Z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d Zd Zd Zd Zd Zd Zd Zd Zy)r   z
3D vector.Nc                     |7|5t        |      dk7  rt        d      t        j                  |d      | _        yt        j                  |||fd      | _        y)zInitialize the class.Nr   z0Vector: x is not a list/tuple/array of 3 numbersd)len
ValueErrorr   array_arselfr"   r#   r$   s       r)   __init__zVector.__init__  sN    91v{ !STTxx3'DH xxAq	3/DHr+   c                 @    | j                   \  }}}d|dd|dd|ddS )zReturn vector 3D coordinates.z<Vector z.2fz, >rb   rc   s       r)   __repr__zVector.__repr__  s2    ((1a!C1S'Ac7!44r+   c                 2    | j                    }t        |      S )zReturn Vector(-x, -y, -z).)rb   r   )rd   as     r)   __neg__zVector.__neg__  s    XXIayr+   c                     t        |t              r$| j                  |j                  z   }t        |      S | j                  t        j                  |      z   }t        |      S )z%Return Vector+other Vector or scalar.
isinstancer   rb   r   ra   rd   otherrk   s      r)   __add__zVector.__add__  L    eV$599$A ay 288E?*Aayr+   c                     t        |t              r$| j                  |j                  z
  }t        |      S | j                  t        j                  |      z
  }t        |      S )z%Return Vector-other Vector or scalar.rn   rp   s      r)   __sub__zVector.__sub__  rs   r+   c                 F    t        | j                  |j                  z        S )z#Return Vector.Vector (dot product).)sumrb   )rd   rq   s     r)   __mul__zVector.__mul__&  s    488eii'((r+   c                 \    | j                   t        j                  |      z  }t        |      S )zReturn Vector(coords/a).)rb   r   ra   r   )rd   r"   rk   s      r)   __truediv__zVector.__truediv__*  s"    HHrxx{"ayr+   c                 (   t        |t              r| j                  \  }}}|j                  \  }}}t        j                  j                  t        j                  ||f||ff            }t        j                  j                  t        j                  ||f||ff             }	t        j                  j                  t        j                  ||f||ff            }
t        ||	|
      S | j                  t        j                  |      z  }t        |      S )z6Return VectorxVector (cross product) or Vectorxscalar.)ro   r   rb   r   linalgdetra   )rd   rq   rk   rG   r;   r^   efc1c2c3s              r)   __pow__zVector.__pow__/  s    eV$hhGAq!iiGAq!rxx!Q!Q(89:B))--1a&1a&)9 :;;Brxx!Q!Q(89:B"b"%%288E?*A!9r+   c                      | j                   |   S )zReturn value of array index i.rh   rd   rH   s     r)   __getitem__zVector.__getitem__<  s    xx{r+   c                 "    || j                   |<   y)zAssign values to array index i.Nrh   )rd   rH   values      r)   __setitem__zVector.__setitem__@  s    r+   c                     || j                   v S )zValidate if i is in array.rh   r   s     r)   __contains__zVector.__contains__D  s    DHH}r+   c                 l    t        j                  t        | j                  | j                  z              S )zReturn vector norm.)r   r   rw   rb   rd   s    r)   r.   zVector.normH  s#    wws488dhh./00r+   c                 X    t        t        | j                  | j                  z              S )zReturn square of vector norm.)r   rw   rb   r   s    r)   normsqzVector.normsqL  s    3txx$((*+,,r+   c                 j    | j                         r#| j                  | j                         z  | _        yy)zNormalize the Vector object.

        Changes the state of ``self`` and doesn't return a value.
        If you need to chain function calls or create a new object
        use the ``normalized`` method.
        N)r.   rb   r   s    r)   r   zVector.normalizeP  s(     99;xx$))+-DH r+   c                 F    | j                         }|j                          |S )zwReturn a normalized copy of the Vector.

        To avoid allocating new objects use the ``normalize`` method.
        )copyr   )rd   rY   s     r)   r-   zVector.normalizedZ  s    
 IIK	r+   c                     | j                         }|j                         }| |z  ||z  z  }t        |d      }t        d|      }t        j                  |      S )z!Return angle between two vectors.r   r   )r.   r   r   r   r   )rd   rq   n1n2r;   s        r)   r    zVector.anglec  sN    YY[ZZ\E\b2g&1IAJyy|r+   c                 @    t        j                  | j                        S )z,Return (a copy of) the array of coordinates.)r   ra   rb   r   s    r)   r7   zVector.get_arraym  s    xx!!r+   c                 X    t        j                  || j                        }t        |      S )zReturn Vector=Matrix x Vector.r   rB   rb   r   rd   matrixrk   s      r)   left_multiplyzVector.left_multiplyq  s     FF6488$ayr+   c                 X    t        j                  | j                  |      }t        |      S )zReturn Vector=Vector x Matrix.r   r   s      r)   right_multiplyzVector.right_multiplyv  s     FF488V$ayr+   c                 ,    t        | j                        S )z!Return a deep copy of the Vector.)r   rb   r   s    r)   r   zVector.copy{  s    dhhr+   )NN)__name__
__module____qualname____doc__re   ri   rl   rr   ru   rx   rz   r   r   r   r   r.   r   r   r-   r    r7   r   r   r    r+   r)   r   r      si    	05

)
1-."

 r+   r   
angle_radsr%   returnc                    t        j                  |       }t        j                  |       }d|k(  r2t        j                  || ddf||ddfddft        j
                        S d|k(  r2t        j                  |d|dfd| d|dfdft        j
                        S t        j                  dd|| dfd||dfdft        j
                        S )	zGenerate a 4x4 single-axis NumPy rotation matrix.

    :param float angle_rads: the desired rotation angle in radians
    :param char axis: character specifying the rotation axis
    r$   r   )r   r   r   r   r   r   r   r   dtyper#   )r   r   r   r   )r   r   r   r   )r/   r0   r6   r   ra   float64)r   r%   cosangsinangs       r)   homog_rot_mtxr     s     XXj!FXXj!F
d{xx&!Q'A&	 **
 	
 
xxFA&!VQ'	 **
 	
 xxFVGQ'FFA&	 **
 	
r+   mtxc                     t        j                  |       }t        j                  |       }|x|d<   |d<   ||d<   | |d<   y)z/Update existing Z rotation matrix to new angle.r   r   r   r   Nr/   r0   r6   r   r   r   r   s       r)   set_Z_homog_rot_mtxr     D    XXj!FXXj!F""CID	CICIr+   c                     t        j                  |       }t        j                  |       }|x|d<   |d<   ||d<   | |d<   y)z/Update existing Y rotation matrix to new angle.r   r   r	   r   Nr   r   s       r)   set_Y_homog_rot_mtxr     r   r+   c                     t        j                  |       }t        j                  |       }|x|d<   |d<   ||d<   | |d<   y)z/Update existing X rotation matrix to new angle.r   r   r   r   Nr   r   s       r)   set_X_homog_rot_mtxr     r   r+   r"   r#   r$   c                 l    t        j                  ddd| fddd|fddd|fdft         j                        S )z[Generate a 4x4 NumPy translation matrix.

    :param x, y, z: translation in each axis
    r   r   r   r   r   ra   r   )r"   r#   r$   s      r)   homog_trans_mtxr     s@    
 88
Q11a|aAq\<@jj r+   c                 "    | |d<   ||d<   ||d<   y)z1Update existing translation matrix to new values.)r   r   )r   r   )r
   r   Nr   )r"   r#   r$   r   s       r)   set_homog_trans_mtxr     s    CICICIr+   scalec                 p    t        j                  | dddgd| ddgdd| dgg dgt         j                        S )zSGenerate a 4x4 NumPy scaling matrix.

    :param float scale: scale multiplier
    r   r   r   r   )r   s    r)   homog_scale_mtxr     sC    
 88
Aq	Aua+aE1-=|Ljj r+   c                     |dk  rdnd}| dk  rdnd}d| k7  rd|k7  rt        j                  ||       S d|k7  rt         j                  dz  |z  S |dk  rt         j                  S dS )Nr   g      g      ?r   g       @)r   arctan2r   )r"   r#   sign_ysign_xs       r)   _get_azimuthr     s~    WT#FWT#F FqAv 	

1a
 Av UUS[6!	 !3, -0r+   xyzc                     t        t        j                  j                  |             }d|k(  ryt	        | d   | d         }t        j
                  | d   |z        }|||fS )zCompute spherical coordinates (r, azimuth, polar_angle) for X,Y,Z point.

    :param array xyz: column vector (3 row x 1 column NumPy array)
    :return: tuple of r, azimuth, polar_angle for input coordinate
    r   )r   r   r   r   r
   )floatr   r|   r.   r   r   )r   razimuthpolar_angles       r)   get_spherical_coordinatesr     s\     	biinnS!"AAv3q63q6*G3q6A:.Kw$$r+      r   a0a1a2revc                    t         }t        }t        }t        }t	        |d    |d    |d    |       ||z
  }t        |      }	t        |	d    |       t        |	d    |       t        j                  t        j                  t                     }
|
j                  |       }t        |d   |d         }t        | |       t        j                  |
      }
|s|
dfS t        ||       t        |	d   |       t        |	d   |       t	        |d   |d   |d   |       t         j                  t        j                  t        j                  t                          }|
|fS )a  Generate transformation matrix to coordinate space defined by 3 points.

    New coordinate space will have:
        acs[0] on XZ plane
        acs[1] origin
        acs[2] on +Z axis

    :param NumPy column array x3 acs: X,Y,Z column input coordinates x3
    :param bool rev: if True, also return reverse transformation matrix
        (to return from coord_space)
    :returns: 4x4 NumPy array, x2 if rev=True
    r   r   r
   N)
gtmgmrygmrzgmrz2r   r   r   r   rB   r   )r   r   r   r   tmmrymrzmrz2rD   scmtazimuth2mrs                r)   coord_spacer     sG   4 
B
C
CD AAA3 	RA	"1	%B A$A$ 
$((3-	 B 	r
A AaD!A$'H 	4( 
2B 4x
 $'1s#1s#1r!ubeR0 
$((5/*	+B r6Mr+   c                 .   t        j                  | j                  d   ddf      }t        j                  d      |d<   t        j                  |       x|ddddf<   |ddddf<   t        j
                  |       |ddddf<   |ddddf    |ddddf<   |S )zCreate [entries] NumPy Z rotation matrices for [entries] angles.

    :param entries: int number of matrices generated.
    :param angle_rads: NumPy array of angles
    :returns: entries x 4 x 4 homogeneous rotation matrices
    r   r   .Nr   r   emptyrA   r@   r0   r6   )r   rzs     r)   multi_rot_Zr   v  s     
:##A&1-	.Bkk!nBsG "z 22Bq!QwK"Q1W+&&$Bq!QwKaAg;,Bq!QwKIr+   c                 .   t        j                  | j                  d   ddf      }t        j                  d      |d<   t        j                  |       x|ddddf<   |ddddf<   t        j
                  |       |ddddf<   |ddddf    |ddddf<   |S )zCreate [entries] NumPy Y rotation matrices for [entries] angles.

    :param entries: int number of matrices generated.
    :param angle_rads: NumPy array of angles
    :returns: entries x 4 x 4 homogeneous rotation matrices
    r   r   .Nr
   r   )r   rys     r)   multi_rot_Yr     s     
:##A&1-	.Bkk!nBsG "z 22Bq!QwK"Q1W+&&$Bq!QwKaAg;,Bq!QwKIr+   a3dLenc                 $   t        j                  |ddf      }t        j                  d      |d<   | dddddf    |dddddf<   | dddf   | dddf   z
  }t         j                  j	                  |d      }t        j
                  |dddf   |dddf         }t        j                  t        j                  |dddf   ||dk7  	            }t        |       }t        |       }	t        j                  |	t        j                  ||            }
t        j                  |
| dddf   j                  d
dd            j                  d
d      }t        j
                  |dddf   |dddf         }t        |       }|st        j                  ||
dd       S t        j                  ||
dd       }
t        |      }t        |      }t        |      }| dddddf   |dddddf<   ||z  |z  |z  }t        j                  |
|g      S )a  Generate [dLen] transform matrices to coord space defined by 3 points.

    New coordinate space will have:
        acs[0] on XZ plane
        acs[1] origin
        acs[2] on +Z axis

    :param NumPy array [entries]x3x3 [entries] XYZ coords for 3 atoms
    :param bool rev: if True, also return reverse transformation matrix
    (to return from coord_space)
    :returns: [entries] 4x4 NumPy arrays, x2 if rev=True

    r   .Nr   r   r   r
   )r%   )wherer   )r   r   r@   r|   r.   r   r   divider   r   matmulreshapera   )r   r   r   r   rD   r   r   r   r   r   r   r   rz2r   r   r   r   s                    r)   multi_coord_spacer     s     
4A,	Bkk!nBsG1ac	]NBq!A#qyM 	1a42ad8A 			qq!Ajj1a4!AqD'*G))BIIa1gqQ?@K 
gX	B 
k\	"B 
2ryyR(	)B 			"bAh&&r1a0199"a@A zz!AqD'1QT7+H xi
 C yybe$$ 
31	B x D k
"C g
C q!QqSyMBq!A#qyM	cC$	B88RHr+   )F)(r   r/   typingr   numpyr   r*   r4   r>   rotaxisrJ   rL   rQ   r[   r   r   strndarrayr   r   r   r   r   r   r   r   tupler   r@   r   r   r   r   r   boolr   r   r   intr   r   r+   r)   <module>r      sp   :   @F:&*Z >:"6   D&
e &
3 &
2:: &
RE 

 E 

 E 

 u  5 RZZ 5 U u 2:: 5 RZZ E e  %2:: %%ue8K2L % bkk!2::&r{{1BJJ'r{{1BJJ'ARZZ( AFd


d

d(*

d9=d
2::rzzD(()dNBJJ 2:: BJJ 2::  E"** EC Ed Erzz Er+   