
    'j1                        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dS )#    )annotations)Iterablecast)repeatN   )Matrix
MatrixDataNDArraySolver)gauss_vector_solvergauss_matrix_solvergauss_jordan_solvergauss_jordan_inverseLUDecompositionreturnr	   c                R    t          | t                    r| j        } d | D             S )Nc                &    g | ]}d  |D             S )c                ,    g | ]}t          |          S  float.0vs     K/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/math/legacy.py
<listcomp>z0copy_float_matrix.<locals>.<listcomp>.<listcomp>   s    ###!U1XX###    r   r   rows     r   r   z%copy_float_matrix.<locals>.<listcomp>   s'    111##s###111r   )
isinstancer   matrix)As    r   copy_float_matrixr#      s/    !V H11q1111r   r"   MatrixData | NDArrayBIterable[float]list[float]c                0   t          |           } t          |          }t          |           }t          | d                   |k    rt          d          t          |          |k    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"   r%   nums      r   r   r      s    , 	!AQA
a&&C
1Q4yyC=>>>
1vv}}K
 
 	

 !QQ"""r   r   c                   t          |           }t          |          }t          |          }t          |d                   |k    rt          d          t          |          |k    rt          d          t          ||           t	          |                                          }t	                      }|D ]%}|                    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G (H---H%%%**,,GXXF < <+Hc::;;;;Mr   r*   Nonec           	     V   t          |           }	 t          |d                   }n# t          $ r d}Y nw xY wt          d|          D ]^}t          | |         |                   }|}t          |dz   |          D ]'}t          | |         |                   }||k    r|}|}(| |         | |         c| |<   | |<   ||         ||         c||<   ||<   t          |dz   |          D ]}| |         |          | |         |         z  }	t          ||          D ]9}
||
k    rd| |         |
<   | |         |
xx         |	| |         |
         z  z  cc<   :|dk    r||xx         |	||         z  z  cc<   t          |          D ]'}
||         |
xx         |	||         |
         z  z  cc<   (`dS )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cr9   s              r   r-   r-   f   s
    a&&C!A$ii    1c]] 1 1!A$q'llQ$$ 	 	C#q	NNE{""# Q47'
AaDQ47'
AaD Q$$ 	1 	1C3
QqT!W$AQ}} 1 188"#AcF3KKcF3KKK1qtCy=0KKKKa#!ad(" -- 1 1CcF3KKK1qtCy=0KKKK1	11 1s   ' 66c                
   t          |           }dg|z  }t          |dz
  dd          D ]X}||         | |         |         z  ||<   t          |dz
  dd          D ]'}||xx         | |         |         ||         z  z  cc<   (Y|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+   r=   )r"   r%   r/   xr@   r   s         r   r.   r.      s     a&&C	A37B## ' 'tad1g~!QB'' 	' 	'CcFFFafQi!A$&&FFFF	'Hr   tuple[Matrix, Matrix]c                   t          |           }t          |          }t          |          }t          |d                   }t          |d                   |k    rt          d          t          |          |k    rt          d          d}d}dg|z  }dg|z  }	dg|z  }
t          |          D ]}d}t          |          D ]j}|
|         dk    r\t          |          D ]L}|
|         dk    r>t	          ||         |                   |k    rt	          ||         |                   }|}|}Mk|
|xx         dz  cc<   ||k    r.||         ||         c||<   ||<   ||         ||         c||<   ||<   ||	|<   |||<   d||         |         z  d||         |<   fd||         D             ||<   fd||         D             ||<   t          |          D ]}||k    r	||         |         }d||         |<   t          |          D ]'}||         |xx         ||         |         |z  z  cc<   (t          |          D ]'}||         |xx         ||         |         |z  z  cc<   (t          |dz
  d	d	          D ]4}|	|         }||         }||k    r|D ]}||         ||         c||<   ||<   5t          |
          t          |
          fS )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)   r1   rF   r         ?c                    g | ]}|z  S r   r   r   r   pivinvs     r   r   z'gauss_jordan_solver.<locals>.<listcomp>       ===!f*===r   c                    g | ]}|z  S r   r   rM   s     r   r   z'gauss_jordan_solver.<locals>.<listcomp>   rO   r   rG   r2   )r#   r+   r,   r=   r>   r   )r"   r%   r5   r6   nmicolirowcol_indicesrow_indicesipivr@   bigjkr   dumr9   _rowrN   s                      @r   r   r      s}   . !##H ##HHAHQKA
8A;1=>>>
8}}FGGGDD#'K#'K37D1XX @ @q 	% 	%AAw!||q % %AAw!||x{1~..#55"%hqk!n"5"5C#$D#$DT


a


4<<-5d^Xd^*HTNHTN-5d^Xd^*HTNHTNAAx~d++"t====htn=======htn===88 	@ 	@Cd{{3-%C"%HSM$Qxx @ @c"""htnS&9C&??""""Qxx @ @c"""htnS&9C&??""""@	@ 1q5"b!! @ @1~1~4<<  @ @)-dT$Z&T
DJJ"""F($;$;$;;;r   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

    rF   r   )r    r   r!   r*   r+   r   r   )r"   r5   nrowss      r   r   r      s^     !V 877MMExfcUE.B.B)C)CDDQGGr   c                  f    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dS )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_detr"   r$   c           	        t          |          }t          |          }d}g }d |D             }t          |          D ],}d}|}	t          ||          D ]0}
