Ë
    7^(hË€  ã                   óÈ   — d dl mZ d dlmZ d dlmZ d dlmZmZ ddl	m
Z
mZmZmZmZmZmZ edk7  rdgZ ed«      Zd	gZ edg¬
«       G d„ d	«      «       Zd dlmZ d dlmZ y)é    )ÚGROUND_TYPES)Úimport_module)Údoctest_depends_on)ÚZZÚQQé   )ÚDMBadInputErrorÚDMDomainErrorÚDMNonSquareMatrixErrorÚDMNonInvertibleMatrixErrorÚDMRankErrorÚDMShapeErrorÚDMValueErrorÚflintÚ*ÚDFM©Úground_typesc                   óî  — e Zd ZdZdZdZdZd„ Zed„ «       Z	d„ Z
ed„ «       Zed	„ «       Zed
„ «       Zed„ «       Zd„ Zd„ Zd„ Zed„ «       Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zed„ «       Zed„ «       Zd„ Zd„ Zed„ «       Zd„ Zed„ «       Z d„ Z!ed„ «       Z"d„ Z#d „ Z$d!„ Z%d"„ Z&d#„ Z'd$„ Z(d%„ Z)d&„ Z*d'„ Z+d(„ Z,d)„ Z-d*„ Z.d+„ Z/d,„ Z0d-„ Z1d.„ Z2ed/„ «       Z3ed0„ «       Z4ed1„ «       Z5ed2„ «       Z6d3„ Z7d4„ Z8d5„ Z9d6„ Z:d7„ Z;d8„ Z<d9„ Z=d:„ Z>d;„ Z?d<„ Z@d=„ ZA eBd>¬?«      d@„ «       ZC eBd>¬?«      dA„ «       ZD eBd>¬?«      dB„ «       ZEdC„ ZFdD„ ZG eBd>¬?«      dE„ «       ZHdF„ ZIdG„ ZJdNdI„ZKdJ„ ZLdOdK„ZM eBd>¬?«      dPdL„«       ZN eBd>¬?«      dPdM„«       ZOyH)Qr   a&  
    Dense FLINT matrix. This class is a wrapper for matrices from python-flint.

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.matrices.dfm import DFM
    >>> dfm = DFM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
    >>> dfm
    [[1, 2], [3, 4]]
    >>> dfm.rep
    [1, 2]
    [3, 4]
    >>> type(dfm.rep)  # doctest: +SKIP
    <class 'flint._flint.fmpz_mat'>

    Usually, the DFM class is not instantiated directly, but is created as the
    internal representation of :class:`~.DomainMatrix`. When
    `SYMPY_GROUND_TYPES` is set to `flint` and `python-flint` is installed, the
    :class:`DFM` class is used automatically as the internal representation of
    :class:`~.DomainMatrix` in dense format if the domain is supported by
    python-flint.

    >>> from sympy.polys.matrices.domainmatrix import DM
    >>> dM = DM([[1, 2], [3, 4]], ZZ)
    >>> dM.rep
    [[1, 2], [3, 4]]

    A :class:`~.DomainMatrix` can be converted to :class:`DFM` by calling the
    :meth:`to_dfm` method:

    >>> dM.to_dfm()
    [[1, 2], [3, 4]]

    ÚdenseTFc                 ó²   — | j                  |«      }d|vr
	  ||«      }n ||Ž }| j	                  |||«      S # t        t        f$ r t        d|› �«      ‚w xY w)úConstruct from a nested list.r   z"Input should be a list of list of )Ú_get_flint_funcÚ
