
    wgNi                     6    d Z ddlmZ ddlmZ  G d de      Zy)a  
Linear algebra
--------------

Linear equations
................

Basic linear algebra is implemented; you can for example solve the linear
equation system::

      x + 2*y = -10
    3*x + 4*y =  10

using ``lu_solve``::

    >>> from mpmath import *
    >>> mp.pretty = False
    >>> A = matrix([[1, 2], [3, 4]])
    >>> b = matrix([-10, 10])
    >>> x = lu_solve(A, b)
    >>> x
    matrix(
    [['30.0'],
     ['-20.0']])

If you don't trust the result, use ``residual`` to calculate the residual ||A*x-b||::

    >>> residual(A, x, b)
    matrix(
    [['3.46944695195361e-18'],
     ['3.46944695195361e-18']])
    >>> str(eps)
    '2.22044604925031e-16'

As you can see, the solution is quite accurate. The error is caused by the
inaccuracy of the internal floating point arithmetic. Though, it's even smaller
than the current machine epsilon, which basically means you can trust the
result.

If you need more speed, use NumPy, or ``fp.lu_solve`` for a floating-point computation.

    >>> fp.lu_solve(A, b)   # doctest: +ELLIPSIS
    matrix(...)

``lu_solve`` accepts overdetermined systems. It is usually not possible to solve
such systems, so the residual is minimized instead. Internally this is done
using Cholesky decomposition to compute a least squares approximation. This means
that that ``lu_solve`` will square the errors. If you can't afford this, use
``qr_solve`` instead. It is twice as slow but more accurate, and it calculates
the residual automatically.


Matrix factorization
....................

The function ``lu`` computes an explicit LU factorization of a matrix::

    >>> P, L, U = lu(matrix([[0,2,3],[4,5,6],[7,8,9]]))
    >>> print(P)
    [0.0  0.0  1.0]
    [1.0  0.0  0.0]
    [0.0  1.0  0.0]
    >>> print(L)
    [              1.0                0.0  0.0]
    [              0.0                1.0  0.0]
    [0.571428571428571  0.214285714285714  1.0]
    >>> print(U)
    [7.0  8.0                9.0]
    [0.0  2.0                3.0]
    [0.0  0.0  0.214285714285714]
    >>> print(P.T*L*U)
    [0.0  2.0  3.0]
    [4.0  5.0  6.0]
    [7.0  8.0  9.0]

Interval matrices
-----------------

Matrices may contain interval elements. This allows one to perform
basic linear algebra operations such as matrix multiplication
and equation solving with rigorous error bounds::

    >>> a = iv.matrix([['0.1','0.3','1.0'],
    ...             ['7.1','5.5','4.8'],
    ...             ['3.2','4.4','5.6']])
    >>>
    >>> b = iv.matrix(['4','0.6','0.5'])
    >>> c = iv.lu_solve(a, b)
    >>> print(c)
    [   [5.2582327113062568605927528666, 5.25823271130625686059275702219]]
    [[-13.1550493962678375411635581388, -13.1550493962678375411635540152]]
    [  [7.42069154774972557628979076189, 7.42069154774972557628979190734]]
    >>> print(a*c)
    [  [3.99999999999999999999999844904, 4.00000000000000000000000155096]]
    [[0.599999999999999999999968898009, 0.600000000000000000000031763736]]
    [[0.499999999999999999999979320485, 0.500000000000000000000020679515]]
    )copy   )xrangec                       e Zd ZddZddZd Zd ZddZd Zd Z	d	 Z
d
 Zd ZddZddZd Zd ZddZd ZddZy)LinearAlgebraMethodsc                 ~   |j                   |j                  k(  st        d      |r.t        || j                        r|j
                  r|j
                  S |s|}|j                         }| j                  | j                  |d      | j                  z        }|j                   }dg|dz
  z  }t        |dz
        D ]-  }d}	t        ||      D ]  }
