Hello Antoine, I'm also relatively new to Sage development, but one thing I can think of is that you could create a constructor module for your implementation of Drinfeld module. In short, the way I see it is that you would have a new module named "constructor" with a class "constructor.FiniteDrinfeldModule" which would be available to the end user. This "constructor class" would take care of formatting the user input and would return a "FiniteDrinfeldModule_rank_two" or a "FiniteDrinfeldModule_generic" depending of the input. This would also give you the opportunity of giving some liberty to the user when creating a Drinfeld module (via a list, an additive polynomial, etc...)
I don't know if this would be the most efficient way, but I've seen such idea for multiple different implementation inside SageMath. Any input from a more experienced Sage developer would be welcome here. Best, David A. Le mardi 7 juin 2022 à 11:41:38 UTC-4, John H Palmieri a écrit : > Isn't this because you're using CachedRepresentation? From the > documentation > <https://doc.sagemath.org/html/en/reference/structure/sage/structure/unique_representation.html> > : > > Instances of a class have a *cached representation behavior* when several > instances constructed with the same arguments share the same memory > representation. > > On Tuesday, June 7, 2022 at 6:18:50 AM UTC-7 antoine....@gmail.com wrote: > >> I thank all of you for your answers. And sorry for taking a bit long to >> reply! >> >> After many trys and errors, here is something that works for me: >> >> ``` >> >> from sage.structure.sage_object import SageObject >> from sage.structure.unique_representation import CachedRepresentation >> >> class MyInteger(CachedRepresentation, SageObject): >> >> @staticmethod >> def __classcall_private__(cls, n): >> if n % 2 == 0: >> return MyInteger_even(n) >> else: >> return cls.__classcall__(cls, n) >> >> def __init__(self, n): >> self.n = n >> >> >> class MyInteger_even(MyInteger): >> >> def __init__(self, n): >> self.n = n >> self.half = n // 2 >> ``` >> >> However, this has a major drawback, as all instances that have same data >> are >> references to one another. And I don't find any way to bypass that: >> >> ``` >> >> sage: from copy import deepcopy >> sage: s1 = MyInteger(2) >> sage: s2 = MyInteger(2) >> sage: s3 = deepcopy(s2) >> sage: s1 == s2 >> True >> sage: s1 is s2 >> True >> sage: s1 == s3 >> True >> sage: s1 is s3 >> True >> sage: s1.n >> 2 >> sage: s3.n = 5 >> sage: s1.n >> 5 >> sage: >> ``` >> >> I appreciate that this may not be a problem for classes representing sets >> and >> categories (like `EuclideanSpace`). But for classes representing children >> elements, this may cause problems. And this is the case for me, as I need >> this >> for `FiniteDrinfeldModule` and `FiniteDrinfeldModule_rank_two` >> (https://trac.sagemath.org/ticket/33713). >> >> Is there a way to avoid this? >> >> Kindest regards, >> Antoine >> >> On Sat, 2022-05-14 at 21:07 -0700, Nils Bruin wrote: >> > It's probably worth pointing out that __classcall_private__ is not a >> standard >> > python facility. It looks like you need >> > >> > class Foo(metaclass=ClasscallMetaclass): >> > >> > to make it work on Foo. >> > >> > On Saturday, 14 May 2022 at 20:21:48 UTC-7 Travis Scrimshaw wrote: >> > > For this you want to use __classcall_private__ as otherwise you would >> likely >> > > end up in an infinite loop when you try to construct the subclass. >> There are >> > > lots of examples of this in the Sage library code. >> > > >> > > Best, >> > > Travis >> > > >> > > >> > > On Sunday, May 15, 2022 at 5:42:49 AM UTC+9 Eric Gourgoulhon wrote: >> > > > Hi, >> > > > >> > > > Le samedi 14 mai 2022 à 00:07:02 UTC+2, David Roe a écrit : >> > > > > I think the following should work: >> > > > > >> > > > > class MyObject: >> > > > > def __classcall__(cls, arg): >> > > > > if isinstance(arg, special): >> > > > > return typecall(MyObject_specific_case, arg) >> > > > > else: >> > > > > return typecall(MyObject, arg) >> > > > > >> > > > > plus the same __init__ you had before. I haven't checked it >> though.... >> > > > > David >> > > > > >> > > > > >> > > > >> > > > >> > > > An alternative is to use __classcall_private__ >> > > > For an example, see the class EuclideanSpace in >> > > > src/sage/manifolds/differentiable/examples/euclidean.py >> > > > >> > > > EuclideanSpace(n) actually returns an instance of the subclass >> > > > EuclideanPlane if n = 2 or of the subclass Euclidean3dimSpace if n >> = 3. >> > > > >> > > > Best wishes, >> > > > >> > > > Eric. >> > -- >> > You received this message because you are subscribed to a topic in the >> Google >> > Groups "sage-devel" group. >> > To unsubscribe from this topic, visit >> > https://groups.google.com/d/topic/sage-devel/PaUReuoxEXI/unsubscribe. >> > To unsubscribe from this group and all its topics, send an email to >> > sage-devel+...@googlegroups.com. >> > To view this discussion on the web visit >> > >> https://groups.google.com/d/msgid/sage-devel/a8bc696f-7887-41b2-8fe9-af3ac055eb71n%40googlegroups.com >> >> > . >> >> >> -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/f3e77e87-29fe-4d8a-b666-44716bf998d5n%40googlegroups.com.