-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbitalgenerator.asy
215 lines (172 loc) · 4.9 KB
/
orbitalgenerator.asy
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
// -----------------------------------------------------------
// -- An Asymptote package for drawing orbitals.
// -- Free under any GNU public license.
// -- (C) Copyright: Peter Luschny, 2008
// -- Version 1 Release 1
// -----------------------------------------------------------
// --
// -- This file is accompanying the package orbital.asy and
// -- provides structures and routines to generate orbitals.
// -- See also the file orbitalusage.asy, which illustrates
// -- the use of the structure 'orbitalgenerator'.
// --
// -- For more information see
// -- http://www.luschny.de/math/swing/orbital/orbitaldoc.pdf
// --
// -----------------------------------------------------------
typedef void VISIT(int[]);
struct OrbitalGenerator
{
private VISIT userhook;
private int N, n, t, count;
private bool isOdd;
private void write(int[] jump)
{
string s;
for(int j : jump) { s += (string)j + ","; }
write(s);
}
private void visit(int[] jump)
{
count = count + 1;
if(userhook != null)
{
userhook(jump);
}
else { this.write(jump); }
}
private void assemble(int[] c)
{
int[] q = new int[N];
for(int i = 0; i < N; ++i){ q[i] = 1; }
for(int i = 0; i < t; ++i){ q[N-c[i+1]-1] = -1; }
if(isOdd)
{
int[] s = {0};
for(int i = 0; i < n; ++i)
{
visit(concat(q[0:i],s,q[i:n]));
}
}
else { visit(q); }
}
restricted static OrbitalGenerator
OrbitalGenerator(int len, VISIT hook = null)
{
if(len > 12)
{
write("Error! n is too big, n <= 12 required.");
write("I set n = 1.");
len = 1;
}
OrbitalGenerator og = new OrbitalGenerator;
bool isodd = len % 2 == 1;
int Len = isodd ? len - 1 : len;
og.isOdd = isodd;
og.n = len;
og.N = Len;
og.t = quotient(Len, 2);
og.count = 0;
og.userhook = hook;
return og;
}
public int generate()
{
int[] c = new int[t + 3];
for(int j = 1; j <= t; ++j) { c[j] = j - 1; }
c[t + 1] = N; c[t + 2] = 0;
// -- Algorithm L in Donald E. Knuth TAOCP 7.2.1.3
while(true)
{
assemble(c);
int j = 1;
while(c[j] + 1 == c[j + 1]) { c[j] = j - 1; ++j; }
if(j > t) { break; }
c[j] += 1 ;
}
return count;
}
} from OrbitalGenerator unravel OrbitalGenerator;
// ------------- End of struct OrbitalGenerator --------------
struct PrimeOrbital
{
restricted int[] jumps;
restricted int numer, denom;
restricted real balance;
private static int[] primes = {
2,3,5,7,11,13,17,19,23,29,31,37,41};
private void primorial(int[] jump)
{
int i = 0; numer = 1; denom = 1;
for(int j : jump)
{
if(j > 0) numer *= primes[i];
else if(j < 0) denom *= primes[i];
++i;
}
balance = numer / denom ;
}
restricted static PrimeOrbital PrimeOrbital(int[] jumps)
{
PrimeOrbital o = new PrimeOrbital;
o.jumps = jumps;
o.primorial(jumps);
return o;
}
public void write()
{
string s;
for(int j : jumps) { s += (string)j + " " ; }
string p = " ["+(string)numer+ "/" +(string)denom+"] "
+" "+format("%.3g", balance);
write(s + p);
}
} from PrimeOrbital unravel PrimeOrbital;
// --------------- End of struct PrimeOrbital ----------------
struct PrimeOrbitalGenerator
{
private PrimeOrbital[] urbi;
private OrbitalGenerator og;
private int urbI;
private void insertOrbit(int[] jumps)
{
urbi[++urbI] = PrimeOrbital(jumps);
if(urbI == 0) return;
PrimeOrbital temp;
for(int top = 0; top < urbI; ++top)
{
for(int j = top ; j >= 0; --j)
{
if(urbi[j].balance < urbi[j+1].balance)
{
break;
}
temp = urbi[j];
urbi[j] = urbi[j+1];
urbi[j+1] = temp;
}
}
}
private void visit(int[] jumps)
{
insertOrbit(jumps);
}
restricted static PrimeOrbitalGenerator
PrimeOrbitalGenerator(int len)
{
PrimeOrbitalGenerator psog = new PrimeOrbitalGenerator;
psog.og = OrbitalGenerator(len, psog.visit);
return psog;
}
public PrimeOrbital[] generate()
{
urbI = -1;
og.generate();
return urbi;
}
public void write()
{
for(PrimeOrbital po : urbi) po.write();
}
} from PrimeOrbitalGenerator unravel PrimeOrbitalGenerator;
// ----------- End of struct PrimeOrbitalGenerator -----------