| j                  t        ||      D cg c]  }| j                  ||
|f          c}      }| j                  |      |k  rt        d      d|z  | j                  ||
|f         z  }||	kD  s|}	|
||<    | j                  ||||          | j                  |||f         |k  rt        d      t        |dz   |      D ]F  }|||fxx   |||f   z  cc<   t        |dz   |      D ]  }
|||
fxx   |||f   |||
f   z  z  cc<     H 0 | j                  ||dz
  |dz
  f         |k  rt        d      |st        | j                        r	||f|_        ||fS c c}w )z
        LU-factorization of a n*n matrix using the Gauss algorithm.
        Returns L and U in one matrix and the pivot indices.

        Use overwrite to specify whether A will be overwritten with L and U.
        need n*n matrix   Nr   matrix is numerically singular)rowscols
ValueError
isinstancematrix_LUr   absminmnormepsr   fsumZeroDivisionErrorswap_row)ctxA	overwrite	use_cacheorigtolnpjbiggestklscurrentis                  [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/mpmath/matrices/linalg.py	LU_decompzLinearAlgebraMethods.LU_decompn   sE    vv.//Aszz2quu55LDAjj1Q#''12FFFAENA 	,AGAq\ HH&A,GQcjj1Q30GH::a=C'+,LMMA#

1QqS6 22W$%GAaD LLAqt$zz!AaC&!S('(HIIAE1% ,!A#!AaC& Aq) ,AacFa!fQqsVm+F,,!	,( ::aAa!en%,#$DEEZcjj91vDH!t+ Hs   (H:Nc                    |j                   |j                  k7  rt        d      |j                   }t        |      |k7  rt	        d      t        |      }|r0t        dt        |            D ]  }| j                  ||||           t        d|      D ]*  }t        |      D ]  }||xx   |||f   ||   z  z  cc<    , |S )zG
        Solve the lower part of a LU factorized matrix for y.
        r	   Value should be equal to nr   r
   )r   r   RuntimeErrorlenr   r   r   r   )r   Lbr   r   r"   r&   r    s           r'   L_solvezLinearAlgebraMethods.L_solve   s     66QVV011FFq6Q;9::GAs1v& )Q1Q4() 1 	&AAY &!!A#1%&	&     c                 T   |j                   |j                  k7  rt        d      |j                   }t        |      |k7  rt	        d      t        |      }t        |dz
  dd      D ]@  }t        |dz   |      D ]  }||xx   |||f   ||   z  z  cc<    ||xx   |||f   z  cc<   B |S )zG
        Solve the upper part of a LU factorized matrix for x.
        r	   r*   r
   )r   r   r+   r,   r   r   r   )r   Uyr   xr&   r    s          r'   U_solvezLinearAlgebraMethods.U_solve   s     66QVV011FFq6Q;9::GAr2& 	AAE1% &!!A#1%&aDAacFND	 r0   c                      j                   }	  xj                   dz  c_           j                  |fi |j                           j                  |fi |j                         }}|j                  |j                  k  rt        d      |j                  |j                  kD  rb|j                  }||z  }||z  }|j                  dd      st         fd|D              s j                  ||      }nL j                  ||      }n9 j                  |      \  }} j                  |||      } j                  ||      }| _         |S # | _         w xY w)a  
        Ax = b => x

        Solve a determined or overdetermined linear equations system.
        Fast LU decomposition is used, which is less accurate than QR decomposition
        (especially for overdetermined systems), but it's twice as efficient.
        Use qr_solve if you want more precision or have to solve a very ill-
        conditioned system.

        If you specify real=True, it does not check for overdeterminded complex
        systems.
        
   #cannot solve underdetermined systemrealFc              3   L   K   | ]  }t        |      j                  u   y wNtypempc).0r&   r   s     r'   	<genexpr>z0LinearAlgebraMethods.lu_solve.<locals>.<genexpr>   s     :1DGsww.:   !$)precr   r   r   r   r   Hgetsumcholesky_solvelu_solver(   r/   r6   )r   r   r.   kwargsrC   AHr5   r   s   `       r'   rH   zLinearAlgebraMethods.lu_solve   s-    xx	HHNH3::a*6*//1:3::a3J63J3O3O3QqAvv !FGGvv SSFFJJvu-:::**1a0AQ*A }}Q'1KK1a(KK1%CH CHs   D/E 	Ec                    |j                   |j                  k7  rt        d      t        |      D ]R  }| j	                  |||      }| j                  |d      d| j                  z  k  r |S | j                  ||       }||z  }T |S )z
        Improve a solution to a linear equation system iteratively.

        This re-uses the LU decomposition and is thus cheap.
        Usually 3 up to 4 iterations are giving the maximal improvement.
        r	   r   r8   )r   r   r+   r   residualnormr   rH   )r   r   r5   r.   maxsteps_rdxs           r'   improve_solutionz%LinearAlgebraMethods.improve_solution   s     66QVV011! 	AQ1%Axx1~377
