
    Og1                        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      V/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/math/legacy.pycopy_float_matrixr      s;    !VHH/01s#!U1X#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
1Q4yC=>>
1v}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
8A;3=>>
8}FGG (H-H%**,GXF <+Hc:;< 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)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   s    a&C!A$i 1c] 1!A$q'lQ$ 	C#q	NE{"#		 Q47'
AaDQ47'
AaD Q$ 	1C3
QqT!W$AQ} 18"#AcF3KcF3K1qtCy=0K	1
 a#!ad(" - 1CcF3K1qtCy=0K1	11  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 )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!      s     a&C	A37B# 'tad1g~!QB' 	'CcFafQi!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HQKA
8A;1=>>
8}FGGDD#'K#'K37D1X @q 	%AAw!|q %AAw!|x{1~.#5"%hqk!n"5C#$D#$D%	% 	T
a
4<-5d^Xd^*HTNHTN-5d^Xd^*HTNHTNAAx~d++"t.6tn=!f*=.6tn=!f*=8 	@Cd{3-%C"%HSM$Qx @c"htnS&9C&??"@Qx @c"htnS&9C&??"@	@/@B 1q5"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887MExfcUE.B)CDQ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M3c)>#)>&> >MMq 	0ACD1a[ %aj3r!uQx=8#:CD	 Dy 8 &Cd8C=D$&qE#JBtHSM!%BqE#J&
 d '
LL1q5!_ 0!uQx"Q%(*1aq1ua 0AqE!Hr!uQx/H00'	02 !&
"$	;  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 )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   )rW   r"   r   XrX   rO   r?   iir4   ipsum_rG   r   r.   s                 r   solve_vectorzLUDecomposition.solve_vectorQ  s\    -..q%(..::q6Q;M  q 		AAhBB%DaDAbEQwrAvq) ,ABqE!HqtO+D,UAaD		 QB' 	)CS6DS1Wa( .3qv--.BsGCL(AcF		)
 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%=Cd3i%=>HFAH>>TZZ'KLL6>mmoFsD%%c*F

)+	 &> Gs   B. B3c                    | j                  t        j                  | j                  | j                  f      j                        S )zReturns 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    
   

DJJ7O!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  sD    
 YYtzz" 	A2a58OC	
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(1V$O<O< 4O<O<dH*Wf Wr$   