
    g<                         d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
 dddZddd	d
Zd Zd Zd Zd Zd Zd Zd ZdddZdddZdddZd Zd Zd Zd ZdS )    )ZZ)SDM	sdm_irrefsdm_rref_den)DDM)	ddm_irrefddm_irref_denauto)methodc                   t          | |d          \  }}t          | |          \  } }|dk    r"t          |           }t          |          \  }}n|dk    r&t	          |           \  }}}t          |          |z  }nW|dk    r?|                     d          \  }	}
t	          |
          \  }}}t          |          |z  }nt          d|           t          ||          \  }}	||fS )	a  
    Compute the reduced row echelon form of a ``DomainMatrix``.

    This function is the implementation of :meth:`DomainMatrix.rref`.

    Chooses the best algorithm depending on the domain, shape, and sparsity of
    the matrix as well as things like the bit count in the case of :ref:`ZZ` or
    :ref:`QQ`. The result is returned over the field associated with the domain
    of the Matrix.

    See Also
    ========

    sympy.polys.matrices.domainmatrix.DomainMatrix.rref
        The ``DomainMatrix`` method that calls this function.
    sympy.polys.matrices.rref._dm_rref_den
        Alternative function for computing RREF with denominator.
    FdenominatorGJFFCDTconvertUnknown method for rref: )_dm_rref_choose_method
_dm_to_fmt	_to_field_dm_rref_GJ_dm_rref_den_FFclear_denoms_rowwise
ValueError)Mr   use_fmtold_fmtMfM_rrefpivotsM_rref_fden_Mrs              U/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/polys/matrices/rref.py_dm_rrefr'   %   s   & -QEJJJOFGAw''JAw~~q\\$R	4 / 2 2#v8$$s*	4&&t&442 / 3 3#v8$$s* =V==>>>67++IFA 6>    T)keep_domainr   c                   t          | |d          \  }}t          | |          \  } }|dk    rt          |           \  }}}n |dk    r~t          t	          |                     \  }}|rN|j        | j        k    r>|                    d          \  }	}|r|d|d         f         j        }n|j        j        }n|}|j        j        }n|dk    r| 	                    d          \  }	}
t          |
          \  }}}|r/|j        | j        k    rt	          |          |z  }| j        j        }n9|}|r|d|d         f         j        }n|j        j        }nt          d|           t          ||          \  }}	|||fS )	a  
    Compute the reduced row echelon form of a ``DomainMatrix`` with denominator.

    This function is the implementation of :meth:`DomainMatrix.rref_den`.

    Chooses the best algorithm depending on the domain, shape, and sparsity of
    the matrix as well as things like the bit count in the case of :ref:`ZZ` or
    :ref:`QQ`. The result is returned over the same domain as the input matrix
    unless ``keep_domain=False`` in which case the result might be over an
    associated ring or field domain.

    See Also
    ========

    sympy.polys.matrices.domainmatrix.DomainMatrix.rref_den
        The ``DomainMatrix`` method that calls this function.
    sympy.polys.matrices.rref._dm_rref
        Alternative function for computing RREF without denominator.
    Tr   r   r   r   r   r   r   )r   r   r   r   r   domainclear_denomselementoner   r   )r   r)   r   r   r   r    r#   r!   r"   r$   r%   M_rref_rs               r&   _dm_rref_denr0   Y   s   ( -QDIIIOFGAw''JAw~~-a00VV	4&y||44&  
	$8?ah66 --d-;;IAv (Qq	\*2m' F-#CC	4&&t&442 / 3 3#v 	(8?ah66x((3.F(,CC F (Qq	\*2m'=V==>>>67++IFA 3r(   c                     | j         j        }||k    rnH|dk    r|                                 } n-|dk    r|                                 } nt	          d|           | |fS )z?Convert a matrix to the given format and return the old format.densesparsezUnknown format: )repfmtto_dense	to_sparser   )r   r5   r   s      r&   r   r      sh    eiG#~~	JJLL	KKMM1C11222g:r(   c                 ^    | j         j        dk    rt          |           S t          |           S )z:Compute RREF using Gauss-Jordan elimination with division.r3   )r4   r5   _dm_rref_GJ_sparse_dm_rref_GJ_denser   s    r&   r   r      s-    uyH!!$$$ ###r(   c                 ^    | j         j        dk    rt          |           S t          |           S )z:Compute RREF using fraction-free Gauss-Jordan elimination.r3   )r4   r5   _dm_rref_den_FF_sparse_dm_rref_den_FF_denser;   s    r&   r   r      s-    uyH%a((($Q'''r(   c                     t          | j                  \  }}}t          || j        | j                  }t          |          }|                     |          |fS )zACompute RREF using sparse Gauss-Jordan elimination with division.)r   r4   r   shaper+   tuplefrom_rep)r   M_rref_dr!   r$   
M_rref_sdms        r&   r9   r9      sO    #AE**HfaXqw11J6]]F::j!!6))r(   c                 P   | j         j        p| j         j        }| j                                                                        }t          ||          }t          || j        | j                   }t          |          }| 
                    |                                          |fS )z@Compute RREF using dense Gauss-Jordan elimination with division.)_partial_pivot)r+   is_RRis_CCr4   to_ddmcopyr   r   r@   rA   rB   to_dfm_or_ddm)r   partial_pivotddmr!   
M_rref_ddms        r&   r:   r:      s    HN4ahnM
%,,..



