1 | initial version |
DobrĂ½ den,
There is no direct way to do it. The question is what properties of the set do you want to use? Here is a simple class that more or less do what you wanted
from sage.misc.mrange import cantor_product
from sage.structure.parent import Parent
from sage.rings.qqbar import AA
class RealZZSubmodule(Parent):
def __init__(self, elts):
self._elts = [AA(x) for x in elts]
Parent.__init__(self, category=(InfiniteEnumeratedSets(), Modules(ZZ)), facade=(AA,))
def _repr_(self):
return "ZZ-module generated by {}".format(self._elts)
def an_element(self):
return self._elts[0]
def _element_constructor_(self, arg):
if isinstance(arg, (tuple,list)):
assert len(arg) == len(self._elts)
return sum(c*e for c,e in zip(arg, self._elts))
elif arg in AA:
raise NotImplementedError("please do it?")
def __iter__(self):
for i in cantor_product(ZZ, repeat=len(self._elts)):
yield self(i)
With it you can do the following
sage: R = RealZZSubmodule([1,sqrt2])
sage: R.cardinality()
+Infinity
sage: R
ZZ-module generated by [1, 1.414213562373095?]
sage: it = iter(R)
sage: for _ in range(10):
....: print next(it)
0
1
1.414213562373095?
-1
2.414213562373095?
-1.414213562373095?
2
0.4142135623730951?
-0.4142135623730951?
2.828427124746190?
In order to make it answers less trivial questions you need to implement more methods...
2 | No.2 Revision |
DobrĂ½ den,
There is no direct way to do it. The question is what properties of the set do you want to use? Here is a simple class that more or less do what you wanted
from sage.misc.mrange import cantor_product
from sage.structure.parent import Parent
from sage.rings.qqbar import AA
class RealZZSubmodule(Parent):
def __init__(self, elts):
self._elts = [AA(x) for x in elts]
Parent.__init__(self, category=(InfiniteEnumeratedSets(), Modules(ZZ)), facade=(AA,))
def _repr_(self):
return "ZZ-module generated by {}".format(self._elts)
def an_element(self):
return self._elts[0]
def _element_constructor_(self, arg):
if isinstance(arg, (tuple,list)):
assert len(arg) == len(self._elts)
return sum(c*e for c,e in zip(arg, self._elts))
elif arg in AA:
raise NotImplementedError("please do it?")
def __iter__(self):
for i in cantor_product(ZZ, repeat=len(self._elts)):
yield self(i)
With it you can do the following
sage: sqrt2 = AA(2).sqrt()
sage: R = RealZZSubmodule([1,sqrt2])
sage: R.cardinality()
+Infinity
sage: R
ZZ-module generated by [1, 1.414213562373095?]
sage: R((1,0))
1
sage: R((0,1))
1.414213562373095?
sage: R((8, -5))
0.9289321881345248?
sage: it = iter(R)
sage: for _ in range(10):
....: print next(it)
0
1
1.414213562373095?
-1
2.414213562373095?
-1.414213562373095?
2
0.4142135623730951?
-0.4142135623730951?
2.828427124746190?
In order to make it answers less trivial questions you need to implement more methods...