I'm trying to define a class based on Algebra, and I'm having problems. I think the issue is that I don't understand how coercion is supposed to work.
Right now I have def class SteenrodAlgebra(Algebra) and def class SteenrodAlgebraElement(AlgebraElement) In the second one, I've defined an _add_ method. Should I also define an __add__ method explicitly, or should I do something with coercion and the class SteenrodAlgebra? I've tried reading the documentation (like section 11.5.2 in the reference manual), and I've tried reading various pieces of source code, but I'm just getting confused. Here's the relevant code. steenrod_prime = 2 class SteenrodAlgebra(Algebra): def __init__(self, p=steenrod_prime): """ INPUT: p: positive prime integer return mod p Steenrod algebra """ if is_prime(p): self.prime = p ParentWithGens.__init__(self, GF(p)) else: raise ValueError, "%s is not prime." % p def __repr__(self): return "mod %d Steenrod algebra" % self.prime def __eq__(self,right): return type(self) == type(right) and \ self.prime == right.prime def __call__(self, x): if isinstance(x, SteenrodAlgebraElement) and x.parent() is self: return x else: return SteenrodAlgebraElement(self,x) # I don't know how to write this. # I took this from FreeAlgebraQuotient, I think. def _coerce_impl(self, x): """ Return the coercion of x into this Steenrod algebra. The algebras that coerce into this quotient ring are: * this Steenrod algebra * its base field """ return self._coerce_try(x, [self, self.base_ring()]) def __contains__(self, x): return isinstance(x, SteenrodAlgebraElement) and x.parent() == self class SteenrodAlgebraElement(AlgebraElement): def __init__(self, poly, p=steenrod_prime): """ INPUT: poly: dictionary with entries of form (monomial, coefficient) Each coefficient is in GF(p), and each monomial is a tuple of non-negative integers (a, b, c, ...), corresponding to the Milnor basis element Sq(a, b, c, ...). p: positive prime number (default steenrod_prime) """ if is_prime(p): if p == 2: RingElement.__init__(self,SteenrodAlgebra(p)) F = parent(self).base_ring() self.base_field = F new_poly = {} for mono in poly: trimmed = check_and_trim(mono) if new_poly.has_key(trimmed): coeff = F(poly[mono] + new_poly[trimmed]) else: coeff = F(poly[mono]) if not coeff.is_zero(): new_poly[trimmed] = coeff self.elt = new_poly else: raise NotImplementedError, "Only the mod 2 Steenrod algebra has been implemented" else: raise ValueError, "%s is not prime." % p def _add_(self, other): F = self.base_field poly1 = self.elt poly2 = other.elt for mono in poly2: if poly1.has_key(mono): coeff = F(poly1[mono] + poly2[mono]) else: coeff = F(poly2[mono]) if coeff == 0: del poly1[mono] else: poly1[mono] = coeff return SteenrodAlgebraElement(poly1) # should I do this? # def __add__(self, other): # return (self._add_(other)) --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---