Skip to content

Commit

Permalink
sagemathgh-38053: work on letterplace algebras
Browse files Browse the repository at this point in the history
    
Moving the letterplace implementation of free algebras to the `Parent`
framework

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#38053
Reported by: Frédéric Chapoton
Reviewer(s): Frédéric Chapoton, Matthias Köppe, Travis Scrimshaw
  • Loading branch information
Release Manager committed Jun 4, 2024
2 parents fddd394 + 16136d1 commit a3f648f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
10 changes: 5 additions & 5 deletions src/sage/algebras/letterplace/free_algebra_letterplace.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
# Copyright (C) 2011 Simon King <simon.king@uni-jena.de>
# Distributed under the terms of the GNU General Public License (GPL),
# version 2 or any later version. The full text of the GPL is available at:
# http://www.gnu.org/licenses/
# https://www.gnu.org/licenses/
#
###############################################################################

cdef class FreeAlgebra_letterplace

from sage.rings.ring cimport Algebra
from sage.structure.parent cimport Parent
from sage.structure.element cimport AlgebraElement, ModuleElement, RingElement, Element
from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular, MPolynomial_libsingular
from sage.algebras.letterplace.free_algebra_element_letterplace cimport FreeAlgebraElement_letterplace
Expand All @@ -22,7 +20,8 @@ cdef class FreeAlgebra_letterplace_libsingular():
cdef MPolynomialRing_libsingular _lp_ring_internal
cdef object _ngens

cdef class FreeAlgebra_letterplace(Algebra):

cdef class FreeAlgebra_letterplace(Parent):
cdef MPolynomialRing_libsingular _commutative_ring
cdef MPolynomialRing_libsingular _current_ring
cdef int _degbound
Expand All @@ -32,3 +31,4 @@ cdef class FreeAlgebra_letterplace(Algebra):
cdef str exponents_to_string(self, E)
cdef str exponents_to_latex(self, E)
cdef tuple _degrees
cdef public object _latex_names
80 changes: 44 additions & 36 deletions src/sage/algebras/letterplace/free_algebra_letterplace.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ TESTS::
sage: loads(dumps(F)) is F
True
sage: F.<x,y,z> = FreeAlgebra(QQ, implementation='letterplace')
sage: F.is_commutative()
False
sage: FreeAlgebra(QQ, implementation='letterplace', names=['x']).is_commutative()
True
.. TODO::
The computation of Groebner bases only works for global term
Expand All @@ -113,7 +119,6 @@ TESTS::
We also do not support coercion from a subalgebra, or between free
algebras with different term orderings, yet.
"""
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.libs.singular.function import lib, singular_function
Expand All @@ -122,6 +127,7 @@ from sage.libs.singular.ring cimport singular_ring_delete, singular_ring_referen
from sage.categories.algebras import Algebras
from sage.rings.noncommutative_ideals import IdealMonoid_nc
from sage.rings.polynomial.plural cimport new_CRing
from sage.misc.cachefunc import cached_method

#####################
# Define some singular functions
Expand Down Expand Up @@ -191,7 +197,7 @@ cdef MPolynomialRing_libsingular make_letterplace_ring(base_ring, blocks):
#####################
# The free algebra

cdef class FreeAlgebra_letterplace(Algebra):
cdef class FreeAlgebra_letterplace(Parent):
"""
Finitely generated free algebra, with arithmetic restricted to weighted homogeneous elements.
Expand Down Expand Up @@ -219,7 +225,6 @@ cdef class FreeAlgebra_letterplace(Algebra):
Traceback (most recent call last):
...
ArithmeticError: can only add elements of the same weighted degree
"""
# It is not really a free algebra over the given generators. Rather,
# it is a free algebra over the commutative monoid generated by the given generators.
Expand All @@ -233,9 +238,10 @@ cdef class FreeAlgebra_letterplace(Algebra):
The free associative version of the given commutative ring.
NOTE:
.. NOTE::
One is supposed to use the `FreeAlgebra` constructor, in order to use the cache.
One is supposed to use the :class:`FreeAlgebra` constructor,
in order to use the cache.
TESTS::
Expand All @@ -255,24 +261,32 @@ cdef class FreeAlgebra_letterplace(Algebra):
"""
if not isinstance(R, MPolynomialRing_libsingular):
raise TypeError("a letterplace algebra must be provided by a polynomial ring of type %s" % MPolynomialRing_libsingular)

self._ngens = R.ngens()
if degrees is None:
varnames = R.variable_names()
self._nb_slackvars = 0
else:
varnames = R.variable_names()[:-1]
self._nb_slackvars = 1
number_of_vars = self._ngens - self._nb_slackvars

base_ring = R.base_ring()
Algebra.__init__(self, base_ring, varnames,
normalize=False, category=Algebras(base_ring))
if number_of_vars <= 1:
cat = Algebras(base_ring).Commutative()
else:
cat = Algebras(base_ring)
Parent.__init__(self, base=base_ring, names=varnames,
normalize=False, category=cat)

