| 1704 | If x has different parent than `self`, they are first coerced to a |
| 1705 | common parent if possible. If this coercion fails, it returns a |
| 1706 | TypeError. This fixes \#5759 |
| 1707 | |
| 1708 | :: |
| 1709 | sage: Zmod(2)(0).divides(Zmod(2)(0)) |
| 1710 | True |
| 1711 | sage: Zmod(2)(0).divides(Zmod(2)(1)) |
| 1712 | False |
| 1713 | sage: Zmod(5)(1).divides(Zmod(2)(1)) |
| 1714 | Traceback (most recent call last): |
| 1715 | ... |
| 1716 | TypeError: no common canonical parent for objects with parents: 'Ring of integers modulo 5' and 'Ring of integers modulo 2' |
| 1717 | sage: Zmod(35)(4).divides(Zmod(7)(1)) |
| 1718 | True |
| 1719 | sage: Zmod(35)(7).divides(Zmod(7)(1)) |
| 1720 | False |
1705 | | # First we test some generic conditions: |
1706 | | try: |
1707 | | if x.is_zero(): |
1708 | | return True # everything divides 0 |
1709 | | except (AttributeError, NotImplementedError): |
1710 | | pass |
1711 | | |
1712 | | try: |
1713 | | if self.is_zero(): |
1714 | | return False # 0 divides nothing else |
1715 | | except (AttributeError, NotImplementedError): |
1716 | | pass |
1717 | | |
1718 | | try: |
1719 | | if self.is_unit(): |
1720 | | return True # units divide everything |
1721 | | except (AttributeError, NotImplementedError): |
1722 | | pass |
1723 | | |
1724 | | try: |
1725 | | if self.is_one(): |
1726 | | return True # 1 divides everything |
1727 | | # (is_unit() may not be implemented) |
1728 | | except (AttributeError, NotImplementedError): |
1729 | | pass |
1730 | | |
1731 | | return (x % self) == 0 |
| 1722 | #Check if the parents are the same: |
| 1723 | |
| 1724 | if have_same_parent(self, x): |
| 1725 | # First we test some generic conditions: |
| 1726 | try: |
| 1727 | if x.is_zero(): |
| 1728 | return True # everything divides 0 |
| 1729 | except (AttributeError, NotImplementedError): |
| 1730 | pass |
| 1731 | |
| 1732 | try: |
| 1733 | if self.is_zero(): |
| 1734 | return False # 0 divides nothing else |
| 1735 | except (AttributeError, NotImplementedError): |
| 1736 | pass |
| 1737 | |
| 1738 | try: |
| 1739 | if self.is_unit(): |
| 1740 | return True # units divide everything |
| 1741 | except (AttributeError, NotImplementedError): |
| 1742 | pass |
| 1743 | |
| 1744 | try: |
| 1745 | if self.is_one(): |
| 1746 | return True # 1 divides everything |
| 1747 | # (is_unit() may not be implemented) |
| 1748 | except (AttributeError, NotImplementedError): |
| 1749 | pass |
| 1750 | |
| 1751 | return (x % self) == 0 |
| 1752 | |
| 1753 | else: |
| 1754 | #Different parents, use coercion |
| 1755 | global coercion_model |
| 1756 | a, b = coercion_model.canonical_coercion(self, x) |
| 1757 | return a.divides(b) |