Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Adding p-adics to metric spaces and some cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Scrimshaw committed Oct 17, 2015
1 parent f8f5b93 commit 041a5d1
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 86 deletions.
10 changes: 10 additions & 0 deletions src/doc/en/reference/categories/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Individual Categories
sage/categories/coxeter_group_algebras
sage/categories/coxeter_groups
sage/categories/crystals
sage/categories/cw_complexes
sage/categories/discrete_valuation
sage/categories/distributive_magmas_and_additive_magmas
sage/categories/division_rings
Expand Down Expand Up @@ -98,6 +99,7 @@ Individual Categories
sage/categories/graded_hopf_algebras_with_basis
sage/categories/graded_modules
sage/categories/graded_modules_with_basis
sage/categories/graphs
sage/categories/group_algebras
sage/categories/groupoid
sage/categories/groups
Expand All @@ -109,10 +111,13 @@ Individual Categories
sage/categories/integral_domains
sage/categories/lattice_posets
sage/categories/left_modules
sage/categories/lie_groups
sage/categories/magmas
sage/categories/magmas_and_additive_magmas
sage/categories/magmatic_algebras
sage/categories/manifolds
sage/categories/matrix_algebras
sage/categories/metric_spaces
sage/categories/modular_abelian_varieties
sage/categories/modules
sage/categories/modules_with_basis
Expand All @@ -139,6 +144,8 @@ Individual Categories
sage/categories/sets_cat
sage/categories/sets_with_grading
sage/categories/sets_with_partial_maps
sage/categories/simplicial_complexes
sage/categories/topological_spaces
sage/categories/unique_factorization_domains
sage/categories/unital_algebras
sage/categories/vector_spaces
Expand Down Expand Up @@ -187,6 +194,7 @@ Examples of parents using categories
sage/categories/examples/commutative_additive_semigroups
sage/categories/examples/coxeter_groups
sage/categories/examples/crystals
sage/categories/examples/cw_complexes
sage/categories/examples/facade_sets
sage/categories/examples/finite_coxeter_groups
sage/categories/examples/finite_dimensional_algebras_with_basis
Expand All @@ -196,8 +204,10 @@ Examples of parents using categories
sage/categories/examples/finite_weyl_groups
sage/categories/examples/graded_connected_hopf_algebras_with_basis
sage/categories/examples/graded_modules_with_basis
sage/categories/examples/graphs
sage/categories/examples/hopf_algebras_with_basis
sage/categories/examples/infinite_enumerated_sets
sage/categories/examples/manifolds
sage/categories/examples/monoids
sage/categories/examples/posets
sage/categories/examples/semigroups_cython
Expand Down
53 changes: 50 additions & 3 deletions src/sage/categories/metric_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ class MetricSpaces(MetricSpacesCategory):
- `d(a, b) = 0` if and only if `a = b`.
A metric space is a set `S` with a distinguished metric.
.. RUBRIC:: Implementation
Objects in this category must implement either a ``dist`` on the parent
or the elements or ``metric`` on the parent; otherwise this will cause
an infinite recursion.
.. TODO::
- Implement a general geodesics class.
- Implement a category for metric unital additive magmas and
move the generic distance `d(a, b) = |a - b|` there.
- Incorperate the length of a geodesic as part of the default
distance cycle.
EXAMPLES::
sage: from sage.categories.metric_spaces import MetricSpaces
sage: C = MetricSpaces()
sage: C
Category of metric spaces
sage: TestSuite(C).run()
"""
def _repr_object_names(self):
"""
Expand Down Expand Up @@ -114,7 +136,7 @@ def _test_metric(self, **options):
for a in S:
for b in S:
d = dist(a, b)
if a is not b:
if a != b:
tester.assertGreater(d, 0)
else:
tester.assertEqual(d, 0)
Expand All @@ -132,9 +154,8 @@ def metric(self):
sage: m(p1, p2)
2.23230104635820
"""
return lambda a,b: self.dist(a, b)
return lambda a,b: a.dist(b)

@abstract_method
def dist(self, a, b):
"""
Return the distance between ``a`` and ``b`` in ``self``.
Expand All @@ -150,9 +171,35 @@ def dist(self, a, b):
sage: PD = HyperbolicPlane().PD()
sage: PD.dist(PD.get_point(0), PD.get_point(I/2))
arccosh(5/3)
TESTS::
sage: RR.dist(-1, pi)
4.14159265358979
sage: RDF.dist(1, -1/2)
1.5
sage: CC.dist(3, 2)
1.00000000000000
sage: CC.dist(-1, I)
1.41421356237310
sage: CDF.dist(-1, I)
1.4142135623730951
"""
return (self(a) - self(b)).abs()

class ElementMethods:
def abs(self):
"""
Return the absolute value of ``self``.
EXAMPLES::
sage: CC(I).abs()
1.00000000000000
"""
P = self.parent()
return P.metric()(self, P.zero())

def dist(self, b):
"""
Return the distance between ``self`` and ``other``.
Expand Down
13 changes: 0 additions & 13 deletions src/sage/rings/complex_double.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,6 @@ cdef class ComplexDoubleField_class(sage.rings.ring.Field):
return 561162115
#return hash(self.str())

