First time here? Check out the FAQ!

Ask Your Question
3

construction of product rings Z/nZ x Z/mZ

asked 11 years ago

jack77 gravatar image

Hello,

is it possible in Sage to construct a product ring of two integer modulo rings?

When just creating an cartesian_product, it is for example not possible to add its elements:

R5=Integers(5)
R3=Integers(3)
CR=R5.cartesian_product(R3)
elem=CR((3,2)) # ok, element construction
elem*elem # ok
elem+elem # fails 
CR.list() #  not implemented
CR.list_of_elements_of_multiplicative_group # not implemented
Preview: (hide)

Comments

Good question! I need this myself.

Stefan gravatar imageStefan ( 11 years ago )

3 Answers

Sort by » oldest newest most voted
3

answered 11 years ago

Stefan gravatar image

The code below (following this tutorial) seems to do the trick. I might turn it into an addition to the Sage codebase at some point (it needs documentation and tests for that, at the very least), but I don't have the time. If you feel like doing that, be my guest!

Just save to product_ring.py and, in Sage do sage: %runfile product_ring.py # from command line

or

load product_ring.py # from notebook.

import sage
from sage.rings.ring import Ring
from sage.rings.all import ZZ
from sage.structure.element import RingElement
from sage.categories.category import Category
from sage.structure.unique_representation import UniqueRepresentation
from sage.categories.rings import Rings
from sage.categories.pushout import ConstructionFunctor

class ProductRingElement(RingElement):
    def __init__(self, data, parent=None):
        if parent is None:
            raise ValueError, "The parent must be provided"
        self._data = tuple(data)
        RingElement.__init__(self,parent)

    def _repr_(self):
        return "(" + ", ".join(x._repr_() for x in self._data) + ")"

    def __cmp__(self, other):
        return cmp(self._data, other._data)

    def _add_(self, other):
        C = self.__class__
        z = zip(self._data, other._data)
        return C(tuple(zz[0]._add_(zz[1]) for zz in z), parent=self.parent())

    def _sub_(self, other):
        C = self.__class__
        z = zip(self._data, other._data)
        return C(tuple(zz[0]._sub_(zz[1]) for zz in z), parent=self.parent())

    def _mul_(self, other):
        C = self.__class__
        z = zip(self._data, other._data)
        return C(tuple(zz[0]._mul_(zz[1]) for zz in z), parent=self.parent())

    def _div_(self, other):
        C = self.__class__
        z = zip(self._data, other._data)
        return C(tuple(zz[0]._div_(zz[1]) for zz in z), parent=self.parent())

    def __iter__(self):
        r"""
        Returns an iterator of the entries of ``self``.
        """
        for x in self._data:
            yield x


class ProductRing(UniqueRepresentation, Ring):
    r"""
    The product ring of a finite number of rings, with elementwise addition
    and multiplication.
    """
    Element = ProductRingElement

    def __init__(self,rings, base=ZZ, category=None):
        r"""
        INPUT:

        - ``rings`` -- a tuple of rings.
        """
        from sage.categories.rings import Rings

        if not all(R.is_ring() for R in rings):
            raise TypeError("Expected a tuple of rings as input.")
        self._rings = tuple(rings)
        Ring.__init__(self, base=base, category=category or Rings())

    def _repr_(self):
        return "Product ring: (" + ", ".join(R._repr_() for R in self._rings) + ")"

    def _element_constructor_(self, *args, **kwds):
        if len(args) == len(self._rings):
            z = zip(self._rings, args)
        elif len(args) == 1:
            try:
                if len(args[0]) != len(self._rings):
                    raise TypeError  # also if args[0] has no len()
                z = zip(self._rings, args[0])
            except TypeError:
                z = zip(self._rings, [args[0] for x in self._rings])
        else:
            raise TypeError("Wrong number of ring elements")
        return self.element_class(tuple(zz[0](zz[1]) for zz in z), parent=self, **kwds)

    def _coerce_map_from_(self, S):
        if all(R.has_coerce_map_from(S) for R in self._rings):
            return True

    def __pow__(self,n):
        r"""
        Returns the ``n``-th power of self as a vector space.
        """
        from sage.modules ...
(more)
Preview: (hide)
link

Comments

Thanks for the code. Someone, please explain to me how to use this code to construct the product of rings in jupiter? Thanks again

GA3165 gravatar imageGA3165 ( 4 years ago )
1

answered 11 years ago

kcrisman gravatar image

Here is part of the problem - the Cartesian product business seems to be about categories, not the actual rings. And it doesn't really give you what you want. In fact, I find this all somewhat disturbing.

sage: type(CR)
sage.sets.cartesian_product.CartesianProduct_with_category
sage: type(R5)
sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic_with_category
sage: elem*elem
(9, 4)
sage: elem.summand_projection(1)
2
sage: type(elem.summand_projection(1))
sage.rings.integer.Integer

You can do it with just the groups, but of course that's not what you want.

Preview: (hide)
link

Comments

'You can do it with just the groups' - you mean using summand_projection? 'of course that's not what you want' - yes, I would like something more comfortable

jack77 gravatar imagejack77 ( 11 years ago )
0

answered 11 years ago

tmonteil gravatar image

It seems that CR forgot its ring structure, and kept only its multiplicative operation:

sage: elem.parent().category()
Category of Cartesian products of monoids
Preview: (hide)
link

Comments

Right... but the question is what went wrong?

kcrisman gravatar imagekcrisman ( 11 years ago )

Well, the question is, is there a way to construct the product ring Z/nZ x Z/mZ in sage

jack77 gravatar imagejack77 ( 11 years ago )

I do not know, i asked to the `sage.categories` specialists.

tmonteil gravatar imagetmonteil ( 11 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 2,102 times

Last updated: Sep 12 '13