Ë
    :vžjÓ ã            	       óä  — d Z ddlZddlZdZdZdZd„ Zd„ Zd„ Zd	„ Z	d
„ Z
dgd„Zd„ Zdhd„Zd„ Z	 	 did„Zdjd„Zdjd„Zd„ Zd„ Zd„ Z	 	 dkd„Zd„ Zdld„Zdmd„Zdnd„Zdnd„Zdnd„Zdnd„Zd„ Zd „ Zdjd!„Zd"„ Z d#„ Z!d$„ Z"d%„ Z#d&„ Z$dod'„Z%dgd(„Z&dgd)„Z' G d*„ d+e(«      Z)d,„ Z*d-„ Z+d.„ Z, ejZ                  e.«      j^                  d/z  Z0g d0¢Z1i dd1“d2d3“d4d5“d6d7“d8d9“d:d;“d<d=“d>d?“d@dA“dBdC“dDdE“dFdG“dHdI“dJdK“dLdM“dNdO“dPdQ“dRdSdTdUdVdWdXdYœ¥Z2 e3dZ„ e2ji                  «       D «       «      Z5dhd[„Z6dhd\„Z7d]„ Z8dpd^„Z9dqd_„Z:d`„ Z;da„ Z<db„ Z=drdc„Z>e?ddk(  r.ddl@Z@ddlAZA ej„                  dde¬f«        e@j†                  «        yy)saM  Homogeneous Transformation Matrices and Quaternions.

A library for calculating 4x4 matrices for translating, rotating, reflecting,
scaling, shearing, projecting, orthogonalizing, and superimposing arrays of
3D homogeneous coordinates as well as for converting between rotation matrices,
Euler angles, and quaternions. Also includes an Arcball control object and
functions to decompose transformation matrices.

:Author:
  `Christoph Gohlke <http://www.lfd.uci.edu/~gohlke/>`_

:Organization:
  Laboratory for Fluorescence Dynamics, University of California, Irvine

:Version: 2015.07.18

Requirements
------------
* `CPython 2.7 or 3.4 <http://www.python.org>`_
* `Numpy 1.9 <http://www.numpy.org>`_
* `Transformations.c 2015.07.18 <http://www.lfd.uci.edu/~gohlke/>`_
  (recommended for speedup of some functions)

Notes
-----
The API is not stable yet and is expected to change between revisions.

This Python code is not optimized for speed. Refer to the transformations.c
module for a faster implementation of some functions.

Documentation in HTML format can be generated with epydoc.

Matrices (M) can be inverted using numpy.linalg.inv(M), be concatenated using
numpy.dot(M0, M1), or transform homogeneous coordinate arrays (v) using
numpy.dot(M, v) for shape (4, *) column vectors, respectively
numpy.dot(v, M.T) for shape (*, 4) row vectors ("array of points").

This module follows the "column vectors on the right" and "row major storage"
(C contiguous) conventions. The translation components are in the right column
of the transformation matrix, i.e. M[:3, 3].
The transpose of the transformation matrices may have to be used to interface
with other graphics systems, e.g. with OpenGL's glMultMatrixd(). See also [16].

Calculations are carried out with numpy.float64 precision.

Vector, point, quaternion, and matrix function arguments are expected to be
"array like", i.e. tuple, list, or numpy arrays.

Return types are numpy arrays unless specified otherwise.

Angles are in radians unless specified otherwise.

Quaternions w+ix+jy+kz are represented as [w, x, y, z].

A triple of Euler angles can be applied/interpreted in 24 ways, which can
be specified using a 4 character string or encoded 4-tuple:

  *Axes 4-string*: e.g. 'sxyz' or 'ryxy'

  - first character : rotations are applied to 's'tatic or 'r'otating frame
  - remaining characters : successive rotation axis 'x', 'y', or 'z'

  *Axes 4-tuple*: e.g. (0, 0, 0, 0) or (1, 1, 1, 1)

  - inner axis: code of axis ('x':0, 'y':1, 'z':2) of rightmost matrix.
  - parity : even (0) if inner axis 'x' is followed by 'y', 'y' is followed
    by 'z', or 'z' is followed by 'x'. Otherwise odd (1).
  - repetition : first and last axis are same (1) or different (0).
  - frame : rotations are applied to static (0) or rotating (1) frame.

Other Python packages and modules for 3D transformations and quaternions:

* `Transforms3d <https://pypi.python.org/pypi/transforms3d>`_
   includes most code of this module.
* `Blender.mathutils <http://www.blender.org/api/blender_python_api>`_
* `numpy-dtypes <https://github.com/numpy/numpy-dtypes>`_

References
----------
(1)  Matrices and transformations. Ronald Goldman.
     In "Graphics Gems I", pp 472-475. Morgan Kaufmann, 1990.
(2)  More matrices and transformations: shear and pseudo-perspective.
     Ronald Goldman. In "Graphics Gems II", pp 320-323. Morgan Kaufmann, 1991.
(3)  Decomposing a matrix into simple transformations. Spencer Thomas.
     In "Graphics Gems II", pp 320-323. Morgan Kaufmann, 1991.
(4)  Recovering the data from the transformation matrix. Ronald Goldman.
     In "Graphics Gems II", pp 324-331. Morgan Kaufmann, 1991.
(5)  Euler angle conversion. Ken Shoemake.
     In "Graphics Gems IV", pp 222-229. Morgan Kaufmann, 1994.
(6)  Arcball rotation control. Ken Shoemake.
     In "Graphics Gems IV", pp 175-192. Morgan Kaufmann, 1994.
(7)  Representing attitude: Euler angles, unit quaternions, and rotation
     vectors. James Diebel. 2006.
(8)  A discussion of the solution for the best rotation to relate two sets
     of vectors. W Kabsch. Acta Cryst. 1978. A34, 827-828.
(9)  Closed-form solution of absolute orientation using unit quaternions.
     BKP Horn. J Opt Soc Am A. 1987. 4(4):629-642.
(10) Quaternions. Ken Shoemake.
     http://www.sfu.ca/~jwa3/cmpt461/files/quatut.pdf
(11) From quaternion to matrix and back. JMP van Waveren. 2005.
     http://www.intel.com/cd/ids/developer/asmo-na/eng/293748.htm
(12) Uniform random rotations. Ken Shoemake.
     In "Graphics Gems III", pp 124-132. Morgan Kaufmann, 1992.
(13) Quaternion in molecular modeling. CFF Karney.
     J Mol Graph Mod, 25(5):595-604
(14) New method for extracting the quaternion from a rotation matrix.
     Itzhack Y Bar-Itzhack, J Guid Contr Dynam. 2000. 23(6): 1085-1087.
(15) Multiple View Geometry in Computer Vision. Hartley and Zissermann.
     Cambridge University Press; 2nd Ed. 2004. Chapter 4, Algorithm 4.7, p 130.
(16) Column Vectors vs. Row Vectors.
     http://steve.hollasch.net/cgindex/math/matrix/column-vec.html

Examples
--------
>>> alpha, beta, gamma = 0.123, -1.234, 2.345
>>> origin, xaxis, yaxis, zaxis = [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]
>>> I = identity_matrix()
>>> Rx = rotation_matrix(alpha, xaxis)
>>> Ry = rotation_matrix(beta, yaxis)
>>> Rz = rotation_matrix(gamma, zaxis)
>>> R = concatenate_matrices(Rx, Ry, Rz)
>>> euler = euler_from_matrix(R, 'rxyz')
>>> numpy.allclose([alpha, beta, gamma], euler)
True
>>> Re = euler_matrix(alpha, beta, gamma, 'rxyz')
>>> is_same_transform(R, Re)
True
>>> al, be, ga = euler_from_matrix(Re, 'rxyz')
>>> is_same_transform(Re, euler_matrix(al, be, ga, 'rxyz'))
True
>>> qx = quaternion_about_axis(alpha, xaxis)
>>> qy = quaternion_about_axis(beta, yaxis)
>>> qz = quaternion_about_axis(gamma, zaxis)
>>> q = quaternion_multiply(qx, qy)
>>> q = quaternion_multiply(q, qz)
>>> Rq = quaternion_matrix(q)
>>> is_same_transform(R, Rq)
True
>>> S = scale_matrix(1.23, origin)
>>> T = translation_matrix([1, 2, 3])
>>> Z = shear_matrix(beta, xaxis, origin, zaxis)
>>> R = random_rotation_matrix(numpy.random.rand(3))
>>> M = concatenate_matrices(T, R, Z, S)
>>> scale, shear, angles, trans, persp = decompose_matrix(M)
>>> numpy.allclose(scale, 1.23)
True
>>> numpy.allclose(trans, [1, 2, 3])
True
>>> numpy.allclose(shear, [0, math.tan(beta), 0])
True
>>> is_same_transform(R, euler_matrix(axes='sxyz', *angles))
True
>>> M1 = compose_matrix(scale, shear, angles, trans, persp)
>>> is_same_transform(M, M1)
True
>>> v0, v1 = random_vector(3), random_vector(3)
>>> M = rotation_matrix(angle_between_vectors(v0, v1), vector_product(v0, v1))
>>> v2 = numpy.dot(v0, M[:3,:3].T)
>>> numpy.allclose(unit_vector(v1), unit_vector(v2))
True

é    Nz
2015.07.18zrestructuredtext en© c                  ó,   — t        j                  d«      S )zãReturn 4x4 identity/unit matrix.

    >>> I = identity_matrix()
    >>> numpy.allclose(I, numpy.dot(I, I))
    True
    >>> numpy.sum(I), numpy.trace(I)
    (4.0, 4.0)
    >>> numpy.allclose(I, numpy.identity(4))
    True

    é   ©ÚnumpyÚidentityr   ó    úh/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/meshcat/transformations.pyÚidentity_matrixr   Í   s   € ô �>‰>˜!ÓÐr	   c                 óH   — t        j                  d«      }| dd |dd…df<   |S )z Return matrix to translate by direction vector.

    >>> v = numpy.random.random(3) - 0.5
    >>> numpy.allclose(v, translation_matrix(v)[:3, 3])
    True

    r   Né   r   )Ú	directionÚMs     r
   Útranslation_matrixr   Ü   s.   € ô 	�‰�qÓ€AØ˜˜!ˆ}€A€b€q€bˆ!€e�HØ€Hr	   c                 óZ   — t        j                  | d¬«      dd…df   j                  «       S )zÈReturn translation vector from translation matrix.

    >>> v0 = numpy.random.random(3) - 0.5
    >>> v1 = translation_from_matrix(translation_matrix(v0))
    >>> numpy.allclose(v0, v1)
    True

    F©ÚcopyNr   )r   Úarrayr   ©Úmatrixs    r
   Útranslation_from_matrixr   é   s+   € ô �;‰;�v EÔ*¨2¨A¨2¨q¨5Ñ1×6Ñ6Ó8Ð8r	   c                 óì   — t        |dd «      }t        j                  d«      }|dd…dd…fxx   dt        j                  ||«      z  z  cc<   dt        j                  | dd |«      z  |z  |dd…df<   |S )aÏ  Return matrix to mirror at plane defined by point and normal vector.

    >>> v0 = numpy.random.random(4) - 0.5
    >>> v0[3] = 1.
    >>> v1 = numpy.random.random(3) - 0.5
    >>> R = reflection_matrix(v0, v1)
    >>> numpy.allclose(2, numpy.trace(R))
    True
    >>> numpy.allclose(v0, numpy.dot(R, v0))
    True
    >>> v2 = v0.copy()
    >>> v2[:3] += v1
    >>> v3 = v0.copy()
    >>> v2[:3] -= v1
    >>> numpy.allclose(v2, numpy.dot(R, v3))
    True

    Nr   r   ç       @)Úunit_vectorr   r   ÚouterÚdot)ÚpointÚnormalr   s      r
   Úreflection_matrixr   õ   s}   € ô& ˜  ˜Ó$€FÜ�‰�qÓ€AØ€b€q€bˆ"ˆ1ˆ"€fƒI�”u—{‘{ 6¨6Ó2Ñ2Ñ2ƒIØ”e—i‘i  b q 	¨6Ó2Ñ2°fÑ<€A€b€q€bˆ!€e�HØ€Hr	   c                 óô  — t        j                  | t         j                  d¬«      }t         j                  j	                  |dd…dd…f   «      \  }}t        j
                  t        t        j                  |«      dz   «      dk  «      d   }t        |«      st        d«      ‚t        j                  |dd…|d   f   «      j                  «       }t         j                  j	                  |«      \  }}t        j
                  t        t        j                  |«      dz
  «      dk  «      d   }t        |«      st        d	«      ‚t        j                  |dd…|d
   f   «      j                  «       }||d   z  }||fS )aQ  Return mirror plane point and normal vector from reflection matrix.

    >>> v0 = numpy.random.random(3) - 0.5
    >>> v1 = numpy.random.random(3) - 0.5
    >>> M0 = reflection_matrix(v0, v1)
    >>> point, normal = reflection_from_matrix(M0)
    >>> M1 = reflection_matrix(point, normal)
    >>> is_same_transform(M0, M1)
    True

    F©Údtyper   Nr   ç      ð?ç:Œ0âŽyE>r   z2no unit eigenvector corresponding to eigenvalue -1ú1no unit eigenvector corresponding to eigenvalue 1éÿÿÿÿ)r   r   Úfloat64ÚlinalgÚeigÚwhereÚabsÚrealÚlenÚ
ValueErrorÚsqueeze)r   r   ÚwÚVÚir   r   s          r
   Úreflection_from_matrixr3     s2  € ô 	�‰�F¤%§-¡-°eÔ<€Aä�<‰<×Ñ˜A˜b˜q˜b " 1 "˜f™IÓ&�D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÜˆqŒ6ÜÐMÓNÐNÜ�Z‰Z˜š!˜Q˜q™T˜'™
