o
    §ƒðj¬<  ã                   @   sà   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d„Zd	dd
œdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zddœdd„Zddœdd „Zddœd!d"„Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+S ),é    )ÚZZ)ÚSDMÚ	sdm_irrefÚsdm_rref_den)ÚDDM)Ú	ddm_irrefÚddm_irref_denÚauto)Úmethodc                C   sº   t | |dd�\}}t| |ƒ\} }|dkrt| ƒ}t|ƒ\}}n3|dkr1t| ƒ\}}}t|ƒ| }n!|dkrK| jdd�\}	}
t|
ƒ\}}}t|ƒ| }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Ú_ÚMr© r$   ú›/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/polys/matrices/rref.pyÚ_dm_rref%   s   r&   T)Úkeep_domainr
   c                C   s8  t | |dd�\}}t| |ƒ\} }|dkrt| ƒ\}}}nt|dkrPtt| ƒƒ\}}|rI|j| jkrI|jdd�\}	}|rD|d|d f j}nL|jj}nG|}|jj}n@|dkr‰| j	dd�\}	}
t|
ƒ\}}}|rv|j| jkrvt|ƒ| }| jj}n|}|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_rr$   r$   r%   Ú_dm_rref_denY   s4   




	r-   c                 C   sX   | j j}||kr	 | |fS |dkr|  ¡ } | |fS |dkr%|  ¡ } | |fS td|› �ƒ‚)z?Convert a matrix to the given format and return the old format.ÚdenseÚsparsezUnknown format: )ÚrepÚfmtÚto_denseÚ	to_sparser   )r   r1   r   r$   r$   r%   r   §   s   úüÿr   c                 C   ó   | j jdkr
t| ƒS t| ƒS )z:Compute RREF using Gauss-Jordan elimination with division.r/   )r0   r1   Ú_dm_rref_GJ_sparseÚ_dm_rref_GJ_dense©r   r$   r$   r%   r   ¸   ó   r   c                 C   r4   )z:Compute RREF using fraction-free Gauss-Jordan elimination.r/   )r0   r1   Ú_dm_rref_den_FF_sparseÚ_dm_rref_den_FF_denser7   r$   r$   r%   r   À   r8   r   c                 C   s6   t | jƒ\}}}t|| j| jƒ}t|ƒ}|  |¡|fS )zACompute RREF using sparse Gauss-Jordan elimination with division.)r   r0   r   Úshaper(   ÚtupleÚfrom_rep)r   ÚM_rref_dr   r"   Ú
M_rref_sdmr$   r$   r%   r5   È   s   r5   c                 C   sT   | j jp| j j}| j ¡  ¡ }t||d�}t|| j| j ƒ}t	|ƒ}|  
| ¡ ¡|fS )z@Compute RREF using dense Gauss-Jordan elimination with division.)Ú_partial_pivot)r(   Úis_RRÚis_CCr0   Úto_ddmÚcopyr   r   r;   r<   r=   Úto_dfm_or_ddm)r   Úpartial_pivotÚddmr   Ú
M_rref_ddmr$   r$   r%   r6   Ð   s   r6   c                 C   s<   t | j| jƒ\}}}t|| j| jƒ}t|ƒ}|  |¡||fS ©zACompute RREF using sparse fraction-free Gauss-Jordan elimination.)r   r0   r(   r   r;   r<   r=   )r   r>   r!   r   r?   r$   r$   r%   r9   Ú   s   r9   c                 C   sJ   | j  ¡  ¡ }t|| jƒ\}}t|| j| jƒ}t|ƒ}|  | 	¡ ¡||fS rI   )
r0   rC   rD   r   r(   r   r;   r<   r=   rE   )r   rG   r!   r   rH   r$   r$   r%   r:   â   s
   r:   Fr   c                C   sØ   |dkr|  d¡r|dtdƒ … }d}||fS d}||fS d}| j}|jr0t| |d�}||fS |jr=t| |d�}||fS |jsC|jrKd}d}||fS |j	r^| j
