ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 03 Sep 2019 14:05:43 -0500implement algebras with some extra structurehttp://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.heluaniTue, 03 Sep 2019 14:05:43 -0500http://ask.sagemath.org/question/47753/is an object of a subcategory an object of the ambient category?http://ask.sagemath.org/question/47724/is-an-object-of-a-subcategory-an-object-of-the-ambient-category/I have defined a category `Cs()` and its subcategory `Ds()` as follows:
<pre><code>
class Cs(Category_over_base_ring):
...
def __init__(self, K):
Category_over_base_ring.__init__(self, K)
def super_categories(self):
return [ GradedAlgebras(self.base_field()) ]
class Ds(CategoryWithAxiom):
_base_category_class_and_axiom = (Cs, 'Commutative')
Cs.Commutative = Ds
</code></pre>
The omitted code I think its irrelevant. I can check that `Ds()` is a subcategory of `Cs()`:
<pre><code>
sage: C = Cs(QQ)
sage: D = Ds(QQ)
sage: D.is_subcategory(C)
True
sage:
</code></pre>
Now I create a parent of `Ds()` as follows:
<pre><code>
class DParent (Parent,UniqueRepresentation):
def __init__(self,K, *args, **kwds ):
if not (K in Fields() or \
(isinstance(K, Category) and K.is_subcategory(Fields()))):
raise ValueError(
"base must be a field or a subcategory of Fields(); got {}".format(K))
self._base_algebra = PolynomialRing(K,*args,**kwds)
super(DParent,self).__init__(self,
category=Ds(K))
...
</code></pre>
And I can check that these parents are objects of `Ds()` but not of `Cs()`:
<pre><code>
sage: J = DParent(QQ, 1, 'x', order=TermOrder('wdeglex', (2,)))
sage: J in C
False
sage: J in D
True
</code></pre>
Where can I be making a mistake?
**Addition:** I am looking at the implementation in sage/categories/category.py and I am more puzzled:
<pre><code>sage: J.category().is_subcategory(C)
True
sage: C.__contains__(J)
False
</code></pre>
But at least I get
<pre><code>
sage: c = J.categories()
sage: any(isinstance(cat,C.subcategory_class) for cat in c)
True
sage: C.__classcontains__(C.subcategory_class,J)
True
sage:
</code></pre>heluaniMon, 02 Sep 2019 11:22:25 -0500http://ask.sagemath.org/question/47724/Power series with coeffs in SymmetricFunctions()http://ask.sagemath.org/question/37460/power-series-with-coeffs-in-symmetricfunctions/The following gives an error:
sym = SymmetricFunctions(QQ)
R.<t> = PowerSeriesRing(sym)
saying that base_ring is not a commutative ring. I can check sym.categories(), and indeed it is not, but I think it should be. Is there an easy way to make this work?
Thanks!paragonFri, 28 Apr 2017 15:42:00 -0500http://ask.sagemath.org/question/37460/RingMorphism defined by a python functionhttp://ask.sagemath.org/question/10795/ringmorphism-defined-by-a-python-function/Is there a way to define a `RingHomomorphism` object that calls a user defined function? I know this is possible for `SetMorphism`, but I can't find a way to do the same for rings.
My use case: I have a python (or cython) function defining an embedding of a (finite) field into another. Of course, I could evaluate it on the generators of the domain and create a `RingHomomorphism_im_gens` object, but my function is much faster to evaluate.
LucaMon, 02 Dec 2013 11:00:15 -0600http://ask.sagemath.org/question/10795/Partial Algebrashttp://ask.sagemath.org/question/8022/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?
StefanThu, 24 Mar 2011 22:54:01 -0500http://ask.sagemath.org/question/8022/