Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
241862 views
1
#################################################################################
2
#
3
# (c) Copyright 2010 William Stein
4
#
5
# This file is part of PSAGE
6
#
7
# PSAGE is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# PSAGE is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
#################################################################################
21
22
23
"""
24
The converter module defines code for converting between Sage objects
25
and MongoDB documents.
26
27
DBConverter is a class whose methods are for converting native Sage
28
objects into MongoDB documents (JSON-like dictionaries), and
29
conversely. In addition to the DBConverter, this module defines two
30
objects: db_converter and to_db. The first, db_converter, is simply
31
an instance of the DBConverter class. The second, to_db is the method
32
of the DBConverter class that converts a native Sage object to a
33
document. Conversion in the other direction, from document to Sage
34
object, must be done by explicitly calling a method of db_converter.
35
This is because no type codes are stored in the database document, in
36
order to not tie the database too tightly to Sage.
37
"""
38
39
class DBConverter:
40
def from_dirichlet_character(self, e):
41
zeta_order = int(e.parent().base_ring().zeta_order())
42
return {'modulus':int(e.modulus()),
43
'order':int(e.order()),
44
'even':e.is_even(),
45
'element':[int(a) for a in e.element()],
46
'zeta_order':zeta_order}
47
48
def to_dirichlet_character(self, character):
49
if character['order'] == 1:
50
from sage.all import trivial_character
51
return trivial_character(character['modulus'])
52
from sage.all import DirichletGroup, CyclotomicField, QQ
53
from sage.modular.dirichlet import DirichletCharacter
54
zeta_order = character['zeta_order']
55
R = QQ if zeta_order == 2 else CyclotomicField(zeta_order)
56
G = DirichletGroup(character['modulus'], R, zeta_order=zeta_order)
57
v = G.an_element().element().parent()(character['element'])
58
return DirichletCharacter(G, v)
59
60
def to_db(self, x):
61
from sage.modular.dirichlet import DirichletCharacter
62
from sage.all import Integer
63
if isinstance(x, DirichletCharacter):
64
return self.from_dirichlet_character(x)
65
elif isinstance(x, (Integer, int, long)):
66
return int(x)
67
elif x is None:
68
return x
69
raise NotImplementedError
70
71
db_converter = DBConverter()
72
to_db = db_converter.to_db
73
74
75