U
    ZÊ¹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|ƒ\}}nf|dkrbt| ƒ\}}}t|ƒ| }nB|dkr–| 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#   úX/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/polys/matrices/rref.pyÚ_dm_rref%   s    r%   T)Úkeep_domainr
   c                C   s<  t | |dd�\}}t| |ƒ\} }|dkr8t| ƒ\}}}nì|dkr tt| ƒƒ\}}|r’|j| jkr’|jdd�\}	}|rˆ|d|d f j}qž|jj}n|}|jj}n„|dk�r| j	dd�\}	}
t|
ƒ\}}}|rî|j| jkrît|ƒ| }| 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   ÚdomainZ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   sL   | j j}||krn2|dkr$|  ¡ } n |dkr6|  ¡ } ntd|› �ƒ‚| |fS )z?Convert a matrix to the given format and return the old format.ÚdenseÚsparsezUnknown format: )ÚrepÚfmtZto_denseZ	to_sparser   )r   r.   r   r#   r#   r$   r   §   s    

r   c                 C   s    | j jdkrt| ƒS t| ƒS dS )z:Compute RREF using Gauss-Jordan elimination with division.r,   N)r-   r.   Ú_dm_rref_GJ_sparseÚ_dm_rref_GJ_dense©r   r#   r#   r$   r   ¸   s    r   c                 C   s    | j jdkrt| ƒS t| ƒS dS )z:Compute RREF using fraction-free Gauss-Jordan elimination.r,   N)r-   r.   Ú_dm_rref_den_FF_sparseÚ_dm_rref_den_FF_denser1   r#   r#   r$   r   À   s    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$   r/   È   s    r/   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.)Z_partial_pivot)r'   Úis_RRÚis_CCr-   Úto_ddmÚcopyr   r   r4   r5   r6   Úto_dfm_or_ddm)r   Zpartial_pivotÚddmr   Ú
M_rref_ddmr#   r#   r$   r0   Ð   s    r0   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   r4   r5   r6   )r   r7   r    r   r8   r#   r#   r$   r2   Ú   s    r2   c                 C   sJ   | j  ¡  ¡ }t|| jƒ\}}t|| j| jƒ}t|ƒ}|  | 	¡ ¡||fS r@   )
r-   r;   r<   r   r'   r   r4   r5   r6   r=   )r   r>   r    r   r?   r#   r#   r$   r3   â   s
    r3   Fr   c                C   s®   |dkr0|  d¡r*|dtdƒ … }d}q¦d}nvd}| j}|jrNt| |d�}nX|jrbt| |d�}nD|jsn|jrxd}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	   Z_denseNr+   r,   r   r   r   )ÚendswithÚlenr'   Zis_ZZÚ_dm_rref_choose_method_ZZZis_QQÚ_dm_rref_choose_method_QQr9   r:   Zis_EXr-   r.   )r   r
   r   r   ÚKr#   r#   r$   r   ë   s*    
	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rP dS qP| ¡ dk rˆd	S d
S dS )z5Choose the fastest method for computing RREF over QQ.é   é   r   c                 S   s   g | ]}|  ¡ ‘qS r#   ©Ú
bit_length)Ú.0Únr#   r#   r$   Ú
<listcomp>/  s     z-_dm_rref_choose_method_QQ.<locals>.<listcomp>é   ©Údefaulté2   r   r   N)Ú_dm_row_densityÚminÚ_dm_QQ_numers_denomsÚmaxr   r)   ZlcmrI   )
r   r   Údensityr!   ÚncolsÚnumersÚdenomsZ
numer_bitsZ	denom_lcmÚdr#   r#   r$   rD     s    	rD   c          
      C   sª   d}t | ƒ\}}}|dk r.||d k r*dS dS |dk r:dS |d||  krNdS t| ƒ}tdd„ |D ƒd	d
�}td	d| | ƒ}d|||d    | }	||	k r¢dS dS dS )z5Choose the fastest method for computing RREF over ZZ.i'  é
   rG   r   r   rF   c                 S   s   g | ]}|  ¡ ‘qS r#   rH   ©rJ   Úer#   r#   r$   rL   n  s     z-_dm_rref_choose_method_ZZ.<locals>.<listcomp>rM   rN   gUUUUUUå?N)rQ   Ú_dm_elementsrT   )
r   r   ZPARAMrU   Únrows_nzrV   ÚelementsÚbitsZwidenessZmax_densityr#   r#   r$   rC   F  s"    rC   c                 C   sN   | j d }| j ¡  ¡ }|s&dd|fS t|ƒ}ttt|ƒƒ| }|||fS d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.
    rM   r   N)r4   r-   Zto_sdmÚvaluesrB   ÚsumÚmap)r   rV   Zrows_nzr^   rU   r#   r#   r$   rQ   |  s    

rQ   c                 C   s   |   ¡ \}}|S )z*Return nonzero elements of a DomainMatrix.)Z
to_flat_nz)r   r_   r!   r#   r#   r$   r]   ’  s    r]   c                 C   s,   t | ƒ}dd„ |D ƒ}dd„ |D ƒ}||fS )zBReturns the numerators and denominators of a DomainMatrix over QQ.c                 S   s   g | ]
}|j ‘qS r#   )Ú	numeratorr[   r#   r#   r$   rL   ›  s     z(_dm_QQ_numers_denoms.<locals>.<listcomp>c                 S   s   g | ]
}|j ‘qS r#   r   r[   r#   r#   r$   rL   œ  s     )r]   )ZMqr_   rW   rX   r#   r#   r$   rS   ˜  s    rS   c                 C   s   | j }|jr|  ¡ S | S dS )z.Convert a DomainMatrix to a field if possible.N)r'   Zhas_assoc_FieldZto_field)r   rE   r#   r#   r$   r      s    r   N)Z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   r/   r0   r2   r3   r   rD   rC   rQ   r]   rS   r   r#   r#   r#   r$   Ú<module>   s&   4N
	.-6