
    [h                         S r SSKJr  SSKJr  SS jrS rS rS rSS	 jr	\SS
 j5       r
\SS j5       r\SS j5       r\SS j5       rSS jrSS jr\SS j5       r\SS j5       r\SS j5       rg)a  
The symmetric eigenvalue problem.
---------------------------------

This file contains routines for the symmetric eigenvalue problem.

high level routines:

  eigsy : real symmetric (ordinary) eigenvalue problem
  eighe : complex hermitian (ordinary) eigenvalue problem
  eigh  : unified interface for eigsy and eighe
  svd_r : singular value decomposition for real matrices
  svd_c : singular value decomposition for complex matrices
  svd   : unified interface for svd_r and svd_c


low level routines:

  r_sy_tridiag : reduction of real symmetric matrix to real symmetric tridiagonal matrix
  c_he_tridiag_0 : reduction of complex hermitian matrix to real symmetric tridiagonal matrix
  c_he_tridiag_1 : auxiliary routine to c_he_tridiag_0
  c_he_tridiag_2 : auxiliary routine to c_he_tridiag_0
  tridiag_eigen : solves the real symmetric tridiagonal matrix eigenvalue problem
  svd_r_raw : raw singular value decomposition for real matrices
  svd_c_raw : raw singular value decomposition for complex matrices
   )xrange   )defunc                    UR                   n[        US-
  SS5       GH  nSn[        SU5       H  nU[        XU4   5      -  nM     Sn	US:w  a  SU-  n	US:X  d  US:X  d  U R                  U	5      (       a  XS-
  U4   X6'   SX&'   Mk  Sn
[        SU5       H   nXU4==   U	-  ss'   XX4   XU4   -  -  n
M"     XS-
  U4   nU R	                  U
5      nUS:  a  U* nX|-  X6'   XU-  -  n
X-
  XS-
  U4'   Sn[        SU5       Hw  nU(       a  XU4   U
-  XU4'   Sn[        SUS-   5       H  nXX4   XU4   -  -  nM     [        US-   U5       H  nXX4   XU4   -  -  nM     X-  X='   XU   XU4   -  -  nMy     USU
-  -  n[        SU5       HH  nXU4   nX=   X-  -
  nXU'   [        SUS-   5       H  nXU4==   XU   -  XX4   -  -   -  ss'   M!     MJ     XU'   GM     [        SU5       H  nX6   X6S-
  '   M     SX5S-
  '   U(       a  SUS'   [        SU5       H  nX&   S:w  a^  [        SU5       HN  nSn[        SU5       H  nXXh4   XU4   -  -  nM     [        SU5       H  nXU4==   XX4   -  -  ss'   M     MP     XU4   X&'   SXU4'   [        SU5       H  nS=XU4'   XU4'   M     M     g[        SU5       H  nXU4   X&'   M     g)a  
This routine transforms a real symmetric matrix A to a real symmetric
tridiagonal matrix T using an orthogonal similarity transformation:
      Q' * A * Q = T     (here ' denotes the matrix transpose).
The orthogonal matrix Q is build up from Householder reflectors.

parameters:
  A         (input/output) On input, A contains the real symmetric matrix of
            dimension (n,n). On output, if calc_ev is true, A contains the
            orthogonal matrix Q, otherwise A is destroyed.

  D         (output) real array of length n, contains the diagonal elements
            of the tridiagonal matrix

  E         (output) real array of length n, contains the offdiagonal elements
            of the tridiagonal matrix in E[0:(n-1)] where is the dimension of
            the matrix A. E[n-1] is undefined.

  calc_ev   (input) If calc_ev is true, this routine explicitly calculates the
            orthogonal matrix Q which is then returned in A. If calc_ev is
            false, Q is not explicitly calculated resulting in a shorter run time.

This routine is a python translation of the fortran routine tred2.f in the
software library EISPACK (see netlib.org) which itself is based on the algol
procedure tred2 described in:
  - Num. Math. 11, p.181-195 (1968) by Martin, Reinsch and Wilkonson
  - Handbook for auto. comp., Vol II, Linear Algebra, p.212-226 (1971)

For a good introduction to Householder reflections, see also
  Stoer, Bulirsch - Introduction to Numerical Analysis.
r       r   N)rowsr   absisinfsqrt)ctxADEcalc_evniscalek	scale_invHFGjHHs                  W/var/www/auris/envauris/lib/python3.13/site-packages/mpmath/matrices/eigen_symmetric.pyr_sy_tridiagr   ,   sr   H 	
AAE1b! 1ASQ3[ E  	A:%I 6UaZ399Y#7#7qSU8ADAD 1AcFiF13!aC& A  c!eHHHQKq5Ay	U
5A#a% 1AQ3!A#AAq1u%qsVa!f_$ &AE1%qsVa!f_$ & 5AD1A#A  !a%[1AA#ArvAaDAq1u%A#!d(Q13Z// &  !w "z Aq\A# AcF!1Atqy1AA#Aq\qsVa!f_, *#Aq\A#!f*, *	 & qS6ADAcFAq\"##A#Q3 "  1AqS6AD     c                    UR                   nSXES-
  '   [        US-
  SS5       GH  nSn[        SU5       HC  nU[        U R                  XU4   5      5      [        U R	                  XU4   5      5      -   -  nME     Sn	US:w  a  SU-  n	US:X  d  U R                  U	5      (       a  SX6'   SX&'   SXFS-
  '   M  US:X  a=  XS-
  U4   n
[        U
5      nXU'   SX&'   US:w  a  XF   U
-  U-  XFS-
  '   O	XF   XFS-
  '   M  Sn[        SU5       HG  nXU4==   U	-  ss'   U R                  XU4   5      nU R	                  XU4   5      nXU-  X-  -   -  nMI     XS-
  U4   n
[        U
5      nU R                  U5      nXU-  -  nX-  X6'   US:w  a  X-  n
XF   * U
-  nX-  nOXF   * nXS-
  U4==   U-  ss'   Sn
[        SU5       H  nUUU4   U-  XU4'   Sn[        SUS-   5       H!  nXR                  XU4   5      XU4   -  -  nM#     [        US-   U5       H  nXUU4   XU4   -  -  nM     X-  UU'   XR                  UU   5      UUU4   -  -  n
M     U
SU-  -  n[        SU5       Hj  nUUU4   n
UU   UU
-  -
  nXU'   [        SUS-   5       H>  nXU4==   U R                  U
5      XH   -  U R                  U5      XU4   -  -   -  ss'   M@     Ml     UXFS-
  '   XU'   GM     [        SU5       H  nX6   X6S-
  '   M     SX5S-
  '   SUS'   [        SU5       H$  nX&   nU R                  XU4   5      X&'   UXU4'   M&     g)a  
This routine transforms a complex hermitian matrix A to a real symmetric
tridiagonal matrix T using an unitary similarity transformation:
      Q' * A * Q = T     (here ' denotes the hermitian matrix transpose,
                          i.e. transposition und conjugation).
The unitary matrix Q is build up from Householder reflectors and
an unitary diagonal matrix.

parameters:
  A         (input/output) On input, A contains the complex hermitian matrix
            of dimension (n,n). On output, A contains the unitary matrix Q
            in compressed form.

  D         (output) real array of length n, contains the diagonal elements
            of the tridiagonal matrix.

  E         (output) real array of length n, contains the offdiagonal elements
            of the tridiagonal matrix in E[0:(n-1)] where is the dimension of
            the matrix A. E[n-1] is undefined.

  T         (output) complex array of length n, contains a unitary diagonal
            matrix.

This routine is a python translation (in slightly modified form) of the fortran
routine htridi.f in the software library EISPACK (see netlib.org) which itself
is a complex version of the algol procedure tred1 described in:
  - Num. Math. 11, p.181-195 (1968) by Martin, Reinsch and Wilkonson
  - Handbook for auto. comp., Vol II, Linear Algebra, p.212-226 (1971)

For a good introduction to Householder reflections, see also
  Stoer, Bulirsch - Introduction to Numerical Analysis.
r   r   r   r   N)r	   r   r
   reimr   r   conj)r   r   r   r   Tr   r   r   r   r   r   fr   rriir   TZr   r   zws                       r   c_he_tridiag_0r)      s   D 	
AAcFAE1b! 1ASA#(3svva!f~+>>>E  	A:E	I A:9--ADADAcF6A#a%AAAaDADAvAA#A# 1AcFiFA#BA#Bb27""A	  c!eHFHHQK	U
y6A4!BFA$B	A#a%A 1AqsVaZAcFAAq1u%XXa!f%A#.. &AE1%qsVa!f_$ & 5AaD!A$!AaC&((A  !a%[1A!A#A!rAvAaDAq1u%A##((1+,sxx{QsV/CCC &  A#!c "f Aq\A# AcFAaDAq\Tvva!f~A# r   c           
         UR                   n[        SU5       H  nXU4   S:w  am  [        SU5       H]  nSn[        SU5       H!  nX`R                  XU4   5      XU4   -  -  nM#     [        SU5       H  nXU4==   XaXt4   -  -  ss'   M     M_     SXU4'   [        SU5       H  nS=XU4'   XU4'   M     M     [        SU5       H&  n[        SU5       H  nXU4==   X'   -  ss'   M     M(     g)a  
This routine forms the unitary matrix Q described in c_he_tridiag_0.

parameters:
  A    (input/output) On input, A is the same matrix as delivered by
       c_he_tridiag_0. On output, A is set to Q.

  T    (input) On input, T is the same array as delivered by c_he_tridiag_0.

r   r   Nr	   r   r"   )r   r   r#   r   r   r   r   r   s           r   c_he_tridiag_1r,   1  s     	
AAq\qS6Q;Aq\1A!aC&)AcF22A &1AcFaAC&j(F &	 " A#1AAcFQsV   Aq\1AcFadNF  r   c           
         UR                   n[        SU5       H&  n[        SU5       H  nX6U4==   X&   -  ss'   M     M(     [        SU5       H|  nXU4   S:w  d  M  [        SU5       H]  nSn[        SU5       H!  nXR                  XU4   5      X6U4   -  -  nM#     [        SU5       H  nX6U4==   XXe4   -  -  ss'   M     M_     M~     g)a  
This routine applied the unitary matrix Q described in c_he_tridiag_0
onto the the matrix B, i.e. it forms Q*B.

parameters:
  A    (input) On input, A is the same matrix as delivered by c_he_tridiag_0.

  T    (input) On input, T is the same array as delivered by c_he_tridiag_0.

  B    (input/output) On input, B is a complex matrix. On output B is replaced
       by Q*B.

This routine is a python translation of the fortran routine htribk.f in the
software library EISPACK (see netlib.org). See c_he_tridiag_0 for more
references.
r   Nr+   )	r   r   r#   Br   r   r   r   r   s	            r   c_he_tridiag_2r/   T  s    $ 	
AAq\1AcFadNF   Aq\qS6Q;Aq\1A!aC&)AcF22A &1AcFaAC&j(F &	 " r   c           	         [        U5      nSX$S-
  '   SU R                  -  n[        U5       GH  nSn Un US-   U:X  a  OB[        X(   5      U R                  [        X   5      [        XS-      5      -   -  ::  a  OUS-   nML  X:X  a  M]  Xu:  a  [        SU-  5      eUS-  nX   n	XS-      U	-
  SX&   -  -  n
U R                  U
S5      nU
S:  a  X-
  nOX-   nX   U	-
  X&   U-  -   n
Su  pn	[        US-
  US-
  S5       GH  nXU   -  nXU   -  n[        U5      [        U
5      :  a)  X-  nU R                  US5      nX-  X.S-   '   SU-  nX-  nO(X-  nU R                  US5      nX-  X.S-   '   SU-  nX-  nXS-      U	-
  n
X   U
-
  U-  SU-  U-  -   nX-  n	X-   XS-   '   X-  U-
  n
[        U[        5      (       a  M  [        UR                  5       H6  nUUUS-   4   nXUU4   -  X-  -   UUUS-   4'   XUU4   -  X-  -
  UUU4'   M8     GM     X   U	-
  X'   XU'   SX('   GM     [        SU5       H  nUS-
  nUnX   n	[        UU5       H  nX   U	:  a  M  UnUU   n	M     UU:X  a  M:  X   UU'   XU'   [        U[        5      (       a  M\  [        UR                  5       H  nUUU4   n	UUU4   UUU4'   XUU4'   M     M     g)a  
This subroutine find the eigenvalues and the first components of the
eigenvectors of a real symmetric tridiagonal matrix using the implicit
QL method.

parameters:

  d (input/output) real array of length n. on input, d contains the diagonal
    elements of the input matrix. on output, d contains the eigenvalues in
    ascending order.

  e (input) real array of length n. on input, e contains the offdiagonal
    elements of the input matrix in e[0:(n-1)]. On output, e has been
    destroyed.

  z (input/output) If z is equal to False, no eigenvectors will be computed.
    Otherwise on input z should have the format z[0:m,0:n] (i.e. a real or
    complex matrix of dimension (m,n) ). On output this matrix will be
    multiplied by the matrix of the eigenvectors (i.e. the columns of this
    matrix are the eigenvectors): z --> z*EV
    That means if z[i,j]={1 if j==j; 0 otherwise} on input, then on output
    z will contain the first m components of the eigenvectors. That means
    if m is equal to n, the i-th eigenvector will be z[:,i].

This routine is a python translation (in slightly modified form) of the
fortran routine imtql2.f in the software library EISPACK (see netlib.org)
which itself is based on the algol procudure imtql2 desribed in:
 - num. math. 12, p. 377-383(1968) by matrin and wilkinson
 - modified in num. math. 15, p. 450(1970) by dubrulle
 - handbook for auto. comp., vol. II-linear algebra, p. 241-248 (1971)
See also the routine gaussq.f in netlog.org or acm algorithm 726.
r   r   r   zBtridiag_eigen: no convergence to an eigenvalue after %d iterations)r   r   r   r   N)
lendpsr   r
   epsRuntimeErrorhypot
isinstanceboolr	   )r   dezr   iterlimlr   mpgrscr   r$   bwr&   r   s                       r   tridiag_eigenrE   y  s^   D 	AAAcF#''kGAYAq5A:qt93qt9s1U8}+D EEE  v|"#gjq#qrrFA Aq5A!ad(+A		!QA1uEEq14!8#AGA!AE1q5"-!H!Hq6CF?A		!QA uA!eHAAAA		!QA uA!eHAAA!eHqLTAXNQUQY.E5a%EAI!!T**#AFF^a!eH#$1v:#5!AaC%#$1v:#5!A# ,/ .8 4!8ADaDAD}  D QlFDAAtqyA!A	 
 6t!!!T""AFF^acF1Q3!A#!A# $! r   c                    U(       d  UR                  5       nU R                  UR                  S5      nU R                  UR                  S5      nU(       a  [        XXESS9  [	        XUS5        U$ [        XXESS9  [	        XXQ5        XA4$ )aO  
This routine solves the (ordinary) eigenvalue problem for a real symmetric
square matrix A. Given A, an orthogonal matrix Q is calculated which
diagonalizes A:

      Q' A Q = diag(E)               and                Q Q' = Q' Q = 1

Here diag(E) is a diagonal matrix whose diagonal is E.
' denotes the transpose.

The columns of Q are the eigenvectors of A and E contains the eigenvalues:

      A Q[:,i] = E[i] Q[:,i]


input:

  A: real matrix of format (n,n) which is symmetric
     (i.e. A=A' or A[i,j]=A[j,i])

  eigvals_only: if true, calculates only the eigenvalues E.
                if false, calculates both eigenvectors and eigenvalues.

  overwrite_a: if true, allows modification of A which may improve
               performance. if false, A is not modified.

output:

  E: vector of format (n). contains the eigenvalues of A in ascending order.

  Q: orthogonal matrix of format (n,n). contains the eigenvectors
     of A as columns.

return value:

      E          if eigvals_only is true
     (E, Q)      if eigvals_only is false

example:
  >>> from mpmath import mp
  >>> A = mp.matrix([[3, 2], [2, 0]])
  >>> E = mp.eigsy(A, eigvals_only = True)
  >>> print(E)
  [-1.0]
  [ 4.0]

  >>> A = mp.matrix([[1, 2], [2, 3]])
  >>> E, Q = mp.eigsy(A)
  >>> print(mp.chop(A * Q[:,0] - E[0] * Q[:,0]))
  [0.0]
  [0.0]

see also: eighe, eigh, eig
r   F)r   T)copyzerosr	   r   rE   )r   r   eigvals_onlyoverwrite_ar8   r9   s         r   eigsyrK     sx    r FFH		!&&!A		!&&!ASQU3ca'SQT2ca#vr   c                    U(       d  UR                  5       nU R                  UR                  S5      nU R                  UR                  S5      nU R                  UR                  S5      nU(       a  [        XXEU5        [	        XUS5        U$ [        XXEU5        U R                  UR                  5      n[	        XXW5        [        XXg5        XG4$ )a  
This routine solves the (ordinary) eigenvalue problem for a complex
hermitian square matrix A. Given A, an unitary matrix Q is calculated which
diagonalizes A:

    Q' A Q = diag(E)               and                Q Q' = Q' Q = 1

Here diag(E) a is diagonal matrix whose diagonal is E.
' denotes the hermitian transpose (i.e. ordinary transposition and
complex conjugation).

The columns of Q are the eigenvectors of A and E contains the eigenvalues:

    A Q[:,i] = E[i] Q[:,i]


input:

  A: complex matrix of format (n,n) which is hermitian
     (i.e. A=A' or A[i,j]=conj(A[j,i]))

  eigvals_only: if true, calculates only the eigenvalues E.
                if false, calculates both eigenvectors and eigenvalues.

  overwrite_a: if true, allows modification of A which may improve
               performance. if false, A is not modified.

output:

  E: vector of format (n). contains the eigenvalues of A in ascending order.

  Q: unitary matrix of format (n,n). contains the eigenvectors
     of A as columns.

return value:

       E         if eigvals_only is true
      (E, Q)     if eigvals_only is false

example:
  >>> from mpmath import mp
  >>> A = mp.matrix([[1, -3 - 1j], [-3 + 1j, -2]])
  >>> E = mp.eighe(A, eigvals_only = True)
  >>> print(E)
  [-4.0]
  [ 3.0]

  >>> A = mp.matrix([[1, 2 + 5j], [2 - 5j, 3]])
  >>> E, Q = mp.eighe(A)
  >>> print(mp.chop(A * Q[:,0] - E[0] * Q[:,0]))
  [0.0]
  [0.0]

see also: eigsy, eigh, eig
r   F)rG   rH   r	   r)   rE   eyer/   )r   r   rI   rJ   r8   r9   tr.   s           r   eigherO   A  s    t FFH		!&&!A		!&&!A		!&&!AsqQ'ca'sqQ'GGAFFOca#sq$vr   c                 |   ^  [        U 4S jU 5       5      nU(       a  T R                  XUS9$ T R                  XUS9$ )a  
"eigh" is a unified interface for "eigsy" and "eighe". Depending on
whether A is real or complex the appropriate function is called.

This routine solves the (ordinary) eigenvalue problem for a real symmetric
or complex hermitian square matrix A. Given A, an orthogonal (A real) or
unitary (A complex) matrix Q is calculated which diagonalizes A:

    Q' A Q = diag(E)               and                Q Q' = Q' Q = 1

Here diag(E) a is diagonal matrix whose diagonal is E.
' denotes the hermitian transpose (i.e. ordinary transposition and
complex conjugation).

The columns of Q are the eigenvectors of A and E contains the eigenvalues:

    A Q[:,i] = E[i] Q[:,i]

input:

  A: a real or complex square matrix of format (n,n) which is symmetric
     (i.e. A[i,j]=A[j,i]) or hermitian (i.e. A[i,j]=conj(A[j,i])).

  eigvals_only: if true, calculates only the eigenvalues E.
                if false, calculates both eigenvectors and eigenvalues.

  overwrite_a: if true, allows modification of A which may improve
               performance. if false, A is not modified.

output:

  E: vector of format (n). contains the eigenvalues of A in ascending order.

  Q: an orthogonal or unitary matrix of format (n,n). contains the
     eigenvectors of A as columns.

return value:

      E         if eigvals_only is true
     (E, Q)     if eigvals_only is false

example:
  >>> from mpmath import mp
  >>> A = mp.matrix([[3, 2], [2, 0]])
  >>> E = mp.eigh(A, eigvals_only = True)
  >>> print(E)
  [-1.0]
  [ 4.0]

  >>> A = mp.matrix([[1, 2], [2, 3]])
  >>> E, Q = mp.eigh(A)
  >>> print(mp.chop(A * Q[:,0] - E[0] * Q[:,0]))
  [0.0]
  [0.0]

  >>> A = mp.matrix([[1, 2 + 5j], [2 - 5j, 3]])
  >>> E, Q = mp.eigh(A)
  >>> print(mp.chop(A * Q[:,0] - E[0] * Q[:,0]))
  [0.0]
  [0.0]

see also: eigsy, eighe, eig
c              3   R   >#    U  H  n[        U5      TR                  L v   M     g 7fNtypempc.0xr   s     r   	<genexpr>eigh.<locals>.<genexpr>       21DGsww&   $')rI   rJ   )anyrO   rK   )r   r   rI   rJ   	iscomplexs   `    r   eighr_     s?    D 222Iyy{ySSyy{ySSr   c                 8   U R                  US5      nU R                  US5      nU R                  SU5      nSUS'   US:X  aK  Sn[        U5       H8  n	U	S-   n
U R                  X-  SU
-  U
-  U R                  S5      -
  -  5      Xi'   M:     GODUS:X  aa  Sn[        U5       HN  n	SU R                  S5      -  XY'   U	S-   n
U R                  X-  SU
-  U
-  U R                  S5      -
  -  5      Xi'   MP     GOUS:X  aX  U R                  U R                  5      n[        U5       H,  n	U	S-   n
U R                  XR                  S5      -  5      Xi'   M.     GOUS	:X  a)  Sn[        U5       H  n	U	S-   n
SU
-  S-
  XY'   XU	'   M     GOPUS
:X  a\  U R                  n[        U5       H  n	SU R                  S5      -  Xi'   M     U R                  SU R                  S5      -  5      US'   GOUS:X  a9  U R                  S-  n[        U5       H  n	SU R                  S5      -  Xi'   M     GOUS:X  aR  U R                  SU-   5      n[        U5       H-  n	U	S-   n
SU
-  S-
  U-   XY'   U R                  XU-   -  5      Xi'   M/     GOWUS:X  Ga  U R                  U5      nU R                  U5      nX4-   nUS-   nSUS-   -  U R                  US-   5      -  U R                  US-   5      -  U R                  U5      -  nXC-
  U-  US'   U R                  SSU-   -  SU-   -  US-   X-  -  -  5      US'   XD-  X3-  -
  n[        SU5       HO  n	U	S-   n
SU
-  U-   nXS-
  U-  -  XY'   U R                  SU
-  X-   -  X-   -  X-   -  X-  S-
  U-  U-  -  5      Xi'   MQ     OC[        U[        5      (       a  [        SU-  5      e[        U[        5      (       d	  U" XV5      nO e[        XXg5        [        [        U5      5       H  n	Xy==   Xy   -  ss'   M     UR                  5       nXXU-  4$ )a  
This routine calulates gaussian quadrature rules for different
families of orthogonal polynomials. Let (a, b) be an interval,
W(x) a positive weight function and n a positive integer.
Then the purpose of this routine is to calculate pairs (x_k, w_k)
for k=0, 1, 2, ... (n-1) which give

  int(W(x) * F(x), x = a..b) = sum(w_k * F(x_k),k = 0..(n-1))

exact for all polynomials F(x) of degree (strictly) less than 2*n. For all
integrable functions F(x) the sum is a (more or less) good approximation to
the integral. The x_k are called nodes (which are the zeros of the
related orthogonal polynomials) and the w_k are called the weights.

parameters
   n        (input) The degree of the quadrature rule, i.e. its number of
            nodes.

   qtype    (input) The family of orthogonal polynmomials for which to
            compute the quadrature rule. See the list below.

   alpha    (input) real number, used as parameter for some orthogonal
            polynomials

   beta     (input) real number, used as parameter for some orthogonal
            polynomials.

return value

  (X, W)    a pair of two real arrays where x_k = X[k] and w_k = W[k].


orthogonal polynomials:

  qtype           polynomial
  -----           ----------

  "legendre"      Legendre polynomials, W(x)=1 on the interval (-1, +1)
  "legendre01"    shifted Legendre polynomials, W(x)=1 on the interval (0, +1)
  "hermite"       Hermite polynomials, W(x)=exp(-x*x) on (-infinity,+infinity)
  "laguerre"      Laguerre polynomials, W(x)=exp(-x) on (0,+infinity)
  "glaguerre"     generalized Laguerre polynomials, W(x)=exp(-x)*x**alpha
                  on (0, +infinity)
  "chebyshev1"    Chebyshev polynomials of the first kind, W(x)=1/sqrt(1-x*x)
                  on (-1, +1)
  "chebyshev2"    Chebyshev polynomials of the second kind, W(x)=sqrt(1-x*x)
                  on (-1, +1)
  "jacobi"        Jacobi polynomials, W(x)=(1-x)**alpha * (1+x)**beta on (-1, +1)
                  with alpha>-1 and beta>-1

examples:
  >>> from mpmath import mp
  >>> f = lambda x: x**8 + 2 * x**6 - 3 * x**4 + 5 * x**2 - 7
  >>> X, W = mp.gauss_quadrature(5, "hermite")
  >>> A = mp.fdot([(f(x), w) for x, w in zip(X, W)])
  >>> B = mp.sqrt(mp.pi) * 57 / 16
  >>> C = mp.quad(lambda x: mp.exp(- x * x) * f(x), [-mp.inf, +mp.inf])
  >>> mp.nprint((mp.chop(A-B, tol = 1e-10), mp.chop(A-C, tol = 1e-10)))
  (0.0, 0.0)

  >>> f = lambda x: x**5 - 2 * x**4 + 3 * x**3 - 5 * x**2 + 7 * x - 11
  >>> X, W = mp.gauss_quadrature(3, "laguerre")
  >>> A = mp.fdot([(f(x), w) for x, w in zip(X, W)])
  >>> B = 76
  >>> C = mp.quad(lambda x: mp.exp(-x) * f(x), [0, +mp.inf])
  >>> mp.nprint(mp.chop(A-B, tol = 1e-10), mp.chop(A-C, tol = 1e-10))
  .0

  # orthogonality of the chebyshev polynomials:
  >>> f = lambda x: mp.chebyt(3, x) * mp.chebyt(2, x)
  >>> X, W = mp.gauss_quadrature(3, "chebyshev1")
  >>> A = mp.fdot([(f(x), w) for x, w in zip(X, W)])
  >>> print(mp.chop(A, tol = 1e-10))
  0.0

references:
  - golub and welsch, "calculations of gaussian quadrature rules", mathematics of
    computation 23, p. 221-230 (1969)
  - golub, "some modified matrix eigenvalue problems", siam review 15, p. 318-334 (1973)
  - stroud and secrest, "gaussian quadrature formulas", prentice-hall (1966)

See also the routine gaussq.f in netlog.org or ACM Transactions on
Mathematical Software algorithm 726.
r   r   r   legendrer      
legendre01   hermitelaguerre
chebyshev1r   
chebyshev2	glaguerrejacobizunknown quadrature rule "%s")rH   r   r   mpfpigammar6   str
ValueErrorrE   r1   	transpose)r   r   qtypealphabetar8   r9   r:   rD   r   r   ababia2b2s                 r   gauss_quadraturerx     s   n 			!QA		!QA		!QAAcF
AAA88AEQUQY%;<=AD  
,	Aswwqz>ADAA88AER!VaZ#''!*%<=>AD  
)	HHSVVAAA88A
N+AD  
*	AAAq519ADaD  
	FFAswwqz>AD xxCGGAJ'!	,	FFQJAswwqz>AD 	+	IIa%i AAAq519u$AD88AUO,AD  
(	wwt}\1fAY#))EAI..4!81DDsyyQT~U#!xxQY1t84qSY8OPQ!{U]*1AAAa%"*C!Gs?+AD88AEQY/18<GCIXYM]`K`cfKfghAD	 
 
E3		9EABBs##!Kq#!CF^	  	
A1u:r   c           
      H   UR                   UR                  pTU R                  US5      nU R                  US5      nS=n=pSU R                  -  n[	        U5       GH  nX-  X|'   S=n=pX:  Ga  [	        X5       H  nXR                  XU4   5      -  n	M     U	S:w  a  [	        X5       H   nXU4==   U	-  ss'   XX4   XU4   -  -  nM"     XU4   nU R                  U5      * nUS:  a  U* nX-  U-
  nX-
  XU4'   [	        US-   U5       HQ  nSn[	        X5       H  nXX4   XU4   -  -  nM     UU-  n[	        X5       H  nXU4==   XX4   -  -  ss'   M     MS     [	        X5       H  nXU4==   U	-  ss'   M     X-  Xl'   S=n=pX:  GaT  XS-
  :w  GaK  [	        US-   U5       H  nXR                  XU4   5      -  n	M     U	(       Ga  [	        US-   U5       H   nXU4==   U	-  ss'   XX4   XU4   -  -  nM"     XUS-   4   nU R                  U5      * nUS:  a  U* nX-  U-
  nX-
  XUS-   4'   [	        US-   U5       H  nXU4   U-  X~'   M     [	        US-   U5       HU  nSn[	        US-   U5       H  nXUU4   XU4   -  -  nM     [	        US-   U5       H  nUUU4==   XU   -  -  ss'   M     MW     [	        US-   U5       H  nXU4==   U	-  ss'   M     [        XR                  Xl   5      U R                  X|   5      -   5      n
GM     [        U[        5      (       d  [	        US-
  SS5       H  nSX,S-   US-   4'   X|S-      S:w  a  [	        US-   U5       H  nXU4   XUS-   4   -  X|S-      -  X,U4'   M!     [	        US-   U5       HV  nSn[	        US-   U5       H  nXX4   UUU4   -  -  nM     [	        US-   U5       H  nUUU4==   XX4   -  -  ss'   M     MX     [	        US-   U5       H  nS=UUU4'   X,U4'   M     M     SUS'   XE:  a  UnOUnU(       a  [	        US-
  SS5       H  nXl   n[	        US-   U5       H	  nSXU4'   M     US:w  a  SU-  n[	        US-   U5       H[  nSn[	        US-   U5       H  nXX4   XU4   -  -  nM     XX4   -  U-  n[	        X5       H  nXU4==   XX4   -  -  ss'   M     M]     [	        X5       H  nUUU4==   U-  ss'   M     O[	        X5       H
  nSUUU4'   M     XU4==   S-  ss'   M     [	        US-
  SS5       GHx  nSn US-  nSn[	        USS5       HD  nUS-
  nU R                  UU   5      U
-   U
:X  a  Sn  O!U R                  UU   5      U
-   U
:X  d  MD    O   U(       a  SnSn[	        WUS-   5       H  nXU   -  nX|==   U-  ss'   U R                  U5      U
-   U
:X  a    OvXl   nU R                  X5      nUXl'   SU-  nUU-  nU* U-  nU(       d  Mb  [	        U5       H1  nUUW4   nUUU4   nUU-  UU-  -   UUU4'   UU-  UU-  -
  UUU4'   M3     M     Xn   nWU:X  a@  US:  a7  U* Xn'   [        U[        5      (       d  [	        U5       H  nX.U4   * X.U4'   M     GMm  UU:  a  [        S	U-  5      eUU   nUS-
  nUU   nUU   nX~   nUU-
  UU-   -  UU-
  UU-   -  -   SU-  U-  -  nU R                  US5      nUS:  a  UU-
  UU-   -  UUX-   -  U-
  -  -   U-  nOUU-
  UU-   -  UUX-
  -  U-
  -  -   U-  nS=nn[	        UUS-   5       GHI  nUUS-      nUUS-      nX-  nUU-  nU R                  UU5      nUUU'   UU-  nUU-  nUU-  X-  -   nUU-  UU-  -
  nUU-  nUU-  n[        U[        5      (       dF  [	        U5       H7  nUUU4   nUUS-   U4   nUU-  UU-  -   UUU4'   UU-  UU-  -
  UUS-   U4'   M9     U R                  UU5      nUUU'   US:w  a  SU-  nUU-  nUU-  nUU-  UU-  -   nUU-  X-  -
  nU(       d  GM  [	        U5       H7  nUUU4   nUUUS-   4   nUU-  UU-  -   UUU4'   UU-  UU-  -
  UUUS-   4'   M9     GML     SUU'   XU'   UXn'   GMt     [	        U5       H  nUnU R                  Xl   5      n[	        US-   U5       H#  nU R                  UU   5      nUU:  d  M  UnUnM%     UU:w  d  MV  Xl   nUU   Xl'   UUU'   U(       a,  [	        U5       H  nUUU4   nUUU4   UUU4'   UUUU4'   M     [        U[        5      (       a  M  [	        U5       H  nX,U4   nUUU4   X,U4'   UUUU4'   M     M     U$ )
a  
This routine computes the singular value decomposition of a matrix A.
Given A, two orthogonal matrices U and V are calculated such that

                A = U S V

where S is a suitable shaped matrix whose off-diagonal elements are zero.
The diagonal elements of S are the singular values of A, i.e. the
squareroots of the eigenvalues of A' A or A A'. Here ' denotes the transpose.
Householder bidiagonalization and a variant of the QR algorithm is used.

overview of the matrices :

  A : m*n       A gets replaced by U
  U : m*n       U replaces A. If n>m then only the first m*m block of U is
                non-zero. column-orthogonal: U' U = B
                here B is a n*n matrix whose first min(m,n) diagonal
                elements are 1 and all other elements are zero.
  S : n*n       diagonal matrix, only the diagonal elements are stored in
                the array S. only the first min(m,n) diagonal elements are non-zero.
  V : n*n       orthogonal: V V' = V' V = 1

parameters:
  A        (input/output) On input, A contains a real matrix of shape m*n.
           On output, if calc_u is true A contains the column-orthogonal
           matrix U; otherwise A is simply used as workspace and thus destroyed.

  V        (input/output) if false, the matrix V is not calculated. otherwise
           V must be a matrix of shape n*n.

  calc_u   (input) If true, the matrix U is calculated and replaces A.
           if false, U is not calculated and A is simply destroyed

return value:
  S        an array of length n containing the singular values of A sorted by
           decreasing magnitude. only the first min(m,n) elements are non-zero.

This routine is a python translation of the fortran routine svd.f in the
software library EISPACK (see netlib.org) which itself is based on the
algol procedure svd described in:
  - num. math. 14, 403-420(1970) by golub and reinsch.
  - wilkinson/reinsch: handbook for auto. comp., vol ii-linear algebra, 134-151(1971).

r   r      r   r   ra   TF8svd: no convergence to an eigenvalue after %d iterations)r	   colsrH   r2   r   fabsr   maxr6   r7   r5   r4   )r   r   Vcalc_ur=   r   Sworkr?   r   anormmaxitsr   rA   r   r$   hr   minnmitsflagr<   nmrB   yr:   rX   jjimaxs                                r   	svd_r_rawr     so   \ 66166q		!QA 99Q?DA[FAY'A5A\!aC&)) "zAcFeOF13!aC&(A & cFXXa[Lq5AEAIA#!QAA#A\qsVa!f_, *AA#A\A#!f*, * (  AcFeOF % yA5Qa%ZAaC^!aC&)) $!QAcFeOF13!aC&(A ( !eHXXa[Lq5AEAI5AaC%!QA!fqjDG (  !QAA#AaC^qsVa!f_, ,#AaC^!A#!1g+- ,	 (  !QAcFeOF ( E88AD>CHHTW,==>q t a!R$AAc!A#gJaCyA~!QA!fq1Q3x/4!9<AcF (!QAA#AaC^qsVa!f_, ,#AaC^!A#!f*, ,	 ( AaC^"##!A#Q3 $ %  #sQUUaR(AAAaC^A# $AvE!QAA#AaC^qsVa!f_, ,qsVq(A#A\A#!f*, * (  AacFaKF &  AAacF &cFaKF% )( AE2r" 1HCDAr2&qS88DG$u,5 D88AbE?U*e3 ' 1q5)AGAGqLGxx{U*e3A		!AADAAAAaAv!'A !!B$A !!A#A&'!ea!emAadG&'!ea!emAacF	 "+ *& AAvq52AD%a..!'A&'!fWAcF "+f}"#]`c#cdd!A1B"ARAAa%AE"a!eA%66QCA		!QAAvQUq1u-a15kQ5F0GG1LqUq1u-a15kQ5F0GG1LqIAArAv&1IacFEEIIaOQEEEAEMEAEMEQ!!T**$Qia"fIac"fI%&UQU]!(%&UQU]!A#(	 (
 IIaO!6AAAAAAEAEMEAEM6$QibdIb1fI&'!ea!em"Q$&'!ea!em"QqS&	 (; 'F DGGADE  #Z AYHHQTNAq!A1A1u	 " 19 AT7ADAdGA!A#AqvYAacF !AafI #
 a&&AA#AtAvYAcF !Ad1fI #1 : Hr   c                    UR                   UR                  pTU R                  US5      nU R                  US5      nU R                  US5      nU R                  US5      n	U R                  US5      n
S=n=pSU R                  -  n[	        U5       GH  nX-  X'   S=n=nnX:  Ga  [	        X5       HP  nXR                  U R                  UUU4   5      5      U R                  U R                  UUU4   5      5      -   -  nMR     US:w  Ga  [	        X5       HL  nUUU4==   U-  ss'   U R                  UUU4   5      nU R                  UUU4   5      nUUU-  UU-  -   -  nMN     XU4   nU R                  U5      * nU R                  U5      S:  a  U* U R                  U5      -
  nU* nOU* U R                  U5      -   nUU R                  U5      -  nUS-  nSU R                  U5      U-  U-
  -  nUU-
  XU4'   UU-  nUU-  U-  X'   [	        US-   U5       Hf  nSn[	        X5       H$  nUU R                  UUU4   5      UUU4   -  -  nM&     UU-  n[	        X5       H  nUUU4==   UUUU4   -  -  ss'   M     Mh     [	        X5       H  nUUU4==   U-  ss'   M     X-  Xo'   S=n=nnX:  Ga;  XS-
  :w  Ga2  [	        US-   U5       HN  nXR                  U R                  XU4   5      5      U R                  U R                  XU4   5      5      -   -  nMP     U(       Ga  [	        US-   U5       HI  nXU4==   U-  ss'   U R                  XU4   5      nU R                  XU4   5      nUUU-  UU-  -   -  nMK     XUS-   4   nU R                  U5      * nU R                  U5      S:  a  U* U R                  U5      -
  nU* nOU* U R                  U5      -   nUU R                  U5      -  nUS-  nSU R                  U5      U-  U-
  -  nUU-
  XUS-   4'   UU-  nUU-  U-  X'   [	        US-   U5       H  nXU4   UU'   M     [	        US-   U5       Hk  nSn[	        US-   U5       H#  nUU R                  XU4   5      UUU4   -  -  nM%     UU-  n[	        US-   U5       H  nUUU4==   UUU   -  -  ss'   M     Mm     [	        US-   U5       H  nXU4==   U-  ss'   M     [        XR                  Xo   5      U R                  X   5      -   5      nGM     [        U[        5      (       Gd  [	        US-
  SS5       H  nSX/S-   US-   4'   XS-      S:w  a  U R                  X   5      n[	        US-   U5       H  nXU4   U-  X/U4'   M     [	        US-   U5       Hg  nSn[	        US-   U5       H#  nUU R                  XU4   5      UUU4   -  -  nM%     [	        US-   U5       H  nUUU4==   UX/U4   -  -  ss'   M     Mi     [	        US-   U5       H  nS=UUU4'   X/U4'   M     M     SUS'   XE:  a  UnOUnU(       Ga  [	        US-
  SS5       GH  nXo   n[	        US-   U5       H	  nSXU4'   M     US:w  a  SU-  n[	        US-   U5       H{  nSn[	        US-   U5       H$  nUU R                  UUU4   5      UUU4   -  -  nM&     UU R                  X   5      -  n[	        X5       H  nUUU4==   UUUU4   -  -  ss'   M     M}     [	        X5       H  nUUU4==   U-  ss'   M     O[	        X5       H
  nSUUU4'   M     XU4==   S-  ss'   GM	     [	        US-
  SS5       GH  nSn US-  nSn[	        USS5       HD  nUS-
  nU R                  U
U   5      U-   U:X  a  Sn  O!U R                  UU   5      U-   U:X  d  MD    O   U(       a  SnSn[	        WUS-   5       H  nUX   -  nX==   U-  ss'   U R                  U5      U-   U:X  a    OwXo   nU R                  UU5      nUXo'   SU-  nUU-  nU* U-  nU(       d  Mc  [	        U5       H1  nUUW4   nUUU4   nUU-  UU-  -   UUU4'   UU-  UU-  -
  UUU4'   M3     M     UU   nWU:X  aC  US:  a:  U* UU'   [        U[        5      (       d  [	        U5       H  nUUU4   * UUU4'   M     GMr  UU:  a  [        S	U-  5      eUU   n US-
  nUU   nU
U   nU
U   nUU-
  UU-   -  UU-
  UU-   -  -   SU-  U-  -  nU R                  US5      nUS:  a  U U-
  U U-   -  UUUU-   -  U-
  -  -   U -  nOU U-
  U U-   -  UUUU-
  -  U-
  -  -   U -  nS=nn[	        UUS-   5       GHL  nU
US-      nUUS-      nUU-  nUU-  nU R                  UU5      nUU
U'   UU-  nUU-  nU U-  UU-  -   nUU-  U U-  -
  nUU-  nUU-  n[        U[        5      (       dF  [	        U5       H7  n!UUU!4   n UUS-   U!4   nU U-  UU-  -   UUU!4'   UU-  U U-  -
  UUS-   U!4'   M9     U R                  UU5      nUUU'   US:w  a  SU-  nUU-  nUU-  nUU-  UU-  -   nUU-  UU-  -
  n U(       d  GM  [	        U5       H7  n!UU!U4   nUU!US-   4   nUU-  UU-  -   UU!U4'   UU-  UU-  -
  UU!US-   4'   M9     GMO     SU
U'   UU
U'   U UU'   GM     [	        U5       H  nUn"U R                  Xo   5      n[	        US-   U5       H#  nU R                  UU   5      nUU:  d  M  UnUn"M%     U"U:w  d  MV  Xo   nUU"   Xo'   UUU"'   U(       a,  [	        U5       H  nUUU4   nUUU"4   UUU4'   UUUU"4'   M     [        U[        5      (       a  M  [	        U5       H  nX/U4   nUU"U4   X/U4'   UUU"U4'   M     M     U$ )
a  
This routine computes the singular value decomposition of a matrix A.
Given A, two unitary matrices U and V are calculated such that

                A = U S V

where S is a suitable shaped matrix whose off-diagonal elements are zero.
The diagonal elements of S are the singular values of A, i.e. the
squareroots of the eigenvalues of A' A or A A'. Here ' denotes the hermitian
transpose (i.e. transposition and conjugation). Householder bidiagonalization
and a variant of the QR algorithm is used.

overview of the matrices :

  A : m*n       A gets replaced by U
  U : m*n       U replaces A. If n>m then only the first m*m block of U is
                non-zero. column-unitary: U' U = B
                here B is a n*n matrix whose first min(m,n) diagonal
                elements are 1 and all other elements are zero.
  S : n*n       diagonal matrix, only the diagonal elements are stored in
                the array S. only the first min(m,n) diagonal elements are non-zero.
  V : n*n       unitary: V V' = V' V = 1

parameters:
  A        (input/output) On input, A contains a complex matrix of shape m*n.
           On output, if calc_u is true A contains the column-unitary
           matrix U; otherwise A is simply used as workspace and thus destroyed.

  V        (input/output) if false, the matrix V is not calculated. otherwise
           V must be a matrix of shape n*n.

  calc_u   (input) If true, the matrix U is calculated and replaces A.
           if false, U is not calculated and A is simply destroyed

return value:
  S        an array of length n containing the singular values of A sorted by
           decreasing magnitude. only the first min(m,n) elements are non-zero.

This routine is a python translation of the fortran routine svd.f in the
software library EISPACK (see netlib.org) which itself is based on the
algol procedure svd described in:
  - num. math. 14, 403-420(1970) by golub and reinsch.
  - wilkinson/reinsch: handbook for auto. comp., vol ii-linear algebra, 134-151(1971).

r   r   rz   r   r   ra   TFr{   )r	   r|   rH   r2   r   r}   r    r!   r   r"   r~   r6   r7   r5   r4   )#r   r   r   r   r=   r   r   r   lbetarbetadworkr?   r   r   r   r   rA   r   arair$   rt   r   r   r   r   r   r<   r   rB   r   r:   rX   r   r   s#                                      r   	svd_c_rawr     s   ^ 66166q		!QA IIaODIIaOEIIaOEIIaOEA[FAY9A5A\#&&1Q3.1CHHSVVAacF^4LLL "zAacFeOF!A#B!A#Bb27**A	 &
 cFXXa[L66!9q=2+DA2+D&	Q*+QA#	 5LE1!QAA#A\SXXa!f-!A#66 *qA#A\!A#!a!f*, * (  AacFeOF & yA5Qa%ZAaC^#&&Q3.1CHHSVVAcF^4LLL $!QAcFeOFA#BA#Bb27**A	 (
 !eHXXa[L66!9q=2+DA2+D&	Q*+q5AaC%	 5LE1!QA1gDG (  !QAA#AaC^SXXa!f-!A#66 ,DA#AaC]!A#!d1g+- + (  !QAcFeOF ( E((14.388EH+==>] ` a!R$AAc!A#gJqSzQHHUX&!QAsVaZAcF (!QAA#AaC^SXXa!f-!A#66 ,#AaC^!A#!a!f*, ,	 ( AaC]"##!A#Q3 # %" #uquuaR(AAAaC^A# $AvE!QAA#AaC^SXXa!f-!A#66 ,CHHUX..A#A\!A#!a!f*, * (  AacFaKF &  AAacF &cFaKF% )( AaCR  1HCDAr2&U88E!H%-6 D88AbE?U*e3 ' 1Q3AEHAHMHxx{U*e3A		!QAADAAAAQAv!'A !!B$A !!A#A%&UQU]AadG%&UQU]AacF	 "+ (& !AAvq52AaD%a..!'A&'!fWAacF "+f}"#]`c#cdd!A1B"Ab	AaAa%AE"a!eA%661q519EA		!QA1uQUq1u-Q!a%[A4E0FF!KaUq1u-Q!a%[A4E0FF!KaIAArAv&!A#JacFEEIIaOaEEEAEMEAEMEQ!!T**$Qia"fIac"fI%&UQU]!(%&UQU]!A#b&	 (
 IIaO!6AAAAAAEAEMEAEM6$QibdIb1fI%&UQU]"Q$%&UQU]"QqS&	 (9 'D E!HE!HAaDC  !X AYHHQTNAq!A1A1u	 " 19 AT7ADAdGA!A#AqvYAacF !AafI #
 a&&AA#AtAvYAcF !Ad1fI #1 : Hr   c                 
   UR                   UR                  peU(       d2  U(       d  UR                  5       n[        XSSS9nUS[	        XV5       nU$ U(       aP  Xe:  aK  U R                  XU5      nU R                  XU5      n	XSS2SU24'   [        X	USS9nUSU nUSU2SU24   nXU4$ U(       d  UR                  5       nU R                  Xf5      n[        XUSS9nXe:  a!  US:X  a  USU2SS24   nUSU nUSS2SU24   nXU4$ )a  
This routine computes the singular value decomposition of a matrix A.
Given A, two orthogonal matrices U and V are calculated such that

       A = U S V        and        U' U = 1         and         V V' = 1

where S is a suitable shaped matrix whose off-diagonal elements are zero.
Here ' denotes the transpose. The diagonal elements of S are the singular
values of A, i.e. the squareroots of the eigenvalues of A' A or A A'.

input:
  A             : a real matrix of shape (m, n)
  full_matrices : if true, U and V are of shape (m, m) and (n, n).
                  if false, U and V are of shape (m, min(m, n)) and (min(m, n), n).
  compute_uv    : if true, U and V are calculated. if false, only S is calculated.
  overwrite_a   : if true, allows modification of A which may improve
                  performance. if false, A is not modified.

output:
  U : an orthogonal matrix: U' U = 1. if full_matrices is true, U is of
      shape (m, m). ortherwise it is of shape (m, min(m, n)).

  S : an array of length min(m, n) containing the singular values of A sorted by
      decreasing magnitude.

  V : an orthogonal matrix: V V' = 1. if full_matrices is true, V is of
      shape (n, n). ortherwise it is of shape (min(m, n), n).

return value:

       S          if compute_uv is false
   (U, S, V)      if compute_uv is true

overview of the matrices:

  full_matrices true:
    A           : m*n
    U           : m*m     U' U  = 1
    S as matrix : m*n
    V           : n*n     V  V' = 1

 full_matrices false:
    A           : m*n
    U           : m*min(n,m)             U' U  = 1
    S as matrix : min(m,n)*min(m,n)
    V           : min(m,n)*n             V  V' = 1

examples:

   >>> from mpmath import mp
   >>> A = mp.matrix([[2, -2, -1], [3, 4, -2], [-2, -2, 0]])
   >>> S = mp.svd_r(A, compute_uv = False)
   >>> print(S)
   [6.0]
   [3.0]
   [1.0]

   >>> U, S, V = mp.svd_r(A)
   >>> print(mp.chop(A - U * mp.diag(S) * V))
   [0.0  0.0  0.0]
   [0.0  0.0  0.0]
   [0.0  0.0  0.0]


see also: svd, svd_c
Fr   r   NTr   )r	   r|   rG   r   minrH   
r   r   full_matrices
compute_uvrJ   r=   r   r   r   A0s
             r   svd_rr     s   J 66166qAc%%8is1xLIIaOYYq_1RaR4cq40bqEbqb!eHqzAIIaOca$/5%bqbdG"1A!BQB$Aayr   c                 
   UR                   UR                  peU(       d2  U(       d  UR                  5       n[        XSSS9nUS[	        XV5       nU$ U(       aP  Xe:  aK  U R                  XU5      nU R                  XU5      n	XSS2SU24'   [        X	USS9nUSU nUSU2SU24   nXU4$ U(       d  UR                  5       nU R                  Xf5      n[        XUSS9nXe:  a!  US:X  a  USU2SS24   nUSU nUSS2SU24   nXU4$ )a[  
This routine computes the singular value decomposition of a matrix A.
Given A, two unitary matrices U and V are calculated such that

       A = U S V        and        U' U = 1         and         V V' = 1

where S is a suitable shaped matrix whose off-diagonal elements are zero.
Here ' denotes the hermitian transpose (i.e. transposition and complex
conjugation). The diagonal elements of S are the singular values of A,
i.e. the squareroots of the eigenvalues of A' A or A A'.

input:
  A             : a complex matrix of shape (m, n)
  full_matrices : if true, U and V are of shape (m, m) and (n, n).
                  if false, U and V are of shape (m, min(m, n)) and (min(m, n), n).
  compute_uv    : if true, U and V are calculated. if false, only S is calculated.
  overwrite_a   : if true, allows modification of A which may improve
                  performance. if false, A is not modified.

output:
  U : an unitary matrix: U' U = 1. if full_matrices is true, U is of
      shape (m, m). ortherwise it is of shape (m, min(m, n)).

  S : an array of length min(m, n) containing the singular values of A sorted by
      decreasing magnitude.

  V : an unitary matrix: V V' = 1. if full_matrices is true, V is of
      shape (n, n). ortherwise it is of shape (min(m, n), n).

return value:

       S          if compute_uv is false
   (U, S, V)      if compute_uv is true

overview of the matrices:

  full_matrices true:
    A           : m*n
    U           : m*m     U' U  = 1
    S as matrix : m*n
    V           : n*n     V  V' = 1

 full_matrices false:
    A           : m*n
    U           : m*min(n,m)             U' U  = 1
    S as matrix : min(m,n)*min(m,n)
    V           : min(m,n)*n             V  V' = 1

example:
  >>> from mpmath import mp
  >>> A = mp.matrix([[-2j, -1-3j, -2+2j], [2-2j, -1-3j, 1], [-3+1j,-2j,0]])
  >>> S = mp.svd_c(A, compute_uv = False)
  >>> print(mp.chop(S - mp.matrix([mp.sqrt(34), mp.sqrt(15), mp.sqrt(6)])))
  [0.0]
  [0.0]
  [0.0]

  >>> U, S, V = mp.svd_c(A)
  >>> print(mp.chop(A - U * mp.diag(S) * V))
  [0.0  0.0  0.0]
  [0.0  0.0  0.0]
  [0.0  0.0  0.0]

see also: svd, svd_r
Fr   NTr   )r	   r|   rG   r   r   rH   r   s
             r   svd_cr   [  s   H 66166qAc%%8is1xLIIaOYYq_1RaR4cq40bqEbqb!eHqzAIIaOca$/5%bqbdG"1A!BQB$Aayr   c                 |   ^  [        U 4S jU 5       5      nU(       a  T R                  XX4S9$ T R                  XX4S9$ )a  
"svd" is a unified interface for "svd_r" and "svd_c". Depending on
whether A is real or complex the appropriate function is called.

This routine computes the singular value decomposition of a matrix A.
Given A, two orthogonal (A real) or unitary (A complex) matrices U and V
are calculated such that

       A = U S V        and        U' U = 1         and         V V' = 1

where S is a suitable shaped matrix whose off-diagonal elements are zero.
Here ' denotes the hermitian transpose (i.e. transposition and complex
conjugation). The diagonal elements of S are the singular values of A,
i.e. the squareroots of the eigenvalues of A' A or A A'.

input:
  A             : a real or complex matrix of shape (m, n)
  full_matrices : if true, U and V are of shape (m, m) and (n, n).
                  if false, U and V are of shape (m, min(m, n)) and (min(m, n), n).
  compute_uv    : if true, U and V are calculated. if false, only S is calculated.
  overwrite_a   : if true, allows modification of A which may improve
                  performance. if false, A is not modified.

output:
  U : an orthogonal or unitary matrix: U' U = 1. if full_matrices is true, U is of
      shape (m, m). ortherwise it is of shape (m, min(m, n)).

  S : an array of length min(m, n) containing the singular values of A sorted by
      decreasing magnitude.

  V : an orthogonal or unitary matrix: V V' = 1. if full_matrices is true, V is of
      shape (n, n). ortherwise it is of shape (min(m, n), n).

return value:

       S          if compute_uv is false
   (U, S, V)      if compute_uv is true

overview of the matrices:

  full_matrices true:
    A           : m*n
    U           : m*m     U' U  = 1
    S as matrix : m*n
    V           : n*n     V  V' = 1

 full_matrices false:
    A           : m*n
    U           : m*min(n,m)             U' U  = 1
    S as matrix : min(m,n)*min(m,n)
    V           : min(m,n)*n             V  V' = 1

examples:

   >>> from mpmath import mp
   >>> A = mp.matrix([[2, -2, -1], [3, 4, -2], [-2, -2, 0]])
   >>> S = mp.svd(A, compute_uv = False)
   >>> print(S)
   [6.0]
   [3.0]
   [1.0]

   >>> U, S, V = mp.svd(A)
   >>> print(mp.chop(A - U * mp.diag(S) * V))
   [0.0  0.0  0.0]
   [0.0  0.0  0.0]
   [0.0  0.0  0.0]

see also: svd_r, svd_c
c              3   R   >#    U  H  n[        U5      TR                  L v   M     g 7frR   rS   rV   s     r   rY   svd.<locals>.<genexpr>
  r[   r\   )r   r   rJ   )r]   r   r   )r   r   r   r   rJ   r^   s   `     r   svdr     s?    R 222Iyy
ynnyy
ynnr   N)T)F)FF)rb   r   r   )FTF)__doc__libmp.backendr   eigenr   r   r)   r,   r/   rE   rK   rO   r_   rx   r   r   r   r   r    r   r   <module>r      s   6 # x|NF)J{~ E EP I IV FT FTR d dTgV	AJ
 d dP c cJ Mo Mor   