Ó#×+Ñ+Ó-€Fä�<‰<×Ñ˜AÓ�D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÜˆqŒ6ÜÐLÓMÐMÜ�J‰J�qš˜A˜b™E˜‘{Ó#×+Ñ+Ó-€EØ	ˆU�1‰XÑ€EØ�&ˆ=Ðr	   c                 ó6  — t        j                  | «      }t        j                  | «      }t        |dd «      }t	        j
                  |||g«      }|t	        j                  ||«      d|z
  z  z  }||z  }|t	        j                  d|d    |d   g|d   d|d    g|d    |d   dgg«      z  }t	        j                  d«      }||dd…dd…f<   |�It	        j                  |dd t        j                  d	¬
«      }|t	        j                  ||«      z
  |dd…df<   |S )aŽ  Return matrix to rotate about axis defined by point and direction.

    >>> R = rotation_matrix(math.pi/2, [0, 0, 1], [1, 0, 0])
    >>> numpy.allclose(numpy.dot(R, [0, 0, 0, 1]), [1, -1, 0, 1])
    True
    >>> angle = (random.random() - 0.5) * (2*math.pi)
    >>> direc = numpy.random.random(3) - 0.5
    >>> point = numpy.random.random(3) - 0.5
    >>> R0 = rotation_matrix(angle, direc, point)
    >>> R1 = rotation_matrix(angle-2*math.pi, direc, point)
    >>> is_same_transform(R0, R1)
    True
    >>> R0 = rotation_matrix(angle, direc, point)
    >>> R1 = rotation_matrix(-angle, -direc, point)
    >>> is_same_transform(R0, R1)
    True
    >>> I = numpy.identity(4, numpy.float64)
    >>> numpy.allclose(I, rotation_matrix(math.pi*2, direc))
    True
    >>> numpy.allclose(2, numpy.trace(rotation_matrix(math.pi/2,
    ...                                               direc, point)))
    True

    Nr   r#   ç        é   é   r   r   Fr!   )ÚmathÚsinÚcosr   r   Údiagr   r   r   r'   r   )Úangler   r   ÚsinaÚcosaÚRr   s          r
   Úrotation_matrixr@   ,  s-  € ô2 �8‰8�E‹?€DÜ�8‰8�E‹?€DÜ˜I b q˜MÓ*€Iä�
‰
�D˜$ Ð%Ó&€AØŒ�‰�Y 	Ó	*¨c°D©jÑ	9Ñ9€AØ�Ñ€IØŒ�‰˜ i°¡l ]°Y¸q±\ÐBØ! !™ c°Y¸q±\°MÐBØ! !™�} i°¡l°SÐ9ð;ó 
<ñ <€Aô 	�‰�qÓ€AØ€A€b€q€bˆ"ˆ1ˆ"€f�IØÐä—‘˜E " 1˜I¬U¯]©]ÀÔGˆØœ5Ÿ9™9 Q¨Ó.Ñ.ˆˆ"ˆ1ˆ"ˆaˆ%‰Ø€Hr	   c                 óf  — t        j                  | t         j                  d¬«      }|dd…dd…f   }t         j                  j	                  |j
                  «      \  }}t        j                  t        t        j                  |«      dz
  «      dk  «      d   }t        |«      st        d«      ‚t        j                  |dd…|d	   f   «      j                  «       }t         j                  j	                  |«      \  }}t        j                  t        t        j                  |«      dz
  «      dk  «      d   }t        |«      st        d«      ‚t        j                  |dd…|d	   f   «      j                  «       }||d   z  }t        j                  |«      dz
  d
z  }	t        |d   «      dkD  r|d   |	dz
  |d   z  |d   z  z   |d   z  }
nLt        |d   «      dkD  r|d   |	dz
  |d   z  |d   z  z   |d   z  }
n|d   |	dz
  |d   z  |d   z  z   |d   z  }
t        j                  |
|	«      }|||fS )a‘  Return rotation angle and axis from rotation matrix.

    >>> angle = (random.random() - 0.5) * (2*math.pi)
    >>> direc = numpy.random.random(3) - 0.5
    >>> point = numpy.random.random(3) - 0.5
    >>> R0 = rotation_matrix(angle, direc, point)
    >>> angle, direc, point = rotation_from_matrix(R0)
    >>> R1 = rotation_matrix(angle, direc, point)
    >>> is_same_transform(R0, R1)
    True

    Fr!   Nr   r#   r$   r   r%   r&   r   r6   ©r7   r   r7   ©r   r6   ©r6   r7   )r   r   r'   r(   r)   ÚTr*   r+   r,   r-   r.   r/   Útracer8   Úatan2)r   r?   ÚR33r0   ÚWr2   r   ÚQr   r>   r=   r<   s               r
   Úrotation_from_matrixrK   X  s  € ô 	�‰�F¤%§-¡-°eÔ<€AØ
ˆBˆQˆB���ˆF‰)€Cä�<‰<×Ñ˜CŸE™EÓ"�D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÜˆqŒ6ÜÐLÓMÐMÜ—
‘
˜1šQ  "¡˜X™;Ó'×/Ñ/Ó1€Iä�<‰<×Ñ˜AÓ�D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÜˆqŒ6ÜÐLÓMÐMÜ�J‰J�qš˜A˜b™E˜‘{Ó#×+Ñ+Ó-€EØ	ˆU�1‰XÑ€Eä�K‰K˜Ó˜sÑ" cÑ)€DÜ
ˆ9�Q‰<Ó˜4ÒØ�$‘˜4 ™8 Y¨q¡\Ñ1°)¸A±,Ñ>Ñ>À)ÈAÁ,ÑN‰Ü	ˆY�q‰\Ó	˜TÒ	!Ø�$‘˜4 ™8 Y¨q¡\Ñ1°)¸A±,Ñ>Ñ>À)ÈAÁ,ÑN‰à�$‘˜4 ™8 Y¨q¡\Ñ1°)¸A±,Ñ>Ñ>À)ÈAÁ,ÑNˆÜ�J‰J�t˜TÓ"€EØ�)˜UÐ"Ð"r	   c                 óx  — |€=t        j                  | | | dg«      }|� |dd |dd…df<   |dd…dfxx   d| z
  z  cc<   |S t        |dd «      }d| z
  } t        j                  d«      }|dd…dd…fxx   | t        j                  ||«      z  z  cc<   |�&| t        j
                  |dd |«      z  |z  |dd…df<   |S )að  Return matrix to scale by factor around origin in direction.

    Use factor -1 for point symmetry.

    >>> v = (numpy.random.rand(4, 5) - 0.5) * 20
    >>> v[3] = 1
    >>> S = scale_matrix(-1.234)
    >>> numpy.allclose(numpy.dot(S, v)[:3], -1.234*v[:3])
    True
    >>> factor = random.random() * 10 - 5
    >>> origin = numpy.random.random(3) - 0.5
    >>> direct = numpy.random.random(3) - 0.5
    >>> S = scale_matrix(factor, origin)
    >>> S = scale_matrix(factor, origin, direct)

    Nr#   r   r   )r   r;   r   r   r   r   )ÚfactorÚoriginr   r   s       r
   Úscale_matrixrO   €  sé   € ð" Ðä�J‰J˜ ¨°Ð4Ó5ˆØÐØ˜b˜q�zˆAˆbˆqˆb�!ˆe‰HØˆbˆqˆb�!ˆe‹H˜˜f™Ñ$‹Hð €Hô   	¨"¨1 Ó.ˆ	Ø�v‘ˆÜ�N‰N˜1ÓˆØ	ˆ"ˆ1ˆ"ˆbˆqˆbˆ&‹	�VœeŸk™k¨)°YÓ?Ñ?Ñ?‹	ØÐØ¤§¡¨6°"°1¨:°yÓ!AÑAÀYÑNˆAˆbˆqˆb�!ˆe‰HØ€Hr	   c                 óN  — t        j                  | t         j                  d¬«      }|dd…dd…f   }t        j                  |«      dz
  }	 t         j                  j                  |«      \  }}t        j                  t        t        j                  |«      |z
  «      dk  «      d   d   }t        j                  |dd…|f   «      j                  «       }|t        |«      z  }t         j                  j                  |«      \  }}t        j                  t        t        j                  |«      d	z
  «      dk  «      d   }t        |«      st        d
«      ‚t        j                  |dd…|d   f   «      j                  «       }||d   z  }|||fS # t        $ r |dz   dz  }d}Y ŒÁw xY w)a]  Return scaling factor, origin and direction from scaling matrix.

    >>> factor = random.random() * 10 - 5
    >>> origin = numpy.random.random(3) - 0.5
    >>> direct = numpy.random.random(3) - 0.5
    >>> S0 = scale_matrix(factor, origin)
    >>> factor, origin, direction = scale_from_matrix(S0)
    >>> S1 = scale_matrix(factor, origin, direction)
    >>> is_same_transform(S0, S1)
    True
    >>> S0 = scale_matrix(factor, origin, direct)
    >>> factor, origin, direction = scale_from_matrix(S0)
    >>> S1 = scale_matrix(factor, origin, direction)
    >>> is_same_transform(S0, S1)
    True

    Fr!   Nr   r   r$   r   ç      @r#   ú,no eigenvector corresponding to eigenvalue 1r&   )r   r   r'   rF   r(   r)   r*   r+   r,   r/   Úvector_normÚ
IndexErrorr-   r.   )	r   r   ÚM33rM   r0   r1   r2   r   rN   s	            r
   Úscale_from_matrixrV   ¢  su  € ô$ 	�‰�F¤%§-¡-°eÔ<€AØ
ˆBˆQˆB���ˆF‰)€CÜ�[‰[˜Ó Ñ#€Fð	ä�|‰|×Ñ Ó$‰ˆˆ1Ü�K‰KœœEŸJ™J q›M¨FÑ2Ó3°dÑ:Ó;¸AÑ>¸qÑAˆÜ—J‘J˜q¢ A ™wÓ'×/Ñ/Ó1ˆ	Ø”[ Ó+Ñ+ˆ	ô �<‰<×Ñ˜AÓ�D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÜˆqŒ6ÜÐGÓHÐHÜ�Z‰Z˜š!˜Q˜r™U˜(™Ó$×,Ñ,Ó.€FØ
ˆf�Q‰iÑ€FØ�6˜9Ð$Ð$øô ò à˜3‘, #Ñ%ˆØŠ	ðús   ÁBF ÆF$Æ#F$c                 ób  — t        j                  d«      }t        j                  | dd t         j                  d¬«      } t	        |dd «      }|��t        j                  |dd t         j                  d¬«      }t        j
                  || z
  |«      x|d<   x|d<   |d<   |dd…dd…fxx   t        j                  ||«      z  cc<   |rK|dd…dd…fxx   t        j                  ||«      z  cc<   t        j
                  | |«      ||z   z  |dd…df<   n t        j
                  | |«      |z  |dd…df<   | |ddd…f<   t        j
                  ||«      |d	<   |S |�Žt        j                  |dd t         j                  d¬«      }t        j
                  ||«      }|dd…dd…fxx   t        j                  ||«      |z  z  cc<   |t        j
                  | |«      |z  z  |dd…df<   |S |dd…dd…fxx   t        j                  ||«      z  cc<   t        j
                  | |«      |z  |dd…df<   |S )
a†  Return matrix to project onto plane defined by point and normal.

    Using either perspective point, projection direction, or none of both.

    If pseudo is True, perspective projections will preserve relative depth
    such that Perspective = dot(Orthogonal, PseudoPerspective).

    >>> P = projection_matrix([0, 0, 0], [1, 0, 0])
    >>> numpy.allclose(P[1:, 1:], numpy.identity(4)[1:, 1:])
    True
    >>> point = numpy.random.random(3) - 0.5
    >>> normal = numpy.random.random(3) - 0.5
    >>> direct = numpy.random.random(3) - 0.5
    >>> persp = numpy.random.random(3) - 0.5
    >>> P0 = projection_matrix(point, normal)
    >>> P1 = projection_matrix(point, normal, direction=direct)
    >>> P2 = projection_matrix(point, normal, perspective=persp)
    >>> P3 = projection_matrix(point, normal, perspective=persp, pseudo=True)
    >>> is_same_transform(P2, numpy.dot(P0, P3))
    True
    >>> P = projection_matrix([3, 0, 0], [1, 1, 0], [1, 0, 0])
    >>> v0 = (numpy.random.rand(4, 5) - 0.5) * 20
    >>> v0[3] = 1
    >>> v1 = numpy.dot(P, v0)
    >>> numpy.allclose(v1[1], v0[1])
    True
    >>> numpy.allclose(v1[0], 3-v1[1])
    True

    r   Nr   Fr!   ©r   r   ©r7   r7   ©r6   r6   ©r   r   )r   r   r   r'   r   r   r   )r   r   r   ÚperspectiveÚpseudor   Úscales          r
   Úprojection_matrixr_   Ë  s)  € ô@ 	�‰�qÓ€AÜ�K‰K˜˜b˜q˜	¬¯©¸UÔC€EÜ˜  ˜Ó$€FØÑä—k‘k +¨b¨q /¼¿¹Ø',ô.ˆä&+§i¡i°¸EÑ0AÀ6Ó&JÐJˆˆ$‰ÐJ�!�D‘'˜A˜d™GØ	ˆ"ˆ1ˆ"ˆbˆqˆbˆ&‹	”U—[‘[ ¨fÓ5Ñ5‹	Ùàˆbˆqˆb�"�1�"ˆf‹IœŸ™ V¨VÓ4Ñ4‹IÜ—y‘y ¨Ó/°;¸vÑ3EÑFˆAˆbˆqˆb�!ˆeŠHä—y‘y ¨Ó/°+Ñ=ˆAˆbˆqˆb�!ˆe‰HØ�7ˆˆ!ˆRˆaˆRˆ%‰Ü—)‘)˜K¨Ó0ˆˆ$‰ð €Hð 