*  a!$BGA	 r0   c                    | j                  |      \  }}|j                  }| j                  |      }| j                  |      }t        |      D ]I  }t        |      D ]9  }||kD  r|||f   |||f<   ||k(  rd|||f<   |||f   |||f<   .|||f   |||f<   ; K | j	                  |      }t        t        |            D ]  }	| j                  ||	||	           |||fS )a7  
        A -> P, L, U

        LU factorisation of a square matrix A. L is the lower, U the upper part.
        P is the permutation matrix indicating the row swaps.

        P*A = L*U

        If you need efficiency, use the low-level method LU_decomp instead, it's
        much more memory efficient.
        r
   )r(   r   r   r   eyer,   r   )
r   r   r   r   r-   r3   r&   r    Pr"   s
             r'   luzLinearAlgebraMethods.lu   s     }}Q1FFJJqMJJqM 	$AAY $q5qsVAacF!VAacFqsVAacFqsVAacF$	$ GGAJA 	%ALLAqt$	%!Qwr0   c                     d|cxk  r|k  sJ d        J d       | j                   g|dz
  z  | j                  gz   | j                   g||z
  z  z   S )z<
        Return the i-th n-dimensional unit vector.
        r   zthis unit vector does not existr
   )zeroone)r   r   r&   s      r'   
unitvectorzLinearAlgebraMethods.unitvector  sY     1zz<<<z<<<zz1Q3377)+sxxj!A#.>>>r0   c                 h   | j                   }	 | xj                   dz  c_          | j                  |fi |j                         }|j                  }| j	                  |      \  }}g }t        d|dz         D ]H  }| j                  ||      }| j                  |||      }	|j                  | j                  ||	             J g }
t        |      D ]<  }g }t        |      D ]  }|j                  ||   |           |
j                  |       >  | j                  |
fi |}|| _         |S # || _         w xY w)z
        Calculate the inverse of a matrix.

        If you want to solve an equation system Ax = b, it's recommended to use
        solve(A, b) instead, it's about 3 times more efficient.
        r8   r
   )
rC   r   r   r   r(   r   rZ   r/   appendr6   )r   r   rI   rC   r   r   r   r&   er4   invrowr    results                 r'   inversezLinearAlgebraMethods.inverse   s/    xx	HHNH

1'',,.AA==#DAqDAq1u% /NN1a(KK1a(CKK1-./
 CAY   +AJJtAwqz*+

3	 
  SZZ.v.FCH CHs   DD( (	D1c                    
 t         j                        st        d      j                  }j                  }||dz
  k  rt        d      g }t        d|dz
        D ]'  
 j                  
fdt        
|      D              }t        |       j                  kD  st        d      |j                   j                   j                  

f                 j                  |      z          j                  ||
   

