Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(matrices): Add fraction free solvers for DomainMatrix #25346

Merged
merged 11 commits into from
Jul 14, 2023
12 changes: 7 additions & 5 deletions doc/src/modules/polys/domainmatrix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ In general, we represent a matrix without concerning about the :py:class:`~.Doma

.. currentmodule:: sympy.polys.matrices

.. autoclass:: sympy.polys.matrices.domainmatrix.DomainMatrix
.. automodule:: sympy.polys.matrices.domainmatrix
:members:

.. currentmodule:: sympy.polys.matrices.ddm
.. automodule:: sympy.polys.matrices.ddm
:members:

.. autoclass:: DDM
.. automodule:: sympy.polys.matrices.dense
:members:

.. currentmodule:: sympy.polys.matrices.sdm
.. automodule:: sympy.polys.matrices._typing
:members:

.. autoclass:: SDM
.. automodule:: sympy.polys.matrices.sdm
smichr marked this conversation as resolved.
Show resolved Hide resolved
:members:

.. currentmodule:: sympy.polys.matrices.normalforms
Expand Down
2 changes: 1 addition & 1 deletion sympy/matrices/determinant.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def _cofactor_matrix(M, method="berkowitz"):
minor_submatrix
"""

if not M.is_square or M.rows < 1:
if not M.is_square:
smichr marked this conversation as resolved.
Show resolved Hide resolved
raise NonSquareMatrixError()

return M._new(M.rows, M.cols,
Expand Down
4 changes: 4 additions & 0 deletions sympy/polys/matrices/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@


class RingElement(Protocol):
"""A ring element.

Must support ``+``, ``-``, ``*``, ``**`` and ``-``.
"""
def __add__(self: T, other: T, /) -> T: ...
def __sub__(self: T, other: T, /) -> T: ...
def __mul__(self: T, other: T, /) -> T: ...
Expand Down
48 changes: 47 additions & 1 deletion sympy/polys/matrices/ddm.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class takes care of copying etc and also stores a Domain object associated
ddm_irmul,
ddm_imatmul,
ddm_irref,
ddm_irref_den,
ddm_idet,
ddm_iinv,
ddm_ilu_split,
Expand Down Expand Up @@ -306,6 +307,26 @@ def to_ddm(self):
return self

def to_sdm(self):
"""
Convert to a :class:`~.SDM`.

Examples
========

>>> from sympy.polys.matrices.ddm import DDM
>>> from sympy import QQ
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
>>> A.to_sdm()
{0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}
>>> type(A.to_sdm())
<class 'sympy.polys.matrices.sdm.SDM'>

See Also
========

SDM
sympy.polys.matrices.sdm.SDM.to_ddm
"""
return SDM.from_list(self, self.shape, self.domain)

def convert_to(self, K):
Expand Down Expand Up @@ -543,13 +564,38 @@ def scc(a):
return a.to_sdm().scc()

def rref(a):
"""Reduced-row echelon form of a and list of pivots"""
"""Reduced-row echelon form of a and list of pivots.

See Also
========

sympy.polys.matrices.domainmatrix.DomainMatrix.rref
Higher level interface to this function.
sympy.polys.matrices.dense.ddm_irref
The underlying algorithm.
"""
b = a.copy()
K = a.domain
partial_pivot = K.is_RealField or K.is_ComplexField
pivots = ddm_irref(b, _partial_pivot=partial_pivot)
return b, pivots

def rref_den(a):
"""Reduced-row echelon form of a with denominator and list of pivots

See Also
========

sympy.polys.matrices.domainmatrix.DomainMatrix.rref_den
Higher level interface to this function.
sympy.polys.matrices.dense.ddm_irref_den
The underlying algorithm.
"""
b = a.copy()
K = a.domain
denom, pivots = ddm_irref_den(b, K)
return b, denom, pivots

def nullspace(a):
rref, pivots = a.rref()
rows, cols = a.shape
Expand Down
Loading