def dist(self, a, b):
"""
Return the distance between ``a`` and ``b``.
EXAMPLES::
sage: CDF.dist(3, 2)
1.0
sage: CDF.dist(-1, I)
1.4142135623730951
"""
return (self(a) - self(b)).abs()

def characteristic(self):
"""
Return the characteristic of the complex double field, which is 0.
Expand Down
13 changes: 0 additions & 13 deletions src/sage/rings/complex_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,16 +745,3 @@ def _factor_univariate_polynomial(self, f):
from sage.structure.factorization import Factorization
return Factorization([(R(g).monic(),e) for g,e in zip(*F)], f.leading_coefficient())

def dist(self, a, b):
"""
Return the distance between ``a`` and ``b``.
EXAMPLES::
sage: CC.dist(3, 2)
1.00000000000000
sage: CC.dist(-1, I)
1.41421356237310
"""
return self(a - b).abs()

13 changes: 0 additions & 13 deletions src/sage/rings/integer_ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1250,19 +1250,6 @@ cdef class IntegerRing_class(PrincipalIdealDomain):
else:
raise ValueError("no nth root of unity in integer ring")

def dist(self, a, b):
"""
Return the distance between ``a`` and ``b``.
EXAMPLES::
sage: ZZ.dist(3, 2)
1
sage: ZZ.dist(-1, 1)
2
"""
return self(a - b).abs()

def parameter(self):
r"""
Return an integer of degree 1 for the Euclidean property of `\ZZ`,
Expand Down
1 change: 1 addition & 0 deletions src/sage/rings/padics/local_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, base, prec, names, element_class, category=None):
category = CompleteDiscreteValuationFields()
else:
category = CompleteDiscreteValuationRings()
category = category.Metric().Complete()
if default_category is not None:
category = check_default_category(default_category, category)
Parent.__init__(self, base, names=(names,), normalize=False, category=category, element_constructor=element_class)
Expand Down
12 changes: 7 additions & 5 deletions src/sage/rings/padics/padic_base_leaves.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __init__(self, p, prec, print_mode, names):
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)])
sage: R = ZpCR(3, 2)
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)])
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)]) # long time
sage: R = ZpCR(next_prime(10^60))
sage: TestSuite(R).run()
Expand Down Expand Up @@ -320,7 +320,7 @@ def __init__(self, p, prec, print_mode, names):
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)])
sage: R = ZpCA(3, 2)
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)])
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)]) # long time
sage: R = ZpCA(next_prime(10^60))
sage: TestSuite(R).run()
Expand Down Expand Up @@ -411,7 +411,7 @@ def __init__(self, p, prec, print_mode, names):
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)])
sage: R = ZpFM(3, 2)
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)])
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)]) # long time
sage: R = ZpFM(next_prime(10^60))
sage: TestSuite(R).run()
Expand Down Expand Up @@ -525,10 +525,12 @@ def __init__(self, p, prec, print_mode, names):
sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^10)], max_runs = 2^12) # long time
sage: R = Qp(3, 1)
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)])
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)]) # long time
sage: R = Qp(3, 2)
sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^9)])
sage: TestSuite(R).run(elements=[R.random_element() for i in range(3^9)],
....: skip="_test_metric") # Skip because too long
sage: R._test_metric(elements=[R.random_element() for i in range(3^3)])
sage: R = Qp(next_prime(10^60))
sage: TestSuite(R).run()
Expand Down
1 change: 1 addition & 0 deletions src/sage/rings/padics/padic_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, base, p, prec, print_mode, names, element_class, category=Non
category = Fields()
else:
category = PrincipalIdealDomains()
category = category.Metric().Complete()
LocalGeneric.__init__(self, base, prec, names, element_class, category)
self._printer = pAdicPrinter(self, print_mode)

Expand Down
13 changes: 0 additions & 13 deletions src/sage/rings/rational_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,19 +934,6 @@ def algebraic_closure(self):
from sage.rings.all import QQbar
return QQbar

def dist(self, a, b):
"""
Return the distance between ``a`` and ``b``.
EXAMPLES::
sage: QQ.dist(3, 2)
1
sage: QQ.dist(-1, 1/2)
3/2
"""
return self(a - b).abs()

def order(self):
r"""
Return the order of `\QQ` which is `\infty`.
Expand Down
13 changes: 0 additions & 13 deletions src/sage/rings/real_double.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,6 @@ cdef class RealDoubleField_class(Field):
return 0
return cmp(type(self), type(x))

def dist(self, a, b):
"""
Return the distance between ``a`` and ``b`` in ``self``.
EXAMPLES::
sage: RDF.dist(5, 1/2)
4.5
sage: RDF.dist(-1, 1/2)
1.5
"""
return (self(a) - self(b)).abs()

def construction(self):
r"""
Returns the functorial construction of ``self``, namely, completion of
Expand Down
13 changes: 0 additions & 13 deletions src/sage/rings/real_mpfr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -801,19 +801,6 @@ cdef class RealField_class(sage.rings.ring.Field):
"""
return self.complex_field()

def dist(self, a, b):
"""
Return the distance between ``a`` and ``b``.
EXAMPLES::
sage: RR.dist(3, 2)
1.00000000000000
sage: RR.dist(-1, pi)
4.14159265358979
"""
return self(a - b).abs()

def ngens(self):
"""
Return the number of generators.
Expand Down

0 comments on commit 041a5d1

Please sign in to comment.