Ð	ä—K‘K 	¨"¨1 ´U·]±]ÈÔOˆ	Ü—	‘	˜) VÓ,ˆØ	ˆ"ˆ1ˆ"ˆbˆqˆbˆ&‹	”U—[‘[ ¨FÓ3°eÑ;Ñ;‹	Ø¤§	¡	¨%°Ó 8¸5Ñ @ÑAˆˆ"ˆ1ˆ"ˆaˆ%‰ð
 €Hð 	
ˆ"ˆ1ˆ"ˆbˆqˆbˆ&‹	”U—[‘[ ¨Ó0Ñ0‹	Ü—9‘9˜U FÓ+¨fÑ4ˆˆ"ˆ1ˆ"ˆaˆ%‰Ø€Hr	   c                 óà  — t        j                  | t         j                  d¬«      }|dd…dd…f   }t         j                  j	                  |«      \  }}t        j
                  t        t        j                  |«      dz
  «      dk  «      d   }|�s¢t        |«      �r–t        j                  |dd…|d   f   «      j                  «       }||d   z  }t         j                  j	                  |«      \  }}t        j
                  t        t        j                  |«      «      dk  «      d   }t        |«      st        d	«      ‚t        j                  |dd…|d   f   «      j                  «       }|t        |«      z  }t         j                  j	                  |j                  «      \  }}t        j
                  t        t        j                  |«      «      dk  «      d   }t        |«      rBt        j                  |dd…|d   f   «      j                  «       }	|	t        |	«      z  }	||	|ddfS ||dddfS t        j
                  t        t        j                  |«      «      dkD  «      d   }t        |«      st        d
«      ‚t        j                  |dd…|d   f   «      j                  «       }||d   z  }|ddd…f    }	|dd…df   t        j                  |dd |	«      z  }
|r|
|	z  }
||	d|
|fS )aš  Return projection plane and perspective point from projection matrix.

    Return values are same as arguments for projection_matrix function:
    point, normal, direction, perspective, and pseudo.

    >>> point = numpy.random.random(3) - 0.5
    >>> normal = numpy.random.random(3) - 0.5
    >>> direct = numpy.random.random(3) - 0.5
    >>> persp = numpy.random.random(3) - 0.5
    >>> P0 = projection_matrix(point, normal)
    >>> result = projection_from_matrix(P0)
    >>> P1 = projection_matrix(*result)
    >>> is_same_transform(P0, P1)
    True
    >>> P0 = projection_matrix(point, normal, direct)
    >>> result = projection_from_matrix(P0)
    >>> P1 = projection_matrix(*result)
    >>> is_same_transform(P0, P1)
    True
    >>> P0 = projection_matrix(point, normal, perspective=persp, pseudo=False)
    >>> result = projection_from_matrix(P0, pseudo=False)
    >>> P1 = projection_matrix(*result)
    >>> is_same_transform(P0, P1)
    True
    >>> P0 = projection_matrix(point, normal, perspective=persp, pseudo=True)
    >>> result = projection_from_matrix(P0, pseudo=True)
    >>> P1 = projection_matrix(*result)
    >>> is_same_transform(P0, P1)
    True

    Fr!   Nr   r#   r$   r   r&   z,no eigenvector corresponding to eigenvalue 0z0no eigenvector not corresponding to eigenvalue 0)r   r   r'   r(   r)   r*   r+   r,   r-   r/   r.   rS   rE   r   )r   r]   r   rU   r0   r1   r2   r   r   r   r\   s              r
   Úprojection_from_matrixra   	  s•  € ô@ 	�‰�F¤%§-¡-°eÔ<€AØ
ˆBˆQˆB���ˆF‰)€CÜ�<‰<×Ñ˜AÓ�D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÚ”c˜!•fä—
‘
˜1šQ  "¡˜X™;Ó'×/Ñ/Ó1ˆØ��q‘Ñˆä�|‰|×Ñ Ó$‰ˆˆ1Ü�K‰KœœEŸJ™J q›MÓ*¨TÑ1Ó2°1Ñ5ˆÜ�1ŒvÜÐKÓLÐLÜ—J‘J˜q¢ A a¡D ™zÓ*×2Ñ2Ó4ˆ	Ø”[ Ó+Ñ+ˆ	ä�|‰|×Ñ §¡Ó&‰ˆˆ1Ü�K‰KœœEŸJ™J q›MÓ*¨TÑ1Ó2°1Ñ5ˆÜˆqŒ6ä—Z‘Z ¢! Q q¡T '¡
Ó+×3Ñ3Ó5ˆFØ”k &Ó)Ñ)ˆFØ˜& )¨T°5Ð8Ð8ð ˜) T¨4°Ð6Ð6ô �K‰KœœEŸJ™J q›MÓ*¨TÑ1Ó2°1Ñ5ˆÜ�1ŒvÜØBóDð Dä—
‘
˜1šQ  "¡˜X™;Ó'×/Ñ/Ó1ˆØ��q‘ÑˆØ�Q˜˜˜�U‘8�ˆØ˜˜˜˜A˜‘h¤§¡¨5°°!¨9°fÓ!=Ñ=ˆÙØ˜6Ñ!ˆKØ�f˜d K°Ð7Ð7r	   c                 ó¢  — | |k\  s
||k\  s||k\  rt        d«      ‚|r^|t        k  rt        d«      ‚d|z  }|| |z
  z  d|| z   || z
  z  dgd|||z
  z  ||z   ||z
  z  dgdd||z   ||z
  z  ||z  ||z
  z  gg d¢g}nAd|| z
  z  dd|| z   | |z
  z  gdd||z
  z  d||z   ||z
  z  gddd||z
  z  ||z   ||z
  z  gg d¢g}t        j                  |«      S )aê  Return matrix to obtain normalized device coordinates from frustum.

    The frustum bounds are axis-aligned along x (left, right),
    y (bottom, top) and z (near, far).

    Normalized device coordinates are in range [-1, 1] if coordinates are
    inside the frustum.

    If perspective is True the frustum is a truncated pyramid with the
    perspective point at origin and direction along z axis, otherwise an
    orthographic canonical view volume (a box).

    Homogeneous coordinates transformed by the perspective clip matrix
    need to be dehomogenized (divided by w coordinate).

    >>> frustum = numpy.random.rand(6)
    >>> frustum[1] += frustum[0]
    >>> frustum[3] += frustum[2]
    >>> frustum[5] += frustum[4]
    >>> M = clip_matrix(perspective=False, *frustum)
    >>> numpy.dot(M, [frustum[0], frustum[2], frustum[4], 1])
    array([-1., -1., -1.,  1.])
    >>> numpy.dot(M, [frustum[1], frustum[3], frustum[5], 1])
    array([ 1.,  1.,  1.,  1.])
    >>> M = clip_matrix(perspective=True, *frustum)
    >>> v = numpy.dot(M, [frustum[0], frustum[2], frustum[4], 1])
    >>> v / v[3]
    array([-1., -1., -1.,  1.])
    >>> v = numpy.dot(M, [frustum[1], frustum[3], frustum[4], 1])
    >>> v / v[3]
    array([ 1.,  1., -1.,  1.])

    zinvalid frustumzinvalid frustum: near <= 0r   r5   )r5   r5   ç      ð¿r5   ©r5   r5   r5   r#   )r.   Ú_EPSr   r   )	ÚleftÚrightÚbottomÚtopÚnearÚfarr\   Útr   s	            r
   Úclip_matrixrm   R  s6  € ðD ˆu‚}˜ #š¨°ªÜÐ*Ó+Ð+ÙØ”4Š<ÜÐ9Ó:Ð:Ø�$‰JˆØ��e‘‰n˜c E¨$¡J°°t±Ñ#<¸cÐBØ�1�f˜S‘j‘> C¨¡J°°V±Ñ#<¸cÐBØ�3˜˜T™ D¨¡HÑ-¨q°©u°c¸$±hÑ/?Ð@Ú"ð$‰ð
 �5˜‘:Ñ  S¨5°©:¸¸U¹
Ñ*CÐDØ�3˜˜F™
Ñ# S¨3¨v©:¸¸s¹
Ñ*CÐDØ�3˜˜S ™X™¨¨T©°D¸±HÑ(=Ð>Ú!ð#ˆô �;‰;�q‹>Ðr	   c                 óŽ  — t        |dd «      }t        |dd «      }t        t        j                  ||«      «      dkD  rt	        d«      ‚t        j                  | «      } t        j                  d«      }|dd…dd…fxx   | t        j                  ||«      z  z  cc<   |  t        j                  |dd |«      z  |z  |dd…df<   |S )a  Return matrix to shear by angle along direction vector on shear plane.

    The shear plane is defined by a point and normal vector. The direction
    vector must be orthogonal to the plane's normal vector.

    A point P is transformed by the shear matrix into P" such that
    the vector P-P" is parallel to the direction vector and its extent is
    given by the angle of P-P'-P", where P' is the orthogonal projection
    of P onto the shear plane.

    >>> angle = (random.random() - 0.5) * 4*math.pi
    >>> direct = numpy.random.random(3) - 0.5
    >>> point = numpy.random.random(3) - 0.5
    >>> normal = numpy.cross(direct, numpy.random.random(3))
    >>> S = shear_matrix(angle, direct, point, normal)
    >>> numpy.allclose(1, numpy.linalg.det(S))
    True

    Nr   g�íµ ÷Æ°>z/direction and normal vectors are not orthogonalr   )	r   r+   r   r   r.   r8   Útanr   r   )r<   r   r   r   r   s        r
   Úshear_matrixrp   †  sÀ   € ô( ˜  ˜Ó$€FÜ˜I b q˜MÓ*€IÜ
Œ5�9‰9�V˜YÓ'Ó(¨4Ò/ÜÐJÓKÐKÜ�H‰H�U‹O€EÜ�‰�qÓ€AØ€b€q€bˆ"ˆ1ˆ"€fƒI�œŸ™ Y°Ó7Ñ7Ñ7ƒIØˆvœŸ	™	 %¨¨ )¨VÓ4Ñ4°yÑ@€A€b€q€bˆ!€e�HØ€Hr	   c                 ó<  — t        j                  | t         j                  d¬«      }|dd…dd…f   }t         j                  j	                  |«      \  }}t        j
                  t        t        j                  |«      dz
  «      dk  «      d   }t        |«      dk  rt        d	|z  «      ‚t        j                  |dd…|f   «      j                  «       j                  }d
}dD ]6  \  }}t        j                  ||   ||   «      }	t        |	«      }||kD  sŒ3|}|	}
Œ8 
|z  }
t        j                  |t        j                  d«      z
  |
«      }t        |«      }||z  }t!        j"                  |«      }t         j                  j	                  |«      \  }}t        j
                  t        t        j                  |«      dz
  «      dk  «      d   }t        |«      st        d«      ‚t        j                  |dd…|d   f   «      j                  «       }||d   z  }||||
fS )aå  Return shear angle, direction and plane from shear matrix.

    >>> angle = (random.random() - 0.5) * 4*math.pi
    >>> direct = numpy.random.random(3) - 0.5
    >>> point = numpy.random.random(3) - 0.5
    >>> normal = numpy.cross(direct, numpy.random.random(3))
    >>> S0 = shear_matrix(angle, direct, point, normal)
    >>> angle, direct, point, normal = shear_from_matrix(S0)
    >>> S1 = shear_matrix(angle, direct, point, normal)
    >>> is_same_transform(S0, S1)
    True

    Fr!   Nr   r#   g-Cëâ6?r   r6   z/no two linear independent eigenvectors found %src   )©r   r7   rC   ©r7   r6   r$   rR   r&   )r   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   rE   ÚcrossrS   r   r   r8   Úatan)r   r   rU   r0   r1   r2   ÚlenormÚi0Úi1Únr   r   r<   r   s                 r
   Úshear_from_matrixrz   ¥  sØ  € ô 	�‰�F¤%§-¡-°eÔ<€AØ
ˆBˆQˆB���ˆF‰)€Cä�<‰<×Ñ˜CÓ �D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÜ
ˆ1ƒv�‚zÜÐJÈQÑNÓOÐOÜ�
‰
�1’Q˜�T‘7Ó×#Ñ#Ó%×'Ñ'€AØ€FÛ*‰ˆˆBÜ�K‰K˜˜"™˜q ™uÓ%ˆÜ˜‹NˆØˆv‹:ØˆFØ‰Fð +ð ˆfÑ€Fä—	‘	˜#¤§¡¨qÓ 1Ñ1°6Ó:€IÜ˜	Ó"€EØ�Ñ€IÜ�I‰I�eÓ€Eä�<‰<×Ñ˜AÓ�D€A€qÜ�‰”CœŸ
™
 1›¨Ñ+Ó,¨tÑ3Ó4°QÑ7€AÜˆqŒ6ÜÐGÓHÐHÜ�J‰J�qš˜A˜b™E˜‘{Ó#×+Ñ+Ó-€EØ	ˆU�1‰XÑ€EØ�)˜U FÐ*Ð*r	   c                 ó:  — t        j                  | t         j                  d¬«      j                  }t	        |d   «      t
        k  rt        d«      ‚||d   z  }|j                  «       }d|dd…df<   t         j                  j                  |«      st        d«      ‚t        j                  d	«      }g d
