Ë
    £èh†3  ã                  ó®   — d dl mZ d dlmZmZ d dlmZ d dlZddlm	Z	m
Z
mZmZ g d¢Zdd„Zdd	„Zdd
„Zdd„Zdd„Z	 	 	 	 	 	 dd„Zdd„Z G d„ de«      Zy)é    )Úannotations)ÚIterableÚcast)ÚrepeatNé   )ÚMatrixÚ
MatrixDataÚNDArrayÚSolver)Úgauss_vector_solverÚgauss_matrix_solverÚgauss_jordan_solverÚgauss_jordan_inverseÚLUDecompositionc           
     ó¢   — t        | t        «      r| j                  } | D ��cg c]  }|D �cg c]  }t        |«      ‘Œ c}‘Œ c}}S c c}w c c}}w ©N)Ú
isinstancer   ÚmatrixÚfloat)ÚAÚrowÚvs      ú1lib/python3.12/site-packages/ezdxf/math/legacy.pyÚcopy_float_matrixr      sC   € Ü�!”VÔØ�H‰HˆÙ/0Ô1©q¨™sÓ#™s˜!ŒU�1�X˜sÓ#¨qÒ1Ð1ùÒ#ùÓ1s   ¢	A«A½AÁAc                óÞ   — t        | «      } t        |«      }t        | «      }t        | d   «      |k7  rt        d«      ‚t        |«      |k7  rt        d«      ‚t	        | |«       t        | |«      S )aš  Solves the linear equation system given by a nxn Matrix A . x = B,
    right-hand side quantities as vector B with n elements by the
    `Gauss-Elimination`_ algorithm, which is faster than the `Gauss-Jordan`_
    algorithm. The speed improvement is more significant for solving multiple
    right-hand side quantities as matrix at once.

    Reference implementation for error checking.

    Args:
        A: matrix [[a11, a12, ..., a1n], [a21, a22, ..., a2n], [a21, a22, ..., a2n],
            ... [an1, an2, ..., ann]]
        B: vector [b1, b2, ..., bn]

    Returns:
        vector as list of floats

    Raises:
        ZeroDivisionError: singular matrix

    r   ú"A square nxn matrix A is required.z=Item count of vector B has to be equal to matrix A row count.)r   ÚlistÚlenÚ
ValueErrorÚ_build_upper_triangleÚ_backsubstitution)r   ÚBÚnums      r   r   r      sp   € ô, 	˜!Ó€AÜˆQ‹€AÜ
ˆa‹&€CÜ
ˆ1ˆQ‰4ƒy�CÒÜÐ=Ó>Ð>Ü
ˆ1ƒv�‚}ÜØKó
ð 	
ô
 ˜!˜QÔÜ˜Q Ó"Ð"ó    c                óV  — t        | «      }t        |«      }t        |«      }t        |d   «      |k7  rt        d«      ‚t        |«      |k7  rt        d«      ‚t        ||«       t	        |¬«      j                  «       }t	        «       }|D ]  }|j                  t        ||«      «       Œ |S )aV  Solves the linear equation system given by a nxn Matrix A . x = B,
    right-hand side quantities as nxm Matrix B by the `Gauss-Elimination`_
    algorithm, which is faster than the `Gauss-Jordan`_ algorithm.

    Reference implementation for error checking.

    Args:
        A: matrix [[a11, a12, ..., a1n], [a21, a22, ..., a2n], [a21, a22, ..., a2n],
            ... [an1, an2, ..., ann]]
        B: matrix [[b11, b12, ..., b1m], [b21, b22, ..., b2m], ... [bn1, bn2, ..., bnm]]

    Returns:
        matrix as :class:`Matrix` object

    Raises:
        ZeroDivisionError: singular matrix

    r   r   ú+Row count of matrices A and B has to match.©r   )r   r   r   r    r   ÚcolsÚ