jdkr^|s^d}d}||fS |rfd}||fS d}||fS )	z3Choose the fastest method for computing RREF for M.r	   Ú_denseNr.   r/   r   r   r   )ÚendswithÚlenr(   Úis_ZZÚ_dm_rref_choose_method_ZZÚis_QQÚ_dm_rref_choose_method_QQrA   rB   Úis_EXr0   r1   )r   r
   r   r   ÚKr$   r$   r%   r   ë   s8   
%Ý#áçéí		ûþr   c          
      C   sŒ   t | ƒ\}}}|td|d ƒk rdS t| ƒ\}}tdd„ |D ƒdd�}tj}|D ]}	t ||	¡}| ¡ d| kr; dS q(| ¡ dk rDd	S d
S )z5Choose the fastest method for computing RREF over QQ.é   é   r   c                 S   ó   g | ]}|  ¡ ‘qS r$   ©Ú
bit_length)Ú.0Únr$   r$   r%   Ú
<listcomp>/  ó    z-_dm_rref_choose_method_QQ.<locals>.<listcomp>é   ©Údefaulté2   r   r   )Ú_dm_row_densityÚminÚ_dm_QQ_numers_denomsÚmaxr   r+   ÚlcmrW   )
r   r   Údensityr"   ÚncolsÚnumersÚdenomsÚ
numer_bitsÚ	denom_lcmÚdr$   r$   r%   rP     s   	ÿrP   c          
      C   s¦   d}t | ƒ\}}}|dk r||d k rdS dS |dk rdS |d||  kr'dS t| ƒ}tdd„ |D ƒd	d
�}td	d| | ƒ}d|||d    | }	||	k rQdS dS )z5Choose the fastest method for computing RREF over ZZ.i'  é
   rT   r   r   rS   c                 S   rU   r$   rV   ©rX   Úer$   r$   r%   rZ   n  r[   z-_dm_rref_choose_method_ZZ.<locals>.<listcomp>r\   r]   gUUUUUUå?)r`   Ú_dm_elementsrc   )
r   r   ÚPARAMre   Únrows_nzrf   ÚelementsÚbitsÚwidenessÚmax_densityr$   r$   r%   rN   F  s"   rN   c                 C   sJ   | j d }| j ¡  ¡ }|sdd|fS t|ƒ}ttt|ƒƒ| }|||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.
    r\   r   )r;   r0   Úto_sdmÚvaluesrL   ÚsumÚmap)r   rf   Úrows_nzrq   re   r$   r$   r%   r`   |  s   


r`   c                 C   s   |   ¡ \}}|S )z*Return nonzero elements of a DomainMatrix.)Ú
to_flat_nz)r   rr   r"   r$   r$   r%   ro   ’  s   ro   c                 C   s,   t | ƒ}dd„ |D ƒ}dd„ |D ƒ}||fS )zBReturns the numerators and denominators of a DomainMatrix over QQ.c                 S   ó   g | ]}|j ‘qS r$   )Ú	numeratorrm   r$   r$   r%   rZ   ›  ó    z(_dm_QQ_numers_denoms.<locals>.<listcomp>c                 S   r|   r$   r   rm   r$   r$   r%   rZ   œ  r~   )ro   )ÚMqrr   rg   rh   r$   r$   r%   rb   ˜  s   rb   c                 C   s   | j }|jr
|  ¡ S | S )z.Convert a DomainMatrix to a field if possible.)r(   Úhas_assoc_FieldÚto_field)r   rR   r$   r$   r%   r      s   r   N)Úsympy.polys.domainsr   Úsympy.polys.matrices.sdmr   r   r   Úsympy.polys.matrices.ddmr   Úsympy.polys.matrices.denser   r   r&   r-   r   r   r   r5   r6   r9   r:   r   rP   rN   r`   ro   rb   r   r$   r$   r$   r%   Ú<module>   s(   4N
	.-6