¢}g d
¢}t        t	        |dd…df   «      t
        kD  «      rNt        j                  |dd…df   t         j                  j                  |j                  «      «      }d|dd…df<   nt        j                  g d¢«      }|ddd…f   j                  «       }d|ddd…f<   |dd…dd…f   j                  «       }t        |d   «      |d<   |dxx   |d   z  cc<   t        j                  |d   |d   «      |d<   |dxx   |d   |d   z  z  cc<   t        |d   «      |d<   |dxx   |d   z  cc<   |dxx   |d   z  cc<   t        j                  |d   |d   «      |d<   |dxx   |d   |d   z  z  cc<   t        j                  |d   |d   «      |d<   |dxx   |d   |d   z  z  cc<   t        |d   «      |d<   |dxx   |d   z  cc<   |ddxxx |d   z  ccc t        j                  |d   t        j                  |d   |d   «      «      dk  r,t        j                   ||«       t        j                   ||«       t#        j$                  |d    «      |d<   t#        j&                  |d   «      r?t#        j(                  |d   |d   «      |d<   t#        j(                  |d   |d   «      |d<   n%t#        j(                  |d    |d   «      |d<   d|d<   |||||fS )aò  Return sequence of transformations from transformation matrix.

    matrix : array_like
        Non-degenerative homogeneous transformation matrix

    Return tuple of:
        scale : vector of 3 scaling factors
        shear : list of shear factors for x-y, x-z, y-z axes
        angles : list of Euler angles about static x, y, z axes
        translate : translation vector along x, y, z axes
        perspective : perspective partition of matrix

    Raise ValueError if matrix is of wrong type or degenerative.

    >>> T0 = translation_matrix([1, 2, 3])
    >>> scale, shear, angles, trans, persp = decompose_matrix(T0)
    >>> T1 = translation_matrix(trans)
    >>> numpy.allclose(T0, T1)
    True
    >>> S = scale_matrix(0.123)
    >>> scale, shear, angles, trans, persp = decompose_matrix(S)
    >>> scale[0]
    0.123
    >>> R0 = euler_matrix(1, 2, 3)
    >>> scale, shear, angles, trans, persp = decompose_matrix(R0)
    >>> R1 = euler_matrix(*angles)
    >>> numpy.allclose(R0, R1)
    True

    Tr!   r[   zM[3, 3] is zerord   Nr   zmatrix is singular)r   )r5   r5   r5   r5   r   r7   r6   rC   rs   rZ   rr   rX   rD   rY   )r   r   r'   rE   r+   re   r.   r   r(   ÚdetÚzerosÚanyr   ÚinvrS   rt   Únegativer8   Úasinr:   rG   )	r   r   ÚPr^   ÚshearÚanglesr\   Ú	translateÚrows	            r
   Údecompose_matrixr‡   Ò  sV  € ô> 	�‰�F¤%§-¡-°dÔ;×=Ñ=€AÜ
ˆ1ˆT‰7ƒ|”dÒÜÐ*Ó+Ð+Øˆˆ4‰�L€AØ	�‰‹€AØ €A‚aˆ€d�GÜ�<‰<×Ñ˜AÔÜÐ-Ó.Ð.ä�K‰K˜Ó€EÚ€EÚ€Fä
Œ3ˆq��!��Q�‰x‹=œ4ÑÔ Ü—i‘i ¢! Q $¡¬¯©×)9Ñ)9¸!¿#¹#Ó)>Ó?ˆØ$ˆŠ!ˆQˆ$Šä—k‘kÒ"6Ó7ˆà�!�R�a�R�%‘—‘“€IØ€A€aˆˆ!ˆ€e�Hà
ˆBˆQˆB���ˆF‰)�.‰.Ó
€CÜ˜3˜q™6Ó"€Eˆ!�HØˆƒFˆe�A‰hÑƒFÜ�y‰y˜˜Q™  Q¡Ó(€Eˆ!�HØˆƒFˆc�!‰f�u˜Q‘xÑÑƒFÜ˜3˜q™6Ó"€Eˆ!�HØˆƒFˆe�A‰hÑƒFØ	ˆ!ƒH��a‘ÑƒHÜ�y‰y˜˜Q™  Q¡Ó(€Eˆ!�HØˆƒFˆc�!‰f�u˜Q‘xÑÑƒFÜ�y‰y˜˜Q™  Q¡Ó(€Eˆ!�HØˆƒFˆc�!‰f�u˜Q‘xÑÑƒFÜ˜3˜q™6Ó"€Eˆ!�HØˆƒFˆe�A‰hÑƒFØ	ˆ!ˆ"ƒI��q‘ÑƒIä‡y�y��Q‘œŸ™ S¨¡V¨S°©VÓ4Ó5¸Ò9Ü�‰�u˜eÔ$Ü�‰�s˜CÔ ä—	‘	˜3˜t™9˜*Ó%€Fˆ1�IÜ‡x�x��q‘	ÔÜ—J‘J˜s 4™y¨#¨d©)Ó4ˆˆq‰	Ü—J‘J˜s 4™y¨#¨d©)Ó4ˆˆqŠ	ô —J‘J  D¡	˜z¨3¨t©9Ó5ˆˆq‰	Øˆˆq‰	à�%˜ ¨KÐ7Ð7r	   c                 ó–  — t        j                  d«      }|�7t        j                  d«      }|dd |ddd…f<   t        j                  ||«      }|�7t        j                  d«      }|dd |dd…df<   t        j                  ||«      }|�-t        |d   |d   |d   d«      }t        j                  ||«      }|�Ct        j                  d«      }	|d   |	d<   |d   |	d	<   |d   |	d
<   t        j                  ||	«      }| �Ct        j                  d«      }
| d   |
d<   | d   |
d<   | d   |
d<   t        j                  ||
«      }||d   z  }|S )ac  Return transformation matrix from sequence of transformations.

    This is the inverse of the decompose_matrix function.

    Sequence of transformations:
        scale : vector of 3 scaling factors
        shear : list of shear factors for x-y, x-z, y-z axes
        angles : list of Euler angles about static x, y, z axes
        translate : translation vector along x, y, z axes
        perspective : perspective partition of matrix

    >>> scale = numpy.random.random(3) - 0.5
    >>> shear = numpy.random.random(3) - 0.5
    >>> angles = (numpy.random.random(3) - 0.5) * (2*math.pi)
    >>> trans = numpy.random.random(3) - 0.5
    >>> persp = numpy.random.random(4) - 0.5
    >>> M0 = compose_matrix(scale, shear, angles, trans, persp)
    >>> result = decompose_matrix(M0)
    >>> M1 = compose_matrix(*result)
    >>> is_same_transform(M0, M1)
    True

    r   Nr   r   r7   r6   Úsxyzrs   rC   rr   rX   rY   rZ   r[   )r   r   r   Úeuler_matrix)r^   rƒ   r„   r…   r\   r   r‚   rE   r?   ÚZÚSs              r
   Úcompose_matrixr�   '  sY  € ô2 	�‰�qÓ€AØÐÜ�N‰N˜1ÓˆØ˜b˜q�/ˆˆ!ŠQˆ$‰Ü�I‰I�a˜‹OˆØÐÜ�N‰N˜1ÓˆØ˜R˜a�=ˆˆ"ˆ1ˆ"ˆaˆ%‰Ü�I‰I�a˜‹OˆØÐÜ˜ ™ F¨1¡I¨v°a©y¸&ÓAˆÜ�I‰I�a˜‹OˆØÐÜ�N‰N˜1ÓˆØ˜‘(ˆˆ$‰Ø˜‘(ˆˆ$‰Ø˜‘(ˆˆ$‰Ü�I‰I�a˜‹OˆØÐÜ�N‰N˜1ÓˆØ˜‘(ˆˆ$‰Ø˜‘(ˆˆ$‰Ø˜‘(ˆˆ$‰Ü�I‰I�a˜‹OˆØˆˆ4‰�L€AØ€Hr	   c                 ód  — | \  }}}t        j                  |«      }t        j                  |«      \  }}}t        j                  |«      \  }}	}
||	z  |
z
  ||z  z  }t        j                  ||z  t        j                  d||z  z
  «      z  dddg| |z  |z  ||z  ddg||	z  ||z  |dgg d¢g«      S )a·  Return orthogonalization matrix for crystallographic cell coordinates.

    Angles are expected in degrees.

    The de-orthogonalization matrix is the inverse.

    >>> O = orthogonalization_matrix([10, 10, 10], [90, 90, 90])
    >>> numpy.allclose(O[:3, :3], numpy.identity(3, float) * 10)
    True
    >>> O = orthogonalization_matrix([9.8, 12.0, 15.5], [87.2, 80.7, 69.7])
    >>> numpy.allclose(numpy.sum(O), 43.063229)
    True

    r#   r5   rd   )r   Úradiansr9   r:   r   r8   Úsqrt)Úlengthsr„   ÚaÚbÚcr=   ÚsinbÚ_r>   ÚcosbÚcosgÚcos               r
   Úorthogonalization_matrixrš   \  sÊ   € ð �G€A€qˆ!Ü�]‰]˜6Ó"€FÜ—I‘I˜fÓ%�M€Dˆ$�Ü—y‘y Ó(Ñ€Dˆ$�Ø
�‰+˜Ñ
 ¨¡Ñ	-€BÜ�;‰;Ø
ˆD‰&”—‘˜3˜r "™u™9Ó%Ñ
%¨°°SÐ9Ø
ˆˆD‰�‰¨¨$©°°SÐ9Ø
ˆD‰&¨¨$©°°SÐ9Ú9ð	;ó <ð <r	   Tc           	      óŒ	  — t        j                  | t         j                  d¬«      } t        j                  |t         j                  d¬«      }| j                  d   }|dk  s+| j                  d   |k  s| j                  |j                  k7  rt	        d«      ‚t        j
                  | d¬«       }t        j                  |dz   «      }||d|…|f<   | |j                  |d«      z  } t        j
                  |d¬«       }t        j                  |dz   «      }	||	d|…|f<   ||j                  |d«      z  }|rât        j                  | |fd¬«      }
t         j                  j                  |
j                  «      \  }}}|d| j                  }|d| }||d|z   }t        j                  |t         j                  j                  |«      «      }t        j                  |t        j                  |df«      fd¬«      }t        j                  |d	|z  d
z   f«      }�nÿ|s|dk7  rÛt         j                  j                  t        j                  || j                  «      «      \  }}}t        j                  ||«      }t         j                  j!                  |«      dk  r=|t        j"                  |dd…|dz
  f   ||dz
  dd…f   dz  «      z  }|dxx   dz  cc<   t        j                  |dz   «      }||d|…d|…f<   �nt        j$                  | |z  d¬«      \  }}}t        j$                  | t        j&                  |dd¬«      z  d¬«      \  }}}t        j$                  | t        j&                  |dd¬«      z  d¬«      \  }}}||z   |z   dddg||z
  ||z
  |z
  ddg||z
  ||z   ||z
  |z
  dg||z
  ||z   ||z   ||z
  |z
  gg}t         j                  j)                  |«      \  }}|dd…t        j*                  |«      f   }|t-        |«      z  }t/        |«      }|r[|sY| | z  } ||z  }|d|…d|…fxx   t1        j2                  t        j$                  |«      t        j$                  | «      z  «      z  cc<   t        j                  t         j                  j5                  |	«      t        j                  ||«      «      }||||f   z  }|S )a«  Return affine transform matrix to register two point sets.

    v0 and v1 are shape (ndims, *) arrays of at least ndims non-homogeneous
    coordinates, where ndims is the dimensionality of the coordinate space.

    If shear is False, a similarity transformation matrix is returned.
    If also scale is False, a rigid/Euclidean transformation matrix
    is returned.

    By default the algorithm by Hartley and Zissermann [15] is used.
    If usesvd is True, similarity and Euclidean transformation matrices
    are calculated by minimizing the weighted sum of squared deviations
    (RMSD) according to the algorithm by Kabsch [8].
    Otherwise, and if ndims is 3, the quaternion based algorithm by Horn [9]
    is used, which is slower when using this Python implementation.

    The returned matrix performs rotation, translation and uniform scaling
    (if specified).

    >>> v0 = [[0, 1031, 1031, 0], [0, 0, 1600, 1600]]
    >>> v1 = [[675, 826, 826, 677], [55, 52, 281, 277]]
    >>> affine_matrix_from_points(v0, v1)
    array([[   0.14549,    0.00062,  675.50008],
           [   0.00048,    0.14094,   53.24971],
           [   0.     ,    0.     ,    1.     ]])
    >>> T = translation_matrix(numpy.random.random(3)-0.5)
    >>> R = random_rotation_matrix(numpy.random.random(3))
    >>> S = scale_matrix(random.random())
    >>> M = concatenate_matrices(T, R, S)
    >>> v0 = (numpy.random.rand(4, 100) - 0.5) * 20
    >>> v0[3] = 1
    >>> v1 = numpy.dot(M, v0)
    >>> v0[:3] += numpy.random.normal(0, 1e-8, 300).reshape(3, -1)
    >>> M = affine_matrix_from_points(v0[:3], v1[:3])
    >>> numpy.allclose(v1, numpy.dot(M, v0))
    True

    More examples in superimposition_matrix()

    Tr!   r   r6   r7   z'input arrays are of wrong shape or type©ÚaxisN©r5   )r#   r   r5   r   r&   rc   éþÿÿÿ)r   r   r'   Úshaper.   Úmeanr   ÚreshapeÚconcatenater(   ÚsvdrE   r   Úpinvr}   Úvstackr|   r   ÚsumÚrollÚeighÚargmaxrS   Úquaternion_matrixr8   r�   r   ) Úv0Úv1rƒ   r^   ÚusesvdÚndimsÚt0ÚM0Út1ÚM1ÚAÚuÚsÚvhÚBÚCrl   r   r?   ÚxxÚyyÚzzÚxyÚyzÚzxÚxzÚyxÚzyÚNr0   r1   Úqs                                    r
   Úaffine_matrix_from_pointsrÅ   w  sA  € ôR 
�‰�RœuŸ}™}°4Ô	8€BÜ	�‰�RœuŸ}™}°4Ô	8€Bà�H‰H�Q‰K€EØˆq‚y�B—H‘H˜Q‘K %Ò'¨2¯8©8°r·x±xÒ+?ÜÐBÓCÐCô �*‰*�R˜aÔ
 Ð	 €BÜ	�‰˜˜a™Ó	 €BØ€B€vˆ€vˆu€}ÑØˆ"�*‰*�U˜AÓ
Ñ€BÜ
�*‰*�R˜aÔ
 Ð	 €BÜ	�‰˜˜a™Ó	 €BØ€B€vˆ€vˆu€}ÑØˆ"�*‰*�U˜AÓ
