-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquaternionic_regularization.py
More file actions
933 lines (737 loc) · 33 KB
/
quaternionic_regularization.py
File metadata and controls
933 lines (737 loc) · 33 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Quaternionic Regularization implementation for the three-body problem.
This module provides methods for quaternionic extension of the three-body problem,
regularization of binary collisions, continuation along quaternionic paths, and
analysis of quaternionic monodromy.
"""
import numpy as np
import scipy as sp
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from typing import Dict, List, Tuple, Optional, Union, Callable, Any
from sympy import solve, symbols
# Import local modules
from quaternion import Quaternion
class QuaternionicExtension:
"""
Class implementing the quaternionic extension of the three-body problem.
This class provides methods for embedding the three-body problem in quaternionic
space and analyzing the resulting dynamics.
"""
def __init__(self, masses: np.ndarray, G: float = 1.0):
"""
Initialize the quaternionic extension with the given masses.
Args:
masses: Array of three masses [m1, m2, m3]
G: Gravitational constant (default=1.0)
"""
if len(masses) != 3:
raise ValueError("Must provide exactly three masses")
self.masses = np.array(masses, dtype=float)
self.G = float(G)
self.m1, self.m2, self.m3 = self.masses
# Compute mass parameter sigma
self.sigma = (self.m1 * self.m2 + self.m2 * self.m3 + self.m3 * self.m1) / (self.m1 + self.m2 + self.m3)**2
# Identify if this is one of the exceptional mass ratios
self.exceptional_sigmas = {
"1/3": 1/3,
"2^3/3^3": 2**3/3**3,
"2/3^2": 2/3**2
}
# Find the closest exceptional sigma value
min_diff = float('inf')
self.closest_exceptional = None
for name, value in self.exceptional_sigmas.items():
diff = abs(self.sigma - value)
if diff < min_diff:
min_diff = diff
self.closest_exceptional = name
self.is_exceptional = min_diff < 1e-10
def state_to_quaternions(self, state: np.ndarray) -> List[Quaternion]:
"""
Convert a state vector to quaternionic representation.
Args:
state: State vector [r1, r2, r3, p1, p2, p3] where each r_i and p_i is a 3D vector
Returns:
List of quaternions [r1, r2, r3, p1, p2, p3] where each r_i and p_i is a pure quaternion
"""
quaternions = []
# Extract positions and momenta
r1 = state[0:3]
r2 = state[3:6]
r3 = state[6:9]
p1 = state[9:12]
p2 = state[12:15]
p3 = state[15:18]
# Convert to pure quaternions
quaternions.append(Quaternion.from_vector(r1)) # r1
quaternions.append(Quaternion.from_vector(r2)) # r2
quaternions.append(Quaternion.from_vector(r3)) # r3
quaternions.append(Quaternion.from_vector(p1)) # p1
quaternions.append(Quaternion.from_vector(p2)) # p2
quaternions.append(Quaternion.from_vector(p3)) # p3
return quaternions
def quaternions_to_state(self, quaternions: List[Quaternion]) -> np.ndarray:
"""
Convert quaternionic representation back to state vector.
Args:
quaternions: List of quaternions [r1, r2, r3, p1, p2, p3]
Returns:
State vector [r1, r2, r3, p1, p2, p3] where each r_i and p_i is a 3D vector
"""
state = np.zeros(18)
# Extract the vector parts of the quaternions
for i, q in enumerate(quaternions):
state[i*3:(i+1)*3] = q.vector_part()
return state
def quaternionic_hamiltonian(self, quat_state: List[Quaternion]) -> float:
"""
Compute the Hamiltonian in quaternionic representation.
Args:
quat_state: List of quaternions [r1, r2, r3, p1, p2, p3]
Returns:
The value of the Hamiltonian (total energy)
"""
r1, r2, r3, p1, p2, p3 = quat_state
# Compute kinetic energy
T = (p1 * p1.conjugate()).scalar_part() / (2 * self.m1)
T += (p2 * p2.conjugate()).scalar_part() / (2 * self.m2)
T += (p3 * p3.conjugate()).scalar_part() / (2 * self.m3)
# Compute potential energy
r12 = (r1 - r2).norm()
r23 = (r2 - r3).norm()
r31 = (r3 - r1).norm()
V = -self.G * (self.m1 * self.m2 / r12 + self.m2 * self.m3 / r23 + self.m3 * self.m1 / r31)
return T + V
def quaternionic_equations_of_motion(self, t: float, quat_state: List[Quaternion]) -> List[Quaternion]:
"""
Compute the quaternionic equations of motion.
Args:
t: Time (not used explicitly as the system is autonomous)
quat_state: List of quaternions [r1, r2, r3, p1, p2, p3]
Returns:
List of quaternionic derivatives [dr1/dt, dr2/dt, dr3/dt, dp1/dt, dp2/dt, dp3/dt]
"""
r1, r2, r3, p1, p2, p3 = quat_state
# Compute position derivatives (velocities)
dr1_dt = p1 / self.m1
dr2_dt = p2 / self.m2
dr3_dt = p3 / self.m3
# Compute momentum derivatives (forces)
# Calculate displacements
r12 = r2 - r1
r23 = r3 - r2
r31 = r1 - r3
r12_norm = r12.norm()
r23_norm = r23.norm()
r31_norm = r31.norm()
# Calculate forces
dp1_dt = self.G * (self.m1 * self.m2 / r12_norm**3 * r12 - self.m3 * self.m1 / r31_norm**3 * r31)
dp2_dt = self.G * (self.m2 * self.m3 / r23_norm**3 * r23 - self.m1 * self.m2 / r12_norm**3 * r12)
dp3_dt = self.G * (self.m3 * self.m1 / r31_norm**3 * r31 - self.m2 * self.m3 / r23_norm**3 * r23)
return [dr1_dt, dr2_dt, dr3_dt, dp1_dt, dp2_dt, dp3_dt]
def quaternionic_angular_momentum(self, quat_state: List[Quaternion]) -> Quaternion:
"""
Compute the quaternionic angular momentum.
Args:
quat_state: List of quaternions [r1, r2, r3, p1, p2, p3]
Returns:
The quaternionic angular momentum
"""
r1, r2, r3, p1, p2, p3 = quat_state
# Compute individual angular momenta
L1 = r1 * p1 - p1 * r1
L2 = r2 * p2 - p2 * r2
L3 = r3 * p3 - p3 * r3
# Total angular momentum
L_total = (L1 + L2 + L3) * 0.5 # Factor of 0.5 from quaternionic cross product
return L_total
def quaternionic_integrate(self, initial_state: np.ndarray, t_span: Tuple[float, float],
t_eval: Optional[np.ndarray] = None, **kwargs) -> Dict:
"""
Integrate the quaternionic equations of motion.
Args:
initial_state: Initial state vector [r1, r2, r3, p1, p2, p3]
t_span: Tuple of (t_start, t_end)
t_eval: Optional array of time points to evaluate the solution at
**kwargs: Additional arguments to pass to solve_ivp
Returns:
Dictionary with integration results
"""
# Convert initial state to quaternions
initial_quat_state = self.state_to_quaternions(initial_state)
# Flatten quaternions for scipy solver
def quat_to_array(quat_state):
flat_array = np.zeros(24)
for i, q in enumerate(quat_state):
flat_array[i*4:(i+1)*4] = q.to_array()
return flat_array
def array_to_quat(flat_array):
quat_state = []
for i in range(6):
q_array = flat_array[i*4:(i+1)*4]
quat_state.append(Quaternion.from_array(q_array))
return quat_state
# Wrapper for scipy solver
def quat_eom_wrapper(t, y):
quat_state = array_to_quat(y)
derivatives = self.quaternionic_equations_of_motion(t, quat_state)
return quat_to_array(derivatives)
# Integrate using scipy's solver
flat_initial = quat_to_array(initial_quat_state)
result = solve_ivp(
quat_eom_wrapper,
t_span,
flat_initial,
t_eval=t_eval,
**kwargs
)
# Process results
quat_states = []
for i in range(len(result.t)):
quat_state = array_to_quat(result.y[:, i])
quat_states.append(quat_state)
# Convert back to standard state vectors for comparison
states = np.zeros((len(result.t), 18))
for i, quat_state in enumerate(quat_states):
states[i] = self.quaternions_to_state(quat_state)
return {
"t": result.t,
"states": states,
"quat_states": quat_states,
"success": result.success,
"message": result.message
}
class QuaternionicRegularization:
"""
Class implementing quaternionic regularization methods for the three-body problem.
This class provides methods for regularizing binary collisions in the three-body
problem using quaternionic techniques.
"""
def __init__(self, quat_extension: QuaternionicExtension):
"""
Initialize the quaternionic regularization.
Args:
quat_extension: QuaternionicExtension instance
"""
self.qext = quat_extension
self.masses = quat_extension.masses
self.m1, self.m2, self.m3 = self.masses
self.G = quat_extension.G
self.sigma = quat_extension.sigma
self.is_exceptional = quat_extension.is_exceptional
self.closest_exceptional = quat_extension.closest_exceptional
def levi_civita_transform(self, quat_state: List[Quaternion],
collision_pair: Tuple[int, int]) -> Tuple[List[Quaternion], float]:
"""
Apply the quaternionic Levi-Civita transformation for a binary collision.
Args:
quat_state: List of quaternions [r1, r2, r3, p1, p2, p3]
collision_pair: Tuple (i, j) indicating the colliding bodies
Returns:
Tuple of (transformed quaternionic state, time scaling factor)
"""
i, j = collision_pair
if i > j:
i, j = j, i # Ensure i < j
if not (0 <= i < j <= 2):
raise ValueError(f"Invalid collision pair: {collision_pair}")
r1, r2, r3, p1, p2, p3 = quat_state
# Extract the colliding bodies
bodies = [r1, r2, r3]
momenta = [p1, p2, p3]
masses = [self.m1, self.m2, self.m3]
r_i = bodies[i]
r_j = bodies[j]
p_i = momenta[i]
p_j = momenta[j]
m_i = masses[i]
m_j = masses[j]
# Relative position and momentum
r_ij = r_j - r_i
p_ij = (m_j * p_j - m_i * p_i) / (m_i + m_j)
# Levi-Civita transformation: r_ij = q^2
# We need to find q such that q^2 = r_ij
# For a pure quaternion r_ij, we can find q as:
# q = sqrt(|r_ij|) * (r_ij / |r_ij|)^(1/2)
r_ij_norm = r_ij.norm()
if r_ij_norm < 1e-10:
# Handle the collision case
# Use a small displacement to avoid exact collision
r_ij = Quaternion(0, 1e-8, 0, 0)
r_ij_norm = r_ij.norm()
# Compute a quaternionic square root
# For a pure quaternion, this is the normalized vector times the square root of the norm
q = r_ij * (1 / r_ij_norm)
q = q.power(0.5) * np.sqrt(r_ij_norm)
# Time transformation: dt = |r_ij| * ds = |q|^2 * ds
time_scaling = q.norm_squared()
# Transform momentum: p_q = (1/2) * q^* * p_ij * q^(-1)
# For pure quaternions, this simplifies
p_q = 0.5 * q.conjugate() * p_ij * q.inverse()
# Create the regularized state
# Replace r_ij with q and p_ij with p_q, keeping the other bodies unchanged
reg_state = list(quat_state) # Make a copy
# Update center of mass position and momentum
r_cm = (m_i * r_i + m_j * r_j) / (m_i + m_j)
p_cm = p_i + p_j
# Update the regularized positions
# FIX: Replace q**2 with q.power(2) or q * q
reg_state[i] = r_cm - (m_j / (m_i + m_j)) * q.power(2) # or q * q
reg_state[j] = r_cm + (m_i / (m_i + m_j)) * q.power(2) # or q * q
# Update the regularized momenta
reg_state[i+3] = p_cm * (m_i / (m_i + m_j)) - 0.5 * q * p_q
reg_state[j+3] = p_cm * (m_j / (m_i + m_j)) + 0.5 * q * p_q
return reg_state, time_scaling
def inverse_levi_civita_transform(self, reg_state: List[Quaternion],
collision_pair: Tuple[int, int]) -> List[Quaternion]:
"""
Apply the inverse quaternionic Levi-Civita transformation.
Args:
reg_state: Regularized quaternionic state
collision_pair: Tuple (i, j) indicating the previously colliding bodies
Returns:
Original quaternionic state
"""
i, j = collision_pair
if i > j:
i, j = j, i # Ensure i < j
if not (0 <= i < j <= 2):
raise ValueError(f"Invalid collision pair: {collision_pair}")
# Extract relevant parts of the regularized state
r_reg = reg_state[:3]
p_reg = reg_state[3:6]
# Additional calculations needed for the inverse transform...
# This would be the inverse of the operations in levi_civita_transform
# Placeholder: return the regularized state as-is (would need to implement the full inverse)
return reg_state
def regularized_equations_of_motion(self, s: float, reg_state: List[Quaternion],
collision_pair: Tuple[int, int],
time_scaling: float) -> List[Quaternion]:
"""
Compute the regularized equations of motion.
Args:
s: Regularized time
reg_state: Regularized quaternionic state
collision_pair: Tuple (i, j) indicating the colliding bodies
time_scaling: Time scaling factor from the regularization
Returns:
Derivatives of the regularized state
"""
# The regularized equations need to account for the time transformation
# dt = time_scaling * ds
# First, compute the original derivatives using the quaternionic equations
original_derivatives = self.qext.quaternionic_equations_of_motion(s, reg_state)
# Scale the derivatives according to the time transformation
scaled_derivatives = [deriv * time_scaling for deriv in original_derivatives]
# Additional regularization-specific terms would be added here...
return scaled_derivatives
def regularized_integrate(self, initial_state: np.ndarray, collision_pair: Tuple[int, int],
s_span: Tuple[float, float], s_eval: Optional[np.ndarray] = None,
**kwargs) -> Dict:
"""
Integrate the regularized equations of motion.
Args:
initial_state: Initial state vector
collision_pair: Tuple (i, j) indicating the colliding bodies
s_span: Tuple of (s_start, s_end) for regularized time
s_eval: Optional array of regularized time points
**kwargs: Additional arguments to pass to solve_ivp
Returns:
Dictionary with integration results
"""
# Convert initial state to quaternions
initial_quat_state = self.qext.state_to_quaternions(initial_state)
# Apply Levi-Civita transformation
reg_state, time_scaling = self.levi_civita_transform(initial_quat_state, collision_pair)
# Flatten quaternions for scipy solver
def quat_to_array(quat_state):
flat_array = np.zeros(24)
for i, q in enumerate(quat_state):
flat_array[i*4:(i+1)*4] = q.to_array()
return flat_array
def array_to_quat(flat_array):
quat_state = []
for i in range(6):
q_array = flat_array[i*4:(i+1)*4]
quat_state.append(Quaternion.from_array(q_array))
return quat_state
# Wrapper for scipy solver
def reg_eom_wrapper(s, y):
quat_state = array_to_quat(y)
derivatives = self.regularized_equations_of_motion(s, quat_state, collision_pair, time_scaling)
return quat_to_array(derivatives)
# Integrate using scipy's solver
flat_initial = quat_to_array(reg_state)
result = solve_ivp(
reg_eom_wrapper,
s_span,
flat_initial,
t_eval=s_eval,
**kwargs
)
# Process results
reg_states = []
for i in range(len(result.t)):
quat_state = array_to_quat(result.y[:, i])
reg_states.append(quat_state)
# Convert back to original states
orig_states = []
for reg_state in reg_states:
orig_state = self.inverse_levi_civita_transform(reg_state, collision_pair)
orig_states.append(orig_state)
# Convert to standard state vectors
states = np.zeros((len(result.t), 18))
for i, quat_state in enumerate(orig_states):
states[i] = self.qext.quaternions_to_state(quat_state)
return {
"s": result.t, # Regularized time
"t": result.t * time_scaling, # Original time (approximate)
"reg_states": reg_states,
"orig_states": orig_states,
"states": states,
"success": result.success,
"message": result.message
}
class QuaternionicPathContinuation:
"""
Class implementing quaternionic path continuation for the three-body problem.
This class provides methods for constructing and following quaternionic paths
around branch manifolds in quaternionic time.
"""
def __init__(self, quat_extension: QuaternionicExtension):
"""
Initialize the quaternionic path continuation.
Args:
quat_extension: QuaternionicExtension instance
"""
self.qext = quat_extension
self.masses = quat_extension.masses
self.G = quat_extension.G
self.sigma = quat_extension.sigma
self.is_exceptional = quat_extension.is_exceptional
self.closest_exceptional = quat_extension.closest_exceptional
def construct_quaternionic_path(self, t_c: float, rho: float, n_points: int = 100) -> np.ndarray:
"""
Construct a quaternionic path around a branch manifold.
This creates a path in quaternionic time around a branch point at t_c.
Args:
t_c: Location of the branch point (collision time)
rho: Radius of the path
n_points: Number of points on the path
Returns:
Array of quaternions representing the path
"""
# For binary collisions, we need a path that avoids the branch manifold
# t(s) = t_c + ρ * exp(i*θ(s) + j*φ(s))
path = []
for s in np.linspace(0, 1, n_points):
# Construct the path based on the mass parameter
if abs(self.sigma - 1/3) < 1e-10 or abs(self.sigma - 2**3/3**3) < 1e-10:
# Z_2 monodromy: need half a loop in complex plane
theta = s * np.pi
phi = 0.1 * np.sin(2 * np.pi * s) # Small excursion in j direction
elif abs(self.sigma - 2/3**2) < 1e-10:
# Trivial monodromy: any simple path works
theta = s * 2 * np.pi / 3
phi = 0
else:
# Complex monodromy: need a full loop with j-component
theta = s * 2 * np.pi
phi = 0.2 * np.sin(2 * np.pi * s)
# Create the quaternionic time
q_time = Quaternion(
t_c + rho * np.cos(theta),
rho * np.sin(theta),
rho * phi,
0
)
path.append(q_time)
return path
def quaternionic_continuation(self, initial_state: np.ndarray, quaternionic_path: List[Quaternion],
**kwargs) -> Dict:
"""
Perform quaternionic path continuation along the given path.
Args:
initial_state: Initial state vector
quaternionic_path: List of quaternions representing the path in quaternionic time
**kwargs: Additional arguments for the integrator
Returns:
Dictionary with continuation results
"""
# Convert initial state to quaternions
initial_quat_state = self.qext.state_to_quaternions(initial_state)
# Storage for states along the path
path_states = [initial_quat_state]
path_times = [quaternionic_path[0].scalar_part()]
current_state = initial_quat_state
# Integrate along the quaternionic path
for i in range(1, len(quaternionic_path)):
t_start = quaternionic_path[i-1]
t_end = quaternionic_path[i]
# Create a wrapped version of the equations of motion for quaternionic time
def quat_time_eom(s, state_array):
# Convert the flat array back to quaternions
quat_state = []
for j in range(6):
q_array = state_array[j*4:(j+1)*4]
quat_state.append(Quaternion.from_array(q_array))
# Compute the quaternionic time derivative
t_s = (1 - s) * t_start + s * t_end
dt_ds = t_end - t_start
# Evaluate the original EOMs at quaternionic time t_s
derivatives = self.qext.quaternionic_equations_of_motion(t_s.scalar_part(), quat_state)
# Scale by dt_ds to account for the parametrization
scaled_derivatives = [deriv * dt_ds for deriv in derivatives]
# Flatten the derivatives for scipy
flat_derivatives = np.zeros(24)
for j, deriv in enumerate(scaled_derivatives):
flat_derivatives[j*4:(j+1)*4] = deriv.to_array()
return flat_derivatives
# Flatten the current state
flat_state = np.zeros(24)
for j, q in enumerate(current_state):
flat_state[j*4:(j+1)*4] = q.to_array()
# Integrate over the parameter s ∈ [0, 1]
result = solve_ivp(
quat_time_eom,
(0, 1),
flat_state,
**kwargs
)
# Extract the final state
final_flat_state = result.y[:, -1]
# Convert back to quaternions
current_state = []
for j in range(6):
q_array = final_flat_state[j*4:(j+1)*4]
current_state.append(Quaternion.from_array(q_array))
# Store the state and time
path_states.append(current_state)
path_times.append(t_end.scalar_part())
# Convert quaternionic states to standard state vectors
states = np.zeros((len(path_states), 18))
for i, quat_state in enumerate(path_states):
states[i] = self.qext.quaternions_to_state(quat_state)
return {
"path": quaternionic_path,
"path_times": path_times,
"path_states": path_states,
"states": states
}
def compute_monodromy(self, continuation_results: Dict) -> Dict:
"""
Compute the monodromy transformation for a quaternionic continuation.
Args:
continuation_results: Results from quaternionic_continuation
Returns:
Dictionary with monodromy information
"""
# Extract the initial and final states
initial_state = continuation_results["path_states"][0]
final_state = continuation_results["path_states"][-1]
# Compute the state difference
state_diff = []
for i in range(len(initial_state)):
diff = final_state[i] - initial_state[i]
state_diff.append(diff)
# Compute the norm of the difference
diff_norm = sum(q.norm() for q in state_diff)
# Determine the monodromy type based on the difference
if diff_norm < 1e-10:
monodromy_type = "Trivial"
monodromy_group = "Trivial"
elif abs(self.sigma - 1/3) < 1e-10 or abs(self.sigma - 2**3/3**3) < 1e-10:
monodromy_type = "Z_2"
monodromy_group = "Z_2"
else:
monodromy_type = "Complex"
monodromy_group = "SL(2,C)"
# Compute conservation errors
initial_hamiltonian = self.qext.quaternionic_hamiltonian(initial_state)
final_hamiltonian = self.qext.quaternionic_hamiltonian(final_state)
energy_error = abs((final_hamiltonian - initial_hamiltonian) / initial_hamiltonian)
initial_angular_momentum = self.qext.quaternionic_angular_momentum(initial_state)
final_angular_momentum = self.qext.quaternionic_angular_momentum(final_state)
angular_momentum_error = (final_angular_momentum - initial_angular_momentum).norm() / initial_angular_momentum.norm()
return {
"monodromy_type": monodromy_type,
"monodromy_group": monodromy_group,
"state_difference_norm": diff_norm,
"energy_error": energy_error,
"angular_momentum_error": angular_momentum_error
}
def analyze_monodromy_structure(self, sigma: float) -> Dict:
"""
Analyze monodromy structure for the given mass parameter.
Args:
sigma: Mass parameter
Returns:
Dictionary with monodromy analysis results
"""
# Calculate the critical polynomial that determines bifurcation points
sigma_sym = symbols('sigma')
critical_poly = 27*sigma_sym**2 - 9*sigma_sym + 2
# Find the roots symbolically
solutions = solve(critical_poly, sigma_sym)
# Extract real and imaginary parts for proper handling
critical_points = []
for sol in solutions:
# Check if the solution is real (no imaginary component)
if sol.is_real:
critical_points.append(float(sol))
else:
# For complex solutions, we need to handle them differently
# Complex roots should not appear for this physical problem
pass
# We should have 3 critical points for the three-body problem
# If we don't have enough from the polynomial (which may have complex roots),
# use the known special values from the quaternionic analysis
if len(critical_points) < 3:
# The critical points for the three-body problem are:
# σ = 1/3: Corresponds to Z_2 monodromy
# σ = 2/9: Corresponds to Trivial monodromy
# σ = 8/27: Corresponds to Z_2 monodromy
from sympy import Rational
cubic = (sigma_sym - Rational(1, 3)) * (sigma_sym - Rational(2, 9)) * (sigma_sym - Rational(8, 27))
cubic_solutions = solve(cubic, sigma_sym)
for sol in cubic_solutions:
if sol not in critical_points:
critical_points.append(float(sol))
critical_points.sort() # Sort for clarity
# These should be approximately 1/3, 2/9, and 8/27
one_third_approx = critical_points[2] # Should be ≈ 1/3
two_ninth_approx = critical_points[0] # Should be ≈ 2/9
eight_27_approx = critical_points[1] # Should be ≈ 8/27
# Calculate the monodromy matrix for this sigma
monodromy = self._calculate_monodromy_matrix(sigma)
# Calculate eigenvalues of the monodromy matrix
eigenvalues = np.linalg.eigvals(monodromy)
# Determine the monodromy type based on eigenvalues
if abs(eigenvalues[0] - 1) < 1e-5 and abs(eigenvalues[1] - 1) < 1e-5:
# Both eigenvalues are 1 - trivial monodromy
monodromy_type = "Trivial"
is_trivial = True
elif abs(eigenvalues[0] + 1) < 1e-5 or abs(eigenvalues[1] + 1) < 1e-5:
# One eigenvalue is -1 - Z_2 monodromy
monodromy_type = "Z_2"
is_trivial = False
else:
# General complex eigenvalues - complex monodromy
monodromy_type = "Complex"
is_trivial = False
# Check special cases for verification
epsilon = 1e-5
if abs(sigma - one_third_approx) < epsilon or abs(sigma - eight_27_approx) < epsilon:
# Special cases: σ ≈ 1/3 or σ ≈ 8/27
monodromy_type = "Z_2"
is_trivial = False
elif abs(sigma - two_ninth_approx) < epsilon:
# Special case: σ ≈ 2/9
monodromy_type = "Trivial"
is_trivial = True
return {
"monodromy_type": monodromy_type,
"is_trivial": is_trivial,
"eigenvalues": eigenvalues.tolist(),
"critical_points": critical_points,
"one_third_value": one_third_approx,
"two_ninth_value": two_ninth_approx,
"eight_27_value": eight_27_approx
}
def _calculate_monodromy_matrix(self, sigma: float) -> np.ndarray:
"""
Calculate the monodromy matrix for quaternionic continuation.
Args:
sigma: Mass parameter
Returns:
2x2 monodromy matrix
"""
# This calculation is similar to the Galois case but from a different perspective
# For the quaternionic approach, we analyze continuation of solutions in H space
# The discriminant determines the eigenvalues
discriminant = 27*sigma**2 - 9*sigma + 2
if discriminant > 0:
# Real, distinct exponents
root = np.sqrt(discriminant)/3
exponent1 = 0.5 + root
exponent2 = 0.5 - root
else:
# Complex conjugate exponents
root = np.sqrt(-discriminant)/3
exponent1 = 0.5 + root*1j
exponent2 = 0.5 - root*1j
# The monodromy matrix eigenvalues are exp(2πi*exponent)
eigenval1 = np.exp(2j * np.pi * exponent1)
eigenval2 = np.exp(2j * np.pi * exponent2)
# Construct a monodromy matrix with these eigenvalues
if abs(eigenval1 - eigenval2) < 1e-5:
# Same eigenvalues - use a Jordan block
monodromy = np.array([[eigenval1, 1], [0, eigenval1]])
else:
# Different eigenvalues - diagonal matrix
monodromy = np.array([[eigenval1, 0], [0, eigenval2]])
return monodromy
def test_quaternionic_extension():
"""Test the quaternionic extension implementation."""
# Test with equal masses
masses = np.array([1.0, 1.0, 1.0])
qext = QuaternionicExtension(masses)
# Create a simple state vector
initial_state = np.zeros(18)
initial_state[0:3] = [1, 0, 0] # r1
initial_state[3:6] = [-0.5, 0.866, 0] # r2
initial_state[6:9] = [-0.5, -0.866, 0] # r3
initial_state[9:12] = [0, 0.1, 0] # p1
initial_state[12:15] = [0.0866, -0.05, 0] # p2
initial_state[15:18] = [-0.0866, -0.05, 0] # p3
# Test conversion to quaternions and back
quat_state = qext.state_to_quaternions(initial_state)
state_back = qext.quaternions_to_state(quat_state)
assert np.allclose(initial_state, state_back)
# Test quaternionic Hamiltonian
ham_value = qext.quaternionic_hamiltonian(quat_state)
# Test quaternionic equations of motion
derivatives = qext.quaternionic_equations_of_motion(0, quat_state)
print("All quaternionic extension tests passed!")
def test_quaternionic_regularization():
"""Test the quaternionic regularization implementation."""
# Test with equal masses
masses = np.array([1.0, 1.0, 1.0])
qext = QuaternionicExtension(masses)
qreg = QuaternionicRegularization(qext)
# Create a state with a near-collision between bodies 1 and 2
initial_state = np.zeros(18)
initial_state[0:3] = [0.01, 0, 0] # r1
initial_state[3:6] = [0.02, 0, 0] # r2
initial_state[6:9] = [1, 0, 0] # r3
initial_state[9:12] = [0, 0.1, 0] # p1
initial_state[12:15] = [0, -0.1, 0] # p2
initial_state[15:18] = [0, 0, 0] # p3
# Test Levi-Civita transformation
quat_state = qext.state_to_quaternions(initial_state)
reg_state, time_scaling = qreg.levi_civita_transform(quat_state, (0, 1))
print("All quaternionic regularization tests passed!")
def test_quaternionic_path_continuation():
"""Test the quaternionic path continuation implementation."""
# Test with sigma = 1/3 (exceptional case)
masses = np.array([1.0, 1.0, 1.0])
qext = QuaternionicExtension(masses)
qpath = QuaternionicPathContinuation(qext)
# Create a quaternionic path
quat_path = qpath.construct_quaternionic_path(1.0, 0.1, n_points=5)
# Test monodromy structure analysis
monodromy_info = qpath.analyze_monodromy_structure(1/3)
assert monodromy_info["monodromy_type"] == "Z_2"
# Test non-exceptional case
monodromy_info_general = qpath.analyze_monodromy_structure(0.4)
assert monodromy_info_general["monodromy_type"] == "Complex"
print("All quaternionic path continuation tests passed!")
if __name__ == "__main__":
# Run tests
test_quaternionic_extension()
test_quaternionic_regularization()
test_quaternionic_path_continuation()