ValueErrorÚ	TypeErrorr	   Ú_new)ÚclsÚrowslistÚshapeÚdomainÚ	flint_matÚreps         úW/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/polys/matrices/_dfm.pyÚ__new__zDFM.__new__m   ss   € à×'Ñ'¨Ó/ˆ	à�E‰>ðUÙ Ó)‘ñ ˜UÐ#ˆCà�x‰x˜˜U FÓ+Ð+øô ¤	Ð*ò UÜ%Ð(JÈ6È(Ð&SÓTÐTðUús	   —8 ¸Ac                 óž   — | j                  |||«       t        j                  | «      }||_        |x|_        \  |_        |_        ||_        |S )z)Internal constructor from a flint matrix.)Ú_checkÚobjectr$   r"   r   ÚrowsÚcolsr    )r   r"   r   r    Úobjs        r#   r   zDFM._new{   sJ   € ð 	�
‰
�3˜˜vÔ&Ü�n‰n˜SÓ!ˆØˆŒØ).Ð.ˆŒ	Ñ&�C”H˜cœhØˆŒ
Øˆ
ó    c                 óP   — | j                  || j                  | j                  «      S )z>Create a new DFM with the same shape and domain but a new rep.)r   r   r    )Úselfr"   s     r#   Ú_new_repzDFM._new_rep…   s   € à�y‰y˜˜dŸj™j¨$¯+©+Ó6Ð6r+   c                 óê  — |j                  «       |j                  «       f}||k7  rt        d«      ‚|t        k(  r%t	        |t
        j                  «      st        d«      ‚|t        k(  r%t	        |t
        j                  «      st        d«      ‚|j                  r5t	        |t
        j                  t
        j                  f«      st        d«      ‚|t        t        fvr|j                  st        d«      ‚y y )Nz(Shape of rep does not match shape of DFMzRep is not a flint.fmpz_matzRep is not a flint.fmpq_matz1Rep is not a flint.fmpz_mod_mat or flint.nmod_matú#Only ZZ and QQ are supported by DFM)ÚnrowsÚncolsr	   r   Ú
isinstancer   Úfmpz_matÚRuntimeErrorr   Úfmpq_matÚis_FFÚfmpz_mod_matÚnmod_matÚNotImplementedError)r   r"   r   r    Úrepshapes        r#   r&   z
DFM._check‰   s½   € à—I‘I“K §¡£Ð-ˆØ�uÒÜ!Ð"LÓMÐMØ”RŠ<¤
¨3´·±Ô ?ÜÐ<Ó=Ð=Ø”rŠ\¤*¨S´%·.±.Ô"AÜÐ<Ó=Ð=Ø�\Š\¤*¨S´5×3EÑ3EÄuÇ~Á~Ð2VÔ"WÜÐRÓSÐSØœB¤˜8Ñ#¨F¯LªLÜ%Ð&KÓLÐLð -9Ð#r+   c                 óV   — |t         t        fv xs |j                  xr |j                  S )z4Return True if the given domain is supported by DFM.)r   r   r7   Ú	_is_flint)r   r    s     r#   Ú_supports_domainzDFM._supports_domain—   s'   € ð œ"œb˜Ð!ÒF V§\¡\Ò%F°f×6FÑ6FÐFr+   c                 ój  ‡‡‡— |t         k(  rt        j                  S |t        k(  rt        j                  S |j
                  rh|j                  «       Št        |j                  t        j                  «      rt        j                  Šˆˆfd„}|S t        j                  ‰«      Šˆfd„}|S t        d«      ‚)z3Return the flint matrix class for the given domain.c                  ó„   •— t        | «      dk(  r(t        | d   t        j                  «      r ‰| d   «      S  ‰g | ¢‰‘­Ž S )Nr   r   )Úlenr3   r   r9   )ÚeÚ_clsÚcs    €€r#   Ú_funcz"DFM._get_flint_func.<locals>._func§   s=   ø€ Ü˜1“v ’{¤z°!°A±$¼¿¹Ô'GÙ# A a¡D›zÐ)á#˜{ Q˜{¨š{Ð*r+   c                  ó2   •— t        j                  g | ¢‰‘­Ž S ©N)r   r8   )rB   Úms    €r#   ú<lambda>z%DFM._get_flint_func.<locals>.<lambda>®   s   ø€ ¤5×#5Ñ#5Ð#<°qÐ#<¸!Ò#<€ r+   r0   )r   r   r4   r   r6   r7   Úcharacteristicr3   ÚoneÚnmodr9   Úfmpz_mod_ctxr:   )r   r    rE   rC   rD   rH   s      @@@r#   r   zDFM._get_flint_funcœ   s‹   ú€ ð ”RŠ<Ü—>‘>Ð!Ø”rŠ\Ü—>‘>Ð!Ø�\Š\Ø×%Ñ%Ó'ˆAÜ˜&Ÿ*™*¤e§j¡jÔ1Ü—~‘~�õ+ð ˆLô ×&Ñ& qÓ)�Û<�ØˆLä%Ð&KÓLÐLr+   c                 ó8   — | j                  | j                  «      S )z5Callable to create a flint matrix of the same domain.)r   r    ©r-   s    r#   rE   z	DFM._func³   s   € ð ×#Ñ# D§K¡KÓ0Ð0r+   c                 ó4   — t        | j                  «       «      S )zReturn ``str(self)``.)ÚstrÚto_ddmrO   s    r#   Ú__str__zDFM.__str__¸   s   € ä�4—;‘;“=Ó!Ð!r+   c                 ó@   — dt        | j                  «       «      dd › �S )zReturn ``repr(self)``.r   é   N)ÚreprrR   rO   s    r#   Ú__repr__zDFM.__repr__¼   s"   € à”T˜$Ÿ+™+›-Ó(¨¨Ð,Ð-Ð.Ð.r+   c                 ó–   — t        |t        «      st        S | j                  |j                  k(  xr | j                  |j                  k(  S )zReturn ``self == other``.)r3   r   ÚNotImplementedr    r"   ©r-   Úothers     r#   Ú__eq__z
DFM.__eq__À   s9   € ä˜%¤Ô%Ü!Ð!ð �{‰{˜eŸl™lÑ*ÒD¨t¯x©x¸5¿9¹9Ñ/DÐDr+   c                 ó   —  | |||«      S )r   © )r   r   r   r    s       r#   Ú	from_listzDFM.from_listÉ   s   € ñ �8˜U FÓ+Ð+r+   c                 ó6   — | j                   j                  «       S )zConvert to a nested list.)r"   ÚtolistrO   s    r#   Úto_listzDFM.to_listÎ   s   € à�x‰x�‰Ó Ð r+   c                 óV   — | j                  | j                  | j                  «      «      S )zReturn a copy of self.)r.   rE   r"   rO   s    r#   ÚcopyzDFM.copyÒ   s   € à�}‰}˜TŸZ™Z¨¯©Ó1Ó2Ð2r+   c                 ót   — t        j                  | j                  «       | j                  | j                  «      S )zConvert to a DDM.)ÚDDMr_   rb   r   r    rO   s    r#   rR   z
DFM.to_ddmÖ   ó#   € ä�}‰}˜TŸ\™\›^¨T¯Z©Z¸¿¹ÓEÐEr+   c                 ót   — t        j                  | j                  «       | j                  | j                  «      S )zConvert to a SDM.)ÚSDMr_   rb   r   r    rO   s    r#   Úto_sdmz
DFM.to_sdmÚ   rg   r+   c                 ó   — | S )zReturn self.r^   rO   s    r#   Úto_dfmz
DFM.to_dfmÞ   s   € àˆr+   c                 ó   — | S )aL  
        Convert to a :class:`DFM`.

        This :class:`DFM` method exists to parallel the :class:`~.DDM` and
        :class:`~.SDM` methods. For :class:`DFM` it will always return self.

        See Also
        ========

        to_ddm
        to_sdm
        sympy.polys.matrices.domainmatrix.DomainMatrix.to_dfm_or_ddm
        r^   rO   s    r#   Úto_dfm_or_ddmzDFM.to_dfm_or_ddmâ   s	   € ð ˆr+   c                 ól   — | j                  |j                  «       |j                  |j                  «      S )zConvert from a DDM.)r_   rb   r   r    )r   Úddms     r#   Úfrom_ddmzDFM.from_ddmò   s%   € ð �}‰}˜SŸ[™[›]¨C¯I©I°s·z±zÓBÐBr+   c                 ó°   — | j                  |«      }	  |g |¢|‘­Ž } | |||«      S # t        $ r t        d|› �«      ‚t        $ r t        d|› �«      ‚w xY w)z Inverse of :meth:`to_list_flat`.z'Incorrect number of elements for shape zInput should be a list of )r   r   r	   r   )r   Úelementsr   r    Úfuncr"   s         r#   Úfrom_list_flatzDFM.from_list_flat÷   s   € ð ×"Ñ" 6Ó*ˆð	IÙÐ(˜Ð(˜xÒ(ˆCñ
 �3˜˜vÓ&Ð&øô	 ò 	UÜ!Ð$KÈEÈ7Ð"SÓTÐTÜò 	IÜ!Ð$>¸v¸hÐ"GÓHÐHð	Iús	   “