Ñ€Báä×Ñ˜r 2˜h¨QÔ/ˆÜ—<‘<×#Ñ# A§C¡CÓ(‰ˆˆ1ˆbØ��ˆZ�\‰\ˆØˆv�ˆJˆØˆu�Q�u‘WÐˆÜ�I‰I�aœŸ™×*Ñ*¨1Ó-Ó.ˆÜ×Ñ˜q¤%§+¡+¨u°a¨jÓ"9Ð:ÀÔCˆÜ�L‰L˜!˜f U™l¨fÑ4Ð5Ó6ŠÙ	�5˜A’:ä—<‘<×#Ñ#¤E§I¡I¨b°"·$±$Ó$7Ó8‰ˆˆ1ˆbä�I‰I�a˜ÓˆÜ�<‰<×Ñ˜AÓ Ò$à”—‘˜Qšq %¨¡'˜z™]¨B¨u°Q©wº¨z©N¸3Ñ,>Ó?Ñ?ˆAØˆb‹E�T‰M‹Eä�N‰N˜5 ™7Ó#ˆØˆˆ&ˆ5ˆ&�&�5�&ˆ.Óô —Y‘Y˜r B™w¨QÔ/‰
ˆˆB�Ü—Y‘Y˜r¤E§J¡J¨r°2¸AÔ$>Ñ>ÀQÔG‰
ˆˆB�Ü—Y‘Y˜r¤E§J¡J¨r°2¸AÔ$>Ñ>ÀQÔG‰
ˆˆB�Ø�‰e�B‰h˜ #¨CÐ0Ø�‰e˜˜2™˜b™ #¨CÐ0Ø�‰e˜˜2™ " R¡%¨¡(¨CÐ0Ø�‰e˜˜2™ " R¡%¨B¨r©E°"©HÐ5ð7ˆô
 �|‰|× Ñ  Ó#‰ˆˆ1ØŠa”—‘˜a“Ð Ñ!ˆØ	Œ[˜‹^Ñˆä˜aÓ ˆá‘Uà
ˆb‰ˆØ
ˆb‰ˆØ	ˆ&ˆ5ˆ&�&�5�&ˆ.ÓœTŸY™Y¤u§y¡y°£}´u·y±yÀ³}Ñ'DÓEÑEÓô 	�	‰	”%—,‘,×"Ñ" 2Ó&¬¯	©	°!°RÓ(8Ó9€AØˆˆ5�%ˆ<‰Ñ€AØ€Hr	   c                 óÆ   — t        j                  | t         j                  d¬«      dd } t        j                  |t         j                  d¬«      dd }t        | |d||¬«      S )ab  Return matrix to transform given 3D point set into second point set.

    v0 and v1 are shape (3, *) or (4, *) arrays of at least 3 points.

    The parameters scale and usesvd are explained in the more general
    affine_matrix_from_points function.

    The returned matrix is a similarity or Euclidean transformation matrix.
    This function has a fast C implementation in transformations.c.

    >>> v0 = numpy.random.rand(3, 10)
    >>> M = superimposition_matrix(v0, v0)
    >>> numpy.allclose(M, numpy.identity(4))
    True
    >>> R = random_rotation_matrix(numpy.random.random(3))
    >>> v0 = [[1,0,0], [0,1,0], [0,0,1], [1,1,1]]
    >>> v1 = numpy.dot(R, v0)
    >>> M = superimposition_matrix(v0, v1)
    >>> numpy.allclose(v1, numpy.dot(M, v0))
    True
    >>> v0 = (numpy.random.rand(4, 100) - 0.5) * 20
    >>> v0[3] = 1
    >>> v1 = numpy.dot(R, v0)
    >>> M = superimposition_matrix(v0, v1)
    >>> numpy.allclose(v1, numpy.dot(M, v0))
    True
    >>> S = scale_matrix(random.random())
    >>> T = translation_matrix(numpy.random.random(3)-0.5)
    >>> M = concatenate_matrices(T, R, S)
    >>> v1 = numpy.dot(M, v0)
    >>> v0[:3] += numpy.random.normal(0, 1e-9, 300).reshape(3, -1)
    >>> M = superimposition_matrix(v0, v1, scale=True)
    >>> numpy.allclose(v1, numpy.dot(M, v0))
    True
    >>> M = superimposition_matrix(v0, v1, scale=True, usesvd=False)
    >>> numpy.allclose(v1, numpy.dot(M, v0))
    True
    >>> v = numpy.empty((4, 100, 3))
    >>> v[:, :, 0] = v0
    >>> M = superimposition_matrix(v0, v1, scale=True, usesvd=False)
    >>> numpy.allclose(v1, numpy.dot(M, v[:, :, 0]))
    True

    Fr!   Nr   )rƒ   r^   r®   )r   r   r'   rÅ   )r¬   r­   r^   r®   s       r
   Úsuperimposition_matrixrÇ   ä  sY   € ôZ 
�‰�RœuŸ}™}°5Ô	9¸"¸1Ð	=€BÜ	�‰�RœuŸ}™}°5Ô	9¸"¸1Ð	=€BÜ$ R¨°5Ø+0¸ôAð Ar	   r‰   c                 ó¶  — 	 t         |   \  }}}}|}t        ||z      }	t        ||z
  dz      }
|r|| }} |r	|  | | }}} t        j                  | «      t        j                  |«      t        j                  |«      }}}t        j                  | «      t        j                  |«      t        j                  |«      }}}||z  ||z  }}||z  ||z  }}t        j                  d«      }|rh||||f<   ||z  |||	f<   ||z  |||
f<   ||z  ||	|f<   | |z  |z   ||	|	f<   | |z  |z
  ||	|
f<   | |z  ||
|f<   ||z  |z   ||
|	f<   ||z  |z
  ||
|
f<   |S ||z  |||f<   ||z  |z
  |||	f<   ||z  |z   |||
f<   ||z  ||	|f<   ||z  |z   ||	|	f<   ||z  |z
  ||	|
f<   | ||
|f<   ||z  ||
|	f<   ||z  ||
|
f<   |S # t        t        f$ r t        |    |\  }}}}Y �ŒÇw xY w)a‡  Return homogeneous rotation matrix from Euler angles and axis sequence.

    ai, aj, ak : Euler's roll, pitch and yaw angles
    axes : One of 24 axis sequences as string or encoded tuple

    >>> R = euler_matrix(1, 2, 3, 'syxz')
    >>> numpy.allclose(numpy.sum(R[0]), -1.34786452)
    True
    >>> R = euler_matrix(1, 2, 3, (0, 1, 0, 1))
    >>> numpy.allclose(numpy.sum(R[0]), -0.383436184)
    True
    >>> ai, aj, ak = (4*math.pi) * (numpy.random.random(3) - 0.5)
    >>> for axes in _AXES2TUPLE.keys():
    ...    R = euler_matrix(ai, aj, ak, axes)
    >>> for axes in _TUPLE2AXES.keys():
    ...    R = euler_matrix(ai, aj, ak, axes)

    r7   r   )
Ú_AXES2TUPLEÚAttributeErrorÚKeyErrorÚ_TUPLE2AXESÚ
_NEXT_AXISr8   r9   r:   r   r   )ÚaiÚajÚakÚaxesÚ	firstaxisÚparityÚ
repetitionÚframer2   ÚjÚkÚsiÚsjÚskÚciÚcjÚckÚccÚcsÚscÚssr   s                         r
   rŠ   rŠ     sm  € ð&4Ü/:¸4Ñ/@Ñ,ˆ	�6˜: uð
 	€AÜ�1�V‘8Ñ€AÜ�1�V‘8˜A‘:Ñ€AáØ�RˆBˆÙØ�S˜2˜# ˜s�ˆBˆä—‘˜"“œtŸx™x¨›|¬T¯X©X°b«\ˆBˆ€BÜ—‘˜"“œtŸx™x¨›|¬T¯X©X°b«\ˆBˆ€BØ�‰U�B�r‘Eˆ€BØ�‰U�B�r‘Eˆ€Bä�‰�qÓ€AÙØˆˆ!ˆQˆ$‰Ø�R‘%ˆˆ!ˆQˆ$‰Ø�R‘%ˆˆ!ˆQˆ$‰Ø�R‘%ˆˆ!ˆQˆ$‰Ø�#�b‘&˜‘)ˆˆ!ˆQˆ$‰Ø�#�b‘&˜‘)ˆˆ!ˆQˆ$‰Ø�#�b‘&ˆˆ!ˆQˆ$‰Ø�R‘%˜‘(ˆˆ!ˆQˆ$‰Ø�R‘%˜‘(ˆˆ!ˆQˆ$‰ð €Hð �R‘%ˆˆ!ˆQˆ$‰Ø�R‘%˜‘(ˆˆ!ˆQˆ$‰Ø�R‘%˜‘(ˆˆ!ˆQˆ$‰Ø�R‘%ˆˆ!ˆQˆ$‰Ø�R‘%˜‘(ˆˆ!ˆQˆ$‰Ø�R‘%˜‘(ˆˆ!ˆQˆ$‰Ø�#ˆˆ!ˆQˆ$‰Ø�R‘%ˆˆ!ˆQˆ$‰Ø�R‘%ˆˆ!ˆQˆ$‰Ø€HøôO œHÐ%ò 4Ü�DÒØ/3Ñ,ˆ	�6˜:£uð4ús   ‚F5 Æ5GÇGc                 ó®  — 	 t         |j                  «          \  }}}}|}t
        ||z      }t
        ||z
  dz      }t        j                  | t        j                  d¬«      dd…dd…f   }	|rØt        j                  |	||f   |	||f   z  |	||f   |	||f   z  z   «      }
|
t        kD  r^t        j                  |	||f   |	||f   «      }t        j                  |
|	||f   «      }t        j                  |	||f   |	||f    «      }�nt        j                  |	||f    |	||f   «      }t        j                  |
|	||f   «      }d}n×t        j                  |	||f   |	||f   z  |	||f   |	||f   z  z   «      }|t        kD  r]t        j                  |	||f   |	||f   «      }t        j                  |	||f    |«      }t        j                  |	||f   |	||f   «      }n?t        j                  |	||f    |	||f   «      }t        j                  |	||f    |«      }d}|r	| | | }}}|r||}}|||fS # t        t        f$ r t        |    |\  }}}}Y �Œ5w xY w)aŸ  Return Euler angles from rotation matrix for specified axis sequence.

    axes : One of 24 axis sequences as string or encoded tuple

    Note that many Euler angle triplets can describe one matrix.

    >>> R0 = euler_matrix(1, 2, 3, 'syxz')
    >>> al, be, ga = euler_from_matrix(R0, 'syxz')
    >>> R1 = euler_matrix(al, be, ga, 'syxz')
    >>> numpy.allclose(R0, R1)
    True
    >>> angles = (4*math.pi) * (numpy.random.random(3) - 0.5)
    >>> for axes in _AXES2TUPLE.keys():
    ...    R0 = euler_matrix(axes=axes, *angles)
    ...    R1 = euler_matrix(axes=axes, *euler_from_matrix(R0, axes))
    ...    if not numpy.allclose(R0, R1): print(axes, "failed")

    r7   Fr!   Nr   r5   )rÉ   ÚlowerrÊ   rË   rÌ   rÍ   r   r   r'   r8   r�   re   rG   )r   rÑ   rÒ   rÓ   rÔ   rÕ   r2   rÖ   r×   r   ÚsyÚaxÚayÚazÚcys                  r
   Úeuler_from_matrixré   V  s„  € ð&4Ü/:¸4¿:¹:»<Ñ/HÑ,ˆ	�6˜: uð
 	€AÜ�1�V‘8Ñ€AÜ�1�V‘8˜A‘:Ñ€Aä�‰�F¤%§-¡-°eÔ<¸R¸a¸RÀÀ!À¸VÑD€AÙÜ�Y‰Y�q˜˜A˜‘w˜q  A ™w‘¨¨1¨a¨4©°°1°a°4±©Ñ8Ó9ˆØ”Š9Ü—‘˜Q˜q !˜t™W q¨¨A¨¡wÓ/ˆBÜ—‘˜R q¨¨A¨¡wÓ/ˆBÜ—‘˜Q˜q !˜t™W q¨¨A¨¡w hÓ/ŠBä—‘˜Q˜q !˜t™W˜H q¨¨A¨¡wÓ/ˆBÜ—‘˜R q¨¨A¨¡wÓ/ˆBØ‰Bä�Y‰Y�q˜˜A˜‘w˜q  A ™w‘¨¨1¨a¨4©°°1°a°4±©Ñ8Ó9ˆØ”Š9Ü—‘˜Q˜q !˜t™W q¨¨A¨¡wÓ/ˆBÜ—‘˜Q˜q !˜t™W˜H rÓ*ˆBÜ—‘˜Q˜q !˜t™W q¨¨A¨¡wÓ/‰Bä—‘˜Q˜q !˜t™W˜H q¨¨A¨¡wÓ/ˆBÜ—‘˜Q˜q !˜t™W˜H rÓ*ˆBØˆBáØ�S˜2˜# ˜s�ˆBˆÙØ�RˆBˆØˆr�2ˆ:ÐøôE œHÐ%ò 4Ü�DÒØ/3Ñ,ˆ	�6˜:£uð4ús   ‚H1 È1IÉIc                 ó,   — t        t        | «      |«      S )zÅReturn Euler angles from quaternion for specified axis sequence.

    >>> angles = euler_from_quaternion([0.99810947, 0.06146124, 0, 0])
    >>> numpy.allclose(angles, [0.123, 0, 0])
    True

    )ré   r«   )Ú
quaternionrÑ   s     r
   Úeuler_from_quaternionrì   �  s   € ô Ô.¨zÓ:¸DÓAÐAr	   c                 óF  — 	 t         |j                  «          \  }}}}|dz   }t
        ||z   dz
     dz   }	t
        ||z
     dz   }