Cs=999FS!'18,,J6]]F::j..0011699r(   c                     t          | j        | j                  \  }}}t          || j        | j                  }t          |          }|                     |          ||fS zACompute RREF using sparse fraction-free Gauss-Jordan elimination.)r   r4   r+   r   r@   rA   rB   )r   rC   r#   r!   rD   s        r&   r=   r=      sU    (99Hc6Xqw11J6]]F::j!!3..r(   c                 0   | j                                                                         }t          || j                  \  }}t          || j        | j                  }t          |          }|                     |	                                          ||fS rP   )
r4   rI   rJ   r	   r+   r   r@   rA   rB   rK   )r   rM   r#   r!   rN   s        r&   r>   r>      sw    
%,,..



CQX..KCS!'18,,J6]]F::j..00113>>r(   Fr   c                b   |dk    r3|                     d          r|dt          d                    }d}nvd}nsd}| j        }|j        rt	          | |          }nQ|j        rt          | |          }n8|j        s|j        rd}d}n%|j	        r| j
        j        dk    r|sd}d}n|rd}nd}||fS )	z3Choose the fastest method for computing RREF for M.r
   _denseNr2   r3   r   r   r   )endswithlenr+   is_ZZ_dm_rref_choose_method_ZZis_QQ_dm_rref_choose_method_QQrG   rH   is_EXr4   r5   )r   r   r   r   Ks        r&   r   r      s     ??8$$ 	Oc(mm^O,FGGGG H7 	.qkJJJFFW 	.qkJJJFFW 	 	FGGW 	g--k- FGG  7?r(   c                t   t          |           \  }}}|t          d|dz            k     rdS t          |           \  }}t          d |D             d          }t          j        }|D ]5}	t	          j        ||	          }|                                d|z  k    r dS 6|                                dk     rdS d	S )
z5Choose the fastest method for computing RREF over QQ.      r   c                 6    g | ]}|                                 S  
bit_length).0ns     r&   
<listcomp>z-_dm_rref_choose_method_QQ.<locals>.<listcomp>/  s     555allnn555r(      default2   r   r   )_dm_row_densitymin_dm_QQ_numers_denomsmaxr   r.   lcmrb   )
r   r   densityr$   ncolsnumersdenoms
numer_bits	denom_lcmds
             r&   rY   rY     s     (**GQ Qa  t *!,,NFF55f555qAAAJI  F9a((	!!AjL0044 1 ""ttr(   c                4   d}t          |           \  }}}|dk     r||dz  k     rdS dS |dk     rdS |d||z  z   k    rdS t          |           }t          d |D             d	          }t          dd
|z  |z            }d|||dz  z  z  z   |z  }	||	k     rdS dS )z5Choose the fastest method for computing RREF over ZZ.i'  
   r^   r   r   r]   c                 6    g | ]}|                                 S r`   ra   rc   es     r&   re   z-_dm_rref_choose_method_ZZ.<locals>.<listcomp>n  s     1111111r(   rf   rg   gUUUUUU?)rj   _dm_elementsrm   )
r   r   PARAMro   nrows_nzrp   elementsbitswidenessmax_densitys
             r&   rW   rW   F  s    $ E
  /q11GXu "}}U1W44 {{t	1uX~%	%	%t AH111111===D 1c%i())HuhtQw.//8;Kttr(   c                     | j         d         }| j                                                                        }|sdd|fS t	          |          }t          t          t          |                    |z  }|||fS )a  Density measure for sparse matrices.

    Defines the "density", ``d`` as the average number of non-zero entries per
    row except ignoring rows that are fully zero. RREF can ignore fully zero
    rows so they are excluded. By definition ``d >= 1`` except that we define
    ``d = 0`` for the zero matrix.

    Returns ``(density, nrows_nz, ncols)`` where ``nrows_nz`` counts the number
    of nonzero rows and ``ncols`` is the number of columns.
    rf   r   )r@   r4   to_sdmvaluesrU   summap)r   rp   rows_nzr}   ro   s        r&   rj   rj   |  sr     GAJEellnn##%%G (!U{w<<c#w''((83%''r(   c                 4    |                                  \  }}|S )z*Return nonzero elements of a DomainMatrix.)
to_flat_nz)r   r~   r$   s      r&   r{   r{     s    ,,..KHaOr(   c                 X    t          |           }d |D             }d |D             }||fS )zBReturns the numerators and denominators of a DomainMatrix over QQ.c                     g | ]	}|j         
S r`   )	numeratorry   s     r&   re   z(_dm_QQ_numers_denoms.<locals>.<listcomp>  s    ,,,aak,,,r(   c                     g | ]	}|j         
S r`   r   ry   s     r&   re   z(_dm_QQ_numers_denoms.<locals>.<listcomp>  s    ...am...r(   )r{   )Mqr~   rq   rr   s       r&   rl   rl     s?    BH,,8,,,F..X...F6>r(   c                 J    | j         }|j        r|                                 S | S )z.Convert a DomainMatrix to a field if possible.)r+   has_assoc_Fieldto_field)r   r[   s     r&   r   r     s'    	A zz||r(   N)sympy.polys.domainsr   sympy.polys.matrices.sdmr   r   r   sympy.polys.matrices.ddmr   sympy.polys.matrices.denser   r	   r'   r0   r   r   r   r9   r:   r=   r>   r   rY   rW   rj   r{   rl   r   r`   r(   r&   <module>r      s  < # " " " " " A A A A A A A A A A ( ( ( ( ( ( ? ? ? ? ? ? ? ? ! 1 1 1 1 1h $( K K K K K\  "$ $ $( ( (* * *: : :/ / /? ? ? 6; + + + + +\ 16 * * * * *Z 16 3 3 3 3 3l( ( (,        r(   