Coverage for /usr/lib/python3/dist-packages/sympy/polys/matrices/domainscalar.py: 31%
78 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1"""
3Module for the DomainScalar class.
5A DomainScalar represents an element which is in a particular
6Domain. The idea is that the DomainScalar class provides the
7convenience routines for unifying elements with different domains.
9It assists in Scalar Multiplication and getitem for DomainMatrix.
11"""
12from ..constructor import construct_domain
14from sympy.polys.domains import Domain, ZZ
17class DomainScalar:
18 r"""
19 docstring
20 """
22 def __new__(cls, element, domain):
23 if not isinstance(domain, Domain):
24 raise TypeError("domain should be of type Domain")
25 if not domain.of_type(element):
26 raise TypeError("element %s should be in domain %s" % (element, domain))
27 return cls.new(element, domain)
29 @classmethod
30 def new(cls, element, domain):
31 obj = super().__new__(cls)
32 obj.element = element
33 obj.domain = domain
34 return obj
36 def __repr__(self):
37 return repr(self.element)
39 @classmethod
40 def from_sympy(cls, expr):
41 [domain, [element]] = construct_domain([expr])
42 return cls.new(element, domain)
44 def to_sympy(self):
45 return self.domain.to_sympy(self.element)
47 def to_domain(self, domain):
48 element = domain.convert_from(self.element, self.domain)
49 return self.new(element, domain)
51 def convert_to(self, domain):
52 return self.to_domain(domain)
54 def unify(self, other):
55 domain = self.domain.unify(other.domain)
56 return self.to_domain(domain), other.to_domain(domain)
58 def __add__(self, other):
59 if not isinstance(other, DomainScalar):
60 return NotImplemented
61 self, other = self.unify(other)
62 return self.new(self.element + other.element, self.domain)
64 def __sub__(self, other):
65 if not isinstance(other, DomainScalar):
66 return NotImplemented
67 self, other = self.unify(other)
68 return self.new(self.element - other.element, self.domain)
70 def __mul__(self, other):
71 if not isinstance(other, DomainScalar):
72 if isinstance(other, int):
73 other = DomainScalar(ZZ(other), ZZ)
74 else:
75 return NotImplemented
77 self, other = self.unify(other)
78 return self.new(self.element * other.element, self.domain)
80 def __floordiv__(self, other):
81 if not isinstance(other, DomainScalar):
82 return NotImplemented
83 self, other = self.unify(other)
84 return self.new(self.domain.quo(self.element, other.element), self.domain)
86 def __mod__(self, other):
87 if not isinstance(other, DomainScalar):
88 return NotImplemented
89 self, other = self.unify(other)
90 return self.new(self.domain.rem(self.element, other.element), self.domain)
92 def __divmod__(self, other):
93 if not isinstance(other, DomainScalar):
94 return NotImplemented
95 self, other = self.unify(other)
96 q, r = self.domain.div(self.element, other.element)
97 return (self.new(q, self.domain), self.new(r, self.domain))
99 def __pow__(self, n):
100 if not isinstance(n, int):
101 return NotImplemented
102 return self.new(self.element**n, self.domain)
104 def __pos__(self):
105 return self.new(+self.element, self.domain)
107 def __eq__(self, other):
108 if not isinstance(other, DomainScalar):
109 return NotImplemented
110 return self.element == other.element and self.domain == other.domain
112 def is_zero(self):
113 return self.element == self.domain.zero
115 def is_one(self):
116 return self.element == self.domain.one