o
    GZh<                     @   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#   H/var/www/auris/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   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   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fmtZto_denseZ	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   r/   )z:Compute RREF using fraction-free Gauss-Jordan elimination.r,   )r-   r.   _dm_rref_den_FF_sparse_dm_rref_den_FF_denser2   r#   r#   r$   r      r3   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$   r0      s   r0   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   r6   r7   r8   to_dfm_or_ddm)r   Zpartial_pivotddmr   
M_rref_ddmr#   r#   r$   r1      s   r1   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   r6   r7   r8   )r   r9   r    r   r:   r#   r#   r$   r4      s   r4   c                 C   sJ   | j   }t|| j\}}t|| j| j}t|}| |	 ||fS rB   )
r-   r=   r>   r   r'   r   r6   r7   r8   r?   )r   r@   r    r   rA   r#   r#   r$   r5      s
   r5   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'   Zis_ZZ_dm_rref_choose_method_ZZZis_QQ_dm_rref_choose_method_QQr;   r<   Z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>   default2   r   r   )_dm_row_densitymin_dm_QQ_numers_denomsmaxr   r)   lcmrL   )
r   r   densityr!   ncolsnumersdenomsZ
numer_bitsZ	denom_lcmdr#   r#   r$   rF     s   	rF   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'  
   rI   r   r   rH   c                 S   rJ   r#   rK   rM   er#   r#   r$   rO   n  rP   z-_dm_rref_choose_method_ZZ.<locals>.<listcomp>rQ   rR   gUUUUUU?)rU   _dm_elementsrX   )
r   r   ZPARAMrZ   nrows_nzr[   elementsbitsZwidenessZmax_densityr#   r#   r$   rE   F  s"   rE   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.
    rQ   r   )r6   r-   Zto_sdmvaluesrD   summap)r   r[   Zrows_nzrc   rZ   r#   r#   r$   rU   |  s   


rU   c                 C   s   |   \}}|S )z*Return nonzero elements of a DomainMatrix.)Z
to_flat_nz)r   rd   r!   r#   r#   r$   rb     s   rb   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#   )	numeratorr`   r#   r#   r$   rO         z(_dm_QQ_numers_denoms.<locals>.<listcomp>c                 S   ri   r#   r   r`   r#   r#   r$   rO     rk   )rb   )ZMqrd   r\   r]   r#   r#   r$   rW     s   rW   c                 C   s   | j }|jr
|  S | S )z.Convert a DomainMatrix to a field if possible.)r'   Zhas_assoc_FieldZto_field)r   rG   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   r0   r1   r4   r5   r   rF   rE   rU   rb   rW   r   r#   r#   r#   r$   <module>   s(   4N
	.-6