append_colr!   )r   r"   Úmatrix_aÚmatrix_br#   ÚcolumnsÚresultÚcols           r   r   r   ?   s£   € ô( ! Ó#€HÜ  Ó#€HÜ
ˆh‹-€CÜ
ˆ8�A‰;Ó˜3ÒÜÐ=Ó>Ð>Ü
ˆ8ƒ}˜ÒÜÐFÓGÐGô ˜( HÔ-ä˜HÔ%×*Ñ*Ó,€GÜ‹X€FÛˆØ×ÑÔ+¨H°cÓ:Õ;ð ð €Mr$   c           	     óŠ  — t        | «      }	 t        |d   «      }t        d|«      D �]  }t        | |   |   «      }|}t        |dz   |«      D ]  }t        | |   |   «      }||kD  sŒ|}|}Œ | |   | |   c| |<   | |<   ||   ||   c||<   ||<   t        |dz   |«      D ]Ž  }| |   |    | |   |   z  }	t        ||«      D ])  }
||
k(  r	d| |   |
<   Œ| |   |
xx   |	| |   |
   z  z  cc<   Œ+ |dk(  r||xx   |	||   z  z  cc<   Œft        |«      D ]  }
||   |
xx   |	||   |
   z  z  cc<   Œ Œ� �Œ
 y# t        $ r d}Y �Œ&w xY w)z£Build upper triangle for backsubstitution. Modifies A and B inplace!

    Args:
         A: row major matrix
         B: vector of floats or row major matrix

    r   r   N)r   Ú	TypeErrorÚrangeÚabs)r   r"   r#   Úb_col_countÚiÚmax_elementÚmax_rowr   ÚvalueÚcr.   s              r   r    r    f   s‘  € ô ˆa‹&€CðÜ˜!˜A™$“iˆô �1�c�]ˆä˜!˜A™$˜q™'“lˆØˆÜ˜˜Q™ Ö$ˆCÜ˜˜#™˜q™	“NˆEØ�{Ó"Ø#�Ø‘ð	 %ð ˜Q™4  7¡Ðˆˆ'‰
�A�a‘DØ˜Q™4  7¡Ðˆˆ'‰
�A�a‘Dô ˜˜Q™ Ö$ˆCØ�3‘˜‘�
˜Q˜q™T !™WÑ$ˆAÜ˜Q –}�Ø˜’8Ø"#�A�c‘F˜3’Kà�c‘F˜3“K 1 q¨¡t¨C¡y¡=Ñ0”Kð	 %ð
 ˜aÒØ�#“˜!˜a ™d™(Ñ"”ä  Ö-�CØ�c‘F˜3“K 1 q¨¡t¨C¡y¡=Ñ0”Kñ .ò %ñ øô ò Ø‹ðús   �D3 Ä3EÅEc                óÐ   — t        | «      }dg|z  }t        |dz
  dd«      D ]A  }||   | |   |   z  ||<   t        |dz
  dd«      D ]  }||xx   | |   |   ||   z  z  cc<   Œ ŒC |S )z›Solve equation A . x = B for an upper triangular matrix A by
    backsubstitution.

    Args:
        A: row major matrix
        B: vector of floats

    ç        r   éÿÿÿÿ)r   r1   )r   r"   r#   Úxr4   r   s         r   r!   r!   ‘   s…   € ô ˆa‹&€CØ	ˆ�‰€AÜ�3˜‘7˜B Ö#ˆØ�‰t�a˜‘d˜1‘g‰~ˆˆ!‰Ü˜˜Q™  BÖ'ˆCØˆc‹F�a˜‘f˜Q‘i ! A¡$Ñ&Ñ&ŒFñ (ð $ð €Hr$   c                óŠ  — t        | «      }t        |«      }t        |«      }t        |d   «      }t        |d   «      |k7  rt        d«      ‚t        |«      |k7  rt        d«      ‚d}d}dg|z  }dg|z  }	dg|z  }