f   z  z
  z  }

fxx   |
   z  cc<   t        
dz   |      D ]T   j                   
fdt        
|      D              |z  }t        
|      D ]  fxx   
f   |z  z  cc<    V * t        |dz
        D cg c]  }||dz
  f    c}t        |dz
  d	d	      D ]I  xx    j                  fd
t        dz   |dz
        D              z  cc<   xx   |   z  cc<   K ||dz
  k(  s-t        ||z
  dz         D cg c]  }|dz
  |z
  |dz
  f    }	}ndg|z  }	||	fS c c}w c c}w )a  
        (A|b) -> H, p, x, res

        (A|b) is the coefficient matrix with left hand side of an optionally
        overdetermined linear equation system.
        H and p contain all information about the transformation matrices.
        x is the solution, res the residual.
         A should be a type of ctx.matrixr
   z$Columns should not be less than rowsr   c              3   D   K   | ]  }t        |f         d z    ywr   N)absr@   r&   r   r    s     r'   rA   z3LinearAlgebraMethods.householder.<locals>.<genexpr>S  s!     >AQqsVa>s    r   c              3   Z   K   | ]"  }j                  |f         |f   z   $ y wr<   conjr@   r&   r   r   r    r"   s     r'   rA   z3LinearAlgebraMethods.householder.<locals>.<genexpr>Z  s/     M1SXXa!f-!A#6M   (+r   r2   c              3   8   K   | ]  }|f   |   z    y wr<    )r@   r    r   r&   r5   s     r'   rA   z3LinearAlgebraMethods.householder.<locals>.<genexpr>`  s!     IqQqsVad]I   )r   r   	TypeErrorr   r   r+   r   r   rf   r   r   r\   signresqrtrY   )r   r   mr   r   r$   kappar4   r&   rP   r    r"   r5   s   ``      ` @@@r'   householderz LinearAlgebraMethods.householderA  s[    !SZZ(>??FFFFq1u9EFF1q5! 
	)A>1>>Aq6CGG# !ABBHHchhsvva!f~..!<=GGq1Q4!AaC&=01EacFadNFAaC^ )HHMq!MMPUU1 )AacFa!fqj(F))
	) "(A/AQqQwZ/Ar2& 	AaDCHHIF1q5!a%4HIIIDaDAaDLD	 AEz(.q1uqy(9:11Q3q5!A#::A: AA!Qz 0 ;s   I*Ic                     | j                   }	 | xj                   dz  c_          | j                  |fi | | j                  |fi | | j                  |fi |}}}||z  |z
  || _         S # || _         w xY w)zt
        Calculate the residual of a solution to a linear equation system.

        r = A*x - b for A*x = b
        r   )rC   r   )r   r   r5   r.   rI   oldprecs         r'   rL   zLinearAlgebraMethods.residualv  sx     ((	HHMH cjj-f-zszz!/Fv/F


STH_X^H_!qAQ37CHwCHs   AA+ +	A4c                 :   || j                   }| j                  }	 | xj                  dz  c_         | j                  |fi |j                          | j                  |fi |j                         }}|j                  |j
                  k  rt        d      | j                  | j                  ||            \  }}}}	| j                  |	      }
|
dk(  r"| j                  | j                  |||            }
 | j                  |fi ||
f|| _        S # || _        w xY w)aa  
        Ax = b => x, ||Ax - b||

        Solve a determined or overdetermined linear equations system and
        calculate the norm of the residual (error).
        QR decomposition using Householder factorization is applied, which gives very
        accurate results even for ill-conditioned matrices. qr_solve is twice as
        efficient.
        r8   r9   r   )