self._commutative_ring = R
self._current_ring = make_letterplace_ring(R, 1)
self._degbound = 1
if degrees is None:
self._degrees = tuple([int(1)] * self._ngens)
else:
if (not isinstance(degrees, (tuple, list))) \
or len(degrees) != self._ngens - self._nb_slackvars \
or len(degrees) != number_of_vars \
or any(i <= 0 for i in degrees):
raise TypeError("the generator degrees must be given by a list or tuple of %d positive integers" % (self._ngens - 1))
self._degrees = tuple([int(i) for i in degrees])
Expand Down Expand Up @@ -307,17 +321,18 @@ cdef class FreeAlgebra_letterplace(Algebra):
"""
return self._ngens - self._nb_slackvars

@cached_method
def gen(self, i):
"""
Return the `i`-th generator.
INPUT:
`i` -- an integer.
- `i` -- an integer
OUTPUT:
Generator number `i`.
The generator with index `i`
EXAMPLES::
Expand All @@ -329,10 +344,7 @@ cdef class FreeAlgebra_letterplace(Algebra):
"""
if i >= self._ngens - self._nb_slackvars:
raise ValueError("this free algebra only has %d generators" % (self._ngens - self._nb_slackvars))
if self._gens is not None:
return self._gens[i]
deg = self._degrees[i]
# self.set_degbound(deg)
p = self._current_ring.gen(i)
cdef int n
cdef int j = self._ngens - 1
Expand All @@ -341,6 +353,18 @@ cdef class FreeAlgebra_letterplace(Algebra):
p *= self._current_ring.gen(j)
return FreeAlgebraElement_letterplace(self, p)

def gens(self):
"""
Return the tuple of generators.
EXAMPLES::
sage: F.<a,b,c> = FreeAlgebra(QQ, implementation='letterplace')
sage: F.gens()
(a, b, c)
"""
return tuple(self.gen(i) for i in range(self.ngens()))

def current_ring(self):
"""
Return the commutative ring that is used to emulate
Expand All @@ -365,9 +389,9 @@ cdef class FreeAlgebra_letterplace(Algebra):
"""
Return the commutative version of this free algebra.
NOTE:
.. NOTE::
This commutative ring is used as a unique key of the free algebra.
This commutative ring is used as a unique key of the free algebra.
EXAMPLES::
Expand Down Expand Up @@ -401,20 +425,6 @@ cdef class FreeAlgebra_letterplace(Algebra):
return self._degrees

# Some basic properties of this ring
def is_commutative(self):
"""
Tell whether this algebra is commutative, i.e., whether the generator number is one.
EXAMPLES::
sage: F.<x,y,z> = FreeAlgebra(QQ, implementation='letterplace')
sage: F.is_commutative()
False
sage: FreeAlgebra(QQ, implementation='letterplace', names=['x']).is_commutative()
True
"""
return self._ngens - self._nb_slackvars <= 1

def is_field(self, proof=True):
"""
Tell whether this free algebra is a field.
Expand Down Expand Up @@ -562,7 +572,7 @@ cdef class FreeAlgebra_letterplace(Algebra):
self.__monoid = IdealMonoid_nc(self)
return self.__monoid

# Auxiliar methods
# Auxiliary methods
cdef str exponents_to_string(self, E):
"""
This auxiliary method is used for the string representation of elements of this free algebra.
Expand Down Expand Up @@ -592,19 +602,19 @@ cdef class FreeAlgebra_letterplace(Algebra):
cdef list out = []
cdef list tmp
for i from 0<=i<nblocks:
# warning : the loop index is changed during the loop
base = i*ngens
tmp = [(j, E[base+j]) for j in range(ngens) if E[base+j]]
tmp = [(j, E[base + j]) for j in range(ngens) if E[base + j]]
if not tmp:
continue
var_ind, exp = tmp[0]
if len(tmp) > 1 or exp > 1:
raise NotImplementedError("\n Apparently you tried to view the letterplace algebra with\n shift-multiplication as the free algebra over a finitely\n generated free abelian monoid.\n In principle, this is correct, but it is not implemented, yet.")

out.append(self._names[var_ind])
i += self._degrees[var_ind] - 1
return '*'.join(out)

# Auxiliar methods
# Auxiliary methods
cdef str exponents_to_latex(self, E):
r"""
This auxiliary method is used for the representation of elements of this free algebra as a latex string.
Expand All @@ -625,14 +635,14 @@ cdef class FreeAlgebra_letterplace(Algebra):
cdef list tmp
cdef list names = self.latex_variable_names()
for i from 0<=i<nblocks:
# warning : the loop index is changed during the loop
base = i*ngens
tmp = [(j, E[base+j]) for j in range(ngens) if E[base+j]]
if not tmp:
continue
var_ind, exp = tmp[0]
if len(tmp) > 1 or exp > 1:
raise NotImplementedError("\n Apparently you tried to view the letterplace algebra with\n shift-multiplication as the free algebra over a finitely\n generated free abelian monoid.\n In principle, this is correct, but it is not implemented, yet.")

out.append(names[var_ind])
i += (self._degrees[var_ind] - 1)
return ' '.join(out)
Expand Down Expand Up @@ -714,7 +724,6 @@ cdef class FreeAlgebra_letterplace(Algebra):
sage: G = FreeAlgebra(GF(5), implementation='letterplace', names=['x','y','z','t'], degrees=[1,2,3,4])
sage: t*G.0 # indirect doctest
t*x
"""
if self == S or self._current_ring.has_coerce_map_from(S):
return True
Expand Down Expand Up @@ -857,7 +866,6 @@ cdef class FreeAlgebra_letterplace(Algebra):
sage: G = FreeAlgebra(GF(5), implementation='letterplace', names=['x','y','z','t'], degrees=[1,2,3,4])
sage: G(t*y + 2*y^3 - 4*z^2) # indirect doctest
(2)*y*y*y + z*z + t*y
"""
if isinstance(x, str):
from sage.misc.sage_eval import sage_eval
Expand Down

0 comments on commit a3f648f

Please sign in to comment.