|r|| }} |r| }| dz  } |dz  }|dz  }t        j                  | «      }t        j                  | «      }t        j                  |«      }t        j                  |«      }t        j                  |«      }t        j                  |«      }||z  }||z  }||z  }||z  }t        j                  d«      }|r-|||z
  z  |d<   |||z   z  ||<   |||z   z  ||	<   |||z
  z  ||
<   n8||z  ||z  z   |d<   ||z  ||z  z
  ||<   ||z  ||z  z   ||	<   ||z  ||z  z
  ||
<   |r||	xx   dz  cc<   |S # t        t        f$ r t        |    |\  }}}}Y �Œ�w xY w)a2  Return quaternion from Euler angles and axis sequence.

    ai, aj, ak : Euler's roll, pitch and yaw angles
    axes : One of 24 axis sequences as string or encoded tuple

    >>> q = quaternion_from_euler(1, 2, 3, 'ryxz')
    >>> numpy.allclose(q, [0.435953, 0.310622, -0.718287, 0.444435])
    True

    r7   r   ©r   r   rc   )rÉ   rã   rÊ   rË   rÌ   rÍ   r8   r:   r9   r   Úempty)rÎ   rÏ   rÐ   rÑ   rÒ   rÓ   rÔ   rÕ   r2   rÖ   r×   rÛ   rØ   rÜ   rÙ   rÝ   rÚ   rÞ   rß   rà   rá   rÄ   s                         r
   Úquaternion_from_eulerrð   ›  sã  € ð4Ü/:¸4¿:¹:»<Ñ/HÑ,ˆ	�6˜: uð
 	�A‰€AÜ�1�V‘8˜A‘:Ñ Ñ"€AÜ�1�V‘8Ñ˜qÑ €AáØ�RˆBˆÙØˆSˆàˆ#�I€BØˆ#�I€BØˆ#�I€BÜ	�‰�"‹€BÜ	�‰�"‹€BÜ	�‰�"‹€BÜ	�‰�"‹€BÜ	�‰�"‹€BÜ	�‰�"‹€BØ	ˆB‰€BØ	ˆB‰€BØ	ˆB‰€BØ	ˆB‰€Bä�‰�EÓ€AÙØ�2˜‘7‰|ˆˆ!‰Ø�2˜‘7‰|ˆˆ!‰Ø�2˜‘7‰|ˆˆ!‰Ø�2˜‘7‰|ˆˆ!Šà�"‰u�r˜"‘u‰}ˆˆ!‰Ø�"‰u�r˜"‘u‰}ˆˆ!‰Ø�"‰u�r˜"‘u‰}ˆˆ!‰Ø�"‰u�r˜"‘u‰}ˆˆ!‰ÙØ	ˆ!‹�‰‹à€HøôS œHÐ%ò 4Ü�DÒØ/3Ñ,ˆ	�6˜:£uð4ús   ‚E= Å=F ÆF c                 óä   — t        j                  d|d   |d   |d   g«      }t        |«      }|t        kD  r|t	        j
                  | dz  «      |z  z  }t	        j                  | dz  «      |d<   |S )z¨Return quaternion for rotation about axis.

    >>> q = quaternion_about_axis(0.123, [1, 0, 0])
    >>> numpy.allclose(q, [0.99810947, 0.06146124, 0, 0])
    True

    r5   r   r7   r6   r   )r   r   rS   re   r8   r9   r:   )r<   r�   rÄ   Úqlens       r
   Úquaternion_about_axisró   Ô  sn   € ô 	�‰�S˜$˜q™' 4¨¡7¨D°©GÐ4Ó5€AÜ�q‹>€DØŒd‚{Ø	ŒT�X‰X�e˜C‘iÓ  4Ñ'Ñ'ˆÜ�8‰8�E˜#‘IÓ€A€a�DØ€Hr	   c           	      ó  — t        j                  | t         j                  d¬«      }t        j                  ||«      }|t        k  rt        j
                  d«      S |t        j                  d|z  «      z  }t        j                  ||«      }t        j                  d|d   z
  |d   z
  |d   |d	   z
  |d
   |d   z   dg|d   |d	   z   d|d   z
  |d   z
  |d   |d   z
  dg|d
   |d   z
  |d   |d   z   d|d   z
  |d   z
  dgg d¢g«      S )a‹  Return homogeneous rotation matrix from quaternion.

    >>> M = quaternion_matrix([0.99810947, 0.06146124, 0, 0])
    >>> numpy.allclose(M, rotation_matrix(0.123, [1, 0, 0]))
    True
    >>> M = quaternion_matrix([1, 0, 0, 0])
    >>> numpy.allclose(M, numpy.identity(4))
    True
    >>> M = quaternion_matrix([0, 1, 0, 0])
    >>> numpy.allclose(M, numpy.diag([1, -1, -1, 1]))
    True

    Tr!   r   r   r#   rZ   r[   rs   )r   r   )r7   r   ©r6   r   r5   rY   )r6   r   rB   rd   )	r   r   r'   r   re   r   r8   r�   r   )rë   rÄ   ry   s      r
   r«   r«   ä  s!  € ô 	�‰�J¤e§m¡m¸$Ô?€AÜ�	‰	�!�Q‹€AØŒ4‚xÜ�~‰~˜aÓ Ð ØŒ�‰�3˜‘7Ó	Ñ€AÜ�‰�A�qÓ€AÜ�;‰;Ø	ˆQˆt‰W‰�Q�t‘WÑ	 ! D¡'¨!¨D©'¡/°q¸±w¸qÀ¹w±ÈÐLØˆt‰W�Q�t‘W‰_˜c ! D¡'™k¨!¨D©'Ñ1°q¸±w¸qÀ¹w±ÈÐLØˆt‰W�Q�t‘W‰_ ! D¡'¨!¨D©'¡/°3°q¸±w±;¸qÀ¹wÑ3FÈÐLÚLð	Nó Oð Or	   c           
      ó8  — t        j                  | t         j                  d¬«      dd…dd…f   }|�rt        j                  d«      }t        j                  |«      }||d   kD  r0||d<   |d   |d	   z
  |d
<   |d   |d   z
  |d<   |d   |d   z
  |d<   n‚d\  }}}|d   |d   kD  rd\  }}}|d   |||f   kD  rd\  }}}|||f   |||f   |||f   z   z
  |d   z   }|||<   |||f   |||f   z   ||<   |||f   |||f   z   ||<   |||f   |||f   z
  |d
<   |dt        j                  ||d   z  «      z  z  }nÃ|d   }|d	   }	|d   }
|d   }|d   }|d   }|d   }|d   }|d   }t        j                  ||z
  |z
  dddg|	|z   ||z
  |z
  ddg|
|z   ||z   ||z
  |z
  dg||z
  |
|z
  ||	z
  ||z   |z   gg«      }|dz  }t         j                  j                  |«      \  }}|g d¢t        j                  |«      f   }|d   dk  rt        j                  ||«       |S )a}  Return quaternion from rotation matrix.

    If isprecise is True, the input matrix is assumed to be a precise rotation
    matrix and a faster algorithm is used.

    >>> q = quaternion_from_matrix(numpy.identity(4), True)
    >>> numpy.allclose(q, [1, 0, 0, 0])
    True
    >>> q = quaternion_from_matrix(numpy.diag([1, -1, -1, 1]))
    >>> numpy.allclose(q, [0, 1, 0, 0]) or numpy.allclose(q, [0, -1, 0, 0])
    True
    >>> R = rotation_matrix(0.123, (1, 2, 3))
    >>> q = quaternion_from_matrix(R, True)
    >>> numpy.allclose(q, [0.9981095, 0.0164262, 0.0328524, 0.0492786])
    True
    >>> R = [[-0.545, 0.797, 0.260, 0], [0.733, 0.603, -0.313, 0],
    ...      [-0.407, 0.021, -0.913, 0], [0, 0, 0, 1]]
    >>> q = quaternion_from_matrix(R)
    >>> numpy.allclose(q, [0.19069, 0.43736, 0.87485, -0.083611])
    True
    >>> R = [[0.395, 0.362, 0.843, 0], [-0.626, 0.796, -0.056, 0],
    ...      [-0.677, -0.498, 0.529, 0], [0, 0, 0, 1]]
    >>> q = quaternion_from_matrix(R)
    >>> numpy.allclose(q, [0.82336615, -0.13610694, 0.46344705, -0.29792603])
    True
    >>> R = random_rotation_matrix()
    >>> q = quaternion_from_matrix(R)
    >>> is_same_transform(R, quaternion_matrix(q))
    True
    >>> R = euler_matrix(0.0, 0.0, numpy.pi/2.0)
    >>> numpy.allclose(quaternion_from_matrix(R, isprecise=False),
    ...                quaternion_from_matrix(R, isprecise=True))
    True

    Fr!   Nr   rî   r[   r   rB   rr   r   rC   rõ   r6   rD   rs   r7   )r7   r6   r   rY   rX   )r6   r   r7   rZ   )r   r7   r6   g      à?r5   rQ   )r   r   r7   r6   )r   r   r'   rï   rF   r8   r�   r(   r©   rª   r€   )r   Ú	ispreciser   rÄ   rl   r2   rÖ   r×   Úm00Úm01Úm02Úm10Úm11Úm12Úm20Úm21Úm22ÚKr0   r1   s                       r
   Úquaternion_from_matrixr  ÿ  s½  € ôH 	�‰�F¤%§-¡-°eÔ<¸R¸a¸RÀÀ!À¸VÑD€AÚÜ�K‰K˜ÓˆÜ�K‰K˜‹NˆØˆq�‰wŠ;ØˆAˆa‰DØ�T‘7˜Q˜t™WÑ$ˆAˆa‰DØ�T‘7˜Q˜t™WÑ$ˆAˆa‰DØ�T‘7˜Q˜t™WÑ$ˆAˆaŠDà‰GˆAˆq�!Ø�‰w˜˜4™Ò Ø!‘��1�aØ�‰w˜˜1˜a˜4™Ò Ø!‘��1�aØ�!�Q�$‘˜1˜Q ˜T™7 Q q¨! t¡WÑ,Ñ-°°$±Ñ7ˆAØˆAˆa‰DØ�Q˜�T‘7˜Q˜q !˜t™WÑ$ˆAˆa‰DØ�Q˜�T‘7˜Q˜q !˜t™WÑ$ˆAˆa‰DØ�Q˜�T‘7˜Q˜q !˜t™WÑ$ˆAˆa‰DØ	ˆS”4—9‘9˜Q  4¡™[Ó)Ñ)Ñ)‰à�‰gˆØ�‰gˆØ�‰gˆØ�‰gˆØ�‰gˆØ�‰gˆØ�‰gˆØ�‰gˆØ�‰gˆä�K‰K˜#˜c™' #™+ s°CÀÐEØ˜c™' s¨3¡w¨s¡{°CÀÐEØ˜c™' s¨3¡w°C¸±G¸C±KÀÐEØ˜c™' s¨3¡w°C¸±GÀÀSÁÈÁÐMðOó Pˆð 	
ˆS‰ˆä�|‰|× Ñ  Ó#‰ˆˆ1ØŠlœEŸL™L¨›OÐ+Ñ,ˆØˆ�tˆc‚zÜ�‰�q˜!ÔØ€Hr	   c                 ó  — |\  }}}}| \  }}}}	t        j                  | |z  ||z  z
  |	|z  z
  ||z  z   ||z  ||z  z   |	|z  z
  ||z  z   | |z  ||z  z   |	|z  z   ||z  z   ||z  ||z  z
  |	|z  z   ||z  z   gt         j                  ¬«      S )z¥Return multiplication of two quaternions.

    >>> q = quaternion_multiply([4, 1, -2, 3], [8, -5, 6, 7])
    >>> numpy.allclose(q, [28, -44, -14, 48])
    True

    ©r"   ©r   r   r'   )
