o
    o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|\}}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$   m/var/www/html/construction_image-detection-poc/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>   default2   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