domainscalar.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. Module for the DomainScalar class.
  3. A DomainScalar represents an element which is in a particular
  4. Domain. The idea is that the DomainScalar class provides the
  5. convenience routines for unifying elements with different domains.
  6. It assists in Scalar Multiplication and getitem for DomainMatrix.
  7. """
  8. from ..constructor import construct_domain
  9. from sympy.polys.domains import Domain, ZZ
  10. class DomainScalar:
  11. r"""
  12. docstring
  13. """
  14. def __new__(cls, element, domain):
  15. if not isinstance(domain, Domain):
  16. raise TypeError("domain should be of type Domain")
  17. if not domain.of_type(element):
  18. raise TypeError("element %s should be in domain %s" % (element, domain))
  19. return cls.new(element, domain)
  20. @classmethod
  21. def new(cls, element, domain):
  22. obj = super().__new__(cls)
  23. obj.element = element
  24. obj.domain = domain
  25. return obj
  26. def __repr__(self):
  27. return repr(self.element)
  28. @classmethod
  29. def from_sympy(cls, expr):
  30. [domain, [element]] = construct_domain([expr])
  31. return cls.new(element, domain)
  32. def to_sympy(self):
  33. return self.domain.to_sympy(self.element)
  34. def to_domain(self, domain):
  35. element = domain.convert_from(self.element, self.domain)
  36. return self.new(element, domain)
  37. def convert_to(self, domain):
  38. return self.to_domain(domain)
  39. def unify(self, other):
  40. domain = self.domain.unify(other.domain)
  41. return self.to_domain(domain), other.to_domain(domain)
  42. def __add__(self, other):
  43. if not isinstance(other, DomainScalar):
  44. return NotImplemented
  45. self, other = self.unify(other)
  46. return self.new(self.element + other.element, self.domain)
  47. def __sub__(self, other):
  48. if not isinstance(other, DomainScalar):
  49. return NotImplemented
  50. self, other = self.unify(other)
  51. return self.new(self.element - other.element, self.domain)
  52. def __mul__(self, other):
  53. if not isinstance(other, DomainScalar):
  54. if isinstance(other, int):
  55. other = DomainScalar(ZZ(other), ZZ)
  56. else:
  57. return NotImplemented
  58. self, other = self.unify(other)
  59. return self.new(self.element * other.element, self.domain)
  60. def __floordiv__(self, other):
  61. if not isinstance(other, DomainScalar):
  62. return NotImplemented
  63. self, other = self.unify(other)
  64. return self.new(self.domain.quo(self.element, other.element), self.domain)
  65. def __mod__(self, other):
  66. if not isinstance(other, DomainScalar):
  67. return NotImplemented
  68. self, other = self.unify(other)
  69. return self.new(self.domain.rem(self.element, other.element), self.domain)
  70. def __divmod__(self, other):
  71. if not isinstance(other, DomainScalar):
  72. return NotImplemented
  73. self, other = self.unify(other)
  74. q, r = self.domain.div(self.element, other.element)
  75. return (self.new(q, self.domain), self.new(r, self.domain))
  76. def __pow__(self, n):
  77. if not isinstance(n, int):
  78. return NotImplemented
  79. return self.new(self.element**n, self.domain)
  80. def __pos__(self):
  81. return self.new(+self.element, self.domain)
  82. def __eq__(self, other):
  83. if not isinstance(other, DomainScalar):
  84. return NotImplemented
  85. return self.element == other.element and self.domain == other.domain
  86. def is_zero(self):
  87. return self.element == self.domain.zero
  88. def is_one(self):
  89. return self.element == self.domain.one