rM   rC   r   r   r   r   r   rv   extendrL   )r   r   r.   rM   rI   rC   rD   r   r5   rP   ress              r'   qr_solvezLinearAlgebraMethods.qr_solve  s     <88Dxx	HHNH3::a*6*//1:3::a3J63J3O3O3QqAvv !FGGAq)9:JAq!Q((1+Caxhhs||Aq!453::a*6*C/CHtCHs   C-D 	Dc                   	
 t        || j                        st        d      |j                  |j                  k(  st        d      || j                  }|j                  }| j                  |      	t        |      D ]  | j                  |f         }t        ||f   z
        |kD  rt        d      || j                  	fdt              D        dd      z
  }||k  rt        d      | j                  |      	f<   t        |      D ]W  
	
fdt              D        }	fd	t              D        }| j                  ||d
      }|
f   |z
  	f   z  	
f<   Y  	S )a}  
        Cholesky decomposition of a symmetric positive-definite matrix `A`.
        Returns a lower triangular matrix `L` such that `A = L \times L^T`.
        More generally, for a complex Hermitian positive-definite matrix,
        a Cholesky decomposition satisfying `A = L \times L^H` is returned.

        The Cholesky decomposition can be used to solve linear equation
        systems twice as efficiently as LU decomposition, or to
        test whether `A` is positive-definite.

        The optional parameter ``tol`` determines the tolerance for
        verifying positive-definiteness.

        **Examples**

        Cholesky decomposition of a positive-definite symmetric matrix::

            >>> from mpmath import *
            >>> mp.dps = 25; mp.pretty = True
            >>> A = eye(3) + hilbert(3)
            >>> nprint(A)
            [     2.0      0.5  0.333333]
            [     0.5  1.33333      0.25]
            [0.333333     0.25       1.2]
            >>> L = cholesky(A)
            >>> nprint(L)
            [ 1.41421      0.0      0.0]
            [0.353553  1.09924      0.0]
            [0.235702  0.15162  1.05899]
            >>> chop(A - L*L.T)
            [0.0  0.0  0.0]
            [0.0  0.0  0.0]
            [0.0  0.0  0.0]

        Cholesky decomposition of a Hermitian matrix::

            >>> A = eye(3) + matrix([[0,0.25j,-0.5j],[-0.25j,0,0],[0.5j,0,0]])
            >>> L = cholesky(A)
            >>> nprint(L)
            [          1.0                0.0                0.0]
            [(0.0 - 0.25j)  (0.968246 + 0.0j)                0.0]
            [ (0.0 + 0.5j)  (0.129099 + 0.0j)  (0.856349 + 0.0j)]
            >>> chop(A - L*L.H)
            [0.0  0.0  0.0]
            [0.0  0.0  0.0]
            [0.0  0.0  0.0]

        Attempted Cholesky decomposition of a matrix that is not positive
        definite::

            >>> A = -eye(3) + hilbert(3)
            >>> L = cholesky(A)
            Traceback (most recent call last):
              ...
            ValueError: matrix is not positive-definite

        **References**

        1. [Wikipedia]_ http://en.wikipedia.org/wiki/Cholesky_decomposition

        rc   r	   zmatrix is not Hermitianc              3   ,   K   | ]  }|f     y wr<   rn   r@   r"   r-   r    s     r'   rA   z0LinearAlgebraMethods.cholesky.<locals>.<genexpr>  s     8a!f8   T)absolutesquaredzmatrix is not positive-definitec              3   ,   K   | ]  }|f     y wr<   rn   )r@   r"   r-   r&   s     r'   rA   z0LinearAlgebraMethods.cholesky.<locals>.<genexpr>       1!q1v1r   c              3   ,   K   | ]  }|f     y wr<   rn   r   s     r'   rA   z0LinearAlgebraMethods.cholesky.<locals>.<genexpr>  r   r   )	conjugate)r   r   r+   r   r   r   r   r   rr   rf   r   rs   fdot)r   r   r   r   cr$   it1it2tr-   r&   r    s            @@@r'   choleskyzLinearAlgebraMethods.cholesky  ss   | !SZZ(ABBvv.//;77(CFFJJqM 	/Aq1vA1QqsV8}s" !:;;CHH8fQi8t  - -A3w !BCCXXa[AacFAq\ /1vay11vay1HHS#H6AaC&1*!A#.!A#	/	/ r0   c           
         | j                   }	 | xj                   dz  c_          | j                  |fi |j                          | j                  fi |j                         c}|j                  |j                  k7  rt        d      | j                  |      j                  }t              |k7  rt        d      t        |      D ]D  xx   | j                  fdt              D              z  cc<   xx   f   z  cc<   F | j                  j                        }||| _         S # || _         w xY w)z
        Ax = b => x

        Solve a symmetric positive-definite linear equation system.
        This is twice as efficient as lu_solve.

        Typical use cases:
        * A.T*A
        * Hessian matrix
        * differential equations
        r8   z can only solve determined systemr*   c              3   8   K   | ]  }|f   |   z    y wr<   rn   )r@   r    r-   r.   r&   s     r'   rA   z6LinearAlgebraMethods.cholesky_solve.<locals>.<genexpr>  s!      B11Q3!A$ Bro   )rC   r   r   r   r   r   r   r,   r   r   r6   T)	r   r   r.   rI   rC   r   r5   r-   r&   s	     `    @@r'   rG   z#LinearAlgebraMethods.cholesky_solve  s    xx	HHNH3::a*6*//1:3::a3J63J3O3O3QDAqvv!&&  !CDDQAA1v{ !=>>AY ! Bq	 BBB!!A# ACC#ACHtCHs   D!D: :	Ec                 b   | j                   }	 | j                  |      j                         }	 | j                  |      \  }}d}t        |      D ]  \  }}||k7  s|dz  } t        |j                        D ]  }||||f   z  } ||| _         S # t        $ r
 Y || _         yw xY w# || _         w xY w)z8
        Calculate the determinant of a matrix.
        r   r
   r2   )rC   r   r   r(   r   	enumerater   r   )r   r   rC   Rr   zr&   r]   s           r'   detzLinearAlgebraMethods.det  s     xx	

