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_fmtZMfÚM_rrefÚpivotsÚM_rref_fÚdenÚ_ÚMr© r#   úl/var/www/html/academy/tools/voices/clone/cosy-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"   Z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   r/   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-   )r.   r/   Ú_dm_rref_GJ_sparseÚ_dm_rref_GJ_dense©r   r#   r#   r$   r   ¸   ó   r   c                 C   r2   )z:Compute RREF using fraction-free Gauss-Jordan elimination.r-   )r.   r/   Ú_dm_rref_den_FF_sparseÚ_dm_rref_den_FF_denser5   r#   r#   r$   r   À   r6   r   c                 C   s6   t | jƒ\}}}t|| j| jƒ}t|ƒ}|  |¡|fS )zACompute RREF using sparse Gauss-Jordan elimination with division.)r   r.   r   Úshaper'   ÚtupleÚfrom_rep)r   ÚM_rref_dr   r!   Ú
M_rref_sdmr#   r#   r$   r3   È   s   r3   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_CCr.   Úto_ddmÚcopyr   r   r9   r:   r;   Úto_dfm_or_ddm)r   Úpartial_pivotÚddmr   Ú
M_rref_ddmr#   r#   r$   r4   Ð   s   r4   c                 C   s<   t | j| jƒ\}}}t|| j| jƒ}t|ƒ}|  |¡||fS ©zACompute RREF using sparse fraction-free Gauss-Jordan elimination.)r   r.   r'   r   r9   r:   r;   )r   r<   r    r   r=   r#   r#   r$   r7   Ú   s   r7   c                 C   sJ   | j  ¡  ¡ }t|| jƒ\}}t|| j| jƒ}t|ƒ}|  | 	¡ ¡||fS rG   )
r.   rA   rB   r   r'   r   r9   r:   r;   rC   )r   rE   r    r   rF   r#   r#   r$   r8   â   s
   r8   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	   Z_denseNr,   r-   r   r   r   )ÚendswithÚlenr'   Úis_ZZÚ_dm_rref_choose_method_ZZÚis_QQÚ_dm_rref_choose_method_QQr?   r@   Úis_EXr.   r/   )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*   ÚlcmrT   )
r   r   Údensityr!   ÚncolsÚnumersÚdenomsZ
numer_bitsZ	denom_lcmÚdr#   r#   r$   rM     s   	ÿrM   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'  é
   rQ   r   r   rP   c                 S   rR   r#   rS   ©rU   Úer#   r#   r$   rW   n  rX   z-_dm_rref_choose_method_ZZ.<locals>.<listcomp>rY   rZ   gUUUUUUå?)r]   Ú_dm_elementsr`   )
r   r   ZPARAMrb   Únrows_nzrc   ÚelementsÚbitsZwidenessZmax_densityr#   r#   r$   rK   F  s"   rK   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.
    rY   r   )r9   r.   Úto_sdmÚvaluesrI   ÚsumÚmap)r   rc   Zrows_nzrk   rb   r#   r#   r$   r]   |  s   


r]   c                 C   s   |   ¡ \}}|S )z*Return nonzero elements of a DomainMatrix.)Ú
to_flat_nz)r   rl   r!   r#   r#   r$   rj   ’  s   rj   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#   )Ú	numeratorrh   r#   r#   r$   rW   ›  ó    z(_dm_QQ_numers_denoms.<locals>.<listcomp>c                 S   rs   r#   r   rh   r#   r#   r$   rW   œ  ru   )rj   )ZMqrl   rd   re   r#   r#   r$   r_   ˜  s   r_   c                 C   s   | j }|jr
|  ¡ S | S )z.Convert a DomainMatrix to a field if possible.)r'   Úhas_assoc_FieldÚto_field)r   rO   r#   r#   r$   r      s   r   N)Úsympy.polys.domainsr   Zsympy.polys.matrices.sdmr   r   r   Zsympy.polys.matrices.ddmr   Zsympy.polys.matrices.denser   r   r%   r+   r   r   r   r3   r4   r7   r8   r   rM   rK   r]   rj   r_   r   r#   r#   r#   r$   Ú<module>   s(   4N
	.-6