t        |«      D �]\  }d}t        |«      D ]N  }|
|   dk7  sŒt        |«      D ]5  }|
|   dk(  sŒt	        ||   |   «      |k\  sŒ!t	        ||   |   «      }|}|}Œ7 ŒP |
|xx   dz  cc<   ||k7  r"||   ||   c||<   ||<   ||   ||   c||<   ||<   ||	|<   |||<   d||   |   z  }d||   |<   ||   D �cg c]  }||z  ‘Œ	 c}||<   ||   D �cg c]  }||z  ‘Œ	 c}||<   t        |«      D ]j  }||k(  rŒ	||   |   }d||   |<   t        |«      D ]  }||   |xx   ||   |   |z  z  cc<   Œ t        |«      D ]  }||   |xx   ||   |   |z  z  cc<   Œ Œl �Œ_ t        |dz
  dd«      D ]*  }|	|   }||   }||k7  sŒ|D ]  }||   ||   c||<   ||<   Œ Œ, t        |¬«      t        |¬«      fS c c}w c c}w )	a§  Solves the linear equation system given by a nxn Matrix A . x = B,
    right-hand side quantities as nxm Matrix B by the `Gauss-Jordan`_ algorithm,
    which is the slowest of all, but it is very reliable. Returns a copy of the
    modified input matrix `A` and the result matrix `x`.

    Internally used for matrix inverse calculation.

    Args:
        A: matrix [[a11, a12, ..., a1n], [a21, a22, ..., a2n], [a21, a22, ..., a2n],
            ... [an1, an2, ..., ann]]
        B: matrix [[b11, b12, ..., b1m], [b21, b22, ..., b2m], ... [bn1, bn2, ..., bnm]]

    Returns:
        2-tuple of :class:`Matrix` objects

    Raises:
        ZeroDivisionError: singular matrix

    r   r   r&   r:   r   ç      ð?r;   r'   )r   r   r   r1   r2   r   )r   r"   r*   r+   ÚnÚmÚicolÚirowÚcol_indicesÚrow_indicesÚipivr4   ÚbigÚjÚkÚpivinvr   r   Údumr.   Ú_rows                        r   r   r   £   sÒ  € ô. ! Ó#€HÜ  Ó#€HäˆH‹€AÜˆH�Q‰KÓ€Aä
ˆ8�A‰;Ó˜1ÒÜÐ=Ó>Ð>Ü
ˆ8ƒ}˜ÒÜÐFÓGÐGà€DØ€DØ�#˜‘'€KØ�#˜‘'€KØˆ3�‰7€Dä�1�XˆØˆÜ�q–ˆAØ�A‰w˜!‹|Ü˜qž�AØ˜A‘w !“|Ü˜x¨™{¨1™~Ó.°#Ó5Ü"% h¨q¡k°!¡nÓ"5˜CØ#$˜DØ#$™Dñ "ð ð 	ˆT‹
�a‰‹
Ø�4Š<Ø-5°d©^¸XÀd¹^Ð*ˆH�T‰N˜H T™NØ-5°d©^¸XÀd¹^Ð*ˆH�T‰N˜H T™Nàˆ�A‰Øˆ�A‰à�x ‘~ dÑ+Ñ+ˆØ"ˆ�‰�tÑØ.6°tªnÓ=©n¨˜!˜f›*¨nÑ=ˆ�‰Ø.6°tªnÓ=©n¨˜!˜f›*¨nÑ=ˆ�‰Ü˜–8ˆCØ�dŠ{ØØ˜3‘- Ñ%ˆCØ"%ˆH�S‰M˜$ÑÜ˜Q–x�Ø˜‘˜cÓ" h¨t¡n°SÑ&9¸CÑ&?Ñ?Ô"ð  ä˜Q–x�Ø˜‘˜cÓ" h¨t¡n°SÑ&9¸CÑ&?Ñ?Ô"ñ  ò ð/ ôB �1�q‘5˜"˜bÖ!ˆØ˜1‰~ˆØ˜1‰~ˆØ�4‹<Û �Ø)-¨d©°T¸$±ZÐ&��T‘
˜D šJñ !ð	 "ô ˜Ô"¤F°(Ô$;Ð;Ð;ùò% >ùÚ=s   Å H;ÅI c           	     ó®   — t        | t        «      r| j                  }nt        | «      }t	        |«      }t        |t        t        dg|«      «      «      d   S )a-  Returns the inverse of matrix `A` as :class:`Matrix` object.

    .. hint::

        For small matrices (n<10) is this function faster than
        LUDecomposition(m).inverse() and as fast even if the decomposition is
        already done.

    Raises:
        ZeroDivisionError: singular matrix

    r:   r   )r   r   r   r   r   r   r   )r   r*   Únrowss      r   r   r   õ   sI   € ô �!”VÔØ—8‘8‰ä˜“7ˆÜ�‹M€EÜ˜x¬¬f°c°U¸EÓ.BÓ)CÓDÀQÑGÐGr$   c                  ó^   — e Zd ZdZdZdd„Zdd„Zdd„Zedd„«       Z	dd„Z
