Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +5 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/_dfm.py +898 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/eigen.py +90 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/linsolve.py +230 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__init__.py +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_domainmatrix.cpython-310.pyc +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_domainscalar.cpython-310.pyc +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_eigen.cpython-310.pyc +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_inverse.cpython-310.pyc +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_linsolve.cpython-310.pyc +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_normalforms.cpython-310.pyc +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_xxm.cpython-310.pyc +0 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_eigen.py +90 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_inverse.py +193 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_linsolve.py +112 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_nullspace.py +209 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_rref.py +737 -0
- llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_xxm.py +864 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/bal_Latn.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/bn.dat +3 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/ca_FR.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/en_AT.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/en_SC.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/et_EE.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/fr_WF.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/gu.dat +3 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/he.dat +3 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/hnj.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/ru_KG.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/scn.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/so.dat +3 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/yue_Hant_HK.dat +0 -0
- vlmpy310/lib/python3.10/site-packages/babel/locale-data/zh_Hant.dat +3 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/__init__.py +55 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/__init__.cpython-310.pyc +0 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/exceptions.cpython-310.pyc +0 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/ext.cpython-310.pyc +0 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/fallback.cpython-310.pyc +0 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/exceptions.py +48 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/ext.py +170 -0
- vlmpy310/lib/python3.10/site-packages/msgpack/fallback.py +929 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/INSTALLER +1 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/LICENSE +20 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/METADATA +27 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/RECORD +15 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/REQUESTED +0 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/WHEEL +5 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/entry_points.txt +3 -0
- vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/top_level.txt +1 -0
- vlmpy310/lib/python3.10/site-packages/pyasn1-0.6.1.dist-info/INSTALLER +1 -0
.gitattributes
CHANGED
|
@@ -1163,3 +1163,8 @@ vlmpy310/lib/python3.10/site-packages/av.libs/libavformat-071c54bd.so.61.7.100 f
|
|
| 1163 |
vlmpy310/lib/python3.10/site-packages/transformers/models/oneformer/__pycache__/modeling_oneformer.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1164 |
vlmpy310/lib/python3.10/site-packages/transformers/models/seamless_m4t/__pycache__/modeling_seamless_m4t.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1165 |
llava_next/lib/python3.10/site-packages/rich/__pycache__/_emoji_codes.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1163 |
vlmpy310/lib/python3.10/site-packages/transformers/models/oneformer/__pycache__/modeling_oneformer.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1164 |
vlmpy310/lib/python3.10/site-packages/transformers/models/seamless_m4t/__pycache__/modeling_seamless_m4t.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1165 |
llava_next/lib/python3.10/site-packages/rich/__pycache__/_emoji_codes.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1166 |
+
vlmpy310/lib/python3.10/site-packages/babel/locale-data/he.dat filter=lfs diff=lfs merge=lfs -text
|
| 1167 |
+
vlmpy310/lib/python3.10/site-packages/babel/locale-data/zh_Hant.dat filter=lfs diff=lfs merge=lfs -text
|
| 1168 |
+
vlmpy310/lib/python3.10/site-packages/babel/locale-data/so.dat filter=lfs diff=lfs merge=lfs -text
|
| 1169 |
+
vlmpy310/lib/python3.10/site-packages/babel/locale-data/bn.dat filter=lfs diff=lfs merge=lfs -text
|
| 1170 |
+
vlmpy310/lib/python3.10/site-packages/babel/locale-data/gu.dat filter=lfs diff=lfs merge=lfs -text
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/_dfm.py
ADDED
|
@@ -0,0 +1,898 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# sympy.polys.matrices.dfm
|
| 3 |
+
#
|
| 4 |
+
# This modules defines the DFM class which is a wrapper for dense flint
|
| 5 |
+
# matrices as found in python-flint.
|
| 6 |
+
#
|
| 7 |
+
# As of python-flint 0.4.1 matrices over the following domains can be supported
|
| 8 |
+
# by python-flint:
|
| 9 |
+
#
|
| 10 |
+
# ZZ: flint.fmpz_mat
|
| 11 |
+
# QQ: flint.fmpq_mat
|
| 12 |
+
# GF(p): flint.nmod_mat (p prime and p < ~2**62)
|
| 13 |
+
#
|
| 14 |
+
# The underlying flint library has many more domains, but these are not yet
|
| 15 |
+
# supported by python-flint.
|
| 16 |
+
#
|
| 17 |
+
# The DFM class is a wrapper for the flint matrices and provides a common
|
| 18 |
+
# interface for all supported domains that is interchangeable with the DDM
|
| 19 |
+
# and SDM classes so that DomainMatrix can be used with any as its internal
|
| 20 |
+
# matrix representation.
|
| 21 |
+
#
|
| 22 |
+
|
| 23 |
+
# TODO:
|
| 24 |
+
#
|
| 25 |
+
# Implement the following methods that are provided by python-flint:
|
| 26 |
+
#
|
| 27 |
+
# - hnf (Hermite normal form)
|
| 28 |
+
# - snf (Smith normal form)
|
| 29 |
+
# - minpoly
|
| 30 |
+
# - is_hnf
|
| 31 |
+
# - is_snf
|
| 32 |
+
# - rank
|
| 33 |
+
#
|
| 34 |
+
# The other types DDM and SDM do not have these methods and the algorithms
|
| 35 |
+
# for hnf, snf and rank are already implemented. Algorithms for minpoly,
|
| 36 |
+
# is_hnf and is_snf would need to be added.
|
| 37 |
+
#
|
| 38 |
+
# Add more methods to python-flint to expose more of Flint's functionality
|
| 39 |
+
# and also to make some of the above methods simpler or more efficient e.g.
|
| 40 |
+
# slicing, fancy indexing etc.
|
| 41 |
+
|
| 42 |
+
from sympy.external.gmpy import GROUND_TYPES
|
| 43 |
+
from sympy.external.importtools import import_module
|
| 44 |
+
from sympy.utilities.decorator import doctest_depends_on
|
| 45 |
+
|
| 46 |
+
from sympy.polys.domains import ZZ, QQ
|
| 47 |
+
|
| 48 |
+
from .exceptions import (
|
| 49 |
+
DMBadInputError,
|
| 50 |
+
DMDomainError,
|
| 51 |
+
DMNonSquareMatrixError,
|
| 52 |
+
DMNonInvertibleMatrixError,
|
| 53 |
+
DMRankError,
|
| 54 |
+
DMShapeError,
|
| 55 |
+
DMValueError,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if GROUND_TYPES != 'flint':
|
| 60 |
+
__doctest_skip__ = ['*']
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
flint = import_module('flint')
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
__all__ = ['DFM']
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@doctest_depends_on(ground_types=['flint'])
|
| 70 |
+
class DFM:
|
| 71 |
+
"""
|
| 72 |
+
Dense FLINT matrix. This class is a wrapper for matrices from python-flint.
|
| 73 |
+
|
| 74 |
+
>>> from sympy.polys.domains import ZZ
|
| 75 |
+
>>> from sympy.polys.matrices.dfm import DFM
|
| 76 |
+
>>> dfm = DFM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
| 77 |
+
>>> dfm
|
| 78 |
+
[[1, 2], [3, 4]]
|
| 79 |
+
>>> dfm.rep
|
| 80 |
+
[1, 2]
|
| 81 |
+
[3, 4]
|
| 82 |
+
>>> type(dfm.rep) # doctest: +SKIP
|
| 83 |
+
<class 'flint._flint.fmpz_mat'>
|
| 84 |
+
|
| 85 |
+
Usually, the DFM class is not instantiated directly, but is created as the
|
| 86 |
+
internal representation of :class:`~.DomainMatrix`. When
|
| 87 |
+
`SYMPY_GROUND_TYPES` is set to `flint` and `python-flint` is installed, the
|
| 88 |
+
:class:`DFM` class is used automatically as the internal representation of
|
| 89 |
+
:class:`~.DomainMatrix` in dense format if the domain is supported by
|
| 90 |
+
python-flint.
|
| 91 |
+
|
| 92 |
+
>>> from sympy.polys.matrices.domainmatrix import DM
|
| 93 |
+
>>> dM = DM([[1, 2], [3, 4]], ZZ)
|
| 94 |
+
>>> dM.rep
|
| 95 |
+
[[1, 2], [3, 4]]
|
| 96 |
+
|
| 97 |
+
A :class:`~.DomainMatrix` can be converted to :class:`DFM` by calling the
|
| 98 |
+
:meth:`to_dfm` method:
|
| 99 |
+
|
| 100 |
+
>>> dM.to_dfm()
|
| 101 |
+
[[1, 2], [3, 4]]
|
| 102 |
+
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
fmt = 'dense'
|
| 106 |
+
is_DFM = True
|
| 107 |
+
is_DDM = False
|
| 108 |
+
|
| 109 |
+
def __new__(cls, rowslist, shape, domain):
|
| 110 |
+
"""Construct from a nested list."""
|
| 111 |
+
flint_mat = cls._get_flint_func(domain)
|
| 112 |
+
|
| 113 |
+
if 0 not in shape:
|
| 114 |
+
try:
|
| 115 |
+
rep = flint_mat(rowslist)
|
| 116 |
+
except (ValueError, TypeError):
|
| 117 |
+
raise DMBadInputError(f"Input should be a list of list of {domain}")
|
| 118 |
+
else:
|
| 119 |
+
rep = flint_mat(*shape)
|
| 120 |
+
|
| 121 |
+
return cls._new(rep, shape, domain)
|
| 122 |
+
|
| 123 |
+
@classmethod
|
| 124 |
+
def _new(cls, rep, shape, domain):
|
| 125 |
+
"""Internal constructor from a flint matrix."""
|
| 126 |
+
cls._check(rep, shape, domain)
|
| 127 |
+
obj = object.__new__(cls)
|
| 128 |
+
obj.rep = rep
|
| 129 |
+
obj.shape = obj.rows, obj.cols = shape
|
| 130 |
+
obj.domain = domain
|
| 131 |
+
return obj
|
| 132 |
+
|
| 133 |
+
def _new_rep(self, rep):
|
| 134 |
+
"""Create a new DFM with the same shape and domain but a new rep."""
|
| 135 |
+
return self._new(rep, self.shape, self.domain)
|
| 136 |
+
|
| 137 |
+
@classmethod
|
| 138 |
+
def _check(cls, rep, shape, domain):
|
| 139 |
+
repshape = (rep.nrows(), rep.ncols())
|
| 140 |
+
if repshape != shape:
|
| 141 |
+
raise DMBadInputError("Shape of rep does not match shape of DFM")
|
| 142 |
+
if domain == ZZ and not isinstance(rep, flint.fmpz_mat):
|
| 143 |
+
raise RuntimeError("Rep is not a flint.fmpz_mat")
|
| 144 |
+
elif domain == QQ and not isinstance(rep, flint.fmpq_mat):
|
| 145 |
+
raise RuntimeError("Rep is not a flint.fmpq_mat")
|
| 146 |
+
elif domain not in (ZZ, QQ):
|
| 147 |
+
raise NotImplementedError("Only ZZ and QQ are supported by DFM")
|
| 148 |
+
|
| 149 |
+
@classmethod
|
| 150 |
+
def _supports_domain(cls, domain):
|
| 151 |
+
"""Return True if the given domain is supported by DFM."""
|
| 152 |
+
return domain in (ZZ, QQ)
|
| 153 |
+
|
| 154 |
+
@classmethod
|
| 155 |
+
def _get_flint_func(cls, domain):
|
| 156 |
+
"""Return the flint matrix class for the given domain."""
|
| 157 |
+
if domain == ZZ:
|
| 158 |
+
return flint.fmpz_mat
|
| 159 |
+
elif domain == QQ:
|
| 160 |
+
return flint.fmpq_mat
|
| 161 |
+
else:
|
| 162 |
+
raise NotImplementedError("Only ZZ and QQ are supported by DFM")
|
| 163 |
+
|
| 164 |
+
@property
|
| 165 |
+
def _func(self):
|
| 166 |
+
"""Callable to create a flint matrix of the same domain."""
|
| 167 |
+
return self._get_flint_func(self.domain)
|
| 168 |
+
|
| 169 |
+
def __str__(self):
|
| 170 |
+
"""Return ``str(self)``."""
|
| 171 |
+
return str(self.to_ddm())
|
| 172 |
+
|
| 173 |
+
def __repr__(self):
|
| 174 |
+
"""Return ``repr(self)``."""
|
| 175 |
+
return f'DFM{repr(self.to_ddm())[3:]}'
|
| 176 |
+
|
| 177 |
+
def __eq__(self, other):
|
| 178 |
+
"""Return ``self == other``."""
|
| 179 |
+
if not isinstance(other, DFM):
|
| 180 |
+
return NotImplemented
|
| 181 |
+
# Compare domains first because we do *not* want matrices with
|
| 182 |
+
# different domains to be equal but e.g. a flint fmpz_mat and fmpq_mat
|
| 183 |
+
# with the same entries will compare equal.
|
| 184 |
+
return self.domain == other.domain and self.rep == other.rep
|
| 185 |
+
|
| 186 |
+
@classmethod
|
| 187 |
+
def from_list(cls, rowslist, shape, domain):
|
| 188 |
+
"""Construct from a nested list."""
|
| 189 |
+
return cls(rowslist, shape, domain)
|
| 190 |
+
|
| 191 |
+
def to_list(self):
|
| 192 |
+
"""Convert to a nested list."""
|
| 193 |
+
return self.rep.tolist()
|
| 194 |
+
|
| 195 |
+
def copy(self):
|
| 196 |
+
"""Return a copy of self."""
|
| 197 |
+
return self._new_rep(self._func(self.rep))
|
| 198 |
+
|
| 199 |
+
def to_ddm(self):
|
| 200 |
+
"""Convert to a DDM."""
|
| 201 |
+
return DDM.from_list(self.to_list(), self.shape, self.domain)
|
| 202 |
+
|
| 203 |
+
def to_sdm(self):
|
| 204 |
+
"""Convert to a SDM."""
|
| 205 |
+
return SDM.from_list(self.to_list(), self.shape, self.domain)
|
| 206 |
+
|
| 207 |
+
def to_dfm(self):
|
| 208 |
+
"""Return self."""
|
| 209 |
+
return self
|
| 210 |
+
|
| 211 |
+
def to_dfm_or_ddm(self):
|
| 212 |
+
"""
|
| 213 |
+
Convert to a :class:`DFM`.
|
| 214 |
+
|
| 215 |
+
This :class:`DFM` method exists to parallel the :class:`~.DDM` and
|
| 216 |
+
:class:`~.SDM` methods. For :class:`DFM` it will always return self.
|
| 217 |
+
|
| 218 |
+
See Also
|
| 219 |
+
========
|
| 220 |
+
|
| 221 |
+
to_ddm
|
| 222 |
+
to_sdm
|
| 223 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_dfm_or_ddm
|
| 224 |
+
"""
|
| 225 |
+
return self
|
| 226 |
+
|
| 227 |
+
@classmethod
|
| 228 |
+
def from_ddm(cls, ddm):
|
| 229 |
+
"""Convert from a DDM."""
|
| 230 |
+
return cls.from_list(ddm.to_list(), ddm.shape, ddm.domain)
|
| 231 |
+
|
| 232 |
+
@classmethod
|
| 233 |
+
def from_list_flat(cls, elements, shape, domain):
|
| 234 |
+
"""Inverse of :meth:`to_list_flat`."""
|
| 235 |
+
func = cls._get_flint_func(domain)
|
| 236 |
+
try:
|
| 237 |
+
rep = func(*shape, elements)
|
| 238 |
+
except ValueError:
|
| 239 |
+
raise DMBadInputError(f"Incorrect number of elements for shape {shape}")
|
| 240 |
+
except TypeError:
|
| 241 |
+
raise DMBadInputError(f"Input should be a list of {domain}")
|
| 242 |
+
return cls(rep, shape, domain)
|
| 243 |
+
|
| 244 |
+
def to_list_flat(self):
|
| 245 |
+
"""Convert to a flat list."""
|
| 246 |
+
return self.rep.entries()
|
| 247 |
+
|
| 248 |
+
def to_flat_nz(self):
|
| 249 |
+
"""Convert to a flat list of non-zeros."""
|
| 250 |
+
return self.to_ddm().to_flat_nz()
|
| 251 |
+
|
| 252 |
+
@classmethod
|
| 253 |
+
def from_flat_nz(cls, elements, data, domain):
|
| 254 |
+
"""Inverse of :meth:`to_flat_nz`."""
|
| 255 |
+
return DDM.from_flat_nz(elements, data, domain).to_dfm()
|
| 256 |
+
|
| 257 |
+
def to_dod(self):
|
| 258 |
+
"""Convert to a DOD."""
|
| 259 |
+
return self.to_ddm().to_dod()
|
| 260 |
+
|
| 261 |
+
@classmethod
|
| 262 |
+
def from_dod(cls, dod, shape, domain):
|
| 263 |
+
"""Inverse of :meth:`to_dod`."""
|
| 264 |
+
return DDM.from_dod(dod, shape, domain).to_dfm()
|
| 265 |
+
|
| 266 |
+
def to_dok(self):
|
| 267 |
+
"""Convert to a DOK."""
|
| 268 |
+
return self.to_ddm().to_dok()
|
| 269 |
+
|
| 270 |
+
@classmethod
|
| 271 |
+
def from_dok(cls, dok, shape, domain):
|
| 272 |
+
"""Inverse of :math:`to_dod`."""
|
| 273 |
+
return DDM.from_dok(dok, shape, domain).to_dfm()
|
| 274 |
+
|
| 275 |
+
def iter_values(self):
|
| 276 |
+
"""Iterater over the non-zero values of the matrix."""
|
| 277 |
+
m, n = self.shape
|
| 278 |
+
rep = self.rep
|
| 279 |
+
for i in range(m):
|
| 280 |
+
for j in range(n):
|
| 281 |
+
repij = rep[i, j]
|
| 282 |
+
if repij:
|
| 283 |
+
yield rep[i, j]
|
| 284 |
+
|
| 285 |
+
def iter_items(self):
|
| 286 |
+
"""Iterate over indices and values of nonzero elements of the matrix."""
|
| 287 |
+
m, n = self.shape
|
| 288 |
+
rep = self.rep
|
| 289 |
+
for i in range(m):
|
| 290 |
+
for j in range(n):
|
| 291 |
+
repij = rep[i, j]
|
| 292 |
+
if repij:
|
| 293 |
+
yield ((i, j), repij)
|
| 294 |
+
|
| 295 |
+
def convert_to(self, domain):
|
| 296 |
+
"""Convert to a new domain."""
|
| 297 |
+
if domain == self.domain:
|
| 298 |
+
return self.copy()
|
| 299 |
+
elif domain == QQ and self.domain == ZZ:
|
| 300 |
+
return self._new(flint.fmpq_mat(self.rep), self.shape, domain)
|
| 301 |
+
elif domain == ZZ and self.domain == QQ:
|
| 302 |
+
# XXX: python-flint has no fmpz_mat.from_fmpq_mat
|
| 303 |
+
return self.to_ddm().convert_to(domain).to_dfm()
|
| 304 |
+
else:
|
| 305 |
+
# It is the callers responsibility to convert to DDM before calling
|
| 306 |
+
# this method if the domain is not supported by DFM.
|
| 307 |
+
raise NotImplementedError("Only ZZ and QQ are supported by DFM")
|
| 308 |
+
|
| 309 |
+
def getitem(self, i, j):
|
| 310 |
+
"""Get the ``(i, j)``-th entry."""
|
| 311 |
+
# XXX: flint matrices do not support negative indices
|
| 312 |
+
# XXX: They also raise ValueError instead of IndexError
|
| 313 |
+
m, n = self.shape
|
| 314 |
+
if i < 0:
|
| 315 |
+
i += m
|
| 316 |
+
if j < 0:
|
| 317 |
+
j += n
|
| 318 |
+
try:
|
| 319 |
+
return self.rep[i, j]
|
| 320 |
+
except ValueError:
|
| 321 |
+
raise IndexError(f"Invalid indices ({i}, {j}) for Matrix of shape {self.shape}")
|
| 322 |
+
|
| 323 |
+
def setitem(self, i, j, value):
|
| 324 |
+
"""Set the ``(i, j)``-th entry."""
|
| 325 |
+
# XXX: flint matrices do not support negative indices
|
| 326 |
+
# XXX: They also raise ValueError instead of IndexError
|
| 327 |
+
m, n = self.shape
|
| 328 |
+
if i < 0:
|
| 329 |
+
i += m
|
| 330 |
+
if j < 0:
|
| 331 |
+
j += n
|
| 332 |
+
try:
|
| 333 |
+
self.rep[i, j] = value
|
| 334 |
+
except ValueError:
|
| 335 |
+
raise IndexError(f"Invalid indices ({i}, {j}) for Matrix of shape {self.shape}")
|
| 336 |
+
|
| 337 |
+
def _extract(self, i_indices, j_indices):
|
| 338 |
+
"""Extract a submatrix with no checking."""
|
| 339 |
+
# Indices must be positive and in range.
|
| 340 |
+
M = self.rep
|
| 341 |
+
lol = [[M[i, j] for j in j_indices] for i in i_indices]
|
| 342 |
+
shape = (len(i_indices), len(j_indices))
|
| 343 |
+
return self.from_list(lol, shape, self.domain)
|
| 344 |
+
|
| 345 |
+
def extract(self, rowslist, colslist):
|
| 346 |
+
"""Extract a submatrix."""
|
| 347 |
+
# XXX: flint matrices do not support fancy indexing or negative indices
|
| 348 |
+
#
|
| 349 |
+
# Check and convert negative indices before calling _extract.
|
| 350 |
+
m, n = self.shape
|
| 351 |
+
|
| 352 |
+
new_rows = []
|
| 353 |
+
new_cols = []
|
| 354 |
+
|
| 355 |
+
for i in rowslist:
|
| 356 |
+
if i < 0:
|
| 357 |
+
i_pos = i + m
|
| 358 |
+
else:
|
| 359 |
+
i_pos = i
|
| 360 |
+
if not 0 <= i_pos < m:
|
| 361 |
+
raise IndexError(f"Invalid row index {i} for Matrix of shape {self.shape}")
|
| 362 |
+
new_rows.append(i_pos)
|
| 363 |
+
|
| 364 |
+
for j in colslist:
|
| 365 |
+
if j < 0:
|
| 366 |
+
j_pos = j + n
|
| 367 |
+
else:
|
| 368 |
+
j_pos = j
|
| 369 |
+
if not 0 <= j_pos < n:
|
| 370 |
+
raise IndexError(f"Invalid column index {j} for Matrix of shape {self.shape}")
|
| 371 |
+
new_cols.append(j_pos)
|
| 372 |
+
|
| 373 |
+
return self._extract(new_rows, new_cols)
|
| 374 |
+
|
| 375 |
+
def extract_slice(self, rowslice, colslice):
|
| 376 |
+
"""Slice a DFM."""
|
| 377 |
+
# XXX: flint matrices do not support slicing
|
| 378 |
+
m, n = self.shape
|
| 379 |
+
i_indices = range(m)[rowslice]
|
| 380 |
+
j_indices = range(n)[colslice]
|
| 381 |
+
return self._extract(i_indices, j_indices)
|
| 382 |
+
|
| 383 |
+
def neg(self):
|
| 384 |
+
"""Negate a DFM matrix."""
|
| 385 |
+
return self._new_rep(-self.rep)
|
| 386 |
+
|
| 387 |
+
def add(self, other):
|
| 388 |
+
"""Add two DFM matrices."""
|
| 389 |
+
return self._new_rep(self.rep + other.rep)
|
| 390 |
+
|
| 391 |
+
def sub(self, other):
|
| 392 |
+
"""Subtract two DFM matrices."""
|
| 393 |
+
return self._new_rep(self.rep - other.rep)
|
| 394 |
+
|
| 395 |
+
def mul(self, other):
|
| 396 |
+
"""Multiply a DFM matrix from the right by a scalar."""
|
| 397 |
+
return self._new_rep(self.rep * other)
|
| 398 |
+
|
| 399 |
+
def rmul(self, other):
|
| 400 |
+
"""Multiply a DFM matrix from the left by a scalar."""
|
| 401 |
+
return self._new_rep(other * self.rep)
|
| 402 |
+
|
| 403 |
+
def mul_elementwise(self, other):
|
| 404 |
+
"""Elementwise multiplication of two DFM matrices."""
|
| 405 |
+
# XXX: flint matrices do not support elementwise multiplication
|
| 406 |
+
return self.to_ddm().mul_elementwise(other.to_ddm()).to_dfm()
|
| 407 |
+
|
| 408 |
+
def matmul(self, other):
|
| 409 |
+
"""Multiply two DFM matrices."""
|
| 410 |
+
shape = (self.rows, other.cols)
|
| 411 |
+
return self._new(self.rep * other.rep, shape, self.domain)
|
| 412 |
+
|
| 413 |
+
# XXX: For the most part DomainMatrix does not expect DDM, SDM, or DFM to
|
| 414 |
+
# have arithmetic operators defined. The only exception is negation.
|
| 415 |
+
# Perhaps that should be removed.
|
| 416 |
+
|
| 417 |
+
def __neg__(self):
|
| 418 |
+
"""Negate a DFM matrix."""
|
| 419 |
+
return self.neg()
|
| 420 |
+
|
| 421 |
+
@classmethod
|
| 422 |
+
def zeros(cls, shape, domain):
|
| 423 |
+
"""Return a zero DFM matrix."""
|
| 424 |
+
func = cls._get_flint_func(domain)
|
| 425 |
+
return cls._new(func(*shape), shape, domain)
|
| 426 |
+
|
| 427 |
+
# XXX: flint matrices do not have anything like ones or eye
|
| 428 |
+
# In the methods below we convert to DDM and then back to DFM which is
|
| 429 |
+
# probably about as efficient as implementing these methods directly.
|
| 430 |
+
|
| 431 |
+
@classmethod
|
| 432 |
+
def ones(cls, shape, domain):
|
| 433 |
+
"""Return a one DFM matrix."""
|
| 434 |
+
# XXX: flint matrices do not have anything like ones
|
| 435 |
+
return DDM.ones(shape, domain).to_dfm()
|
| 436 |
+
|
| 437 |
+
@classmethod
|
| 438 |
+
def eye(cls, n, domain):
|
| 439 |
+
"""Return the identity matrix of size n."""
|
| 440 |
+
# XXX: flint matrices do not have anything like eye
|
| 441 |
+
return DDM.eye(n, domain).to_dfm()
|
| 442 |
+
|
| 443 |
+
@classmethod
|
| 444 |
+
def diag(cls, elements, domain):
|
| 445 |
+
"""Return a diagonal matrix."""
|
| 446 |
+
return DDM.diag(elements, domain).to_dfm()
|
| 447 |
+
|
| 448 |
+
def applyfunc(self, func, domain):
|
| 449 |
+
"""Apply a function to each entry of a DFM matrix."""
|
| 450 |
+
return self.to_ddm().applyfunc(func, domain).to_dfm()
|
| 451 |
+
|
| 452 |
+
def transpose(self):
|
| 453 |
+
"""Transpose a DFM matrix."""
|
| 454 |
+
return self._new(self.rep.transpose(), (self.cols, self.rows), self.domain)
|
| 455 |
+
|
| 456 |
+
def hstack(self, *others):
|
| 457 |
+
"""Horizontally stack matrices."""
|
| 458 |
+
return self.to_ddm().hstack(*[o.to_ddm() for o in others]).to_dfm()
|
| 459 |
+
|
| 460 |
+
def vstack(self, *others):
|
| 461 |
+
"""Vertically stack matrices."""
|
| 462 |
+
return self.to_ddm().vstack(*[o.to_ddm() for o in others]).to_dfm()
|
| 463 |
+
|
| 464 |
+
def diagonal(self):
|
| 465 |
+
"""Return the diagonal of a DFM matrix."""
|
| 466 |
+
M = self.rep
|
| 467 |
+
m, n = self.shape
|
| 468 |
+
return [M[i, i] for i in range(min(m, n))]
|
| 469 |
+
|
| 470 |
+
def is_upper(self):
|
| 471 |
+
"""Return ``True`` if the matrix is upper triangular."""
|
| 472 |
+
M = self.rep
|
| 473 |
+
for i in range(self.rows):
|
| 474 |
+
for j in range(i):
|
| 475 |
+
if M[i, j]:
|
| 476 |
+
return False
|
| 477 |
+
return True
|
| 478 |
+
|
| 479 |
+
def is_lower(self):
|
| 480 |
+
"""Return ``True`` if the matrix is lower triangular."""
|
| 481 |
+
M = self.rep
|
| 482 |
+
for i in range(self.rows):
|
| 483 |
+
for j in range(i + 1, self.cols):
|
| 484 |
+
if M[i, j]:
|
| 485 |
+
return False
|
| 486 |
+
return True
|
| 487 |
+
|
| 488 |
+
def is_diagonal(self):
|
| 489 |
+
"""Return ``True`` if the matrix is diagonal."""
|
| 490 |
+
return self.is_upper() and self.is_lower()
|
| 491 |
+
|
| 492 |
+
def is_zero_matrix(self):
|
| 493 |
+
"""Return ``True`` if the matrix is the zero matrix."""
|
| 494 |
+
M = self.rep
|
| 495 |
+
for i in range(self.rows):
|
| 496 |
+
for j in range(self.cols):
|
| 497 |
+
if M[i, j]:
|
| 498 |
+
return False
|
| 499 |
+
return True
|
| 500 |
+
|
| 501 |
+
def nnz(self):
|
| 502 |
+
"""Return the number of non-zero elements in the matrix."""
|
| 503 |
+
return self.to_ddm().nnz()
|
| 504 |
+
|
| 505 |
+
def scc(self):
|
| 506 |
+
"""Return the strongly connected components of the matrix."""
|
| 507 |
+
return self.to_ddm().scc()
|
| 508 |
+
|
| 509 |
+
@doctest_depends_on(ground_types='flint')
|
| 510 |
+
def det(self):
|
| 511 |
+
"""
|
| 512 |
+
Compute the determinant of the matrix using FLINT.
|
| 513 |
+
|
| 514 |
+
Examples
|
| 515 |
+
========
|
| 516 |
+
|
| 517 |
+
>>> from sympy import Matrix
|
| 518 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 519 |
+
>>> dfm = M.to_DM().to_dfm()
|
| 520 |
+
>>> dfm
|
| 521 |
+
[[1, 2], [3, 4]]
|
| 522 |
+
>>> dfm.det()
|
| 523 |
+
-2
|
| 524 |
+
|
| 525 |
+
Notes
|
| 526 |
+
=====
|
| 527 |
+
|
| 528 |
+
Calls the ``.det()`` method of the underlying FLINT matrix.
|
| 529 |
+
|
| 530 |
+
For :ref:`ZZ` or :ref:`QQ` this calls ``fmpz_mat_det`` or
|
| 531 |
+
``fmpq_mat_det`` respectively.
|
| 532 |
+
|
| 533 |
+
At the time of writing the implementation of ``fmpz_mat_det`` uses one
|
| 534 |
+
of several algorithms depending on the size of the matrix and bit size
|
| 535 |
+
of the entries. The algorithms used are:
|
| 536 |
+
|
| 537 |
+
- Cofactor for very small (up to 4x4) matrices.
|
| 538 |
+
- Bareiss for small (up to 25x25) matrices.
|
| 539 |
+
- Modular algorithms for larger matrices (up to 60x60) or for larger
|
| 540 |
+
matrices with large bit sizes.
|
| 541 |
+
- Modular "accelerated" for larger matrices (60x60 upwards) if the bit
|
| 542 |
+
size is smaller than the dimensions of the matrix.
|
| 543 |
+
|
| 544 |
+
The implementation of ``fmpq_mat_det`` clears denominators from each
|
| 545 |
+
row (not the whole matrix) and then calls ``fmpz_mat_det`` and divides
|
| 546 |
+
by the product of the denominators.
|
| 547 |
+
|
| 548 |
+
See Also
|
| 549 |
+
========
|
| 550 |
+
|
| 551 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.det
|
| 552 |
+
Higher level interface to compute the determinant of a matrix.
|
| 553 |
+
"""
|
| 554 |
+
# XXX: At least the first three algorithms described above should also
|
| 555 |
+
# be implemented in the pure Python DDM and SDM classes which at the
|
| 556 |
+
# time of writng just use Bareiss for all matrices and domains.
|
| 557 |
+
# Probably in Python the thresholds would be different though.
|
| 558 |
+
return self.rep.det()
|
| 559 |
+
|
| 560 |
+
@doctest_depends_on(ground_types='flint')
|
| 561 |
+
def charpoly(self):
|
| 562 |
+
"""
|
| 563 |
+
Compute the characteristic polynomial of the matrix using FLINT.
|
| 564 |
+
|
| 565 |
+
Examples
|
| 566 |
+
========
|
| 567 |
+
|
| 568 |
+
>>> from sympy import Matrix
|
| 569 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 570 |
+
>>> dfm = M.to_DM().to_dfm() # need ground types = 'flint'
|
| 571 |
+
>>> dfm
|
| 572 |
+
[[1, 2], [3, 4]]
|
| 573 |
+
>>> dfm.charpoly()
|
| 574 |
+
[1, -5, -2]
|
| 575 |
+
|
| 576 |
+
Notes
|
| 577 |
+
=====
|
| 578 |
+
|
| 579 |
+
Calls the ``.charpoly()`` method of the underlying FLINT matrix.
|
| 580 |
+
|
| 581 |
+
For :ref:`ZZ` or :ref:`QQ` this calls ``fmpz_mat_charpoly`` or
|
| 582 |
+
``fmpq_mat_charpoly`` respectively.
|
| 583 |
+
|
| 584 |
+
At the time of writing the implementation of ``fmpq_mat_charpoly``
|
| 585 |
+
clears a denominator from the whole matrix and then calls
|
| 586 |
+
``fmpz_mat_charpoly``. The coefficients of the characteristic
|
| 587 |
+
polynomial are then multiplied by powers of the denominator.
|
| 588 |
+
|
| 589 |
+
The ``fmpz_mat_charpoly`` method uses a modular algorithm with CRT
|
| 590 |
+
reconstruction. The modular algorithm uses ``nmod_mat_charpoly`` which
|
| 591 |
+
uses Berkowitz for small matrices and non-prime moduli or otherwise
|
| 592 |
+
the Danilevsky method.
|
| 593 |
+
|
| 594 |
+
See Also
|
| 595 |
+
========
|
| 596 |
+
|
| 597 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.charpoly
|
| 598 |
+
Higher level interface to compute the characteristic polynomial of
|
| 599 |
+
a matrix.
|
| 600 |
+
"""
|
| 601 |
+
# FLINT polynomial coefficients are in reverse order compared to SymPy.
|
| 602 |
+
return self.rep.charpoly().coeffs()[::-1]
|
| 603 |
+
|
| 604 |
+
@doctest_depends_on(ground_types='flint')
|
| 605 |
+
def inv(self):
|
| 606 |
+
"""
|
| 607 |
+
Compute the inverse of a matrix using FLINT.
|
| 608 |
+
|
| 609 |
+
Examples
|
| 610 |
+
========
|
| 611 |
+
|
| 612 |
+
>>> from sympy import Matrix, QQ
|
| 613 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 614 |
+
>>> dfm = M.to_DM().to_dfm().convert_to(QQ)
|
| 615 |
+
>>> dfm
|
| 616 |
+
[[1, 2], [3, 4]]
|
| 617 |
+
>>> dfm.inv()
|
| 618 |
+
[[-2, 1], [3/2, -1/2]]
|
| 619 |
+
>>> dfm.matmul(dfm.inv())
|
| 620 |
+
[[1, 0], [0, 1]]
|
| 621 |
+
|
| 622 |
+
Notes
|
| 623 |
+
=====
|
| 624 |
+
|
| 625 |
+
Calls the ``.inv()`` method of the underlying FLINT matrix.
|
| 626 |
+
|
| 627 |
+
For now this will raise an error if the domain is :ref:`ZZ` but will
|
| 628 |
+
use the FLINT method for :ref:`QQ`.
|
| 629 |
+
|
| 630 |
+
The FLINT methods for :ref:`ZZ` and :ref:`QQ` are ``fmpz_mat_inv`` and
|
| 631 |
+
``fmpq_mat_inv`` respectively. The ``fmpz_mat_inv`` method computes an
|
| 632 |
+
inverse with denominator. This is implemented by calling
|
| 633 |
+
``fmpz_mat_solve`` (see notes in :meth:`lu_solve` about the algorithm).
|
| 634 |
+
|
| 635 |
+
The ``fmpq_mat_inv`` method clears denominators from each row and then
|
| 636 |
+
multiplies those into the rhs identity matrix before calling
|
| 637 |
+
``fmpz_mat_solve``.
|
| 638 |
+
|
| 639 |
+
See Also
|
| 640 |
+
========
|
| 641 |
+
|
| 642 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.inv
|
| 643 |
+
Higher level method for computing the inverse of a matrix.
|
| 644 |
+
"""
|
| 645 |
+
# TODO: Implement similar algorithms for DDM and SDM.
|
| 646 |
+
#
|
| 647 |
+
# XXX: The flint fmpz_mat and fmpq_mat inv methods both return fmpq_mat
|
| 648 |
+
# by default. The fmpz_mat method has an optional argument to return
|
| 649 |
+
# fmpz_mat instead for unimodular matrices.
|
| 650 |
+
#
|
| 651 |
+
# The convention in DomainMatrix is to raise an error if the matrix is
|
| 652 |
+
# not over a field regardless of whether the matrix is invertible over
|
| 653 |
+
# its domain or over any associated field. Maybe DomainMatrix.inv
|
| 654 |
+
# should be changed to always return a matrix over an associated field
|
| 655 |
+
# except with a unimodular argument for returning an inverse over a
|
| 656 |
+
# ring if possible.
|
| 657 |
+
#
|
| 658 |
+
# For now we follow the existing DomainMatrix convention...
|
| 659 |
+
K = self.domain
|
| 660 |
+
m, n = self.shape
|
| 661 |
+
|
| 662 |
+
if m != n:
|
| 663 |
+
raise DMNonSquareMatrixError("cannot invert a non-square matrix")
|
| 664 |
+
|
| 665 |
+
if K == ZZ:
|
| 666 |
+
raise DMDomainError("field expected, got %s" % K)
|
| 667 |
+
elif K == QQ:
|
| 668 |
+
try:
|
| 669 |
+
return self._new_rep(self.rep.inv())
|
| 670 |
+
except ZeroDivisionError:
|
| 671 |
+
raise DMNonInvertibleMatrixError("matrix is not invertible")
|
| 672 |
+
else:
|
| 673 |
+
# If more domains are added for DFM then we will need to consider
|
| 674 |
+
# what happens here.
|
| 675 |
+
raise NotImplementedError("DFM.inv() is not implemented for %s" % K)
|
| 676 |
+
|
| 677 |
+
def lu(self):
|
| 678 |
+
"""Return the LU decomposition of the matrix."""
|
| 679 |
+
L, U, swaps = self.to_ddm().lu()
|
| 680 |
+
return L.to_dfm(), U.to_dfm(), swaps
|
| 681 |
+
|
| 682 |
+
# XXX: The lu_solve function should be renamed to solve. Whether or not it
|
| 683 |
+
# uses an LU decomposition is an implementation detail. A method called
|
| 684 |
+
# lu_solve would make sense for a situation in which an LU decomposition is
|
| 685 |
+
# reused several times to solve iwth different rhs but that would imply a
|
| 686 |
+
# different call signature.
|
| 687 |
+
#
|
| 688 |
+
# The underlying python-flint method has an algorithm= argument so we could
|
| 689 |
+
# use that and have e.g. solve_lu and solve_modular or perhaps also a
|
| 690 |
+
# method= argument to choose between the two. Flint itself has more
|
| 691 |
+
# possible algorithms to choose from than are exposed by python-flint.
|
| 692 |
+
|
| 693 |
+
@doctest_depends_on(ground_types='flint')
|
| 694 |
+
def lu_solve(self, rhs):
|
| 695 |
+
"""
|
| 696 |
+
Solve a matrix equation using FLINT.
|
| 697 |
+
|
| 698 |
+
Examples
|
| 699 |
+
========
|
| 700 |
+
|
| 701 |
+
>>> from sympy import Matrix, QQ
|
| 702 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 703 |
+
>>> dfm = M.to_DM().to_dfm().convert_to(QQ)
|
| 704 |
+
>>> dfm
|
| 705 |
+
[[1, 2], [3, 4]]
|
| 706 |
+
>>> rhs = Matrix([1, 2]).to_DM().to_dfm().convert_to(QQ)
|
| 707 |
+
>>> dfm.lu_solve(rhs)
|
| 708 |
+
[[0], [1/2]]
|
| 709 |
+
|
| 710 |
+
Notes
|
| 711 |
+
=====
|
| 712 |
+
|
| 713 |
+
Calls the ``.solve()`` method of the underlying FLINT matrix.
|
| 714 |
+
|
| 715 |
+
For now this will raise an error if the domain is :ref:`ZZ` but will
|
| 716 |
+
use the FLINT method for :ref:`QQ`.
|
| 717 |
+
|
| 718 |
+
The FLINT methods for :ref:`ZZ` and :ref:`QQ` are ``fmpz_mat_solve``
|
| 719 |
+
and ``fmpq_mat_solve`` respectively. The ``fmpq_mat_solve`` method
|
| 720 |
+
uses one of two algorithms:
|
| 721 |
+
|
| 722 |
+
- For small matrices (<25 rows) it clears denominators between the
|
| 723 |
+
matrix and rhs and uses ``fmpz_mat_solve``.
|
| 724 |
+
- For larger matrices it uses ``fmpq_mat_solve_dixon`` which is a
|
| 725 |
+
modular approach with CRT reconstruction over :ref:`QQ`.
|
| 726 |
+
|
| 727 |
+
The ``fmpz_mat_solve`` method uses one of four algorithms:
|
| 728 |
+
|
| 729 |
+
- For very small (<= 3x3) matrices it uses a Cramer's rule.
|
| 730 |
+
- For small (<= 15x15) matrices it uses a fraction-free LU solve.
|
| 731 |
+
- Otherwise it uses either Dixon or another multimodular approach.
|
| 732 |
+
|
| 733 |
+
See Also
|
| 734 |
+
========
|
| 735 |
+
|
| 736 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lu_solve
|
| 737 |
+
Higher level interface to solve a matrix equation.
|
| 738 |
+
"""
|
| 739 |
+
if not self.domain == rhs.domain:
|
| 740 |
+
raise DMDomainError("Domains must match: %s != %s" % (self.domain, rhs.domain))
|
| 741 |
+
|
| 742 |
+
# XXX: As for inv we should consider whether to return a matrix over
|
| 743 |
+
# over an associated field or attempt to find a solution in the ring.
|
| 744 |
+
# For now we follow the existing DomainMatrix convention...
|
| 745 |
+
if not self.domain.is_Field:
|
| 746 |
+
raise DMDomainError("Field expected, got %s" % self.domain)
|
| 747 |
+
|
| 748 |
+
m, n = self.shape
|
| 749 |
+
j, k = rhs.shape
|
| 750 |
+
if m != j:
|
| 751 |
+
raise DMShapeError("Matrix size mismatch: %s * %s vs %s * %s" % (m, n, j, k))
|
| 752 |
+
sol_shape = (n, k)
|
| 753 |
+
|
| 754 |
+
# XXX: The Flint solve method only handles square matrices. Probably
|
| 755 |
+
# Flint has functions that could be used to solve non-square systems
|
| 756 |
+
# but they are not exposed in python-flint yet. Alternatively we could
|
| 757 |
+
# put something here using the features that are available like rref.
|
| 758 |
+
if m != n:
|
| 759 |
+
return self.to_ddm().lu_solve(rhs.to_ddm()).to_dfm()
|
| 760 |
+
|
| 761 |
+
try:
|
| 762 |
+
sol = self.rep.solve(rhs.rep)
|
| 763 |
+
except ZeroDivisionError:
|
| 764 |
+
raise DMNonInvertibleMatrixError("Matrix det == 0; not invertible.")
|
| 765 |
+
|
| 766 |
+
return self._new(sol, sol_shape, self.domain)
|
| 767 |
+
|
| 768 |
+
def nullspace(self):
|
| 769 |
+
"""Return a basis for the nullspace of the matrix."""
|
| 770 |
+
# Code to compute nullspace using flint:
|
| 771 |
+
#
|
| 772 |
+
# V, nullity = self.rep.nullspace()
|
| 773 |
+
# V_dfm = self._new_rep(V)._extract(range(self.rows), range(nullity))
|
| 774 |
+
#
|
| 775 |
+
# XXX: That gives the nullspace but does not give us nonpivots. So we
|
| 776 |
+
# use the slower DDM method anyway. It would be better to change the
|
| 777 |
+
# signature of the nullspace method to not return nonpivots.
|
| 778 |
+
#
|
| 779 |
+
# XXX: Also python-flint exposes a nullspace method for fmpz_mat but
|
| 780 |
+
# not for fmpq_mat. This is the reverse of the situation for DDM etc
|
| 781 |
+
# which only allow nullspace over a field. The nullspace method for
|
| 782 |
+
# DDM, SDM etc should be changed to allow nullspace over ZZ as well.
|
| 783 |
+
# The DomainMatrix nullspace method does allow the domain to be a ring
|
| 784 |
+
# but does not directly call the lower-level nullspace methods and uses
|
| 785 |
+
# rref_den instead. Nullspace methods should also be added to all
|
| 786 |
+
# matrix types in python-flint.
|
| 787 |
+
ddm, nonpivots = self.to_ddm().nullspace()
|
| 788 |
+
return ddm.to_dfm(), nonpivots
|
| 789 |
+
|
| 790 |
+
def nullspace_from_rref(self, pivots=None):
|
| 791 |
+
"""Return a basis for the nullspace of the matrix."""
|
| 792 |
+
# XXX: Use the flint nullspace method!!!
|
| 793 |
+
sdm, nonpivots = self.to_sdm().nullspace_from_rref(pivots=pivots)
|
| 794 |
+
return sdm.to_dfm(), nonpivots
|
| 795 |
+
|
| 796 |
+
def particular(self):
|
| 797 |
+
"""Return a particular solution to the system."""
|
| 798 |
+
return self.to_ddm().particular().to_dfm()
|
| 799 |
+
|
| 800 |
+
def _lll(self, transform=False, delta=0.99, eta=0.51, rep='zbasis', gram='approx'):
|
| 801 |
+
"""Call the fmpz_mat.lll() method but check rank to avoid segfaults."""
|
| 802 |
+
|
| 803 |
+
# XXX: There are tests that pass e.g. QQ(5,6) for delta. That fails
|
| 804 |
+
# with a TypeError in flint because if QQ is fmpq then conversion with
|
| 805 |
+
# float fails. We handle that here but there are two better fixes:
|
| 806 |
+
#
|
| 807 |
+
# - Make python-flint's fmpq convert with float(x)
|
| 808 |
+
# - Change the tests because delta should just be a float.
|
| 809 |
+
|
| 810 |
+
def to_float(x):
|
| 811 |
+
if QQ.of_type(x):
|
| 812 |
+
return float(x.numerator) / float(x.denominator)
|
| 813 |
+
else:
|
| 814 |
+
return float(x)
|
| 815 |
+
|
| 816 |
+
delta = to_float(delta)
|
| 817 |
+
eta = to_float(eta)
|
| 818 |
+
|
| 819 |
+
if not 0.25 < delta < 1:
|
| 820 |
+
raise DMValueError("delta must be between 0.25 and 1")
|
| 821 |
+
|
| 822 |
+
# XXX: The flint lll method segfaults if the matrix is not full rank.
|
| 823 |
+
m, n = self.shape
|
| 824 |
+
if self.rep.rank() != m:
|
| 825 |
+
raise DMRankError("Matrix must have full row rank for Flint LLL.")
|
| 826 |
+
|
| 827 |
+
# Actually call the flint method.
|
| 828 |
+
return self.rep.lll(transform=transform, delta=delta, eta=eta, rep=rep, gram=gram)
|
| 829 |
+
|
| 830 |
+
@doctest_depends_on(ground_types='flint')
|
| 831 |
+
def lll(self, delta=0.75):
|
| 832 |
+
"""Compute LLL-reduced basis using FLINT.
|
| 833 |
+
|
| 834 |
+
See :meth:`lll_transform` for more information.
|
| 835 |
+
|
| 836 |
+
Examples
|
| 837 |
+
========
|
| 838 |
+
|
| 839 |
+
>>> from sympy import Matrix
|
| 840 |
+
>>> M = Matrix([[1, 2, 3], [4, 5, 6]])
|
| 841 |
+
>>> M.to_DM().to_dfm().lll()
|
| 842 |
+
[[2, 1, 0], [-1, 1, 3]]
|
| 843 |
+
|
| 844 |
+
See Also
|
| 845 |
+
========
|
| 846 |
+
|
| 847 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lll
|
| 848 |
+
Higher level interface to compute LLL-reduced basis.
|
| 849 |
+
lll_transform
|
| 850 |
+
Compute LLL-reduced basis and transform matrix.
|
| 851 |
+
"""
|
| 852 |
+
if self.domain != ZZ:
|
| 853 |
+
raise DMDomainError("ZZ expected, got %s" % self.domain)
|
| 854 |
+
elif self.rows > self.cols:
|
| 855 |
+
raise DMShapeError("Matrix must not have more rows than columns.")
|
| 856 |
+
|
| 857 |
+
rep = self._lll(delta=delta)
|
| 858 |
+
return self._new_rep(rep)
|
| 859 |
+
|
| 860 |
+
@doctest_depends_on(ground_types='flint')
|
| 861 |
+
def lll_transform(self, delta=0.75):
|
| 862 |
+
"""Compute LLL-reduced basis and transform using FLINT.
|
| 863 |
+
|
| 864 |
+
Examples
|
| 865 |
+
========
|
| 866 |
+
|
| 867 |
+
>>> from sympy import Matrix
|
| 868 |
+
>>> M = Matrix([[1, 2, 3], [4, 5, 6]]).to_DM().to_dfm()
|
| 869 |
+
>>> M_lll, T = M.lll_transform()
|
| 870 |
+
>>> M_lll
|
| 871 |
+
[[2, 1, 0], [-1, 1, 3]]
|
| 872 |
+
>>> T
|
| 873 |
+
[[-2, 1], [3, -1]]
|
| 874 |
+
>>> T.matmul(M) == M_lll
|
| 875 |
+
True
|
| 876 |
+
|
| 877 |
+
See Also
|
| 878 |
+
========
|
| 879 |
+
|
| 880 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lll
|
| 881 |
+
Higher level interface to compute LLL-reduced basis.
|
| 882 |
+
lll
|
| 883 |
+
Compute LLL-reduced basis without transform matrix.
|
| 884 |
+
"""
|
| 885 |
+
if self.domain != ZZ:
|
| 886 |
+
raise DMDomainError("ZZ expected, got %s" % self.domain)
|
| 887 |
+
elif self.rows > self.cols:
|
| 888 |
+
raise DMShapeError("Matrix must not have more rows than columns.")
|
| 889 |
+
|
| 890 |
+
rep, T = self._lll(transform=True, delta=delta)
|
| 891 |
+
basis = self._new_rep(rep)
|
| 892 |
+
T_dfm = self._new(T, (self.rows, self.rows), self.domain)
|
| 893 |
+
return basis, T_dfm
|
| 894 |
+
|
| 895 |
+
|
| 896 |
+
# Avoid circular imports
|
| 897 |
+
from sympy.polys.matrices.ddm import DDM
|
| 898 |
+
from sympy.polys.matrices.ddm import SDM
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/eigen.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
Routines for computing eigenvectors with DomainMatrix.
|
| 4 |
+
|
| 5 |
+
"""
|
| 6 |
+
from sympy.core.symbol import Dummy
|
| 7 |
+
|
| 8 |
+
from ..agca.extensions import FiniteExtension
|
| 9 |
+
from ..factortools import dup_factor_list
|
| 10 |
+
from ..polyroots import roots
|
| 11 |
+
from ..polytools import Poly
|
| 12 |
+
from ..rootoftools import CRootOf
|
| 13 |
+
|
| 14 |
+
from .domainmatrix import DomainMatrix
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def dom_eigenvects(A, l=Dummy('lambda')):
|
| 18 |
+
charpoly = A.charpoly()
|
| 19 |
+
rows, cols = A.shape
|
| 20 |
+
domain = A.domain
|
| 21 |
+
_, factors = dup_factor_list(charpoly, domain)
|
| 22 |
+
|
| 23 |
+
rational_eigenvects = []
|
| 24 |
+
algebraic_eigenvects = []
|
| 25 |
+
for base, exp in factors:
|
| 26 |
+
if len(base) == 2:
|
| 27 |
+
field = domain
|
| 28 |
+
eigenval = -base[1] / base[0]
|
| 29 |
+
|
| 30 |
+
EE_items = [
|
| 31 |
+
[eigenval if i == j else field.zero for j in range(cols)]
|
| 32 |
+
for i in range(rows)]
|
| 33 |
+
EE = DomainMatrix(EE_items, (rows, cols), field)
|
| 34 |
+
|
| 35 |
+
basis = (A - EE).nullspace(divide_last=True)
|
| 36 |
+
rational_eigenvects.append((field, eigenval, exp, basis))
|
| 37 |
+
else:
|
| 38 |
+
minpoly = Poly.from_list(base, l, domain=domain)
|
| 39 |
+
field = FiniteExtension(minpoly)
|
| 40 |
+
eigenval = field(l)
|
| 41 |
+
|
| 42 |
+
AA_items = [
|
| 43 |
+
[Poly.from_list([item], l, domain=domain).rep for item in row]
|
| 44 |
+
for row in A.rep.to_ddm()]
|
| 45 |
+
AA_items = [[field(item) for item in row] for row in AA_items]
|
| 46 |
+
AA = DomainMatrix(AA_items, (rows, cols), field)
|
| 47 |
+
EE_items = [
|
| 48 |
+
[eigenval if i == j else field.zero for j in range(cols)]
|
| 49 |
+
for i in range(rows)]
|
| 50 |
+
EE = DomainMatrix(EE_items, (rows, cols), field)
|
| 51 |
+
|
| 52 |
+
basis = (AA - EE).nullspace(divide_last=True)
|
| 53 |
+
algebraic_eigenvects.append((field, minpoly, exp, basis))
|
| 54 |
+
|
| 55 |
+
return rational_eigenvects, algebraic_eigenvects
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def dom_eigenvects_to_sympy(
|
| 59 |
+
rational_eigenvects, algebraic_eigenvects,
|
| 60 |
+
Matrix, **kwargs
|
| 61 |
+
):
|
| 62 |
+
result = []
|
| 63 |
+
|
| 64 |
+
for field, eigenvalue, multiplicity, eigenvects in rational_eigenvects:
|
| 65 |
+
eigenvects = eigenvects.rep.to_ddm()
|
| 66 |
+
eigenvalue = field.to_sympy(eigenvalue)
|
| 67 |
+
new_eigenvects = [
|
| 68 |
+
Matrix([field.to_sympy(x) for x in vect])
|
| 69 |
+
for vect in eigenvects]
|
| 70 |
+
result.append((eigenvalue, multiplicity, new_eigenvects))
|
| 71 |
+
|
| 72 |
+
for field, minpoly, multiplicity, eigenvects in algebraic_eigenvects:
|
| 73 |
+
eigenvects = eigenvects.rep.to_ddm()
|
| 74 |
+
l = minpoly.gens[0]
|
| 75 |
+
|
| 76 |
+
eigenvects = [[field.to_sympy(x) for x in vect] for vect in eigenvects]
|
| 77 |
+
|
| 78 |
+
degree = minpoly.degree()
|
| 79 |
+
minpoly = minpoly.as_expr()
|
| 80 |
+
eigenvals = roots(minpoly, l, **kwargs)
|
| 81 |
+
if len(eigenvals) != degree:
|
| 82 |
+
eigenvals = [CRootOf(minpoly, l, idx) for idx in range(degree)]
|
| 83 |
+
|
| 84 |
+
for eigenvalue in eigenvals:
|
| 85 |
+
new_eigenvects = [
|
| 86 |
+
Matrix([x.subs(l, eigenvalue) for x in vect])
|
| 87 |
+
for vect in eigenvects]
|
| 88 |
+
result.append((eigenvalue, multiplicity, new_eigenvects))
|
| 89 |
+
|
| 90 |
+
return result
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/linsolve.py
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# sympy.polys.matrices.linsolve module
|
| 3 |
+
#
|
| 4 |
+
# This module defines the _linsolve function which is the internal workhorse
|
| 5 |
+
# used by linsolve. This computes the solution of a system of linear equations
|
| 6 |
+
# using the SDM sparse matrix implementation in sympy.polys.matrices.sdm. This
|
| 7 |
+
# is a replacement for solve_lin_sys in sympy.polys.solvers which is
|
| 8 |
+
# inefficient for large sparse systems due to the use of a PolyRing with many
|
| 9 |
+
# generators:
|
| 10 |
+
#
|
| 11 |
+
# https://github.com/sympy/sympy/issues/20857
|
| 12 |
+
#
|
| 13 |
+
# The implementation of _linsolve here handles:
|
| 14 |
+
#
|
| 15 |
+
# - Extracting the coefficients from the Expr/Eq input equations.
|
| 16 |
+
# - Constructing a domain and converting the coefficients to
|
| 17 |
+
# that domain.
|
| 18 |
+
# - Using the SDM.rref, SDM.nullspace etc methods to generate the full
|
| 19 |
+
# solution working with arithmetic only in the domain of the coefficients.
|
| 20 |
+
#
|
| 21 |
+
# The routines here are particularly designed to be efficient for large sparse
|
| 22 |
+
# systems of linear equations although as well as dense systems. It is
|
| 23 |
+
# possible that for some small dense systems solve_lin_sys which uses the
|
| 24 |
+
# dense matrix implementation DDM will be more efficient. With smaller systems
|
| 25 |
+
# though the bulk of the time is spent just preprocessing the inputs and the
|
| 26 |
+
# relative time spent in rref is too small to be noticeable.
|
| 27 |
+
#
|
| 28 |
+
|
| 29 |
+
from collections import defaultdict
|
| 30 |
+
|
| 31 |
+
from sympy.core.add import Add
|
| 32 |
+
from sympy.core.mul import Mul
|
| 33 |
+
from sympy.core.singleton import S
|
| 34 |
+
|
| 35 |
+
from sympy.polys.constructor import construct_domain
|
| 36 |
+
from sympy.polys.solvers import PolyNonlinearError
|
| 37 |
+
|
| 38 |
+
from .sdm import (
|
| 39 |
+
SDM,
|
| 40 |
+
sdm_irref,
|
| 41 |
+
sdm_particular_from_rref,
|
| 42 |
+
sdm_nullspace_from_rref
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
from sympy.utilities.misc import filldedent
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _linsolve(eqs, syms):
|
| 49 |
+
|
| 50 |
+
"""Solve a linear system of equations.
|
| 51 |
+
|
| 52 |
+
Examples
|
| 53 |
+
========
|
| 54 |
+
|
| 55 |
+
Solve a linear system with a unique solution:
|
| 56 |
+
|
| 57 |
+
>>> from sympy import symbols, Eq
|
| 58 |
+
>>> from sympy.polys.matrices.linsolve import _linsolve
|
| 59 |
+
>>> x, y = symbols('x, y')
|
| 60 |
+
>>> eqs = [Eq(x + y, 1), Eq(x - y, 2)]
|
| 61 |
+
>>> _linsolve(eqs, [x, y])
|
| 62 |
+
{x: 3/2, y: -1/2}
|
| 63 |
+
|
| 64 |
+
In the case of underdetermined systems the solution will be expressed in
|
| 65 |
+
terms of the unknown symbols that are unconstrained:
|
| 66 |
+
|
| 67 |
+
>>> _linsolve([Eq(x + y, 0)], [x, y])
|
| 68 |
+
{x: -y, y: y}
|
| 69 |
+
|
| 70 |
+
"""
|
| 71 |
+
# Number of unknowns (columns in the non-augmented matrix)
|
| 72 |
+
nsyms = len(syms)
|
| 73 |
+
|
| 74 |
+
# Convert to sparse augmented matrix (len(eqs) x (nsyms+1))
|
| 75 |
+
eqsdict, const = _linear_eq_to_dict(eqs, syms)
|
| 76 |
+
Aaug = sympy_dict_to_dm(eqsdict, const, syms)
|
| 77 |
+
K = Aaug.domain
|
| 78 |
+
|
| 79 |
+
# sdm_irref has issues with float matrices. This uses the ddm_rref()
|
| 80 |
+
# function. When sdm_rref() can handle float matrices reasonably this
|
| 81 |
+
# should be removed...
|
| 82 |
+
if K.is_RealField or K.is_ComplexField:
|
| 83 |
+
Aaug = Aaug.to_ddm().rref()[0].to_sdm()
|
| 84 |
+
|
| 85 |
+
# Compute reduced-row echelon form (RREF)
|
| 86 |
+
Arref, pivots, nzcols = sdm_irref(Aaug)
|
| 87 |
+
|
| 88 |
+
# No solution:
|
| 89 |
+
if pivots and pivots[-1] == nsyms:
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
# Particular solution for non-homogeneous system:
|
| 93 |
+
P = sdm_particular_from_rref(Arref, nsyms+1, pivots)
|
| 94 |
+
|
| 95 |
+
# Nullspace - general solution to homogeneous system
|
| 96 |
+
# Note: using nsyms not nsyms+1 to ignore last column
|
| 97 |
+
V, nonpivots = sdm_nullspace_from_rref(Arref, K.one, nsyms, pivots, nzcols)
|
| 98 |
+
|
| 99 |
+
# Collect together terms from particular and nullspace:
|
| 100 |
+
sol = defaultdict(list)
|
| 101 |
+
for i, v in P.items():
|
| 102 |
+
sol[syms[i]].append(K.to_sympy(v))
|
| 103 |
+
for npi, Vi in zip(nonpivots, V):
|
| 104 |
+
sym = syms[npi]
|
| 105 |
+
for i, v in Vi.items():
|
| 106 |
+
sol[syms[i]].append(sym * K.to_sympy(v))
|
| 107 |
+
|
| 108 |
+
# Use a single call to Add for each term:
|
| 109 |
+
sol = {s: Add(*terms) for s, terms in sol.items()}
|
| 110 |
+
|
| 111 |
+
# Fill in the zeros:
|
| 112 |
+
zero = S.Zero
|
| 113 |
+
for s in set(syms) - set(sol):
|
| 114 |
+
sol[s] = zero
|
| 115 |
+
|
| 116 |
+
# All done!
|
| 117 |
+
return sol
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def sympy_dict_to_dm(eqs_coeffs, eqs_rhs, syms):
|
| 121 |
+
"""Convert a system of dict equations to a sparse augmented matrix"""
|
| 122 |
+
elems = set(eqs_rhs).union(*(e.values() for e in eqs_coeffs))
|
| 123 |
+
K, elems_K = construct_domain(elems, field=True, extension=True)
|
| 124 |
+
elem_map = dict(zip(elems, elems_K))
|
| 125 |
+
neqs = len(eqs_coeffs)
|
| 126 |
+
nsyms = len(syms)
|
| 127 |
+
sym2index = dict(zip(syms, range(nsyms)))
|
| 128 |
+
eqsdict = []
|
| 129 |
+
for eq, rhs in zip(eqs_coeffs, eqs_rhs):
|
| 130 |
+
eqdict = {sym2index[s]: elem_map[c] for s, c in eq.items()}
|
| 131 |
+
if rhs:
|
| 132 |
+
eqdict[nsyms] = -elem_map[rhs]
|
| 133 |
+
if eqdict:
|
| 134 |
+
eqsdict.append(eqdict)
|
| 135 |
+
sdm_aug = SDM(enumerate(eqsdict), (neqs, nsyms + 1), K)
|
| 136 |
+
return sdm_aug
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def _linear_eq_to_dict(eqs, syms):
|
| 140 |
+
"""Convert a system Expr/Eq equations into dict form, returning
|
| 141 |
+
the coefficient dictionaries and a list of syms-independent terms
|
| 142 |
+
from each expression in ``eqs```.
|
| 143 |
+
|
| 144 |
+
Examples
|
| 145 |
+
========
|
| 146 |
+
|
| 147 |
+
>>> from sympy.polys.matrices.linsolve import _linear_eq_to_dict
|
| 148 |
+
>>> from sympy.abc import x
|
| 149 |
+
>>> _linear_eq_to_dict([2*x + 3], {x})
|
| 150 |
+
([{x: 2}], [3])
|
| 151 |
+
"""
|
| 152 |
+
coeffs = []
|
| 153 |
+
ind = []
|
| 154 |
+
symset = set(syms)
|
| 155 |
+
for e in eqs:
|
| 156 |
+
if e.is_Equality:
|
| 157 |
+
coeff, terms = _lin_eq2dict(e.lhs, symset)
|
| 158 |
+
cR, tR = _lin_eq2dict(e.rhs, symset)
|
| 159 |
+
# there were no nonlinear errors so now
|
| 160 |
+
# cancellation is allowed
|
| 161 |
+
coeff -= cR
|
| 162 |
+
for k, v in tR.items():
|
| 163 |
+
if k in terms:
|
| 164 |
+
terms[k] -= v
|
| 165 |
+
else:
|
| 166 |
+
terms[k] = -v
|
| 167 |
+
# don't store coefficients of 0, however
|
| 168 |
+
terms = {k: v for k, v in terms.items() if v}
|
| 169 |
+
c, d = coeff, terms
|
| 170 |
+
else:
|
| 171 |
+
c, d = _lin_eq2dict(e, symset)
|
| 172 |
+
coeffs.append(d)
|
| 173 |
+
ind.append(c)
|
| 174 |
+
return coeffs, ind
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def _lin_eq2dict(a, symset):
|
| 178 |
+
"""return (c, d) where c is the sym-independent part of ``a`` and
|
| 179 |
+
``d`` is an efficiently calculated dictionary mapping symbols to
|
| 180 |
+
their coefficients. A PolyNonlinearError is raised if non-linearity
|
| 181 |
+
is detected.
|
| 182 |
+
|
| 183 |
+
The values in the dictionary will be non-zero.
|
| 184 |
+
|
| 185 |
+
Examples
|
| 186 |
+
========
|
| 187 |
+
|
| 188 |
+
>>> from sympy.polys.matrices.linsolve import _lin_eq2dict
|
| 189 |
+
>>> from sympy.abc import x, y
|
| 190 |
+
>>> _lin_eq2dict(x + 2*y + 3, {x, y})
|
| 191 |
+
(3, {x: 1, y: 2})
|
| 192 |
+
"""
|
| 193 |
+
if a in symset:
|
| 194 |
+
return S.Zero, {a: S.One}
|
| 195 |
+
elif a.is_Add:
|
| 196 |
+
terms_list = defaultdict(list)
|
| 197 |
+
coeff_list = []
|
| 198 |
+
for ai in a.args:
|
| 199 |
+
ci, ti = _lin_eq2dict(ai, symset)
|
| 200 |
+
coeff_list.append(ci)
|
| 201 |
+
for mij, cij in ti.items():
|
| 202 |
+
terms_list[mij].append(cij)
|
| 203 |
+
coeff = Add(*coeff_list)
|
| 204 |
+
terms = {sym: Add(*coeffs) for sym, coeffs in terms_list.items()}
|
| 205 |
+
return coeff, terms
|
| 206 |
+
elif a.is_Mul:
|
| 207 |
+
terms = terms_coeff = None
|
| 208 |
+
coeff_list = []
|
| 209 |
+
for ai in a.args:
|
| 210 |
+
ci, ti = _lin_eq2dict(ai, symset)
|
| 211 |
+
if not ti:
|
| 212 |
+
coeff_list.append(ci)
|
| 213 |
+
elif terms is None:
|
| 214 |
+
terms = ti
|
| 215 |
+
terms_coeff = ci
|
| 216 |
+
else:
|
| 217 |
+
# since ti is not null and we already have
|
| 218 |
+
# a term, this is a cross term
|
| 219 |
+
raise PolyNonlinearError(filldedent('''
|
| 220 |
+
nonlinear cross-term: %s''' % a))
|
| 221 |
+
coeff = Mul._from_args(coeff_list)
|
| 222 |
+
if terms is None:
|
| 223 |
+
return coeff, {}
|
| 224 |
+
else:
|
| 225 |
+
terms = {sym: coeff * c for sym, c in terms.items()}
|
| 226 |
+
return coeff * terms_coeff, terms
|
| 227 |
+
elif not a.has_xfree(symset):
|
| 228 |
+
return a, {}
|
| 229 |
+
else:
|
| 230 |
+
raise PolyNonlinearError('nonlinear term: %s' % a)
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__init__.py
ADDED
|
File without changes
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_domainmatrix.cpython-310.pyc
ADDED
|
Binary file (54.3 kB). View file
|
|
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_domainscalar.cpython-310.pyc
ADDED
|
Binary file (5.94 kB). View file
|
|
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_eigen.cpython-310.pyc
ADDED
|
Binary file (3.03 kB). View file
|
|
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_inverse.cpython-310.pyc
ADDED
|
Binary file (5.3 kB). View file
|
|
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_linsolve.cpython-310.pyc
ADDED
|
Binary file (3.36 kB). View file
|
|
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_normalforms.cpython-310.pyc
ADDED
|
Binary file (3.58 kB). View file
|
|
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/__pycache__/test_xxm.cpython-310.pyc
ADDED
|
Binary file (27.9 kB). View file
|
|
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_eigen.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for the sympy.polys.matrices.eigen module
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from sympy.core.singleton import S
|
| 6 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 7 |
+
from sympy.matrices.dense import Matrix
|
| 8 |
+
|
| 9 |
+
from sympy.polys.agca.extensions import FiniteExtension
|
| 10 |
+
from sympy.polys.domains import QQ
|
| 11 |
+
from sympy.polys.polytools import Poly
|
| 12 |
+
from sympy.polys.rootoftools import CRootOf
|
| 13 |
+
from sympy.polys.matrices.domainmatrix import DomainMatrix
|
| 14 |
+
|
| 15 |
+
from sympy.polys.matrices.eigen import dom_eigenvects, dom_eigenvects_to_sympy
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_dom_eigenvects_rational():
|
| 19 |
+
# Rational eigenvalues
|
| 20 |
+
A = DomainMatrix([[QQ(1), QQ(2)], [QQ(1), QQ(2)]], (2, 2), QQ)
|
| 21 |
+
rational_eigenvects = [
|
| 22 |
+
(QQ, QQ(3), 1, DomainMatrix([[QQ(1), QQ(1)]], (1, 2), QQ)),
|
| 23 |
+
(QQ, QQ(0), 1, DomainMatrix([[QQ(-2), QQ(1)]], (1, 2), QQ)),
|
| 24 |
+
]
|
| 25 |
+
assert dom_eigenvects(A) == (rational_eigenvects, [])
|
| 26 |
+
|
| 27 |
+
# Test converting to Expr:
|
| 28 |
+
sympy_eigenvects = [
|
| 29 |
+
(S(3), 1, [Matrix([1, 1])]),
|
| 30 |
+
(S(0), 1, [Matrix([-2, 1])]),
|
| 31 |
+
]
|
| 32 |
+
assert dom_eigenvects_to_sympy(rational_eigenvects, [], Matrix) == sympy_eigenvects
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_dom_eigenvects_algebraic():
|
| 36 |
+
# Algebraic eigenvalues
|
| 37 |
+
A = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ)
|
| 38 |
+
Avects = dom_eigenvects(A)
|
| 39 |
+
|
| 40 |
+
# Extract the dummy to build the expected result:
|
| 41 |
+
lamda = Avects[1][0][1].gens[0]
|
| 42 |
+
irreducible = Poly(lamda**2 - 5*lamda - 2, lamda, domain=QQ)
|
| 43 |
+
K = FiniteExtension(irreducible)
|
| 44 |
+
KK = K.from_sympy
|
| 45 |
+
algebraic_eigenvects = [
|
| 46 |
+
(K, irreducible, 1, DomainMatrix([[KK((lamda-4)/3), KK(1)]], (1, 2), K)),
|
| 47 |
+
]
|
| 48 |
+
assert Avects == ([], algebraic_eigenvects)
|
| 49 |
+
|
| 50 |
+
# Test converting to Expr:
|
| 51 |
+
sympy_eigenvects = [
|
| 52 |
+
(S(5)/2 - sqrt(33)/2, 1, [Matrix([[-sqrt(33)/6 - S(1)/2], [1]])]),
|
| 53 |
+
(S(5)/2 + sqrt(33)/2, 1, [Matrix([[-S(1)/2 + sqrt(33)/6], [1]])]),
|
| 54 |
+
]
|
| 55 |
+
assert dom_eigenvects_to_sympy([], algebraic_eigenvects, Matrix) == sympy_eigenvects
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_dom_eigenvects_rootof():
|
| 59 |
+
# Algebraic eigenvalues
|
| 60 |
+
A = DomainMatrix([
|
| 61 |
+
[0, 0, 0, 0, -1],
|
| 62 |
+
[1, 0, 0, 0, 1],
|
| 63 |
+
[0, 1, 0, 0, 0],
|
| 64 |
+
[0, 0, 1, 0, 0],
|
| 65 |
+
[0, 0, 0, 1, 0]], (5, 5), QQ)
|
| 66 |
+
Avects = dom_eigenvects(A)
|
| 67 |
+
|
| 68 |
+
# Extract the dummy to build the expected result:
|
| 69 |
+
lamda = Avects[1][0][1].gens[0]
|
| 70 |
+
irreducible = Poly(lamda**5 - lamda + 1, lamda, domain=QQ)
|
| 71 |
+
K = FiniteExtension(irreducible)
|
| 72 |
+
KK = K.from_sympy
|
| 73 |
+
algebraic_eigenvects = [
|
| 74 |
+
(K, irreducible, 1,
|
| 75 |
+
DomainMatrix([
|
| 76 |
+
[KK(lamda**4-1), KK(lamda**3), KK(lamda**2), KK(lamda), KK(1)]
|
| 77 |
+
], (1, 5), K)),
|
| 78 |
+
]
|
| 79 |
+
assert Avects == ([], algebraic_eigenvects)
|
| 80 |
+
|
| 81 |
+
# Test converting to Expr (slow):
|
| 82 |
+
l0, l1, l2, l3, l4 = [CRootOf(lamda**5 - lamda + 1, i) for i in range(5)]
|
| 83 |
+
sympy_eigenvects = [
|
| 84 |
+
(l0, 1, [Matrix([-1 + l0**4, l0**3, l0**2, l0, 1])]),
|
| 85 |
+
(l1, 1, [Matrix([-1 + l1**4, l1**3, l1**2, l1, 1])]),
|
| 86 |
+
(l2, 1, [Matrix([-1 + l2**4, l2**3, l2**2, l2, 1])]),
|
| 87 |
+
(l3, 1, [Matrix([-1 + l3**4, l3**3, l3**2, l3, 1])]),
|
| 88 |
+
(l4, 1, [Matrix([-1 + l4**4, l4**3, l4**2, l4, 1])]),
|
| 89 |
+
]
|
| 90 |
+
assert dom_eigenvects_to_sympy([], algebraic_eigenvects, Matrix) == sympy_eigenvects
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_inverse.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import ZZ, Matrix
|
| 2 |
+
from sympy.polys.matrices import DM, DomainMatrix
|
| 3 |
+
from sympy.polys.matrices.dense import ddm_iinv
|
| 4 |
+
from sympy.polys.matrices.exceptions import DMNonInvertibleMatrixError
|
| 5 |
+
from sympy.matrices.exceptions import NonInvertibleMatrixError
|
| 6 |
+
|
| 7 |
+
import pytest
|
| 8 |
+
from sympy.testing.pytest import raises
|
| 9 |
+
from sympy.core.numbers import all_close
|
| 10 |
+
|
| 11 |
+
from sympy.abc import x
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Examples are given as adjugate matrix and determinant adj_det should match
|
| 15 |
+
# these exactly but inv_den only matches after cancel_denom.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
INVERSE_EXAMPLES = [
|
| 19 |
+
|
| 20 |
+
(
|
| 21 |
+
'zz_1',
|
| 22 |
+
DomainMatrix([], (0, 0), ZZ),
|
| 23 |
+
DomainMatrix([], (0, 0), ZZ),
|
| 24 |
+
ZZ(1),
|
| 25 |
+
),
|
| 26 |
+
|
| 27 |
+
(
|
| 28 |
+
'zz_2',
|
| 29 |
+
DM([[2]], ZZ),
|
| 30 |
+
DM([[1]], ZZ),
|
| 31 |
+
ZZ(2),
|
| 32 |
+
),
|
| 33 |
+
|
| 34 |
+
(
|
| 35 |
+
'zz_3',
|
| 36 |
+
DM([[2, 0],
|
| 37 |
+
[0, 2]], ZZ),
|
| 38 |
+
DM([[2, 0],
|
| 39 |
+
[0, 2]], ZZ),
|
| 40 |
+
ZZ(4),
|
| 41 |
+
),
|
| 42 |
+
|
| 43 |
+
(
|
| 44 |
+
'zz_4',
|
| 45 |
+
DM([[1, 2],
|
| 46 |
+
[3, 4]], ZZ),
|
| 47 |
+
DM([[ 4, -2],
|
| 48 |
+
[-3, 1]], ZZ),
|
| 49 |
+
ZZ(-2),
|
| 50 |
+
),
|
| 51 |
+
|
| 52 |
+
(
|
| 53 |
+
'zz_5',
|
| 54 |
+
DM([[2, 2, 0],
|
| 55 |
+
[0, 2, 2],
|
| 56 |
+
[0, 0, 2]], ZZ),
|
| 57 |
+
DM([[4, -4, 4],
|
| 58 |
+
[0, 4, -4],
|
| 59 |
+
[0, 0, 4]], ZZ),
|
| 60 |
+
ZZ(8),
|
| 61 |
+
),
|
| 62 |
+
|
| 63 |
+
(
|
| 64 |
+
'zz_6',
|
| 65 |
+
DM([[1, 2, 3],
|
| 66 |
+
[4, 5, 6],
|
| 67 |
+
[7, 8, 9]], ZZ),
|
| 68 |
+
DM([[-3, 6, -3],
|
| 69 |
+
[ 6, -12, 6],
|
| 70 |
+
[-3, 6, -3]], ZZ),
|
| 71 |
+
ZZ(0),
|
| 72 |
+
),
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 77 |
+
def test_Matrix_inv(name, A, A_inv, den):
|
| 78 |
+
|
| 79 |
+
def _check(**kwargs):
|
| 80 |
+
if den != 0:
|
| 81 |
+
assert A.inv(**kwargs) == A_inv
|
| 82 |
+
else:
|
| 83 |
+
raises(NonInvertibleMatrixError, lambda: A.inv(**kwargs))
|
| 84 |
+
|
| 85 |
+
K = A.domain
|
| 86 |
+
A = A.to_Matrix()
|
| 87 |
+
A_inv = A_inv.to_Matrix() / K.to_sympy(den)
|
| 88 |
+
_check()
|
| 89 |
+
for method in ['GE', 'LU', 'ADJ', 'CH', 'LDL', 'QR']:
|
| 90 |
+
_check(method=method)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 94 |
+
def test_dm_inv_den(name, A, A_inv, den):
|
| 95 |
+
if den != 0:
|
| 96 |
+
A_inv_f, den_f = A.inv_den()
|
| 97 |
+
assert A_inv_f.cancel_denom(den_f) == A_inv.cancel_denom(den)
|
| 98 |
+
else:
|
| 99 |
+
raises(DMNonInvertibleMatrixError, lambda: A.inv_den())
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 103 |
+
def test_dm_inv(name, A, A_inv, den):
|
| 104 |
+
A = A.to_field()
|
| 105 |
+
if den != 0:
|
| 106 |
+
A_inv = A_inv.to_field() / den
|
| 107 |
+
assert A.inv() == A_inv
|
| 108 |
+
else:
|
| 109 |
+
raises(DMNonInvertibleMatrixError, lambda: A.inv())
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 113 |
+
def test_ddm_inv(name, A, A_inv, den):
|
| 114 |
+
A = A.to_field().to_ddm()
|
| 115 |
+
if den != 0:
|
| 116 |
+
A_inv = (A_inv.to_field() / den).to_ddm()
|
| 117 |
+
assert A.inv() == A_inv
|
| 118 |
+
else:
|
| 119 |
+
raises(DMNonInvertibleMatrixError, lambda: A.inv())
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 123 |
+
def test_sdm_inv(name, A, A_inv, den):
|
| 124 |
+
A = A.to_field().to_sdm()
|
| 125 |
+
if den != 0:
|
| 126 |
+
A_inv = (A_inv.to_field() / den).to_sdm()
|
| 127 |
+
assert A.inv() == A_inv
|
| 128 |
+
else:
|
| 129 |
+
raises(DMNonInvertibleMatrixError, lambda: A.inv())
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 133 |
+
def test_dense_ddm_iinv(name, A, A_inv, den):
|
| 134 |
+
A = A.to_field().to_ddm().copy()
|
| 135 |
+
K = A.domain
|
| 136 |
+
A_result = A.copy()
|
| 137 |
+
if den != 0:
|
| 138 |
+
A_inv = (A_inv.to_field() / den).to_ddm()
|
| 139 |
+
ddm_iinv(A_result, A, K)
|
| 140 |
+
assert A_result == A_inv
|
| 141 |
+
else:
|
| 142 |
+
raises(DMNonInvertibleMatrixError, lambda: ddm_iinv(A_result, A, K))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 146 |
+
def test_Matrix_adjugate(name, A, A_inv, den):
|
| 147 |
+
A = A.to_Matrix()
|
| 148 |
+
A_inv = A_inv.to_Matrix()
|
| 149 |
+
assert A.adjugate() == A_inv
|
| 150 |
+
for method in ["bareiss", "berkowitz", "bird", "laplace", "lu"]:
|
| 151 |
+
assert A.adjugate(method=method) == A_inv
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
@pytest.mark.parametrize('name, A, A_inv, den', INVERSE_EXAMPLES)
|
| 155 |
+
def test_dm_adj_det(name, A, A_inv, den):
|
| 156 |
+
assert A.adj_det() == (A_inv, den)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def test_inverse_inexact():
|
| 160 |
+
|
| 161 |
+
M = Matrix([[x-0.3, -0.06, -0.22],
|
| 162 |
+
[-0.46, x-0.48, -0.41],
|
| 163 |
+
[-0.14, -0.39, x-0.64]])
|
| 164 |
+
|
| 165 |
+
Mn = Matrix([[1.0*x**2 - 1.12*x + 0.1473, 0.06*x + 0.0474, 0.22*x - 0.081],
|
| 166 |
+
[0.46*x - 0.237, 1.0*x**2 - 0.94*x + 0.1612, 0.41*x - 0.0218],
|
| 167 |
+
[0.14*x + 0.1122, 0.39*x - 0.1086, 1.0*x**2 - 0.78*x + 0.1164]])
|
| 168 |
+
|
| 169 |
+
d = 1.0*x**3 - 1.42*x**2 + 0.4249*x - 0.0546540000000002
|
| 170 |
+
|
| 171 |
+
Mi = Mn / d
|
| 172 |
+
|
| 173 |
+
M_dm = M.to_DM()
|
| 174 |
+
M_dmd = M_dm.to_dense()
|
| 175 |
+
M_dm_num, M_dm_den = M_dm.inv_den()
|
| 176 |
+
M_dmd_num, M_dmd_den = M_dmd.inv_den()
|
| 177 |
+
|
| 178 |
+
# XXX: We don't check M_dm().to_field().inv() which currently uses division
|
| 179 |
+
# and produces a more complicate result from gcd cancellation failing.
|
| 180 |
+
# DomainMatrix.inv() over RR(x) should be changed to clear denominators and
|
| 181 |
+
# use DomainMatrix.inv_den().
|
| 182 |
+
|
| 183 |
+
Minvs = [
|
| 184 |
+
M.inv(),
|
| 185 |
+
(M_dm_num.to_field() / M_dm_den).to_Matrix(),
|
| 186 |
+
(M_dmd_num.to_field() / M_dmd_den).to_Matrix(),
|
| 187 |
+
M_dm_num.to_Matrix() / M_dm_den.as_expr(),
|
| 188 |
+
M_dmd_num.to_Matrix() / M_dmd_den.as_expr(),
|
| 189 |
+
]
|
| 190 |
+
|
| 191 |
+
for Minv in Minvs:
|
| 192 |
+
for Mi1, Mi2 in zip(Minv.flat(), Mi.flat()):
|
| 193 |
+
assert all_close(Mi2, Mi1)
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_linsolve.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# test_linsolve.py
|
| 3 |
+
#
|
| 4 |
+
# Test the internal implementation of linsolve.
|
| 5 |
+
#
|
| 6 |
+
|
| 7 |
+
from sympy.testing.pytest import raises
|
| 8 |
+
|
| 9 |
+
from sympy.core.numbers import I
|
| 10 |
+
from sympy.core.relational import Eq
|
| 11 |
+
from sympy.core.singleton import S
|
| 12 |
+
from sympy.abc import x, y, z
|
| 13 |
+
|
| 14 |
+
from sympy.polys.matrices.linsolve import _linsolve
|
| 15 |
+
from sympy.polys.solvers import PolyNonlinearError
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test__linsolve():
|
| 19 |
+
assert _linsolve([], [x]) == {x:x}
|
| 20 |
+
assert _linsolve([S.Zero], [x]) == {x:x}
|
| 21 |
+
assert _linsolve([x-1,x-2], [x]) is None
|
| 22 |
+
assert _linsolve([x-1], [x]) == {x:1}
|
| 23 |
+
assert _linsolve([x-1, y], [x, y]) == {x:1, y:S.Zero}
|
| 24 |
+
assert _linsolve([2*I], [x]) is None
|
| 25 |
+
raises(PolyNonlinearError, lambda: _linsolve([x*(1 + x)], [x]))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test__linsolve_float():
|
| 29 |
+
|
| 30 |
+
# This should give the exact answer:
|
| 31 |
+
eqs = [
|
| 32 |
+
y - x,
|
| 33 |
+
y - 0.0216 * x
|
| 34 |
+
]
|
| 35 |
+
# Should _linsolve return floats here?
|
| 36 |
+
sol = {x:0, y:0}
|
| 37 |
+
assert _linsolve(eqs, (x, y)) == sol
|
| 38 |
+
|
| 39 |
+
# Other cases should be close to eps
|
| 40 |
+
|
| 41 |
+
def all_close(sol1, sol2, eps=1e-15):
|
| 42 |
+
close = lambda a, b: abs(a - b) < eps
|
| 43 |
+
assert sol1.keys() == sol2.keys()
|
| 44 |
+
return all(close(sol1[s], sol2[s]) for s in sol1)
|
| 45 |
+
|
| 46 |
+
eqs = [
|
| 47 |
+
0.8*x + 0.8*z + 0.2,
|
| 48 |
+
0.9*x + 0.7*y + 0.2*z + 0.9,
|
| 49 |
+
0.7*x + 0.2*y + 0.2*z + 0.5
|
| 50 |
+
]
|
| 51 |
+
sol_exact = {x:-29/42, y:-11/21, z:37/84}
|
| 52 |
+
sol_linsolve = _linsolve(eqs, [x,y,z])
|
| 53 |
+
assert all_close(sol_exact, sol_linsolve)
|
| 54 |
+
|
| 55 |
+
eqs = [
|
| 56 |
+
0.9*x + 0.3*y + 0.4*z + 0.6,
|
| 57 |
+
0.6*x + 0.9*y + 0.1*z + 0.7,
|
| 58 |
+
0.4*x + 0.6*y + 0.9*z + 0.5
|
| 59 |
+
]
|
| 60 |
+
sol_exact = {x:-88/175, y:-46/105, z:-1/25}
|
| 61 |
+
sol_linsolve = _linsolve(eqs, [x,y,z])
|
| 62 |
+
assert all_close(sol_exact, sol_linsolve)
|
| 63 |
+
|
| 64 |
+
eqs = [
|
| 65 |
+
0.4*x + 0.3*y + 0.6*z + 0.7,
|
| 66 |
+
0.4*x + 0.3*y + 0.9*z + 0.9,
|
| 67 |
+
0.7*x + 0.9*y,
|
| 68 |
+
]
|
| 69 |
+
sol_exact = {x:-9/5, y:7/5, z:-2/3}
|
| 70 |
+
sol_linsolve = _linsolve(eqs, [x,y,z])
|
| 71 |
+
assert all_close(sol_exact, sol_linsolve)
|
| 72 |
+
|
| 73 |
+
eqs = [
|
| 74 |
+
x*(0.7 + 0.6*I) + y*(0.4 + 0.7*I) + z*(0.9 + 0.1*I) + 0.5,
|
| 75 |
+
0.2*I*x + 0.2*I*y + z*(0.9 + 0.2*I) + 0.1,
|
| 76 |
+
x*(0.9 + 0.7*I) + y*(0.9 + 0.7*I) + z*(0.9 + 0.4*I) + 0.4,
|
| 77 |
+
]
|
| 78 |
+
sol_exact = {
|
| 79 |
+
x:-6157/7995 - 411/5330*I,
|
| 80 |
+
y:8519/15990 + 1784/7995*I,
|
| 81 |
+
z:-34/533 + 107/1599*I,
|
| 82 |
+
}
|
| 83 |
+
sol_linsolve = _linsolve(eqs, [x,y,z])
|
| 84 |
+
assert all_close(sol_exact, sol_linsolve)
|
| 85 |
+
|
| 86 |
+
# XXX: This system for x and y over RR(z) is problematic.
|
| 87 |
+
#
|
| 88 |
+
# eqs = [
|
| 89 |
+
# x*(0.2*z + 0.9) + y*(0.5*z + 0.8) + 0.6,
|
| 90 |
+
# 0.1*x*z + y*(0.1*z + 0.6) + 0.9,
|
| 91 |
+
# ]
|
| 92 |
+
#
|
| 93 |
+
# linsolve(eqs, [x, y])
|
| 94 |
+
# The solution for x comes out as
|
| 95 |
+
#
|
| 96 |
+
# -3.9e-5*z**2 - 3.6e-5*z - 8.67361737988404e-20
|
| 97 |
+
# x = ----------------------------------------------
|
| 98 |
+
# 3.0e-6*z**3 - 1.3e-5*z**2 - 5.4e-5*z
|
| 99 |
+
#
|
| 100 |
+
# The 8e-20 in the numerator should be zero which would allow z to cancel
|
| 101 |
+
# from top and bottom. It should be possible to avoid this somehow because
|
| 102 |
+
# the inverse of the matrix only has a quadratic factor (the determinant)
|
| 103 |
+
# in the denominator.
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def test__linsolve_deprecated():
|
| 107 |
+
raises(PolyNonlinearError, lambda:
|
| 108 |
+
_linsolve([Eq(x**2, x**2 + y)], [x, y]))
|
| 109 |
+
raises(PolyNonlinearError, lambda:
|
| 110 |
+
_linsolve([(x + y)**2 - x**2], [x]))
|
| 111 |
+
raises(PolyNonlinearError, lambda:
|
| 112 |
+
_linsolve([Eq((x + y)**2, x**2)], [x]))
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_nullspace.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.polys.matrices.ddm import DDM
|
| 4 |
+
from sympy.polys.matrices.sdm import SDM
|
| 5 |
+
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
zeros = lambda shape, K: DomainMatrix.zeros(shape, K).to_dense()
|
| 9 |
+
eye = lambda n, K: DomainMatrix.eye(n, K).to_dense()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
#
|
| 13 |
+
# DomainMatrix.nullspace can have a divided answer or can return an undivided
|
| 14 |
+
# uncanonical answer. The uncanonical answer is not unique but we can make it
|
| 15 |
+
# unique by making it primitive (remove gcd). The tests here all show the
|
| 16 |
+
# primitive form. We test two things:
|
| 17 |
+
#
|
| 18 |
+
# A.nullspace().primitive()[1] == answer.
|
| 19 |
+
# A.nullspace(divide_last=True) == _divide_last(answer).
|
| 20 |
+
#
|
| 21 |
+
# The nullspace as returned by DomainMatrix and related classes is the
|
| 22 |
+
# transpose of the nullspace as returned by Matrix. Matrix returns a list of
|
| 23 |
+
# of column vectors whereas DomainMatrix returns a matrix whose rows are the
|
| 24 |
+
# nullspace vectors.
|
| 25 |
+
#
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
NULLSPACE_EXAMPLES = [
|
| 29 |
+
|
| 30 |
+
(
|
| 31 |
+
'zz_1',
|
| 32 |
+
DM([[ 1, 2, 3]], ZZ),
|
| 33 |
+
DM([[-2, 1, 0],
|
| 34 |
+
[-3, 0, 1]], ZZ),
|
| 35 |
+
),
|
| 36 |
+
|
| 37 |
+
(
|
| 38 |
+
'zz_2',
|
| 39 |
+
zeros((0, 0), ZZ),
|
| 40 |
+
zeros((0, 0), ZZ),
|
| 41 |
+
),
|
| 42 |
+
|
| 43 |
+
(
|
| 44 |
+
'zz_3',
|
| 45 |
+
zeros((2, 0), ZZ),
|
| 46 |
+
zeros((0, 0), ZZ),
|
| 47 |
+
),
|
| 48 |
+
|
| 49 |
+
(
|
| 50 |
+
'zz_4',
|
| 51 |
+
zeros((0, 2), ZZ),
|
| 52 |
+
eye(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, -1, 0, 0, 0, 0, 1]], ZZ),
|
| 100 |
+
),
|
| 101 |
+
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def _to_DM(A, ans):
|
| 106 |
+
"""Convert the answer to DomainMatrix."""
|
| 107 |
+
if isinstance(A, DomainMatrix):
|
| 108 |
+
return A.to_dense()
|
| 109 |
+
elif isinstance(A, DDM):
|
| 110 |
+
return DomainMatrix(list(A), A.shape, A.domain).to_dense()
|
| 111 |
+
elif isinstance(A, SDM):
|
| 112 |
+
return DomainMatrix(dict(A), A.shape, A.domain).to_dense()
|
| 113 |
+
else:
|
| 114 |
+
assert False # pragma: no cover
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def _divide_last(null):
|
| 118 |
+
"""Normalize the nullspace by the rightmost non-zero entry."""
|
| 119 |
+
null = null.to_field()
|
| 120 |
+
|
| 121 |
+
if null.is_zero_matrix:
|
| 122 |
+
return null
|
| 123 |
+
|
| 124 |
+
rows = []
|
| 125 |
+
for i in range(null.shape[0]):
|
| 126 |
+
for j in reversed(range(null.shape[1])):
|
| 127 |
+
if null[i, j]:
|
| 128 |
+
rows.append(null[i, :] / null[i, j])
|
| 129 |
+
break
|
| 130 |
+
else:
|
| 131 |
+
assert False # pragma: no cover
|
| 132 |
+
|
| 133 |
+
return DomainMatrix.vstack(*rows)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def _check_primitive(null, null_ans):
|
| 137 |
+
"""Check that the primitive of the answer matches."""
|
| 138 |
+
null = _to_DM(null, null_ans)
|
| 139 |
+
cont, null_prim = null.primitive()
|
| 140 |
+
assert null_prim == null_ans
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def _check_divided(null, null_ans):
|
| 144 |
+
"""Check the divided answer."""
|
| 145 |
+
null = _to_DM(null, null_ans)
|
| 146 |
+
null_ans_norm = _divide_last(null_ans)
|
| 147 |
+
assert null == null_ans_norm
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
@pytest.mark.parametrize('name, A, A_null', NULLSPACE_EXAMPLES)
|
| 151 |
+
def test_Matrix_nullspace(name, A, A_null):
|
| 152 |
+
A = A.to_Matrix()
|
| 153 |
+
|
| 154 |
+
A_null_cols = A.nullspace()
|
| 155 |
+
|
| 156 |
+
# We have to patch up the case where the nullspace is empty
|
| 157 |
+
if A_null_cols:
|
| 158 |
+
A_null_found = Matrix.hstack(*A_null_cols)
|
| 159 |
+
else:
|
| 160 |
+
A_null_found = Matrix.zeros(A.cols, 0)
|
| 161 |
+
|
| 162 |
+
A_null_found = A_null_found.to_DM().to_field().to_dense()
|
| 163 |
+
|
| 164 |
+
# The Matrix result is the transpose of DomainMatrix result.
|
| 165 |
+
A_null_found = A_null_found.transpose()
|
| 166 |
+
|
| 167 |
+
_check_divided(A_null_found, A_null)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@pytest.mark.parametrize('name, A, A_null', NULLSPACE_EXAMPLES)
|
| 171 |
+
def test_dm_dense_nullspace(name, A, A_null):
|
| 172 |
+
A = A.to_field().to_dense()
|
| 173 |
+
A_null_found = A.nullspace(divide_last=True)
|
| 174 |
+
_check_divided(A_null_found, A_null)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
@pytest.mark.parametrize('name, A, A_null', NULLSPACE_EXAMPLES)
|
| 178 |
+
def test_dm_sparse_nullspace(name, A, A_null):
|
| 179 |
+
A = A.to_field().to_sparse()
|
| 180 |
+
A_null_found = A.nullspace(divide_last=True)
|
| 181 |
+
_check_divided(A_null_found, A_null)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
@pytest.mark.parametrize('name, A, A_null', NULLSPACE_EXAMPLES)
|
| 185 |
+
def test_ddm_nullspace(name, A, A_null):
|
| 186 |
+
A = A.to_field().to_ddm()
|
| 187 |
+
A_null_found, _ = A.nullspace()
|
| 188 |
+
_check_divided(A_null_found, A_null)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
@pytest.mark.parametrize('name, A, A_null', NULLSPACE_EXAMPLES)
|
| 192 |
+
def test_sdm_nullspace(name, A, A_null):
|
| 193 |
+
A = A.to_field().to_sdm()
|
| 194 |
+
A_null_found, _ = A.nullspace()
|
| 195 |
+
_check_divided(A_null_found, A_null)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
@pytest.mark.parametrize('name, A, A_null', NULLSPACE_EXAMPLES)
|
| 199 |
+
def test_dm_dense_nullspace_fracfree(name, A, A_null):
|
| 200 |
+
A = A.to_dense()
|
| 201 |
+
A_null_found = A.nullspace()
|
| 202 |
+
_check_primitive(A_null_found, A_null)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
@pytest.mark.parametrize('name, A, A_null', NULLSPACE_EXAMPLES)
|
| 206 |
+
def test_dm_sparse_nullspace_fracfree(name, A, A_null):
|
| 207 |
+
A = A.to_sparse()
|
| 208 |
+
A_null_found = A.nullspace()
|
| 209 |
+
_check_primitive(A_null_found, A_null)
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_rref.py
ADDED
|
@@ -0,0 +1,737 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import ZZ, QQ, ZZ_I, EX, Matrix, eye, zeros, symbols
|
| 2 |
+
from sympy.polys.matrices import DM, DomainMatrix
|
| 3 |
+
from sympy.polys.matrices.dense import ddm_irref_den, ddm_irref
|
| 4 |
+
from sympy.polys.matrices.ddm import DDM
|
| 5 |
+
from sympy.polys.matrices.sdm import SDM, sdm_irref, sdm_rref_den
|
| 6 |
+
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
#
|
| 11 |
+
# The dense and sparse implementations of rref_den are ddm_irref_den and
|
| 12 |
+
# sdm_irref_den. These can give results that differ by some factor and also
|
| 13 |
+
# give different results if the order of the rows is changed. The tests below
|
| 14 |
+
# show all results on lowest terms as should be returned by cancel_denom.
|
| 15 |
+
#
|
| 16 |
+
# The EX domain is also a case where the dense and sparse implementations
|
| 17 |
+
# can give results in different forms: the results should be equivalent but
|
| 18 |
+
# are not canonical because EX does not have a canonical form.
|
| 19 |
+
#
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
a, b, c, d = symbols('a, b, c, d')
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
qq_large_1 = DM([
|
| 26 |
+
[ (1,2), (1,3), (1,5), (1,7), (1,11), (1,13), (1,17), (1,19), (1,23), (1,29), (1,31)],
|
| 27 |
+
[ (1,37), (1,41), (1,43), (1,47), (1,53), (1,59), (1,61), (1,67), (1,71), (1,73), (1,79)],
|
| 28 |
+
[ (1,83), (1,89), (1,97),(1,101),(1,103),(1,107),(1,109),(1,113),(1,127),(1,131),(1,137)],
|
| 29 |
+
[(1,139),(1,149),(1,151),(1,157),(1,163),(1,167),(1,173),(1,179),(1,181),(1,191),(1,193)],
|
| 30 |
+
[(1,197),(1,199),(1,211),(1,223),(1,227),(1,229),(1,233),(1,239),(1,241),(1,251),(1,257)],
|
| 31 |
+
[(1,263),(1,269),(1,271),(1,277),(1,281),(1,283),(1,293),(1,307),(1,311),(1,313),(1,317)],
|
| 32 |
+
[(1,331),(1,337),(1,347),(1,349),(1,353),(1,359),(1,367),(1,373),(1,379),(1,383),(1,389)],
|
| 33 |
+
[(1,397),(1,401),(1,409),(1,419),(1,421),(1,431),(1,433),(1,439),(1,443),(1,449),(1,457)],
|
| 34 |
+
[(1,461),(1,463),(1,467),(1,479),(1,487),(1,491),(1,499),(1,503),(1,509),(1,521),(1,523)],
|
| 35 |
+
[(1,541),(1,547),(1,557),(1,563),(1,569),(1,571),(1,577),(1,587),(1,593),(1,599),(1,601)],
|
| 36 |
+
[(1,607),(1,613),(1,617),(1,619),(1,631),(1,641),(1,643),(1,647),(1,653),(1,659),(1,661)]],
|
| 37 |
+
QQ)
|
| 38 |
+
|
| 39 |
+
qq_large_2 = qq_large_1 + 10**100 * DomainMatrix.eye(11, QQ)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
RREF_EXAMPLES = [
|
| 43 |
+
(
|
| 44 |
+
'zz_1',
|
| 45 |
+
DM([[1, 2, 3]], ZZ),
|
| 46 |
+
DM([[1, 2, 3]], ZZ),
|
| 47 |
+
ZZ(1),
|
| 48 |
+
),
|
| 49 |
+
|
| 50 |
+
(
|
| 51 |
+
'zz_2',
|
| 52 |
+
DomainMatrix([], (0, 0), ZZ),
|
| 53 |
+
DomainMatrix([], (0, 0), ZZ),
|
| 54 |
+
ZZ(1),
|
| 55 |
+
),
|
| 56 |
+
|
| 57 |
+
(
|
| 58 |
+
'zz_3',
|
| 59 |
+
DM([[1, 2],
|
| 60 |
+
[3, 4]], ZZ),
|
| 61 |
+
DM([[1, 0],
|
| 62 |
+
[0, 1]], ZZ),
|
| 63 |
+
ZZ(1),
|
| 64 |
+
),
|
| 65 |
+
|
| 66 |
+
(
|
| 67 |
+
'zz_4',
|
| 68 |
+
DM([[1, 0],
|
| 69 |
+
[3, 4]], ZZ),
|
| 70 |
+
DM([[1, 0],
|
| 71 |
+
[0, 1]], ZZ),
|
| 72 |
+
ZZ(1),
|
| 73 |
+
),
|
| 74 |
+
|
| 75 |
+
(
|
| 76 |
+
'zz_5',
|
| 77 |
+
DM([[0, 2],
|
| 78 |
+
[3, 4]], ZZ),
|
| 79 |
+
DM([[1, 0],
|
| 80 |
+
[0, 1]], ZZ),
|
| 81 |
+
ZZ(1),
|
| 82 |
+
),
|
| 83 |
+
|
| 84 |
+
(
|
| 85 |
+
'zz_6',
|
| 86 |
+
DM([[1, 2, 3],
|
| 87 |
+
[4, 5, 6],
|
| 88 |
+
[7, 8, 9]], ZZ),
|
| 89 |
+
DM([[1, 0, -1],
|
| 90 |
+
[0, 1, 2],
|
| 91 |
+
[0, 0, 0]], ZZ),
|
| 92 |
+
ZZ(1),
|
| 93 |
+
),
|
| 94 |
+
|
| 95 |
+
(
|
| 96 |
+
'zz_7',
|
| 97 |
+
DM([[0, 0, 0],
|
| 98 |
+
[0, 0, 0],
|
| 99 |
+
[1, 0, 0]], ZZ),
|
| 100 |
+
DM([[1, 0, 0],
|
| 101 |
+
[0, 0, 0],
|
| 102 |
+
[0, 0, 0]], ZZ),
|
| 103 |
+
ZZ(1),
|
| 104 |
+
),
|
| 105 |
+
|
| 106 |
+
(
|
| 107 |
+
'zz_8',
|
| 108 |
+
DM([[0, 0, 0],
|
| 109 |
+
[0, 0, 0],
|
| 110 |
+
[0, 0, 0]], ZZ),
|
| 111 |
+
DM([[0, 0, 0],
|
| 112 |
+
[0, 0, 0],
|
| 113 |
+
[0, 0, 0]], ZZ),
|
| 114 |
+
ZZ(1),
|
| 115 |
+
),
|
| 116 |
+
|
| 117 |
+
(
|
| 118 |
+
'zz_9',
|
| 119 |
+
DM([[1, 1, 0],
|
| 120 |
+
[0, 0, 2],
|
| 121 |
+
[0, 0, 0]], ZZ),
|
| 122 |
+
DM([[1, 1, 0],
|
| 123 |
+
[0, 0, 1],
|
| 124 |
+
[0, 0, 0]], ZZ),
|
| 125 |
+
ZZ(1),
|
| 126 |
+
),
|
| 127 |
+
|
| 128 |
+
(
|
| 129 |
+
'zz_10',
|
| 130 |
+
DM([[2, 2, 0],
|
| 131 |
+
[0, 0, 2],
|
| 132 |
+
[0, 0, 0]], ZZ),
|
| 133 |
+
DM([[1, 1, 0],
|
| 134 |
+
[0, 0, 1],
|
| 135 |
+
[0, 0, 0]], ZZ),
|
| 136 |
+
ZZ(1),
|
| 137 |
+
),
|
| 138 |
+
|
| 139 |
+
(
|
| 140 |
+
'zz_11',
|
| 141 |
+
DM([[2, 2, 0],
|
| 142 |
+
[0, 2, 2],
|
| 143 |
+
[0, 0, 2]], ZZ),
|
| 144 |
+
DM([[1, 0, 0],
|
| 145 |
+
[0, 1, 0],
|
| 146 |
+
[0, 0, 1]], ZZ),
|
| 147 |
+
ZZ(1),
|
| 148 |
+
),
|
| 149 |
+
|
| 150 |
+
(
|
| 151 |
+
'zz_12',
|
| 152 |
+
DM([[ 1, 2, 3],
|
| 153 |
+
[ 4, 5, 6],
|
| 154 |
+
[ 7, 8, 9],
|
| 155 |
+
[10, 11, 12]], ZZ),
|
| 156 |
+
DM([[1, 0, -1],
|
| 157 |
+
[0, 1, 2],
|
| 158 |
+
[0, 0, 0],
|
| 159 |
+
[0, 0, 0]], ZZ),
|
| 160 |
+
ZZ(1),
|
| 161 |
+
),
|
| 162 |
+
|
| 163 |
+
(
|
| 164 |
+
'zz_13',
|
| 165 |
+
DM([[ 1, 2, 3],
|
| 166 |
+
[ 4, 5, 6],
|
| 167 |
+
[ 7, 8, 9],
|
| 168 |
+
[10, 11, 13]], ZZ),
|
| 169 |
+
DM([[ 1, 0, 0],
|
| 170 |
+
[ 0, 1, 0],
|
| 171 |
+
[ 0, 0, 1],
|
| 172 |
+
[ 0, 0, 0]], ZZ),
|
| 173 |
+
ZZ(1),
|
| 174 |
+
),
|
| 175 |
+
|
| 176 |
+
(
|
| 177 |
+
'zz_14',
|
| 178 |
+
DM([[1, 2, 4, 3],
|
| 179 |
+
[4, 5, 10, 6],
|
| 180 |
+
[7, 8, 16, 9]], ZZ),
|
| 181 |
+
DM([[1, 0, 0, -1],
|
| 182 |
+
[0, 1, 2, 2],
|
| 183 |
+
[0, 0, 0, 0]], ZZ),
|
| 184 |
+
ZZ(1),
|
| 185 |
+
),
|
| 186 |
+
|
| 187 |
+
(
|
| 188 |
+
'zz_15',
|
| 189 |
+
DM([[1, 2, 4, 3],
|
| 190 |
+
[4, 5, 10, 6],
|
| 191 |
+
[7, 8, 17, 9]], ZZ),
|
| 192 |
+
DM([[1, 0, 0, -1],
|
| 193 |
+
[0, 1, 0, 2],
|
| 194 |
+
[0, 0, 1, 0]], ZZ),
|
| 195 |
+
ZZ(1),
|
| 196 |
+
),
|
| 197 |
+
|
| 198 |
+
(
|
| 199 |
+
'zz_16',
|
| 200 |
+
DM([[1, 2, 0, 1],
|
| 201 |
+
[1, 1, 9, 0]], ZZ),
|
| 202 |
+
DM([[1, 0, 18, -1],
|
| 203 |
+
[0, 1, -9, 1]], ZZ),
|
| 204 |
+
ZZ(1),
|
| 205 |
+
),
|
| 206 |
+
|
| 207 |
+
(
|
| 208 |
+
'zz_17',
|
| 209 |
+
DM([[1, 1, 1],
|
| 210 |
+
[1, 2, 2]], ZZ),
|
| 211 |
+
DM([[1, 0, 0],
|
| 212 |
+
[0, 1, 1]], ZZ),
|
| 213 |
+
ZZ(1),
|
| 214 |
+
),
|
| 215 |
+
|
| 216 |
+
(
|
| 217 |
+
# Here the sparse implementation and dense implementation give very
|
| 218 |
+
# different denominators: 4061232 and -1765176.
|
| 219 |
+
'zz_18',
|
| 220 |
+
DM([[94, 24, 0, 27, 0],
|
| 221 |
+
[79, 0, 0, 0, 0],
|
| 222 |
+
[85, 16, 71, 81, 0],
|
| 223 |
+
[ 0, 0, 72, 77, 0],
|
| 224 |
+
[21, 0, 34, 0, 0]], ZZ),
|
| 225 |
+
DM([[ 1, 0, 0, 0, 0],
|
| 226 |
+
[ 0, 1, 0, 0, 0],
|
| 227 |
+
[ 0, 0, 1, 0, 0],
|
| 228 |
+
[ 0, 0, 0, 1, 0],
|
| 229 |
+
[ 0, 0, 0, 0, 0]], ZZ),
|
| 230 |
+
ZZ(1),
|
| 231 |
+
),
|
| 232 |
+
|
| 233 |
+
(
|
| 234 |
+
# Let's have a denominator that cannot be cancelled.
|
| 235 |
+
'zz_19',
|
| 236 |
+
DM([[1, 2, 4],
|
| 237 |
+
[4, 5, 6]], ZZ),
|
| 238 |
+
DM([[3, 0, -8],
|
| 239 |
+
[0, 3, 10]], ZZ),
|
| 240 |
+
ZZ(3),
|
| 241 |
+
),
|
| 242 |
+
|
| 243 |
+
(
|
| 244 |
+
'zz_20',
|
| 245 |
+
DM([[0, 0, 0, 0, 0],
|
| 246 |
+
[0, 0, 0, 0, 0],
|
| 247 |
+
[0, 0, 0, 0, 4]], ZZ),
|
| 248 |
+
DM([[0, 0, 0, 0, 1],
|
| 249 |
+
[0, 0, 0, 0, 0],
|
| 250 |
+
[0, 0, 0, 0, 0]], ZZ),
|
| 251 |
+
ZZ(1),
|
| 252 |
+
),
|
| 253 |
+
|
| 254 |
+
(
|
| 255 |
+
'zz_21',
|
| 256 |
+
DM([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 257 |
+
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 258 |
+
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 259 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
|
| 260 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 1]], ZZ),
|
| 261 |
+
DM([[1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 262 |
+
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 263 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
|
| 264 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
|
| 265 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]], ZZ),
|
| 266 |
+
ZZ(1),
|
| 267 |
+
),
|
| 268 |
+
|
| 269 |
+
(
|
| 270 |
+
'zz_22',
|
| 271 |
+
DM([[1, 1, 1, 0, 1],
|
| 272 |
+
[1, 1, 0, 1, 0],
|
| 273 |
+
[1, 0, 1, 0, 1],
|
| 274 |
+
[1, 1, 0, 1, 0],
|
| 275 |
+
[1, 0, 0, 0, 0]], ZZ),
|
| 276 |
+
DM([[1, 0, 0, 0, 0],
|
| 277 |
+
[0, 1, 0, 0, 0],
|
| 278 |
+
[0, 0, 1, 0, 1],
|
| 279 |
+
[0, 0, 0, 1, 0],
|
| 280 |
+
[0, 0, 0, 0, 0]], ZZ),
|
| 281 |
+
ZZ(1),
|
| 282 |
+
),
|
| 283 |
+
|
| 284 |
+
(
|
| 285 |
+
'zz_large_1',
|
| 286 |
+
DM([
|
| 287 |
+
[ 0, 0, 0, 81, 0, 0, 75, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0],
|
| 288 |
+
[ 0, 0, 0, 0, 0, 86, 0, 92, 79, 54, 0, 7, 0, 0, 0, 0, 79, 0, 0, 0],
|
| 289 |
+
[89, 54, 81, 0, 0, 20, 0, 0, 0, 0, 0, 0, 51, 0, 94, 0, 0, 77, 0, 0],
|
| 290 |
+
[ 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 48, 29, 0, 0, 5, 0, 32, 0],
|
| 291 |
+
[ 0, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 0, 0, 0, 11],
|
| 292 |
+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 43, 0, 0],
|
| 293 |
+
[ 0, 0, 0, 0, 0, 38, 91, 0, 0, 0, 0, 38, 0, 0, 0, 0, 0, 26, 0, 0],
|
| 294 |
+
[69, 0, 0, 0, 0, 0, 94, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55],
|
| 295 |
+
[ 0, 13, 18, 49, 49, 88, 0, 0, 35, 54, 0, 0, 51, 0, 0, 0, 0, 0, 0, 87],
|
| 296 |
+
[ 0, 0, 0, 0, 31, 0, 40, 0, 0, 0, 0, 0, 0, 50, 0, 0, 0, 0, 88, 0],
|
| 297 |
+
[ 0, 0, 0, 0, 0, 0, 0, 0, 98, 0, 0, 0, 15, 53, 0, 92, 0, 0, 0, 0],
|
| 298 |
+
[ 0, 0, 0, 95, 0, 0, 0, 36, 0, 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 0, 1]], ZZ),
|
| 328 |
+
ZZ(1),
|
| 329 |
+
),
|
| 330 |
+
|
| 331 |
+
(
|
| 332 |
+
'zz_large_2',
|
| 333 |
+
DM([
|
| 334 |
+
[ 0, 0, 0, 0, 50, 0, 6, 81, 0, 1, 86, 0, 0, 98, 82, 94, 4, 0, 0, 29],
|
| 335 |
+
[ 0, 44, 43, 0, 62, 0, 0, 0, 60, 0, 0, 0, 0, 71, 9, 0, 57, 41, 0, 93],
|
| 336 |
+
[ 0, 0, 28, 0, 74, 89, 42, 0, 28, 0, 6, 0, 0, 0, 44, 0, 0, 0, 77, 19],
|
| 337 |
+
[ 0, 21, 82, 0, 30, 88, 0, 89, 68, 0, 0, 0, 79, 41, 0, 0, 99, 0, 0, 0],
|
| 338 |
+
[31, 0, 0, 0, 19, 64, 0, 0, 79, 0, 5, 0, 72, 10, 60, 32, 64, 59, 0, 24],
|
| 339 |
+
[ 0, 0, 0, 0, 0, 57, 0, 94, 0, 83, 20, 0, 0, 9, 31, 0, 49, 26, 58, 0],
|
| 340 |
+
[ 0, 65, 56, 31, 64, 0, 0, 0, 0, 0, 0, 52, 85, 0, 0, 0, 0, 51, 0, 0],
|
| 341 |
+
[ 0, 35, 0, 0, 0, 69, 0, 0, 64, 0, 0, 0, 0, 70, 0, 0, 90, 0, 75, 76],
|
| 342 |
+
[69, 7, 0, 90, 0, 0, 84, 0, 47, 69, 19, 20, 42, 0, 0, 32, 71, 35, 0, 0],
|
| 343 |
+
[39, 0, 90, 0, 0, 4, 85, 0, 0, 55, 0, 0, 0, 35, 67, 40, 0, 40, 0, 77],
|
| 344 |
+
[98, 63, 0, 71, 0, 50, 0, 2, 61, 0, 38, 0, 0, 0, 0, 75, 0, 40, 33, 56],
|
| 345 |
+
[ 0, 73, 0, 64, 0, 38, 0, 35, 61, 0, 0, 52, 0, 7, 0, 51, 0, 0, 0, 34],
|
| 346 |
+
[ 0, 0, 28, 0, 34, 5, 63, 45, 14, 42, 60, 16, 76, 54, 99, 0, 28, 30, 0, 0],
|
| 347 |
+
[58, 37, 14, 0, 0, 0, 94, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 8, 90, 53],
|
| 348 |
+
[86, 74, 94, 0, 49, 10, 60, 0, 40, 18, 0, 0, 0, 31, 60, 24, 0, 1, 0, 29],
|
| 349 |
+
[53, 0, 0, 97, 0, 0, 58, 0, 0, 39, 44, 47, 0, 0, 0, 12, 50, 0, 0, 11],
|
| 350 |
+
[ 4, 0, 92, 10, 28, 0, 0, 89, 0, 0, 18, 54, 23, 39, 0, 2, 0, 48, 0, 92],
|
| 351 |
+
[ 0, 0, 90, 77, 95, 33, 0, 0, 49, 22, 39, 0, 0, 0, 0, 0, 0, 40, 0, 0],
|
| 352 |
+
[96, 0, 0, 0, 0, 38, 86, 0, 22, 76, 0, 0, 0, 0, 83, 88, 95, 65, 72, 0],
|
| 353 |
+
[81, 65, 0, 4, 60, 0, 19, 0, 0, 68, 0, 0, 89, 0, 67, 22, 0, 0, 55, 33]],
|
| 354 |
+
ZZ),
|
| 355 |
+
DM([
|
| 356 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 357 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 358 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 359 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 360 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 361 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 362 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0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z_large_3',
|
| 382 |
+
DM([
|
| 383 |
+
[62,35,89,58,22,47,30,28,52,72,17,56,80,26,64,21,10,35,24,42,96,32,23,50,92,37,76,94,63,66],
|
| 384 |
+
[20,47,96,34,10,98,19,6,29,2,19,92,61,94,38,41,32,9,5,94,31,58,27,41,72,85,61,62,40,46],
|
| 385 |
+
[69,26,35,68,25,52,94,13,38,65,81,10,29,15,5,4,13,99,85,0,80,51,60,60,26,77,85,2,87,25],
|
| 386 |
+
[99,58,69,15,52,12,18,7,27,56,12,54,21,92,38,95,33,83,28,1,44,8,29,84,92,12,2,25,46,46],
|
| 387 |
+
[93,13,55,48,35,87,24,40,23,35,25,32,0,19,0,85,4,79,26,11,46,75,7,96,76,11,7,57,99,75],
|
| 388 |
+
[128,85,26,51,161,173,77,78,85,103,123,58,91,147,38,91,161,36,123,81,102,25,75,59,17,150,112,65,77,143],
|
| 389 |
+
[15,59,61,82,12,83,34,8,94,71,66,7,91,21,48,69,26,12,64,38,97,87,38,15,51,33,93,43,66,89],
|
| 390 |
+
[74,74,53,39,69,90,41,80,32,66,40,83,87,87,61,38,12,80,24,49,37,90,19,33,56,0,46,57,56,60],
|
| 391 |
+
[82,11,0,25,56,58,39,49,92,93,80,38,19,62,33,85,19,61,14,30,45,91,97,34,97,53,92,28,33,43],
|
| 392 |
+
[83,79,41,16,95,35,53,45,26,4,71,76,61,69,69,72,87,92,59,72,54,11,22,83,8,57,77,55,19,22],
|
| 393 |
+
[49,34,13,31,72,77,52,70,46,41,37,6,42,66,35,6,75,33,62,57,30,14,26,31,9,95,89,13,12,90],
|
| 394 |
+
[29,3,49,30,51,32,77,41,38,50,16,1,87,81,93,88,58,91,83,0,38,67,29,64,60,84,5,60,23,28],
|
| 395 |
+
[79,51,13,20,89,96,25,8,39,62,86,52,49,81,3,85,86,3,61,24,72,11,49,28,8,55,23,52,65,53],
|
| 396 |
+
[96,86,73,20,41,20,37,18,10,61,85,24,40,83,69,41,4,92,23,99,64,33,18,36,32,56,60,98,39,24],
|
| 397 |
+
[32,62,47,80,51,66,17,1,9,30,65,75,75,88,99,92,64,53,53,86,38,51,41,14,35,18,39,25,26,32],
|
| 398 |
+
[39,21,8,16,33,6,35,85,75,62,43,34,18,68,71,28,32,18,12,0,81,53,1,99,3,5,45,99,35,33],
|
| 399 |
+
[19,95,89,45,75,94,92,5,84,93,34,17,50,56,79,98,68,82,65,81,51,90,5,95,33,71,46,61,14,7],
|
| 400 |
+
[53,92,8,49,67,84,21,79,49,95,66,48,36,14,62,97,26,45,58,31,83,48,11,89,67,72,91,34,56,89],
|
| 401 |
+
[56,76,99,92,40,8,0,16,15,48,35,72,91,46,81,14,86,60,51,7,33,12,53,78,48,21,3,89,15,79],
|
| 402 |
+
[81,43,33,49,6,49,36,32,57,74,87,91,17,37,31,17,67,1,40,38,69,8,3,48,59,37,64,97,11,3],
|
| 403 |
+
[98,48,77,16,2,48,57,38,63,59,79,35,16,71,60,86,71,41,14,76,80,97,77,69,4,58,22,55,26,73],
|
| 404 |
+
[80,47,78,44,31,48,47,29,29,62,19,21,17,24,19,3,53,93,97,57,13,54,12,10,77,66,60,75,32,21],
|
| 405 |
+
[86,63,2,13,71,38,86,23,18,15,91,65,77,65,9,92,50,0,17,42,99,80,99,27,10,99,92,9,87,84],
|
| 406 |
+
[66,27,72,13,13,15,72,75,39,3,14,71,15,68,10,19,49,54,11,29,47,20,63,13,97,47,24,62,16,96],
|
| 407 |
+
[42,63,83,60,49,68,9,53,75,87,40,25,12,63,0,12,0,95,46,46,55,25,89,1,51,1,1,96,80,52],
|
| 408 |
+
[35,9,97,13,86,39,66,48,41,57,23,38,11,9,35,72,88,13,41,60,10,64,71,23,1,5,23,57,6,19],
|
| 409 |
+
[70,61,5,50,72,60,77,13,41,94,1,45,52,22,99,47,27,18,99,42,16,48,26,9,88,77,10,94,11,92],
|
| 410 |
+
[55,68,58,2,72,56,81,52,79,37,1,40,21,46,27,60,37,13,97,42,85,98,69,60,76,44,42,46,29,73],
|
| 411 |
+
[73,0,43,17,89,97,45,2,68,14,55,60,95,2,74,85,88,68,93,76,38,76,2,51,45,76,50,79,56,18],
|
| 412 |
+
[72,58,41,39,24,80,23,79,44,7,98,75,30,6,85,60,20,58,77,71,90,51,38,80,30,15,33,10,82,8]],
|
| 413 |
+
ZZ),
|
| 414 |
+
Matrix([
|
| 415 |
+
[eye(29) * 2028539767964472550625641331179545072876560857886207583101,
|
| 416 |
+
Matrix([ 4260575808093245475167216057435155595594339172099000182569,
|
| 417 |
+
169148395880755256182802335904188369274227936894862744452,
|
| 418 |
+
4915975976683942569102447281579134986891620721539038348914,
|
| 419 |
+
6113916866367364958834844982578214901958429746875633283248,
|
| 420 |
+
5585689617819894460378537031623265659753379011388162534838,
|
| 421 |
+
359776822829880747716695359574308645968094838905181892423,
|
| 422 |
+
-2800926112141776386671436511182421432449325232461665113305,
|
| 423 |
+
941642292388230001722444876624818265766384442910688463158,
|
| 424 |
+
3648811843256146649321864698600908938933015862008642023935,
|
| 425 |
+
-4104526163246702252932955226754097174212129127510547462419,
|
| 426 |
+
-704814955438106792441896903238080197619233342348191408078,
|
| 427 |
+
1640882266829725529929398131287244562048075707575030019335,
|
| 428 |
+
-4068330845192910563212155694231438198040299927120544468520,
|
| 429 |
+
136589038308366497790495711534532612862715724187671166593,
|
| 430 |
+
2544937011460702462290799932536905731142196510605191645593,
|
| 431 |
+
755591839174293940486133926192300657264122907519174116472,
|
| 432 |
+
-3683838489869297144348089243628436188645897133242795965021,
|
| 433 |
+
-522207137101161299969706310062775465103537953077871128403,
|
| 434 |
+
-2260451796032703984456606059649402832441331339246756656334,
|
| 435 |
+
-6476809325293587953616004856993300606040336446656916663680,
|
| 436 |
+
3521944238996782387785653800944972787867472610035040989081,
|
| 437 |
+
2270762115788407950241944504104975551914297395787473242379,
|
| 438 |
+
-3259947194628712441902262570532921252128444706733549251156,
|
| 439 |
+
-5624569821491886970999097239695637132075823246850431083557,
|
| 440 |
+
-3262698255682055804320585332902837076064075936601504555698,
|
| 441 |
+
5786719943788937667411185880136324396357603606944869545501,
|
| 442 |
+
-955257841973865996077323863289453200904051299086000660036,
|
| 443 |
+
-1294235552446355326174641248209752679127075717918392702116,
|
| 444 |
+
-3718353510747301598130831152458342785269166356215331448279,
|
| 445 |
+
]),],
|
| 446 |
+
[zeros(1, 29), zeros(1, 1)],
|
| 447 |
+
]).to_DM().to_dense(),
|
| 448 |
+
ZZ(2028539767964472550625641331179545072876560857886207583101),
|
| 449 |
+
),
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
(
|
| 453 |
+
'qq_1',
|
| 454 |
+
DM([[(1,2), 0], [0, 2]], QQ),
|
| 455 |
+
DM([[1, 0], [0, 1]], QQ),
|
| 456 |
+
QQ(1),
|
| 457 |
+
),
|
| 458 |
+
|
| 459 |
+
(
|
| 460 |
+
# Standard square case
|
| 461 |
+
'qq_2',
|
| 462 |
+
DM([[0, 1],
|
| 463 |
+
[1, 1]], QQ),
|
| 464 |
+
DM([[1, 0],
|
| 465 |
+
[0, 1]], QQ),
|
| 466 |
+
QQ(1),
|
| 467 |
+
),
|
| 468 |
+
|
| 469 |
+
(
|
| 470 |
+
# m < n case
|
| 471 |
+
'qq_3',
|
| 472 |
+
DM([[1, 2, 1],
|
| 473 |
+
[3, 4, 1]], QQ),
|
| 474 |
+
DM([[1, 0, -1],
|
| 475 |
+
[0, 1, 1]], QQ),
|
| 476 |
+
QQ(1),
|
| 477 |
+
),
|
| 478 |
+
|
| 479 |
+
(
|
| 480 |
+
# same m < n but reversed
|
| 481 |
+
'qq_4',
|
| 482 |
+
DM([[3, 4, 1],
|
| 483 |
+
[1, 2, 1]], QQ),
|
| 484 |
+
DM([[1, 0, -1],
|
| 485 |
+
[0, 1, 1]], QQ),
|
| 486 |
+
QQ(1),
|
| 487 |
+
),
|
| 488 |
+
|
| 489 |
+
(
|
| 490 |
+
# m > n case
|
| 491 |
+
'qq_5',
|
| 492 |
+
DM([[1, 0],
|
| 493 |
+
[1, 3],
|
| 494 |
+
[0, 1]], QQ),
|
| 495 |
+
DM([[1, 0],
|
| 496 |
+
[0, 1],
|
| 497 |
+
[0, 0]], QQ),
|
| 498 |
+
QQ(1),
|
| 499 |
+
),
|
| 500 |
+
|
| 501 |
+
(
|
| 502 |
+
# Example with missing pivot
|
| 503 |
+
'qq_6',
|
| 504 |
+
DM([[1, 0, 1],
|
| 505 |
+
[3, 0, 1]], QQ),
|
| 506 |
+
DM([[1, 0, 0],
|
| 507 |
+
[0, 0, 1]], QQ),
|
| 508 |
+
QQ(1),
|
| 509 |
+
),
|
| 510 |
+
|
| 511 |
+
(
|
| 512 |
+
# This is intended to trigger the threshold where we give up on
|
| 513 |
+
# clearing denominators.
|
| 514 |
+
'qq_large_1',
|
| 515 |
+
qq_large_1,
|
| 516 |
+
DomainMatrix.eye(11, QQ).to_dense(),
|
| 517 |
+
QQ(1),
|
| 518 |
+
),
|
| 519 |
+
|
| 520 |
+
(
|
| 521 |
+
# This is intended to trigger the threshold where we use rref_den over
|
| 522 |
+
# QQ.
|
| 523 |
+
'qq_large_2',
|
| 524 |
+
qq_large_2,
|
| 525 |
+
DomainMatrix.eye(11, QQ).to_dense(),
|
| 526 |
+
QQ(1),
|
| 527 |
+
),
|
| 528 |
+
|
| 529 |
+
(
|
| 530 |
+
# Example with missing pivot and no replacement
|
| 531 |
+
|
| 532 |
+
# This example is just enough to show a different result from the dense
|
| 533 |
+
# and sparse versions of the algorithm:
|
| 534 |
+
#
|
| 535 |
+
# >>> A = Matrix([[0, 1], [0, 2], [1, 0]])
|
| 536 |
+
# >>> A.to_DM().to_sparse().rref_den()[0].to_Matrix()
|
| 537 |
+
# Matrix([
|
| 538 |
+
# [1, 0],
|
| 539 |
+
# [0, 1],
|
| 540 |
+
# [0, 0]])
|
| 541 |
+
# >>> A.to_DM().to_dense().rref_den()[0].to_Matrix()
|
| 542 |
+
# Matrix([
|
| 543 |
+
# [2, 0],
|
| 544 |
+
# [0, 2],
|
| 545 |
+
# [0, 0]])
|
| 546 |
+
#
|
| 547 |
+
'qq_7',
|
| 548 |
+
DM([[0, 1],
|
| 549 |
+
[0, 2],
|
| 550 |
+
[1, 0]], QQ),
|
| 551 |
+
DM([[1, 0],
|
| 552 |
+
[0, 1],
|
| 553 |
+
[0, 0]], QQ),
|
| 554 |
+
QQ(1),
|
| 555 |
+
),
|
| 556 |
+
|
| 557 |
+
(
|
| 558 |
+
# Gaussian integers
|
| 559 |
+
'zz_i_1',
|
| 560 |
+
DM([[(0,1), 1, 1],
|
| 561 |
+
[ 1, 1, 1]], ZZ_I),
|
| 562 |
+
DM([[1, 0, 0],
|
| 563 |
+
[0, 1, 1]], ZZ_I),
|
| 564 |
+
ZZ_I(1),
|
| 565 |
+
),
|
| 566 |
+
|
| 567 |
+
(
|
| 568 |
+
# EX: test_issue_23718
|
| 569 |
+
'EX_1',
|
| 570 |
+
DM([
|
| 571 |
+
[a, b, 1],
|
| 572 |
+
[c, d, 1]], EX),
|
| 573 |
+
DM([[a*d - b*c, 0, -b + d],
|
| 574 |
+
[ 0, a*d - b*c, a - c]], EX),
|
| 575 |
+
EX(a*d - b*c),
|
| 576 |
+
),
|
| 577 |
+
|
| 578 |
+
]
|
| 579 |
+
|
| 580 |
+
|
| 581 |
+
def _to_DM(A, ans):
|
| 582 |
+
"""Convert the answer to DomainMatrix."""
|
| 583 |
+
if isinstance(A, DomainMatrix):
|
| 584 |
+
return A.to_dense()
|
| 585 |
+
elif isinstance(A, Matrix):
|
| 586 |
+
return A.to_DM(ans.domain).to_dense()
|
| 587 |
+
|
| 588 |
+
if not (hasattr(A, 'shape') and hasattr(A, 'domain')):
|
| 589 |
+
shape, domain = ans.shape, ans.domain
|
| 590 |
+
else:
|
| 591 |
+
shape, domain = A.shape, A.domain
|
| 592 |
+
|
| 593 |
+
if isinstance(A, (DDM, list)):
|
| 594 |
+
return DomainMatrix(list(A), shape, domain).to_dense()
|
| 595 |
+
elif isinstance(A, (SDM, dict)):
|
| 596 |
+
return DomainMatrix(dict(A), shape, domain).to_dense()
|
| 597 |
+
else:
|
| 598 |
+
assert False # pragma: no cover
|
| 599 |
+
|
| 600 |
+
|
| 601 |
+
def _pivots(A_rref):
|
| 602 |
+
"""Return the pivots from the rref of A."""
|
| 603 |
+
return tuple(sorted(map(min, A_rref.to_sdm().values())))
|
| 604 |
+
|
| 605 |
+
|
| 606 |
+
def _check_cancel(result, rref_ans, den_ans):
|
| 607 |
+
"""Check the cancelled result."""
|
| 608 |
+
rref, den, pivots = result
|
| 609 |
+
if isinstance(rref, (DDM, SDM, list, dict)):
|
| 610 |
+
assert type(pivots) is list
|
| 611 |
+
pivots = tuple(pivots)
|
| 612 |
+
rref = _to_DM(rref, rref_ans)
|
| 613 |
+
rref2, den2 = rref.cancel_denom(den)
|
| 614 |
+
assert rref2 == rref_ans
|
| 615 |
+
assert den2 == den_ans
|
| 616 |
+
assert pivots == _pivots(rref)
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
def _check_divide(result, rref_ans, den_ans):
|
| 620 |
+
"""Check the divided result."""
|
| 621 |
+
rref, pivots = result
|
| 622 |
+
if isinstance(rref, (DDM, SDM, list, dict)):
|
| 623 |
+
assert type(pivots) is list
|
| 624 |
+
pivots = tuple(pivots)
|
| 625 |
+
rref_ans = rref_ans.to_field() / den_ans
|
| 626 |
+
rref = _to_DM(rref, rref_ans)
|
| 627 |
+
assert rref == rref_ans
|
| 628 |
+
assert _pivots(rref) == pivots
|
| 629 |
+
|
| 630 |
+
|
| 631 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 632 |
+
def test_Matrix_rref(name, A, A_rref, den):
|
| 633 |
+
K = A.domain
|
| 634 |
+
A = A.to_Matrix()
|
| 635 |
+
A_rref_found, pivots = A.rref()
|
| 636 |
+
if K.is_EX:
|
| 637 |
+
A_rref_found = A_rref_found.expand()
|
| 638 |
+
_check_divide((A_rref_found, pivots), A_rref, den)
|
| 639 |
+
|
| 640 |
+
|
| 641 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 642 |
+
def test_dm_dense_rref(name, A, A_rref, den):
|
| 643 |
+
A = A.to_field()
|
| 644 |
+
_check_divide(A.rref(), A_rref, den)
|
| 645 |
+
|
| 646 |
+
|
| 647 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 648 |
+
def test_dm_dense_rref_den(name, A, A_rref, den):
|
| 649 |
+
_check_cancel(A.rref_den(), A_rref, den)
|
| 650 |
+
|
| 651 |
+
|
| 652 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 653 |
+
def test_dm_sparse_rref(name, A, A_rref, den):
|
| 654 |
+
A = A.to_field().to_sparse()
|
| 655 |
+
_check_divide(A.rref(), A_rref, den)
|
| 656 |
+
|
| 657 |
+
|
| 658 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 659 |
+
def test_dm_sparse_rref_den(name, A, A_rref, den):
|
| 660 |
+
A = A.to_sparse()
|
| 661 |
+
_check_cancel(A.rref_den(), A_rref, den)
|
| 662 |
+
|
| 663 |
+
|
| 664 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 665 |
+
def test_dm_sparse_rref_den_keep_domain(name, A, A_rref, den):
|
| 666 |
+
A = A.to_sparse()
|
| 667 |
+
A_rref_f, den_f, pivots_f = A.rref_den(keep_domain=False)
|
| 668 |
+
A_rref_f = A_rref_f.to_field() / den_f
|
| 669 |
+
_check_divide((A_rref_f, pivots_f), A_rref, den)
|
| 670 |
+
|
| 671 |
+
|
| 672 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 673 |
+
def test_dm_sparse_rref_den_keep_domain_CD(name, A, A_rref, den):
|
| 674 |
+
A = A.to_sparse()
|
| 675 |
+
A_rref_f, den_f, pivots_f = A.rref_den(keep_domain=False, method='CD')
|
| 676 |
+
A_rref_f = A_rref_f.to_field() / den_f
|
| 677 |
+
_check_divide((A_rref_f, pivots_f), A_rref, den)
|
| 678 |
+
|
| 679 |
+
|
| 680 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 681 |
+
def test_dm_sparse_rref_den_keep_domain_GJ(name, A, A_rref, den):
|
| 682 |
+
A = A.to_sparse()
|
| 683 |
+
A_rref_f, den_f, pivots_f = A.rref_den(keep_domain=False, method='GJ')
|
| 684 |
+
A_rref_f = A_rref_f.to_field() / den_f
|
| 685 |
+
_check_divide((A_rref_f, pivots_f), A_rref, den)
|
| 686 |
+
|
| 687 |
+
|
| 688 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 689 |
+
def test_ddm_rref_den(name, A, A_rref, den):
|
| 690 |
+
A = A.to_ddm()
|
| 691 |
+
_check_cancel(A.rref_den(), A_rref, den)
|
| 692 |
+
|
| 693 |
+
|
| 694 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 695 |
+
def test_sdm_rref_den(name, A, A_rref, den):
|
| 696 |
+
A = A.to_sdm()
|
| 697 |
+
_check_cancel(A.rref_den(), A_rref, den)
|
| 698 |
+
|
| 699 |
+
|
| 700 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 701 |
+
def test_ddm_rref(name, A, A_rref, den):
|
| 702 |
+
A = A.to_field().to_ddm()
|
| 703 |
+
_check_divide(A.rref(), A_rref, den)
|
| 704 |
+
|
| 705 |
+
|
| 706 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 707 |
+
def test_sdm_rref(name, A, A_rref, den):
|
| 708 |
+
A = A.to_field().to_sdm()
|
| 709 |
+
_check_divide(A.rref(), A_rref, den)
|
| 710 |
+
|
| 711 |
+
|
| 712 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 713 |
+
def test_ddm_irref(name, A, A_rref, den):
|
| 714 |
+
A = A.to_field().to_ddm().copy()
|
| 715 |
+
pivots_found = ddm_irref(A)
|
| 716 |
+
_check_divide((A, pivots_found), A_rref, den)
|
| 717 |
+
|
| 718 |
+
|
| 719 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 720 |
+
def test_ddm_irref_den(name, A, A_rref, den):
|
| 721 |
+
A = A.to_ddm().copy()
|
| 722 |
+
(den_found, pivots_found) = ddm_irref_den(A, A.domain)
|
| 723 |
+
result = (A, den_found, pivots_found)
|
| 724 |
+
_check_cancel(result, A_rref, den)
|
| 725 |
+
|
| 726 |
+
|
| 727 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 728 |
+
def test_sparse_sdm_rref(name, A, A_rref, den):
|
| 729 |
+
A = A.to_field().to_sdm()
|
| 730 |
+
_check_divide(sdm_irref(A)[:2], A_rref, den)
|
| 731 |
+
|
| 732 |
+
|
| 733 |
+
@pytest.mark.parametrize('name, A, A_rref, den', RREF_EXAMPLES)
|
| 734 |
+
def test_sparse_sdm_rref_den(name, A, A_rref, den):
|
| 735 |
+
A = A.to_sdm().copy()
|
| 736 |
+
K = A.domain
|
| 737 |
+
_check_cancel(sdm_rref_den(A, K), A_rref, den)
|
llava_next/lib/python3.10/site-packages/sympy/polys/matrices/tests/test_xxm.py
ADDED
|
@@ -0,0 +1,864 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Test basic features of DDM, SDM and DFM.
|
| 3 |
+
#
|
| 4 |
+
# These three types are supposed to be interchangeable, so we should use the
|
| 5 |
+
# same tests for all of them for the most part.
|
| 6 |
+
#
|
| 7 |
+
# The tests here cover the basic part of the inerface that the three types
|
| 8 |
+
# should expose and that DomainMatrix should mostly rely on.
|
| 9 |
+
#
|
| 10 |
+
# More in-depth tests of the heavier algorithms like rref etc should go in
|
| 11 |
+
# their own test files.
|
| 12 |
+
#
|
| 13 |
+
# Any new methods added to the DDM, SDM or DFM classes should be tested here
|
| 14 |
+
# and added to all classes.
|
| 15 |
+
#
|
| 16 |
+
|
| 17 |
+
from sympy.external.gmpy import GROUND_TYPES
|
| 18 |
+
|
| 19 |
+
from sympy import ZZ, QQ, GF, ZZ_I, symbols
|
| 20 |
+
|
| 21 |
+
from sympy.polys.matrices.exceptions import (
|
| 22 |
+
DMBadInputError,
|
| 23 |
+
DMDomainError,
|
| 24 |
+
DMNonSquareMatrixError,
|
| 25 |
+
DMNonInvertibleMatrixError,
|
| 26 |
+
DMShapeError,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
from sympy.polys.matrices.domainmatrix import DM, DomainMatrix, DDM, SDM, DFM
|
| 30 |
+
|
| 31 |
+
from sympy.testing.pytest import raises, skip
|
| 32 |
+
import pytest
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_XXM_constructors():
|
| 36 |
+
"""Test the DDM, etc constructors."""
|
| 37 |
+
|
| 38 |
+
lol = [
|
| 39 |
+
[ZZ(1), ZZ(2)],
|
| 40 |
+
[ZZ(3), ZZ(4)],
|
| 41 |
+
[ZZ(5), ZZ(6)],
|
| 42 |
+
]
|
| 43 |
+
dod = {
|
| 44 |
+
0: {0: ZZ(1), 1: ZZ(2)},
|
| 45 |
+
1: {0: ZZ(3), 1: ZZ(4)},
|
| 46 |
+
2: {0: ZZ(5), 1: ZZ(6)},
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
lol_0x0 = []
|
| 50 |
+
lol_0x2 = []
|
| 51 |
+
lol_2x0 = [[], []]
|
| 52 |
+
dod_0x0 = {}
|
| 53 |
+
dod_0x2 = {}
|
| 54 |
+
dod_2x0 = {}
|
| 55 |
+
|
| 56 |
+
lol_bad = [
|
| 57 |
+
[ZZ(1), ZZ(2)],
|
| 58 |
+
[ZZ(3), ZZ(4)],
|
| 59 |
+
[ZZ(5), ZZ(6), ZZ(7)],
|
| 60 |
+
]
|
| 61 |
+
dod_bad = {
|
| 62 |
+
0: {0: ZZ(1), 1: ZZ(2)},
|
| 63 |
+
1: {0: ZZ(3), 1: ZZ(4)},
|
| 64 |
+
2: {0: ZZ(5), 1: ZZ(6), 2: ZZ(7)},
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
XDM_dense = [DDM]
|
| 68 |
+
XDM_sparse = [SDM]
|
| 69 |
+
|
| 70 |
+
if GROUND_TYPES == 'flint':
|
| 71 |
+
XDM_dense.append(DFM)
|
| 72 |
+
|
| 73 |
+
for XDM in XDM_dense:
|
| 74 |
+
|
| 75 |
+
A = XDM(lol, (3, 2), ZZ)
|
| 76 |
+
assert A.rows == 3
|
| 77 |
+
assert A.cols == 2
|
| 78 |
+
assert A.domain == ZZ
|
| 79 |
+
assert A.shape == (3, 2)
|
| 80 |
+
if XDM is not DFM:
|
| 81 |
+
assert ZZ.of_type(A[0][0]) is True
|
| 82 |
+
else:
|
| 83 |
+
assert ZZ.of_type(A.rep[0, 0]) is True
|
| 84 |
+
|
| 85 |
+
Adm = DomainMatrix(lol, (3, 2), ZZ)
|
| 86 |
+
if XDM is DFM:
|
| 87 |
+
assert Adm.rep == A
|
| 88 |
+
assert Adm.rep.to_ddm() != A
|
| 89 |
+
elif GROUND_TYPES == 'flint':
|
| 90 |
+
assert Adm.rep.to_ddm() == A
|
| 91 |
+
assert Adm.rep != A
|
| 92 |
+
else:
|
| 93 |
+
assert Adm.rep == A
|
| 94 |
+
assert Adm.rep.to_ddm() == A
|
| 95 |
+
|
| 96 |
+
assert XDM(lol_0x0, (0, 0), ZZ).shape == (0, 0)
|
| 97 |
+
assert XDM(lol_0x2, (0, 2), ZZ).shape == (0, 2)
|
| 98 |
+
assert XDM(lol_2x0, (2, 0), ZZ).shape == (2, 0)
|
| 99 |
+
raises(DMBadInputError, lambda: XDM(lol, (2, 3), ZZ))
|
| 100 |
+
raises(DMBadInputError, lambda: XDM(lol_bad, (3, 2), ZZ))
|
| 101 |
+
raises(DMBadInputError, lambda: XDM(dod, (3, 2), ZZ))
|
| 102 |
+
|
| 103 |
+
for XDM in XDM_sparse:
|
| 104 |
+
|
| 105 |
+
A = XDM(dod, (3, 2), ZZ)
|
| 106 |
+
assert A.rows == 3
|
| 107 |
+
assert A.cols == 2
|
| 108 |
+
assert A.domain == ZZ
|
| 109 |
+
assert A.shape == (3, 2)
|
| 110 |
+
assert ZZ.of_type(A[0][0]) is True
|
| 111 |
+
|
| 112 |
+
assert DomainMatrix(dod, (3, 2), ZZ).rep == A
|
| 113 |
+
|
| 114 |
+
assert XDM(dod_0x0, (0, 0), ZZ).shape == (0, 0)
|
| 115 |
+
assert XDM(dod_0x2, (0, 2), ZZ).shape == (0, 2)
|
| 116 |
+
assert XDM(dod_2x0, (2, 0), ZZ).shape == (2, 0)
|
| 117 |
+
raises(DMBadInputError, lambda: XDM(dod, (2, 3), ZZ))
|
| 118 |
+
raises(DMBadInputError, lambda: XDM(lol, (3, 2), ZZ))
|
| 119 |
+
raises(DMBadInputError, lambda: XDM(dod_bad, (3, 2), ZZ))
|
| 120 |
+
|
| 121 |
+
raises(DMBadInputError, lambda: DomainMatrix(lol, (2, 3), ZZ))
|
| 122 |
+
raises(DMBadInputError, lambda: DomainMatrix(lol_bad, (3, 2), ZZ))
|
| 123 |
+
raises(DMBadInputError, lambda: DomainMatrix(dod_bad, (3, 2), ZZ))
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def test_XXM_eq():
|
| 127 |
+
"""Test equality for DDM, SDM, DFM and DomainMatrix."""
|
| 128 |
+
|
| 129 |
+
lol1 = [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]]
|
| 130 |
+
dod1 = {0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}
|
| 131 |
+
|
| 132 |
+
lol2 = [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(5)]]
|
| 133 |
+
dod2 = {0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(5)}}
|
| 134 |
+
|
| 135 |
+
A1_ddm = DDM(lol1, (2, 2), ZZ)
|
| 136 |
+
A1_sdm = SDM(dod1, (2, 2), ZZ)
|
| 137 |
+
A1_dm_d = DomainMatrix(lol1, (2, 2), ZZ)
|
| 138 |
+
A1_dm_s = DomainMatrix(dod1, (2, 2), ZZ)
|
| 139 |
+
|
| 140 |
+
A2_ddm = DDM(lol2, (2, 2), ZZ)
|
| 141 |
+
A2_sdm = SDM(dod2, (2, 2), ZZ)
|
| 142 |
+
A2_dm_d = DomainMatrix(lol2, (2, 2), ZZ)
|
| 143 |
+
A2_dm_s = DomainMatrix(dod2, (2, 2), ZZ)
|
| 144 |
+
|
| 145 |
+
A1_all = [A1_ddm, A1_sdm, A1_dm_d, A1_dm_s]
|
| 146 |
+
A2_all = [A2_ddm, A2_sdm, A2_dm_d, A2_dm_s]
|
| 147 |
+
|
| 148 |
+
if GROUND_TYPES == 'flint':
|
| 149 |
+
|
| 150 |
+
A1_dfm = DFM([[1, 2], [3, 4]], (2, 2), ZZ)
|
| 151 |
+
A2_dfm = DFM([[1, 2], [3, 5]], (2, 2), ZZ)
|
| 152 |
+
|
| 153 |
+
A1_all.append(A1_dfm)
|
| 154 |
+
A2_all.append(A2_dfm)
|
| 155 |
+
|
| 156 |
+
for n, An in enumerate(A1_all):
|
| 157 |
+
for m, Am in enumerate(A1_all):
|
| 158 |
+
if n == m:
|
| 159 |
+
assert (An == Am) is True
|
| 160 |
+
assert (An != Am) is False
|
| 161 |
+
else:
|
| 162 |
+
assert (An == Am) is False
|
| 163 |
+
assert (An != Am) is True
|
| 164 |
+
|
| 165 |
+
for n, An in enumerate(A2_all):
|
| 166 |
+
for m, Am in enumerate(A2_all):
|
| 167 |
+
if n == m:
|
| 168 |
+
assert (An == Am) is True
|
| 169 |
+
assert (An != Am) is False
|
| 170 |
+
else:
|
| 171 |
+
assert (An == Am) is False
|
| 172 |
+
assert (An != Am) is True
|
| 173 |
+
|
| 174 |
+
for n, A1 in enumerate(A1_all):
|
| 175 |
+
for m, A2 in enumerate(A2_all):
|
| 176 |
+
assert (A1 == A2) is False
|
| 177 |
+
assert (A1 != A2) is True
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def test_to_XXM():
|
| 181 |
+
"""Test to_ddm etc. for DDM, SDM, DFM and DomainMatrix."""
|
| 182 |
+
|
| 183 |
+
lol = [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]]
|
| 184 |
+
dod = {0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}
|
| 185 |
+
|
| 186 |
+
A_ddm = DDM(lol, (2, 2), ZZ)
|
| 187 |
+
A_sdm = SDM(dod, (2, 2), ZZ)
|
| 188 |
+
A_dm_d = DomainMatrix(lol, (2, 2), ZZ)
|
| 189 |
+
A_dm_s = DomainMatrix(dod, (2, 2), ZZ)
|
| 190 |
+
|
| 191 |
+
A_all = [A_ddm, A_sdm, A_dm_d, A_dm_s]
|
| 192 |
+
|
| 193 |
+
if GROUND_TYPES == 'flint':
|
| 194 |
+
A_dfm = DFM(lol, (2, 2), ZZ)
|
| 195 |
+
A_all.append(A_dfm)
|
| 196 |
+
|
| 197 |
+
for A in A_all:
|
| 198 |
+
assert A.to_ddm() == A_ddm
|
| 199 |
+
assert A.to_sdm() == A_sdm
|
| 200 |
+
if GROUND_TYPES != 'flint':
|
| 201 |
+
raises(NotImplementedError, lambda: A.to_dfm())
|
| 202 |
+
assert A.to_dfm_or_ddm() == A_ddm
|
| 203 |
+
|
| 204 |
+
# Add e.g. DDM.to_DM()?
|
| 205 |
+
# assert A.to_DM() == A_dm
|
| 206 |
+
|
| 207 |
+
if GROUND_TYPES == 'flint':
|
| 208 |
+
for A in A_all:
|
| 209 |
+
assert A.to_dfm() == A_dfm
|
| 210 |
+
for K in [ZZ, QQ, GF(5), ZZ_I]:
|
| 211 |
+
if isinstance(A, DFM) and not DFM._supports_domain(K):
|
| 212 |
+
raises(NotImplementedError, lambda: A.convert_to(K))
|
| 213 |
+
else:
|
| 214 |
+
A_K = A.convert_to(K)
|
| 215 |
+
if DFM._supports_domain(K):
|
| 216 |
+
A_dfm_K = A_dfm.convert_to(K)
|
| 217 |
+
assert A_K.to_dfm() == A_dfm_K
|
| 218 |
+
assert A_K.to_dfm_or_ddm() == A_dfm_K
|
| 219 |
+
else:
|
| 220 |
+
raises(NotImplementedError, lambda: A_K.to_dfm())
|
| 221 |
+
assert A_K.to_dfm_or_ddm() == A_ddm.convert_to(K)
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def test_DFM_domains():
|
| 225 |
+
"""Test which domains are supported by DFM."""
|
| 226 |
+
|
| 227 |
+
x, y = symbols('x, y')
|
| 228 |
+
|
| 229 |
+
if GROUND_TYPES in ('python', 'gmpy'):
|
| 230 |
+
|
| 231 |
+
supported = []
|
| 232 |
+
flint_funcs = {}
|
| 233 |
+
not_supported = [ZZ, QQ, GF(5), QQ[x], QQ[x,y]]
|
| 234 |
+
|
| 235 |
+
elif GROUND_TYPES == 'flint':
|
| 236 |
+
|
| 237 |
+
import flint
|
| 238 |
+
supported = [ZZ, QQ]
|
| 239 |
+
flint_funcs = {
|
| 240 |
+
ZZ: flint.fmpz_mat,
|
| 241 |
+
QQ: flint.fmpq_mat,
|
| 242 |
+
}
|
| 243 |
+
not_supported = [
|
| 244 |
+
# This could be supported but not yet implemented in SymPy:
|
| 245 |
+
GF(5),
|
| 246 |
+
# Other domains could be supported but not implemented as matrices
|
| 247 |
+
# in python-flint:
|
| 248 |
+
QQ[x],
|
| 249 |
+
QQ[x,y],
|
| 250 |
+
QQ.frac_field(x,y),
|
| 251 |
+
# Others would potentially never be supported by python-flint:
|
| 252 |
+
ZZ_I,
|
| 253 |
+
]
|
| 254 |
+
|
| 255 |
+
else:
|
| 256 |
+
assert False, "Unknown GROUND_TYPES: %s" % GROUND_TYPES
|
| 257 |
+
|
| 258 |
+
for domain in supported:
|
| 259 |
+
assert DFM._supports_domain(domain) is True
|
| 260 |
+
assert DFM._get_flint_func(domain) == flint_funcs[domain]
|
| 261 |
+
for domain in not_supported:
|
| 262 |
+
assert DFM._supports_domain(domain) is False
|
| 263 |
+
raises(NotImplementedError, lambda: DFM._get_flint_func(domain))
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def _DM(lol, typ, K):
|
| 267 |
+
"""Make a DM of type typ over K from lol."""
|
| 268 |
+
A = DM(lol, K)
|
| 269 |
+
|
| 270 |
+
if typ == 'DDM':
|
| 271 |
+
return A.to_ddm()
|
| 272 |
+
elif typ == 'SDM':
|
| 273 |
+
return A.to_sdm()
|
| 274 |
+
elif typ == 'DFM':
|
| 275 |
+
if GROUND_TYPES != 'flint':
|
| 276 |
+
skip("DFM not supported in this ground type")
|
| 277 |
+
return A.to_dfm()
|
| 278 |
+
else:
|
| 279 |
+
assert False, "Unknown type %s" % typ
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
def _DMZ(lol, typ):
|
| 283 |
+
"""Make a DM of type typ over ZZ from lol."""
|
| 284 |
+
return _DM(lol, typ, ZZ)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def _DMQ(lol, typ):
|
| 288 |
+
"""Make a DM of type typ over QQ from lol."""
|
| 289 |
+
return _DM(lol, typ, QQ)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def DM_ddm(lol, K):
|
| 293 |
+
"""Make a DDM over K from lol."""
|
| 294 |
+
return _DM(lol, 'DDM', K)
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
def DM_sdm(lol, K):
|
| 298 |
+
"""Make a SDM over K from lol."""
|
| 299 |
+
return _DM(lol, 'SDM', K)
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
def DM_dfm(lol, K):
|
| 303 |
+
"""Make a DFM over K from lol."""
|
| 304 |
+
return _DM(lol, 'DFM', K)
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def DMZ_ddm(lol):
|
| 308 |
+
"""Make a DDM from lol."""
|
| 309 |
+
return _DMZ(lol, 'DDM')
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def DMZ_sdm(lol):
|
| 313 |
+
"""Make a SDM from lol."""
|
| 314 |
+
return _DMZ(lol, 'SDM')
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
def DMZ_dfm(lol):
|
| 318 |
+
"""Make a DFM from lol."""
|
| 319 |
+
return _DMZ(lol, 'DFM')
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def DMQ_ddm(lol):
|
| 323 |
+
"""Make a DDM from lol."""
|
| 324 |
+
return _DMQ(lol, 'DDM')
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def DMQ_sdm(lol):
|
| 328 |
+
"""Make a SDM from lol."""
|
| 329 |
+
return _DMQ(lol, 'SDM')
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
def DMQ_dfm(lol):
|
| 333 |
+
"""Make a DFM from lol."""
|
| 334 |
+
return _DMQ(lol, 'DFM')
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
DM_all = [DM_ddm, DM_sdm, DM_dfm]
|
| 338 |
+
DMZ_all = [DMZ_ddm, DMZ_sdm, DMZ_dfm]
|
| 339 |
+
DMQ_all = [DMQ_ddm, DMQ_sdm, DMQ_dfm]
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 343 |
+
def test_XDM_getitem(DM):
|
| 344 |
+
"""Test getitem for DDM, etc."""
|
| 345 |
+
|
| 346 |
+
lol = [[0, 1], [2, 0]]
|
| 347 |
+
A = DM(lol)
|
| 348 |
+
m, n = A.shape
|
| 349 |
+
|
| 350 |
+
indices = [-3, -2, -1, 0, 1, 2]
|
| 351 |
+
|
| 352 |
+
for i in indices:
|
| 353 |
+
for j in indices:
|
| 354 |
+
if -2 <= i < m and -2 <= j < n:
|
| 355 |
+
assert A.getitem(i, j) == ZZ(lol[i][j])
|
| 356 |
+
else:
|
| 357 |
+
raises(IndexError, lambda: A.getitem(i, j))
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 361 |
+
def test_XDM_setitem(DM):
|
| 362 |
+
"""Test setitem for DDM, etc."""
|
| 363 |
+
|
| 364 |
+
A = DM([[0, 1, 2], [3, 4, 5]])
|
| 365 |
+
|
| 366 |
+
A.setitem(0, 0, ZZ(6))
|
| 367 |
+
assert A == DM([[6, 1, 2], [3, 4, 5]])
|
| 368 |
+
|
| 369 |
+
A.setitem(0, 1, ZZ(7))
|
| 370 |
+
assert A == DM([[6, 7, 2], [3, 4, 5]])
|
| 371 |
+
|
| 372 |
+
A.setitem(0, 2, ZZ(8))
|
| 373 |
+
assert A == DM([[6, 7, 8], [3, 4, 5]])
|
| 374 |
+
|
| 375 |
+
A.setitem(0, -1, ZZ(9))
|
| 376 |
+
assert A == DM([[6, 7, 9], [3, 4, 5]])
|
| 377 |
+
|
| 378 |
+
A.setitem(0, -2, ZZ(10))
|
| 379 |
+
assert A == DM([[6, 10, 9], [3, 4, 5]])
|
| 380 |
+
|
| 381 |
+
A.setitem(0, -3, ZZ(11))
|
| 382 |
+
assert A == DM([[11, 10, 9], [3, 4, 5]])
|
| 383 |
+
|
| 384 |
+
raises(IndexError, lambda: A.setitem(0, 3, ZZ(12)))
|
| 385 |
+
raises(IndexError, lambda: A.setitem(0, -4, ZZ(13)))
|
| 386 |
+
|
| 387 |
+
A.setitem(1, 0, ZZ(14))
|
| 388 |
+
assert A == DM([[11, 10, 9], [14, 4, 5]])
|
| 389 |
+
|
| 390 |
+
A.setitem(1, 1, ZZ(15))
|
| 391 |
+
assert A == DM([[11, 10, 9], [14, 15, 5]])
|
| 392 |
+
|
| 393 |
+
A.setitem(-1, 1, ZZ(16))
|
| 394 |
+
assert A == DM([[11, 10, 9], [14, 16, 5]])
|
| 395 |
+
|
| 396 |
+
A.setitem(-2, 1, ZZ(17))
|
| 397 |
+
assert A == DM([[11, 17, 9], [14, 16, 5]])
|
| 398 |
+
|
| 399 |
+
raises(IndexError, lambda: A.setitem(2, 0, ZZ(18)))
|
| 400 |
+
raises(IndexError, lambda: A.setitem(-3, 0, ZZ(19)))
|
| 401 |
+
|
| 402 |
+
A.setitem(1, 2, ZZ(0))
|
| 403 |
+
assert A == DM([[11, 17, 9], [14, 16, 0]])
|
| 404 |
+
|
| 405 |
+
A.setitem(1, -2, ZZ(0))
|
| 406 |
+
assert A == DM([[11, 17, 9], [14, 0, 0]])
|
| 407 |
+
|
| 408 |
+
A.setitem(1, -3, ZZ(0))
|
| 409 |
+
assert A == DM([[11, 17, 9], [0, 0, 0]])
|
| 410 |
+
|
| 411 |
+
A.setitem(0, 0, ZZ(0))
|
| 412 |
+
assert A == DM([[0, 17, 9], [0, 0, 0]])
|
| 413 |
+
|
| 414 |
+
A.setitem(0, -1, ZZ(0))
|
| 415 |
+
assert A == DM([[0, 17, 0], [0, 0, 0]])
|
| 416 |
+
|
| 417 |
+
A.setitem(0, 0, ZZ(0))
|
| 418 |
+
assert A == DM([[0, 17, 0], [0, 0, 0]])
|
| 419 |
+
|
| 420 |
+
A.setitem(0, -2, ZZ(0))
|
| 421 |
+
assert A == DM([[0, 0, 0], [0, 0, 0]])
|
| 422 |
+
|
| 423 |
+
A.setitem(0, -3, ZZ(1))
|
| 424 |
+
assert A == DM([[1, 0, 0], [0, 0, 0]])
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
class _Sliced:
|
| 428 |
+
def __getitem__(self, item):
|
| 429 |
+
return item
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
_slice = _Sliced()
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 436 |
+
def test_XXM_extract_slice(DM):
|
| 437 |
+
A = DM([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 438 |
+
assert A.extract_slice(*_slice[:,:]) == A
|
| 439 |
+
assert A.extract_slice(*_slice[1:,:]) == DM([[4, 5, 6], [7, 8, 9]])
|
| 440 |
+
assert A.extract_slice(*_slice[1:,1:]) == DM([[5, 6], [8, 9]])
|
| 441 |
+
assert A.extract_slice(*_slice[1:,:-1]) == DM([[4, 5], [7, 8]])
|
| 442 |
+
assert A.extract_slice(*_slice[1:,:-1:2]) == DM([[4], [7]])
|
| 443 |
+
assert A.extract_slice(*_slice[:,::2]) == DM([[1, 3], [4, 6], [7, 9]])
|
| 444 |
+
assert A.extract_slice(*_slice[::2,:]) == DM([[1, 2, 3], [7, 8, 9]])
|
| 445 |
+
assert A.extract_slice(*_slice[::2,::2]) == DM([[1, 3], [7, 9]])
|
| 446 |
+
assert A.extract_slice(*_slice[::2,::-2]) == DM([[3, 1], [9, 7]])
|
| 447 |
+
assert A.extract_slice(*_slice[::-2,::2]) == DM([[7, 9], [1, 3]])
|
| 448 |
+
assert A.extract_slice(*_slice[::-2,::-2]) == DM([[9, 7], [3, 1]])
|
| 449 |
+
assert A.extract_slice(*_slice[:,::-1]) == DM([[3, 2, 1], [6, 5, 4], [9, 8, 7]])
|
| 450 |
+
assert A.extract_slice(*_slice[::-1,:]) == DM([[7, 8, 9], [4, 5, 6], [1, 2, 3]])
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 454 |
+
def test_XXM_extract(DM):
|
| 455 |
+
|
| 456 |
+
A = DM([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 457 |
+
|
| 458 |
+
assert A.extract([0, 1, 2], [0, 1, 2]) == A
|
| 459 |
+
assert A.extract([1, 2], [1, 2]) == DM([[5, 6], [8, 9]])
|
| 460 |
+
assert A.extract([1, 2], [0, 1]) == DM([[4, 5], [7, 8]])
|
| 461 |
+
assert A.extract([1, 2], [0, 2]) == DM([[4, 6], [7, 9]])
|
| 462 |
+
assert A.extract([1, 2], [0]) == DM([[4], [7]])
|
| 463 |
+
assert A.extract([1, 2], []) == DM([[1]]).zeros((2, 0), ZZ)
|
| 464 |
+
assert A.extract([], [0, 1, 2]) == DM([[1]]).zeros((0, 3), ZZ)
|
| 465 |
+
|
| 466 |
+
raises(IndexError, lambda: A.extract([1, 2], [0, 3]))
|
| 467 |
+
raises(IndexError, lambda: A.extract([1, 2], [0, -4]))
|
| 468 |
+
raises(IndexError, lambda: A.extract([3, 1], [0, 1]))
|
| 469 |
+
raises(IndexError, lambda: A.extract([-4, 2], [3, 1]))
|
| 470 |
+
|
| 471 |
+
B = DM([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
|
| 472 |
+
assert B.extract([1, 2], [1, 2]) == DM([[0, 0], [0, 0]])
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
def test_XXM_str():
|
| 476 |
+
|
| 477 |
+
A = DomainMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], (3, 3), ZZ)
|
| 478 |
+
|
| 479 |
+
assert str(A) == \
|
| 480 |
+
'DomainMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], (3, 3), ZZ)'
|
| 481 |
+
assert str(A.to_ddm()) == \
|
| 482 |
+
'[[1, 2, 3], [4, 5, 6], [7, 8, 9]]'
|
| 483 |
+
assert str(A.to_sdm()) == \
|
| 484 |
+
'{0: {0: 1, 1: 2, 2: 3}, 1: {0: 4, 1: 5, 2: 6}, 2: {0: 7, 1: 8, 2: 9}}'
|
| 485 |
+
|
| 486 |
+
assert repr(A) == \
|
| 487 |
+
'DomainMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], (3, 3), ZZ)'
|
| 488 |
+
assert repr(A.to_ddm()) == \
|
| 489 |
+
'DDM([[1, 2, 3], [4, 5, 6], [7, 8, 9]], (3, 3), ZZ)'
|
| 490 |
+
assert repr(A.to_sdm()) == \
|
| 491 |
+
'SDM({0: {0: 1, 1: 2, 2: 3}, 1: {0: 4, 1: 5, 2: 6}, 2: {0: 7, 1: 8, 2: 9}}, (3, 3), ZZ)'
|
| 492 |
+
|
| 493 |
+
B = DomainMatrix({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3)}}, (2, 2), ZZ)
|
| 494 |
+
|
| 495 |
+
assert str(B) == \
|
| 496 |
+
'DomainMatrix({0: {0: 1, 1: 2}, 1: {0: 3}}, (2, 2), ZZ)'
|
| 497 |
+
assert str(B.to_ddm()) == \
|
| 498 |
+
'[[1, 2], [3, 0]]'
|
| 499 |
+
assert str(B.to_sdm()) == \
|
| 500 |
+
'{0: {0: 1, 1: 2}, 1: {0: 3}}'
|
| 501 |
+
|
| 502 |
+
assert repr(B) == \
|
| 503 |
+
'DomainMatrix({0: {0: 1, 1: 2}, 1: {0: 3}}, (2, 2), ZZ)'
|
| 504 |
+
|
| 505 |
+
if GROUND_TYPES != 'gmpy':
|
| 506 |
+
assert repr(B.to_ddm()) == \
|
| 507 |
+
'DDM([[1, 2], [3, 0]], (2, 2), ZZ)'
|
| 508 |
+
assert repr(B.to_sdm()) == \
|
| 509 |
+
'SDM({0: {0: 1, 1: 2}, 1: {0: 3}}, (2, 2), ZZ)'
|
| 510 |
+
else:
|
| 511 |
+
assert repr(B.to_ddm()) == \
|
| 512 |
+
'DDM([[mpz(1), mpz(2)], [mpz(3), mpz(0)]], (2, 2), ZZ)'
|
| 513 |
+
assert repr(B.to_sdm()) == \
|
| 514 |
+
'SDM({0: {0: mpz(1), 1: mpz(2)}, 1: {0: mpz(3)}}, (2, 2), ZZ)'
|
| 515 |
+
|
| 516 |
+
if GROUND_TYPES == 'flint':
|
| 517 |
+
|
| 518 |
+
assert str(A.to_dfm()) == \
|
| 519 |
+
'[[1, 2, 3], [4, 5, 6], [7, 8, 9]]'
|
| 520 |
+
assert str(B.to_dfm()) == \
|
| 521 |
+
'[[1, 2], [3, 0]]'
|
| 522 |
+
|
| 523 |
+
assert repr(A.to_dfm()) == \
|
| 524 |
+
'DFM([[1, 2, 3], [4, 5, 6], [7, 8, 9]], (3, 3), ZZ)'
|
| 525 |
+
assert repr(B.to_dfm()) == \
|
| 526 |
+
'DFM([[1, 2], [3, 0]], (2, 2), ZZ)'
|
| 527 |
+
|
| 528 |
+
|
| 529 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 530 |
+
def test_XXM_from_list(DM):
|
| 531 |
+
T = type(DM([[0]]))
|
| 532 |
+
|
| 533 |
+
lol = [[1, 2, 4], [4, 5, 6]]
|
| 534 |
+
lol_ZZ = [[ZZ(1), ZZ(2), ZZ(4)], [ZZ(4), ZZ(5), ZZ(6)]]
|
| 535 |
+
lol_ZZ_bad = [[ZZ(1), ZZ(2), ZZ(4)], [ZZ(4), ZZ(5), ZZ(6), ZZ(7)]]
|
| 536 |
+
|
| 537 |
+
assert T.from_list(lol_ZZ, (2, 3), ZZ) == DM(lol)
|
| 538 |
+
raises(DMBadInputError, lambda: T.from_list(lol_ZZ_bad, (3, 2), ZZ))
|
| 539 |
+
|
| 540 |
+
|
| 541 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 542 |
+
def test_XXM_to_list(DM):
|
| 543 |
+
lol = [[1, 2, 4], [4, 5, 6]]
|
| 544 |
+
assert DM(lol).to_list() == [[ZZ(1), ZZ(2), ZZ(4)], [ZZ(4), ZZ(5), ZZ(6)]]
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 548 |
+
def test_XXM_to_list_flat(DM):
|
| 549 |
+
lol = [[1, 2, 4], [4, 5, 6]]
|
| 550 |
+
assert DM(lol).to_list_flat() == [ZZ(1), ZZ(2), ZZ(4), ZZ(4), ZZ(5), ZZ(6)]
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 554 |
+
def test_XXM_from_list_flat(DM):
|
| 555 |
+
T = type(DM([[0]]))
|
| 556 |
+
flat = [ZZ(1), ZZ(2), ZZ(4), ZZ(4), ZZ(5), ZZ(6)]
|
| 557 |
+
assert T.from_list_flat(flat, (2, 3), ZZ) == DM([[1, 2, 4], [4, 5, 6]])
|
| 558 |
+
raises(DMBadInputError, lambda: T.from_list_flat(flat, (3, 3), ZZ))
|
| 559 |
+
|
| 560 |
+
|
| 561 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 562 |
+
def test_XXM_to_flat_nz(DM):
|
| 563 |
+
M = DM([[1, 2, 0], [0, 0, 0], [0, 0, 3]])
|
| 564 |
+
elements = [ZZ(1), ZZ(2), ZZ(3)]
|
| 565 |
+
indices = ((0, 0), (0, 1), (2, 2))
|
| 566 |
+
assert M.to_flat_nz() == (elements, (indices, M.shape))
|
| 567 |
+
|
| 568 |
+
|
| 569 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 570 |
+
def test_XXM_from_flat_nz(DM):
|
| 571 |
+
T = type(DM([[0]]))
|
| 572 |
+
elements = [ZZ(1), ZZ(2), ZZ(3)]
|
| 573 |
+
indices = ((0, 0), (0, 1), (2, 2))
|
| 574 |
+
data = (indices, (3, 3))
|
| 575 |
+
result = DM([[1, 2, 0], [0, 0, 0], [0, 0, 3]])
|
| 576 |
+
assert T.from_flat_nz(elements, data, ZZ) == result
|
| 577 |
+
raises(DMBadInputError, lambda: T.from_flat_nz(elements, (indices, (2, 3)), ZZ))
|
| 578 |
+
|
| 579 |
+
|
| 580 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 581 |
+
def test_XXM_to_dod(DM):
|
| 582 |
+
dod = {0: {0: ZZ(1), 2: ZZ(4)}, 1: {0: ZZ(4), 1: ZZ(5), 2: ZZ(6)}}
|
| 583 |
+
assert DM([[1, 0, 4], [4, 5, 6]]).to_dod() == dod
|
| 584 |
+
|
| 585 |
+
|
| 586 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 587 |
+
def test_XXM_from_dod(DM):
|
| 588 |
+
T = type(DM([[0]]))
|
| 589 |
+
dod = {0: {0: ZZ(1), 2: ZZ(4)}, 1: {0: ZZ(4), 1: ZZ(5), 2: ZZ(6)}}
|
| 590 |
+
assert T.from_dod(dod, (2, 3), ZZ) == DM([[1, 0, 4], [4, 5, 6]])
|
| 591 |
+
|
| 592 |
+
|
| 593 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 594 |
+
def test_XXM_to_dok(DM):
|
| 595 |
+
dod = {(0, 0): ZZ(1), (0, 2): ZZ(4),
|
| 596 |
+
(1, 0): ZZ(4), (1, 1): ZZ(5), (1, 2): ZZ(6)}
|
| 597 |
+
assert DM([[1, 0, 4], [4, 5, 6]]).to_dok() == dod
|
| 598 |
+
|
| 599 |
+
|
| 600 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 601 |
+
def test_XXM_from_dok(DM):
|
| 602 |
+
T = type(DM([[0]]))
|
| 603 |
+
dod = {(0, 0): ZZ(1), (0, 2): ZZ(4),
|
| 604 |
+
(1, 0): ZZ(4), (1, 1): ZZ(5), (1, 2): ZZ(6)}
|
| 605 |
+
assert T.from_dok(dod, (2, 3), ZZ) == DM([[1, 0, 4], [4, 5, 6]])
|
| 606 |
+
|
| 607 |
+
|
| 608 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 609 |
+
def test_XXM_iter_values(DM):
|
| 610 |
+
values = [ZZ(1), ZZ(4), ZZ(4), ZZ(5), ZZ(6)]
|
| 611 |
+
assert sorted(DM([[1, 0, 4], [4, 5, 6]]).iter_values()) == values
|
| 612 |
+
|
| 613 |
+
|
| 614 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 615 |
+
def test_XXM_iter_items(DM):
|
| 616 |
+
items = [((0, 0), ZZ(1)), ((0, 2), ZZ(4)),
|
| 617 |
+
((1, 0), ZZ(4)), ((1, 1), ZZ(5)), ((1, 2), ZZ(6))]
|
| 618 |
+
assert sorted(DM([[1, 0, 4], [4, 5, 6]]).iter_items()) == items
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 622 |
+
def test_XXM_from_ddm(DM):
|
| 623 |
+
T = type(DM([[0]]))
|
| 624 |
+
ddm = DDM([[1, 2, 4], [4, 5, 6]], (2, 3), ZZ)
|
| 625 |
+
assert T.from_ddm(ddm) == DM([[1, 2, 4], [4, 5, 6]])
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 629 |
+
def test_XXM_zeros(DM):
|
| 630 |
+
T = type(DM([[0]]))
|
| 631 |
+
assert T.zeros((2, 3), ZZ) == DM([[0, 0, 0], [0, 0, 0]])
|
| 632 |
+
|
| 633 |
+
|
| 634 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 635 |
+
def test_XXM_ones(DM):
|
| 636 |
+
T = type(DM([[0]]))
|
| 637 |
+
assert T.ones((2, 3), ZZ) == DM([[1, 1, 1], [1, 1, 1]])
|
| 638 |
+
|
| 639 |
+
|
| 640 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 641 |
+
def test_XXM_eye(DM):
|
| 642 |
+
T = type(DM([[0]]))
|
| 643 |
+
assert T.eye(3, ZZ) == DM([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 644 |
+
assert T.eye((3, 2), ZZ) == DM([[1, 0], [0, 1], [0, 0]])
|
| 645 |
+
|
| 646 |
+
|
| 647 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 648 |
+
def test_XXM_diag(DM):
|
| 649 |
+
T = type(DM([[0]]))
|
| 650 |
+
assert T.diag([1, 2, 3], ZZ) == DM([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
|
| 651 |
+
|
| 652 |
+
|
| 653 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 654 |
+
def test_XXM_transpose(DM):
|
| 655 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 656 |
+
assert A.transpose() == DM([[1, 4], [2, 5], [3, 6]])
|
| 657 |
+
|
| 658 |
+
|
| 659 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 660 |
+
def test_XXM_add(DM):
|
| 661 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 662 |
+
B = DM([[1, 2, 3], [4, 5, 6]])
|
| 663 |
+
C = DM([[2, 4, 6], [8, 10, 12]])
|
| 664 |
+
assert A.add(B) == C
|
| 665 |
+
|
| 666 |
+
|
| 667 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 668 |
+
def test_XXM_sub(DM):
|
| 669 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 670 |
+
B = DM([[1, 2, 3], [4, 5, 6]])
|
| 671 |
+
C = DM([[0, 0, 0], [0, 0, 0]])
|
| 672 |
+
assert A.sub(B) == C
|
| 673 |
+
|
| 674 |
+
|
| 675 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 676 |
+
def test_XXM_mul(DM):
|
| 677 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 678 |
+
b = ZZ(2)
|
| 679 |
+
assert A.mul(b) == DM([[2, 4, 6], [8, 10, 12]])
|
| 680 |
+
assert A.rmul(b) == DM([[2, 4, 6], [8, 10, 12]])
|
| 681 |
+
|
| 682 |
+
|
| 683 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 684 |
+
def test_XXM_matmul(DM):
|
| 685 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 686 |
+
B = DM([[1, 2], [3, 4], [5, 6]])
|
| 687 |
+
C = DM([[22, 28], [49, 64]])
|
| 688 |
+
assert A.matmul(B) == C
|
| 689 |
+
|
| 690 |
+
|
| 691 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 692 |
+
def test_XXM_mul_elementwise(DM):
|
| 693 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 694 |
+
B = DM([[1, 2, 3], [4, 5, 6]])
|
| 695 |
+
C = DM([[1, 4, 9], [16, 25, 36]])
|
| 696 |
+
assert A.mul_elementwise(B) == C
|
| 697 |
+
|
| 698 |
+
|
| 699 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 700 |
+
def test_XXM_neg(DM):
|
| 701 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 702 |
+
C = DM([[-1, -2, -3], [-4, -5, -6]])
|
| 703 |
+
assert A.neg() == C
|
| 704 |
+
|
| 705 |
+
|
| 706 |
+
@pytest.mark.parametrize('DM', DM_all)
|
| 707 |
+
def test_XXM_convert_to(DM):
|
| 708 |
+
A = DM([[1, 2, 3], [4, 5, 6]], ZZ)
|
| 709 |
+
B = DM([[1, 2, 3], [4, 5, 6]], QQ)
|
| 710 |
+
assert A.convert_to(QQ) == B
|
| 711 |
+
assert B.convert_to(ZZ) == A
|
| 712 |
+
|
| 713 |
+
|
| 714 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 715 |
+
def test_XXM_scc(DM):
|
| 716 |
+
A = DM([
|
| 717 |
+
[0, 1, 0, 0, 0, 0],
|
| 718 |
+
[1, 0, 0, 0, 0, 0],
|
| 719 |
+
[0, 0, 1, 0, 0, 0],
|
| 720 |
+
[0, 0, 0, 1, 0, 1],
|
| 721 |
+
[0, 0, 0, 0, 1, 0],
|
| 722 |
+
[0, 0, 0, 1, 0, 1]])
|
| 723 |
+
assert A.scc() == [[0, 1], [2], [3, 5], [4]]
|
| 724 |
+
|
| 725 |
+
|
| 726 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 727 |
+
def test_XXM_hstack(DM):
|
| 728 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 729 |
+
B = DM([[7, 8], [9, 10]])
|
| 730 |
+
C = DM([[1, 2, 3, 7, 8], [4, 5, 6, 9, 10]])
|
| 731 |
+
ABC = DM([[1, 2, 3, 7, 8, 1, 2, 3, 7, 8],
|
| 732 |
+
[4, 5, 6, 9, 10, 4, 5, 6, 9, 10]])
|
| 733 |
+
assert A.hstack(B) == C
|
| 734 |
+
assert A.hstack(B, C) == ABC
|
| 735 |
+
|
| 736 |
+
|
| 737 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 738 |
+
def test_XXM_vstack(DM):
|
| 739 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 740 |
+
B = DM([[7, 8, 9]])
|
| 741 |
+
C = DM([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 742 |
+
ABC = DM([[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 743 |
+
assert A.vstack(B) == C
|
| 744 |
+
assert A.vstack(B, C) == ABC
|
| 745 |
+
|
| 746 |
+
|
| 747 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 748 |
+
def test_XXM_applyfunc(DM):
|
| 749 |
+
A = DM([[1, 2, 3], [4, 5, 6]])
|
| 750 |
+
B = DM([[2, 4, 6], [8, 10, 12]])
|
| 751 |
+
assert A.applyfunc(lambda x: 2*x, ZZ) == B
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 755 |
+
def test_XXM_is_upper(DM):
|
| 756 |
+
assert DM([[1, 2, 3], [0, 5, 6]]).is_upper() is True
|
| 757 |
+
assert DM([[1, 2, 3], [4, 5, 6]]).is_upper() is False
|
| 758 |
+
|
| 759 |
+
|
| 760 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 761 |
+
def test_XXM_is_lower(DM):
|
| 762 |
+
assert DM([[1, 0, 0], [4, 5, 0]]).is_lower() is True
|
| 763 |
+
assert DM([[1, 2, 3], [4, 5, 6]]).is_lower() is False
|
| 764 |
+
|
| 765 |
+
|
| 766 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 767 |
+
def test_XXM_is_diagonal(DM):
|
| 768 |
+
assert DM([[1, 0, 0], [0, 5, 0]]).is_diagonal() is True
|
| 769 |
+
assert DM([[1, 2, 3], [4, 5, 6]]).is_diagonal() is False
|
| 770 |
+
|
| 771 |
+
|
| 772 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 773 |
+
def test_XXM_diagonal(DM):
|
| 774 |
+
assert DM([[1, 0, 0], [0, 5, 0]]).diagonal() == [1, 5]
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 778 |
+
def test_XXM_is_zero_matrix(DM):
|
| 779 |
+
assert DM([[0, 0, 0], [0, 0, 0]]).is_zero_matrix() is True
|
| 780 |
+
assert DM([[1, 0, 0], [0, 0, 0]]).is_zero_matrix() is False
|
| 781 |
+
|
| 782 |
+
|
| 783 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 784 |
+
def test_XXM_det_ZZ(DM):
|
| 785 |
+
assert DM([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).det() == 0
|
| 786 |
+
assert DM([[1, 2, 3], [4, 5, 6], [7, 8, 10]]).det() == -3
|
| 787 |
+
|
| 788 |
+
|
| 789 |
+
@pytest.mark.parametrize('DM', DMQ_all)
|
| 790 |
+
def test_XXM_det_QQ(DM):
|
| 791 |
+
dM1 = DM([[(1,2), (2,3)], [(3,4), (4,5)]])
|
| 792 |
+
assert dM1.det() == QQ(-1,10)
|
| 793 |
+
|
| 794 |
+
|
| 795 |
+
@pytest.mark.parametrize('DM', DMQ_all)
|
| 796 |
+
def test_XXM_inv_QQ(DM):
|
| 797 |
+
dM1 = DM([[(1,2), (2,3)], [(3,4), (4,5)]])
|
| 798 |
+
dM2 = DM([[(-8,1), (20,3)], [(15,2), (-5,1)]])
|
| 799 |
+
assert dM1.inv() == dM2
|
| 800 |
+
assert dM1.matmul(dM2) == DM([[1, 0], [0, 1]])
|
| 801 |
+
|
| 802 |
+
dM3 = DM([[(1,2), (2,3)], [(1,4), (1,3)]])
|
| 803 |
+
raises(DMNonInvertibleMatrixError, lambda: dM3.inv())
|
| 804 |
+
|
| 805 |
+
dM4 = DM([[(1,2), (2,3), (3,4)], [(1,4), (1,3), (1,2)]])
|
| 806 |
+
raises(DMNonSquareMatrixError, lambda: dM4.inv())
|
| 807 |
+
|
| 808 |
+
|
| 809 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 810 |
+
def test_XXM_inv_ZZ(DM):
|
| 811 |
+
dM1 = DM([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
|
| 812 |
+
# XXX: Maybe this should return a DM over QQ instead?
|
| 813 |
+
# XXX: Handle unimodular matrices?
|
| 814 |
+
raises(DMDomainError, lambda: dM1.inv())
|
| 815 |
+
|
| 816 |
+
|
| 817 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 818 |
+
def test_XXM_charpoly_ZZ(DM):
|
| 819 |
+
dM1 = DM([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
|
| 820 |
+
assert dM1.charpoly() == [1, -16, -12, 3]
|
| 821 |
+
|
| 822 |
+
|
| 823 |
+
@pytest.mark.parametrize('DM', DMQ_all)
|
| 824 |
+
def test_XXM_charpoly_QQ(DM):
|
| 825 |
+
dM1 = DM([[(1,2), (2,3)], [(3,4), (4,5)]])
|
| 826 |
+
assert dM1.charpoly() == [QQ(1,1), QQ(-13,10), QQ(-1,10)]
|
| 827 |
+
|
| 828 |
+
|
| 829 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 830 |
+
def test_XXM_lu_solve_ZZ(DM):
|
| 831 |
+
dM1 = DM([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
|
| 832 |
+
dM2 = DM([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 833 |
+
raises(DMDomainError, lambda: dM1.lu_solve(dM2))
|
| 834 |
+
|
| 835 |
+
|
| 836 |
+
@pytest.mark.parametrize('DM', DMQ_all)
|
| 837 |
+
def test_XXM_lu_solve_QQ(DM):
|
| 838 |
+
dM1 = DM([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
|
| 839 |
+
dM2 = DM([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 840 |
+
dM3 = DM([[(-2,3),(-4,3),(1,1)],[(-2,3),(11,3),(-2,1)],[(1,1),(-2,1),(1,1)]])
|
| 841 |
+
assert dM1.lu_solve(dM2) == dM3 == dM1.inv()
|
| 842 |
+
|
| 843 |
+
dM4 = DM([[1, 2, 3], [4, 5, 6]])
|
| 844 |
+
dM5 = DM([[1, 0], [0, 1], [0, 0]])
|
| 845 |
+
raises(DMShapeError, lambda: dM4.lu_solve(dM5))
|
| 846 |
+
|
| 847 |
+
|
| 848 |
+
@pytest.mark.parametrize('DM', DMQ_all)
|
| 849 |
+
def test_XXM_nullspace_QQ(DM):
|
| 850 |
+
dM1 = DM([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 851 |
+
# XXX: Change the signature to just return the nullspace. Possibly
|
| 852 |
+
# returning the rank or nullity makes sense but the list of nonpivots is
|
| 853 |
+
# not useful.
|
| 854 |
+
assert dM1.nullspace() == (DM([[1, -2, 1]]), [2])
|
| 855 |
+
|
| 856 |
+
|
| 857 |
+
@pytest.mark.parametrize('DM', DMZ_all)
|
| 858 |
+
def test_XXM_lll(DM):
|
| 859 |
+
M = DM([[1, 2, 3], [4, 5, 20]])
|
| 860 |
+
M_lll = DM([[1, 2, 3], [-1, -5, 5]])
|
| 861 |
+
T = DM([[1, 0], [-5, 1]])
|
| 862 |
+
assert M.lll() == M_lll
|
| 863 |
+
assert M.lll_transform() == (M_lll, T)
|
| 864 |
+
assert T.matmul(M) == M_lll
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/bal_Latn.dat
ADDED
|
Binary file (10.2 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/bn.dat
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c917a9df4b64c5204a90e82d216cd9044d58de9973b42228bbc5a285a9d6f12c
|
| 3 |
+
size 217482
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/ca_FR.dat
ADDED
|
Binary file (672 Bytes). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/en_AT.dat
ADDED
|
Binary file (1.31 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/en_SC.dat
ADDED
|
Binary file (1.19 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/et_EE.dat
ADDED
|
Binary file (653 Bytes). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/fr_WF.dat
ADDED
|
Binary file (616 Bytes). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/gu.dat
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d8b0511cdf49a126ff45e012b09b5598f597086e6b2e69a2a5078e9565fa8f4f
|
| 3 |
+
size 206876
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/he.dat
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:67d34108712607c9287293ce141a82b87c340fc0cc8c1661e9ca71c91727885f
|
| 3 |
+
size 219719
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/hnj.dat
ADDED
|
Binary file (2.12 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/ru_KG.dat
ADDED
|
Binary file (659 Bytes). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/scn.dat
ADDED
|
Binary file (9.37 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/so.dat
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a3e3818cddb2df338dc9156c49b32091b6545428e20d3a7bfd490b782ee4fa6d
|
| 3 |
+
size 162786
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/yue_Hant_HK.dat
ADDED
|
Binary file (636 Bytes). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/babel/locale-data/zh_Hant.dat
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6d57035e407e1ecfff4e17654e5c23048760ab1fc2152c122146c5599da09910
|
| 3 |
+
size 153398
|
vlmpy310/lib/python3.10/site-packages/msgpack/__init__.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ruff: noqa: F401
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from .exceptions import * # noqa: F403
|
| 5 |
+
from .ext import ExtType, Timestamp
|
| 6 |
+
|
| 7 |
+
version = (1, 1, 0)
|
| 8 |
+
__version__ = "1.1.0"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
if os.environ.get("MSGPACK_PUREPYTHON"):
|
| 12 |
+
from .fallback import Packer, Unpacker, unpackb
|
| 13 |
+
else:
|
| 14 |
+
try:
|
| 15 |
+
from ._cmsgpack import Packer, Unpacker, unpackb
|
| 16 |
+
except ImportError:
|
| 17 |
+
from .fallback import Packer, Unpacker, unpackb
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def pack(o, stream, **kwargs):
|
| 21 |
+
"""
|
| 22 |
+
Pack object `o` and write it to `stream`
|
| 23 |
+
|
| 24 |
+
See :class:`Packer` for options.
|
| 25 |
+
"""
|
| 26 |
+
packer = Packer(**kwargs)
|
| 27 |
+
stream.write(packer.pack(o))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def packb(o, **kwargs):
|
| 31 |
+
"""
|
| 32 |
+
Pack object `o` and return packed bytes
|
| 33 |
+
|
| 34 |
+
See :class:`Packer` for options.
|
| 35 |
+
"""
|
| 36 |
+
return Packer(**kwargs).pack(o)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def unpack(stream, **kwargs):
|
| 40 |
+
"""
|
| 41 |
+
Unpack an object from `stream`.
|
| 42 |
+
|
| 43 |
+
Raises `ExtraData` when `stream` contains extra bytes.
|
| 44 |
+
See :class:`Unpacker` for options.
|
| 45 |
+
"""
|
| 46 |
+
data = stream.read()
|
| 47 |
+
return unpackb(data, **kwargs)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# alias for compatibility to simplejson/marshal/pickle.
|
| 51 |
+
load = unpack
|
| 52 |
+
loads = unpackb
|
| 53 |
+
|
| 54 |
+
dump = pack
|
| 55 |
+
dumps = packb
|
vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.37 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/exceptions.cpython-310.pyc
ADDED
|
Binary file (1.78 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/ext.cpython-310.pyc
ADDED
|
Binary file (6.06 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/msgpack/__pycache__/fallback.cpython-310.pyc
ADDED
|
Binary file (23.7 kB). View file
|
|
|
vlmpy310/lib/python3.10/site-packages/msgpack/exceptions.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class UnpackException(Exception):
|
| 2 |
+
"""Base class for some exceptions raised while unpacking.
|
| 3 |
+
|
| 4 |
+
NOTE: unpack may raise exception other than subclass of
|
| 5 |
+
UnpackException. If you want to catch all error, catch
|
| 6 |
+
Exception instead.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class BufferFull(UnpackException):
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class OutOfData(UnpackException):
|
| 15 |
+
pass
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class FormatError(ValueError, UnpackException):
|
| 19 |
+
"""Invalid msgpack format"""
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class StackError(ValueError, UnpackException):
|
| 23 |
+
"""Too nested"""
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Deprecated. Use ValueError instead
|
| 27 |
+
UnpackValueError = ValueError
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class ExtraData(UnpackValueError):
|
| 31 |
+
"""ExtraData is raised when there is trailing data.
|
| 32 |
+
|
| 33 |
+
This exception is raised while only one-shot (not streaming)
|
| 34 |
+
unpack.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
def __init__(self, unpacked, extra):
|
| 38 |
+
self.unpacked = unpacked
|
| 39 |
+
self.extra = extra
|
| 40 |
+
|
| 41 |
+
def __str__(self):
|
| 42 |
+
return "unpack(b) received extra data."
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Deprecated. Use Exception instead to catch all exception during packing.
|
| 46 |
+
PackException = Exception
|
| 47 |
+
PackValueError = ValueError
|
| 48 |
+
PackOverflowError = OverflowError
|
vlmpy310/lib/python3.10/site-packages/msgpack/ext.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|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|
+
import struct
|
| 3 |
+
from collections import namedtuple
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class ExtType(namedtuple("ExtType", "code data")):
|
| 7 |
+
"""ExtType represents ext type in msgpack."""
|
| 8 |
+
|
| 9 |
+
def __new__(cls, code, data):
|
| 10 |
+
if not isinstance(code, int):
|
| 11 |
+
raise TypeError("code must be int")
|
| 12 |
+
if not isinstance(data, bytes):
|
| 13 |
+
raise TypeError("data must be bytes")
|
| 14 |
+
if not 0 <= code <= 127:
|
| 15 |
+
raise ValueError("code must be 0~127")
|
| 16 |
+
return super().__new__(cls, code, data)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class Timestamp:
|
| 20 |
+
"""Timestamp represents the Timestamp extension type in msgpack.
|
| 21 |
+
|
| 22 |
+
When built with Cython, msgpack uses C methods to pack and unpack `Timestamp`.
|
| 23 |
+
When using pure-Python msgpack, :func:`to_bytes` and :func:`from_bytes` are used to pack and
|
| 24 |
+
unpack `Timestamp`.
|
| 25 |
+
|
| 26 |
+
This class is immutable: Do not override seconds and nanoseconds.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
__slots__ = ["seconds", "nanoseconds"]
|
| 30 |
+
|
| 31 |
+
def __init__(self, seconds, nanoseconds=0):
|
| 32 |
+
"""Initialize a Timestamp object.
|
| 33 |
+
|
| 34 |
+
:param int seconds:
|
| 35 |
+
Number of seconds since the UNIX epoch (00:00:00 UTC Jan 1 1970, minus leap seconds).
|
| 36 |
+
May be negative.
|
| 37 |
+
|
| 38 |
+
:param int nanoseconds:
|
| 39 |
+
Number of nanoseconds to add to `seconds` to get fractional time.
|
| 40 |
+
Maximum is 999_999_999. Default is 0.
|
| 41 |
+
|
| 42 |
+
Note: Negative times (before the UNIX epoch) are represented as neg. seconds + pos. ns.
|
| 43 |
+
"""
|
| 44 |
+
if not isinstance(seconds, int):
|
| 45 |
+
raise TypeError("seconds must be an integer")
|
| 46 |
+
if not isinstance(nanoseconds, int):
|
| 47 |
+
raise TypeError("nanoseconds must be an integer")
|
| 48 |
+
if not (0 <= nanoseconds < 10**9):
|
| 49 |
+
raise ValueError("nanoseconds must be a non-negative integer less than 999999999.")
|
| 50 |
+
self.seconds = seconds
|
| 51 |
+
self.nanoseconds = nanoseconds
|
| 52 |
+
|
| 53 |
+
def __repr__(self):
|
| 54 |
+
"""String representation of Timestamp."""
|
| 55 |
+
return f"Timestamp(seconds={self.seconds}, nanoseconds={self.nanoseconds})"
|
| 56 |
+
|
| 57 |
+
def __eq__(self, other):
|
| 58 |
+
"""Check for equality with another Timestamp object"""
|
| 59 |
+
if type(other) is self.__class__:
|
| 60 |
+
return self.seconds == other.seconds and self.nanoseconds == other.nanoseconds
|
| 61 |
+
return False
|
| 62 |
+
|
| 63 |
+
def __ne__(self, other):
|
| 64 |
+
"""not-equals method (see :func:`__eq__()`)"""
|
| 65 |
+
return not self.__eq__(other)
|
| 66 |
+
|
| 67 |
+
def __hash__(self):
|
| 68 |
+
return hash((self.seconds, self.nanoseconds))
|
| 69 |
+
|
| 70 |
+
@staticmethod
|
| 71 |
+
def from_bytes(b):
|
| 72 |
+
"""Unpack bytes into a `Timestamp` object.
|
| 73 |
+
|
| 74 |
+
Used for pure-Python msgpack unpacking.
|
| 75 |
+
|
| 76 |
+
:param b: Payload from msgpack ext message with code -1
|
| 77 |
+
:type b: bytes
|
| 78 |
+
|
| 79 |
+
:returns: Timestamp object unpacked from msgpack ext payload
|
| 80 |
+
:rtype: Timestamp
|
| 81 |
+
"""
|
| 82 |
+
if len(b) == 4:
|
| 83 |
+
seconds = struct.unpack("!L", b)[0]
|
| 84 |
+
nanoseconds = 0
|
| 85 |
+
elif len(b) == 8:
|
| 86 |
+
data64 = struct.unpack("!Q", b)[0]
|
| 87 |
+
seconds = data64 & 0x00000003FFFFFFFF
|
| 88 |
+
nanoseconds = data64 >> 34
|
| 89 |
+
elif len(b) == 12:
|
| 90 |
+
nanoseconds, seconds = struct.unpack("!Iq", b)
|
| 91 |
+
else:
|
| 92 |
+
raise ValueError(
|
| 93 |
+
"Timestamp type can only be created from 32, 64, or 96-bit byte objects"
|
| 94 |
+
)
|
| 95 |
+
return Timestamp(seconds, nanoseconds)
|
| 96 |
+
|
| 97 |
+
def to_bytes(self):
|
| 98 |
+
"""Pack this Timestamp object into bytes.
|
| 99 |
+
|
| 100 |
+
Used for pure-Python msgpack packing.
|
| 101 |
+
|
| 102 |
+
:returns data: Payload for EXT message with code -1 (timestamp type)
|
| 103 |
+
:rtype: bytes
|
| 104 |
+
"""
|
| 105 |
+
if (self.seconds >> 34) == 0: # seconds is non-negative and fits in 34 bits
|
| 106 |
+
data64 = self.nanoseconds << 34 | self.seconds
|
| 107 |
+
if data64 & 0xFFFFFFFF00000000 == 0:
|
| 108 |
+
# nanoseconds is zero and seconds < 2**32, so timestamp 32
|
| 109 |
+
data = struct.pack("!L", data64)
|
| 110 |
+
else:
|
| 111 |
+
# timestamp 64
|
| 112 |
+
data = struct.pack("!Q", data64)
|
| 113 |
+
else:
|
| 114 |
+
# timestamp 96
|
| 115 |
+
data = struct.pack("!Iq", self.nanoseconds, self.seconds)
|
| 116 |
+
return data
|
| 117 |
+
|
| 118 |
+
@staticmethod
|
| 119 |
+
def from_unix(unix_sec):
|
| 120 |
+
"""Create a Timestamp from posix timestamp in seconds.
|
| 121 |
+
|
| 122 |
+
:param unix_float: Posix timestamp in seconds.
|
| 123 |
+
:type unix_float: int or float
|
| 124 |
+
"""
|
| 125 |
+
seconds = int(unix_sec // 1)
|
| 126 |
+
nanoseconds = int((unix_sec % 1) * 10**9)
|
| 127 |
+
return Timestamp(seconds, nanoseconds)
|
| 128 |
+
|
| 129 |
+
def to_unix(self):
|
| 130 |
+
"""Get the timestamp as a floating-point value.
|
| 131 |
+
|
| 132 |
+
:returns: posix timestamp
|
| 133 |
+
:rtype: float
|
| 134 |
+
"""
|
| 135 |
+
return self.seconds + self.nanoseconds / 1e9
|
| 136 |
+
|
| 137 |
+
@staticmethod
|
| 138 |
+
def from_unix_nano(unix_ns):
|
| 139 |
+
"""Create a Timestamp from posix timestamp in nanoseconds.
|
| 140 |
+
|
| 141 |
+
:param int unix_ns: Posix timestamp in nanoseconds.
|
| 142 |
+
:rtype: Timestamp
|
| 143 |
+
"""
|
| 144 |
+
return Timestamp(*divmod(unix_ns, 10**9))
|
| 145 |
+
|
| 146 |
+
def to_unix_nano(self):
|
| 147 |
+
"""Get the timestamp as a unixtime in nanoseconds.
|
| 148 |
+
|
| 149 |
+
:returns: posix timestamp in nanoseconds
|
| 150 |
+
:rtype: int
|
| 151 |
+
"""
|
| 152 |
+
return self.seconds * 10**9 + self.nanoseconds
|
| 153 |
+
|
| 154 |
+
def to_datetime(self):
|
| 155 |
+
"""Get the timestamp as a UTC datetime.
|
| 156 |
+
|
| 157 |
+
:rtype: `datetime.datetime`
|
| 158 |
+
"""
|
| 159 |
+
utc = datetime.timezone.utc
|
| 160 |
+
return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(
|
| 161 |
+
seconds=self.seconds, microseconds=self.nanoseconds // 1000
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
@staticmethod
|
| 165 |
+
def from_datetime(dt):
|
| 166 |
+
"""Create a Timestamp from datetime with tzinfo.
|
| 167 |
+
|
| 168 |
+
:rtype: Timestamp
|
| 169 |
+
"""
|
| 170 |
+
return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 1000)
|
vlmpy310/lib/python3.10/site-packages/msgpack/fallback.py
ADDED
|
@@ -0,0 +1,929 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Fallback pure Python implementation of msgpack"""
|
| 2 |
+
|
| 3 |
+
import struct
|
| 4 |
+
import sys
|
| 5 |
+
from datetime import datetime as _DateTime
|
| 6 |
+
|
| 7 |
+
if hasattr(sys, "pypy_version_info"):
|
| 8 |
+
from __pypy__ import newlist_hint
|
| 9 |
+
from __pypy__.builders import BytesBuilder
|
| 10 |
+
|
| 11 |
+
_USING_STRINGBUILDER = True
|
| 12 |
+
|
| 13 |
+
class BytesIO:
|
| 14 |
+
def __init__(self, s=b""):
|
| 15 |
+
if s:
|
| 16 |
+
self.builder = BytesBuilder(len(s))
|
| 17 |
+
self.builder.append(s)
|
| 18 |
+
else:
|
| 19 |
+
self.builder = BytesBuilder()
|
| 20 |
+
|
| 21 |
+
def write(self, s):
|
| 22 |
+
if isinstance(s, memoryview):
|
| 23 |
+
s = s.tobytes()
|
| 24 |
+
elif isinstance(s, bytearray):
|
| 25 |
+
s = bytes(s)
|
| 26 |
+
self.builder.append(s)
|
| 27 |
+
|
| 28 |
+
def getvalue(self):
|
| 29 |
+
return self.builder.build()
|
| 30 |
+
|
| 31 |
+
else:
|
| 32 |
+
from io import BytesIO
|
| 33 |
+
|
| 34 |
+
_USING_STRINGBUILDER = False
|
| 35 |
+
|
| 36 |
+
def newlist_hint(size):
|
| 37 |
+
return []
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
from .exceptions import BufferFull, ExtraData, FormatError, OutOfData, StackError
|
| 41 |
+
from .ext import ExtType, Timestamp
|
| 42 |
+
|
| 43 |
+
EX_SKIP = 0
|
| 44 |
+
EX_CONSTRUCT = 1
|
| 45 |
+
EX_READ_ARRAY_HEADER = 2
|
| 46 |
+
EX_READ_MAP_HEADER = 3
|
| 47 |
+
|
| 48 |
+
TYPE_IMMEDIATE = 0
|
| 49 |
+
TYPE_ARRAY = 1
|
| 50 |
+
TYPE_MAP = 2
|
| 51 |
+
TYPE_RAW = 3
|
| 52 |
+
TYPE_BIN = 4
|
| 53 |
+
TYPE_EXT = 5
|
| 54 |
+
|
| 55 |
+
DEFAULT_RECURSE_LIMIT = 511
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def _check_type_strict(obj, t, type=type, tuple=tuple):
|
| 59 |
+
if type(t) is tuple:
|
| 60 |
+
return type(obj) in t
|
| 61 |
+
else:
|
| 62 |
+
return type(obj) is t
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def _get_data_from_buffer(obj):
|
| 66 |
+
view = memoryview(obj)
|
| 67 |
+
if view.itemsize != 1:
|
| 68 |
+
raise ValueError("cannot unpack from multi-byte object")
|
| 69 |
+
return view
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def unpackb(packed, **kwargs):
|
| 73 |
+
"""
|
| 74 |
+
Unpack an object from `packed`.
|
| 75 |
+
|
| 76 |
+
Raises ``ExtraData`` when *packed* contains extra bytes.
|
| 77 |
+
Raises ``ValueError`` when *packed* is incomplete.
|
| 78 |
+
Raises ``FormatError`` when *packed* is not valid msgpack.
|
| 79 |
+
Raises ``StackError`` when *packed* contains too nested.
|
| 80 |
+
Other exceptions can be raised during unpacking.
|
| 81 |
+
|
| 82 |
+
See :class:`Unpacker` for options.
|
| 83 |
+
"""
|
| 84 |
+
unpacker = Unpacker(None, max_buffer_size=len(packed), **kwargs)
|
| 85 |
+
unpacker.feed(packed)
|
| 86 |
+
try:
|
| 87 |
+
ret = unpacker._unpack()
|
| 88 |
+
except OutOfData:
|
| 89 |
+
raise ValueError("Unpack failed: incomplete input")
|
| 90 |
+
except RecursionError:
|
| 91 |
+
raise StackError
|
| 92 |
+
if unpacker._got_extradata():
|
| 93 |
+
raise ExtraData(ret, unpacker._get_extradata())
|
| 94 |
+
return ret
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
_NO_FORMAT_USED = ""
|
| 98 |
+
_MSGPACK_HEADERS = {
|
| 99 |
+
0xC4: (1, _NO_FORMAT_USED, TYPE_BIN),
|
| 100 |
+
0xC5: (2, ">H", TYPE_BIN),
|
| 101 |
+
0xC6: (4, ">I", TYPE_BIN),
|
| 102 |
+
0xC7: (2, "Bb", TYPE_EXT),
|
| 103 |
+
0xC8: (3, ">Hb", TYPE_EXT),
|
| 104 |
+
0xC9: (5, ">Ib", TYPE_EXT),
|
| 105 |
+
0xCA: (4, ">f"),
|
| 106 |
+
0xCB: (8, ">d"),
|
| 107 |
+
0xCC: (1, _NO_FORMAT_USED),
|
| 108 |
+
0xCD: (2, ">H"),
|
| 109 |
+
0xCE: (4, ">I"),
|
| 110 |
+
0xCF: (8, ">Q"),
|
| 111 |
+
0xD0: (1, "b"),
|
| 112 |
+
0xD1: (2, ">h"),
|
| 113 |
+
0xD2: (4, ">i"),
|
| 114 |
+
0xD3: (8, ">q"),
|
| 115 |
+
0xD4: (1, "b1s", TYPE_EXT),
|
| 116 |
+
0xD5: (2, "b2s", TYPE_EXT),
|
| 117 |
+
0xD6: (4, "b4s", TYPE_EXT),
|
| 118 |
+
0xD7: (8, "b8s", TYPE_EXT),
|
| 119 |
+
0xD8: (16, "b16s", TYPE_EXT),
|
| 120 |
+
0xD9: (1, _NO_FORMAT_USED, TYPE_RAW),
|
| 121 |
+
0xDA: (2, ">H", TYPE_RAW),
|
| 122 |
+
0xDB: (4, ">I", TYPE_RAW),
|
| 123 |
+
0xDC: (2, ">H", TYPE_ARRAY),
|
| 124 |
+
0xDD: (4, ">I", TYPE_ARRAY),
|
| 125 |
+
0xDE: (2, ">H", TYPE_MAP),
|
| 126 |
+
0xDF: (4, ">I", TYPE_MAP),
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
class Unpacker:
|
| 131 |
+
"""Streaming unpacker.
|
| 132 |
+
|
| 133 |
+
Arguments:
|
| 134 |
+
|
| 135 |
+
:param file_like:
|
| 136 |
+
File-like object having `.read(n)` method.
|
| 137 |
+
If specified, unpacker reads serialized data from it and `.feed()` is not usable.
|
| 138 |
+
|
| 139 |
+
:param int read_size:
|
| 140 |
+
Used as `file_like.read(read_size)`. (default: `min(16*1024, max_buffer_size)`)
|
| 141 |
+
|
| 142 |
+
:param bool use_list:
|
| 143 |
+
If true, unpack msgpack array to Python list.
|
| 144 |
+
Otherwise, unpack to Python tuple. (default: True)
|
| 145 |
+
|
| 146 |
+
:param bool raw:
|
| 147 |
+
If true, unpack msgpack raw to Python bytes.
|
| 148 |
+
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).
|
| 149 |
+
|
| 150 |
+
:param int timestamp:
|
| 151 |
+
Control how timestamp type is unpacked:
|
| 152 |
+
|
| 153 |
+
0 - Timestamp
|
| 154 |
+
1 - float (Seconds from the EPOCH)
|
| 155 |
+
2 - int (Nanoseconds from the EPOCH)
|
| 156 |
+
3 - datetime.datetime (UTC).
|
| 157 |
+
|
| 158 |
+
:param bool strict_map_key:
|
| 159 |
+
If true (default), only str or bytes are accepted for map (dict) keys.
|
| 160 |
+
|
| 161 |
+
:param object_hook:
|
| 162 |
+
When specified, it should be callable.
|
| 163 |
+
Unpacker calls it with a dict argument after unpacking msgpack map.
|
| 164 |
+
(See also simplejson)
|
| 165 |
+
|
| 166 |
+
:param object_pairs_hook:
|
| 167 |
+
When specified, it should be callable.
|
| 168 |
+
Unpacker calls it with a list of key-value pairs after unpacking msgpack map.
|
| 169 |
+
(See also simplejson)
|
| 170 |
+
|
| 171 |
+
:param str unicode_errors:
|
| 172 |
+
The error handler for decoding unicode. (default: 'strict')
|
| 173 |
+
This option should be used only when you have msgpack data which
|
| 174 |
+
contains invalid UTF-8 string.
|
| 175 |
+
|
| 176 |
+
:param int max_buffer_size:
|
| 177 |
+
Limits size of data waiting unpacked. 0 means 2**32-1.
|
| 178 |
+
The default value is 100*1024*1024 (100MiB).
|
| 179 |
+
Raises `BufferFull` exception when it is insufficient.
|
| 180 |
+
You should set this parameter when unpacking data from untrusted source.
|
| 181 |
+
|
| 182 |
+
:param int max_str_len:
|
| 183 |
+
Deprecated, use *max_buffer_size* instead.
|
| 184 |
+
Limits max length of str. (default: max_buffer_size)
|
| 185 |
+
|
| 186 |
+
:param int max_bin_len:
|
| 187 |
+
Deprecated, use *max_buffer_size* instead.
|
| 188 |
+
Limits max length of bin. (default: max_buffer_size)
|
| 189 |
+
|
| 190 |
+
:param int max_array_len:
|
| 191 |
+
Limits max length of array.
|
| 192 |
+
(default: max_buffer_size)
|
| 193 |
+
|
| 194 |
+
:param int max_map_len:
|
| 195 |
+
Limits max length of map.
|
| 196 |
+
(default: max_buffer_size//2)
|
| 197 |
+
|
| 198 |
+
:param int max_ext_len:
|
| 199 |
+
Deprecated, use *max_buffer_size* instead.
|
| 200 |
+
Limits max size of ext type. (default: max_buffer_size)
|
| 201 |
+
|
| 202 |
+
Example of streaming deserialize from file-like object::
|
| 203 |
+
|
| 204 |
+
unpacker = Unpacker(file_like)
|
| 205 |
+
for o in unpacker:
|
| 206 |
+
process(o)
|
| 207 |
+
|
| 208 |
+
Example of streaming deserialize from socket::
|
| 209 |
+
|
| 210 |
+
unpacker = Unpacker()
|
| 211 |
+
while True:
|
| 212 |
+
buf = sock.recv(1024**2)
|
| 213 |
+
if not buf:
|
| 214 |
+
break
|
| 215 |
+
unpacker.feed(buf)
|
| 216 |
+
for o in unpacker:
|
| 217 |
+
process(o)
|
| 218 |
+
|
| 219 |
+
Raises ``ExtraData`` when *packed* contains extra bytes.
|
| 220 |
+
Raises ``OutOfData`` when *packed* is incomplete.
|
| 221 |
+
Raises ``FormatError`` when *packed* is not valid msgpack.
|
| 222 |
+
Raises ``StackError`` when *packed* contains too nested.
|
| 223 |
+
Other exceptions can be raised during unpacking.
|
| 224 |
+
"""
|
| 225 |
+
|
| 226 |
+
def __init__(
|
| 227 |
+
self,
|
| 228 |
+
file_like=None,
|
| 229 |
+
*,
|
| 230 |
+
read_size=0,
|
| 231 |
+
use_list=True,
|
| 232 |
+
raw=False,
|
| 233 |
+
timestamp=0,
|
| 234 |
+
strict_map_key=True,
|
| 235 |
+
object_hook=None,
|
| 236 |
+
object_pairs_hook=None,
|
| 237 |
+
list_hook=None,
|
| 238 |
+
unicode_errors=None,
|
| 239 |
+
max_buffer_size=100 * 1024 * 1024,
|
| 240 |
+
ext_hook=ExtType,
|
| 241 |
+
max_str_len=-1,
|
| 242 |
+
max_bin_len=-1,
|
| 243 |
+
max_array_len=-1,
|
| 244 |
+
max_map_len=-1,
|
| 245 |
+
max_ext_len=-1,
|
| 246 |
+
):
|
| 247 |
+
if unicode_errors is None:
|
| 248 |
+
unicode_errors = "strict"
|
| 249 |
+
|
| 250 |
+
if file_like is None:
|
| 251 |
+
self._feeding = True
|
| 252 |
+
else:
|
| 253 |
+
if not callable(file_like.read):
|
| 254 |
+
raise TypeError("`file_like.read` must be callable")
|
| 255 |
+
self.file_like = file_like
|
| 256 |
+
self._feeding = False
|
| 257 |
+
|
| 258 |
+
#: array of bytes fed.
|
| 259 |
+
self._buffer = bytearray()
|
| 260 |
+
#: Which position we currently reads
|
| 261 |
+
self._buff_i = 0
|
| 262 |
+
|
| 263 |
+
# When Unpacker is used as an iterable, between the calls to next(),
|
| 264 |
+
# the buffer is not "consumed" completely, for efficiency sake.
|
| 265 |
+
# Instead, it is done sloppily. To make sure we raise BufferFull at
|
| 266 |
+
# the correct moments, we have to keep track of how sloppy we were.
|
| 267 |
+
# Furthermore, when the buffer is incomplete (that is: in the case
|
| 268 |
+
# we raise an OutOfData) we need to rollback the buffer to the correct
|
| 269 |
+
# state, which _buf_checkpoint records.
|
| 270 |
+
self._buf_checkpoint = 0
|
| 271 |
+
|
| 272 |
+
if not max_buffer_size:
|
| 273 |
+
max_buffer_size = 2**31 - 1
|
| 274 |
+
if max_str_len == -1:
|
| 275 |
+
max_str_len = max_buffer_size
|
| 276 |
+
if max_bin_len == -1:
|
| 277 |
+
max_bin_len = max_buffer_size
|
| 278 |
+
if max_array_len == -1:
|
| 279 |
+
max_array_len = max_buffer_size
|
| 280 |
+
if max_map_len == -1:
|
| 281 |
+
max_map_len = max_buffer_size // 2
|
| 282 |
+
if max_ext_len == -1:
|
| 283 |
+
max_ext_len = max_buffer_size
|
| 284 |
+
|
| 285 |
+
self._max_buffer_size = max_buffer_size
|
| 286 |
+
if read_size > self._max_buffer_size:
|
| 287 |
+
raise ValueError("read_size must be smaller than max_buffer_size")
|
| 288 |
+
self._read_size = read_size or min(self._max_buffer_size, 16 * 1024)
|
| 289 |
+
self._raw = bool(raw)
|
| 290 |
+
self._strict_map_key = bool(strict_map_key)
|
| 291 |
+
self._unicode_errors = unicode_errors
|
| 292 |
+
self._use_list = use_list
|
| 293 |
+
if not (0 <= timestamp <= 3):
|
| 294 |
+
raise ValueError("timestamp must be 0..3")
|
| 295 |
+
self._timestamp = timestamp
|
| 296 |
+
self._list_hook = list_hook
|
| 297 |
+
self._object_hook = object_hook
|
| 298 |
+
self._object_pairs_hook = object_pairs_hook
|
| 299 |
+
self._ext_hook = ext_hook
|
| 300 |
+
self._max_str_len = max_str_len
|
| 301 |
+
self._max_bin_len = max_bin_len
|
| 302 |
+
self._max_array_len = max_array_len
|
| 303 |
+
self._max_map_len = max_map_len
|
| 304 |
+
self._max_ext_len = max_ext_len
|
| 305 |
+
self._stream_offset = 0
|
| 306 |
+
|
| 307 |
+
if list_hook is not None and not callable(list_hook):
|
| 308 |
+
raise TypeError("`list_hook` is not callable")
|
| 309 |
+
if object_hook is not None and not callable(object_hook):
|
| 310 |
+
raise TypeError("`object_hook` is not callable")
|
| 311 |
+
if object_pairs_hook is not None and not callable(object_pairs_hook):
|
| 312 |
+
raise TypeError("`object_pairs_hook` is not callable")
|
| 313 |
+
if object_hook is not None and object_pairs_hook is not None:
|
| 314 |
+
raise TypeError("object_pairs_hook and object_hook are mutually exclusive")
|
| 315 |
+
if not callable(ext_hook):
|
| 316 |
+
raise TypeError("`ext_hook` is not callable")
|
| 317 |
+
|
| 318 |
+
def feed(self, next_bytes):
|
| 319 |
+
assert self._feeding
|
| 320 |
+
view = _get_data_from_buffer(next_bytes)
|
| 321 |
+
if len(self._buffer) - self._buff_i + len(view) > self._max_buffer_size:
|
| 322 |
+
raise BufferFull
|
| 323 |
+
|
| 324 |
+
# Strip buffer before checkpoint before reading file.
|
| 325 |
+
if self._buf_checkpoint > 0:
|
| 326 |
+
del self._buffer[: self._buf_checkpoint]
|
| 327 |
+
self._buff_i -= self._buf_checkpoint
|
| 328 |
+
self._buf_checkpoint = 0
|
| 329 |
+
|
| 330 |
+
# Use extend here: INPLACE_ADD += doesn't reliably typecast memoryview in jython
|
| 331 |
+
self._buffer.extend(view)
|
| 332 |
+
view.release()
|
| 333 |
+
|
| 334 |
+
def _consume(self):
|
| 335 |
+
"""Gets rid of the used parts of the buffer."""
|
| 336 |
+
self._stream_offset += self._buff_i - self._buf_checkpoint
|
| 337 |
+
self._buf_checkpoint = self._buff_i
|
| 338 |
+
|
| 339 |
+
def _got_extradata(self):
|
| 340 |
+
return self._buff_i < len(self._buffer)
|
| 341 |
+
|
| 342 |
+
def _get_extradata(self):
|
| 343 |
+
return self._buffer[self._buff_i :]
|
| 344 |
+
|
| 345 |
+
def read_bytes(self, n):
|
| 346 |
+
ret = self._read(n, raise_outofdata=False)
|
| 347 |
+
self._consume()
|
| 348 |
+
return ret
|
| 349 |
+
|
| 350 |
+
def _read(self, n, raise_outofdata=True):
|
| 351 |
+
# (int) -> bytearray
|
| 352 |
+
self._reserve(n, raise_outofdata=raise_outofdata)
|
| 353 |
+
i = self._buff_i
|
| 354 |
+
ret = self._buffer[i : i + n]
|
| 355 |
+
self._buff_i = i + len(ret)
|
| 356 |
+
return ret
|
| 357 |
+
|
| 358 |
+
def _reserve(self, n, raise_outofdata=True):
|
| 359 |
+
remain_bytes = len(self._buffer) - self._buff_i - n
|
| 360 |
+
|
| 361 |
+
# Fast path: buffer has n bytes already
|
| 362 |
+
if remain_bytes >= 0:
|
| 363 |
+
return
|
| 364 |
+
|
| 365 |
+
if self._feeding:
|
| 366 |
+
self._buff_i = self._buf_checkpoint
|
| 367 |
+
raise OutOfData
|
| 368 |
+
|
| 369 |
+
# Strip buffer before checkpoint before reading file.
|
| 370 |
+
if self._buf_checkpoint > 0:
|
| 371 |
+
del self._buffer[: self._buf_checkpoint]
|
| 372 |
+
self._buff_i -= self._buf_checkpoint
|
| 373 |
+
self._buf_checkpoint = 0
|
| 374 |
+
|
| 375 |
+
# Read from file
|
| 376 |
+
remain_bytes = -remain_bytes
|
| 377 |
+
if remain_bytes + len(self._buffer) > self._max_buffer_size:
|
| 378 |
+
raise BufferFull
|
| 379 |
+
while remain_bytes > 0:
|
| 380 |
+
to_read_bytes = max(self._read_size, remain_bytes)
|
| 381 |
+
read_data = self.file_like.read(to_read_bytes)
|
| 382 |
+
if not read_data:
|
| 383 |
+
break
|
| 384 |
+
assert isinstance(read_data, bytes)
|
| 385 |
+
self._buffer += read_data
|
| 386 |
+
remain_bytes -= len(read_data)
|
| 387 |
+
|
| 388 |
+
if len(self._buffer) < n + self._buff_i and raise_outofdata:
|
| 389 |
+
self._buff_i = 0 # rollback
|
| 390 |
+
raise OutOfData
|
| 391 |
+
|
| 392 |
+
def _read_header(self):
|
| 393 |
+
typ = TYPE_IMMEDIATE
|
| 394 |
+
n = 0
|
| 395 |
+
obj = None
|
| 396 |
+
self._reserve(1)
|
| 397 |
+
b = self._buffer[self._buff_i]
|
| 398 |
+
self._buff_i += 1
|
| 399 |
+
if b & 0b10000000 == 0:
|
| 400 |
+
obj = b
|
| 401 |
+
elif b & 0b11100000 == 0b11100000:
|
| 402 |
+
obj = -1 - (b ^ 0xFF)
|
| 403 |
+
elif b & 0b11100000 == 0b10100000:
|
| 404 |
+
n = b & 0b00011111
|
| 405 |
+
typ = TYPE_RAW
|
| 406 |
+
if n > self._max_str_len:
|
| 407 |
+
raise ValueError(f"{n} exceeds max_str_len({self._max_str_len})")
|
| 408 |
+
obj = self._read(n)
|
| 409 |
+
elif b & 0b11110000 == 0b10010000:
|
| 410 |
+
n = b & 0b00001111
|
| 411 |
+
typ = TYPE_ARRAY
|
| 412 |
+
if n > self._max_array_len:
|
| 413 |
+
raise ValueError(f"{n} exceeds max_array_len({self._max_array_len})")
|
| 414 |
+
elif b & 0b11110000 == 0b10000000:
|
| 415 |
+
n = b & 0b00001111
|
| 416 |
+
typ = TYPE_MAP
|
| 417 |
+
if n > self._max_map_len:
|
| 418 |
+
raise ValueError(f"{n} exceeds max_map_len({self._max_map_len})")
|
| 419 |
+
elif b == 0xC0:
|
| 420 |
+
obj = None
|
| 421 |
+
elif b == 0xC2:
|
| 422 |
+
obj = False
|
| 423 |
+
elif b == 0xC3:
|
| 424 |
+
obj = True
|
| 425 |
+
elif 0xC4 <= b <= 0xC6:
|
| 426 |
+
size, fmt, typ = _MSGPACK_HEADERS[b]
|
| 427 |
+
self._reserve(size)
|
| 428 |
+
if len(fmt) > 0:
|
| 429 |
+
n = struct.unpack_from(fmt, self._buffer, self._buff_i)[0]
|
| 430 |
+
else:
|
| 431 |
+
n = self._buffer[self._buff_i]
|
| 432 |
+
self._buff_i += size
|
| 433 |
+
if n > self._max_bin_len:
|
| 434 |
+
raise ValueError(f"{n} exceeds max_bin_len({self._max_bin_len})")
|
| 435 |
+
obj = self._read(n)
|
| 436 |
+
elif 0xC7 <= b <= 0xC9:
|
| 437 |
+
size, fmt, typ = _MSGPACK_HEADERS[b]
|
| 438 |
+
self._reserve(size)
|
| 439 |
+
L, n = struct.unpack_from(fmt, self._buffer, self._buff_i)
|
| 440 |
+
self._buff_i += size
|
| 441 |
+
if L > self._max_ext_len:
|
| 442 |
+
raise ValueError(f"{L} exceeds max_ext_len({self._max_ext_len})")
|
| 443 |
+
obj = self._read(L)
|
| 444 |
+
elif 0xCA <= b <= 0xD3:
|
| 445 |
+
size, fmt = _MSGPACK_HEADERS[b]
|
| 446 |
+
self._reserve(size)
|
| 447 |
+
if len(fmt) > 0:
|
| 448 |
+
obj = struct.unpack_from(fmt, self._buffer, self._buff_i)[0]
|
| 449 |
+
else:
|
| 450 |
+
obj = self._buffer[self._buff_i]
|
| 451 |
+
self._buff_i += size
|
| 452 |
+
elif 0xD4 <= b <= 0xD8:
|
| 453 |
+
size, fmt, typ = _MSGPACK_HEADERS[b]
|
| 454 |
+
if self._max_ext_len < size:
|
| 455 |
+
raise ValueError(f"{size} exceeds max_ext_len({self._max_ext_len})")
|
| 456 |
+
self._reserve(size + 1)
|
| 457 |
+
n, obj = struct.unpack_from(fmt, self._buffer, self._buff_i)
|
| 458 |
+
self._buff_i += size + 1
|
| 459 |
+
elif 0xD9 <= b <= 0xDB:
|
| 460 |
+
size, fmt, typ = _MSGPACK_HEADERS[b]
|
| 461 |
+
self._reserve(size)
|
| 462 |
+
if len(fmt) > 0:
|
| 463 |
+
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
|
| 464 |
+
else:
|
| 465 |
+
n = self._buffer[self._buff_i]
|
| 466 |
+
self._buff_i += size
|
| 467 |
+
if n > self._max_str_len:
|
| 468 |
+
raise ValueError(f"{n} exceeds max_str_len({self._max_str_len})")
|
| 469 |
+
obj = self._read(n)
|
| 470 |
+
elif 0xDC <= b <= 0xDD:
|
| 471 |
+
size, fmt, typ = _MSGPACK_HEADERS[b]
|
| 472 |
+
self._reserve(size)
|
| 473 |
+
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
|
| 474 |
+
self._buff_i += size
|
| 475 |
+
if n > self._max_array_len:
|
| 476 |
+
raise ValueError(f"{n} exceeds max_array_len({self._max_array_len})")
|
| 477 |
+
elif 0xDE <= b <= 0xDF:
|
| 478 |
+
size, fmt, typ = _MSGPACK_HEADERS[b]
|
| 479 |
+
self._reserve(size)
|
| 480 |
+
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
|
| 481 |
+
self._buff_i += size
|
| 482 |
+
if n > self._max_map_len:
|
| 483 |
+
raise ValueError(f"{n} exceeds max_map_len({self._max_map_len})")
|
| 484 |
+
else:
|
| 485 |
+
raise FormatError("Unknown header: 0x%x" % b)
|
| 486 |
+
return typ, n, obj
|
| 487 |
+
|
| 488 |
+
def _unpack(self, execute=EX_CONSTRUCT):
|
| 489 |
+
typ, n, obj = self._read_header()
|
| 490 |
+
|
| 491 |
+
if execute == EX_READ_ARRAY_HEADER:
|
| 492 |
+
if typ != TYPE_ARRAY:
|
| 493 |
+
raise ValueError("Expected array")
|
| 494 |
+
return n
|
| 495 |
+
if execute == EX_READ_MAP_HEADER:
|
| 496 |
+
if typ != TYPE_MAP:
|
| 497 |
+
raise ValueError("Expected map")
|
| 498 |
+
return n
|
| 499 |
+
# TODO should we eliminate the recursion?
|
| 500 |
+
if typ == TYPE_ARRAY:
|
| 501 |
+
if execute == EX_SKIP:
|
| 502 |
+
for i in range(n):
|
| 503 |
+
# TODO check whether we need to call `list_hook`
|
| 504 |
+
self._unpack(EX_SKIP)
|
| 505 |
+
return
|
| 506 |
+
ret = newlist_hint(n)
|
| 507 |
+
for i in range(n):
|
| 508 |
+
ret.append(self._unpack(EX_CONSTRUCT))
|
| 509 |
+
if self._list_hook is not None:
|
| 510 |
+
ret = self._list_hook(ret)
|
| 511 |
+
# TODO is the interaction between `list_hook` and `use_list` ok?
|
| 512 |
+
return ret if self._use_list else tuple(ret)
|
| 513 |
+
if typ == TYPE_MAP:
|
| 514 |
+
if execute == EX_SKIP:
|
| 515 |
+
for i in range(n):
|
| 516 |
+
# TODO check whether we need to call hooks
|
| 517 |
+
self._unpack(EX_SKIP)
|
| 518 |
+
self._unpack(EX_SKIP)
|
| 519 |
+
return
|
| 520 |
+
if self._object_pairs_hook is not None:
|
| 521 |
+
ret = self._object_pairs_hook(
|
| 522 |
+
(self._unpack(EX_CONSTRUCT), self._unpack(EX_CONSTRUCT)) for _ in range(n)
|
| 523 |
+
)
|
| 524 |
+
else:
|
| 525 |
+
ret = {}
|
| 526 |
+
for _ in range(n):
|
| 527 |
+
key = self._unpack(EX_CONSTRUCT)
|
| 528 |
+
if self._strict_map_key and type(key) not in (str, bytes):
|
| 529 |
+
raise ValueError("%s is not allowed for map key" % str(type(key)))
|
| 530 |
+
if isinstance(key, str):
|
| 531 |
+
key = sys.intern(key)
|
| 532 |
+
ret[key] = self._unpack(EX_CONSTRUCT)
|
| 533 |
+
if self._object_hook is not None:
|
| 534 |
+
ret = self._object_hook(ret)
|
| 535 |
+
return ret
|
| 536 |
+
if execute == EX_SKIP:
|
| 537 |
+
return
|
| 538 |
+
if typ == TYPE_RAW:
|
| 539 |
+
if self._raw:
|
| 540 |
+
obj = bytes(obj)
|
| 541 |
+
else:
|
| 542 |
+
obj = obj.decode("utf_8", self._unicode_errors)
|
| 543 |
+
return obj
|
| 544 |
+
if typ == TYPE_BIN:
|
| 545 |
+
return bytes(obj)
|
| 546 |
+
if typ == TYPE_EXT:
|
| 547 |
+
if n == -1: # timestamp
|
| 548 |
+
ts = Timestamp.from_bytes(bytes(obj))
|
| 549 |
+
if self._timestamp == 1:
|
| 550 |
+
return ts.to_unix()
|
| 551 |
+
elif self._timestamp == 2:
|
| 552 |
+
return ts.to_unix_nano()
|
| 553 |
+
elif self._timestamp == 3:
|
| 554 |
+
return ts.to_datetime()
|
| 555 |
+
else:
|
| 556 |
+
return ts
|
| 557 |
+
else:
|
| 558 |
+
return self._ext_hook(n, bytes(obj))
|
| 559 |
+
assert typ == TYPE_IMMEDIATE
|
| 560 |
+
return obj
|
| 561 |
+
|
| 562 |
+
def __iter__(self):
|
| 563 |
+
return self
|
| 564 |
+
|
| 565 |
+
def __next__(self):
|
| 566 |
+
try:
|
| 567 |
+
ret = self._unpack(EX_CONSTRUCT)
|
| 568 |
+
self._consume()
|
| 569 |
+
return ret
|
| 570 |
+
except OutOfData:
|
| 571 |
+
self._consume()
|
| 572 |
+
raise StopIteration
|
| 573 |
+
except RecursionError:
|
| 574 |
+
raise StackError
|
| 575 |
+
|
| 576 |
+
next = __next__
|
| 577 |
+
|
| 578 |
+
def skip(self):
|
| 579 |
+
self._unpack(EX_SKIP)
|
| 580 |
+
self._consume()
|
| 581 |
+
|
| 582 |
+
def unpack(self):
|
| 583 |
+
try:
|
| 584 |
+
ret = self._unpack(EX_CONSTRUCT)
|
| 585 |
+
except RecursionError:
|
| 586 |
+
raise StackError
|
| 587 |
+
self._consume()
|
| 588 |
+
return ret
|
| 589 |
+
|
| 590 |
+
def read_array_header(self):
|
| 591 |
+
ret = self._unpack(EX_READ_ARRAY_HEADER)
|
| 592 |
+
self._consume()
|
| 593 |
+
return ret
|
| 594 |
+
|
| 595 |
+
def read_map_header(self):
|
| 596 |
+
ret = self._unpack(EX_READ_MAP_HEADER)
|
| 597 |
+
self._consume()
|
| 598 |
+
return ret
|
| 599 |
+
|
| 600 |
+
def tell(self):
|
| 601 |
+
return self._stream_offset
|
| 602 |
+
|
| 603 |
+
|
| 604 |
+
class Packer:
|
| 605 |
+
"""
|
| 606 |
+
MessagePack Packer
|
| 607 |
+
|
| 608 |
+
Usage::
|
| 609 |
+
|
| 610 |
+
packer = Packer()
|
| 611 |
+
astream.write(packer.pack(a))
|
| 612 |
+
astream.write(packer.pack(b))
|
| 613 |
+
|
| 614 |
+
Packer's constructor has some keyword arguments:
|
| 615 |
+
|
| 616 |
+
:param default:
|
| 617 |
+
When specified, it should be callable.
|
| 618 |
+
Convert user type to builtin type that Packer supports.
|
| 619 |
+
See also simplejson's document.
|
| 620 |
+
|
| 621 |
+
:param bool use_single_float:
|
| 622 |
+
Use single precision float type for float. (default: False)
|
| 623 |
+
|
| 624 |
+
:param bool autoreset:
|
| 625 |
+
Reset buffer after each pack and return its content as `bytes`. (default: True).
|
| 626 |
+
If set this to false, use `bytes()` to get content and `.reset()` to clear buffer.
|
| 627 |
+
|
| 628 |
+
:param bool use_bin_type:
|
| 629 |
+
Use bin type introduced in msgpack spec 2.0 for bytes.
|
| 630 |
+
It also enables str8 type for unicode. (default: True)
|
| 631 |
+
|
| 632 |
+
:param bool strict_types:
|
| 633 |
+
If set to true, types will be checked to be exact. Derived classes
|
| 634 |
+
from serializable types will not be serialized and will be
|
| 635 |
+
treated as unsupported type and forwarded to default.
|
| 636 |
+
Additionally tuples will not be serialized as lists.
|
| 637 |
+
This is useful when trying to implement accurate serialization
|
| 638 |
+
for python types.
|
| 639 |
+
|
| 640 |
+
:param bool datetime:
|
| 641 |
+
If set to true, datetime with tzinfo is packed into Timestamp type.
|
| 642 |
+
Note that the tzinfo is stripped in the timestamp.
|
| 643 |
+
You can get UTC datetime with `timestamp=3` option of the Unpacker.
|
| 644 |
+
|
| 645 |
+
:param str unicode_errors:
|
| 646 |
+
The error handler for encoding unicode. (default: 'strict')
|
| 647 |
+
DO NOT USE THIS!! This option is kept for very specific usage.
|
| 648 |
+
|
| 649 |
+
:param int buf_size:
|
| 650 |
+
Internal buffer size. This option is used only for C implementation.
|
| 651 |
+
"""
|
| 652 |
+
|
| 653 |
+
def __init__(
|
| 654 |
+
self,
|
| 655 |
+
*,
|
| 656 |
+
default=None,
|
| 657 |
+
use_single_float=False,
|
| 658 |
+
autoreset=True,
|
| 659 |
+
use_bin_type=True,
|
| 660 |
+
strict_types=False,
|
| 661 |
+
datetime=False,
|
| 662 |
+
unicode_errors=None,
|
| 663 |
+
buf_size=None,
|
| 664 |
+
):
|
| 665 |
+
self._strict_types = strict_types
|
| 666 |
+
self._use_float = use_single_float
|
| 667 |
+
self._autoreset = autoreset
|
| 668 |
+
self._use_bin_type = use_bin_type
|
| 669 |
+
self._buffer = BytesIO()
|
| 670 |
+
self._datetime = bool(datetime)
|
| 671 |
+
self._unicode_errors = unicode_errors or "strict"
|
| 672 |
+
if default is not None and not callable(default):
|
| 673 |
+
raise TypeError("default must be callable")
|
| 674 |
+
self._default = default
|
| 675 |
+
|
| 676 |
+
def _pack(
|
| 677 |
+
self,
|
| 678 |
+
obj,
|
| 679 |
+
nest_limit=DEFAULT_RECURSE_LIMIT,
|
| 680 |
+
check=isinstance,
|
| 681 |
+
check_type_strict=_check_type_strict,
|
| 682 |
+
):
|
| 683 |
+
default_used = False
|
| 684 |
+
if self._strict_types:
|
| 685 |
+
check = check_type_strict
|
| 686 |
+
list_types = list
|
| 687 |
+
else:
|
| 688 |
+
list_types = (list, tuple)
|
| 689 |
+
while True:
|
| 690 |
+
if nest_limit < 0:
|
| 691 |
+
raise ValueError("recursion limit exceeded")
|
| 692 |
+
if obj is None:
|
| 693 |
+
return self._buffer.write(b"\xc0")
|
| 694 |
+
if check(obj, bool):
|
| 695 |
+
if obj:
|
| 696 |
+
return self._buffer.write(b"\xc3")
|
| 697 |
+
return self._buffer.write(b"\xc2")
|
| 698 |
+
if check(obj, int):
|
| 699 |
+
if 0 <= obj < 0x80:
|
| 700 |
+
return self._buffer.write(struct.pack("B", obj))
|
| 701 |
+
if -0x20 <= obj < 0:
|
| 702 |
+
return self._buffer.write(struct.pack("b", obj))
|
| 703 |
+
if 0x80 <= obj <= 0xFF:
|
| 704 |
+
return self._buffer.write(struct.pack("BB", 0xCC, obj))
|
| 705 |
+
if -0x80 <= obj < 0:
|
| 706 |
+
return self._buffer.write(struct.pack(">Bb", 0xD0, obj))
|
| 707 |
+
if 0xFF < obj <= 0xFFFF:
|
| 708 |
+
return self._buffer.write(struct.pack(">BH", 0xCD, obj))
|
| 709 |
+
if -0x8000 <= obj < -0x80:
|
| 710 |
+
return self._buffer.write(struct.pack(">Bh", 0xD1, obj))
|
| 711 |
+
if 0xFFFF < obj <= 0xFFFFFFFF:
|
| 712 |
+
return self._buffer.write(struct.pack(">BI", 0xCE, obj))
|
| 713 |
+
if -0x80000000 <= obj < -0x8000:
|
| 714 |
+
return self._buffer.write(struct.pack(">Bi", 0xD2, obj))
|
| 715 |
+
if 0xFFFFFFFF < obj <= 0xFFFFFFFFFFFFFFFF:
|
| 716 |
+
return self._buffer.write(struct.pack(">BQ", 0xCF, obj))
|
| 717 |
+
if -0x8000000000000000 <= obj < -0x80000000:
|
| 718 |
+
return self._buffer.write(struct.pack(">Bq", 0xD3, obj))
|
| 719 |
+
if not default_used and self._default is not None:
|
| 720 |
+
obj = self._default(obj)
|
| 721 |
+
default_used = True
|
| 722 |
+
continue
|
| 723 |
+
raise OverflowError("Integer value out of range")
|
| 724 |
+
if check(obj, (bytes, bytearray)):
|
| 725 |
+
n = len(obj)
|
| 726 |
+
if n >= 2**32:
|
| 727 |
+
raise ValueError("%s is too large" % type(obj).__name__)
|
| 728 |
+
self._pack_bin_header(n)
|
| 729 |
+
return self._buffer.write(obj)
|
| 730 |
+
if check(obj, str):
|
| 731 |
+
obj = obj.encode("utf-8", self._unicode_errors)
|
| 732 |
+
n = len(obj)
|
| 733 |
+
if n >= 2**32:
|
| 734 |
+
raise ValueError("String is too large")
|
| 735 |
+
self._pack_raw_header(n)
|
| 736 |
+
return self._buffer.write(obj)
|
| 737 |
+
if check(obj, memoryview):
|
| 738 |
+
n = obj.nbytes
|
| 739 |
+
if n >= 2**32:
|
| 740 |
+
raise ValueError("Memoryview is too large")
|
| 741 |
+
self._pack_bin_header(n)
|
| 742 |
+
return self._buffer.write(obj)
|
| 743 |
+
if check(obj, float):
|
| 744 |
+
if self._use_float:
|
| 745 |
+
return self._buffer.write(struct.pack(">Bf", 0xCA, obj))
|
| 746 |
+
return self._buffer.write(struct.pack(">Bd", 0xCB, obj))
|
| 747 |
+
if check(obj, (ExtType, Timestamp)):
|
| 748 |
+
if check(obj, Timestamp):
|
| 749 |
+
code = -1
|
| 750 |
+
data = obj.to_bytes()
|
| 751 |
+
else:
|
| 752 |
+
code = obj.code
|
| 753 |
+
data = obj.data
|
| 754 |
+
assert isinstance(code, int)
|
| 755 |
+
assert isinstance(data, bytes)
|
| 756 |
+
L = len(data)
|
| 757 |
+
if L == 1:
|
| 758 |
+
self._buffer.write(b"\xd4")
|
| 759 |
+
elif L == 2:
|
| 760 |
+
self._buffer.write(b"\xd5")
|
| 761 |
+
elif L == 4:
|
| 762 |
+
self._buffer.write(b"\xd6")
|
| 763 |
+
elif L == 8:
|
| 764 |
+
self._buffer.write(b"\xd7")
|
| 765 |
+
elif L == 16:
|
| 766 |
+
self._buffer.write(b"\xd8")
|
| 767 |
+
elif L <= 0xFF:
|
| 768 |
+
self._buffer.write(struct.pack(">BB", 0xC7, L))
|
| 769 |
+
elif L <= 0xFFFF:
|
| 770 |
+
self._buffer.write(struct.pack(">BH", 0xC8, L))
|
| 771 |
+
else:
|
| 772 |
+
self._buffer.write(struct.pack(">BI", 0xC9, L))
|
| 773 |
+
self._buffer.write(struct.pack("b", code))
|
| 774 |
+
self._buffer.write(data)
|
| 775 |
+
return
|
| 776 |
+
if check(obj, list_types):
|
| 777 |
+
n = len(obj)
|
| 778 |
+
self._pack_array_header(n)
|
| 779 |
+
for i in range(n):
|
| 780 |
+
self._pack(obj[i], nest_limit - 1)
|
| 781 |
+
return
|
| 782 |
+
if check(obj, dict):
|
| 783 |
+
return self._pack_map_pairs(len(obj), obj.items(), nest_limit - 1)
|
| 784 |
+
|
| 785 |
+
if self._datetime and check(obj, _DateTime) and obj.tzinfo is not None:
|
| 786 |
+
obj = Timestamp.from_datetime(obj)
|
| 787 |
+
default_used = 1
|
| 788 |
+
continue
|
| 789 |
+
|
| 790 |
+
if not default_used and self._default is not None:
|
| 791 |
+
obj = self._default(obj)
|
| 792 |
+
default_used = 1
|
| 793 |
+
continue
|
| 794 |
+
|
| 795 |
+
if self._datetime and check(obj, _DateTime):
|
| 796 |
+
raise ValueError(f"Cannot serialize {obj!r} where tzinfo=None")
|
| 797 |
+
|
| 798 |
+
raise TypeError(f"Cannot serialize {obj!r}")
|
| 799 |
+
|
| 800 |
+
def pack(self, obj):
|
| 801 |
+
try:
|
| 802 |
+
self._pack(obj)
|
| 803 |
+
except:
|
| 804 |
+
self._buffer = BytesIO() # force reset
|
| 805 |
+
raise
|
| 806 |
+
if self._autoreset:
|
| 807 |
+
ret = self._buffer.getvalue()
|
| 808 |
+
self._buffer = BytesIO()
|
| 809 |
+
return ret
|
| 810 |
+
|
| 811 |
+
def pack_map_pairs(self, pairs):
|
| 812 |
+
self._pack_map_pairs(len(pairs), pairs)
|
| 813 |
+
if self._autoreset:
|
| 814 |
+
ret = self._buffer.getvalue()
|
| 815 |
+
self._buffer = BytesIO()
|
| 816 |
+
return ret
|
| 817 |
+
|
| 818 |
+
def pack_array_header(self, n):
|
| 819 |
+
if n >= 2**32:
|
| 820 |
+
raise ValueError
|
| 821 |
+
self._pack_array_header(n)
|
| 822 |
+
if self._autoreset:
|
| 823 |
+
ret = self._buffer.getvalue()
|
| 824 |
+
self._buffer = BytesIO()
|
| 825 |
+
return ret
|
| 826 |
+
|
| 827 |
+
def pack_map_header(self, n):
|
| 828 |
+
if n >= 2**32:
|
| 829 |
+
raise ValueError
|
| 830 |
+
self._pack_map_header(n)
|
| 831 |
+
if self._autoreset:
|
| 832 |
+
ret = self._buffer.getvalue()
|
| 833 |
+
self._buffer = BytesIO()
|
| 834 |
+
return ret
|
| 835 |
+
|
| 836 |
+
def pack_ext_type(self, typecode, data):
|
| 837 |
+
if not isinstance(typecode, int):
|
| 838 |
+
raise TypeError("typecode must have int type.")
|
| 839 |
+
if not 0 <= typecode <= 127:
|
| 840 |
+
raise ValueError("typecode should be 0-127")
|
| 841 |
+
if not isinstance(data, bytes):
|
| 842 |
+
raise TypeError("data must have bytes type")
|
| 843 |
+
L = len(data)
|
| 844 |
+
if L > 0xFFFFFFFF:
|
| 845 |
+
raise ValueError("Too large data")
|
| 846 |
+
if L == 1:
|
| 847 |
+
self._buffer.write(b"\xd4")
|
| 848 |
+
elif L == 2:
|
| 849 |
+
self._buffer.write(b"\xd5")
|
| 850 |
+
elif L == 4:
|
| 851 |
+
self._buffer.write(b"\xd6")
|
| 852 |
+
elif L == 8:
|
| 853 |
+
self._buffer.write(b"\xd7")
|
| 854 |
+
elif L == 16:
|
| 855 |
+
self._buffer.write(b"\xd8")
|
| 856 |
+
elif L <= 0xFF:
|
| 857 |
+
self._buffer.write(b"\xc7" + struct.pack("B", L))
|
| 858 |
+
elif L <= 0xFFFF:
|
| 859 |
+
self._buffer.write(b"\xc8" + struct.pack(">H", L))
|
| 860 |
+
else:
|
| 861 |
+
self._buffer.write(b"\xc9" + struct.pack(">I", L))
|
| 862 |
+
self._buffer.write(struct.pack("B", typecode))
|
| 863 |
+
self._buffer.write(data)
|
| 864 |
+
|
| 865 |
+
def _pack_array_header(self, n):
|
| 866 |
+
if n <= 0x0F:
|
| 867 |
+
return self._buffer.write(struct.pack("B", 0x90 + n))
|
| 868 |
+
if n <= 0xFFFF:
|
| 869 |
+
return self._buffer.write(struct.pack(">BH", 0xDC, n))
|
| 870 |
+
if n <= 0xFFFFFFFF:
|
| 871 |
+
return self._buffer.write(struct.pack(">BI", 0xDD, n))
|
| 872 |
+
raise ValueError("Array is too large")
|
| 873 |
+
|
| 874 |
+
def _pack_map_header(self, n):
|
| 875 |
+
if n <= 0x0F:
|
| 876 |
+
return self._buffer.write(struct.pack("B", 0x80 + n))
|
| 877 |
+
if n <= 0xFFFF:
|
| 878 |
+
return self._buffer.write(struct.pack(">BH", 0xDE, n))
|
| 879 |
+
if n <= 0xFFFFFFFF:
|
| 880 |
+
return self._buffer.write(struct.pack(">BI", 0xDF, n))
|
| 881 |
+
raise ValueError("Dict is too large")
|
| 882 |
+
|
| 883 |
+
def _pack_map_pairs(self, n, pairs, nest_limit=DEFAULT_RECURSE_LIMIT):
|
| 884 |
+
self._pack_map_header(n)
|
| 885 |
+
for k, v in pairs:
|
| 886 |
+
self._pack(k, nest_limit - 1)
|
| 887 |
+
self._pack(v, nest_limit - 1)
|
| 888 |
+
|
| 889 |
+
def _pack_raw_header(self, n):
|
| 890 |
+
if n <= 0x1F:
|
| 891 |
+
self._buffer.write(struct.pack("B", 0xA0 + n))
|
| 892 |
+
elif self._use_bin_type and n <= 0xFF:
|
| 893 |
+
self._buffer.write(struct.pack(">BB", 0xD9, n))
|
| 894 |
+
elif n <= 0xFFFF:
|
| 895 |
+
self._buffer.write(struct.pack(">BH", 0xDA, n))
|
| 896 |
+
elif n <= 0xFFFFFFFF:
|
| 897 |
+
self._buffer.write(struct.pack(">BI", 0xDB, n))
|
| 898 |
+
else:
|
| 899 |
+
raise ValueError("Raw is too large")
|
| 900 |
+
|
| 901 |
+
def _pack_bin_header(self, n):
|
| 902 |
+
if not self._use_bin_type:
|
| 903 |
+
return self._pack_raw_header(n)
|
| 904 |
+
elif n <= 0xFF:
|
| 905 |
+
return self._buffer.write(struct.pack(">BB", 0xC4, n))
|
| 906 |
+
elif n <= 0xFFFF:
|
| 907 |
+
return self._buffer.write(struct.pack(">BH", 0xC5, n))
|
| 908 |
+
elif n <= 0xFFFFFFFF:
|
| 909 |
+
return self._buffer.write(struct.pack(">BI", 0xC6, n))
|
| 910 |
+
else:
|
| 911 |
+
raise ValueError("Bin is too large")
|
| 912 |
+
|
| 913 |
+
def bytes(self):
|
| 914 |
+
"""Return internal buffer contents as bytes object"""
|
| 915 |
+
return self._buffer.getvalue()
|
| 916 |
+
|
| 917 |
+
def reset(self):
|
| 918 |
+
"""Reset internal buffer.
|
| 919 |
+
|
| 920 |
+
This method is useful only when autoreset=False.
|
| 921 |
+
"""
|
| 922 |
+
self._buffer = BytesIO()
|
| 923 |
+
|
| 924 |
+
def getbuffer(self):
|
| 925 |
+
"""Return view of internal buffer."""
|
| 926 |
+
if _USING_STRINGBUILDER:
|
| 927 |
+
return memoryview(self.bytes())
|
| 928 |
+
else:
|
| 929 |
+
return self._buffer.getbuffer()
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/INSTALLER
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pip
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The MIT License (MIT)
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2014-2022 Matthew Brennan Jones <matthew.brennan.jones@gmail.com>
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
| 6 |
+
this software and associated documentation files (the "Software"), to deal in
|
| 7 |
+
the Software without restriction, including without limitation the rights to
|
| 8 |
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
| 9 |
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
| 10 |
+
subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
| 17 |
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
| 18 |
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
| 19 |
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 20 |
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/METADATA
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.1
|
| 2 |
+
Name: py-cpuinfo
|
| 3 |
+
Version: 9.0.0
|
| 4 |
+
Summary: Get CPU info with pure Python
|
| 5 |
+
Home-page: https://github.com/workhorsy/py-cpuinfo
|
| 6 |
+
Author: Matthew Brennan Jones
|
| 7 |
+
Author-email: matthew.brennan.jones@gmail.com
|
| 8 |
+
License: MIT
|
| 9 |
+
Platform: UNKNOWN
|
| 10 |
+
Classifier: Development Status :: 5 - Production/Stable
|
| 11 |
+
Classifier: Topic :: Utilities
|
| 12 |
+
Classifier: License :: OSI Approved :: MIT License
|
| 13 |
+
Classifier: Programming Language :: Python :: 3
|
| 14 |
+
License-File: LICENSE
|
| 15 |
+
|
| 16 |
+
py-cpuinfo
|
| 17 |
+
==========
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
Py-cpuinfo gets CPU info with pure Python. Py-cpuinfo should work
|
| 21 |
+
without any extra programs or libraries, beyond what your OS provides.
|
| 22 |
+
It does not require any compilation(C/C++, assembly, et cetera) to use.
|
| 23 |
+
It works with Python 3.
|
| 24 |
+
|
| 25 |
+
Documentation can be viewed here: https://github.com/workhorsy/py-cpuinfo
|
| 26 |
+
|
| 27 |
+
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/RECORD
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
../../../bin/cpuinfo,sha256=cbFrrk_uu3L9jDppjHzFSmKr4kR7CDbIh27vGRm3EsI,220
|
| 2 |
+
cpuinfo/__init__.py,sha256=T6gndqGAggfJCu4_iOziTnomCN7KzaAK_OYTewE4FMA,44
|
| 3 |
+
cpuinfo/__main__.py,sha256=nSxC6Hqhi-0lN7Z4WwtKdxQdf3cUJefb5hOahCzh4Yg,33
|
| 4 |
+
cpuinfo/__pycache__/__init__.cpython-310.pyc,,
|
| 5 |
+
cpuinfo/__pycache__/__main__.cpython-310.pyc,,
|
| 6 |
+
cpuinfo/__pycache__/cpuinfo.cpython-310.pyc,,
|
| 7 |
+
cpuinfo/cpuinfo.py,sha256=HHyDlDUNovE3QzJ3hviiM1ngyOC4iD7i6oGiz2iTmVk,84388
|
| 8 |
+
py_cpuinfo-9.0.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
| 9 |
+
py_cpuinfo-9.0.0.dist-info/LICENSE,sha256=3br3Y5a_XHqkWXWiHq_i4i7st9paoNt8sOYVL6r-800,1127
|
| 10 |
+
py_cpuinfo-9.0.0.dist-info/METADATA,sha256=rRFelvhFdoYcXnXXYDAbgdIxQ8_iVUa5lUHgEmU3ncE,794
|
| 11 |
+
py_cpuinfo-9.0.0.dist-info/RECORD,,
|
| 12 |
+
py_cpuinfo-9.0.0.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 13 |
+
py_cpuinfo-9.0.0.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
| 14 |
+
py_cpuinfo-9.0.0.dist-info/entry_points.txt,sha256=ZwrsclY_xUA0xJZK98bLxBdcowxnkK0ANYUT4FYcZJ8,42
|
| 15 |
+
py_cpuinfo-9.0.0.dist-info/top_level.txt,sha256=XsjpunhkxD4hvznqQjrFNw0rtgizHEOGzewPZY3UEtU,8
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/REQUESTED
ADDED
|
File without changes
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: bdist_wheel (0.37.1)
|
| 3 |
+
Root-Is-Purelib: true
|
| 4 |
+
Tag: py3-none-any
|
| 5 |
+
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/entry_points.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[console_scripts]
|
| 2 |
+
cpuinfo = cpuinfo:main
|
| 3 |
+
|
vlmpy310/lib/python3.10/site-packages/py_cpuinfo-9.0.0.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
cpuinfo
|
vlmpy310/lib/python3.10/site-packages/pyasn1-0.6.1.dist-info/INSTALLER
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pip
|