' §.Ac                 ó6   — | j                   j                  «       S )zConvert to a flat list.)r"   ÚentriesrO   s    r#   Úto_list_flatzDFM.to_list_flat  s   € à�x‰x×ÑÓ!Ð!r+   c                 ó>   — | j                  «       j                  «       S )z$Convert to a flat list of non-zeros.)rR   Ú
to_flat_nzrO   s    r#   rz   zDFM.to_flat_nz  s   € à�{‰{‹}×'Ñ'Ó)Ð)r+   c                 óL   — t        j                  |||«      j                  «       S )zInverse of :meth:`to_flat_nz`.)rf   Úfrom_flat_nzrl   )r   rs   Údatar    s       r#   r|   zDFM.from_flat_nz  s"   € ô ×Ñ ¨$°Ó7×>Ñ>Ó@Ð@r+   c                 ó>   — | j                  «       j                  «       S )zConvert to a DOD.)rR   Úto_dodrO   s    r#   r   z
DFM.to_dod  ó   € à�{‰{‹}×#Ñ#Ó%Ð%r+   c                 óL   — t        j                  |||«      j                  «       S )zInverse of :meth:`to_dod`.)rf   Úfrom_dodrl   )r   Údodr   r    s       r#   r‚   zDFM.from_dod  ó    € ô �|‰|˜C ¨Ó/×6Ñ6Ó8Ð8r+   c                 ó>   — | j                  «       j                  «       S )zConvert to a DOK.)rR   Úto_dokrO   s    r#   r†   z
DFM.to_dok  r€   r+   c                 óL   — t        j                  |||«      j                  «       S )zInverse of :math:`to_dod`.)rf   Úfrom_dokrl   )r   Údokr   r    s       r#   rˆ   zDFM.from_dok  r„   r+   c              #   ó¨   K  — | j                   \  }}| j                  }t        |«      D ]%  }t        |«      D ]  }|||f   }|sŒ|||f   –— Œ Œ' y­w)z/Iterate over the non-zero values of the matrix.N©r   r"   Úrange©r-   rH   Únr"   ÚiÚjÚrepijs          r#   Úiter_valueszDFM.iter_values"  s_   è ø€ à�z‰z‰ˆˆ1Ø�h‰hˆÜ�q“ò 	$ˆAÜ˜1“Xò $�Ø˜A˜q˜D™	�ÚØ˜a ˜d™)“Oñ$ñ	$ùs   ‚AAÁAc              #   ó¦   K  — | j                   \  }}| j                  }t        |«      D ]$  }t        |«      D ]  }|||f   }|sŒ||f|f–— Œ Œ& y­w)zBIterate over indices and values of nonzero elements of the matrix.Nr‹   r�   s          r#   Ú
iter_itemszDFM.iter_items,  s`   è ø€ à�z‰z‰ˆˆ1Ø�h‰hˆÜ�q“ò 	*ˆAÜ˜1“Xò *�Ø˜A˜q˜D™	�ÚØ˜q˜6 5˜/Ó)ñ*ñ	*ùs   ‚AAÁAc                 ó~  — || j                   k(  r| j                  «       S |t        k(  rM| j                   t        k(  r:| j	                  t
        j                  | j                  «      | j                  |«      S | j                  |«      r-| j                  «       j                  |«      j                  «       S t        d«      ‚)zConvert to a new domain.r0   )r    rd   r   r   r   r   r6   r"   r   r>   rR   Ú
convert_torl   r:   )r-   r    s     r#   r–   zDFM.convert_to6  sŒ   € à�T—[‘[Ò Ø—9‘9“;ÐØ”rŠ\˜dŸk™k¬RÒ/Ø—9‘9œUŸ^™^¨D¯H©HÓ5°t·z±zÀ6ÓJÐJØ×"Ñ" 6Ô*à—;‘;“=×+Ñ+¨FÓ3×:Ñ:Ó<Ð<ô &Ð&KÓLÐLr+   c           	      óÂ   — | j                   \  }}|dk  r||z  }|dk  r||z  }	 | j                  ||f   S # t        $ r t        d|› d|› d| j                   › �«      ‚w xY w)zGet the ``(i, j)``-th entry.r   úInvalid indices (ú, ú) for Matrix of shape ©r   r"   r   Ú
IndexError)r-   r�   r�   rH   rŽ   s        r#   ÚgetitemzDFM.getitemD  s~   € ð �z‰z‰ˆˆ1ØˆqŠ5Ø�‰FˆAØˆqŠ5Ø�‰FˆAð	]Ø—8‘8˜A˜q˜D‘>Ð!øÜò 	]ÜÐ0°°°2°a°SÐ8NÈtÏzÉzÈlÐ[Ó\Ð\ð	]ús	   ¥6 ¶(Ac           	      óÄ   — | j                   \  }}|dk  r||z  }|dk  r||z  }	 || j                  ||f<   y# t        $ r t        d|› d|› d| j                   › �«      ‚w xY w)zSet the ``(i, j)``-th entry.r   r˜   r™   rš   Nr›   )r-   r�   r�   ÚvaluerH   rŽ   s         r#   ÚsetitemzDFM.setitemR  s}   € ð �z‰z‰ˆˆ1ØˆqŠ5Ø�‰FˆAØˆqŠ5Ø�‰FˆAð	]Ø"ˆD�H‰H�Q˜�TŠNøÜò 	]ÜÐ0°°°2°a°SÐ8NÈtÏzÉzÈlÐ[Ó\Ð\ð	]ús	   ¥7 ·(Ac           
      óÞ   — | j                   }|D ��cg c]  }|D �cg c]	  }|||f   ‘Œ c}‘Œ }}}t        |«      t        |«      f}| j                  ||| j                  «      S c c}w c c}}w )z%Extract a submatrix with no checking.)r"   rA   r_   r    )r-   Ú	i_indicesÚ	j_indicesÚMr�   r�   Úlolr   s           r#   Ú_extractzDFM._extract`  sd   € ð �H‰HˆØ5>×?° Ö+˜A��!�Q�$“Ô+Ð?ˆÑ?Ü�Y“¤ Y£Ð0ˆØ�~‰~˜c 5¨$¯+©+Ó6Ð6ùò ,ùÓ?s   ’	A)›A$©A)Á$A)c                 ó€  — | j                   \  }}g }g }|D ]H  }|dk  r||z   }n|}d|cxk  r|k  sn t        d|› d| j                   › �«      ‚|j                  |«       ŒJ |D ]H  }	|	dk  r|	|z   }