dd„Zdd	„Zdd
„Zy)r   a™  Represents a `LU decomposition`_ matrix of A, raise :class:`ZeroDivisionError`
    for a singular matrix.

    This algorithm is a little bit faster than the `Gauss-Elimination`_
    algorithm using CPython and much faster when using pypy.

    The :attr:`LUDecomposition.matrix` attribute gives access to the matrix data
    as list of rows like in the :class:`Matrix` class, and the :attr:`LUDecomposition.index`
    attribute gives access to the swapped row indices.

    Args:
        A: matrix [[a11, a12, ..., a1n], [a21, a22, ..., a2n], [a21, a22, ..., a2n],
            ... [an1, an2, ..., ann]]

    raises:
        ZeroDivisionError: singular matrix

    )r   ÚindexÚ_detc           	     óŽ  — t        |«      }t        |«      }d}g }|D �cg c]  }dt        d„ |D «       «      z  ‘Œ }}t        |«      D ]á  }d}	|}
t        ||«      D ]#  }||   t	        ||   |   «      z  }||	kD  sŒ |}	|}
Œ% ||
k7  r9t        |«      D ]   }||
   |   }||   |   ||
   |<   |||   |<   Œ" | }||   ||
<   |j                  |
«       t        |dz   |«      D ]H  }||   |   ||   |   z  }|||   |<   t        |dz   |«      D ]  }||   |xx   |||   |   z  z  cc<   Œ ŒJ Œã || _        || _        || _        y c c}w )Nr>   c              3  ó2   K  — | ]  }t        |«      –— Œ y ­wr   )r2   )Ú.0r   s     r   Ú	<genexpr>z+LUDecomposition.__init__.<locals>.<genexpr>'  s   è ø€ Ð)>¹#°Q¬#¨a¯&¹#ùs   ‚r:   r   )	r   r   Úmaxr1   r2   ÚappendrO   r   rP   )Úselfr   Úlur?   ÚdetrO   r   ÚscalingrH   rF   Úimaxr4   Útempr.   rG   s                  r   Ú__init__zLUDecomposition.__init__   sŒ  € Ü*¨1Ó-ˆÜ�R“ˆØˆØˆñ KMÓMÉ"À3 ¤cÑ)>¹#Ó)>Ó&>Ó >È"ˆÐMä�q–ˆAØˆCØˆDÜ˜1˜a–[�Ø% a™j¬3¨r°!©u°Q©x«=Ñ8�Ø˜#“:Ø�CØ‘Dð	 !ð �DŠyÜ  ž8�CØ˜d™8 C™=�DØ$& q¡E¨#¡J�B�t‘H˜S‘MØ!%�B�q‘E˜#’Jð $ð
 �d�Ø '¨¡
