| | 950 | def gcd(self, other): |
|---|
| | 951 | r""" |
|---|
| | 952 | Return the gcd of two factorizations. |
|---|
| | 953 | |
|---|
| | 954 | EXAMPLES: |
|---|
| | 955 | sage: factor(-30).gcd(factor(-160)) |
|---|
| | 956 | 2 * 5 |
|---|
| | 957 | sage: factor(gcd(-30,160)) |
|---|
| | 958 | 2 * 5 |
|---|
| | 959 | |
|---|
| | 960 | """ |
|---|
| | 961 | if not isinstance(other, Factorization): |
|---|
| | 962 | return self * Factorization([(other, 1)]) |
|---|
| | 963 | if self.is_commutative() and other.is_commutative(): |
|---|
| | 964 | d1 = dict(self) |
|---|
| | 965 | d2 = dict(other) |
|---|
| | 966 | s = {} |
|---|
| | 967 | for a in set(d1.keys()).intersection(set(d2.keys())): |
|---|
| | 968 | s[a] = min(d1[a],d2[a]) |
|---|
| | 969 | return Factorization(list(s.iteritems()), unit=self.base_ring()(1)) |
|---|
| | 970 | else: |
|---|
| | 971 | raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" |
|---|
| | 972 | |
|---|
| | 973 | def lcm(self, other): |
|---|
| | 974 | r""" |
|---|
| | 975 | Return the lcm of two factorizations. |
|---|
| | 976 | |
|---|
| | 977 | EXAMPLES: |
|---|
| | 978 | sage: factor(-10).lcm(factor(-16)) |
|---|
| | 979 | 2^4 * 5 |
|---|
| | 980 | sage: factor(lcm(-10,16)) |
|---|
| | 981 | 2^4 * 5 |
|---|
| | 982 | |
|---|
| | 983 | """ |
|---|
| | 984 | if not isinstance(other, Factorization): |
|---|
| | 985 | return self * Factorization([(other, 1)]) |
|---|
| | 986 | if self.is_commutative() and other.is_commutative(): |
|---|
| | 987 | d1 = dict(self) |
|---|
| | 988 | d2 = dict(other) |
|---|
| | 989 | s = {} |
|---|
| | 990 | for a in set(d1.keys()).union(set(d2.keys())): |
|---|
| | 991 | s[a] = max(d1.get(a,0),d2.get(a,0)) |
|---|
| | 992 | return Factorization(list(s.iteritems()), unit=self.base_ring()(1)) |
|---|
| | 993 | else: |
|---|
| | 994 | raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" |
|---|
| | 995 | |
|---|