n|	}
d|
cxk  r|k  sn t        d|	› d| j                   › �«      ‚|j                  |
«       ŒJ | j                  ||«      S )zExtract a submatrix.r   zInvalid row index z for Matrix of shape zInvalid column index )r   rœ   Úappendr¦   )r-   r   ÚcolslistrH   rŽ   Únew_rowsÚnew_colsr�   Úi_posr�   Új_poss              r#   ÚextractzDFM.extracth  sæ   € ð
 �z‰z‰ˆˆ1àˆØˆàò 	#ˆAØ�1ŠuØ˜A™‘à�Ø˜”> ”>Ü Ð#5°a°SÐ8MÈdÏjÉjÈ\Ð!ZÓ[Ð[Ø�O‰O˜EÕ"ð	#ð ò 	#ˆAØ�1ŠuØ˜A™‘à�Ø˜”> ”>Ü Ð#8¸¸Ð;PÐQU×Q[ÑQ[ÐP\Ð!]Ó^Ð^Ø�O‰O˜EÕ"ð	#ð �}‰}˜X xÓ0Ð0r+   c                 ó|   — | j                   \  }}t        |«      |   }t        |«      |   }| j                  ||«      S )zSlice a DFM.)r   rŒ   r¦   )r-   ÚrowsliceÚcolslicerH   rŽ   r¢   r£   s          r#   Úextract_slicezDFM.extract_slice†  s>   € ð �z‰z‰ˆˆ1Ü˜!“H˜XÑ&ˆ	Ü˜!“H˜XÑ&ˆ	Ø�}‰}˜Y¨	Ó2Ð2r+   c                 ó:   — | j                  | j                   «      S ©zNegate a DFM matrix.©r.   r"   rO   s    r#   ÚnegzDFM.negŽ  s   € à�}‰}˜dŸh™h˜YÓ'Ð'r+   c                 óR   — | j                  | j                  |j                  z   «      S )zAdd two DFM matrices.rµ   rZ   s     r#   ÚaddzDFM.add’  ó   € à�}‰}˜TŸX™X¨¯	©	Ñ1Ó2Ð2r+   c                 óR   — | j                  | j                  |j                  z
  «      S )zSubtract two DFM matrices.rµ   rZ   s     r#   ÚsubzDFM.sub–  r¹   r+   c                 ó>   — | j                  | j                  |z  «      S )z1Multiply a DFM matrix from the right by a scalar.rµ   rZ   s     r#   ÚmulzDFM.mulš  s   € à�}‰}˜TŸX™X¨Ñ-Ó.Ð.r+   c                 ó>   — | j                  || j                  z  «      S )z0Multiply a DFM matrix from the left by a scalar.rµ   rZ   s     r#   ÚrmulzDFM.rmulž  s   € à�}‰}˜U T§X¡XÑ-Ó.Ð.r+   c                 óx   — | j                  «       j                  |j                  «       «      j                  «       S )z/Elementwise multiplication of two DFM matrices.)rR   Úmul_elementwiserl   rZ   s     r#   rÁ   zDFM.mul_elementwise¢  s*   € ð �{‰{‹}×,Ñ,¨U¯\©\«^Ó<×CÑCÓEÐEr+   c                 óš   — | j                   |j                  f}| j                  | j                  |j                  z  || j                  «      S )zMultiply two DFM matrices.)r(   r)   r   r"   r    )r-   r[   r   s      r#   Úmatmulz
DFM.matmul§  s8   € à—‘˜EŸJ™JÐ'ˆØ�y‰y˜Ÿ™ E§I¡IÑ-¨u°d·k±kÓBÐBr+   c                 ó"   — | j                  «       S r´   )r¶   rO   s    r#   Ú__neg__zDFM.__neg__°  s   € à�x‰x‹zÐr+   c                 óP   — | j                  |«      }| j                   ||Ž ||«      S )zReturn a zero DFM matrix.)r   r   )r   r   r    rt   s       r#   Úzerosz	DFM.zeros´  s+   € ð ×"Ñ" 6Ó*ˆØ�x‰x™˜e˜ e¨VÓ4Ð4r+   c                 óJ   — t        j                  ||«      j                  «       S )zReturn a one DFM matrix.)rf   Úonesrl   )r   r   r    s      r#   rÉ   zDFM.ones¾  s   € ô �x‰x˜˜vÓ&×-Ñ-Ó/Ð/r+   c                 óJ   — t        j                  ||«      j                  «       S )z%Return the identity matrix of size n.)rf   Úeyerl   )r   rŽ   r    s      r#   rË   zDFM.eyeÄ  s   € ô �w‰w�q˜&Ó!×(Ñ(Ó*Ð*r+   c                 óJ   — t        j                  ||«      j                  «       S )zReturn a diagonal matrix.)rf   Údiagrl   )r   rs   r    s      r#   rÍ   zDFM.diagÊ  s   € ô �x‰x˜ &Ó)×0Ñ0Ó2Ð2r+   c                 ó^   — | j                  «       j                  ||«      j                  «       S )z/Apply a function to each entry of a DFM matrix.)rR   Ú	applyfuncrl   )r-   rt   r    s      r#   rÏ   zDFM.applyfuncÏ  s$   € à�{‰{‹}×&Ñ& t¨VÓ4×;Ñ;Ó=Ð=r+   c                 ó˜   — | j                  | j                  j                  «       | j                  | j                  f| j
                  «      S )zTranspose a DFM matrix.)r   r"   Ú	transposer)   r(   r    rO   s    r#   rÑ   zDFM.transposeÓ  s3   € à�y‰y˜Ÿ™×+Ñ+Ó-°·	±	¸4¿9¹9Ð/EÀtÇ{Á{ÓSÐSr+   c                 óš   —  | j                  «       j                  |D �cg c]  }|j                  «       ‘Œ c}Ž j                  «       S c c}w )zHorizontally stack matrices.)rR   Úhstackrl   ©r-   ÚothersÚos      r#   rÓ   z
