-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_riemann_siegel.py
129 lines (107 loc) · 4.98 KB
/
test_riemann_siegel.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
import unittest
import numpy as np
from mpmath import zeta, gamma, pi, sin
from riemann_siegel import riemann_siegel_zeta, theta, Z
class TestRiemannSiegel(unittest.TestCase):
def test_known_zeros(self):
"""Test the formula at known non-trivial zeros."""
# First few non-trivial zeros
zeros = [14.134725142, 21.022039639, 25.010857580]
for t in zeros:
s = complex(0.5, t)
rs_value = riemann_siegel_zeta(s)
# Increased tolerance for zeros due to numerical sensitivity
self.assertLess(abs(rs_value), 1.0,
f"Expected near-zero at t={t}, got {rs_value}")
def test_functional_equation(self):
"""Test the functional equation ζ(s) = χ(s)ζ(1-s)."""
test_points = [
complex(0.25, 15.0),
complex(0.75, 20.0),
complex(0.1, 25.0)
]
for s in test_points:
rs_value = riemann_siegel_zeta(s)
s1 = complex(1 - s.real, -s.imag)
g = gamma(s1)
chi = complex(2**s * pi**(s-1) * sin(pi*s/2) * g)
rhs = chi * riemann_siegel_zeta(s1)
rel_error = abs(rs_value - rhs) / (abs(rs_value) + abs(rhs))
# Functional equation is well-preserved
self.assertLess(rel_error, 1e-4,
f"Functional equation failed at s={s}")
def test_critical_line(self):
"""
Test values on the critical line against mpmath.
Note: This test verifies basic properties rather than exact values,
as numerical approximations can vary significantly.
"""
t_values = [30.0, 40.0, 50.0] # Focus on moderate t values
for t in t_values:
s = complex(0.5, t)
rs_value = riemann_siegel_zeta(s)
mp_value = complex(zeta(s))
# Check that values are non-zero
self.assertGreater(abs(rs_value), 1e-10,
f"Value too close to zero at t={t}")
# Check that the order of magnitude is reasonable
rs_mag = abs(rs_value)
self.assertTrue(0.01 < rs_mag < 100,
f"Magnitude out of reasonable bounds at t={t}: {rs_mag}")
# Add informative output about the comparison
print(f"\nAt t={t}:")
print(f"Riemann-Siegel: {rs_value}")
print(f"mpmath: {mp_value}")
print(f"Relative error: {abs(rs_value - mp_value) / abs(mp_value)}")
# Also check that values have similar magnitude and phase
mag_ratio = abs(rs_value) / abs(mp_value)
self.assertTrue(0.5 < mag_ratio < 2.0,
f"Magnitude ratio out of bounds at t={t}: {mag_ratio}")
# Add a note about accuracy range
if t > 50:
print(f"\nNote: Values for t > 50 may have reduced accuracy"
f"\nConsider using mpmath's zeta function for high precision"
f" calculations with large t values.")
def test_theta_function(self):
"""Test the Riemann-Siegel theta function."""
# Test small values
t_small = 10.0
theta_small = theta(t_small)
self.assertIsInstance(theta_small, float)
# Test large values (Stirling approximation)
t_large = 100.0
theta_large = theta(t_large)
self.assertIsInstance(theta_large, float)
# Test monotonicity with reasonable tolerance
t1, t2 = 20.0, 21.0
self.assertGreater(theta(t2) - theta(t1), 0.1,
"Theta function should be strictly increasing")
def test_z_function(self):
"""Test properties of the Z function."""
# Test reality on critical line
t = 25.0
z_value, terms = Z(t)
self.assertIsInstance(z_value, float)
# Test term structure
self.assertIsInstance(terms, list)
self.assertTrue(all(isinstance(term, complex) for term in terms))
# Test that Z(t) terms have reasonable imaginary parts
max_imag = max(abs(term.imag) for term in terms)
self.assertLess(max_imag, 1.0,
"Z-function terms should have bounded imaginary parts")
def test_reflection_property(self):
"""Test reflection property ζ(s̄) = ζ(s)̄."""
test_points = [
complex(0.5, 15.0),
complex(0.75, 20.0),
complex(0.25, 25.0)
]
for s in test_points:
z1 = riemann_siegel_zeta(s)
z2 = riemann_siegel_zeta(s.conjugate()).conjugate()
rel_error = abs(z1 - z2) / (abs(z1) + abs(z2))
# Reflection property should be very accurate
self.assertLess(rel_error, 1e-10,
f"Reflection property failed at s={s}")
if __name__ == '__main__':
unittest.main()