1""$A}}Q'1 A!! 16GA AFF^ QqsVCH %  CH CHs4   B% B B% ,B% 	B"B% !B""B% %	B.c                 R     | fd} ||       | j                  |            z  S )a)  
        Calculate the condition number of a matrix using a specified matrix norm.

        The condition number estimates the sensitivity of a matrix to errors.
        Example: small input errors for ill-conditioned coefficient matrices
        alter the solution of the system dramatically.

        For ill-conditioned matrices it's recommended to use qr_solve() instead
        of lu_solve(). This does not help with input errors however, it just avoids
        to add additional errors.

        Definition:    cond(A) = ||A|| * ||A**-1||
        c                 (    j                  | d      S )Nr
   )r   )r5   r   s    r'   <lambda>z+LinearAlgebraMethods.cond.<locals>.<lambda>=  s    SYYq^ r0   )ra   )r   r   rM   s   `  r'   condzLinearAlgebraMethods.cond.  s+     <+DAwckk!n---r0   c                    | j                  |j                  |j                        }t        |j                        D ]F  }| j	                  ||j                  |            }t        t        |            D ]  }||   |||f<    H |S )z,Solve a * x = b  where a and b are matrices.)r   r   r   rangerH   columnr,   )r   ar.   rP   r&   r   r    s          r'   lu_solve_matz!LinearAlgebraMethods.lu_solve_mat@  sx    JJqvvqvv&qvv 	AQ,A3q6] A$!Q$	 r0   c           
          t         j                        sJ j                  }j                  }|dk\  sJ ||k\  sJ |dk\  sJ t	         fdD              } j                  |      5   j                  |d      }j                         |r j                  dd      } j                  dd      }	 j                  d      }