DFM.hstack×  ó8   € à#ˆt�{‰{‹}×#Ñ#¸&Ö%A°Q a§h¡h¥jÒ%AÐB×IÑIÓKÐKùÒ%Aó   ŸAc                 óš   —  | j                  «       j                  |D �cg c]  }|j                  «       ‘Œ c}Ž j                  «       S c c}w )zVertically stack matrices.)rR   Úvstackrl   rÔ   s      r#   rÚ   z
DFM.vstackÛ  r×   rØ   c                 ó’   — | j                   }| j                  \  }}t        t        ||«      «      D �cg c]	  }|||f   ‘Œ c}S c c}w )z$Return the diagonal of a DFM matrix.)r"   r   rŒ   Úmin)r-   r¤   rH   rŽ   r�   s        r#   ÚdiagonalzDFM.diagonalß  s?   € à�H‰HˆØ�z‰z‰ˆˆ1Ü!&¤s¨1¨a£yÓ!1Ö2˜A��!�Q�$“Ò2Ð2ùÒ2s   ³Ac                 ó¬   — | j                   }t        | j                  «      D ]0  }t        t        || j                  «      «      D ]  }|||f   sŒ  y Œ2 y)z2Return ``True`` if the matrix is upper triangular.FT)r"   rŒ   r(   rÜ   r)   ©r-   r¤   r�   r�   s       r#   Úis_upperzDFM.is_upperå  sU   € à�H‰HˆÜ�t—y‘yÓ!ò 	!ˆAÜœ3˜q $§)¡)Ó,Ó-ò !�Ø�Q˜�T“7Ú ñ!ð	!ð r+   c                 ó    — | j                   }t        | j                  «      D ]*  }t        |dz   | j                  «      D ]  }|||f   sŒ  y Œ, y)z2Return ``True`` if the matrix is lower triangular.r   FT©r"   rŒ   r(   r)   rß   s       r#   Úis_lowerzDFM.is_lowerî  sT   € à�H‰HˆÜ�t—y‘yÓ!ò 	!ˆAÜ˜1˜q™5 $§)¡)Ó,ò !�Ø�Q˜�T“7Ú ñ!ð	!ð r+   c                 óF   — | j                  «       xr | j                  «       S )z*Return ``True`` if the matrix is diagonal.)rà   rã   rO   s    r#   Úis_diagonalzDFM.is_diagonal÷  s   € à�}‰}‹Ò2 4§=¡=£?Ð2r+   c                 ó˜   — | j                   }t        | j                  «      D ]&  }t        | j                  «      D ]  }|||f   sŒ  y Œ( y)z1Return ``True`` if the matrix is the zero matrix.FTrâ   rß   s       r#   Úis_zero_matrixzDFM.is_zero_matrixû  sN   € à�H‰HˆÜ�t—y‘yÓ!ò 	!ˆAÜ˜4Ÿ9™9Ó%ò !�Ø�Q˜�T“7Ú ñ!ð	!ð r+   c                 ó>   — | j                  «       j                  «       S )z5Return the number of non-zero elements in the matrix.)rR   ÚnnzrO   s    r#   ré   zDFM.nnz  ó   € à�{‰{‹}× Ñ Ó"Ð"r+   c                 ó>   — | j                  «       j                  «       S )z7Return the strongly connected components of the matrix.)rR   ÚsccrO   s    r#   rì   zDFM.scc  rê   r+   r   r   c                 ó6   — | j                   j                  «       S )a£  
        Compute the determinant of the matrix using FLINT.

        Examples
        ========

        >>> from sympy import Matrix
        >>> M = Matrix([[1, 2], [3, 4]])
        >>> dfm = M.to_DM().to_dfm()
        >>> dfm
        [[1, 2], [3, 4]]
        >>> dfm.det()
        -2

        Notes
        =====

        Calls the ``.det()`` method of the underlying FLINT matrix.

        For :ref:`ZZ` or :ref:`QQ` this calls ``fmpz_mat_det`` or
        ``fmpq_mat_det`` respectively.

        At the time of writing the implementation of ``fmpz_mat_det`` uses one
        of several algorithms depending on the size of the matrix and bit size
        of the entries. The algorithms used are:

        - Cofactor for very small (up to 4x4) matrices.
        - Bareiss for small (up to 25x25) matrices.
        - Modular algorithms for larger matrices (up to 60x60) or for larger
          matrices with large bit sizes.
        - Modular "accelerated" for larger matrices (60x60 upwards) if the bit
          size is smaller than the dimensions of the matrix.

        The implementation of ``fmpq_mat_det`` clears denominators from each
        row (not the whole matrix) and then calls ``fmpz_mat_det`` and divides
        by the product of the denominators.

        See Also
        ========

        sympy.polys.matrices.domainmatrix.DomainMatrix.det
            Higher level interface to compute the determinant of a matrix.
        )r"   ÚdetrO   s    r#   rî   zDFM.det  s   € ðb �x‰x�|‰|‹~Ðr+   c                 ó^   — | j                   j                  «       j                  «       ddd…   S )a#  
        Compute the characteristic polynomial of the matrix using FLINT.

        Examples
        ========

        >>> from sympy import Matrix
        >>> M = Matrix([[1, 2], [3, 4]])
        >>> dfm = M.to_DM().to_dfm()  # need ground types = 'flint'
        >>> dfm
        [[1, 2], [3, 4]]
        >>> dfm.charpoly()
        [1, -5, -2]

        Notes
        =====

        Calls the ``.charpoly()`` method of the underlying FLINT matrix.

        For :ref:`ZZ` or :ref:`QQ` this calls ``fmpz_mat_charpoly`` or
        ``fmpq_mat_charpoly`` respectively.

        At the time of writing the implementation of ``fmpq_mat_charpoly``
        clears a denominator from the whole matrix and then calls
        ``fmpz_mat_charpoly``. The coefficients of the characteristic
        polynomial are then multiplied by powers of the denominator.

        The ``fmpz_mat_charpoly`` method uses a modular algorithm with CRT
        reconstruction. The modular algorithm uses ``nmod_mat_charpoly`` which
        uses Berkowitz for small matrices and non-prime moduli or otherwise
        the Danilevsky method.

        See Also
        ========

        sympy.polys.matrices.domainmatrix.DomainMatrix.charpoly
            Higher level interface to compute the characteristic polynomial of
            a matrix.
        Néÿÿÿÿ)r"   ÚcharpolyÚcoeffsrO   s    r#   rñ   zDFM.charpoly?  s*   € ðT �x‰x× Ñ Ó"×)Ñ)Ó+©D¨b¨DÑ1Ð1r+   c                 óP  — | j                   }| j                  \  }}||k7  rt        d«      ‚|t        k(  rt	        d|z  «      ‚|t
        k(  s|j                  r*	 | j                  | j                  j                  «       «      S t        d|z  «      ‚# t        $ r t        d«      ‚w xY w)aä  
        Compute the inverse of a matrix using FLINT.

        Examples
        ========

        >>> from sympy import Matrix, QQ
        >>> M = Matrix([[1, 2], [3, 4]])
        >>> dfm = M.to_DM().to_dfm().convert_to(QQ)
        >>> dfm
        [[1, 2], [3, 4]]
        >>> dfm.inv()
        [[-2, 1], [3/2, -1/2]]
        >>> dfm.matmul(dfm.inv())
        [[1, 0], [0, 1]]

        Notes
        =====

        Calls the ``.inv()`` method of the underlying FLINT matrix.

        For now this will raise an error if the domain is :ref:`ZZ` but will
        use the FLINT method for :ref:`QQ`.

        The FLINT methods for :ref:`ZZ` and :ref:`QQ` are ``fmpz_mat_inv`` and
        ``fmpq_mat_inv`` respectively. The ``fmpz_mat_inv`` method computes an
        inverse with denominator. This is implemented by calling
        ``fmpz_mat_solve`` (see notes in :meth:`lu_solve` about the algorithm).

        The ``fmpq_mat_inv`` method clears denominators from each row and then
        multiplies those into the rhs identity matrix before calling
        ``fmpz_mat_solve``.

        See Also
        ========

        sympy.polys.matrices.domainmatrix.DomainMatrix.inv
            Higher level method for computing the inverse of a matrix.
        z!cannot invert a non-square matrixzfield expected, got %szmatrix is not invertiblez#DFM.inv() is not implemented for %s)r    r   r   r   r
   r   r7   r.   r"   ÚinvÚZeroDivisionErrorr   r:   )r-   ÚKrH   rŽ   s       r#   rô   zDFM.invk  s¤   € ðn �K‰KˆØ�z‰z‰ˆˆ1à�Š6Ü(Ð)LÓMÐMà”Š7ÜÐ 8¸1Ñ <Ó=Ð=Ø”"ŠW˜ŸšðMØ—}‘} T§X¡X§\¡\£^Ó4Ð4ô &Ð&KÈaÑ&OÓPÐPøô %ò MÜ0Ð1KÓLÐLðMús   Á(B ÂB%c                 óˆ   — | j                  «       j                  «       \  }}}|j                  «       |j                  «       |fS )z*Return the LU decomposition of the matrix.)rR   Úlurl   )r-   ÚLÚUÚswapss       r#   rø   zDFM.lu´  s5   € à—k‘k“m×&Ñ&Ó(‰ˆˆ1ˆeØ�x‰x‹z˜1Ÿ8™8›: uÐ,Ð,r+   c                 ó„   — | j                  «       j                  «       \  }}|j                  «       |j                  «       fS )z*Return the QR decomposition of the matrix.)rR   Úqrrl   )r-   ÚQÚRs      r#   rý   zDFM.qr¹  s1   € à�{‰{‹}×ÑÓ!‰ˆˆ1Ø�x‰x‹z˜1Ÿ8™8›:Ð%Ð%r+   c           
      óŒ  — | j                   |j                   k(  s%t        d| j                   ›d|j                   ›�«      ‚| j                   j                  st        d| j                   z  «      ‚| j                  \  }}|j                  \  }}||k7  rt	        d|›d|›d|›d|›�«      ‚||f}||k7  r;| j                  «       j                  |j                  «       «      j                  «       S 	 | j                  j                  |j                  «      }| j                  ||| j                   «      S # t        $ r t        d«      ‚w xY w)aè  
        Solve a matrix equation using FLINT.

        Examples
        ========

        >>> from sympy import Matrix, QQ
        >>> M = Matrix([[1, 2], [3, 4]])
        >>> dfm = M.to_DM().to_dfm().convert_to(QQ)
        >>> dfm
        [[1, 2], [3, 4]]
        >>> rhs = Matrix([1, 2]).to_DM().to_dfm().convert_to(QQ)
        >>> dfm.lu_solve(rhs)
        [[0], [1/2]]

        Notes
        =====

        Calls the ``.solve()`` method of the underlying FLINT matrix.

        For now this will raise an error if the domain is :ref:`ZZ` but will
        use the FLINT method for :ref:`QQ`.

        The FLINT methods for :ref:`ZZ` and :ref:`QQ` are ``fmpz_mat_solve``
        and ``fmpq_mat_solve`` respectively. The ``fmpq_mat_solve`` method
        uses one of two algorithms:

        - For small matrices (<25 rows) it clears denominators between the
          matrix and rhs and uses ``fmpz_mat_solve``.
        - For larger matrices it uses ``fmpq_mat_solve_dixon`` which is a
          modular approach with CRT reconstruction over :ref:`QQ`.

        The ``fmpz_mat_solve`` method uses one of four algorithms:

        - For very small (<= 3x3) matrices it uses a Cramer's rule.
        - For small (<= 15x15) matrices it uses a fraction-free LU solve.
        - Otherwise it uses either Dixon or another multimodular approach.

        See Also
        ========

        sympy.polys.matrices.domainmatrix.DomainMatrix.lu_solve
            Higher level interface to solve a matrix equation.
        zDomains must match: z != zField expected, got %szMatrix size mismatch: z * z vs z Matrix det == 0; not invertible.)r    r
   Úis_Fieldr   r   rR   Úlu_solverl   r"   Úsolverõ   r   r   )r-   ÚrhsrH   rŽ   r�   ÚkÚ	sol_shapeÚsols           r#   r  zDFM.lu_solveÉ  s  € ð\ �{‰{˜cŸj™jÒ(ÝÀ$Ç+Ã+ÈsÏzÊzÐ ZÓ[Ð[ð
 �{‰{×#Ò#ÜÐ 8¸4¿;¹;Ñ FÓGÐGà�z‰z‰ˆˆ1Ø�y‰y‰ˆˆ1Ø�Š6ÝÊQÒPQÒSTÑVWÐXÓYÐYØ˜�Fˆ	ð �Š6Ø—;‘;“=×)Ñ)¨#¯*©*«,Ó7×>Ñ>Ó@Ð@ð	QØ—(‘(—.‘. §¡Ó)ˆCð �y‰y˜˜i¨¯©Ó5Ð5øô !ò 	QÜ,Ð-OÓPÐPð	Qús   Ã,%D. Ä.Ec                 óŒ  — | j                   t        k(  rÉt        | j                  dd«      }|�°| j                  j	                  «       \  }}}}| j
                  \  }}| j                  |||f| j                   «      | j                  |||f| j                   «      | j                  |||f| j                   «      | j                  || j
                  | j                   «      fS | j                  «       j	                  «       \  }}	}
}|j                  «       }|	j                  «       }|
j                  «       }|j                  «       }||||fS )a  
        Fraction-free LU decomposition of DFM.

        Explanation
        ===========

        Uses `python-flint` if possible for a matrix of
        integers otherwise uses the DDM method.

        See Also
        ========

        sympy.polys.matrices.ddm.DDM.fflu
        ÚffluN)	r    r   Úgetattrr"   r	  r   r   rR   rl   )r-   r	  ÚPrù   ÚDrú   rH   rŽ   Úddm_pÚddm_lÚddm_dÚddm_us               r#   r	  zDFM.fflu  s  € ð �;‰;œ"ÒÜ˜4Ÿ8™8 V¨TÓ2ˆDØÐØ!ŸX™XŸ]™]›_‘