Úquaternion1Úquaternion0Úw0Úx0Úy0Úz0Úw1Úx1Úy1Úz1s
             r
   Úquaternion_multiplyr  P  sÈ   € ð !�N€BˆˆB�Ø �N€BˆˆB�Ü�;‰;˜˜˜B™  B¡™¨¨B©Ñ.°°B±Ñ6Ø˜B™  B¡™¨¨B©Ñ.°°B±Ñ6Ø˜˜B™  B¡™¨¨B©Ñ.°°B±Ñ6Ø˜B™  B¡™¨¨B©Ñ.°°B±Ñ6ð8ô @E¿}¹}ôNð Nr	   c                 óŠ   — t        j                  | t         j                  d¬«      }t        j                  |dd |dd «       |S )z¨Return conjugate of quaternion.

    >>> q0 = random_quaternion()
    >>> q1 = quaternion_conjugate(q0)
    >>> q1[0] == q0[0] and all(q1[1:] == -q0[1:])
    True

    Tr!   r7   N)r   r   r'   r€   ©rë   rÄ   s     r
   Úquaternion_conjugater  `  s9   € ô 	�‰�J¤e§m¡m¸$Ô?€AÜ	‡N�N�1�Q�R�5˜!˜A˜B˜%Ô Ø€Hr	   c                 ó¸   — t        j                  | t         j                  d¬«      }t        j                  |dd |dd «       |t        j                  ||«      z  S )z´Return inverse of quaternion.

    >>> q0 = random_quaternion()
    >>> q1 = quaternion_inverse(q0)
    >>> numpy.allclose(quaternion_multiply(q0, q1), [1, 0, 0, 0])
    True

    Tr!   r7   N)r   r   r'   r€   r   r  s     r
   Úquaternion_inverser  n  sI   € ô 	�‰�J¤e§m¡m¸$Ô?€AÜ	‡N�N�1�Q�R�5˜!˜A˜B˜%Ô ØŒu�y‰y˜˜A‹ÑÐr	   c                 ó   — t        | d   «      S )zTReturn real part of quaternion.

    >>> quaternion_real([3, 0, 1, 2])
    3.0

    r   )Úfloat©rë   s    r
   Úquaternion_realr  |  s   € ô �˜A‘ÓÐr	   c                 óT   — t        j                  | dd t         j                  d¬«      S )zlReturn imaginary part of quaternion.

    >>> quaternion_imag([3, 0, 1, 2])
    array([ 0.,  1.,  2.])

    r7   r   Tr!   r  r  s    r
   Úquaternion_imagr  †  s!   € ô �;‰;�z ! A�¬e¯m©mÀ$ÔGÐGr	   c                 ó8  — t        | dd «      }t        |dd «      }|dk(  r|S |dk(  r|S t        j                  ||«      }t        t        |«      dz
  «      t        k  r|S |r|dk  r| }t        j
                  ||«       t        j                  |«      |t        j                  z  z   }t        |«      t        k  r|S dt        j                  |«      z  }	|t        j                  d|z
  |z  «      |	z  z  }|t        j                  ||z  «      |	z  z  }||z  }|S )a  Return spherical linear interpolation between two quaternions.

    >>> q0 = random_quaternion()
    >>> q1 = random_quaternion()
    >>> q = quaternion_slerp(q0, q1, 0)
    >>> numpy.allclose(q, q0)
    True
    >>> q = quaternion_slerp(q0, q1, 1, 1)
    >>> numpy.allclose(q, q1)
    True
    >>> q = quaternion_slerp(q0, q1, 0.5)
    >>> angle = math.acos(numpy.dot(q0, q))
    >>> (numpy.allclose(2, math.acos(numpy.dot(q0, q1)) / angle) or
         numpy.allclose(2, math.acos(-numpy.dot(q0, q1)) / angle))
    True

    Nr   r5   r#   )
r   r   r   r+   re   r€   r8   ÚacosÚpir9   )
Úquat0Úquat1ÚfractionÚspinÚshortestpathÚq0Úq1Údr<   Úisins
             r
   Úquaternion_slerpr(  �  s  € ô$ 
�U˜2˜A�YÓ	€BÜ	�U˜2˜A�YÓ	€BØ�3‚Øˆ	Ø	�SŠØˆ	Ü�	‰	�"�bÓ€AÜ
Œ3ˆq‹6�C‰<Óœ4ÒØˆ	Ù˜˜CšàˆBˆÜ�‰�r˜2ÔÜ�I‰I�a‹L˜4¤$§'¡'™>Ñ)€EÜ
ˆ5ƒz”DÒØˆ	Ø”—‘˜%“Ñ €DØŒ$�(‰(�C˜(‘N eÑ+Ó
,¨tÑ
3Ñ3€BØŒ$�(‰(�8˜eÑ#Ó
$ tÑ
+Ñ+€BØˆ"�H€BØ€Ir	   c                 óô  — | € t         j                  j                  d«      } nt        | «      dk(  sJ ‚t        j                  d| d   z
  «      }t        j                  | d   «      }t
        j                  dz  }|| d   z  }|| d   z  }t        j                  t        j                  |«      |z  t        j                  |«      |z  t        j                  |«      |z  t        j                  |«      |z  g«      S )ai  Return uniform random unit quaternion.

    rand: array like or None
        Three independent random variables that are uniformly distributed
        between 0 and 1.

    >>> q = random_quaternion()
    >>> numpy.allclose(1, vector_norm(q))
    True
    >>> q = random_quaternion(numpy.random.random(3))
    >>> len(q.shape), q.shape[0]==4
    (1, True)

    r   r#   r   r   r7   r6   )
r   ÚrandomÚrandr-   r�   r8   r  r   r:   r9   )r+  Úr1Úr2Úpi2r²   Út2s         r
   Úrandom_quaternionr0  ¹  sÏ   € ð €|Ü�|‰|× Ñ  Ó#‰ä�4‹y˜AŠ~Ðˆ~Ü	�‰�C˜$˜q™'‘MÓ	"€BÜ	�‰�D˜‘GÓ	€BÜ
�'‰'�C‰-€CØ	ˆt�A‰w‰€BØ	ˆt�A‰w‰€BÜ�;‰;œŸ	™	 "› bÑ(¬%¯)©)°B«-¸Ñ*:ÜŸ	™	 "› bÑ(¬%¯)©)°B«-¸Ñ*:ð<ó =ð =r	   c                 ó*   — t        t        | «      «      S )a.  Return uniform random rotation matrix.

    rand: array like
        Three independent random variables that are uniformly distributed
        between 0 and 1 for each returned quaternion.

    >>> R = random_rotation_matrix()
    >>> numpy.allclose(numpy.dot(R.T, R), numpy.identity(4))
    True

    )r«   r0  )r+  s    r
   Úrandom_rotation_matrixr2  Õ  s   € ô Ô.¨tÓ4Ó5Ð5r	   c                   ór   — e Zd ZdZdd„Zd„ Zd„ Zed„ «       Zej                  d„ «       Zd„ Z
d	„ Zdd
„Zd„ Zy)ÚArcballa^  Virtual Trackball Control.

    >>> ball = Arcball()
    >>> ball = Arcball(initial=numpy.identity(4))
    >>> ball.place([320, 320], 320)
    >>> ball.down([500, 250])
    >>> ball.drag([475, 275])
    >>> R = ball.matrix()
    >>> numpy.allclose(numpy.sum(R), 3.90583455)
    True
    >>> ball = Arcball(initial=[1, 0, 0, 0])
    >>> ball.place([320, 320], 320)
    >>> ball.setaxes([1, 1, 0], [-1, 1, 0])
    >>> ball.constrain = True
    >>> ball.down([400, 200])
    >>> ball.drag([200, 400])
    >>> R = ball.matrix()
    >>> numpy.allclose(numpy.sum(R), 0.2055924)
    True
    >>> ball.next()

    Nc                 óÞ  — d| _         d| _        d| _        ddg| _        t	        j
                  g d¢«      | _        d| _        |€t	        j
                  g d¢«      | _        nut	        j
                  |t        j                  ¬«      }|j                  dk(  rt        |«      | _        n0|j                  d	k(  r|t        |«      z  }|| _        nt        d
«      ‚| j                  x| _        | _        y)z`Initialize virtual trackball control.

        initial : quaternion or rotation matrix

        Nr#   r5   )r5   r5   r#   F)r#   r5   r5   r5   r  )r   r   rî   z"initial not a quaternion or matrix)Ú_axisÚ_axesÚ_radiusÚ_centerr   r   Ú_vdownÚ
_constrainÚ_qdownr'   r    r  rS   r.   Ú_qnowÚ_qpre)ÚselfÚinitials     r
   Ú__init__zArcball.__init__û  s½   € ð ˆŒ
ØˆŒ
ØˆŒØ˜S�zˆŒÜ—k‘k¢/Ó2ˆŒØˆŒØˆ?ÜŸ+™+Ò&:Ó;ˆD�Kä—k‘k '´·±Ô?ˆGØ�}‰} Ò&Ü4°WÓ=�•Ø—‘ %Ò'Øœ; wÓ/Ñ/�Ø%�•ä Ð!EÓFÐFØ"&§+¡+Ð-ˆŒ
�T•Zr	   c                 ól   — t        |«      | _        |d   | j                  d<   |d   | j                  d<   y)z×Place Arcball, e.g. when window size changes.

        center : sequence[2]
            Window coordinates of trackball center.
        radius : float
            Radius of trackball in window coordinates.

        r   r7   N)r  r8  r9  )r?  ÚcenterÚradiuss      r
   ÚplacezArcball.place  s1   € ô ˜V“}ˆŒØ  ™)ˆ�‰�Q‰Ø  ™)ˆ�‰�QŠr	   c                 ó^   — |€d| _         y|D �cg c]  }t        |«      ‘Œ c}| _         yc c}w )z Set axes to constrain rotations.N)r7  r   )r?  rÑ   r�   s      r
   ÚsetaxeszArcball.setaxes!  s,   € àˆ<ØˆD�Já8<Ó=¹°œ+ dÕ+¸Ñ=ˆD�JùÒ=s   �*c                 ó   — | j                   S )z'Return state of constrain to axis mode.)r;  ©r?  s    r
   Ú	constrainzArcball.constrain(  s   € ð �‰Ðr	   c                 ó$   — t        |«      | _        y)z$Set state of constrain to axis mode.N)Úboolr;  )r?  Úvalues     r
   rJ  zArcball.constrain-  s   € ô ˜u›+ˆ�r	   c                 óT  — t        || j                  | j                  «      | _        | j                  x| _        | _        | j                  rW| j                  �Kt        | j                  | j                  «      | _
        t        | j                  | j                  «      | _        yd| _
        y)z>Set initial cursor window coordinates and pick constrain-axis.N)Úarcball_map_to_spherer9  r8  r:  r=  r<  r>  r;  r7  Úarcball_nearest_axisr6  Úarcball_constrain_to_axis)r?  r   s     r
   ÚdownzArcball.down2  so   € ä+¨E°4·<±<ÀÇÁÓNˆŒØ#'§:¡:Ð-ˆŒ�d”jØ�?Š?˜tŸz™zÐ5Ü-¨d¯k©k¸4¿:¹:ÓFˆDŒJÜ3°D·K±KÀÇÁÓLˆD�KàˆD�Jr	   c                 óÚ  — t        || j                  | j                  «      }| j                  �t	        || j                  «      }| j
                  | _        t        j                  | j                  |«      }t        j                  ||«      t        k  r| j                  | _        yt        j                  | j                  |«      |d   |d   |d   g}t        || j                  «      | _        y)z)Update current cursor window coordinates.Nr   r7   r6   )rO  r9  r8  r6  rQ  r=  r>  r   rt   r:  r   re   r<  r  )r?  r   Úvnowrl   rÄ   s        r
   ÚdragzArcball.drag<  s©   € ä$ U¨D¯L©L¸$¿,¹,ÓGˆØ�:‰:Ð!Ü,¨T°4·:±:Ó>ˆDØ—Z‘ZˆŒ
Ü�K‰K˜Ÿ™ TÓ*ˆÜ�9‰9�Q˜‹?œTÒ!ØŸ™ˆD�Jä—‘˜4Ÿ;™;¨Ó-¨q°©t°Q°q±T¸1¸Q¹4Ð@ˆAÜ,¨Q°·±Ó<ˆD�Jr	   c                 ó€   — t        | j                  | j                  d|z   d«      }| j                  |c| _        | _        y)z,Continue rotation in direction of last drag.r   FN)r(  r>  r=  )r?  ÚaccelerationrÄ   s      r
   ÚnextzArcball.nextI  s3   € ä˜TŸZ™Z¨¯©°S¸Ñ5EÀuÓMˆØ!%§¡¨QÐˆŒ
�D•Jr	   c                 ó,   — t        | j                  «      S )z#Return homogeneous rotation matrix.)r«   r=  rI  s    r
   r   zArcball.matrixN  s   € ä  §¡Ó,Ð,r	   ©Nrž   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rA  rE  rG  ÚpropertyrJ  ÚsetterrR  rU  rX  r   r   r	   r
   r4  r4  ä  sX   „ ñó,.ò2$ò>ð ñó ðð ×Ññ&ó ð&òò=ó/ó
-r	   r4  c                 ó  — | d   |d   z
  |z  }|d   | d   z
  |z  }||z  ||z  z   }|dkD  r3t        j                  |«      }t        j                  ||z  ||z  dg«      S t        j                  ||t        j                  d|z
  «      g«      S )z7Return unit sphere coordinates from window coordinates.r   r7   r#   r5   )r8   r�   r   r   )r   rC  rD  r¬   r­   ry   s         r
   rO  rO  S  s–   € à