t        d|      D ]  f   } j                  |      } j                  |      }|z
  dk\  rJ j                   fdt        dz   |      D              } j                   j                  |            }n|
}||
k(  r||
k(  r|	|<   ||
k  r! j                  |dz  |dz  z   |dz  z         }n! j                  |dz  |dz  z   |dz  z          } j                  ||z
  |z  | |z        |<    j                  |          }|||z
  z  }t        dz   |      D ]  }|fxx   |z  cc<    |f<   t        dz   |      D ]e   j                   fdt        |      D              }| j                  |      z  }t        |      D ]  }|fxx   |f   |z  z  cc<    g  j                  |d      f<    n j                  d      } j                  d      }	t        d|      D ]a  f   }|z
  dkD  r: j                  fd	t        dz   |      D              } j                  |      }n|z
  dk(  rt        |dz
  f         }n|	}||	k(  r|	|<   v||	k  r j                  |dz  |dz  z         }n j                  |dz  |dz  z          }||z
  |z  |<   |    }|||z
  z  }t        dz   |      D ]  }|fxx   |z  cc<    |f<   t        dz   |      D ]U   j                  fd
t        |      D              }||z  }t        |      D ]  }|fxx   |f   |z  z  cc<    W |f<   d |dk(  s|dk(  r|fcddd       S j                         }t        d|      D ]  t        dz   |      D ]	  }|	||f<     |}|dk(  s|dk(  r|}xj                  ||z
  z  c_        t        d|      D ]!  |f<   t        d      D ]	  }|	|f<    # t        |dz
  dd      D ]  |    }fxx   |z  cc<   t        dz   |      D ]  |r? j                   fdt        dz   |      D              }| j                  |      z  }n. j                  fdt        dz   |      D              }||z  }|f<   t        dz   |      D ]  }|fxx   |f   |z  z  cc<     t        dz   |      D ]  }|fxx   |z  cc<     |d|d|f   fcddd       S # 1 sw Y   yxY w)al  
        Compute a QR factorization $A = QR$ where
        A is an m x n matrix of real or complex numbers where m >= n

        mode has following meanings:
        (1) mode = 'raw' returns two matrixes (A, tau) in the
            internal format used by LAPACK
        (2) mode = 'skinny' returns the leading n columns of Q
            and n rows of R
        (3) Any other value returns the leading m columns of Q
            and m rows of R

        edps is the increase in mp precision used for calculations

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15
            >>> mp.pretty = True
            >>> A = matrix([[1, 2], [3, 4], [1, 1]])
            >>> Q, R = qr(A)
            >>> Q
            [-0.301511344577764   0.861640436855329   0.408248290463863]
            [-0.904534033733291  -0.123091490979333  -0.408248290463863]
            [-0.301511344577764  -0.492365963917331   0.816496580927726]
            >>> R
            [-3.3166247903554  -4.52267016866645]
            [             0.0  0.738548945875996]
            [             0.0                0.0]
            >>> Q * R
            [1.0  2.0]
            [3.0  4.0]
            [1.0  1.0]
            >>> chop(Q.T * Q)
            [1.0  0.0  0.0]
            [0.0  1.0  0.0]
            [0.0  0.0  1.0]
            >>> B = matrix([[1+0j, 2-3j], [3+j, 4+5j]])
            >>> Q, R = qr(B)
            >>> nprint(Q)
            [     (-0.301511 + 0.0j)   (0.0695795 - 0.95092j)]
            [(-0.904534 - 0.301511j)  (-0.115966 + 0.278318j)]
            >>> nprint(R)
            [(-3.31662 + 0.0j)  (-5.72872 - 2.41209j)]
            [              0.0       (3.91965 + 0.0j)]
            >>> Q * R
            [(1.0 + 0.0j)  (2.0 - 3.0j)]
            [(3.0 + 1.0j)  (4.0 + 5.0j)]
            >>> chop(Q.T * Q.conjugate())
            [1.0  0.0]
            [0.0  1.0]

        r   c              3   L   K   | ]  }t        |      j                  u   y wr<   r=   )r@   r5   r   s     r'   rA   z*LinearAlgebraMethods.qr.<locals>.<genexpr>  s     21DGsww&2rB   r
   z1.0z0.0r   c              3   Z   K   | ]"  }|f   j                  |f         z   $ y wr<   ri   )r@   r&   r   r   r    s     r'   rA   z*LinearAlgebraMethods.qr.<locals>.<genexpr>  s/     ([a!AaC&!AaC&1A*A([rl   c              3   Z   K   | ]"  }|f   j                  |f         z   $ y wr<   ri   rk   s     r'   rA   z*LinearAlgebraMethods.qr.<locals>.<genexpr>  s/     $U1QqsVchhq1v.>%>$Url   c              3   2   K   | ]  }|f   d z    ywre   rn   rg   s     r'   rA   z*LinearAlgebraMethods.qr.<locals>.<genexpr>  s     (O!1QqS6A+(Os   c              3   <   K   | ]  }|f   |f   z    y wr<   rn   r@   r&   r   r    r"   s     r'   rA   z*LinearAlgebraMethods.qr.<locals>.<genexpr>  s%     $M!a!fq1vo$M   rawRAWNskinnySKINNYr2   c              3   Z   K   | ]"  }|f   j                  |f         z   $ y wr<   ri   rk   s     r'   rA   z*LinearAlgebraMethods.qr.<locals>.<genexpr>  s/     $W1QqsVchhq1v.>%>$Wrl   c              3   <   K   | ]  }|f   |f   z    y wr<   rn   r   s     r'   rA   z*LinearAlgebraMethods.qr.<locals>.<genexpr>	  s%     $MQqsVa!f_$Mr   )r   r   r   r   anyextradpsr   r?   mpfr   rr   imr   rs   rj   rf   )r   r   modeedpsrt   r   cmplxtaurY   rX   rzeroalphaalphralphixnormbetar   zar&   r4   tempdar   r   r    r"   s   ``                      @@r'   qrzLinearAlgebraMethods.qrI  s   p !SZZ(((FFFFAvvAvvqyy 222 \\$ F	!**Qq/CA
 ggeU+wwue,  1 "2AacFEFF5MEFF5ME!z #([6RSTURUWX>([ [ # 9 %Ue^!%A u}"xxq5!8(;eQh(FG #E1H)<uax)G HH WWte|t&;eVd]LCF#a&))A-B#AaC^ %!A#"% !AacF#AaC^ 4HH$UqRS$UU 388A;!'1 4AacFa!ftm3F44 !WWT51AacFE"2H ggenwwu~  1 ""AacFE!qy #(Ovac1~(O O #A#! #Qqs1uX $}!%A t|"xxq5!8(;< #E1H)< =="Uld2CFQA-B#AaC^ %!A#"% !AacF#AaC^ 4HH$Mva|$MM 1u!'! 4AacFa!ftm3F44 "AacFE""J 45=#vwF	! F	!D AAq\ "!Q "A!AacF""
 A dh&6 FFqsOFAq\ "!A#1 "A!AacF"" AaCR( !VG!A#!!Q 	0AHH$WqQRsTU$WW 388A;HH$MfQqS!n$MM 1u!AacF#AaC^ 0!A#!AaC&4-/0	0  !Q !AadGqLG!!$ a!AaCj=MF	! F	! F	!s   /OW;F W;;X)FTr<   )r
   )fullr8   )__name__
__module____qualname__r(   r/   r6   rH   rR   rV   rZ   ra   rv   rL   r|   r   rG   r   r   r   r   rn   r0   r'   r   r   l   s`    +Z& 'R$@?B'j8TlB..$I!r0   r   N)__doc__r   libmp.backendr   objectr   rn   r0   r'   <module>r      s"   `N  "f
!6 f
!r0   