Ask Your Question

Stefan's profile - activity

2023-02-21 16:43:58 +0200 received badge  Famous Question (source)
2022-12-23 04:35:09 +0200 received badge  Taxonomist
2020-05-25 06:38:17 +0200 received badge  Good Question (source)
2016-01-03 01:01:51 +0200 received badge  Nice Question (source)
2015-07-18 09:18:36 +0200 received badge  Notable Question (source)
2015-04-15 20:41:50 +0200 received badge  Popular Question (source)
2014-08-23 21:50:34 +0200 received badge  Good Answer (source)
2014-05-12 05:51:20 +0200 received badge  Nice Answer (source)
2013-09-13 15:04:55 +0200 answered a question distributed computing?

There's a discussion of this in sage-support, see here:

https://groups.google.com/d/topic/sage-support/Ia8IMUewiho/discussion

2013-09-13 11:29:55 +0200 received badge  Necromancer (source)
2013-09-13 11:29:55 +0200 received badge  Teacher (source)
2013-09-12 19:13:28 +0200 answered a question construction of product rings Z/nZ x Z/mZ

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)
2013-08-29 11:32:42 +0200 commented question construction of product rings Z/nZ x Z/mZ

Good question! I need this myself.

2012-11-30 11:49:48 +0200 answered a question Installation problem on Mac OSX

I ran into the same problem. Turns out I downloaded the version for OS X 10.8 while I'm running 10.7. The

sage-5.4.1-OSX-64bit-10.7-x86_64-Darwin.dmg

binary works just fine for me.

Incidentally, when did the binaries become dependent on the OS version? That's just annoying.

2011-03-25 08:43:24 +0200 answered a question how to execute a self-written python library within an online notebook

There seems to be some discussion here

2011-03-25 08:41:02 +0200 received badge  Supporter (source)
2011-03-25 07:36:14 +0200 received badge  Student (source)
2011-03-25 04:54:01 +0200 asked a question Partial Algebras

Hi!

As part of a future package for matroid theory, I would like to implement an algebraic structure called a "partial field".

A partial field is a tuple P = (S, 0, 1, +, *) such that

  1. S - {0} is a group G under * with identity 1
  2. Every element p has an additive inverse q such that p + q = 0
  3. Otherwise, the sum p + q is defined for only some choices of p + q.

Associativity of + is respected "as much as possible". Formally, this means that there exists a ring R such that G is contained in the group of units of R, and addition in the partial field is the restriction of addition in the ring to S.

Example: the regular partial field. This has elements {-1, 0, 1}, with addition and multiplication as in ZZ, but 1 + 1 is not defined.

How can we best implement such structures? That is, where should the category PartialFields go? One choice would be to subclass Rings. However, for various reasons it is desirable to return an "undefined_element" instead of the sum in the ring. This would break associativity tests that seem to be required for rings.

Example: take the partial field {(-1)^s 2^k : s,k in ZZ} U {0}, with usual multiplication and addition in QQ restricted to this set (e.g. 1/2 - 1 = -1/2 is ok, but 4 - 1/4 is undefined).

  • (1 + 1) + (1 -2) = 1
  • ((1 + 1) + 1) - 2 = undefined_element

If we do not subclass Ring, then a whole new can of worms is opened: we're no longer allowed to fill matrices with our elements. I'm not looking forward to re-implementing the Matrix class (though we would probably subclass it anyway as PMatrix, since we need a different determinant and rank algorithm, as well as some new methods).

P.S. How does one define ZZ[1/2] in Sage? P.P.S. How would I coerce from my own, custom ring into QQ?