U
    L?h<                     @   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#   K/var/www/html/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rt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rt|| }| 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}qd}nvd}| j}|jrNt| |d}nX|jrbt| |d}nD|jsn|jrxd}d}n.|j	r| j
jdkr|sd}d}n|rd}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 rd	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>   default2   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 rd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