�‰(�V˜A‘YÑ
 &Ñ	(€BØ
�‰)�e˜A‘hÑ
 &Ñ	(€BØ
ˆ2‰��2‘‰€AØˆ3‚wä�I‰I�a‹LˆÜ�{‰{˜B˜q™D " Q¡$¨Ð,Ó-Ð-ä�{‰{˜B ¤D§I¡I¨c°A©gÓ$6Ð7Ó8Ð8r	   c                 ó¬  — t        j                  | t         j                  d¬«      }t        j                  |t         j                  d¬«      }||t        j                  ||«      z  z  }t	        |«      }|t
        kD  r%|d   dk  rt        j                  ||«       ||z  }|S |d   dk(  rt        j                  g d¢«      S t        |d    |d   dg«      S )	z*Return sphere point perpendicular to axis.Tr!   r6   r5   r#   )r#   r5   r5   r7   r   )r   r   r'   r   rS   re   r€   r   )r   r�   Úvr’   ry   s        r
   rQ  rQ  `  s´   € ä�‰�E¤§¡°TÔ:€AÜ�‰�D¤§¡°DÔ9€AØˆŒU�Y‰Y�q˜!‹_Ñ	Ñ€AÜ�A‹€AØŒ4‚xØˆQ‰4�#Š:Ü�N‰N˜1˜aÔ Ø	ˆQ‰ˆØˆØˆ�tˆs‚{Ü�{‰{š?Ó+Ð+Ü˜˜1™˜˜q ™t SÐ)Ó*Ð*r	   c                 ó¼   — t        j                  | t         j                  d¬«      } d}d}|D ],  }t        j                  t	        | |«      | «      }||kD  sŒ)|}|}Œ. |S )z+Return axis, which arc is nearest to point.Fr!   Nrc   )r   r   r'   r   rQ  )r   rÑ   ÚnearestÚmxr�   rl   s         r
   rP  rP  p  s\   € ä�K‰K˜¤U§]¡]¸Ô?€EØ€GØ	€BÛˆÜ�I‰IÔ/°°tÓ<¸eÓDˆØˆr‹6ØˆGØ‰Bð	 ð
 €Nr	   g      @)r7   r6   r   r7   )r   r   r   r   Úsxyx)r   r   r7   r   Úsxzy)r   r7   r   r   Úsxzx)r   r7   r7   r   Úsyzx)r7   r   r   r   Úsyzy)r7   r   r7   r   Úsyxz)r7   r7   r   r   Úsyxy)r7   r7   r7   r   Úszxy)r6   r   r   r   Úszxz)r6   r   r7   r   Úszyx)r6   r7   r   r   Úszyz)r6   r7   r7   r   Úrzyx)r   r   r   r7   Úrxyx)r   r   r7   r7   Úryzx)r   r7   r   r7   Úrxzx)r   r7   r7   r7   Úrxzy)r7   r   r   r7   )r7   r   r7   r7   )r7   r7   r   r7   )r7   r7   r7   r7   )r6   r   r   r7   )r6   r   r7   r7   )r6   r7   r   r7   )r6   r7   r7   r7   )ÚryzyÚrzxyÚryxyÚryxzÚrzxzÚrxyzÚrzyzc              #   ó*   K  — | ]  \  }}||f–— Œ y ­wrZ  r   )Ú.0r×   rc  s      r
   Ú	<genexpr>r€  Ž  s   è ø€ Ð:Ñ&9™d˜a �A�q”6Ñ&9ùs   ‚c                 ó¸  — t        j                  | t         j                  d¬«      } |€| j                  dk(  r)t	        j
                  t        j                  | | «      «      S | | z  } t        j                  t        j                  | |¬«      «      }t        j
                  ||«       |S | | z  } t        j                  | ||¬«       t        j
                  ||«       y)a©  Return length, i.e. Euclidean norm, of ndarray along axis.

    >>> v = numpy.random.random(3)
    >>> n = vector_norm(v)
    >>> numpy.allclose(n, numpy.linalg.norm(v))
    True
    >>> v = numpy.random.rand(6, 5, 3)
    >>> n = vector_norm(v, axis=-1)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=2)))
    True
    >>> n = vector_norm(v, axis=1)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=1)))
    True
    >>> v = numpy.random.rand(5, 4, 3)
    >>> n = numpy.empty((5, 3))
    >>> vector_norm(v, axis=1, out=n)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=1)))
    True
    >>> vector_norm([])
    0.0
    >>> vector_norm([1])
    1.0

    Tr!   Nr7   rœ   )r�   Úout)	r   r   r'   Úndimr8   r�   r   Ú
atleast_1dr§   )Údatar�   r‚  s      r
   rS   rS   ‘  s¢   € ô2 �;‰;�t¤5§=¡=°tÔ<€DØ
€{Ø�9‰9˜Š>Ü—9‘9œUŸY™Y t¨TÓ2Ó3Ð3Ø�‰ˆÜ×ÑœuŸy™y¨°DÔ9Ó:ˆÜ�
‰
�3˜ÔØˆ
à�‰ˆÜ�	‰	�$˜T sÕ+Ü�
‰
�3˜Õr	   c                 óÔ  — |€ct        j                  | t         j                  d¬«      } | j                  dk(  rN| t	        j
                  t        j                  | | «      «      z  } | S || urt        j                  | d¬«      |dd |} t        j                  t        j                  | | z  |«      «      }t        j
                  ||«       |�t        j                  ||«      }| |z  } |€| S y)aì  Return ndarray normalized by length, i.e. Euclidean norm, along axis.

    >>> v0 = numpy.random.random(3)
    >>> v1 = unit_vector(v0)
    >>> numpy.allclose(v1, v0 / numpy.linalg.norm(v0))
    True
    >>> v0 = numpy.random.rand(5, 4, 3)
    >>> v1 = unit_vector(v0, axis=-1)
    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=2)), 2)
    >>> numpy.allclose(v1, v2)
    True
    >>> v1 = unit_vector(v0, axis=1)
    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=1)), 1)
    >>> numpy.allclose(v1, v2)
    True
    >>> v1 = numpy.empty((5, 4, 3))
    >>> unit_vector(v0, axis=1, out=v1)
    >>> numpy.allclose(v1, v2)
    True
    >>> list(unit_vector([]))
    []
    >>> list(unit_vector([1]))
    [1.0]

    NTr!   r7   Fr   )
r   r   r'   rƒ  r8   r�   r   r„  r§   Úexpand_dims)r…  r�   r‚  Úlengths       r
   r   r   ¸  sÊ   € ð4 €{Ü�{‰{˜4¤u§}¡}¸4Ô@ˆØ�9‰9˜Š>Ø”D—I‘IœeŸi™i¨¨dÓ3Ó4Ñ4ˆDØˆKà�d‰?Ü—[‘[ ¨EÔ2ˆC‘ˆFØˆÜ×ÑœeŸi™i¨¨T©	°4Ó8Ó9€FÜ	‡J�Jˆv�vÔØÐÜ×"Ñ" 6¨4Ó0ˆØˆF�N€DØ
€{Øˆð r	   c                 ó@   — t         j                  j                  | «      S )a  Return array of random doubles in the half-open interval [0.0, 1.0).

    >>> v = random_vector(10000)
    >>> numpy.all(v >= 0) and numpy.all(v < 1)
    True
    >>> v0 = random_vector(10)
    >>> v1 = random_vector(10)
    >>> numpy.any(v0 == v1)
    False

    )r   r*  )Úsizes    r
   Úrandom_vectorr‹  ä  s   € ô �<‰<×Ñ˜tÓ$Ð$r	   c                 ó2   — t        j                  | ||¬«      S )aH  Return vector perpendicular to vectors.

    >>> v = vector_product([2, 0, 0], [0, 3, 0])
    >>> numpy.allclose(v, [0, 0, 6])
    True
    >>> v0 = [[2, 0, 0, 2], [0, 2, 0, 2], [0, 0, 2, 2]]
    >>> v1 = [[3], [0], [0]]
    >>> v = vector_product(v0, v1)
    >>> numpy.allclose(v, [[0, 0, 0, 0], [0, 0, 6, 6], [0, -6, 0, -6]])
    True
    >>> v0 = [[2, 0, 0], [2, 0, 0], [0, 2, 0], [2, 0, 0]]
    >>> v1 = [[0, 3, 0], [0, 0, 3], [0, 0, 3], [3, 3, 3]]
    >>> v = vector_product(v0, v1, axis=1)
    >>> numpy.allclose(v, [[0, 0, 6], [0, -6, 0], [6, 0, 0], [0, -6, 6]])
    True

    rœ   )r   rt   )r¬   r­   r�   s      r
   Úvector_productr�  ó  s   € ô$ �;‰;�r˜2 DÔ)Ð)r	   c                 ój  — t        j                  | t         j                  d¬«      } t        j                  |t         j                  d¬«      }t        j                  | |z  |¬«      }|t	        | |¬«      t	        ||¬«      z  z  }t        j
                  |r|«      S t        j                  |«      «      S )a  Return angle between vectors.

    If directed is False, the input vectors are interpreted as undirected axes,
    i.e. the maximum angle is pi/2.

    >>> a = angle_between_vectors([1, -2, 3], [-1, 2, -3])
    >>> numpy.allclose(a, math.pi)
    True
    >>> a = angle_between_vectors([1, -2, 3], [-1, 2, -3], directed=False)
    >>> numpy.allclose(a, 0)
    True
    >>> v0 = [[2, 0, 0, 2], [0, 2, 0, 2], [0, 0, 2, 2]]
    >>> v1 = [[3], [0], [0]]
    >>> a = angle_between_vectors(v0, v1)
    >>> numpy.allclose(a, [0, 1.5708, 1.5708, 0.95532])
    True
    >>> v0 = [[2, 0, 0], [2, 0, 0], [0, 2, 0], [2, 0, 0]]
    >>> v1 = [[0, 3, 0], [0, 0, 3], [0, 0, 3], [3, 3, 3]]
    >>> a = angle_between_vectors(v0, v1, axis=1)
    >>> numpy.allclose(a, [1.5708, 1.5708, 1.5708, 0.95532])
    True

    Fr!   rœ   )r   r   r'   r§   rS   ÚarccosÚfabs)r¬   r­   Údirectedr�   r   s        r
   Úangle_between_vectorsr’    s‚   € ô0 
�‰�RœuŸ}™}°5Ô	9€BÜ	�‰�RœuŸ}™}°5Ô	9€BÜ
�)‰)�B˜‘G $Ô
'€CØŒ;�r Ô%¬°B¸TÔ(BÑBÑB€CÜ�<‰<™x˜Ó=Ð=¬U¯Z©Z¸«_Ó=Ð=r	   c                 ó@   — t         j                  j                  | «      S )aw  Return inverse of square transformation matrix.

    >>> M0 = random_rotation_matrix()
    >>> M1 = inverse_matrix(M0.T)
    >>> numpy.allclose(M1, numpy.linalg.inv(M0.T))
    True
    >>> for size in range(1, 7):
    ...     M0 = numpy.random.rand(size, size)
    ...     M1 = inverse_matrix(M0)
    ...     if not numpy.allclose(M1, numpy.linalg.inv(M0)): print(size)

    )r   r(   r   r   s    r
   Úinverse_matrixr”  '  s   € ô �<‰<×Ñ˜FÓ#Ð#r	   c                  ój   — t        j                  d«      }| D ]  }t        j                  ||«      }Œ |S )a  Return concatenation of series of transformation matrices.

    >>> M = numpy.random.rand(16).reshape((4, 4)) - 0.5
    >>> numpy.allclose(M, concatenate_matrices(M))
    True
    >>> numpy.allclose(numpy.dot(M, M.T), concatenate_matrices(M, M.T))
    True

    r   )r   r   r   )Úmatricesr   r2   s      r
   Úconcatenate_matricesr—  7  s0   € ô 	�‰�qÓ€AÛˆÜ�I‰I�a˜‹O‰ð à€Hr	   c                 óæ   — t        j                  | t         j                  d¬«      } | | d   z  } t        j                  |t         j                  d¬«      }||d   z  }t        j                  | |«      S )zÙReturn True if two matrices perform same transformation.

    >>> is_same_transform(numpy.identity(4), numpy.identity(4))
    True
    >>> is_same_transform(numpy.identity(4), random_rotation_matrix())
    False

    Tr!   r[   )r   r   r'   Úallclose)Úmatrix0Úmatrix1s     r
   Úis_same_transformrœ  G  s]   € ô �k‰k˜'¬¯©¸TÔB€GØˆw�t‰}Ñ€GÜ�k‰k˜'¬¯©¸TÔB€GØˆw�t‰}Ñ€GÜ�>‰>˜' 7Ó+Ð+r	   c                 óŽ  — ddl }ddlm} 	 |s	 || «      }n |d| z   |¬«      }t        |«      D ]m  }|r|j	                  |«      rŒ|r>|t        «       v rt        «       |   t        «       ||z   <   n|r|j                  d|z   «       t        ||«      t        «       |<   Œo y# t        $ r |r|j                  d| z  «       Y yY yw xY w)	zôTry import all public attributes from module into global namespace.

    Existing attributes with name clashes are renamed with prefix.
    Attributes starting with underscore are ignored by default.

    Return True on successful import.

    r   N)Úimport_moduleÚ.)Úpackagezno Python implementation of Tzfailed to import module %s)	ÚwarningsÚ	importlibrž  ÚdirÚ
startswithÚglobalsÚwarnÚgetattrÚImportError)	Únamer   r¦  ÚprefixÚignorer¡  rž  ÚmoduleÚattrs	            r
   Ú_import_moduler®  W  sÇ   € ó Ý'ðÙÙ" 4Ó(‰Fá" 3¨¡:°wÔ?ˆFô
 ˜–KˆDÙ˜$Ÿ/™/¨&Ô1ØÙØœ7›9Ñ$Ü/6«y¸©”G“I˜f t™mÒ,ÙØ—M‘MÐ"@À4Ñ"GÔHÜ% f¨dÓ3ŒG‹I�dŠOð  ð øô ò ?ÙØ�M‰MÐ6¸Ñ=Ö>ñ ð?ús   ŒB  Â CÃCÚ__main__é   )ÚsuppressÚ	precisionrZ  )NN)NNF)F)NNNNN)TTT)FT)r‰   )r   T)r   )Tr   )NTÚ_py_r–   )Dr^  r8   r   Ú__version__Ú__docformat__Ú__all__r   r   r   r   r3   r@   rK   rO   rV   r_   ra   rm   rp   rz   r‡   r�   rš   rÅ   rÇ   rŠ   ré   rì   rð   ró   r«   r  r  r  r  r  r  r(  r0  r2  Úobjectr4  rO  rQ  rP  Úfinfor  Úepsre   rÍ   rÉ   ÚdictÚitemsrÌ   rS   r   r‹  r�  r’  r”  r—  rœ  r®  r[  Údoctestr*  Úset_printoptionsÚtestmodr   r	   r
   Ú<module>r¿     s­  ðñBaóF ã à€Ø%€Ø
€òò
ò	9òò4ó:)òX%#óPòD&%ðR 04Ø/4ó;ó|F8óR1òhò>*+òZR8ðj CGØ#ó2òj<ó6jóZ0Aóf<ó~7ótBó6òrò Oó6NòbNò òò òHó&óR=ó86ôl-ˆfô l-ò^
9ò+ò 
ð €u‡{�{�5Ó×Ñ Ñ#€ò €
ðFØ
ˆLðFØ  ,ðFØ06¸ðFà
ˆLðFà  ,ðFà06¸ðFð ˆLðFð ! ,ðFð 17¸ðFð ˆLð	Fð ! ,ð	Fð 17¸ð	Fð
 ˆLðFð
 ! ,ðFð
 17¸ðFð ˆLðFð ! ,ðFð 9EØ ,¸Ø ,¸òF€ñ Ñ: k×&7Ñ&7Ô&9Ó:Ó:€ó$óN)òX%ó*ó*>ò>$ò ò ,ó ðD ˆzÒÛÛØ€E×Ñ D°AÕ6Ø€G‡O�OÕð	 r	   