�˜‘à�L‰L˜ÔÜ˜1˜q™5 !–_�Ø˜!‘u˜Q‘x " Q¡%¨¡(Ñ*�Ø��1‘�a‘Ü˜q 1™u až�AØ�q‘E˜!“H  r¨!¡u¨Q¡x¡Ñ/”Hñ )ñ %ð' ð2 !&ˆŒ
Ø"$ˆŒØˆ�	ùò;  Ns   ŸEc                ó,   — t        | j                  «      S r   )Ústrr   ©rW   s    r   Ú__str__zLUDecomposition.__str__F  s   € Ü�4—;‘;ÓÐr$   c                ó^   — | j                   › dt        j                  | j                  «      › �S )NÚ )Ú	__class__ÚreprlibÚreprr   r`   s    r   Ú__repr__zLUDecomposition.__repr__I  s&   € Ø—.‘.Ð! ¤7§<¡<°·±Ó#<Ð"=Ð>Ð>r$   c                ó,   — t        | j                  «      S )z Count of matrix rows (and cols).)r   r   r`   s    r   rM   zLUDecomposition.nrowsL  s   € ô �4—;‘;ÓÐr$   c                ó  — |D �cg c]  }t        |«      ‘Œ }}| j                  }| j                  }| j                  }d}t	        |«      |k7  rt        d«      ‚t        |«      D ]N  }||   }	||	   }
||   ||	<   |dk7  r&t        |dz
  |«      D ]  }|
||   |   ||   z  z  }
Œ n
|
dk7  r|dz   }|
||<   ŒP t        |dz
  dd«      D ]:  }||   }
t        |dz   |«      D ]  }|
||   |   ||   z  z  }
Œ |
||   |   z  ||<   Œ< |S c c}w )züSolves the linear equation system given by the nxn Matrix A . x = B,
        right-hand side quantities as vector B with n elements.

        Args:
            B: vector [b1, b2, ..., bn]

        Returns:
            vector as list of floats

        r   z;Item count of vector B has to be equal to matrix row count.r   r:   r;   )r   r   rO   rM   r   r   r1   )rW   r"   r   ÚXrX   rO   r?   Úiir4   ÚipÚsum_rG   r   r.   s                 r   Úsolve_vectorzLUDecomposition.solve_vectorQ  sI  € ñ -.Ó.©A qœ% �(¨AˆÐ.ØŸ™ˆØŸ:™:ˆØ—‘ˆØˆäˆq‹6�QŠ;ÜØMóð ô �q–ˆAØ˜A‘hˆBØ˜B™%ˆDØ�a‘DˆAˆb‰EØ�QŠwÜ˜r A™v qÖ)�AØ˜B˜q™E !™H q¨¡t™OÑ+‘Dñ *à˜’Ø˜‘U�ØˆAˆaŠDð ô ˜˜Q™  BÖ'ˆCØ�S‘6ˆDÜ˜S 1™W aÖ(�Ø˜˜3™ ™ q¨¡vÑ-Ñ-‘ð )à˜B˜s™G C™LÑ(ˆAˆcŠFð	 (ð
 ˆùò7 /s   …Dc                óp  — t        |t        «      s$t        |D �cg c]  }t        |«      ‘Œ c}¬«      }nt        t        |«      }|j                  | j                  k7  rt        d«      ‚t        |j                  «       D �cg c]  }| j                  |«      ‘Œ c}¬«      j                  «       S c c}w c c}w )a=  Solves the linear equation system given by the nxn Matrix A . x = B,
        right-hand side quantities as nxm Matrix B.

        Args:
            B: matrix [[b11, b12, ..., b1m], [b21, b22, ..., b2m],
                ... [bn1, bn2, ..., bnm]]

        Returns:
            matrix as :class:`Matrix` object

        r'   z,Row count of self and matrix B has to match.)	r   r   r   r   rM   r   r(   rn   Ú	transpose)rW   r"   r   r+   r.   s        r   Úsolve_matrixzLUDecomposition.solve_matrixy  s”   € ô ˜!œVÔ$Ü¹1Ó%=¹1°C¤d¨3¥i¸1Ñ%=Ô>‰HäœF A“ˆHà�>‰>˜TŸZ™ZÒ'ÜÐKÓLÐLäØ6>·m±m´oÓF±o¨s�D×%Ñ% cÕ*°oÑFô
ç
‰)‹+ð	ùò &>ùò Gs   šB.Â B3c                óŒ   — | j                  t        j                  | j                  | j                  f¬«      j                  «      S )z‚Returns the inverse of matrix as :class:`Matrix` object,
        raise :class:`ZeroDivisionError` for a singular matrix.

        )Úshape)rq   r   ÚidentityrM   r   r`   s    r   ÚinversezLUDecomposition.inverse‘  s1   € ð
 × Ñ ¤§¡¸¿
¹
ÀDÇJÁJÐ7OÔ!P×!WÑ!WÓXÐXr$   c                ó€   — | j                   }| j                  }t        | j                  «      D ]  }|||   |   z  }Œ |S )zmReturns the determinant of matrix, raises :class:`ZeroDivisionError`
        if matrix is singular.

        )rP   r   r1   rM   )rW   rY   rX   r4   s       r   ÚdeterminantzLUDecomposition.determinant˜  s?   € ð
 —Y‘YˆØŸ™ˆÜ�t—z‘zÖ"ˆAØ�2�a‘5˜‘8‰O‰Cð #àˆ
r$   N)r   úMatrixData | NDArray)Úreturnr_   )ry   Úint)r"   úIterable[float]ry   úlist[float])r"   rx   ry   r   )ry   r   )ry   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r]   ra   rg   ÚpropertyrM   rn   rq   ru   rw   © r$   r   r   r   
  sH   „ ñð& ,€Ió$óL ó?ð ò ó ð ó&óPó0Yô	r$   r   )ry   r	   )r   rx   r"   r{   ry   r|   )r   rx   r"   rx   ry   r   )r   r	   r"   r   ry   ÚNone)r   r	   r"   r|   ry   r|   )r   rx   r"   rx   ry   ztuple[Matrix, Matrix])r   r	   ry   r   )Ú
__future__r   Útypingr   r   Ú	itertoolsr   re   Úlinalgr   r	   r
   r   Ú__all__r   r   r   r    r!   r   r   r   rƒ   r$   r   Ú<module>rŠ      sr   ðõ #ß !Ý Û ç 7Ó 7ò€ó2ó"#óJ$óN(1óVð$O<ØðO<Ø 4ðO<àóO<ódHô*W�fõ Wr$   