��1�a˜Ø—z‘z‘��1à—I‘I˜a ! Q ¨¯©Ó5Ø—I‘I˜a ! Q ¨¯©Ó5Ø—I‘I˜a ! Q ¨¯©Ó5Ø—I‘I˜a §¡¨T¯[©[Ó9ð	ð ð &*§[¡[£]×%7Ñ%7Ó%9Ñ"ˆˆu�e˜UØ�L‰L‹NˆØ�L‰L‹NˆØ�L‰L‹NˆØ�L‰L‹NˆØ�!�Q˜ˆzÐr+   c                 óh   — | j                  «       j                  «       \  }}|j                  «       |fS )ú/Return a basis for the nullspace of the matrix.)rR   Ú	nullspacerl   )r-   rp   Ú	nonpivotss      r#   r  zDFM.nullspace5  s-   € ð& Ÿ™›×0Ñ0Ó2‰ˆˆYØ�z‰z‹|˜YÐ&Ð&r+   Nc                 ól   — | j                  «       j                  |¬«      \  }}|j                  «       |fS )r  )Úpivots)rj   Únullspace_from_rrefrl   )r-   r  Úsdmr  s       r#   r  zDFM.nullspace_from_rrefK  s2   € ð Ÿ™›×:Ñ:À&Ð:ÓI‰ˆˆYØ�z‰z‹|˜YÐ&Ð&r+   c                 óZ   — | j                  «       j                  «       j                  «       S )z+Return a particular solution to the system.)rR   Ú
particularrl   rO   s    r#   r  zDFM.particularQ  s    € à�{‰{‹}×'Ñ'Ó)×0Ñ0Ó2Ð2r+   c                 ó  — d„ } ||«      } ||«      }d|cxk  rdk  st        d«      ‚ t        d«      ‚| j                  \  }}| j                  j                  «       |k7  rt	        d«      ‚| j                  j                  |||||¬«      S )zACall the fmpz_mat.lll() method but check rank to avoid segfaults.c                 ó˜   — t        j                  | «      r+t        | j                  «      t        | j                  «      z  S t        | «      S rG   )r   Úof_typeÚfloatÚ	numeratorÚdenominator)Úxs    r#   Úto_floatzDFM._lll.<locals>.to_float_  s3   € Ü�z‰z˜!Œ}Ü˜QŸ[™[Ó)¬E°!·-±-Ó,@Ñ@Ð@ä˜Q“x�r+   g      Ð?r   z delta must be between 0.25 and 1z-Matrix must have full row rank for Flint LLL.)Ú	transformÚdeltaÚetar"   Úgram)r   r   r"   Úrankr   Úlll)	r-   r#  r$  r%  r"   r&  r"  rH   rŽ   s	            r#   Ú_lllzDFM._lllU  s’   € ò	 ñ ˜“ˆÙ�s‹mˆà�eÔ˜aÒÜÐAÓBÐBð  ÜÐAÓBÐBð �z‰z‰ˆˆ1Ø�8‰8�=‰=‹?˜aÒÜÐMÓNÐNð �x‰x�|‰| i°uÀ#È3ÐUYˆ|ÓZÐZr+   c                 óæ   — | j                   t        k7  rt        d| j                   z  «      ‚| j                  | j                  kD  rt        d«      ‚| j                  |¬«      }| j                  |«      S )a  Compute LLL-reduced basis using FLINT.

        See :meth:`lll_transform` for more information.

        Examples
        ========

        >>> from sympy import Matrix
        >>> M = Matrix([[1, 2, 3], [4, 5, 6]])
        >>> M.to_DM().to_dfm().lll()
        [[2, 1, 0], [-1, 1, 3]]

        See Also
        ========

        sympy.polys.matrices.domainmatrix.DomainMatrix.lll
            Higher level interface to compute LLL-reduced basis.
        lll_transform
            Compute LLL-reduced basis and transform matrix.
        úZZ expected, got %sú,Matrix must not have more rows than columns.)r$  )r    r   r
   r(   r)   r   r)  r.   )r-   r$  r"   s      r#   r(  zDFM.llls  s`   € ð, �;‰;œ"ÒÜÐ 5¸¿¹Ñ CÓDÐDØ�Y‰Y˜Ÿ™Ò"ÜÐMÓNÐNà�i‰i˜eˆiÓ$ˆØ�}‰}˜SÓ!Ð!r+   c                 ó\  — | j                   t        k7  rt        d| j                   z  «      ‚| j                  | j                  kD  rt        d«      ‚| j                  d|¬«      \  }}| j                  |«      }| j                  || j                  | j                  f| j                   «      }||fS )ad  Compute LLL-reduced basis and transform using FLINT.

        Examples
        ========

        >>> from sympy import Matrix
        >>> M = Matrix([[1, 2, 3], [4, 5, 6]]).to_DM().to_dfm()
        >>> M_lll, T = M.lll_transform()
        >>> M_lll
        [[2, 1, 0], [-1, 1, 3]]
        >>> T
        [[-2, 1], [3, -1]]
        >>> T.matmul(M) == M_lll
        True

        See Also
        ========

        sympy.polys.matrices.domainmatrix.DomainMatrix.lll
            Higher level interface to compute LLL-reduced basis.
        lll
            Compute LLL-reduced basis without transform matrix.
        r+  r,  T)r#  r$  )	r    r   r
   r(   r)   r   r)  r.   r   )r-   r$  r"   ÚTÚbasisÚT_dfms         r#   Úlll_transformzDFM.lll_transform‘  s’   € ð2 �;‰;œ"ÒÜÐ 5¸¿¹Ñ CÓDÐDØ�Y‰Y˜Ÿ™Ò"ÜÐMÓNÐNà—‘ T°�Ó7‰ˆˆQØ—‘˜cÓ"ˆØ—	‘	˜!˜dŸi™i¨¯©Ð3°T·[±[ÓAˆØ�eˆ|Ðr+   rG   )Fg®Gáz®ï?gR¸…ëQà?ÚzbasisÚapprox)g      è?)PÚ__name__Ú
