Recently, I’ve been trawling through source code (the documentation wasn’t too helpful) and figuring out the basic design principles for implementing new structures. I came across an official tutorial on this. I’ve followed it ok, but I question the content here about equality and containment. It claims the following:
If
P
is an instance of a custom class andx==P(x)
evaluates as True without raising any errors, thenx in P
returns True.
I don’t think this is correct, at least, not anymore. But I’m not sure what is correct. Consider the following:
V = MyCustomClassInheritingFromCombinatorialFreeModule(initialisation_data)
# V._element_class: MyCustomClassInheritingFrom(IndexedFreeModuleElement)
# V.category(): Graded algebras with basis over the rational field
two = V(ZZ(2))
t = QQ(2)
print(2 in V, 2 in QQ)
print(QQ(2) == V(QQ(2)), QQ(2) == V(2), QQ(2) in V, V(2) in V)
print(t == V(t), t == V(2), two == V(2))
print(two in V, t in V, t == two)
Outputs:
False True
True True False True
True True True
True False True
Here, V
is an instance of a custom class I’ve been implementing, inheriting from CombinatorialFreeModule
. It is defined over the rationals. I’ve established element constructor methods for it, and a custom element class; I’ve checked that it admits coercion from the rationals and the integers.
Notice that t==V(t)
returns True without any error, yet t in V
returns False. What? It’s probably not such a big deal that something like “2” isn’t automatically seen as an element of my algebra, but I would like to know what the updated correct statement is, given that this tutorial (the only thing I could find on the matter, I’m not sure what to search for) isn’t right.
What’s going on? If the tutorial’s remark is still correct, then I guess I’ll have to edit with more details, but I’d appreciate advice on what kind of thing might be going wrong. Like I said, V.has_coerce_map_from(QQ)
and so on, all return True
. My IDE is Visual Studio Code, working with Jupyter notebook and hooked up to a remote WSL system which has Sage installed. It’s interesting to note my IDE colourises in
as yellow here when I apply it to V
, but when I test print(1 in QQ)
, in
is now coloured purple (and it returns True, as expected).
I tested the tutorial’s source code on my system, and noted the membership protocol works perfectly there. Also, in
colourises correctly as purple for their class. Strange.
0