� ��gGb����ddlmZddlmZmZmZddlmZddlm Z m Z m Z ddl m Z ddlmZmZd�Zd �Zd �Zd �Zd �Zd �Zd�Zefd�Zd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS) �)� expand_mul)�Dummy�uniquely_named_symbol�symbols)�numbered_symbols�)� ShapeError�NonSquareMatrixError�NonInvertibleMatrixError)�_fuzzy_positive_definite)�_get_intermediate_simp�_iszeroc���������std����j�jkrtd������j�j��fd���S)aSolves ``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 zMatrix should be diagonalz Size mismatchc�.���||f�||fz S�N�)�i�j�M�rhss ���f/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/matrices/solvers.py�<lambda>z!_diagonal_solve.<locals>.<lambda>+s����Q��T��Q�q�!�t�W�)<��)� is_diagonal� TypeError�rows�_new�cols)rrs``r�_diagonal_solver sp����6 �=�=�?�?�5��3�4�4�4� �x�1�6�����(�(�(� �6�6� ��#�(�<�<�<�<�<� >� >�>rc �f�����ddlm}�jstd���|j�jkrt d����jstd���t��}|� �j|j ���t|j ��D]��t�j��D]n����fdkrtd���||��ft����fd�t���D����z ���fz �����f<�o�������S) ��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.c3�B�K�|]}��|f�|�fzV��dSrr��.0�kr�Xrrs ����r� <genexpr>z*_lower_triangular_solve.<locals>.<genexpr>PsK�����+;�+;�,-�,-�Q��T�7�1�Q��T�7�?�+;�+;�+;�+;�+;�+;r)�denser#� is_squarer rr �is_lower� ValueErrorr �zerosr�ranger�sumr�rrr#�dpsr,rrs` @@@r�_lower_triangular_solver7.s|������"*�)�)�)�)�)� �;�=�"�#;�<�<�<� �x�1�6����2�3�3�3� �:�=��;�<�<�<� � "� "�C� � "� "�1�6�3�8� 4� 4�A� �3�8�_�_�G�G���q�v��� G� G�A���A��w�!�|�|�� >�?�?�?��c�3�q�!�t�9�s�+;�+;�+;�+;�+;�+;�+;�16�q���+;�+;�+;�(;�(;�;�>?��1��g�F�G�G�A�a��d�G�G�  G� �6�6�!�9�9�rc ��|jstd���|j|jkrtd���|jst d���t ��}d�t|j��D��}|���D])\}}}||kr||� ||f���*|� ��}t|j ��D]g}t|j��D]P}||D]"\}}|||fxx||||fzzcc<�#||||f|||fz ��|||f<�Q�h|� |��S)r!r$r%r&c��g|]}g��Srr�r*rs r� <listcomp>z2_lower_triangular_solve_sparse.<locals>.<listcomp>n��� &� &� &�1�B� &� &� &r) r/r rr r0r1r r3�row_list�append� as_mutablerr� rrr6rrr�vr,�us r�_lower_triangular_solve_sparserCUs|��" �;�=�"�#;�<�<�<� �x�1�6����2�3�3�3� �:�=��;�<�<�<� !� #� #�C� &� &��a�f� � � &� &� &�D��:�:�<�<�#�#���1�a� �q�5�5� ��G�N�N�A�q�6� "� "� "�� �����A� �3�8�_�_�-�-���s�x��� -� -�A��Q�� %� %���1��!�Q�$����1�Q�q�!�t�W�9�$������c�!�A�q�D�'�A�a��d�G�+�,�,�A�a��d�G�G�  -� �6�6�!�9�9�rc ������ddlm}�jstd���|j�jkrt d����jstd���t��}|� �j|j ���t|j ��D]��tt�j����D]w����fdkrtd���||��ft����fd�t�dz�j��D����z ���fz �����f<�x�������S) ��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 rr"r$�Matrix size mismatch.�Matrix is not upper triangular.rr'c3�B�K�|]}��|f�|�fzV��dSrrr)s ����rr-z*_upper_triangular_solve.<locals>.<genexpr>�sS�����+G�+G�,-�,-�Q��T�7�1�Q��T�7�?�+G�+G�+G�+G�+G�+Gr)r.r#r/r rr �is_upperrr r2rr3�reversedr1r4rr5s` @@@r�_upper_triangular_solverK�s�������"*�)�)�)�)�)� �;�=�"�#;�<�<�<� �x�1�6����0�1�1�1� �:�;��9�:�:�:� � "� "�C� � "� "�1�6�3�8� 4� 4�A� �3�8�_�_�S�S���%���-�-�(�(� S� S�A���A��w�!�|�|� �!?�@�@�@��c�3�q�!�t�9�s�+G�+G�+G�+G�+G�+G�+G�16�q�1�u�a�f�1E�1E�+G�+G�+G�(G�(G�G�JK�A�q�D�'�R�S�S�A�a��d�G�G�  S� �6�6�!�9�9�rc ���|jstd���|j|jkrtd���|jst d���t ��}d�t|j��D��}|���D])\}}}||kr||� ||f���*|� ��}t|j ��D]�}tt|j����D]]}t||��D]"\}}|||fxx||||fzzcc<�#||||f|||fz ��|||f<�^��|� |��S)rEr$rFrGc��g|]}g��Srrr:s rr;z2_upper_triangular_solve_sparse.<locals>.<listcomp>�r<r)r/r rr rIrr r3r=r>r?rrJrr@s r�_upper_triangular_solve_sparserN�s���" �;�=�"�#;�<�<�<� �x�1�6����0�1�1�1� �:�;��9�:�:�:� !� #� #�C� &� &��a�f� � � &� &� &�D��:�:�<�<�#�#���1�a� �q�5�5� ��G�N�N�A�q�6� "� "� "�� �����A� �3�8�_�_�-�-���%���/�/�*�*� -� -�A� ��a��)�)� %� %���1��!�Q�$����1�Q�q�!�t�W�9�$������c�!�A�q�D�'�A�a��d�G�+�,�,�A�a��d�G�G�  -� �6�6�!�9�9�rc���|j|jkrtd���d}d}|���rd}n |jsd}|st |��durF|j}|�|��}|�|��}|��� }|�|���}|� |��}|r|j� |��S|j � |��S)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) rr�NotImplementedError� is_symmetric� is_hermitianr �H�multiply�cholesky�lower_triangular_solve�upper_triangular_solve�T)rrrR�reformrV�L�Ys r�_cholesky_solver_�s ��( �v�����!� D�F�F� F��I��F��~�~����� � � �^���� �)�)�!�,�,��5�5��C���J�J�q�M�M���J�J�s�O�O�����(�(�(� � � � �Y� �'�'�A� � � ��%�%�A��/���+�+�A�.�.�.���+�+�A�.�.�.rc�,�|j|jkrtd���d}d}|���rd}n |jsd}|st |��durF|j}|�|��}|�|��}|��� }|�|���\}}|� |��}|� |��}|r|j� |��S|j � |��S)a�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 rPTFrQ) rrrSrTrUr rVrW�LDLdecompositionrY�diagonal_solverZr[) rrrRr\rVr]�Dr^�Zs r� _LDLsolveres#��> �v�����!� D�F�F� F��I��F��~�~����� � � �^���� �)�)�!�,�,��5�5��C���J�J�q�M�M���J�J�s�O�O�����(�(�(� � � � � � � 2� 2�D�A�q� � #� #�C� (� (�A� � � �A� � �A��/���+�+�A�.�.�.���+�+�A�.�.�.rc��� � �|j|jkrtd���|j}|j}||krtd��� |�|d���\}}n#t $rt d���wxYwt��� |�|��� ��}t|��D]G}tt||����D]'} ||| f� |� || � � fd����(�H||krZt||��D];}t|j��D]$} |||| f��st d����%�<|d|�d d �f}t|d z d d ��D]a}t|d z|��D]'} ||| f� |� || � � fd ����(|||f� |� |� � fd ����b|�|��S)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�&���||�zz ��Srr��x�yr6�scales ��rrz_LUsolve.<locals>.<lambda>h����C�C��A��I� �,>�,>�rzThe system is inconsistent.rNr�����c�&���||�zz ��Srrrks ��rrz_LUsolve.<locals>.<lambda>wrorc� ���|�z ��Srr)rl�_r6rns ��rrz_LUsolve.<locals>.<lambda>zs�����Q��Y���r)rr rrS�LUdecomposition_Simpler1r r � permute_rowsr?r3�min� zip_row_op�row_op� __class__) rrrg�m�n�A�perm�brrr6rns @@r�_LUsolver<s�����* �x�1�6���� B�D�D� D� ��A� ��A��1�u�u�!�"J�K�K�K�K��*�*�!�T�+�3�3���4�4�� �K�K�K�&�'I�J�J�J�K���� !� "� "�C� � � �4� � � +� +� -� -�A��1�X�X�@�@���s�1�a�y�y�!�!� @� @�A��a��d�G�E� �L�L��A�>�>�>�>�>� ?� ?� ?� ?� @�  �1�u�u��q�!��� D� D�A��1�6�]�]� D� D��!�z�!�A�q�D�'�*�*�D�$�%B�C�C�C�D� D� �a��c�1�1�1�f�I���1�q�5�"�b� !� !�1�1���q�1�u�a��� @� @�A��a��d�G�E� �L�L��A�>�>�>�>�>� ?� ?� ?� ?��!�Q�$��� ����/�/�/�/�/�0�0�0�0� �=�=�� � �s �A!�!A;c��ttt��}|���\}}|j|z}g}|j}t |dz dd��D]k}||dd�f} t |dz|��D]} | ||| f||dz | z zz} �|| ��} |�| |||fz ���l|j|ddd��S)a�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 rrpN)r r�QRdecompositionr[rr3r>�vstack) rr~r6�Q�Rrmrlr{r�tmpr+s r�_QRsolver�s���> "�*�j� 9� 9�C� � � � � �D�A�q� �3��7�A� �A� ��A� �1�q�5�"�b� !� !� � ����1�1�1��g���q�1�u�a��� *� *�A� �1�Q��T�7�Q�q�1�u�q�y�\�)� )�C�C��c�#�h�h�� ����q��A��w������ �1�8�Q�t�t��t�W� �rFc�>����ddlm}m}|j}|�|���|�����}|j}|dd�d| �fj\}�|�d���\} �| dd�d| �f| dd�| d�f} } tt�fd�������t���} �fd�t| j��D��} |�| z��j } | | d�dd�fjstd���t!d |gd �d �� ��j}t%|���|�fd �t�| z |z��D������| z |��}| d| �| f}| d| �dd�f}|�|||zz |��}|�|��}t���D]}||dd�f|| |dd�f<�||��||��}}|r||| fS||fS)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)�Matrixr2NT)�simplifyc���|�kSrr)�p�cols �rrz%_gauss_jordan_solve.<locals>.<lambda>@s ���a�#�g�rc���g|]}|�v�|�� Srr)r*�c�pivotss �rr;z'_gauss_jordan_solve.<locals>.<listcomp>Es���B�B�B�A�!�6�/�/�a�/�/�/rzLinear system has no solution�tauc�F�t|���d��S)N� 1234567890)�str�rstrip)rs rrz%_gauss_jordan_solve.<locals>.<lambda>Rs��c�!�f�f�m�m�L�9�9�rc� �d|zS)Nrsr)�ss rrz%_gauss_jordan_solve.<locals>.<lambda>Ss ��S�1�W�r)�compare�modifyc�.��g|]}t�����Sr)�next)r*r+�gens �rr;z'_gauss_jordan_solve.<locals>.<listcomp>Us���A�A�A��4��9�9�A�A�Ar)�sympy.matricesr�r2ry�hstack�copyr�shape�rref�list�filter�lenr3r[�is_zero_matrixr1r�namer�reshaper�)r�B�freevarr�r2�cls�aug�B_cols�rowr|rA�rank�free_var_index� permutationr�r��V�vt�free_sol�solr+r�r�r�s @@@r�_gauss_jordan_solver��s������B-�,�,�,�,�,�,�,��{�C��x�x������!�&�&�(�(�+�+�C��v�F��1�1�1�h��w�h�;��%�H�C�����$��'�'�I�A�v��!�!�!�X�v�g�X�+���!�!�!�f�W�X�X�+��q�A��V�-�-�-�-�v�6�6�7�7�F��F� � �D�C�B�B�B��q�v���B�B�B�N��&��.�0�1�1�3�K� �T�U�U�A�A�A�X�;� %�:��8�9�9�9� !����9�9�$�$� &� &� &�&*� � �D� !� !�C� �6�A�A�A�A�e�S�4�Z��,?�&@�&@�A�A�A� B� B� J� J� �$�J�� � �C���$���&�'�A���$������{�B��z�z�"�q�3�w�,��,�,�H� �%��V� � �C� �3�Z�Z�/�/��!)�!�A�A�A�#���K��N�A�A�A� ����s�3�x�x���S����C����C��'�'��C�x�rNc��ddlm}|}|���}|�T|j|j}}t d�||��t ���}|�|||��j}|� |��||j��|� |��z � |��zS)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)�eyeNzw:{}_:{})r�) r�r��pinvrr�formatrryr[rW) rr��arbitrary_matrixr�r|�A_pinvrr�ws r� _pinv_solver�ks���\#�"�"�"�"�"� �A� �V�V�X�X�F����6�1�6�d��"�:�#4�#4�T�4�#@�#@�e�L�L�L���;�;�t�T�1�5�5�7�� �?�?�1� � ���Q�V��� �O�O�A� � �"� (��)9� :� :� ;�;r�laplacec����� � �ddlm}�� �� fd�}�dkr ddlm}|}n,�dkr ddlm}|}nt �t ��r�fd�}n�}|���}|�j�} t�jd��D]D� t�jd ��D]'� |�j g�j�|�R���|z | � � f<�(�E�� | ��S) aeSolves 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)r2c�8��|�kr �|�fn �||fSrr)rrrr�rr�s ����r�entryz_cramer_solve.<locals>.entry�s%����3�h�h�s�1�c�6�{�{�A�a��d�G�3r�bird)� _det_birdr�)� _det_laplacec�0��|�����S)N��method)�det)�matrix� det_methods �rrz_cramer_solve.<locals>.<lambda>�s���V�Z�Z�z�Z�:�:�rr) r.r2� determinantr�r�� isinstancer�r�r3ry) rrr�r2r�r�r�r��det_Mrlr�r�s ``` @@r� _cramer_solver��s[�������X������4�4�4�4�4�4�4�4��V���*�*�*�*�*�*���� �y� � �-�-�-�-�-�-���� �J�� $� $��:�:�:�:����� �C��F�F�E� ��s�y��A��S�Y�q�\�"�"�D�D�����1��&�&� D� D�C��#�k�a�k�:�1�7�:�E�:�:�:�;�;�e�C�A�c�3�h�K�K� D� �;�;�q�>�>�r�GJc�4�|dvrJ |�|��\}}|rtd���n#t$rtd���wxYw|S|dkr|�|��S|dkr|�|��S|dkr|�|��S|dkr|�|��S|dkr|�|��S|d kr|�|��S|� |� ��� |��S) aLSolves 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. )r��GEzcMatrix det == 0; not invertible. Try ``M.gauss_jordan_solve(rhs)`` to obtain a parametric solution.ri�LU�CH�QR�LDL�PINV�CRAMERr�) �gauss_jordan_solver r1�LUsolve�cholesky_solve�QRsolve�LDLsolve� pinv_solve� cramer_solve�invrW)rrr��soln�params r�_solver� sR��b���� O��.�.�s�3�3�K�D�%�� V�.�0U�V�V�V� V��� O� O� O�*�+M�N�N� N� O����� � �4����y�y��~�~�� �4�������$�$�$� �4����y�y��~�~�� �5����z�z�#���� �6� � ��|�|�C� � � � �8� � ��~�~�c�"�"�"��u�u�F�u�#�#�,�,�S�1�1�1s �)0�A r�c�"�|dkr|�|��S|dkr|�|��S|dkr|�|��S|dkr|�|��S|j}||z�||z|���S)a4Return 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�rV�solve)rrr��ts r�_solve_least_squaresr�Xs���X��~�~�����$�$�$� �4����y�y��~�~�� �5����z�z�#���� �6� � ��|�|�C� � � � �C���A��}�}�Q��W�V�}�4�4�4r)Fr)r�)r�)r�)�sympy.core.functionr�sympy.core.symbolrrr�sympy.utilities.iterablesr� exceptionsr r r �eigenr � utilitiesr rrr7rCrKrNr_rerr�r�r�r�r�r�rrr�<module>r�s���*�*�*�*�*�*�C�C�C�C�C�C�C�C�C�C�6�6�6�6�6�6�R�R�R�R�R�R�R�R�R�R�+�+�+�+�+�+�6�6�6�6�6�6�6�6�!>�!>�!>�H%�%�%�N(�(�(�V%�%�%�N(�(�(�V,/�,/�,/�^8/�8/�8/�v!(�@�@�@�@�F3�3�3�ls�s�s�s�lY;�Y;�Y;�Y;�x@�@�@�@�FK2�K2�K2�K2�\V5�V5�V5�V5�V5�V5r
Memory