ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 06 Sep 2019 11:43:55 +0200implement algebras with some extra structurehttps://ask.sagemath.org/question/47753/implement-algebras-with-some-extra-structure/I seem not to be understanding the way to implement categories with extra structure. Suppose I want to implement the category `Cs` of pairs `(A,S)` where `A` is a `QQ`-algebra and `S` is a linear endomorphism of `A` with the obvious morphisms. `Algebras(QQ)` is a full subcategory of `Cs` by adding the zero endormophism. And we also have the forgetful functor from `Cs` to `Algebras(QQ)` which consists to simply forget `S`.
Now from reading the examples in
https://doc.sagemath.org/html/en/reference/categories/index.html
or
https://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html
I could set up
<pre><code>class Cs(Category)
def super_categories(self):
return[Algebras(QQ)]
</code></pre>
And that woul d give me a canonical forgetful functor `Cs` -> `Algebras(QQ)`. Now my problem is when I want to create a parent of `Cs()` starting from a parent in `Algebras(QQ)`. That is, I could set up a new parent and elements which will have to implement the methods for `Algebras(QQ)`:
<pre><code>sage: from sage.misc.abstract_method import abstract_methods_of_class
sage: abstract_methods_of_class(Algebras(QQ).parent_class)
{'optional': ['algebra_generators'], 'required': ['__contains__']}
sage: abstract_methods_of_class(Algebras(QQ).element_class)
{'optional': ['_add_', '_mul_'], 'required': ['__nonzero__']}
</code></pre>
But instead of implementing those methods I would want to use the methods of the underlying parent of `Algebras(QQ)`. Something like a hypothetical `PolynomialRing(QQ, 'x', category=Cs())`. In other words I'm looking to implement the functor `A -> (A, 0)` above.
Finally I have similar concerns about implementing `Cs()` in the other two possible ways, namely as the subcategory of the category of arrows in `VectorSpaces(QQ)` with the same source and target an object from `Algebras(QQ)`, or as a super category of `Algebras(QQ)` using `_subcategory_hook_`. Always I get to the point where I don't know how to implement something like `PolynomialRing(QQ, category=Cs())`. What I am doing now is keeping a copy of the algebra `A` inside of an instance of `Cs.parent_class` and then implementing the element and parent mehods of `Algebras(QQ)` by pointing to the corresponding methods of `A` but that seems silly. I'd appreciate any pointer.Tue, 03 Sep 2019 21:05:43 +0200https://ask.sagemath.org/question/47753/implement-algebras-with-some-extra-structure/Answer by heluani for <p>I seem not to be understanding the way to implement categories with extra structure. Suppose I want to implement the category <code>Cs</code> of pairs <code>(A,S)</code> where <code>A</code> is a <code>QQ</code>-algebra and <code>S</code> is a linear endomorphism of <code>A</code> with the obvious morphisms. <code>Algebras(QQ)</code> is a full subcategory of <code>Cs</code> by adding the zero endormophism. And we also have the forgetful functor from <code>Cs</code> to <code>Algebras(QQ)</code> which consists to simply forget <code>S</code>. </p>
<p>Now from reading the examples in
<a href="https://doc.sagemath.org/html/en/reference/categories/index.html">https://doc.sagemath.org/html/en/refe...</a>
or
<a href="https://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html">https://doc.sagemath.org/html/en/them...</a>
I could set up </p>
<pre><code>class Cs(Category)
def super_categories(self):
return[Algebras(QQ)]
</code></pre>
<p>And that woul d give me a canonical forgetful functor <code>Cs</code> -> <code>Algebras(QQ)</code>. Now my problem is when I want to create a parent of <code>Cs()</code> starting from a parent in <code>Algebras(QQ)</code>. That is, I could set up a new parent and elements which will have to implement the methods for <code>Algebras(QQ)</code>:</p>
<pre><code>sage: from sage.misc.abstract_method import abstract_methods_of_class
sage: abstract_methods_of_class(Algebras(QQ).parent_class)
{'optional': ['algebra_generators'], 'required': ['__contains__']}
sage: abstract_methods_of_class(Algebras(QQ).element_class)
{'optional': ['_add_', '_mul_'], 'required': ['__nonzero__']}
</code></pre>
<p>But instead of implementing those methods I would want to use the methods of the underlying parent of <code>Algebras(QQ)</code>. Something like a hypothetical <code>PolynomialRing(QQ, 'x', category=Cs())</code>. In other words I'm looking to implement the functor <code>A -> (A, 0)</code> above. </p>
<p>Finally I have similar concerns about implementing <code>Cs()</code> in the other two possible ways, namely as the subcategory of the category of arrows in <code>VectorSpaces(QQ)</code> with the same source and target an object from <code>Algebras(QQ)</code>, or as a super category of <code>Algebras(QQ)</code> using <code>_subcategory_hook_</code>. Always I get to the point where I don't know how to implement something like <code>PolynomialRing(QQ, category=Cs())</code>. What I am doing now is keeping a copy of the algebra <code>A</code> inside of an instance of <code>Cs.parent_class</code> and then implementing the element and parent mehods of <code>Algebras(QQ)</code> by pointing to the corresponding methods of <code>A</code> but that seems silly. I'd appreciate any pointer.</p>
https://ask.sagemath.org/question/47753/implement-algebras-with-some-extra-structure/?answer=47775#post-id-47775What I found was the easiest is to define my parents as inheriting from the corresponding objects from `Algebras(QQ)`, and then reset the category using `Parent`'s methods since I am guaranteed to have a subcategory. For example
<pre><code> def CParent (MPolynomialRing_libsingular)
def __init__(self, n, names, order)
self._somedata = somevalue
super(CParent, self).__init__(QQ, n, names, order)
self._unset_category()
self._init_category_(Cs)
</code></pre>
This way I don't even need to implement an element class and I can focus on morphisms which is the only thing that changes in this category. Fri, 06 Sep 2019 11:43:55 +0200https://ask.sagemath.org/question/47753/implement-algebras-with-some-extra-structure/?answer=47775#post-id-47775