
    'jtg                    R   U d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlZd dlmZ d dlmZ d dlZd dlZd dlZd dlmZ d dlmZ g dZdDdZeee                  Zded<   eeedf                  Zded<   eeef         Z ded<   ej!        ej"                 Z!ded<   dEdZ# ed          dFd            Z$ G d d          Z% G d dej&                  Z'dGdHd#Z(dId%Z)dJd)Z*dKd,Z+ G d- d.e'          Z,dLd/Z-dJd0Z.dMd2Z/dNdOd5Z0dNdPd7Z1dQd:Z2 G d; d<e'          Z3dRd>Z4dSdCZ5dS )T    )annotations)IterableTupleListSequenceAnycastOptional)	TypeAliasN)	lru_cache)repeat)	USE_C_EXT)MatrixSolvernumpy_vector_solvernumpy_matrix_solverNumpySolvertridiagonal_vector_solvertridiagonal_matrix_solverdetect_banded_matrixcompact_banded_matrixBandedMatrixLUbanded_matrixquadratic_equationcubic_equationbinomial_coefficientreturnIterable[list]c               '  D   K   t          |  D ]}t          |          V  d S N)ziplist)argses     K/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/math/linalg.pyzip_to_listr&   ,   s4      $Z  1gg     r   
MatrixData.FrozenMatrixDataShapeNDArrayc                R    t          | t                    r| j        } d | D             S )Nc                &    g | ]}d  |D             S )c                ,    g | ]}t          |          S  float.0vs     r%   
<listcomp>z0copy_float_matrix.<locals>.<listcomp>.<listcomp>:   s    ###!U1XX###r'   r/   r3   rows     r%   r5   z%copy_float_matrix.<locals>.<listcomp>:   s'    111##s###111r'   )
isinstancer   matrix)As    r%   copy_float_matrixr;   7   s/    !V H11q1111r'      )maxsizekintir1   c                    || k    rt          d          S t          j        |           }t          j        |          }t          j        | |z
            }t          |||z  z            S )a	  Computes the binomial coefficient (denoted by `k choose i`).

    Please see the following website for details:
    http://mathworld.wolfram.com/BinomialCoefficient.html

    Args:
        k: size of the set of distinct elements
        i: size of the subsets

    r   )r1   math	factorial)r>   r@   k_facti_factk_i_facts        r%   r   r   =   sa     	1uuQxx.##F.##FN1q5))H8f,-...r'   c                  x   e Zd ZdZdZ	 	 	 d@dAd
ZdBdZdCdZdDdZdDdZ	e
dEd            ZedFd            ZedFd            ZedGd            ZdHdZdHdZdHdZdId ZdId!ZdJdKd%ZdJdKd&ZdLdKd(ZedMd)            ZdNd+ZdNd,ZdOd-ZdPd1ZdQd3ZdRd7ZdRd8Z dSd:Z!e!Z"dSd;Z#e#Z$dSd<Z%e%Z&dOd=Z'dOd>Z(dTd?Z)dS )Ur   a  Basic matrix implementation based :class:`numpy.ndarray`. Matrix data is stored in
    row major order, this means in a list of rows, where each row is a list of floats.

    Initialization:

        - Matrix(shape=(rows, cols)) ... new matrix filled with zeros
        - Matrix(matrix[, shape=(rows, cols)]) ... from copy of matrix and optional reshape
        - Matrix([[row_0], [row_1], ..., [row_n]]) ... from Iterable[Iterable[float]]
        - Matrix([a1, a2, ..., an], shape=(rows, cols)) ... from Iterable[float] and shape

    .. versionchanged:: 1.2
        Implementation based on :class:`numpy.ndarray`.

    Attributes:
        matrix: matrix data as :class:`numpy.ndarray`

    )r9   abs_tolNitemsr   shapeOptional[Shape]r9   Optional[MatrixData | NDArray]c                   d| _         t          j        dt          j                  | _        |'t          j        |t          j                  | _        d S ||t          j        |          | _        d S d S t          |t                    r<||j        }|j        	                    |          
                                | _        d S t          |          }	 t          j        d |D             t          j                  | _        d S # t          $ rN |Ht          j        t          |          t          j                  	                    |          | _        Y d S Y d S w xY w)N-q=r/   dtypec                ,    g | ]}t          |          S r/   r"   r6   s     r%   r5   z#Matrix.__init__.<locals>.<listcomp>   s    'C'C'CcS		'C'C'Cr'   )rH   nparrayfloat64r9   zerosr8   r   rJ   reshapecopyr"   	TypeError)selfrI   rJ   r9   s       r%   __init__zMatrix.__init__i   sN    $!x"*===(6<<<DKF=  huoov&& 
	Y},..u55::<<DKKKKKEY h'C'CU'C'C'C2:VVV Y Y Y$"$(4;;bj"I"I"I"Q"QRW"X"XDKKKK %$$Ys   /D	 	AE! E!r   r+   c                4    t          j        | j                  S r    )rS   ravelr9   rZ   s    r%   __iter__zMatrix.__iter__   s    x$$$r'   'Matrix'c                l    t          | j                                                  }| j        |_        |S )Nr9   )r   r9   rX   rH   rZ   ms     r%   __copy__zMatrix.__copy__   s.    $+**,,---L	r'   strc                *    t          | j                  S r    )rf   r9   r^   s    r%   __str__zMatrix.__str__   s    4;r'   c                <    dt          j        | j                   dS )NzMatrix())reprlibreprr9   r^   s    r%   __repr__zMatrix.__repr__   s    5dk225555r'   Iterable[float]r*   c                    t          t          j        t          |           t          j                                      |                    S )z[Returns a new matrix for iterable `items` in the configuration of
        `shape`.
        rO   rb   )r   rS   rT   r"   rU   rW   )rI   rJ   s     r%   rW   zMatrix.reshape   s:    
 RXd5kkDDDLLUSSTTTTr'   r?   c                &    | j         j        d         S zCount of matrix rows.r   r9   rJ   r^   s    r%   nrowszMatrix.nrows        { ##r'   c                &    | j         j        d         S )zCount of matrix columns.   rr   r^   s    r%   ncolszMatrix.ncols   rt   r'   c                    | j         j        S )z9Shape of matrix as (n, m) tuple for n rows and m columns.rr   r^   s    r%   rJ   zMatrix.shape   s     {  r'   indexlist[float]c                6    t          | j        |                   S )z&Returns row `index` as list of floats.r"   r9   rZ   ry   s     r%   r7   z
Matrix.row   s    DK&'''r'   c                >    t          | j        dd|f                   S )z(Return column `index` as list of floats.Nr|   r}   s     r%   colz
Matrix.col   s    DK5)***r'   c                P    t          | j                            |                    S )a  Returns diagonal `index` as list of floats.

        An `index` of 0 specifies the main diagonal, negative values
        specifies diagonals below the main diagonal and positive values
        specifies diagonals above the main diagonal.

        e.g. given a 4x4 matrix:

        - index 0 is [00, 11, 22, 33],
        - index -1 is [10, 21, 32] and
        - index +1 is [01, 12, 23]

        )r"   r9   diagonalr}   s     r%   diagzMatrix.diag   s"     DK((//000r'   list[list[float]]c                >    t          d | j        D                       S )zReturn a list of all rows.c              3  4   K   | ]}t          |          V  d S r    rR   )r3   rs     r%   	<genexpr>zMatrix.rows.<locals>.<genexpr>   s(      11DGG111111r'   r|   r^   s    r%   rowszMatrix.rows   s!    11T[111111r'   c                D      fdt           j                  D             S )zReturn a list of all columns.c                T    g | ]$}t                              |                    %S r/   )r"   r   )r3   r@   rZ   s     r%   r5   zMatrix.cols.<locals>.<listcomp>   s+    ===aTXXa[[!!===r'   )rangerw   r^   s   `r%   colszMatrix.cols   s'    ====5+<+<====r'         ?float | Iterable[float]Nonec                    t          |t          t          f          rt          |          g| j        z  }t	          |          }t          |          | j        k    rt          d          || j        |<   dS )z>Set row values to a fixed value or from an iterable of floats.zInvalid item countN)r8   r1   r?   rw   r"   len
ValueErrorr9   rZ   ry   rI   s      r%   set_rowzMatrix.set_row   sk    eeS\** 	05\\NTZ/EUu::##1222"Er'   c                    t          |t          t          f          rt          |          g| j        z  }t	          |          | j        dd|f<   dS )zASet column values to a fixed value or from an iterable of floats.N)r8   r1   r?   rs   r"   r9   r   s      r%   set_colzMatrix.set_col   sL    eeS\** 	05\\NTZ/E $UAAAuHr'   r   c                   t          |t          t          f          rt          t          |                    }t	          |d          }t          t          |d                    }t          t          t	          | j	        | j
                            |          D ]*\  }}	 || j        ||z   ||z   f<   # t          $ r Y  dS w xY wdS )a  Set diagonal values to a fixed value or from an iterable of floats.

        An `index` of ``0`` specifies the main diagonal, negative values
        specifies diagonals below the main diagonal and positive values
        specifies diagonals above the main diagonal.

        e.g. given a 4x4 matrix:
        index ``0`` is [00, 11, 22, 33],
        index ``-1`` is [10, 21, 32] and
        index ``+1`` is [01, 12, 23]

        r   N)r8   r1   r?   r   maxabsminr!   r   rs   rw   r9   
IndexError)rZ   ry   rI   
col_offset
row_offsetvalues         r%   set_diagzMatrix.set_diag   s     eeS\** 	)5<<((EeQ--
c%mm,,
c$*dj&A&A B BEJJ 	 	LE5FKEJ.
0BBCC   	 	s   !B44
CCc                R    t          |          }|                    dd           |S )z6Returns the identity matrix for configuration `shape`.)rJ   r   r   )r   r   )clsrJ   rd   s      r%   identityzMatrix.identity   s-     	

1cr'   Sequence[float]c                    | j         j        dk    r(t          j        |gt          j                  | _         dS t          |          | j        k    r t          j        | j         |f         | _         dS t          d          )zAppend a row to the matrix.r   rO   Invalid item count.N)	r9   sizerS   rT   rU   r   rw   r_r   rZ   rI   s     r%   
append_rowzMatrix.append_row   sk    ;q  (E7"*===DKKKZZ4:%%%U 23DKKK2333r'   c                   | j         j        dk    r1t          j        d |D             t          j                  | _         dS t          |          | j        k    r t          j        | j         |f         | _         dS t          d          )zAppend a column to the matrix.r   c                    g | ]}|gS r/   r/   )r3   items     r%   r5   z%Matrix.append_col.<locals>.<listcomp>  s    #=#=#=tTF#=#=#=r'   rO   r   N)	r9   r   rS   rT   rU   r   rs   c_r   r   s     r%   
append_colzMatrix.append_col  sx    ;q  (#=#=u#=#=#=RZPPPDKKKZZ4:%%%U 23DKKK2333r'   c                P    |                                  }d|j        j        _        |S )z@Returns a frozen matrix, all data is stored in immutable tuples.F)re   r9   flags	writeablerc   s     r%   freezezMatrix.freeze  s     MMOO#( r'   r   tuple[int, int]r1   c                6    t          | j        |                   S )zjGet value by (row, col) index tuple, fancy slicing as known from
        numpy is not supported.

        )r1   r9   )rZ   r   s     r%   __getitem__zMatrix.__getitem__  s    
 T[&'''r'   r   c                    || j         |<   dS )zjSet value by (row, col) index tuple, fancy slicing as known from
        numpy is not supported.

        Nrb   )rZ   r   r   s      r%   __setitem__zMatrix.__setitem__  s    
 "Dr'   otherobjectboolc                    t          |t                    st          d          | j        |j        k    rt          d          t	          t          j        | j        |j        k                        S )z'Returns ``True`` if matrices are equal.Matrix class required.Matrices have different shapes.)r8   r   rY   rJ   r   rS   allr9   rZ   r   s     r%   __eq__zMatrix.__eq__  sb    %(( 	64555:$$=>>>BF4;%,677888r'   c           	        t          |t                    st          d          | j        |j        k    rt          d          t	          t          j        t          j        | j        |j        | j	                                      S )zReturns ``True`` if matrices are close to equal, tolerance value for
        comparison is adjustable by the attribute :attr:`Matrix.abs_tol`.

        r   r   )atol)
r8   r   rY   rJ   r   rS   r   iscloser9   rH   r   s     r%   r   zMatrix.isclose'  sr    
 %(( 	64555:$$=>>>BF2:dk5<dlSSSTTUUUr'   Matrix | floatc                    t          |t                    r-t          t          j        | j        |j                            S t          | j        t          |          z            }|S )z[Matrix multiplication by another matrix or a float, returns a new
        matrix.

        rb   )r8   r   rS   matmulr9   r1   )rZ   r   r9   s      r%   __mul__zMatrix.__mul__2  sY    
 eV$$ 	?4;!E!EFFFF4;u#=>>>Fr'   c                    t          |t                    rt          | j        |j        z             S t          | j        t          |          z             S )zCMatrix addition by another matrix or a float, returns a new matrix.rb   r8   r   r9   r1   r   s     r%   __add__zMatrix.__add__?  sL    eV$$ 	=u|!;<<<<uU||!;<<<<r'   c                    t          |t                    rt          | j        |j        z
            S t          | j        t          |          z
            S )zXMatrix subtraction by another matrix or a float, returns a new
        matrix.

        rb   r   r   s     r%   __sub__zMatrix.__sub__H  sN    
 eV$$ 	=u|!;<<<<uU||!;<<<<r'   c                6    t          | j        j                  S )z Returns a new transposed matrix.rb   )r   r9   Tr^   s    r%   	transposezMatrix.transposeT  s    T[]++++r'   c                    | j         | j        k    rt          d          	 t          t          j                            | j                            S # t          j        j        $ r t          w xY w)z(Returns inverse of matrix as new object.z+Inverse of non-square matrix not supported.rb   )
rs   rw   rY   r   rS   linalginvr9   LinAlgErrorZeroDivisionErrorr^   s    r%   inversezMatrix.inverseX  sl    :##IJJJ	$t{!;!;<<<<y$ 	$ 	$ 	$##	$s   1A A/c                d    t          t          j                            | j                            S )ziReturns determinant of matrix, raises :class:`ZeroDivisionError`
        if matrix is singular.

        )r1   rS   r   detr9   r^   s    r%   determinantzMatrix.determinanta  s"    
 RY]]4;//000r'   )NNN)rI   r   rJ   rK   r9   rL   )r   r+   )r   r`   )r   rf   )rI   rn   rJ   r*   r   r   r   r?   )r   r*   )ry   r?   r   rz   )r   r   )r   )ry   r?   rI   r   r   r   )r   r   )rJ   r*   r   r   )rI   r   r   r   )r   r   )r   r   r   r1   )r   r   r   r1   )r   r   r   r   )r   r   r   r   )r   r1   )*__name__
__module____qualname____doc__	__slots__r[   r_   re   rh   rm   staticmethodrW   propertyrs   rw   rJ   r7   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   __imul__r   __iadd__r   __isub__r   r   r   r/   r'   r%   r   r   T   s        $ &I !%15	Y Y Y Y Y:% % % %   
       6 6 6 6 U U U \U $ $ $ X$ $ $ $ X$ ! ! ! X!( ( ( (+ + + +1 1 1 1 2 2 2 2> > > ># # # # #, , , , ,    2    [4 4 4 44 4 4 4   ( ( ( (" " " "9 9 9 9	V 	V 	V 	V	 	 	 	 H= = = = H= = = = H, , , ,$ $ $ $1 1 1 1 1 1r'   r   c                  R    e Zd Zej        d
d            Zej        dd            Zd	S )r   BMatrixData | NDArrayr   r   c                    d S r    r/   rZ   r   s     r%   solve_matrixzSolver.solve_matrixj      r'   rn   rz   c                    d S r    r/   r   s     r%   solve_vectorzSolver.solve_vectorn  r   r'   Nr   r   r   r   r   rn   r   rz   )r   r   r   abcabstractmethodr   r   r/   r'   r%   r   r   i  sZ            	     r'   r   rN   abcr   c                   t          |           |k     rt          |          |k     r| fS | |z  fS 	 t          j        |dz  d| z  |z  z
            }n# t          $ r t	                      cY S w xY w| |z   d| z  z  | |z
  d| z  z  fS )z~Returns the solution for the quadratic equation ``a*x^2 + b*x + c = 0``.

    Returns 0-2 solutions as a tuple of floats.
                 @)r   rB   sqrtr   tuple)r   r   r   rH   discriminants        r%   r   r   s  s    
 1vvq66GB5LQyyAA	!122   wwR,37+\0AcAg/NOOs    A A/.A/dc                   t          |           dk     r0	 t          |||          S # t          $ r t                      cY S w xY w|| z  }|| z  }|| z  }||z  }|dz  }d|z  |z
  dz  }	d|z  |z  d|z  z
  d||z  z  z
  dz  }
|	|	z  |	z  }||
|
z  z   }|dk    rt	          j        |          }d}t	          j        d	|
|z             t	          j        t          |
|z             |          z  }t	          j        d	|
|z
            t	          j        t          |
|z
            |          z  }||z   }||z
  r| |z   fS |dz  }| |z   | |z
  | |z
  fS t	          j        |
t	          j        |           z            }t	          j        |	           dz  }|t	          j	        |dz            z  |z
  |t	          j	        |dt          j
        z  z   dz            z  |z
  |t	          j	        |d
t          j
        z  z   dz            z  |z
  fS )zReturns the solution for the cubic equation ``a*x^3 + b*x^2 + c*x + d = 0``.

    Returns 0-3 solutions as a tuple of floats.
    rN   g      @g      "@g      ;@r   g      K@        gUUUUUU?r   g      @)r   r   ArithmeticErrorr   rB   r   copysignpowacoscospi)r   r   r   r   r:   r   CAAA3QRQQQDsqrtDexpSr   STST_2thsqrtQ2s                        r%   r   r     sF   
 1vv~~	%aA... 	 	 	77NNN		AA	AA	AA	
QB	
SB	q2A	q1tax	#a.	0D8A
a%!)Cq1uACxx	!M#q5y))DHSU^^S,I,IIM#q5y))DHSU^^S,I,IIUq5 	C"H;8Dbd
d
  
1ty#&	'	'BYr]]S F"s(###b(2dg-4555:2dg-4555: s   & A Ar:   r   r   c                    t          j        | t           j                  }t          j        |t           j                  }t          t           j                            ||                    S )am  Solves the linear equation system given by a nxn Matrix A . x = B by the
    numpy.linalg.solve() function.

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

    Raises:
        numpy.linalg.LinAlgError: singular matrix

    rO   rb   )rS   rT   rU   r   r   solver:   r   mat_Amat_Bs       r%   r   r     sQ     HQbj)))EHQbj)))E667777r'   rn   rz   c                   t          j        | t           j                  }t          j        d |D             t           j                  }t          t          j        t           j                            ||                              S )at  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 numpy.linalg.solve()
    function.

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

    Raises:
        numpy.linalg.LinAlgError: singular matrix

    rO   c                .    g | ]}t          |          gS r/   r0   r2   s     r%   r5   z'numpy_vector_solver.<locals>.<listcomp>  s     ,,,QuQxxj,,,r'   )rS   rT   rU   r"   r]   r   r  r  s       r%   r   r     sg     HQbj)))EH,,!,,,BJ???E6677888r'   c                  *    e Zd ZdZddZdd	ZddZdS )r   z5Replaces in v1.2 the :class:`LUDecomposition` solver.r:   r   r   r   c                P    t          j        |t           j                  | _        d S )NrO   )rS   rT   rU   r  )rZ   r:   s     r%   r[   zNumpySolver.__init__  s    Xarz222


r'   r   r   c                    t          j        |t           j                  }t          t           j                            | j        |                    S )aN  
        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]]

        Raises:
            numpy.linalg.LinAlgError: singular matrix

        rO   rb   )rS   rT   rU   r   r   r  r  rZ   r   r  s      r%   r   zNumpySolver.solve_matrix  s=     "*---RY__TZ??@@@@r'   rn   rz   c                    t          j        d |D             t           j                  }t          t          j        t           j                            | j        |                              S )a  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]

        Raises:
            numpy.linalg.LinAlgError: singular matrix

        c                .    g | ]}t          |          gS r/   r0   r2   s     r%   r5   z,NumpySolver.solve_vector.<locals>.<listcomp>  s     000588*000r'   rO   )rS   rT   rU   r"   r]   r   r  r  r  s      r%   r   zNumpySolver.solve_vector  sS     00a000
CCCBHRY__TZ??@@AAAr'   N)r:   r   r   r   r   r   )r   r   r   r   r[   r   r   r/   r'   r%   r   r     s`        ??3 3 3 3A A A A B B B B B Br'   r   c                `    d | D             \  }}}t          |||t          |                    S )a%  Solves the linear equation system given by a tri-diagonal nxn Matrix
    A . x = B, right-hand side quantities as vector B. Matrix A is diagonal
    matrix defined by 3 diagonals [-1 (a), 0 (b), +1 (c)].

    Note: a0 is not used but has to be present, cn-1 is also not used and must
    not be present.

    If an :class:`ZeroDivisionError` exception occurs, the equation system can
    possibly be solved by :code:`BandedMatrixLU(A, 1, 1).solve_vector(B)`

    Args:
        A: diagonal matrix [[a0..an-1], [b0..bn-1], [c0..cn-1]] ::

            [[b0, c0, 0, 0, ...],
            [a1, b1, c1, 0, ...],
            [0, a2, b2, c2, ...],
            ... ]

        B: iterable of floats [[b1, b1, ..., bn]

    Returns:
        list of floats

    Raises:
        ZeroDivisionError: singular matrix

    c                ,    g | ]}t          |          S r/   rR   r2   s     r%   r5   z-tridiagonal_vector_solver.<locals>.<listcomp>      """1tAww"""r'   )_solve_tridiagonal_matrixr"   )r:   r   r   r   r   s        r%   r   r     s7    8 #""""GAq!$Q1d1gg666r'   c                   d | D             \  t          |t                    st          d |D                       }nt          t          |          }|j        t	                    k    rt          d          t          fd|                                D                                                       S )a  Solves the linear equation system given by a tri-diagonal nxn Matrix
    A . x = B, right-hand side quantities as nxm Matrix B. Matrix A is diagonal
    matrix defined by 3 diagonals [-1 (a), 0 (b), +1 (c)].

    Note: a0 is not used but has to be present, cn-1 is also not used and must
    not be present.

    If an :class:`ZeroDivisionError` exception occurs, the equation system
    can possibly be solved by :code:`BandedMatrixLU(A, 1, 1).solve_vector(B)`

    Args:
        A: diagonal matrix [[a0..an-1], [b0..bn-1], [c0..cn-1]] ::

            [[b0, c0, 0, 0, ...],
            [a1, b1, c1, 0, ...],
            [0, a2, b2, c2, ...],
            ... ]

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

    Returns:
        matrix as :class:`Matrix` object

    Raises:
        ZeroDivisionError: singular matrix

    c                ,    g | ]}t          |          S r/   rR   r2   s     r%   r5   z-tridiagonal_matrix_solver.<locals>.<listcomp>;  r"  r'   c                ,    g | ]}t          |          S r/   rR   r6   s     r%   r5   z-tridiagonal_matrix_solver.<locals>.<listcomp>=  s    !9!9!9$s))!9!9!9r'   rb   z+Row count of matrices A and B has to match.c                4    g | ]}t          |          S r/   )r#  )r3   r   r   r   r   s     r%   r5   z-tridiagonal_matrix_solver.<locals>.<listcomp>D  s(    SSSC)!Q377SSSr'   )r8   r   r	   rs   r   r   r   r   )r:   r   matrix_br   r   r   s      @@@r%   r   r     s    B #""""GAq!a   #!9!9q!9!9!9:::??~QFGGGSSSSSS8==??SSS  ikkr'   r   c                   t          |           }dg|z  }dg|z  }|d         }|d         |z  |d<   t          d|          D ]P}||dz
           |z  ||<   ||         | |         ||         z  z
  }||         | |         ||dz
           z  z
  |z  ||<   Qt          |dz
  dd          D ]'}||xx         ||dz            ||dz            z  z  cc<   (|S )aB  Solves the linear equation system given by a tri-diagonal
    Matrix(a, b, c) . x = r.

    Matrix configuration::

        [[b0, c0, 0, 0, ...],
        [a1, b1, c1, 0, ...],
        [0, a2, b2, c2, ...],
        ... ]

    Args:
        a: lower diagonal [a0 .. an-1], a0 is not used but has to be present
        b: central diagonal [b0 .. bn-1]
        c: upper diagonal [c0 .. cn-1], cn-1 is not used and must not be present
        r: right-hand side quantities

    Returns:
        vector x as list of floats

    Raises:
        ZeroDivisionError: singular matrix

    r   r   rv   r   )r   r   )	r   r   r   r   nugambetjs	            r%   r#  r#  H  s    4 VVAUQYAuqyC1CQ4#:AaD1a[[ . .1q5CAdQqTCF]"!qtaAh&#-!AEB## & &	!AE
Qq1uX%%Hr'   Ttuple[Matrix, int, int]c                T    t          | |          \  }}t          | ||          }|||fS )aY  Transform matrix A into a compact banded matrix representation.
    Returns compact representation as :class:`Matrix` object and
    lower- and upper band count m1 and m2.

    Args:
        A: input :class:`Matrix`
        check_all: check all diagonals if ``True`` or abort testing
            after first all zero diagonal if ``False``.

    )r   r   )r:   	check_allm1m2rd   s        r%   r   r   q  s4     "!Y//FBaR((Ab"9r'   r   c                J     d fd}d fd} |             |            fS )zReturns lower- and upper band count m1 and m2.

    Args:
        A: input :class:`Matrix`
        check_all: check all diagonals if ``True`` or abort testing
            after first all zero diagonal if ``False``.

    r   r?   c                     d} t          dj                  D ]+}t                              |                    r|} 's n,| S Nr   rv   )r   rw   anyr   )r4  r   r:   r2  s     r%   	detect_m2z'detect_banded_matrix.<locals>.detect_m2  sY    q!'"" 	 	A166!99~~  	r'   c                     d} t          dj                  D ],}t                              |                     r|} (s n-| S r7  )r   rs   r8  r   )r3  r   r:   r2  s     r%   	detect_m1z'detect_banded_matrix.<locals>.detect_m1  s[    q!'"" 	 	A1661"::  	r'   r   r/   )r:   r2  r9  r;  s   ``  r%   r   r     sc                 9;;		##r'   r3  r4  c                   | j         | j        k    rt          d          t                      }t	          |dd          D ]F}dg|z  }|                    |                     |                      |                    |           G|                    |                     d                     t	          d|dz             D ]E}|                     |          }|                    dg|z             |                    |           F|S )zReturns compact banded matrix representation as :class:`Matrix` object.

    Args:
        A: matrix to transform
        m1: lower band count, excluding main matrix diagonal
        m2: upper band count, excluding main matrix diagonal

    zSquare matrix required.r   r*  r   rv   )rs   rw   rY   r   r   extendr   r   )r:   r3  r4  rd   r   r   s         r%   r   r     s     	w!'1222A2q"  eai

1661"::	SLL1b1f  ffQii

C519	SHr'   c                  B    e Zd ZdZddZedd	            ZddZddZdS )r   z9Represents a LU decomposition of a compact banded matrix.r:   r   r3  r?   r4  c                    t           }t          rddlm} t	          |          | _        t	          |          | _         ||j        | j        | j                  \  | _        | _	        | _
        d S )Nr   )lu_decompose)_lu_decomposer   ezdxf.acc.np_supportr@  r?   r3  r4  r9   upperlowerry   )rZ   r:   r3  r4  r@  s        r%   r[   zBandedMatrixLU.__init__  se    $ 	:9999992ww2ww-9\!(DGTW-U-U*
DJ


r'   r   c                &    | j         j        d         S rq   )rC  rJ   r^   s    r%   rs   zBandedMatrixLU.nrows  s     z""r'   r   rn   rz   c           
     $   t           }t          rddlm} t	          j        |t          j                  }t          |          | j        k    rt          d          t           ||| j        | j        | j        | j        | j                            S )a  Solves the linear equation system given by the banded 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   )solve_vector_banded_matrixrO   ;Item count of vector B has to be equal to matrix row count.)_solve_vector_banded_matrixr   rB  rG  rS   rT   rU   r   rs   r   r"   rC  rD  ry   r3  r4  )rZ   r   rG  xs       r%   r   zBandedMatrixLU.solve_vector  s     &A" 	HGGGGGGXarz222q66TZM   &&4:tz4:tw 
 
 	
r'   r   c                     t          |          }|j         j        k    rt          d          t           fd|                                D                                                       S )aM  
        Solves the linear equation system given by the banded 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

        rb   z,Row count of self and matrix B has to match.c                :    g | ]}                     |          S r/   )r   )r3   r   rZ   s     r%   r5   z/BandedMatrixLU.solve_matrix.<locals>.<listcomp>  s'    FFFsD%%c**FFFr'   )r   rs   r   r   r   )rZ   r   r(  s   `  r%   r   zBandedMatrixLU.solve_matrix  sr     ###>TZ''KLLLFFFFhmmooFFF
 
 

)++	r'   N)r:   r   r3  r?   r4  r?   r   r   r   )	r   r   r   r   r[   r   rs   r   r   r/   r'   r%   r   r     sz        CCV V V V # # # X#
 
 
 
8     r'   r    tuple[NDArray, NDArray, NDArray]c                   t          j        | t           j                  }|j        d         }t          j        ||ft           j                  }t          j        |ft           j                  }||z   dz   }|}t          |          D ][}	t          ||	z
  |          D ]}
||	         |
         ||	         |
|z
  <   |dz  }t          ||z
  dz
  |          D ]}
d||	         |
<   \|}t          |          D ]P}||         d         }|}	||k     r|dz  }t          |dz   |          D ]>}
t          ||
         d                   t          |          k    r||
         d         }|
}	?|	dz   ||<   |	|k    rAt          |          D ]1}
||	         |
         ||         |
         c||         |
<   ||	         |
<   2t          |dz   |          D ]}}	||	         d         ||         d         z  }|||         |	|z
  dz
  <   t          d|          D ].}
||	         |
         |||         |
         z  z
  ||	         |
dz
  <   /d||	         |dz
  <   ~R|||fS )NrO   r   rv   r   )rS   rT   rU   rJ   rV   int64r   r   )r:   r3  r4  rC  r+  rD  ry   mmlr@   r/  r>   dums                r%   rA  rA     s   Xarz222E [^AXq"gRZ888EXqd"(333E2gkBA2YY  rAvr"" 	* 	*A#AhqkE!HQUOO	QrAvz2&& 	 	AE!HQKK	 	A1XX # #Ahqkq55FAq1ua 	 	A58A;#c((**Ahqkq5a662YY D D+08A;a(aU1Xa[[q1ua 	# 	#A(1+a+C"%E!HQUQY1b\\ B B"'(1+eAhqk0A"AaQ"E!HR!V	# %r'   rJ  rC  rD  ry   c                h   |j         d         }| j         d         |k    rt          d          |}|}||z   dz   }	|}
t          |          D ]v}||         dz
  }||k    r| |         | |         c| |<   | |<   |
|k     r|
dz  }
t          |dz   |
          D ]-}| |xx         ||         ||z
  dz
           | |         z  z  cc<   .wd}
t          |dz
  dd          D ]Y}| |         }t          d|
          D ]}|||         |         | ||z            z  z  } |||         d         z  | |<   |
|	k     r|
dz  }
Z| S )zSolves the linear equation system given by the banded 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   rH  rv   r*  )rJ   r   r   )rJ  rC  rD  ry   r3  r4  r+  alaurP  rQ  r>   r/  r@   rR  s                  r%   rI  rI  *  s   $ [^AwqzQVWWWBB2gkBA1XX , ,!HqL661qtJAaD!A$q55FAq1ua 	, 	,AaDDDBqE!a%!)$qt++DDDD	, 	
A1q5"b!!  dq! 	' 	'A2a58aAh&&CCRU1X~!r66FAHr'   )r   r   )r   r(   )r>   r?   r@   r?   r   r1   )rN   )r   r1   r   r1   r   r1   r   r   )
r   r1   r   r1   r   r1   r   r1   r   r   )r:   r   r   r   r   r   )r:   r   r   rn   r   rz   )r:   r(   r   rn   r   rz   )
r   rz   r   rz   r   rz   r   rz   r   rz   )T)r:   r   r   r0  )r:   r   r   r   )r:   r   r3  r?   r4  r?   r   r   )r:   r+   r3  r?   r4  r?   r   rM  )rJ  r+   rC  r+   rD  r+   ry   r+   r3  r?   r4  r?   r   r+   )6
__future__r   typingr   r   r   r   r   r	   r
   typing_extensionsr   r   	functoolsr   	itertoolsr   rB   rk   numpyrS   numpy.typingnpt	ezdxf.accr   __all__r&   r1   r(   __annotations__r)   r?   r*   r+   rU   r;   r   r   ABCr   r   r   r   r   r   r   r   r#  r   r   r   r   rA  rI  r/   r'   r%   <module>rb     s   # " " " " " "                  ( ' ' ' ' ' 



                                $   
 T%[)
 ) ) ) )#E%*$56  6 6 6 6c? " " " "[, , , , ,2 2 2 2 3/ / / /,R1 R1 R1 R1 R1 R1 R1 R1j    SW   P P P P P"+ + + +\8 8 8 8"9 9 9 9$"B "B "B "B "B& "B "B "BJ7 7 7 7@+ + + +\& & & &R     $ $ $ $ $>   8A A A A AV A A AH' ' ' 'T, , , , , ,r'   