
    Ogtg                    b   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d(d	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jB                  ejD                     Z!d
ed<   d)dZ# ed      d*d       Z$ G d d      Z% G d dejL                        Z'd+d,dZ(d-dZ)d.dZ*d/dZ+ G d de'      Z,d0dZ-	 	 	 	 	 	 d.dZ.	 	 	 	 	 	 	 	 	 	 d1d Z/d2d3d!Z0d2d4d"Z1d5d#Z2 G d$ d%e'      Z3d6d&Z4	 	 	 	 	 	 	 	 	 	 	 	 	 	 d7d'Z5y)8    )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_coefficientc               '  @   K   t        |  D ]  }t        |        y wN)ziplist)argses     V/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/math/linalg.pyzip_to_listr$   ,   s"     $Z 1gs   r   
MatrixData.FrozenMatrixDataShapeNDArrayc           
         t        | t              r| j                  } | D cg c]  }|D cg c]  }t        |       c} c}}S c c}w c c}}w r   
isinstancer   matrixfloat)Arowvs      r#   copy_float_matrixr1   7   s;    !VHH/01s#!U1X#11#1s   	AAAA   )maxsizec                    || kD  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   )r-   math	factorial)kik_facti_factk_i_facts        r#   r   r   =   sW     	1uQx..#F..#FNN1q5)H8f,-..    c                  b   e Zd ZdZdZ	 	 	 d#	 	 	 	 	 d$dZd%dZd&dZd'dZd'dZ	e
d(d	       Zed)d
       Zed)d       Zed*d       Zd+dZd+dZd+dZd,dZd,dZd-d.dZd-d.dZd/d.dZed0d       Zd1dZd1dZd2dZd3dZd4dZd5dZd5dZ d6dZ!e!Z"d6dZ#e#Z$d6dZ%e%Z&d2d Z'd2d!Z(d7d"Z)y)8r   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`

    )r,   abs_tolNc                   d| _         t        j                  dt        j                        | _        |+t        j                  |t        j                        | _        y ||t        j
                  |      | _        y y t        |t              r=||j                  }|j                  j                  |      j                         | _        y t        |      }	 t        j                  |D cg c]  }t        |       c}t        j                        | _        y c c}w # t        $ rI |Dt        j                  t        |      t        j                        j                  |      | _        Y y Y y w xY w)N-q= dtype)r>   nparrayfloat64r,   zerosr+   r   shapereshapecopyr    	TypeError)selfitemsrH   r,   r/   s        r#   __init__zMatrix.__init__i   s    $!xx"**=((6<DK=  hhuov&},,..u5::<DKKEY hhU'CcS	'C2::V'C Y$"$((4;bjj"I"Q"QRW"XDK %Ys%   D +D=D D AE10E1c                @    t        j                  | j                        S r   )rD   ravelr,   rL   s    r#   __iter__zMatrix.__iter__   s    xx$$r<   c                p    t        | j                  j                               }| j                  |_        |S )Nr,   )r   r,   rJ   r>   rL   ms     r#   __copy__zMatrix.__copy__   s)    $++**,-LL	r<   c                ,    t        | j                        S r   )strr,   rQ   s    r#   __str__zMatrix.__str__   s    4;;r<   c                H    dt        j                  | j                         dS )NzMatrix())reprlibreprr,   rQ   s    r#   __repr__zMatrix.__repr__   s    dkk23155r<   c                    t        t        j                  t        |       t        j                        j                  |            S )z[Returns a new matrix for iterable `items` in the configuration of
        `shape`.
        rB   rT   )r   rD   rE   r    rF   rI   )rM   rH   s     r#   rI   zMatrix.reshape   s-    
 RXXd5kDLLUSTTr<   c                4    | j                   j                  d   S zCount of matrix rows.r   r,   rH   rQ   s    r#   nrowszMatrix.nrows        {{  ##r<   c                4    | j                   j                  d   S )zCount of matrix columns.   rc   rQ   s    r#   ncolszMatrix.ncols   re   r<   c                .    | j                   j                  S )z9Shape of matrix as (n, m) tuple for n rows and m columns.rc   rQ   s    r#   rH   zMatrix.shape   s     {{   r<   c                2    t        | j                  |         S )z&Returns row `index` as list of floats.r    r,   rL   indexs     r#   r/   z
Matrix.row   s    DKK&''r<   c                :    t        | j                  dd|f         S )z(Return column `index` as list of floats.Nrk   rl   s     r#   colz
Matrix.col   s    DKK5)**r<   c                J    t        | j                  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    r,   diagonalrl   s     r#   diagzMatrix.diag   s     DKK((/00r<   c                :    t        d | j                  D              S )zReturn a list of all rows.c              3  2   K   | ]  }t        |        y wr   )r    ).0rs     r#   	<genexpr>zMatrix.rows.<locals>.<genexpr>   s     1DG1s   rk   rQ   s    r#   rowszMatrix.rows   s    1T[[111r<   c                    t        | j                        D cg c]  }t        | j                  |             c}S c c}w )zReturn a list of all columns.)rangerh   r    ro   )rL   r8   s     r#   colszMatrix.cols   s,    +0+<=aTXXa[!===s   !<c                    t        |t        t        f      rt        |      g| j                  z  }t	        |      }t        |      | j                  k7  rt        d      || j                  |<   y)z>Set row values to a fixed value or from an iterable of floats.zInvalid item countN)r+   r-   intrh   r    len
ValueErrorr,   rL   rm   rM   s      r#   set_rowzMatrix.set_row   sW    eeS\*5\NTZZ/EUu:#122"Er<   c                    t        |t        t        f      rt        |      g| j                  z  }t	        |      | j
                  dd|f<   y)zASet column values to a fixed value or from an iterable of floats.N)r+   r-   r}   rd   r    r,   r   s      r#   set_colzMatrix.set_col   s:    eeS\*5\NTZZ/E $UAuHr<   c                `   t        |t        t        f      rt        t        |            }t	        |d      }t        t        |d            }t        t        t	        | j                  | j                              |      D ]  \  }}	 || j                  ||z   ||z   f<    y# t        $ r Y  yw xY w)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)r+   r-   r}   r   maxabsminr   rz   rd   rh   r,   
