-
Notifications
You must be signed in to change notification settings - Fork 0
/
deltas.py
executable file
·452 lines (388 loc) · 14.3 KB
/
deltas.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
#!/opt/local/bin/python
##################################################################
# Deltas #
##################################################################
# Purpose: #
# this script will from commutators of operators create #
# 1. a string of annihilation and creation operators #
# 2. do the normal ordering of the operators #
# 3. evaluate which ones are non-zero #
# 4. evaluate the delta functions #
##################################################################
# written by: Elke Fasshauer February 2017 #
##################################################################
import itertools
import os
def number_create(operators):
n = len(operators)
ncreate = 0
for pos in range(0,n):
operator_character = operators[pos][1]
if operator_character == 'create':
ncreate = ncreate + 1
return ncreate
def number_annihilate(operators):
n = len(operators)
nannihilate = 0
for pos in range(0,n):
operator_character = operators[pos][1]
if operator_character == 'annihilate':
nannihilate = nannihilate + 1
return nannihilate
def is_legitimate(operators):
"Determine whether the number of creation and annihilation operators match"
n = len(operators)
ncreate = number_create(operators)
nannihilate = number_annihilate(operators)
if ncreate == nannihilate:
legitimate = True
else:
legitimate = False
return legitimate
def first_annihilate(operators):
n = len(operators)
for pos in range(n-1,-1,-1):
operator_character = operators[pos][1]
if operator_character == 'annihilate':
first_annihilate = pos
return first_annihilate
def is_normal_ordered(operators):
n = len(operators)
for pos in range(0,n):
operator_character = operators[pos][1]
if operator_character == 'create':
last_create = pos
pos_first_annihilate = first_annihilate(operators)
if last_create > pos_first_annihilate:
is_normal = False
else:
is_normal = True
return is_normal
def find_next_create(op_string,pos_ann):
n = len(op_string)
for pos in range(pos_ann,n):
operator_character = op_string[pos][1]
if operator_character == 'create':
pos_create = pos
break
return pos_create
def find_pos_to_change(operators):
first_ann = first_annihilate(operators)
pos_create = find_next_create(op_string=operators, pos_ann=first_ann)
if (pos_create - first_ann) > 1:
pos_ann = pos_create - 1
else:
pos_ann = first_ann
return (pos_ann,pos_create)
def find_indices_to_change(operators,positions):
(pos1,pos2) = positions
i1 = operators[pos1][0]
i2 = operators[pos2][0]
return (i1,i2)
def diff_spaces(op_string,pos1,pos2):
first = op_string[pos1][0]
sec = op_string[pos2][0]
first_occ = (first in ['i','j','k','l','m','n','o'])
first_virt = (first in ['a','b','c','d','e','f','g'])
sec_occ = (sec in ['i','j','k','l','m','n','o'])
sec_virt = (sec in ['a','b','c','d','e','f','g'])
if ((first_occ and sec_virt) or (first_virt and sec_occ)):
return True
else:
return False
def create_normal_order(op_strings):
"operates on a list of operator strings"
print 'start normal ordering'
ncreate = number_create(op_strings[0])
maxperm = ncreate**4
for perm in range(0,maxperm):
n_strings = len(op_strings)
for i in range(n_strings-1,-1,-1):
curr_string = op_strings[i]
ac_ops = []
deltas = []
split_ops_from_deltas(curr_string,deltas,ac_ops)
if (is_legitimate(curr_string) == False):
print 'Non-legitimate string'
break
if (is_normal_ordered(curr_string) == False):
positions = find_pos_to_change(ac_ops)
indices = find_indices_to_change(ac_ops,positions)
new_string = ac_ops[:] + deltas[:]
(i1,i2) = indices
(pos1,pos2) = positions
del op_strings[i]
if (diff_spaces(ac_ops,pos1,pos2) == True):
#print new_string
new_string[pos1], new_string[pos2] = new_string[pos2], new_string[pos1]
new_string.append(('-','-'))
#print new_string, '\n'
else:
# Delta function
del ac_ops[pos2]
del ac_ops[pos1]
new_delta = ac_ops[:] + deltas[:]
new_delta.append(('Delta',i1,i2))
op_strings.append(new_delta)
# swap indices
new_string[pos1], new_string[pos2] = new_string[pos2], new_string[pos1]
new_string.append(('-','-'))
op_strings.append(new_string)
#print op_strings
def remove_zero_strings(op_strings):
print 'remove zero strings'
n = len(op_strings)
for term in range(n-1,-1,-1):
curr_string = op_strings[term]
for i in range(0,len(curr_string)):
index = curr_string[i][0]
if index in ['a','b','c','d','e','f','g']:
del op_strings[term]
break
# modified from
# http://code.activestate.com/recipes/579051-get-the-inversion-number-of-a-permutation/ under MIT license
def inversion_compared_to_reference(permList,reference):
"""
Description - This function returns the number of inversions in a
permutation compared to a given order.
Preconditions - The parameter permList is a list of unique positve numbers.
Postconditions - The number of inversions in permList has been returned.
Input - permList : list, reference list
Output - numInversions : int
"""
n = len(permList)
if len(permList)==1:
return 0
else:
numInversion=len(permList)-permList.index(reference[n-1])-1
permList.remove(reference[n-1])
return numInversion+inversion_compared_to_reference(permList,reference)
def permutations_list(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = range(n)
cycles = range(n, n-r, -1)
yield list(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield list(pool[i] for i in indices[:r])
break
else:
return
def split_ops_from_deltas(op_string,deltas,ac_ops):
n = len(op_string)
for op in range(0,n):
if (op_string[op][1] == 'create' or op_string[op][1] == 'annihilate'):
ac_ops.append(op_string[op])
else:
deltas.append(op_string[op])
def remove_redundant_perms(perms,reference):
n = len(perms)
for i in range(n-1,-1,-1):
perm = perms[i]
n_create = number_create(perm)
for j in range(0,n_create*2):
ref_pos = j
curr_pos = perm.index(reference[j])
if (reference[j][1] == 'create'):
if not (2*ref_pos == curr_pos):
del perms[i]
break
def perm_to_deltas(permutations,delta_list):
n = len(permutations)
for i in range(n-1,-1,-2):
first = permutations[i][0]
second = permutations[i-1][0]
delta_list.append(('Delta',first,second))
#print delta_list
def resolve_deltas(op_strings):
print 'resolve deltas'
n_strings = len(op_strings)
for string in range(n_strings-1,-1,-1):
curr_string = list(op_strings[string])
del op_strings[string]
n = len(curr_string)
deltas = []
ac_ops = []
split_ops_from_deltas(curr_string,deltas,ac_ops)
#print 'Deltas', deltas, '\n'
#print 'ac_ops', ac_ops, '\n'
perms = list(permutations_list(ac_ops))
remove_redundant_perms(perms,ac_ops)
for i in range(0,len(perms)):
all_deltas = deltas[:]
perm = list(perms[i])
n_inv = inversion_compared_to_reference(permList=perm,reference=ac_ops)
if (n_inv%2 != 0): #odd
all_deltas.append(('-','-'))
# we now have the signs correct and add the missing Delta functions
perm = list(perms[i])
perm_to_deltas(perm,all_deltas)
op_strings.append(all_deltas)
def latex_from_deltas(deltas,lines):
n_deltas = len(deltas)
minus_signs = deltas.count(('-','-'))
if (minus_signs%2 == 0): #even
sign = '+'
else:
sign = '-'
deltas.append(sign)
for i in range(n_deltas-1,-1,-1):
curr = deltas[i]
if (curr == ('-','-')):
del deltas[i]
else:
first = deltas[i][1]
sec = deltas[i][2]
del deltas[i]
delta = r'\delta_{' + first + sec + '}'
deltas.append(delta)
deltas.sort()
string = ' & ' + ' '.join(deltas) + r'\\'
lines.append(string)
def rm_plus_minus(lines,outlines):
plus = []
minus = []
for i in range(0,len(lines)):
if ('+' in lines[i]):
plus.append(lines[i])
elif ('-' in lines[i]):
minus.append(lines[i])
for j in range(len(plus)-1,-1,-1):
curr_plus = plus[j]
tmp_plus = curr_plus.translate(None,'&')
red_plus = tmp_plus.translate(None,'+')
for k in range(0,len(minus)):
curr_minus = minus[k]
tmp_minus = curr_minus.translate(None,'&')
red_minus = tmp_minus.translate(None,'-')
if (red_plus == red_minus):
del plus[j]
del minus[k]
break
for j in range(0,len(plus)):
outlines.append(plus[j])
for k in range(0,len(minus)):
outlines.append(minus[k])
def make_outfile(all_strings,filename):
print 'make output file'
header_list = [r'\documentclass{scrartcl}',r'\usepackage[utf8]{inputenc}',r'\usepackage{amsmath}',r'\allowdisplaybreaks','\n',r'\begin{document}','\n','\section*{'+name+'}','\n',r'\begin{align}']
outfile = open(filename, mode="w")
header = '\n'.join(header_list)
outfile.write(header)
outfile.write('\n')
n = len(all_strings)
latexlines = []
for i in range(0,n):
curr = all_strings[i]
latex_from_deltas(deltas=curr,lines=latexlines)
outlines = []
rm_plus_minus(latexlines,outlines)
outlines.sort()
res_lines = '\n'.join(outlines)
#print res_lines
outfile.write(res_lines)
if (len(res_lines) == 0):
outfile.write('0')
outfile.write('\n')
bottom_list = [r'\end{align}','\n',r'\end{document}']
bottom = '\n'.join(bottom_list)
outfile.write(bottom)
outfile.close
def make_deltas(op_strings,name):
create_normal_order(op_strings)
remove_zero_strings(op_strings)
resolve_deltas(op_strings)
make_outfile(op_strings,name)
##################################################################
# all functions above this line #
##################################################################
exc_ia = [('a', 'create'), ('i', 'annihilate')]
exc_jb = [('b', 'create'), ('j', 'annihilate')]
exc_nf = [('f', 'create'), ('n', 'annihilate')]
exc_og = [('g', 'create'), ('o', 'annihilate')]
deexc_ia = [('i', 'create'),('a', 'annihilate')]
deexc_jb = [('j', 'create'),('b', 'annihilate')]
deexc_nf = [('n', 'create'),('f', 'annihilate')]
deexc_og = [('o', 'create'),('g', 'annihilate')]
MP1_ket = [('d', 'create'), ('l', 'annihilate'), ('c', 'create'), ('k', 'annihilate')]
MP1_bra = [('k', 'create'), ('c', 'annihilate'), ('l', 'create'), ('d', 'annihilate')]
MP1_bra_p = [('i', 'create'), ('a', 'annihilate'), ('j', 'create'), ('b', 'annihilate')]
MP1_bra_p2 = [('m', 'create'), ('e', 'annihilate'), ('n', 'create'), ('f', 'annihilate')]
MP2_ket = [('c', 'create'), ('k', 'annihilate')]
MP2_bra_p = [('k', 'create'), ('c', 'annihilate')]
F = [('p', 'create'), ('q', 'annihilate')]
V1 = [('p', 'create'), ('r', 'create'), ('s', 'annihilate'), ('q', 'annihilate')]
V2 = [('p', 'create'), ('q', 'annihilate')]
all_strings = []
#MP_norm = MP1_bra_p + MP1_ket
#name = 'norm-MP1.tex'
#all_strings.append(MP_norm)
###################################################################
# Sigma - Matrix
###################################################################
##<SCF| op |SCF>
#comm1 = deexc_ia + exc_jb
#comm3 = exc_jb + deexc_ia
#comm3.append(('-','-'))
#name = 'Sigma-SCF-op-SCF.tex'
#-------------------------------------------------------
###<MP1| op |SCF>
#comm1 = MP1_bra + deexc_ia + exc_jb
#comm3 = MP1_bra + exc_jb + deexc_ia
#comm3.append(('-','-'))
#name = 'Sigma-MP1-op-SCF.tex'
#-------------------------------------------------------
##<SCF| op |MP1>
#comm1 = deexc_ia + exc_jb + MP1_ket
#comm3 = exc_jb + deexc_ia + MP1_ket
#comm3.append(('-','-'))
#name = 'Sigma-SCF-op-MP1.tex'
#######################################################
##<MP1| op |MP1>
#comm1 = MP1_bra_p2 + deexc_ia + exc_jb + MP1_ket
#comm3 = MP1_bra_p2 + exc_jb + deexc_ia + MP1_ket
#comm3.append(('-','-'))
#name = 'Sigma-MP1-op-MP1.tex'
#-------------------------------------------------------
##<SCF| op |MP2>
#comm1 = deexc_ia + exc_jb + MP2_ket
#comm3 = exc_jb + deexc_ia + F + MP2_ket
#comm3.append(('-','-'))
#name = 'Sigma-SCF-op-MP2.tex'
##<MP2| op |SCF>
comm1 = MP2_bra_p + deexc_ia + exc_jb
comm3 = MP2_bra_p + exc_jb + deexc_ia
comm3.append(('-','-'))
name = 'Sigma-MP2-op-SCF.tex'
#-------------------------------------------------------
all_strings.append(comm1)
#all_strings.append(comm2)
all_strings.append(comm3)
#all_strings.append(comm4)
#print all_strings
make_deltas(all_strings,name)
#ref = [('a', 'create'), ('i', 'annihilate'), ('b', 'create'), ('j', 'annihilate')]
#ex1 = [('b', 'create'), ('j', 'annihilate'), ('a', 'create'), ('i', 'annihilate')]
#
#all_strings = []
#op_string = [('a', 'create'), ('i', 'annihilate'), ('Delta','p','q'), ('b', 'create'), ('j', 'annihilate')]
#op2_string = [('p', 'create'), ('r', 'annihilate'), ('q', 'create'), ('s', 'annihilate')]
#print all_strings, '\n'
#create_normal_order(all_strings)
#remove_zero_strings(all_strings)
#print len(all_strings)
#for i in range(0,len(all_strings)):
# print all_strings[i], '\n'