Ask Your Question

Polynomial ring indexed over an abelian group (magma).

asked 2020-04-16 19:27:57 +0200

jin gravatar image

Hi, I wonder if currently there's a cleaner way to do $x^v \cdot x^w = x^{v+w}$ in Sage, where $v, w$ are some vectors in a vector space?

Once a basis ${v_1, \cdots v_n}$ for the vector space is chosen, this can be done by identifying $x_i$ with $x^{v_i}$, and use the multivariate ring $R[x_1, \cdots, x_n]$. However, I would like to do this in an intrinsic manner, i.e. not choosing a basis.

More generally, I think for any element $m$ in any magma $M$, we should be able to define an algebra $R[x^m|m\in M]$ over any given ring $R$. Notice that this is not the same as FreeAbelianMonoid generated over $M$, as in this case $x^m x^{n}$ is not the same as $x^{mn}$.


Is $R[x^m|m\in M$$ currently doable? If not, I might work on writing it.


A reason why I think it would be helpful: it can help calculating generalized characters of representations of quantum groups.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-04-18 12:53:57 +0200

mwageringel gravatar image

updated 2020-04-18 13:00:16 +0200

This functionality exists in Sage under the name of group algebras.

For example for a cyclic group:

sage: G = ZZ.quotient(10)
sage: A = G.algebra(QQ, category=CommutativeAdditiveGroups(), prefix='x'); A
Algebra of Ring of integers modulo 10 over Rational Field
sage: x = A.basis()
sage: x[G(7)] * x[G(6)]
sage: x[G(7) + G(6)]
sage: (x[G(2)] + x[G(9)])^2
2*x1 + x4 + x8

The construction is more general and also works for monoids, semigroups and even for sets (without multiplication), for example:

sage: Semigroups().example('free').algebra(ZZ)
Algebra of An example of a semigroup: the free semigroup generated by ('a', 'b', 'c', 'd') over Integer Ring

It looks like this is not fully implemented for magmas yet though. A place to start working on this would be to implement Magmas.Algebras similar to Semigroups.Algebras, I think.

Since vector spaces can be viewed as commutative additive groups, this algebra construction should also work for vector spaces. An implementation detail makes this tricky though.

sage: V = QQ^4
sage: A = V.algebra(ZZ, prefix='x')
sage: x = A.basis()
sage: v = V([1,2,0,3])
sage: v.set_immutable()
sage: x[v]
x[(1, 2, 0, 3)]
sage: x[v] * x[v]
TypeError: mutable vectors are unhashable

The implementation of the algebra requires its indexing elements to be immutable. The sum of two (immutable) vectors is not usually immutable though, so the last statement in the previous example fails.

There is no easy solution to this, but a workaround is to define a wrapper class of vectors, which makes sure its elements are immutable and that the addition operation preserves this property. Here is a rudimentary implementation (which might lack some features, see sage.categories.examples for more details):

class ImmutableVectors(UniqueRepresentation, Parent):
    def __init__(self, V):
        self._vspace = V
        Parent.__init__(self, category=CommutativeAdditiveGroups())

    def _repr_(self):
        return "Immutable vectors in %s" % self._vspace

    def _an_element_(self):
        return self(self._vspace.an_element())

    def zero(self):
        return self(

    class ImmutableVectorElement(ElementWrapper):
        def __init__(self, parent, value):
            super().__init__(parent, value)
        def _add_(self, other):
            return ImmutableVectors.Element(self.parent(), self.value + other.value)

    Element = ImmutableVectorElement

Now the wrapped vectors are immutable.

sage: V = QQ^4
sage: V_im = ImmutableVectors(V); V_im
Immutable vectors in Vector space of dimension 4 over Rational Field
sage: set_random_seed(1)
sage: v, w = [V_im(V.random_element()) for _ in (1, 2)]; v, w
((-1/12, 0, -3, -2), (2, 0, -3/2, -1))
sage: v.value.is_immutable()

Some examples of an algebra over these immutable vectors:

sage: A = V_im.algebra(ZZ, prefix='x'); A
Algebra of Immutable vectors in Vector space of dimension 4 over Rational Field over Integer Ring
sage: x = A.basis()
sage: A.an_element()
x(1, 0, 0, 0)
x(0, 0, 0, 0)
sage: x[v]
x(-1/12, 0, -3, -2)
sage: x[v] == A.monomial(v)
sage: x[v] * x[w]
x(23/12, 0, -9/2, -3)
sage: x[v + w] == x[v] * x[w]
sage: u = V_im(V([1..4]))
sage: f = x[u] + x[V_im(1/3 * u.value)]; f
x(1, 2, 3, 4) + x(1/3, 2/3, 1, 4/3)
sage: f^2
x(2, 4, 6, 8) + 2*x(4/3, 8/3, 4, 16/3) + x(2/3, 4/3, 2, 8/3)

Beware that this construction is very general, but can also be quite slow on large problems.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2020-04-16 19:27:57 +0200

Seen: 78 times

Last updated: Apr 18 '20