IndexError)rL   rm   rM   
col_offset
row_offsetvalues         r#   set_diagzMatrix.set_diag   s     eeS\*5<(EeQ-
c%m,
c$**djj&A BEJ 	LE5FKEJ.
0BBC	  s   B  	B-,B-c                B    t        |      }|j                  dd       |S )z6Returns the identity matrix for configuration `shape`.)rH   r         ?)r   r   )clsrH   rV   s      r#   identityzMatrix.identity   s!     	

1cr<   c                   | j                   j                  dk(  r,t        j                  |gt        j                        | _         yt        |      | j                  k(  r%t        j                  | j                   |f   | _         yt        d      )zAppend a row to the matrix.r   rB   Invalid item count.N)	r,   sizerD   rE   rF   r~   rh   r_r   )rL   rM   s     r#   
append_rowzMatrix.append_row   sa    ;;q ((E7"**=DKZ4::%%%U 23DK233r<   c                B   | j                   j                  dk(  r:t        j                  |D cg c]  }|g c}t        j                        | _         yt        |      | j                  k(  r%t        j                  | j                   |f   | _         yt        d      c c}w )zAppend a column to the matrix.r   rB   r   N)	r,   r   rD   rE   rF   r~   rd   c_r   )rL   rM   items      r#   
append_colzMatrix.append_col  sq    ;;q ((u#=tTF#=RZZPDKZ4::%%%U 23DK233	 $>s   
Bc                \    | j                         }d|j                  j                  _        |S )z@Returns a frozen matrix, all data is stored in immutable tuples.F)rW   r,   flags	writeablerU   s     r#   freezezMatrix.freeze  s"    MMO#( r<   c                2    t        | j                  |         S )zjGet value by (row, col) index tuple, fancy slicing as known from
        numpy is not supported.

        )r-   r,   )rL   r   s     r#   __getitem__zMatrix.__getitem__  s    
 T[[&''r<   c                "    || j                   |<   y)zjSet value by (row, col) index tuple, fancy slicing as known from
        numpy is not supported.

        NrT   )rL   r   r   s      r#   __setitem__zMatrix.__setitem__  s    
 "Dr<   c                    t        |t              st        d      | j                  |j                  k7  rt        d      t	        t        j                  | j                  |j                  k(              S )z'Returns ``True`` if matrices are equal.Matrix class required.Matrices have different shapes.)r+   r   rK   rH   boolrD   allr,   rL   others     r#   __eq__zMatrix.__eq__  sU    %(455::$=>>BFF4;;%,,6788r<   c           	     $   t        |t              st        d      | j                  |j                  k7  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)
r+   r   rK   rH   r   rD   r   iscloser,   r>   r   s     r#   r   zMatrix.isclose'  sc    
 %(455::$=>>BFF2::dkk5<<dllSTUUr<   c                    t        |t              r4t        t        j                  | j                  |j                              S t        | j                  t        |      z        }|S )z[Matrix multiplication by another matrix or a float, returns a new
        matrix.

        rT   )r+   r   rD   matmulr,   r-   )rL   r   r,   s      r#   __mul__zMatrix.__mul__2  sH    
 eV$4;;!E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.rT   r*   r   s     r#   __add__zMatrix.__add__?  s<    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.

        rT   r*   r   s     r#   __sub__zMatrix.__sub__H  s>    
 eV$u||!;<<uU|!;<<r<   c                B    t        | j                  j                        S )z Returns a new transposed matrix.rT   )r   r,   TrQ   s    r#   	transposezMatrix.transposeT  s    T[[]]++r<   c                    | j                   | j                  k7  rt        d      	 t        t        j
                  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.rT   )
rd   rh   rK   r   rD   linalginvr,   LinAlgErrorZeroDivisionErrorrQ   s    r#   inversezMatrix.inverseX  s^    ::#IJJ	$t{{!;<<yy$$ 	$##	$s   2A $A=c                f    t        t        j                  j                  | j                              S )ziReturns determinant of matrix, raises :class:`ZeroDivisionError`
        if matrix is singular.

        )r-   rD   r   detr,   rQ   s    r#   determinantzMatrix.determinanta  s     
 RYY]]4;;/00r<   )NNN)rM   r   rH   zOptional[Shape]r,   zOptional[MatrixData | NDArray])returnr(   )r   z'Matrix')r   rY   )rM   Iterable[float]rH   r'   r   r   r   r}   )r   r'   )rm   r}   r   list[float])r   zlist[list[float]])r   )rm   r}   rM   zfloat | Iterable[float]r   None)r   r   )rH   r'   r   r   )rM   Sequence[float]r   r   )r   r   )r   tuple[int, int]r   r-   )r   r   r   r-   )r   objectr   r   )r   zMatrix | floatr   r   )r   r-   )*__name__
__module____qualname____doc__	__slots__rN   rR   rW   rZ   r_   staticmethodrI   propertyrd   rh   rH   r/   ro   rr   rx   r{   r   r   r   classmethodr   r   r   r   r   r   r   r   r   __imul__r   __iadd__r   __isub__r   r   r   rA   r<   r#   r   r   T   s7   $ &I !%15	YY Y /	Y:%
 6 U U $ $ $ $ ! !(+1 2>#,2  44("9	V	 H= H= H,$1r<   r   c                  X    e Zd Zej                  dd       Zej                  dd       Zy)r   c                     y r   rA   rL   Bs     r#   solve_matrixzSolver.solve_matrixj      r<   c                     y r   rA   r   s     r#   solve_vectorzSolver.solve_vectorn  r   r<   Nr   MatrixData | NDArrayr   r   r   r   r   r   )r   r   r   abcabstractmethodr   r   rA   r<   r#   r   r   i  s4      	 r<   r   c                    t        |       |k  rt        |      |k  r| fS | |z  fS 	 t        j                  |dz  d| z  |z  z
        }| |z   d| z  z  | |z
  d| z  z  fS # t        $ r t	               cY S w xY w)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   r5   sqrtr   tuple)abcr>   discriminants        r#   r   r   s  s    
 1vq6GB5LQyyyAA	!12 R,37+\0AcAg/NOO  ws   !A" "A87A8c                
   t        |       dk  r	 t        |||      S || 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 # t        $ r t               cY S w xY w)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.
    r@   g      @g      "@g      ;@r   g      K@        gUUUUUU?r   g      @)r   r   ArithmeticErrorr   r5   r   copysignpowacoscospi)r   r   r   dr.   r   CAAA3QRQQQDsqrtDexpSr   STST_2thsqrtQ2s                        r#   r   r     s"   
 1v~	%aA.. 	
AA	AA	AA	
QB	
SB	q2A	q1tax	#a.	0D8A
a%!)Cq1uACx		!MM#q5y)DHHSU^S,IIMM#q5y)DHHSU^S,IIUq5C"H;8Dbd
d
  
1tyy#&	'BYYr]S F"s(##b(2dgg-455:2dgg-455: ?  	7N	s   G, ,HHc                    t        j                  | t         j                        }t        j                  |t         j                        }t        t         j                  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

    rB   rT   )rD   rE   rF   r   r   solve)r.   r   mat_Amat_Bs       r#   r   r     sF     HHQbjj)EHHQbjj)E677r<   c                H   t        j                  | t         j                        }t        j                  |D cg c]  }t        |      g c}t         j                        }t	        t        j
                  t         j                  j                  ||                  S c c}w )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

    rB   )rD   rE   rF   r-   r    rP   r   r  )r.   r   r  r0   r  s        r#   r   r     se     HHQbjj)EHH!,QuQxj,BJJ?E6788 -s   Bc                  (    e Zd ZdZddZddZddZy)	r   z5Replaces in v1.2 the :class:`LUDecomposition` solver.c                X    t        j                  |t         j                        | _        y )NrB   )rD   rE   rF   r  )rL   r.   s     r#   rN   zNumpySolver.__init__  s    XXarzz2
r<   c                    t        j                  |t         j                        }t        t         j                  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

        rB   rT   )rD   rE   rF   r   r   r  r  )rL   r   r  s      r#   r   zNumpySolver.solve_matrix  s6     "**-RYY__TZZ?@@r<   c                   t        j                  |D cg c]  }t        |      g c}t         j                        }t	        t        j
                  t         j                  j                  | j                  |                  S c c}w )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

        rB   )	rD   rE   r-   rF   r    rP   r   r  r  )rL   r   r0   r  s       r#   r   zNumpySolver.solve_vector  sU     a058*0

CBHHRYY__TZZ?@AA 1s   BN)r.   r   r   r   r   r   )r   r   r   r   rN   r   r   rA   r<   r#   r   r     s    ?3A Br<   r   c                t    | D cg c]  }t        |       c}\  }}}t        |||t        |            S c c}w )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

    )r    _solve_tridiagonal_matrix)r.   r   r0   r   r   r   s         r#   r   r     s8    8 !""1tAw"GAq!$Q1d1g66 #s   5c                   | D cg c]  }t        |       c}\  }}}t        |t              s$t        |D cg c]  }t        |       c}      }nt        t        |      }|j                  t        |      k7  rt        d      t        |j                         D cg c]  }t        ||||       c}      j                         S c c}w c c}w c c}w )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

    rT   z+Row count of matrices A and B has to match.)
r    r+   r   r	   rd   r~   r   r{   r  r   )	r.   r   r0   r   r   r   r/   matrix_bro   s	            r#   r   r     s    B !""1tAw"GAq!a q!9$s)!9:?~~QFGGCK==?SC)!Q37Sik #!9 Ts   CCCc                J   t        |       }dg|z  }dg|z  }|d   }|d   |z  |d<   t        d|      D ];  }||dz
     |z  ||<   ||   | |   ||   z  z
  }||   | |   ||dz
     z  z
  |z  ||<   = t        |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   rg   r   )r~   rz   )	r   r   r   rv   nugambetjs	            r#   r  r  H  s    4 VAUQYAuqyC1CQ4#:AaD1a[ .1q5CAdQqTCF]"!qtaAh&#-!.
 AEB# &	!AE
Qq1uX%%&Hr<   c                D    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m2rV   s        r#   r   r   q  s.     "!Y/FBaR(Ab"9r<   c                >     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``.

    c                     d} t        dj                        D ]$  }t        j                  |            r|}  r# | S  | S Nr   rg   )rz   rh   anyrr   )r  r   r.   r  s     r#   	detect_m2z'detect_banded_matrix.<locals>.detect_m2  sH    q!''" 	A166!9~		
 	r<   c                     d} t        dj                        D ]%  }t        j                  |             r|} !r$ | S  | S r  )rz   rd   r  rr   )r  r   r.   r  s     r#   	detect_m1z'detect_banded_matrix.<locals>.detect_m1  sJ    q!''" 	A1661":		
 	r<   r   rA   )r.   r  r  r  s   ``  r#   r   r     s     ;	##r<   c                   | j                   | j                  k7  rt        d      t               }t	        |dd      D ]:  }dg|z  }|j                  | j                  |              |j                  |       < |j                  | j                  d             t	        d|dz         D ]9  }| j                  |      }|j                  dg|z         |j                  |       ; |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   rg   )rd   rh   rK   r   rz   extendrr   r   )r.   r  r  rV   r   ro   s         r#   r   r     s     	ww!''122A2q" eai

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

C519	S Hr<   c                  :    e Zd ZdZddZedd       Zd	dZd
dZy)r   z9Represents a LU decomposition of a compact banded matrix.c                    t         }t        rddlm} t	        |      | _        t	        |      | _         ||j                  | j
                  | j                        \  | _        | _	        | _
        y )Nr   )lu_decompose)_lu_decomposer   ezdxf.acc.np_supportr$  r}   r  r  r,   upperlowerrm   )rL   r.   r  r  r$  s        r#   rN   zBandedMatrixLU.__init__  sJ    $92w2w-9!((DGGTWW-U*
DJ
r<   c                4    | j                   j                  d   S rb   )r'  rH   rQ   s    r#   rd   zBandedMatrixLU.nrows  s     zz""r<   c           
     F   t         }t        rddlm} t	        j
                  |t        j                        }t        |      | j                  k7  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_matrixrB   ;Item count of vector B has to be equal to matrix row count.)_solve_vector_banded_matrixr   r&  r+  rD   rE   rF   r~   rd   r   r    r'  r(  rm   r  r  )rL   r   r+  xs       r#   r   zBandedMatrixLU.solve_vector  s}     &A"GXXarzz2q6TZZM  &4::tzz4::tww
 	
r<   c                    t        |      }|j                  | j                  k7  rt        d      t        |j                         D cg c]  }| j	                  |       c}      j                         S c c}w )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

        rT   z,Row count of self and matrix B has to match.)r   rd   r   r{   r   r   )rL   r   r  ro   s       r#   r   zBandedMatrixLU.solve_matrix  s`     #>>TZZ'KLL6>mmoFsD%%c*F

)+	Fs   A6N)r.   r   r  r}   r  r}   r   r   r   )	r   r   r   r   rN   r   rd   r   r   rA   r<   r#   r   r     s)    CV # #
8r<   r   c                   t        j                  | t         j                        }|j                  d   }t        j                  ||ft         j                        }t        j                  |ft         j
                        }||z   dz   }|}t        |      D ]K  }	t        ||	z
  |      D ]  }
||	   |
   ||	   |
|z
  <    |dz  }t        ||z
  dz
  |      D ]
  }
d||	   |
<    M |}t        |      D ]  }||   d   }|}	||k  r|dz  }t        |dz   |      D ]*  }
t        ||
   d         t        |      kD  s!||
   d   }|
}	, |	dz   ||<   |	|k7  r-t        |      D ]  }
||	   |
   ||   |
   c||   |
<   ||	   |
<   ! t        |dz   |      D ]Z  }	||	   d   ||   d   z  }|||   |	|z
  dz
  <   t        d|      D ]  }
||	   |
   |||   |
   z  z
  ||	   |
dz
  <   ! d||	   |dz
  <   \  |||fS )NrB   r   rg   r   )rD   rE   rF   rH   rG   int64rz   r   )r.   r  r  r'  r  r(  rm   mmlr8   r  r7   dums                r#   r%  r%     sd   XXarzz2E [[^AXXq"gRZZ8EXXqd"((3E2gkBA2Y rAvr" 	*A#AhqkE!HQUO	*	QrAvz2& 	AE!HQK		 	A1X #Ahqkq5FAq1ua 	A58A;#c(*Ahqk	 q5a62Y D+08A;a(aU1Xa[D q1ua 	#A(1+a+C"%E!HQUQY1b\ B"'(1+eAhqk0A"AaQB"E!HR!V	##( %r<   c                   |j                   d   }| j                   d   |k7  rt        d      |}|}||z   dz   }	|}
t        |      D ]]  }||   dz
  }||k7  r| |   | |   c| |<   | |<   |
|k  r|
dz  }
t        |dz   |
      D ]!  }| |xx   ||   ||z
  dz
     | |   z  z  cc<   # _ d}
t        |dz
  dd      D ]E  }| |   }t        d|
      D ]  }|||   |   | ||z      z  z  } |||   d   z  | |<   |
|	k  sA|
dz  }
G | 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   r,  rg   r  )rH   r   rz   )r.  r'  r(  rm   r  r  r  alaur2  r3  r7   r  r8   r4  s                  r#   r-  r-  *  sg   $ [[^AwwqzQVWWBB2gkBA1X ,!HqL61qtJAaD!A$q5FAq1ua 	,AaDBqE!a%!)$qt++D	,, 	
A1q5"b! dq! 	'A2a58aAh&&C	'RU1X~!r6FA Hr<   )r   zIterable[list])r   r%   )r7   r}   r8   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   r   )r.   r%   r   r   r   r   )
r   r   r   r   r   r   rv   r   r   r   )T)r.   r   r   ztuple[Matrix, int, int])r.   r   r   r   )r.   r   r  r}   r  r}   r   r   )r.   r(   r  r}   r  r}   r   z tuple[NDArray, NDArray, NDArray])r.  r(   r'  r(   r(  r(   rm   r(   r  r}   r  r}   r   r(   )6
__future__r   typingr   r   r   r   r   r	   r
   typing_extensionsr   r   	functoolsr   	itertoolsr   r5   r]   numpyrD   numpy.typingnpt	ezdxf.accr   __all__r$   r-   r%   __annotations__r&   r}   r'   r(   rF   r1   r   r   ABCr   r   r   r   r   r   r   r   r  r   r   r   r   r%  r-  rA   r<   r#   <module>rD     s   #   ( 
       $
 T%[)
I )#E%*$56 ) 6c?y "[[, ,2 3/ /,R1 R1jSWW P"+\8"9$"B& "BJ7@++ 4++\&&"&'2&7B&&R $>8AV AH'T,,, , 	,
 	, 	, ,r<   