-
Notifications
You must be signed in to change notification settings - Fork 5
/
my_orbit_structure.h
239 lines (218 loc) · 7.05 KB
/
my_orbit_structure.h
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
#ifndef MY_ORBIT_STRUCTURE
#define MY_ORBIT_STRUCTURE
#include "my_intrinsics.h"
#include "my_type_functions.h"
template<typename F>
requires(Transformation(F))
DistanceType(F) distance(Domain(F) x, Domain(F) y, F f)
{
// Preconditions:
// - y is reachable from x under f
typedef DistanceType(F) N;
N n = N(0);
while (x != y) {
x = f(x);
n = n + N(1);
}
return n;
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P) &&
Domain(F) == Domain(P))
Domain(F) collision_point(const Domain(F)& x, F f, P p)
{
// Preconditions:
// - p(x) if and only if f(x) is defined
if (!p(x)) return x;
Domain(F) slow = x; // slow = f^0(x)
Domain(F) fast = f(x); // fast = f^1(x)
// n <- 0 (completed iterations)
while (fast != slow) { // slow = f^n(x) ^ fast = f^{2n+1}(x)
slow = f(slow); // slow = f^{n+1}(x) ^ fast = f^{2n+3}(x)
if (!p(fast)) return fast;
fast = f(fast); // slow = f^n(x) ^ fast = f^{2n+2}(x)
if (!p(fast)) return fast;
fast = f(fast); // slow = f^n(x) ^ fast = f^{2n+3}(x)
// n <- n + 1
}
return fast; // slow = f^n(x) ^ fast = f^{2n+1}(x)
// Postconditions:
// - return value is terminal point or collision point
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P) &&
Domain(F) == Domain(P))
bool terminating(const Domain(F)& x, F f, P p)
{
// Preconditions:
// - p(x) if and only if f(x) is defined
return !p(collision_point(x, f, p));
}
template<typename F>
requires(Transformation(F))
Domain(F)
collision_point_nonterminating_orbit(const Domain(F)& x, F f)
{
Domain(F) slow = x; // slow = f^0(x)
Domain(F) fast = f(x); // fast = f^1(x)
// n <- 0 (completed iterations)
while (fast != slow) { // slow = f^n(x) ^ fast = f^{2n+1}(x)
slow = f(slow); // slow = f^{n+1}(x) ^ fast = f^{2n+1}(x)
fast = f(fast); // slow = f^{n+1}(x) ^ fast = f^{2n+2}(x)
fast = f(fast); // slow = f^{n+1}(x) ^ fast = f^{2n+3}(x)
// n <- n + 1
}
return fast; // slow = f^n(x) ^ fast = f^{2n+1}(x)
// Postconditions:
// - return value is collision point
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(F) &&
Domain(F) == Domain(P))
bool circular(const Domain(F)& x, F f, P p)
{
// Preconditions:
// - p(x) if and only if f(x) is defined
Domain(F) y = collision_point(x, f, p);
return p(y) && x == f(y);
}
template<typename F>
requires(Transformation(F))
bool circular_nonterminating_orbit(const Domain(F)& x, F f)
{
return x == f(collision_point_nonterminating_orbit(x, f));
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P)
&& Domain(F) == Domain(P))
DistanceType(F) cycle_size(const Domain(F)& x, F f, P p)
{
// Preconditions:
// - p(x) if and only if f(x) is defined
typedef DistanceType(F) N;
Domain(F) y = collision_point(x, f, p);
if (!p(y)) return N(0);
return N(1) + distance(f(y), y, f);
}
template<typename F>
requires(Transformation(F))
DistanceType(F) cycle_size_nonterminating_orbit(const Domain(F)& x, F f)
{
typedef DistanceType(F) N;
Domain(F) y = collision_point_nonterminating_orbit(x, f);
return N(1) + distance(f(y), y, f);
}
template<typename F>
requires(Transformation(F))
Domain(F) convergent_point(Domain(F) x0, Domain(F) x1, F f)
{
// Preconditions:
// - there exists some n in DistanceType(F) such that n >= 0 and f^n(x0) = f^n(x1)
while (x0 != x1) {
x0 = f(x0);
x1 = f(x1);
}
return x0;
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P) &&
Domain(F) == Domain(P))
Domain(F) connection_point(const Domain(F)& x, F f, P p)
{
// Preconditions:
// - p(x) if and only if f(x) is defined
Domain(F) y = collision_point(x, f, p);
if (!p(y)) return y;
return convergent_point(x, f(y), f);
}
template<typename F>
requires(Transformation(F))
Domain(F)
connection_point_nonterminating_orbit(const Domain(F)& x, F f)
{
return convergent_point(
x,
f(collision_point_nonterminating_orbit(x, f)),
f);
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P) &&
Domain(F) == Domain(P))
bool intersects(const Domain(F)& x0, const Domain(F)& x1, F f, P p)
{
// Preconditions:
// - p(x) if and only if f(x) is defined
Domain(F) y0 = collision_point(x0, f, p);
Domain(F) y1 = collision_point(x1, f, p);
if (!p(y0) || !p(y1)) return y0 == y1;
Domain(F) y = y0;
do {
if (y == y1) return true;
y = f(y);
} while (y != y0);
return false;
}
template<typename F>
requires(Transformation(F))
bool intersects_nonterminating_orbit(const Domain(F)& x0, const Domain(F)& x1, F f)
{
Domain(F) y0 = collision_point_nonterminating_orbit(x0, f);
Domain(F) y1 = collision_point_nonterminating_orbit(x1, f);
Domain(F) y = y0;
do {
if (y == y1) return true;
y = f(y);
} while (y != y0);
return false;
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P) &&
Domain(F) == Domain(P))
Domain(F)* convergent_point_guarded(Domain(F) x0, Domain(F) x1, F f, P p)
{
// Preconditions:
// - p(x) if and only if f(x) is defined
// - intersects(x0, x1, f, p)
Domain(F) y0 = connection_point(x0, f, p);
Domain(F) y1 = connection_point(x1, f, p);
bool x0_entered_cycle = false;
bool x1_entered_cycle = false;
while (x0 != x1) {
if (!p(x0) || !p(x1)) return NULL;
if (x0 == y0) x0_entered_cycle = true;
if (x1 == y1) x1_entered_cycle = true;
if (x0_entered_cycle && x1_entered_cycle) return NULL;
x0 = f(x0);
x1 = f(x1);
}
return new Domain(F)(x0);
}
template<typename F, typename P>
requires(Transformation(F) && UnaryPredicate(P) &&
Domain(F) == Domain(P))
triple<DistanceType(F), DistanceType(F), Domain(F)>
orbit_structure(const Domain(F)& x, F f, P p)
{
// Preconditions:
// p(x) if and only if f(x) is defined
typedef DistanceType(F) N;
Domain(F) y = connection_point(x, f, p);
N m = distance(x, y, f);
N n(0);
if (p(y)) n = distance(f(y), y, f);
// Terminating: m = h - 1, n = 0
// Otherwise: m = h, n = c - 1
return triple<N, N, Domain(F)>(m, n, y);
}
template<typename F>
requires(Transformation(F))
triple<DistanceType(F), DistanceType(F), Domain(F)>
orbit_structure_nonterminating_orbit(const Domain(F)& x, F f)
{
typedef DistanceType(F) N;
Domain(F) y = connection_point_nonterminating_orbit(x, f);
return triple<N, N, Domain(F)>(distance(x, y, f),
distance(f(y), y, f),
y);
}
#endif