
    h1                        S SK Jr  S SKJrJr  S SKJr  S SKrSSKJ	r	J
r
JrJr  / SQrSS jrSS	 jrSS
 jrSS jrSS jr      SS jrSS jr " S S\5      rg)    )annotations)Iterablecast)repeatN   )Matrix
MatrixDataNDArraySolver)gauss_vector_solvergauss_matrix_solvergauss_jordan_solvergauss_jordan_inverseLUDecompositionc           
         [        U [        5      (       a  U R                  n U  VVs/ s H  o Vs/ s H  n[        U5      PM     snPM      snn$ s  snf s  snnf N)
isinstancer   matrixfloat)Arowvs      C/var/www/html/env/lib/python3.13/site-packages/ezdxf/math/legacy.pycopy_float_matrixr      sF    !VHH/01qs#s!U1Xs#q11#1s   	AAAAc                    [        U 5      n [        U5      n[        U 5      n[        U S   5      U:w  a  [        S5      e[        U5      U:w  a  [        S5      e[	        X5        [        X5      $ )ab  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      sl    , 	!AQA
a&C
1Q4yC=>>
1v}K
 	

 !Q""    c                T   [        U 5      n[        U5      n[        U5      n[        US   5      U:w  a  [        S5      e[        U5      U:w  a  [        S5      e[        X#5        [	        US9R                  5       n[	        5       nU H  nUR                  [        X'5      5        M     U$ )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-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
8A;3=>>
8}FGG (-H%**,GXF+H:;  Mr$   c           	        [        U 5      n [        US   5      n[        SU5       GH   n[        X   U   5      nUn[        US-   U5       H  n[        X   U   5      nX:  d  M  UnUnM      X   X   sX'   X'   X   X   sX'   X'   [        US-   U5       H  nX   U   * X   U   -  n	[        XB5       H(  n
XJ:X  a	  SX   U
'   M  X   U
==   XU   U
   -  -  ss'   M*     US:X  a  X==   XU   -  -  ss'   Mc  [        U5       H  n
X   U
==   XU   U
   -  -  ss'   M     M     GM     g! [         a    Sn GN!f = f)zBuild 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   se    a&C!A$i 1c]!$q'lQ$Cq	NE"#	 % 4
AD4
AD Q$C
QT!W$AQ}8"#AF3KF3K1tCy=0K	 %
 a!d(" -CF3K1tCy=0K . %   s   D- -D=<D=c                    [        U 5      nS/U-  n[        US-
  SS5       H>  nX   X   U   -  X4'   [        US-
  SS5       H  nX==   X   U   X4   -  -  ss'   M     M@     U$ )zSolve 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!      sy     a&C	A37B#tad1g~QB'CFafQi!$&&F ( $ Hr$   c                   [        U 5      n[        U5      n[        U5      n[        US   5      n[        US   5      U:w  a  [        S5      e[        U5      U:w  a  [        S5      eSnSnS/U-  nS/U-  n	S/U-  n
[        U5       GH^  nSn[        U5       HS  nX   S:w  d  M  [        U5       H7  nX   S:X  d  M  [	        X-   U   5      U:  d  M#  [	        X-   U   5      nUnUnM9     MU     X==   S-  ss'   Xv:w  a  X&   X'   sX''   X&'   X6   X7   sX7'   X6'   XyU'   XhU'   SX&   U   -  nSX&   U'   X&    Vs/ s H  nUU-  PM
     snX&'   X6    Vs/ s H  nUU-  PM
     snX6'   [        U5       Ho  nUU:X  a  M  UU   U   nSUU   U'   [        U5       H  nUU   U==   X&   U   U-  -  ss'   M     [        U5       H  nUU   U==   X6   U   U-  -  ss'   M     Mq     GMa     [        US-
  SS5       H,  nX   nX   nXv:w  d  M  U H  nUU   UU   sUU'   UU'   M     M.     [        US9[        US94$ s  snf s  snf )	as  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HQKA
8A;1=>>
8}FGGDD#'K#'K37D1XqAw!|qAw!|x{1~.#5"%hk!n"5C#$D#$D "  	
a
<-5^X^*HNHN-5^X^*HNHNAAx~d++"t.6n=n!f*n=.6n=n!f*n=8Cd{3-%C"%HSM$Qxc"hnS&9C&??"  Qxc"hnS&9C&??"   / B 1q5"b!~~< )-dT$Z&T
DJ !	 " "F($;;;% >=s   :H=Ic           	         [        U [        5      (       a  U R                  nO[        U 5      n[	        U5      n[        U[        [        S/U5      5      5      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      sL     !V887MExfcUE.B)CDQGGr$   c                  v    \ rS rSrSrSrSS jrSS jrSS jr\	SS j5       r
SS jrSS	 jrSS
 jrSS jrSrg)r   i
  ai  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           	        [        U5      n[        U5      nSn/ nU Vs/ s H  nS[        S U 5       5      -  PM     nn[        U5       H  nSn	Un
[        X5       H#  nX{   [	        X+   U   5      -  nX:  d  M  Un	Un
M%     X:w  a5  [        U5       H  nX*   U   nX(   U   X*   U'   XU   U'   M     U* nXx   Xz'   UR                  U
5        [        US-   U5       HF  nX+   U   X(   U   -  nXU   U'   [        US-   U5       H  nX+   U==   XU   U   -  -  ss'   M     MH     M     XPl        X l        X@l        g s  snf )Nr>   c              3  8   #    U  H  n[        U5      v   M     g 7fr   )r2   ).0r   s     r   	<genexpr>+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__LUDecomposition.__init__   sf   *1-R KMM"3c)>#)>&> >"MqACD1[%j3ruQx=8:CD	 ! y 8C8C=D$&E#JBHSM!%qE#J $
 d '
LL1q5!_uQx"%(*1aq1uaAE!H!uQx/H ) %' 2 !&
"$	;  Ns   D?c                ,    [        U R                  5      $ r   )strr   rX   s    r   __str__LUDecomposition.__str__F  s    4;;r$   c                `    U R                    S[        R                  " U R                  5       3$ )N )	__class__reprlibreprr   rb   s    r   __repr__LUDecomposition.__repr__I  s&    ..!7<<#<"=>>r$   c                ,    [        U R                  5      $ )z Count of matrix rows (and cols).)r   r   rb   s    r   rM   LUDecomposition.nrowsL  s     4;;r$   c                   U Vs/ s H  n[        U5      PM     nnU R                  nU R                  nU R                  nSn[	        U5      U:w  a  [        S5      e[        U5       HL  nXX   n	X9   n
X8   X9'   US:w  a&  [        US-
  U5       H  nXU   U   X;   -  -  n
M     OU
S:w  a  US-   nXU'   MN     [        US-
  SS5       H8  nX<   n
[        US-   U5       H  nXU   U   X=   -  -  n
M     XU   U   -  X<'   M:     U$ s  snf )zSolves 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   )rX   r"   r   XrY   rO   r?   iir4   ipsum_rG   r   r.   s                 r   solve_vectorLUDecomposition.solve_vectorQ  s1    -..Aq%(A.::q6Q;M  qAhB%DDAEQwrAvq)AqE!HqtO+D *UaD  QB'C6DS1Wa(3qv-- )sGCL(AF	 (
 7 /s   Dc                z   [        U[        5      (       d$  [        U Vs/ s H  n[        U5      PM     snS9nO[        [        U5      nUR                  U R                  :w  a  [        S5      e[        UR                  5        Vs/ s H  o@R                  U5      PM     snS9R                  5       $ s  snf s  snf )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(   rs   	transpose)rX   r"   r   r+   r.   s        r   solve_matrixLUDecomposition.solve_matrixy  s     !V$$1%=1Cd3i1%=>HFAH>>TZZ'KLL6>mmoFos%%c*oF

)+	 &> Gs   B3B8c                    U R                  [        R                  " U R                  U R                  4S9R                  5      $ )zrReturns the inverse of matrix as :class:`Matrix` object,
raise :class:`ZeroDivisionError` for a singular matrix.

)shape)rw   r   identityrM   r   rb   s    r   inverseLUDecomposition.inverse  s1    
   

DJJ7O!P!W!WXXr$   c                    U R                   nU R                  n[        U R                  5       H  nXU   U   -  nM     U$ )z]Returns the determinant of matrix, raises :class:`ZeroDivisionError`
if matrix is singular.

)rP   r   r1   rM   )rX   rZ   rY   r4   s       r   determinantLUDecomposition.determinant  s=    
 YYtzz"Aa58OC #
r$   )rP   rO   r   N)r   MatrixData | NDArray)returnra   )r   int)r"   Iterable[float]r   list[float])r"   r   r   r   )r   r   )r   r   )__name__
__module____qualname____firstlineno____doc__	__slots__r^   rc   rj   propertyrM   rs   rw   r|   r   __static_attributes__ r$   r   r   r   
  sH    & ,I$L ?    &P0Y	r$   r   )r   r	   )r   r   r"   r   r   r   )r   r   r"   r   r   r   )r   r	   r"   r   r   None)r   r	   r"   r   r   r   )r   r   r"   r   r   ztuple[Matrix, Matrix])r   r	   r   r   )
__future__r   typingr   r   	itertoolsr   rh   linalgr   r	   r
   r   __all__r   r   r   r    r!   r   r   r   r   r$   r   <module>r      sr    # !   7 72"#J$N(1V$O<O< 4O<O<dH*Wf Wr$   