
    \hKb                         S SK Jr  S SKJrJrJr  S SKJr  SSKJ	r	J
r
Jr  SSKJr  SSKJrJr  S rS	 rS
 rS rS rS rS r\4S jrS rSS jrSS jrSS jrSS jrSS jrg)    )
expand_mul)Dummyuniquely_named_symbolsymbols)numbered_symbols   )
ShapeErrorNonSquareMatrixErrorNonInvertibleMatrixError)_fuzzy_positive_definite)_get_intermediate_simp_iszeroc                    ^ ^ T R                  5       (       d  [        S5      eTR                  T R                  :w  a  [        S5      eT R                  TR                  TR                  U U4S j5      $ )a  Solves ``Ax = B`` efficiently, where A is a diagonal Matrix,
with non-zero diagonal entries.

Examples
========

>>> from sympy import Matrix, eye
>>> A = eye(2)*2
>>> B = Matrix([[1, 2], [3, 4]])
>>> A.diagonal_solve(B) == B/2
True

See Also
========

sympy.matrices.dense.DenseMatrix.lower_triangular_solve
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
gauss_jordan_solve
cholesky_solve
LDLsolve
LUsolve
QRsolve
pinv_solve
cramer_solve
zMatrix should be diagonalzSize mismatchc                    > TX4   TX 4   -  $ N )ijMrhss     N/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/solvers.py<lambda>!_diagonal_solve.<locals>.<lambda>+   s    QTQqtW)<    )is_diagonal	TypeErrorrows_newcols)r   r   s   ``r   _diagonal_solver    
   sX    6 ==??344