||
         t          ||
         |                   z  }||k    r|}|
}	1||	k    rPt          |          D ]2}||	         |         }||         |         ||	         |<   |||         |<   3| }||         ||	<   |                    |	           t          |dz   |          D ]e}
||
         |         ||         |         z  }|||
         |<   t          |dz   |          D ]'}||
         |xx         |||         |         z  z  cc<   (f.|| _        || _        || _        d S )NrK   c                F    g | ]}d t          d |D                       z  S )rK   c              3  4   K   | ]}t          |          V  d S N)r>   r   s     r   	<genexpr>z6LUDecomposition.__init__.<locals>.<listcomp>.<genexpr>'  s(      )>)>Q#a&&)>)>)>)>)>)>r   )maxr   s     r   r   z,LUDecomposition.__init__.<locals>.<listcomp>'  s4    MMM3c)>)>#)>)>)>&>&> >MMMr   rF   r   )r#   r+   r=   r>   appendr`   r!   ra   )selfr"   lurQ   detr`   scalingrZ   rX   imaxr@   tempr9   rY   s                 r   __init__zLUDecomposition.__init__   s   *1--R  NM"MMMq 	0 	0ACD1a[[  %aj3r!uQx==8#::CDDyy 88 & &Cd8C=D$&qE#JBtHSM!%BqE#JJd '
LL1q5!__ 0 0!uQx"Q%(*1aq1ua 0 0AqE!HHHr!uQx/HHHH00 !&
"$			r   r   strc                *    t          | j                  S re   )rp   r!   ri   s    r   __str__zLUDecomposition.__str__F  s    4;r   c                H    | j          dt          j        | j                   S )N )	__class__reprlibreprr!   rr   s    r   __repr__zLUDecomposition.__repr__I  s$    .>>7<#<#<>>>r   intc                *    t          | j                  S )z Count of matrix rows (and cols).)r+   r!   rr   s    r   r^   zLUDecomposition.nrowsL  s     4;r   r%   r&   r'   c                >   d |D             }| j         }| j        }| j        }d}t          |          |k    rt	          d          t          |          D ]d}||         }||         }	||         ||<   |dk    r1t          |dz
  |          D ]}
|	||         |
         ||
         z  z  }	n|	dk    r|dz   }|	||<   et          |dz
  dd          D ]N}||         }	t          |dz   |          D ]}|	||         |         ||         z  z  }	|	||         |         z  ||<   O|S )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

        c                ,    g | ]}t          |          S r   r   r   s     r   r   z0LUDecomposition.solve_vector.<locals>.<listcomp>\  s    ...q%((...r   r   z;Item count of vector B has to be equal to matrix row count.r   rF   rG   )r!   r`   r^   r+   r,   r=   )ri   r%   Xrj   r`   rQ   iir@   ipsum_rY   r   r9   s                r   solve_vectorzLUDecomposition.solve_vectorQ  sn    /.A...:q66Q;;M   q 		 		AAhBB%DaDAbEQwwrAvq)) , ,ABqE!HqtO+DD,UAaDDQB'' 	) 	)CS6DS1Wa(( . .3qv--BsGCL(AcFFr   r   c                L    t          |t                    st          d |D                       }nt          t          |          }|j         j        k    rt	          d          t           fd|                                D                                                       S )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

        c                ,    g | ]}t          |          S r   )r*   r   s     r   r   z0LUDecomposition.solve_matrix.<locals>.<listcomp>  s    %=%=%=Cd3ii%=%=%=r   r2   z,Row count of self and matrix B has to match.c                :    g | ]}                     |          S r   )r   )r   r9   ri   s     r   r   z0LUDecomposition.solve_matrix.<locals>.<listcomp>  s'    FFFsD%%c**FFFr   )r    r   r   r^   r,   r3   	transpose)ri   r%   r6   s   `  r   solve_matrixzLUDecomposition.solve_matrixy  s     !V$$ 	'%=%=1%=%=%=>>>HHFAH>TZ''KLLLFFFFhmmooFFF
 
 

)++	r   c                t    |                      t          j        | j        | j        f          j                  S )zReturns the inverse of matrix as :class:`Matrix` object,
        raise :class:`ZeroDivisionError` for a singular matrix.

        )shape)r   r   identityr^   r!   rr   s    r   inversezLUDecomposition.inverse  s1    
   
DJ7O!P!P!P!WXXXr   r   c                r    | j         }| j        }t          | j                  D ]}|||         |         z  }|S )zmReturns the determinant of matrix, raises :class:`ZeroDivisionError`
        if matrix is singular.

        )ra   r!   r=   r^   )ri   rk   rj   r@   s       r   determinantzLUDecomposition.determinant  sC    
 Ytz"" 	 	A2a58OCC
r   N)r"   r$   )r   rp   )r   rz   )r%   r&   r   r'   )r%   r$   r   r   )r   r   )r   r   )__name__
__module____qualname____doc__	__slots__ro   rs   ry   propertyr^   r   r   r   r   r   r   r   r   r   
  s         & ,I$ $ $ $L       ? ? ? ?       X & & & &P   0Y Y Y Y	 	 	 	 	 	r   r   )r   r	   )r"   r$   r%   r&   r   r'   )r"   r$   r%   r$   r   r   )r"   r	   r%   r*   r   r:   )r"   r	   r%   r'   r   r'   )r"   r$   r%   r$   r   rI   )r"   r	   r   r   )
__future__r   typingr   r   	itertoolsr   rw   linalgr   r	   r
   r   __all__r#   r   r   r-   r.   r   r   r   r   r   r   <module>r      sW   # " " " " " ! ! ! ! ! ! ! !        7 7 7 7 7 7 7 7 7 7 7 7  2 2 2 2"# "# "# "#J$ $ $ $N(1 (1 (1 (1V   $O< O< O< O<dH H H H*W W W W Wf W W W W Wr   