__module__Ú__qualname__Ú__doc__ÚfmtÚis_DFMÚis_DDMr$   Úclassmethodr   r.   r&   r>   r   ÚpropertyrE   rS   rW   r\   r_   rb   rd   rR   rj   rl   rn   rq   ru   rx   rz   r|   r   r‚   r†   rˆ   r’   r”   r–   r�   r    r¦   r®   r²   r¶   r¸   r»   r½   r¿   rÁ   rÃ   rÅ   rÇ   rÉ   rË   rÍ   rÏ   rÑ   rÓ   rÚ   rÝ   rà   rã   rå   rç   ré   rì   r   rî   rñ   rô   rø   rý   r  r	  r  r  r  r)  r(  r1  r^   r+   r#   r   r   E   sñ  „ ñ ðD €CØ€FØ€Fò,ð ñó ðò7ð ñMó ðMð ñGó ðGð ñMó ðMð, ñ1ó ð1ò"ò/òEð ñ,ó ð,ò!ò3òFòFòòð  ñCó ðCð ñ	'ó ð	'ò"ò*ð ñAó ðAò&ð ñ9ó ð9ò&ð ñ9ó ð9ò$ò*òMò]ò]ò7ò1ò<3ò(ò3ò3ò/ò/òFò
Còð ñ5ó ð5ð ñ0ó ð0ð
 ñ+ó ð+ð
 ñ3ó ð3ò>òTòLòLò3òòò3òò#ò#ñ  WÔ-ñ0ó .ð0ñd  WÔ-ñ)2ó .ð)2ñV  WÔ-ñFQó .ðFQòP-ò
&ñ   WÔ-ñH6ó .ðH6òTòB'ó,'ò3ó[ñ<  WÔ-ò"ó .ð"ñ:  WÔ-ò ó .ñ r+   )rf   )ri   N)Úsympy.external.gmpyr   Úsympy.external.importtoolsr   Úsympy.utilities.decoratorr   Úsympy.polys.domainsr   r   Ú
exceptionsr	   r
   r   r   r   r   r   Ú__doctest_skip__r   Ú__all__r   Úsympy.polys.matrices.ddmrf   ri   r^   r+   r#   ú<module>rE     sv   ðõT -Ý 4Ý 8ç &÷÷ ñ ð �7ÒØ�uÐñ 	�gÓ€ð ˆ'€ñ  ' Ô+÷lð ló ,ðlõ` )Þ (r+   