xx166((66#((<> >r   c                 f  ^ ^^^ SSK Jn  T R                  (       d  [        S5      eUR                  T R                  :w  a  [        S5      eT R                  (       d  [        S5      e[        5       nUR                  T R                  UR                  5      m[        UR                  5       Hq  m[        T R                  5       HU  mT TT4   S:X  a  [        S5      eU" UTT4   [        U UUU4S j[        T5       5       5      -
  T TT4   -  5      TTT4'   MW     Ms     T R                  T5      $ )	Solves ``Ax = B``, where A is a lower triangular matrix.

See Also
========

upper_triangular_solve
gauss_jordan_solve
cholesky_solve
diagonal_solve
LDLsolve
LUsolve
QRsolve
pinv_solve
cramer_solve
r   MutableDenseMatrixMatrix must be square.Matrices size mismatch. Matrix must be lower triangular.r   Matrix must be non-singular.c              3   B   >#    U  H  nTTU4   TUT4   -  v   M     g 7fr   r   .0kr   Xr   r   s     r   	<genexpr>*_lower_triangular_solve.<locals>.<genexpr>P   s,      +;19A ,-QT71QT7?19   )denser$   	is_squarer
   r   r	   is_lower
ValueErrorr   zerosr   ranger   sumr   r   r   r$   dpsr-   r   r   s   `   @@@r   _lower_triangular_solver:   .   s   " *;;"#;<<
xx166233::;<<
 
"C

"
"166388
4A388_qvvAAw!| >??3q!t9s +;16q+; (; ;>?1gF GAadG	   66!9r   c           
         U R                   (       d  [        S5      eUR                  U R                  :w  a  [        S5      eU R                  (       d  [        S5      e[        5       n[        U R                  5       Vs/ s H  n/ PM     nnU R                  5        H!  u  p5nX5:  d  M  XC   R                  XV45        M#     UR                  5       n[        UR                  5       HW  n[        UR                  5       H;  nXC    H  u  pXsU4==   XgX4   -  -  ss'   M     U" XsU4   XU4   -  5      XsU4'   M=     MY     U R                  U5      $ s  snf )r"   r%   r&   r'   )r2   r
   r   r	   r3   r4   r   r6   row_listappend
as_mutabler   r   	r   r   r9   r   r   r   vr-   us	            r   _lower_triangular_solve_sparserB   U   s#   " ;;"#;<<
xx166233::;<<!#Caff&1BD&::<a5GNNA6"   	A388_sxxAQ$1qtW9$   !qD'AdG+,AdG	 !  66!9 's   ?Ec                   ^ ^^^ SSK Jn  T R                  (       d  [        S5      eUR                  T R                  :w  a  [        S5      eT R                  (       d  [        S5      e[        5       nUR                  T R                  UR                  5      m[        UR                  5       H  m[        [        T R                  5      5       Hc  mT TT4   S:X  a  [        S5      eU" UTT4   [        U UUU4S j[        TS-   T R                  5       5       5      -
  T TT4   -  5      TTT4'   Me     M     T R                  T5      $ )	Solves ``Ax = B``, where A is an upper triangular matrix.

See Also
========

lower_triangular_solve
gauss_jordan_solve
cholesky_solve
diagonal_solve
LDLsolve
LUsolve
QRsolve
pinv_solve
cramer_solve
r   r#   r%   Matrix size mismatch.Matrix is not upper triangular.r   r(   c              3   B   >#    U  H  nTTU4   TUT4   -  v   M     g 7fr   r   r*   s     r   r.   *_upper_triangular_solve.<locals>.<genexpr>   s-      +G1EA ,-QT71QT7?1Er0   )r1   r$   r2   r
   r   r	   is_upperr   r   r5   r   r6   reversedr4   r7   r   r8   s   `   @@@r   _upper_triangular_solverK      s   " *;;"#;<<
xx166011::9::
 
"C

"
"166388
4A388_%-(AAw!| !?@@3q!t9s +G16q1uaff1E+G (G GJKAqD'R SAadG	 )  66!9r   c           
         U R                   (       d  [        S5      eUR                  U R                  :w  a  [        S5      eU R                  (       d  [        S5      e[        5       n[        U R                  5       Vs/ s H  n/ PM     nnU R                  5        H!  u  p5nX5:  d  M  XC   R                  XV45        M#     UR                  5       n[        UR                  5       Hi  n[        [        UR                  5      5       HD  n[        XC   5       H  u  pXsU4==   XgX4   -  -  ss'   M     U" XsU4   XU4   -  5      XsU4'   MF     Mk     U R                  U5      $ s  snf )rD   r%   rE   rF   )r2   r
   r   r	   rI   r   r   r6   r<   r=   r>   r   rJ   r   r?   s	            r   _upper_triangular_solve_sparserM      s-   " ;;"#;<<
xx166011::9::!#Caff&1BD&::<a5GNNA6"   	A388_%/*A )Q$1qtW9$ * !qD'AdG+,AdG	 +  66!9 's   ?E%c                    U R                   U R                  :  a  [        S5      eSnSnU R                  5       (       a  SnOU R                  (       d  SnU(       d  [        U 5      SL aC  U R                  nUR                  U 5      n UR                  U5      nU R                  5       (       + nU R                  US9nUR                  U5      nU(       a  UR                  R                  U5      $ UR                  R                  U5      $ )a  Solves ``Ax = B`` using Cholesky decomposition,
for a general square non-singular matrix.
For a non-square matrix with rows > cols,
the least squares solution is returned.

See Also
========

sympy.matrices.dense.DenseMatrix.lower_triangular_solve
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
gauss_jordan_solve
diagonal_solve
LDLsolve
LUsolve
QRsolve
pinv_solve
cramer_solve
6Under-determined System. Try M.gauss_jordan_solve(rhs)TF	hermitian)r   r   NotImplementedErroris_symmetricis_hermitianr   Hmultiplycholeskylower_triangular_solveupper_triangular_solveT)r   r   rQ   reformrU   LYs          r   _cholesky_solver^      s    ( 	vv!DF 	F IF~~	^^)!,5CCJJqMJJsO((		

Y
'A	  %A++A..++A..r   c                 :   U R                   U R                  :  a  [        S5      eSnSnU R                  5       (       a  SnOU R                  (       d  SnU(       d  [        U 5      SL aC  U R                  nUR                  U 5      n UR                  U5      nU R                  5       (       + nU R                  US9u  pVUR                  U5      nUR                  U5      nU(       a  UR                  R                  U5      $ UR                  R                  U5      $ )aA  Solves ``Ax = B`` using LDL decomposition,
for a general square and non-singular matrix.

For a non-square matrix with rows > cols,
the least squares solution is returned.

Examples
========

>>> from sympy import Matrix, eye
>>> A = eye(2)*2
>>> B = Matrix([[1, 2], [3, 4]])
>>> A.LDLsolve(B) == B/2
True

See Also
========

sympy.matrices.dense.DenseMatrix.LDLdecomposition
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
gauss_jordan_solve
cholesky_solve
diagonal_solve
LUsolve
QRsolve
pinv_solve
cramer_solve
rO   TFrP   )r   r   rR   rS   rT   r   rU   rV   LDLdecompositionrX   diagonal_solverY   rZ   )	r   r   rQ   r[   rU   r\   Dr]   Zs	            r   	_LDLsolverd     s    > 	vv!DF 	F IF~~	^^)!,5CCJJqMJJsO((		2DA##C(AAA++A..++A..r   c                   ^
^ UR                   U R                   :w  a  [        S5      eU R                   nU R                  nX4:  a  [        S5      e U R	                  USS9u  pV[        5       m
UR                  U5      R                  5       n[        U5       H;  n[        [        X5      5       H   n	XXU	4   mUR                  XU
U4S j5        M"     M=     X4:  aU  [        XC5       H;  n[        UR                  5       H  n	U" XxU	4   5      (       a  M  [        S5      e   M=     USU2S	S	24   n[        US
-
  SS5       HS  n[        US
-   U5       H   n	XXU	4   mUR                  XU
U4S j5        M"     XXU4   mUR                  UU
U4S j5        MU     UR                  U5      $ ! [
         a    [        S5      ef = f)a  Solve the linear system ``Ax = rhs`` for ``x`` where ``A = M``.

This is for symbolic matrices, for real or complex ones use
mpmath.lu_solve or mpmath.qr_solve.

See Also
========

sympy.matrices.dense.DenseMatrix.lower_triangular_solve
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
gauss_jordan_solve
cholesky_solve
diagonal_solve
LDLsolve
QRsolve
pinv_solve
LUdecomposition
cramer_solve
z4``M`` and ``rhs`` must have the same number of rows.z&Underdetermined systems not supported.T)
iszerofunc	rankcheck Matrix det == 0; not invertible.c                     > T" U TU-  -
  5      $ r   r   xyr9   scales     r   r   _LUsolve.<locals>.<lambda>h      CEAI,>r   zThe system is inconsistent.r   Nr   c                     > T" U TU-  -
  5      $ r   r   rj   s     r   r   rn   w  ro   r   c                     > T" TS-  U -  5      $ )Nrp   r   )rk   _r9   rm   s     r   r   rn   z  s    UBY]!3r   )r   r	   r   rR   LUdecomposition_Simpler4   r   r   permute_rowsr>   r6   min
zip_row_oprow_op	__class__)r   r   rf   mnApermbr   r   r9   rm   s             @@r   _LUsolver   <  s   * xx166BD 	D 	
A	Au!"JKKK**!T + 3
 !
"C


4
 
+
+
-A 1Xs1y!AdGELL>? "  	uqA166]!!qD'**$%BCC # 
 ac1fI 1q5"b!q1uaAdGELL>? ! Q$	34 " ===  K&'IJJKs   F( (F>c                    [        [        [        5      nU R                  5       u  p4UR                  U-  n/ nUR                  n[        US-
  SS5       HS  nXXSS24   n	[        US-   U5       H  n
XX4   XgS-
  U
-
     -  -  n	M     U" U	5      n	UR                  XX4   -  5        MU     U R                  " USSS2   6 $ )aQ  Solve the linear system ``Ax = b``.

``M`` is the matrix ``A``, the method argument is the vector
``b``.  The method returns the solution vector ``x``.  If ``b`` is a
matrix, the system is solved for each column of ``b`` and the
return value is a matrix of the same shape as ``b``.

This method is slower (approximately by a factor of 2) but
more stable for floating-point arithmetic than the LUsolve method.
However, LUsolve usually uses an exact arithmetic, so you do not need
to use QRsolve.

This is mainly for educational purposes and symbolic matrices, for real
(or complex) matrices use mpmath.qr_solve.

See Also
========

sympy.matrices.dense.DenseMatrix.lower_triangular_solve
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
gauss_jordan_solve
cholesky_solve
diagonal_solve
LDLsolve
LUsolve
pinv_solve
QRdecomposition
cramer_solve
r   rp   N)r   r   QRdecompositionrZ   r   r6   r=   vstack)r   r~   r9   QRrl   rk   r{   r   tmpr,   s              r   _QRsolver     s    > "*j9CDA337A
 	A	A1q5"b!1gq1uaAQT7Q1uqy\))C ! #h	w " 88QttWr   c                 
  ^ SSK JnJn  U R                  nU R	                  U R                  5       UR                  5       5      nUR                  nUSS2SU* 24   R                  u  nmUR                  SS9u  pU	SS2SU* 24   U	SS2U* S24   p[        [        U4S jU
5      5      n
[        U
5      n[        U	R                  5       Vs/ s H  oU
;  d  M
  UPM     nnU" X-   5      R                  nXS2SS24   R                  (       d  [        S5      e[!        SU/S	 S
 S9R"                  n[%        U5      nU" [        TU-
  U-  5       Vs/ s H  n['        U5      PM     sn5      R)                  TU-
  U5      nU	SU2U4   nUSU2SS24   nUR+                  UUU-  -
  U5      nU" TU5      n[        T5       H  nUUSS24   UUU   SS24'   M     U" U5      U" U5      nnU(       a  UUU4$ UU4$ s  snf s  snf )a}  
Solves ``Ax = B`` using Gauss Jordan elimination.

There may be zero, one, or infinite solutions.  If one solution
exists, it will be returned. If infinite solutions exist, it will
be returned parametrically. If no solutions exist, It will throw
ValueError.

Parameters
==========

B : Matrix
    The right hand side of the equation to be solved for.  Must have
    the same number of rows as matrix A.

freevar : boolean, optional
    Flag, when set to `True` will return the indices of the free
    variables in the solutions (column Matrix), for a system that is
    undetermined (e.g. A has more columns than rows), for which
    infinite solutions are possible, in terms of arbitrary
    values of free variables. Default `False`.

Returns
=======

x : Matrix
    The matrix that will satisfy ``Ax = B``.  Will have as many rows as
    matrix A has columns, and as many columns as matrix B.

params : Matrix
    If the system is underdetermined (e.g. A has more columns than
    rows), infinite solutions are possible, in terms of arbitrary
    parameters. These arbitrary parameters are returned as params
    Matrix.

free_var_index : List, optional
    If the system is underdetermined (e.g. A has more columns than
    rows), infinite solutions are possible, in terms of arbitrary
    values of free variables. Then the indices of the free variables
    in the solutions (column Matrix) are returned by free_var_index,
    if the flag `freevar` is set to `True`.

Examples
========

>>> from sympy import Matrix
>>> A = Matrix([[1, 2, 1, 1], [1, 2, 2, -1], [2, 4, 0, 6]])
>>> B = Matrix([7, 12, 4])
>>> sol, params = A.gauss_jordan_solve(B)
>>> sol
Matrix([
[-2*tau0 - 3*tau1 + 2],
[                 tau0],
[           2*tau1 + 5],
[                 tau1]])
>>> params
Matrix([
[tau0],
[tau1]])
>>> taus_zeroes = { tau:0 for tau in params }
>>> sol_unique = sol.xreplace(taus_zeroes)
>>> sol_unique
    Matrix([
[2],
[0],
[5],
[0]])


>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
>>> B = Matrix([3, 6, 9])
>>> sol, params = A.gauss_jordan_solve(B)
>>> sol
Matrix([
[-1],
[ 2],
[ 0]])
>>> params
Matrix(0, 1, [])

>>> A = Matrix([[2, -7], [-1, 4]])
>>> B = Matrix([[-21, 3], [12, -2]])
>>> sol, params = A.gauss_jordan_solve(B)
>>> sol
Matrix([
[0, -2],
[3, -1]])
>>> params
Matrix(0, 2, [])


>>> from sympy import Matrix
>>> A = Matrix([[1, 2, 1, 1], [1, 2, 2, -1], [2, 4, 0, 6]])
>>> B = Matrix([7, 12, 4])
>>> sol, params, freevars = A.gauss_jordan_solve(B, freevar=True)
>>> sol
Matrix([
[-2*tau0 - 3*tau1 + 2],
[                 tau0],
[           2*tau1 + 5],
[                 tau1]])
>>> params
Matrix([
[tau0],
[tau1]])
>>> freevars
[1, 3]


See Also
========

sympy.matrices.dense.DenseMatrix.lower_triangular_solve
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
cholesky_solve
diagonal_solve
LDLsolve
LUsolve
QRsolve
pinv

References
==========

.. [1] https://en.wikipedia.org/wiki/Gaussian_elimination

r   )Matrixr5   NT)simplifyc                    > U T:  $ r   r   )pcols    r   r   %_gauss_jordan_solve.<locals>.<lambda>@  s	    a#gr   zLinear system has no solutiontauc                 6    [        U 5      R                  S5      $ )N
1234567890)strrstrip)r   s    r   r   r   R  s    c!fmmL9r   c                     SU -   $ )Nrs   r   )ss    r   r   r   S  s    S1Wr   )comparemodify)sympy.matricesr   r5   ry   hstackcopyr   shaperreflistfilterlenr6   rZ   is_zero_matrixr4   r   namer   nextreshaper   )r   Bfreevarr   r5   clsaugB_colsrowr|   pivotsr@   rankcfree_var_indexpermutationr   genr,   r   Vvtfree_solsolr   s                           @r   _gauss_jordan_solver     s   B -{{Cxx!&&(+CvvF1hwh;%%HC $'IA!XvgX+!fWX+qV-v67FFD "'qvvBA6/aNB 0133K UAX;%%899 !9$&&*d 	 D!CeS4Z,?&@A&@49&@ABJJ$J C $&'A${Bzz"q3w,,H V
C3Z!)!A#KNA  3xSCC''CxG C  Bs   	G;G;H Nc                 b   SSK Jn  U nU R                  5       nUcP  UR                  UR                  pv[	        SR                  Xg5      [        S9nU R                  XvU5      R                  nUR                  U5      U" UR                  5      UR                  U5      -
  R                  U5      -   $ )a  Solve ``Ax = B`` using the Moore-Penrose pseudoinverse.

There may be zero, one, or infinite solutions.  If one solution
exists, it will be returned.  If infinite solutions exist, one will
be returned based on the value of arbitrary_matrix.  If no solutions
exist, the least-squares solution is returned.

Parameters
==========

B : Matrix
    The right hand side of the equation to be solved for.  Must have
    the same number of rows as matrix A.
arbitrary_matrix : Matrix
    If the system is underdetermined (e.g. A has more columns than
    rows), infinite solutions are possible, in terms of an arbitrary
    matrix.  This parameter may be set to a specific matrix to use
    for that purpose; if so, it must be the same shape as x, with as
    many rows as matrix A has columns, and as many columns as matrix
    B.  If left as None, an appropriate matrix containing dummy
    symbols in the form of ``wn_m`` will be used, with n and m being
    row and column position of each symbol.

Returns
=======

x : Matrix
    The matrix that will satisfy ``Ax = B``.  Will have as many rows as
    matrix A has columns, and as many columns as matrix B.

Examples
========

>>> from sympy import Matrix
>>> A = Matrix([[1, 2, 3], [4, 5, 6]])
>>> B = Matrix([7, 8])
>>> A.pinv_solve(B)
Matrix([
[ _w0_0/6 - _w1_0/3 + _w2_0/6 - 55/18],
[-_w0_0/3 + 2*_w1_0/3 - _w2_0/3 + 1/9],
[ _w0_0/6 - _w1_0/3 + _w2_0/6 + 59/18]])
>>> A.pinv_solve(B, arbitrary_matrix=Matrix([0, 0, 0]))
Matrix([
[-55/18],
[   1/9],
[ 59/18]])

See Also
========

sympy.matrices.dense.DenseMatrix.lower_triangular_solve
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
gauss_jordan_solve
cholesky_solve
diagonal_solve
LDLsolve
LUsolve
QRsolve
pinv

Notes
=====

This may return either exact solutions or least squares solutions.
To determine which, check ``A * A.pinv() * B == B``.  It will be
True if exact solutions exist, and False if only a least-squares
solution exists.  Be aware that the left hand side of that equation
may need to be simplified to correctly compare to the right hand
side.

References
==========

.. [1] https://en.wikipedia.org/wiki/Moore-Penrose_pseudoinverse#Obtaining_all_solutions_of_a_linear_system

r   )eyezw:{}_:{})r   )
r   r   pinvr   r   formatr   ry   rZ   rV   )	r   r   arbitrary_matrixr   r|   A_pinvr   r   ws	            r   _pinv_solver   k  s    \ #AVVXF66166d":#4#4T#@eL;;t1577??1QVVOOA" ()9 :; ;r   c                   ^ ^^^
^ SSK Jn  U U
UU4S jnTS:X  a	  SSKJn  UnO-TS:X  a	  SSKJn  UnO[        T[        5      (       a  U4S jnOTnU" T 5      nU" TR                  6 n	[        TR                  S   5       HN  m[        TR                  S	   5       H/  m
U" T R                  " / T R                  QUP76 5      U-  U	T
T4'   M1     MP     T R                  U	5      $ )
a  Solves system of linear equations using Cramer's rule.

This method is relatively inefficient compared to other methods.
However it only uses a single division, assuming a division-free determinant
method is provided. This is helpful to minimize the chance of divide-by-zero
cases in symbolic solutions to linear systems.

Parameters
==========
M : Matrix
    The matrix representing the left hand side of the equation.
rhs : Matrix
    The matrix representing the right hand side of the equation.
det_method : str or callable
    The method to use to calculate the determinant of the matrix.
    The default is ``'laplace'``.  If a callable is passed, it should take a
    single argument, the matrix, and return the determinant of the matrix.

Returns
=======
x : Matrix
    The matrix that will satisfy ``Ax = B``.  Will have as many rows as
    matrix A has columns, and as many columns as matrix B.

Examples
========

>>> from sympy import Matrix
>>> A = Matrix([[0, -6, 1], [0, -6, -1], [-5, -2, 3]])
>>> B = Matrix([[-30, -9], [-18, -27], [-26, 46]])
>>> x = A.cramer_solve(B)
>>> x
Matrix([
[ 0, -5],
[ 4,  3],
[-6,  9]])

References
==========

.. [1] https://en.wikipedia.org/wiki/Cramer%27s_rule#Explicit_formulas_for_small_systems

r   )r5   c                 *   > UT:X  a  TU T4   $ TX4   $ r   r   )r   r   r   r   r   r   s     r   entry_cramer_solve.<locals>.entry  s"    3hs1c6{3AadG3r   bird)	_det_birdlaplace)_det_laplacec                 "   > U R                  TS9$ )Nmethod)det)matrix
det_methods    r   r   _cramer_solve.<locals>.<lambda>  s    VZZzZ:r   r   )
r1   r5   determinantr   r   
isinstancer   r   r6   ry   )r   r   r   r5   r   r   r   r   det_Mrk   r   r   s   ```       @@r   _cramer_solver     s    X 4 4 V*	y	 -	J	$	$:FEsyyASYYq\"1&Cakk:177:E:;eCAc3hK ' # ;;q>r   c                    US;   a)   U R                  U5      u  p4U(       a  [        S5      e U$ US:X  a  U R                  U5      $ US:X  a  U R	                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US	:X  a  U R                  U5      $ U R                  US
9R                  U5      $ ! [         a    [        S5      ef = f)a  Solves linear equation where the unique solution exists.

Parameters
==========

rhs : Matrix
    Vector representing the right hand side of the linear equation.

method : string, optional
    If set to ``'GJ'`` or ``'GE'``, the Gauss-Jordan elimination will be
    used, which is implemented in the routine ``gauss_jordan_solve``.

    If set to ``'LU'``, ``LUsolve`` routine will be used.

    If set to ``'QR'``, ``QRsolve`` routine will be used.

    If set to ``'PINV'``, ``pinv_solve`` routine will be used.

    If set to ``'CRAMER'``, ``cramer_solve`` routine will be used.

    It also supports the methods available for special linear systems

    For positive definite systems:

    If set to ``'CH'``, ``cholesky_solve`` routine will be used.

    If set to ``'LDL'``, ``LDLsolve`` routine will be used.

    To use a different method and to compute the solution via the
    inverse, use a method defined in the .inv() docstring.

Returns
=======

solutions : Matrix
    Vector representing the solution.

Raises
======

ValueError
    If there is not a unique solution then a ``ValueError`` will be
    raised.

    If ``M`` is not square, a ``ValueError`` and a different routine
    for solving the system will be suggested.
)GJGEzcMatrix det == 0; not invertible. Try ``M.gauss_jordan_solve(rhs)`` to obtain a parametric solution.rh   LUCHQRLDLPINVCRAMERr   )gauss_jordan_solver   r4   LUsolvecholesky_solveQRsolveLDLsolve
pinv_solvecramer_solveinvrV   )r   r   r   solnparams        r   _solver   
  s	   b 	O..s3KD. 0U V V  	4yy~	4$$	4yy~	5zz#	6	||C  	8	~~c""uuFu#,,S11%  	O*+MNN	Os   %C C.c                     US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ U R                  nX0-  R                  X1-  US9$ )a`  Return the least-square fit to the data.

Parameters
==========

rhs : Matrix
    Vector representing the right hand side of the linear equation.

method : string or boolean, optional
    If set to ``'CH'``, ``cholesky_solve`` routine will be used.

    If set to ``'LDL'``, ``LDLsolve`` routine will be used.

    If set to ``'QR'``, ``QRsolve`` routine will be used.

    If set to ``'PINV'``, ``pinv_solve`` routine will be used.

    Otherwise, the conjugate of ``M`` will be used to create a system
    of equations that is passed to ``solve`` along with the hint
    defined by ``method``.

Returns
=======

solutions : Matrix
    Vector representing the solution.

Examples
========

>>> from sympy import Matrix, ones
>>> A = Matrix([1, 2, 3])
>>> B = Matrix([2, 3, 4])
>>> S = Matrix(A.row_join(B))
>>> S
Matrix([
[1, 2],
[2, 3],
[3, 4]])

If each line of S represent coefficients of Ax + By
and x and y are [2, 3] then S*xy is:

>>> r = S*Matrix([2, 3]); r
Matrix([
[ 8],
[13],
[18]])

But let's add 1 to the middle value and then solve for the
least-squares value of xy:

>>> xy = S.solve_least_squares(Matrix([8, 14, 18])); xy
Matrix([
[ 5/3],
[10/3]])

The error is given by S*xy - r:

>>> S*xy - r
Matrix([
[1/3],
[1/3],
[1/3]])
>>> _.norm().n(2)
0.58

If a different xy is used, the norm will be higher:

>>> xy += ones(2, 1)/10
>>> (S*xy - r).norm().n(2)
1.5

r   r   r   r   r   )r   r   r   r   rU   solve)r   r   r   ts       r   _solve_least_squaresr   X  s~    X ~$$	4yy~	5zz#	6	||C  CC}}QWV}44r   )Fr   )r   )r   )r   )sympy.core.functionr   sympy.core.symbolr   r   r   sympy.utilities.iterablesr   
exceptionsr	   r
   r   eigenr   	utilitiesr   r   r    r:   rB   rK   rM   r^   rd   r   r   r   r   r   r   r   r   r   r   <module>r      s{    * C C 6 R R + 6!>H%N(V%N(V,/^8/v !( @F3lslY;x@FK2\V5r   