-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
hyperbolic_model.py
1494 lines (1180 loc) · 47.5 KB
/
hyperbolic_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
r"""
Hyperbolic Models
In this module, a hyperbolic model is a collection of data that allow
the user to implement new models of hyperbolic space with minimal effort.
The data include facts about the underlying set (such as whether the
model is bounded), facts about the metric (such as whether the model is
conformal), facts about the isometry group (such as whether it is a
linear or projective group), and more. Generally speaking, any data
or method that pertains to the model itself -- rather than the points,
geodesics, or isometries of the model -- is implemented in this module.
Abstractly, a model of hyperbolic space is a connected, simply connected
manifold equipped with a complete Riemannian metric of constant curvature
`-1`. This module records information sufficient to enable computations
in hyperbolic space without explicitly specifying the underlying set or
its Riemannian metric. Although, see the
`SageManifolds <http://sagemanifolds.obspm.fr/>`_ project if
you would like to take this approach.
This module implements the abstract base class for a model of hyperbolic
space of arbitrary dimension. It also contains the implementations of
specific models of hyperbolic geometry.
AUTHORS:
- Greg Laun (2013): Initial version.
EXAMPLES:
We illustrate how the classes in this module encode data by comparing
the upper half plane (UHP), Poincaré disk (PD) and hyperboloid (HM)
models. First we create::
sage: U = HyperbolicPlane().UHP()
sage: P = HyperbolicPlane().PD()
sage: H = HyperbolicPlane().HM()
We note that the UHP and PD models are bounded while the HM model is
not::
sage: U.is_bounded() and P.is_bounded()
True
sage: H.is_bounded()
False
The isometry groups of UHP and PD are projective, while that of HM is
linear::
sage: U.is_isometry_group_projective()
True
sage: H.is_isometry_group_projective()
False
The models are responsible for determining if the coordinates of points
and the matrix of linear maps are appropriate for constructing points
and isometries in hyperbolic space::
sage: U.point_in_model(2 + I)
True
sage: U.point_in_model(2 - I)
False
sage: U.point_in_model(2)
False
sage: U.boundary_point_in_model(2)
True
"""
#***********************************************************************
#
# Copyright (C) 2013 Greg Laun <glaun@math.umd.edu>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#***********************************************************************
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.parent import Parent
from sage.misc.bindable_class import BindableClass
from sage.misc.lazy_import import lazy_import
from sage.functions.other import imag, real
from sage.misc.functional import sqrt
from sage.functions.all import arccosh
from sage.rings.cc import CC
from sage.rings.real_double import RDF
from sage.rings.real_mpfr import RR
from sage.rings.infinity import infinity
from sage.symbolic.constants import I
from sage.matrix.constructor import matrix
from sage.categories.homset import Hom
from sage.geometry.hyperbolic_space.hyperbolic_constants import EPSILON, LORENTZ_GRAM
from sage.geometry.hyperbolic_space.hyperbolic_point import (
HyperbolicPoint, HyperbolicPointUHP)
from sage.geometry.hyperbolic_space.hyperbolic_isometry import (
HyperbolicIsometry, HyperbolicIsometryUHP,
HyperbolicIsometryPD, HyperbolicIsometryKM, moebius_transform)
from sage.geometry.hyperbolic_space.hyperbolic_geodesic import (
HyperbolicGeodesic, HyperbolicGeodesicUHP, HyperbolicGeodesicPD,
HyperbolicGeodesicKM, HyperbolicGeodesicHM)
from sage.geometry.hyperbolic_space.hyperbolic_coercion import (
CoercionUHPtoPD, CoercionUHPtoKM, CoercionUHPtoHM,
CoercionPDtoUHP, CoercionPDtoKM, CoercionPDtoHM,
CoercionKMtoUHP, CoercionKMtoPD, CoercionKMtoHM,
CoercionHMtoUHP, CoercionHMtoPD, CoercionHMtoKM)
lazy_import('sage.modules.free_module_element', 'vector')
#####################################################################
## Abstract model
class HyperbolicModel(Parent, UniqueRepresentation, BindableClass):
r"""
Abstract base class for hyperbolic models.
"""
Element = HyperbolicPoint
_Geodesic = HyperbolicGeodesic
_Isometry = HyperbolicIsometry
def __init__(self, space, name, short_name, bounded, conformal,
dimension, isometry_group, isometry_group_is_projective):
"""
Initialize ``self``.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: TestSuite(UHP).run()
sage: PD = HyperbolicPlane().PD()
sage: TestSuite(PD).run()
sage: KM = HyperbolicPlane().KM()
sage: TestSuite(KM).run()
sage: HM = HyperbolicPlane().HM()
sage: TestSuite(HM).run()
"""
self._name = name
self._short_name = short_name
self._bounded = bounded
self._conformal = conformal
self._dimension = dimension
self._isometry_group = isometry_group
self._isometry_group_is_projective = isometry_group_is_projective
from sage.geometry.hyperbolic_space.hyperbolic_interface import HyperbolicModels
Parent.__init__(self, category=HyperbolicModels(space))
def _repr_(self): # Abstract
"""
Return a string representation of ``self``.
EXAMPLES::
sage: HyperbolicPlane().UHP()
Hyperbolic plane in the Upper Half Plane Model
"""
return u'Hyperbolic plane in the {}'.format(self._name)
def _element_constructor_(self, x, is_boundary=None, **graphics_options): #Abstract
"""
Construct an element of ``self``.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP(2 + I)
Point in UHP I + 2
"""
return self.get_point(x, is_boundary, **graphics_options)
def name(self): # Abstract
"""
Return the name of this model.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP.name()
'Upper Half Plane Model'
"""
return self._name
def short_name(self):
"""
Return the short name of this model.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP.short_name()
'UHP'
"""
return self._short_name
def is_bounded(self):
"""
Return ``True`` if ``self`` is a bounded model.
EXAMPLES::
sage: HyperbolicPlane().UHP().is_bounded()
True
sage: HyperbolicPlane().PD().is_bounded()
True
sage: HyperbolicPlane().KM().is_bounded()
True
sage: HyperbolicPlane().HM().is_bounded()
False
"""
return self._bounded
def is_conformal(self):
"""
Return ``True`` if ``self`` is a conformal model.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP.is_conformal()
True
"""
return self._conformal
def is_isometry_group_projective(self):
"""
Return ``True`` if the isometry group of ``self`` is projective.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP.is_isometry_group_projective()
True
"""
return self._isometry_group_is_projective
def point_in_model(self, p):
r"""
Return ``True`` if the point ``p`` is in the interior of the
given model and ``False`` otherwise.
INPUT:
- any object that can converted into a complex number
OUTPUT:
- boolean
EXAMPLES::
sage: HyperbolicPlane().UHP().point_in_model(I)
True
sage: HyperbolicPlane().UHP().point_in_model(-I)
False
"""
return True
def point_test(self, p): #Abstract
r"""
Test whether a point is in the model. If the point is in the
model, do nothing. Otherwise, raise a ``ValueError``.
EXAMPLES::
sage: from sage.geometry.hyperbolic_space.hyperbolic_model import HyperbolicModelUHP
sage: HyperbolicPlane().UHP().point_test(2 + I)
sage: HyperbolicPlane().UHP().point_test(2 - I)
Traceback (most recent call last):
...
ValueError: -I + 2 is not a valid point in the UHP model
"""
if not (self.point_in_model(p) or self.boundary_point_in_model(p)):
error_string = "{0} is not a valid point in the {1} model"
raise ValueError(error_string.format(p, self._short_name))
def boundary_point_in_model(self, p): #Abstract
r"""
Return ``True`` if the point is on the ideal boundary of hyperbolic
space and ``False`` otherwise.
INPUT:
- any object that can converted into a complex number
OUTPUT:
- boolean
EXAMPLES::
sage: HyperbolicPlane().UHP().boundary_point_in_model(I)
False
"""
return True
def bdry_point_test(self, p): #Abstract
r"""
Test whether a point is in the model. If the point is in the
model, do nothing; otherwise raise a ``ValueError``.
EXAMPLES::
sage: HyperbolicPlane().UHP().bdry_point_test(2)
sage: HyperbolicPlane().UHP().bdry_point_test(1 + I)
Traceback (most recent call last):
...
ValueError: I + 1 is not a valid boundary point in the UHP model
"""
if not self._bounded or not self.boundary_point_in_model(p):
error_string = "{0} is not a valid boundary point in the {1} model"
raise ValueError(error_string.format(p, self._short_name))
def isometry_in_model(self, A): #Abstract
r"""
Return ``True`` if the input matrix represents an isometry of the
given model and ``False`` otherwise.
INPUT:
- a matrix that represents an isometry in the appropriate model
OUTPUT:
- boolean
EXAMPLES::
sage: HyperbolicPlane().UHP().isometry_in_model(identity_matrix(2))
True
sage: HyperbolicPlane().UHP().isometry_in_model(identity_matrix(3))
False
"""
return True
def isometry_test(self, A): #Abstract
r"""
Test whether an isometry ``A`` is in the model.
If the isometry is in the model, do nothing. Otherwise, raise
a ``ValueError``.
EXAMPLES::
sage: HyperbolicPlane().UHP().isometry_test(identity_matrix(2))
sage: HyperbolicPlane().UHP().isometry_test(matrix(2, [I,1,2,1]))
Traceback (most recent call last):
...
ValueError:
[I 1]
[2 1] is not a valid isometry in the UHP model
"""
if not self.isometry_in_model(A):
error_string = "\n{0} is not a valid isometry in the {1} model"
raise ValueError(error_string.format(A, self._short_name))
def get_point(self, coordinates, is_boundary=None, **graphics_options):
r"""
Return a point in ``self``.
Automatically determine the type of point to return given either:
#. the coordinates of a point in the interior or ideal boundary
of hyperbolic space, or
#. a :class:`~sage.geometry.hyperbolic_space.hyperbolic_point.HyperbolicPoint` object.
INPUT:
- a point in hyperbolic space or on the ideal boundary
OUTPUT:
- a :class:`~sage.geometry.hyperbolic_space.hyperbolic_point.HyperbolicPoint`
EXAMPLES:
We can create an interior point via the coordinates::
sage: HyperbolicPlane().UHP().get_point(2*I)
Point in UHP 2*I
Or we can create a boundary point via the coordinates::
sage: HyperbolicPlane().UHP().get_point(23)
Boundary point in UHP 23
However we cannot create points outside of our model::
sage: HyperbolicPlane().UHP().get_point(12 - I)
Traceback (most recent call last):
...
ValueError: -I + 12 is not a valid point in the UHP model
::
sage: HyperbolicPlane().UHP().get_point(2 + 3*I)
Point in UHP 3*I + 2
sage: HyperbolicPlane().PD().get_point(0)
Point in PD 0
sage: HyperbolicPlane().KM().get_point((0,0))
Point in KM (0, 0)
sage: HyperbolicPlane().HM().get_point((0,0,1))
Point in HM (0, 0, 1)
sage: p = HyperbolicPlane().UHP().get_point(I, color="red")
sage: p.graphics_options()
{'color': 'red'}
::
sage: HyperbolicPlane().UHP().get_point(12)
Boundary point in UHP 12
sage: HyperbolicPlane().UHP().get_point(infinity)
Boundary point in UHP +Infinity
sage: HyperbolicPlane().PD().get_point(I)
Boundary point in PD I
sage: HyperbolicPlane().KM().get_point((0,-1))
Boundary point in KM (0, -1)
"""
if isinstance(coordinates, HyperbolicPoint):
if coordinates.parent() is not self:
coordinates = self(coordinates)
coordinates.update_graphics(True, **graphics_options)
return coordinates #both Point and BdryPoint
if is_boundary is None:
is_boundary = self.boundary_point_in_model(coordinates)
return self.element_class(self, coordinates, is_boundary, **graphics_options)
def get_geodesic(self, start, end=None, **graphics_options): #Abstract
r"""
Return a geodesic in the appropriate model.
EXAMPLES::
sage: HyperbolicPlane().UHP().get_geodesic(I, 2*I)
Geodesic in UHP from I to 2*I
sage: HyperbolicPlane().PD().get_geodesic(0, I/2)
Geodesic in PD from 0 to 1/2*I
sage: HyperbolicPlane().KM().get_geodesic((1/2, 1/2), (0,0))
Geodesic in KM from (1/2, 1/2) to (0, 0)
sage: HyperbolicPlane().HM().get_geodesic((0,0,1), (1,0, sqrt(2)))
Geodesic in HM from (0, 0, 1) to (1, 0, sqrt(2))
TESTS::
sage: UHP = HyperbolicPlane().UHP()
sage: g = UHP.get_geodesic(UHP.get_point(I), UHP.get_point(2 + I))
sage: h = UHP.get_geodesic(I, 2 + I)
sage: g == h
True
"""
if end is None:
if isinstance(start, HyperbolicGeodesic):
G = start
if G.model() is not self:
G = G.to_model(self)
G.update_graphics(True, **graphics_options)
return G
raise ValueError("the start and end points must be specified")
return self._Geodesic(self, self(start), self(end), **graphics_options)
def get_isometry(self, A):
r"""
Return an isometry in ``self`` from the matrix ``A`` in the
isometry group of ``self``.
EXAMPLES::
sage: HyperbolicPlane().UHP().get_isometry(identity_matrix(2))
Isometry in UHP
[1 0]
[0 1]
sage: HyperbolicPlane().PD().get_isometry(identity_matrix(2))
Isometry in PD
[1 0]
[0 1]
sage: HyperbolicPlane().KM().get_isometry(identity_matrix(3))
Isometry in KM
[1 0 0]
[0 1 0]
[0 0 1]
sage: HyperbolicPlane().HM().get_isometry(identity_matrix(3))
Isometry in HM
[1 0 0]
[0 1 0]
[0 0 1]
"""
if isinstance(A, HyperbolicIsometry):
if A.model() is not self:
return A.to_model(self)
return A
return self._Isometry(self, A)
def random_element(self, **kwargs):
r"""
Return a random point in ``self``.
The points are uniformly distributed over the rectangle
`[-10, 10] \times [0, 10 i]` in the upper half plane model.
EXAMPLES::
sage: p = HyperbolicPlane().UHP().random_element()
sage: bool((p.coordinates().imag()) > 0)
True
sage: p = HyperbolicPlane().PD().random_element()
sage: HyperbolicPlane().PD().point_in_model(p.coordinates())
True
sage: p = HyperbolicPlane().KM().random_element()
sage: HyperbolicPlane().KM().point_in_model(p.coordinates())
True
sage: p = HyperbolicPlane().HM().random_element().coordinates()
sage: bool((p[0]**2 + p[1]**2 - p[2]**2 - 1) < 10**-8)
True
"""
return self.random_point(**kwargs)
def random_point(self, **kwargs):
r"""
Return a random point of ``self``.
The points are uniformly distributed over the rectangle
`[-10, 10] \times [0, 10 i]` in the upper half plane model.
EXAMPLES::
sage: p = HyperbolicPlane().UHP().random_point()
sage: bool((p.coordinates().imag()) > 0)
True
sage: PD = HyperbolicPlane().PD()
sage: p = PD.random_point()
sage: PD.point_in_model(p.coordinates())
True
"""
R = self.realization_of().a_realization()
return self(R.random_point(**kwargs))
def random_geodesic(self, **kwargs):
r"""
Return a random hyperbolic geodesic.
Return the geodesic between two random points.
EXAMPLES::
sage: h = HyperbolicPlane().PD().random_geodesic()
sage: all( e.coordinates().abs() <= 1 for e in h.endpoints() )
True
"""
R = self.realization_of().a_realization()
g_ends = [R.random_point(**kwargs) for k in range(2)]
return self.get_geodesic(self(g_ends[0]), self(g_ends[1]))
def random_isometry(self, preserve_orientation=True, **kwargs):
r"""
Return a random isometry in the model of ``self``.
INPUT:
- ``preserve_orientation`` -- if ``True`` return an
orientation-preserving isometry
OUTPUT:
- a hyperbolic isometry
EXAMPLES::
sage: A = HyperbolicPlane().PD().random_isometry()
sage: A.preserves_orientation()
True
sage: B = HyperbolicPlane().PD().random_isometry(preserve_orientation=False)
sage: B.preserves_orientation()
False
"""
R = self.realization_of().a_realization()
A = R.random_isometry(preserve_orientation, **kwargs)
return A.to_model(self)
################
# Dist methods #
################
def dist(self, a, b):
r"""
Calculate the hyperbolic distance between ``a`` and ``b``.
INPUT:
- ``a``, ``b`` -- a point or geodesic
OUTPUT:
- the hyperbolic distance
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: p1 = UHP.get_point(5 + 7*I)
sage: p2 = UHP.get_point(1.0 + I)
sage: UHP.dist(p1, p2)
2.23230104635820
sage: PD = HyperbolicPlane().PD()
sage: p1 = PD.get_point(0)
sage: p2 = PD.get_point(I/2)
sage: PD.dist(p1, p2)
arccosh(5/3)
sage: UHP(p1).dist(UHP(p2))
arccosh(5/3)
sage: KM = HyperbolicPlane().KM()
sage: p1 = KM.get_point((0, 0))
sage: p2 = KM.get_point((1/2, 1/2))
sage: numerical_approx(KM.dist(p1, p2))
0.881373587019543
sage: HM = HyperbolicPlane().HM()
sage: p1 = HM.get_point((0,0,1))
sage: p2 = HM.get_point((1,0,sqrt(2)))
sage: numerical_approx(HM.dist(p1, p2))
0.881373587019543
Distance between a point and itself is 0::
sage: p = UHP.get_point(47 + I)
sage: UHP.dist(p, p)
0
Points on the boundary are infinitely far from interior points::
sage: UHP.get_point(3).dist(UHP.get_point(I))
+Infinity
TESTS::
sage: UHP.dist(UHP.get_point(I), UHP.get_point(2*I))
arccosh(5/4)
sage: UHP.dist(I, 2*I)
arccosh(5/4)
"""
def coords(x):
return self(x).coordinates()
if isinstance(a, HyperbolicGeodesic):
if isinstance(b, HyperbolicGeodesic):
if not a.is_parallel(b):
return 0
if a.is_ultra_parallel(b):
perp = a.common_perpendicular(b)
# Find where a and b intersect the common perp...
p = a.intersection(perp)[0]
q = b.intersection(perp)[0]
# ...and return their distance
return self._dist_points(coords(p), coords(q))
raise NotImplementedError("can only compute distance between"
" ultra-parallel and intersecting geodesics")
# If only one is a geodesic, make sure it's b to make things easier
a,b = b,a
if isinstance(b, HyperbolicGeodesic):
(p, q) = b.ideal_endpoints()
return self._dist_geod_point(coords(p), coords(q), coords(a))
return self._dist_points(coords(a), coords(b))
def _dist_points(self, p1, p2):
r"""
Compute the distance between two points.
INPUT:
- ``p1``, ``p2`` -- the coordinates of the points
EXAMPLES::
sage: HyperbolicPlane().PD()._dist_points(3/5*I, 0)
arccosh(17/8)
"""
R = self.realization_of().a_realization()
phi = R.coerce_map_from(self)
return R._dist_points(phi.image_coordinates(p1), phi.image_coordinates(p2))
def _dist_geod_point(self, start, end, p):
r"""
Return the hyperbolic distance from a given hyperbolic geodesic
and a hyperbolic point.
INPUT:
- ``start`` -- the start ideal point coordinates of the geodesic
- ``end`` -- the end ideal point coordinates of the geodesic
- ``p`` -- the coordinates of the point
OUTPUT:
- the hyperbolic distance
EXAMPLES::
sage: HyperbolicPlane().PD()._dist_geod_point(3/5*I + 4/5, I, 0)
arccosh(1/10*sqrt(5)*((sqrt(5) - 1)^2 + 4) + 1)
If `p` is a boundary point, the distance is infinity::
sage: HyperbolicPlane().PD()._dist_geod_point(3/5*I + 4/5, I, 12/13*I + 5/13)
+Infinity
"""
R = self.realization_of().a_realization()
assert R is not self
def phi(c):
return R.coerce_map_from(self).image_coordinates(c)
return R._dist_geod_point(phi(start), phi(end), phi(p))
####################
# Isometry methods #
####################
def isometry_from_fixed_points(self, repel, attract):
r"""
Given two fixed points ``repel`` and ``attract`` as hyperbolic
points return a hyperbolic isometry with ``repel`` as repelling
fixed point and ``attract`` as attracting fixed point.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: PD = HyperbolicPlane().PD()
sage: PD.isometry_from_fixed_points(-i, i)
Isometry in PD
[ 3/4 1/4*I]
[-1/4*I 3/4]
::
sage: p, q = PD.get_point(1/2 + I/2), PD.get_point(6/13 + 9/13*I)
sage: PD.isometry_from_fixed_points(p, q)
Traceback (most recent call last):
...
ValueError: fixed points of hyperbolic elements must be ideal
sage: p, q = PD.get_point(4/5 + 3/5*I), PD.get_point(-I)
sage: PD.isometry_from_fixed_points(p, q)
Isometry in PD
[ 1/6*I - 2/3 -1/3*I - 1/6]
[ 1/3*I - 1/6 -1/6*I - 2/3]
"""
R = self.realization_of().a_realization()
return R.isometry_from_fixed_points(R(self(repel)), R(self(attract))).to_model(self)
#####################################################################
## Upper half plane model
class HyperbolicModelUHP(HyperbolicModel):
r"""
Upper Half Plane model.
"""
Element = HyperbolicPointUHP
_Geodesic = HyperbolicGeodesicUHP
_Isometry = HyperbolicIsometryUHP
def __init__(self, space):
"""
Initialize ``self``.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: TestSuite(UHP).run()
"""
HyperbolicModel.__init__(self, space,
name="Upper Half Plane Model", short_name="UHP",
bounded=True, conformal=True, dimension=2,
isometry_group="PSL(2, \\RR)", isometry_group_is_projective=True)
def _coerce_map_from_(self, X):
"""
Return if there is a coercion map from ``X`` to ``self``.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP.has_coerce_map_from(HyperbolicPlane().PD())
True
sage: UHP.has_coerce_map_from(HyperbolicPlane().KM())
True
sage: UHP.has_coerce_map_from(HyperbolicPlane().HM())
True
sage: UHP.has_coerce_map_from(QQ)
False
"""
if isinstance(X, HyperbolicModelPD):
return CoercionPDtoUHP(Hom(X, self))
if isinstance(X, HyperbolicModelKM):
return CoercionKMtoUHP(Hom(X, self))
if isinstance(X, HyperbolicModelHM):
return CoercionHMtoUHP(Hom(X, self))
return super(HyperbolicModelUHP, self)._coerce_map_from_(X)
def point_in_model(self, p):
r"""
Check whether a complex number lies in the open upper half plane.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP.point_in_model(1 + I)
True
sage: UHP.point_in_model(infinity)
False
sage: UHP.point_in_model(CC(infinity))
False
sage: UHP.point_in_model(RR(infinity))
False
sage: UHP.point_in_model(1)
False
sage: UHP.point_in_model(12)
False
sage: UHP.point_in_model(1 - I)
False
sage: UHP.point_in_model(-2*I)
False
sage: UHP.point_in_model(I)
True
sage: UHP.point_in_model(0) # Not interior point
False
"""
if isinstance(p, HyperbolicPoint):
return p.is_boundary()
return bool(imag(CC(p)) > 0)
def boundary_point_in_model(self, p):
r"""
Check whether a complex number is a real number or ``\infty``.
In the ``UHP.model_name_name``, this is the ideal boundary of
hyperbolic space.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP.boundary_point_in_model(1 + I)
False
sage: UHP.boundary_point_in_model(infinity)
True
sage: UHP.boundary_point_in_model(CC(infinity))
True
sage: UHP.boundary_point_in_model(RR(infinity))
True
sage: UHP.boundary_point_in_model(1)
True
sage: UHP.boundary_point_in_model(12)
True
sage: UHP.boundary_point_in_model(1 - I)
False
sage: UHP.boundary_point_in_model(-2*I)
False
sage: UHP.boundary_point_in_model(0)
True
sage: UHP.boundary_point_in_model(I)
False
"""
if isinstance(p, HyperbolicPoint):
return p.is_boundary()
im = abs(imag(CC(p)).n())
return (im < EPSILON) or bool(p == infinity)
def isometry_in_model(self, A):
r"""
Check that ``A`` acts as an isometry on the upper half plane.
That is, ``A`` must be an invertible `2 \times 2` matrix with real
entries.
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: A = matrix(2,[1,2,3,4])
sage: UHP.isometry_in_model(A)
True
sage: B = matrix(2,[I,2,4,1])
sage: UHP.isometry_in_model(B)
False
An example of a matrix `A` such that `\det(A) \neq 1`, but the `A`
acts isometrically::
sage: C = matrix(2,[10,0,0,10])
sage: UHP.isometry_in_model(C)
True
"""
if isinstance(A, HyperbolicIsometry):
return True
return bool(A.ncols() == 2 and A.nrows() == 2 and
sum([k in RR for k in A.list()]) == 4 and
abs(A.det()) > -EPSILON)
def get_background_graphic(self, **bdry_options):
r"""
Return a graphic object that makes the model easier to visualize.
For the upper half space, the background object is the ideal boundary.
EXAMPLES::
sage: hp = HyperbolicPlane().UHP().get_background_graphic()
"""
from sage.plot.line import line
bd_min = bdry_options.get('bd_min', -5)
bd_max = bdry_options.get('bd_max', 5)
return line(((bd_min, 0), (bd_max, 0)), color='black')
################
# Dist methods #
################
def _dist_points(self, p1, p2):
r"""
Compute the distance between two points in the Upper Half Plane
using the hyperbolic metric.
INPUT:
- ``p1``, ``p2`` -- the coordinates of the points
EXAMPLES::
sage: HyperbolicPlane().UHP()._dist_points(4.0*I, I)
1.38629436111989
"""
num = (real(p2) - real(p1))**2 + (imag(p2) - imag(p1))**2
denom = 2 * imag(p1) * imag(p2)
if denom == 0:
return infinity
return arccosh(1 + num/denom)
def _dist_geod_point(self, start, end, p):
r"""
Return the hyperbolic distance from a given hyperbolic geodesic
and a hyperbolic point.
INPUT:
- ``start`` -- the start ideal point coordinates of the geodesic
- ``end`` -- the end ideal point coordinates of the geodesic
- ``p`` -- the coordinates of the point
OUTPUT:
- the hyperbolic distance
EXAMPLES::
sage: UHP = HyperbolicPlane().UHP()
sage: UHP._dist_geod_point(2, infinity, I)
arccosh(1/10*sqrt(5)*((sqrt(5) - 1)^2 + 4) + 1)
If `p` is a boundary point, the distance is infinity::
sage: HyperbolicPlane().UHP()._dist_geod_point(2, infinity, 5)
+Infinity
"""
# Here is the trick for computing distance to a geodesic:
# find an isometry mapping the geodesic to the geodesic between
# 0 and infinity (so corresponding to the line imag(z) = 0.
# then any complex number is r exp(i*theta) in polar coordinates.
# the mutual perpendicular between this point and imag(z) = 0
# intersects imag(z) = 0 at ri. So we calculate the distance
# between r exp(i*theta) and ri after we transform the original
# point.
if start + end != infinity:
# Not a straight line:
# Map the endpoints to 0 and infinity and the midpoint to 1.
T = HyperbolicGeodesicUHP._crossratio_matrix(start, (start + end)/2, end)
else:
# Is a straight line: