-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion1.py
2074 lines (1815 loc) · 80.2 KB
/
version1.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
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from math import factorial
import numpy as np
from sympy import I, symbols, Function, preorder_traversal, Basic, Expr, signsimp, Add, Mul, Pow
from sympy.core.function import UndefinedFunction
from itertools import permutations
# vibrational_indices_str = ' '.join(['v{:02d}'.format(i) for i in range(0, 30)])
v = [symbols('v{:02d}'.format(i)) for i in range(0, 30)]
# rotational_indices_str = ' '.join(['r{:02d}'.format(i) for i in range(0, 30)])
r = [symbols('r{:02d}'.format(i)) for i in range(0, 30)]
A = Function('A')
A20 = Function('A20')
A30 = Function('A30')
A40 = Function('A40')
B40 = Function('B40')
A21 = Function('A21')
A31 = Function('A31')
A02 = Function('A02')
A12 = Function('A12')
A22 = Function('A22')
A32 = Function('A32')
jop = Function('jop')
pop = Function('pop')
qop = Function('qop')
ee = Function('ee')
lop = Function('lop')
sigma = Function('sigma')
omega = Function('omega')
class GenericTerm:
def __init__(self, coeff, m: int, n: int, type: str):
if type=='H' or type=='S':
self.type = type
else:
raise AttributeError('Unrecognized type {}.'.format(type))
self.coeff = coeff
if m < 0 or n < 0 or (m+n-2) < 0:
raise AttributeError('Invalid vibration and/or rotation operators.')
else:
self.vib_order = m
self.rot_order = n
if m == 0 and n == 2:
self.order = 1
else:
self.order = m + n - 2
def __repr__(self):
return "{}*{}({},{})".format(self.coeff, self.type, self.vib_order, self.rot_order)
def __add__(self, other):
if isinstance(other, GenericTerm):
if self.combinesWith(other):
self.coeff = self.coeff + other.coeff
return self
else:
return GenericExpression([self, other])
elif isinstance(other, GenericExpression):
items_list = other.items
items_list.append(self)
return GenericExpression(items_list)
elif isinstance(other, GenericCommutator):
return GenericExpression([other, GenericCommutator])
else:
raise NotImplementedError
def __sub__(self, other):
return self + (other*(-1))
def __mul__(self, other):
if isinstance(other, (GenericTerm, GenericExpression, GenericCommutator)):
raise NotImplementedError
else:
try:
new = self
new.coeff = new.coeff*other
return new
except:
raise TypeError
def __rmul__(self, other):
if isinstance(other, (GenericTerm, GenericExpression, GenericCommutator)):
raise NotImplementedError
else:
try:
new = self
new.coeff = new.coeff*other
return new
except:
raise TypeError
def __eq__(self, other):
if self.combinesWith(other) and self.coeff == other.coeff:
return True
else:
return False
def combinesWith(self,other):
if isinstance(other, GenericTerm):
if self.type == other.type and self.vib_order == other.vib_order and self.rot_order == other.rot_order:
return True
else:
return False
else:
return False
def vibCommutator(self, other):
if isinstance(other, (GenericTerm, GenericCommutator)):
if self.vib_order == 0 or other.vib_order == 0:
return 0
else:
return GenericCommutator(1, self, other, 'V')
elif isinstance(other, GenericExpression):
item_list = [self.vibCommutator(item) for item in other.items]
return GenericExpression(item_list)
else:
raise NotImplementedError
def rotCommutator(self,other):
if isinstance(other, (GenericTerm, GenericCommutator)):
if self.rot_order == 0 or other.rot_order == 0:
return 0
else:
return GenericCommutator(1, self, other, 'R')
elif isinstance(other, GenericExpression):
item_list = [self.rotCommutator(item) for item in other.items]
return GenericExpression(item_list)
else:
raise NotImplementedError
def select(self, m, n):
if self.vib_order == m and self.rot_order == n:
return self
else:
return 0
class GenericExpression:
def __init__(self, items_list):
self.items = []
for item in items_list:
if isinstance(item, GenericExpression):
for sub_item in item.items:
self.items.append(sub_item)
elif isinstance(item, (GenericTerm, GenericCommutator)):
if item.order >= 0 and not ([item.vib_order, item.rot_order] in [[0,0],[1,0],[0,1],[1,1]]):
self.items.append(item)
elif item == 0:
pass
else:
raise ValueError('Unrecognized object encountered: {}'.format(item))
unique_items = []
for item in self.items:
if not any(item.combinesWith(unique) for unique in unique_items):
unique_items.append(item)
if len(unique_items) != len(self.items):
combined_items = []
for unique in unique_items:
combines_with_items = []
for item in self.items:
if item.combinesWith(unique):
combines_with_items.append(item)
item_sum = combines_with_items[0]
for item in combines_with_items[1:]:
item_sum += item
combined_items.append(item_sum)
self.items = combined_items
final_items = []
for item in self.items:
if item.coeff != 0:
final_items.append(item)
def __repr__(self):
return "{}".format([item for item in self.items])
def __add__(self, other):
if isinstance(other, (GenericTerm, GenericCommutator)):
return GenericExpression([*self.items, other])
elif isinstance(other, GenericExpression):
return GenericExpression([*self.items, *other.items])
elif other == 0:
return self
else:
raise TypeError('Cannot add {} to GenericExpression object'.format(other))
def __sub__(self, other):
return self + (other*(-1))
def __mul__(self, other):
if isinstance(other, (GenericTerm, GenericExpression, GenericCommutator)):
raise NotImplementedError
else:
try:
value = GenericExpression([item * other for item in self.items])
return value
except:
raise TypeError('Cannot multiply GenericExpression object by {}'.format(other))
def __rmul__(self, other):
return self*other
def __getitem__(self, item):
return self.items[item]
def __eq__(self, other):
if isinstance(other, GenericExpression):
raise NotImplementedError('Need to define expression_sort function first.')
else:
return False
def select(self, m, n):
selected = []
for item in self.items:
if item.vib_order == m and item.rot_order == n:
selected.append(item)
return GenericExpression(selected)
class GenericCommutator:
def __init__(self, coeff, term1, term2, type: str):
if type == 'V' or type == 'R':
self.type = type
else:
raise TypeError
if isinstance(term1, (GenericTerm, GenericCommutator)) and isinstance(term2, (GenericTerm, GenericCommutator)):
self.coeff = coeff*term1.coeff*term2.coeff
term1.coeff = 1
term2.coeff = 1
self.term1 = term1
self.term2 = term2
if self.type == 'V':
self.vib_order = term1.vib_order + term2.vib_order - 2
self.rot_order = term1.rot_order + term2.rot_order
else:
self.vib_order = term1.vib_order + term2.vib_order
self.rot_order = term1.rot_order + term2.rot_order - 1
self.order = self.vib_order + self.rot_order - 2
elif isinstance(term1, GenericExpression) or isinstance(term2, GenericExpression):
raise TypeError('Use object commutator function to evaluate the commutator with an expression.')
else:
raise NotImplementedError
def __repr__(self):
return "{}*[{},{}]{}".format(self.coeff, self.term1, self.term2, self.type)
def __add__(self, other):
if isinstance(other, GenericCommutator):
if self.combinesWith(other):
new = self
new.coeff = new.coeff + other.coeff
return new
else:
return GenericExpression([self, other])
elif isinstance(other, (GenericTerm, GenericExpression)):
return GenericExpression([self, other])
else:
raise TypeError
def __sub__(self, other):
return self + (other * (-1))
def __mul__(self, other):
if isinstance(other, (GenericTerm, GenericExpression, GenericCommutator)):
raise NotImplementedError
else:
try:
new = self
new.coeff = new.coeff * other
return new
except:
raise TypeError
def __rmul__(self, other):
if isinstance(other, (GenericTerm, GenericExpression, GenericCommutator)):
raise NotImplementedError
else:
try:
new = self
new.coeff = new.coeff * other
return new
except:
raise TypeError
def __eq__(self, other):
if self.combinesWith(other) and self.coeff == other.coeff:
return True
else:
return False
def combinesWith(self, other):
if isinstance(other, GenericCommutator):
if self.term1 == other.term1 and self.term2 == other.term2 and self.type == other.type:
return True
else:
return False
else:
return False
def select(self, m, n):
if self.vib_order == m and self.rot_order == n:
return self
else:
return 0
class Term:
def __init__(self, vib_op_list: list, rot_op_list: list, coefficient, vib_indices: list, rot_indices: list):
self._n_vib_op = len(vib_op_list)
self._n_rot_op = len(rot_op_list)
self._vib_indices = vib_indices
self._rot_indices = rot_indices
self._vib_op = vib_op_list
self._rot_op = rot_op_list
self._coeff = coefficient
@property
def vib_op(self):
return self._vib_op
@property
def rot_op(self):
return self._rot_op
@property
def coeff(self):
return self._coeff
@coeff.setter
def coeff(self, value):
self._coeff = value
@property
def n_vib_op(self):
return self._n_vib_op
@property
def n_rot_op(self):
return self._n_rot_op
@property
def vib_indices(self):
return self._vib_indices
@property
def rot_indices(self):
return self._rot_indices
def __repr__(self):
operators = []
for vib_op in self.vib_op:
operators.append(str(vib_op))
for rot_op in self.rot_op:
operators.append(str(rot_op))
return "Sum(({})*{})".format(self.coeff, "*".join(operators))
def __add__(self, other):
if isinstance(other, Term):
if self.willCombineWith(other):
return self.combineWith(other)
else:
return Expression([self, other])
elif isinstance(other, Expression):
return Expression([self, *other.items])
elif other == 0:
return self
else:
raise TypeError('Cannot add {} to Term object.'.format(other))
def __sub__(self, other):
return self + (other*(-1))
def __mul__(self, other):
if isinstance(other, Expression):
return Expression([self*term for term in other.items])
elif isinstance(other, Term):
n_left_vib_indices = len(self.vib_indices)
n_left_rot_indices = len(self.rot_indices)
n_right_vib_indices = len(other.vib_indices)
n_right_rot_indices = len(other.rot_indices)
left_vib_indices = v[0:n_left_vib_indices]
right_vib_indices = v[n_left_vib_indices:(n_left_vib_indices+n_right_vib_indices)]
left_rot_indices = r[0:n_left_rot_indices]
right_rot_indices = r[n_left_rot_indices:(n_left_rot_indices+n_right_rot_indices)]
new_left = self.changeIndices(left_vib_indices, left_rot_indices)
new_right = other.changeIndices(right_vib_indices, right_rot_indices)
final_vib_indices = left_vib_indices + right_vib_indices
final_rot_indices = left_rot_indices + right_rot_indices
final_coeff = new_left.coeff * new_right.coeff
final_vib_op = new_left.vib_op + new_right.vib_op
final_rot_op = new_left.rot_op + new_right.rot_op
return Term(final_vib_op, final_rot_op, final_coeff, final_vib_indices, final_rot_indices)
else:
try:
new_coefficient = self.coeff*other
return Term(self.vib_op, self.rot_op, new_coefficient, self.vib_indices, self.rot_indices)
except:
raise TypeError('Cannot multiply coefficient by {}.'.format(other))
def __rmul__(self, other):
raise NotImplementedError
def __eq__(self, other):
if isinstance(other, Term):
if all([self.vib_op == other.vib_op,
self.rot_op == other.rot_op,
self.vib_indices == other.vib_indices,
self.rot_indices == other.rot_indices,
self.coeff == other.coeff]):
return True
else:
return False
else:
return False
def __len__(self):
return 1
def willCombineWith(self, other):
if isinstance(other, Term):
if self.n_vib_op != other.n_vib_op:
return False
for i in range(0, self.n_vib_op):
if self.vib_op[i].func != other.vib_op[i].func:
return False
if self.n_rot_op != other.n_rot_op:
return False
for i in range(0, self.n_rot_op):
if self.rot_op[i].func != other.rot_op[i].func:
return False
return True
else:
return False
def changeVibIndices(self, new_vib_indices_list):
if len(new_vib_indices_list) != len(self.vib_indices):
raise ValueError('Unequal length of vib indices lists.')
substitution_rules = {}
for i in range(0, len(self.vib_indices)):
substitution_rules[self.vib_indices[i]] = new_vib_indices_list[i]
new_vib_op = [x.subs(substitution_rules, simultaneous=True) for x in self.vib_op]
new_coeff = self.coeff.subs(substitution_rules, simultaneous=True)
new_vib_indices = []
for x in new_vib_indices_list:
if x not in new_vib_indices:
new_vib_indices.append(x)
return Term(new_vib_op, self.rot_op, new_coeff, new_vib_indices, self.rot_indices)
def changeRotIndices(self, new_rot_indices_list):
if len(new_rot_indices_list) != len(self.rot_indices):
raise ValueError('Unequal length of rot indices lists.')
substitution_rules = {}
for i in range(0, len(self.rot_indices)):
substitution_rules[self.rot_indices[i]] = new_rot_indices_list[i]
new_rot_op = [x.subs(substitution_rules, simultaneous=True) for x in self.rot_op]
new_coeff = self.coeff.subs(substitution_rules, simultaneous=True)
return Term(self.vib_op, new_rot_op, new_coeff, self.vib_indices, new_rot_indices_list)
def changeIndices(self, new_vib_indices_list, new_rot_indices_list):
new_vib_term = self.changeVibIndices(new_vib_indices_list)
final_term = new_vib_term.changeRotIndices(new_rot_indices_list)
return final_term
def combineWith(self, other):
if self.willCombineWith(other):
if len(self.vib_indices) <= len(other.vib_indices):
smaller_vib_term = self
larger_vib_term = other
else:
smaller_vib_term = other
larger_vib_term = self
combined_vib_indices = larger_vib_term.vib_indices
combined_vib_op = larger_vib_term.vib_op
if smaller_vib_term.vib_indices == larger_vib_term.vib_indices:
new_smaller_vib_term = smaller_vib_term
else:
# Changing smaller term to match indices of larger term. That way the additional indices of the larger
# term are unaffected. If same, then doesn't matter.
vib_substitutions = {}
for i in range(0, smaller_vib_term.n_vib_op):
smaller_vib_index = [x for x in preorder_traversal(smaller_vib_term.vib_op[i])][1]
larger_vib_index = [x for x in preorder_traversal(larger_vib_term.vib_op[i])][1]
vib_substitutions[smaller_vib_index] = larger_vib_index
if len(vib_substitutions.keys()) < len(smaller_vib_term.vib_indices):
has_rules = [i for i in vib_substitutions.keys()]
needs_rules = [i for i in smaller_vib_term.vib_indices if i not in has_rules]
used_indices = [value for key, value in vib_substitutions.items()]
unused_indices = [i for i in larger_vib_term.vib_indices if i not in used_indices]
for i in range(0, len(needs_rules)):
vib_substitutions[needs_rules[i]] = unused_indices[i]
new_vib_indices = [x.subs(vib_substitutions, simultaneous=True) for x in smaller_vib_term.vib_indices]
new_smaller_vib_term = smaller_vib_term.changeVibIndices(new_vib_indices)
if len(new_smaller_vib_term.rot_indices) <= len(larger_vib_term.rot_indices):
smaller_rot_term = new_smaller_vib_term
larger_rot_term = larger_vib_term
else:
smaller_rot_term = larger_vib_term
larger_rot_term = new_smaller_vib_term
combined_rot_indices = larger_rot_term.rot_indices
combined_rot_op = larger_rot_term.rot_op
if smaller_rot_term.rot_indices == larger_rot_term.rot_indices:
new_smaller_rot_term = smaller_rot_term
else:
rot_substitutions = {}
for i in range(0, smaller_rot_term.n_rot_op):
smaller_rot_index = [x for x in preorder_traversal(smaller_rot_term.rot_op[i])][1]
larger_rot_index = [x for x in preorder_traversal(larger_rot_term.rot_op[i])][1]
rot_substitutions[smaller_rot_index] = larger_rot_index
if len(rot_substitutions.keys()) < len(smaller_rot_term.rot_indices):
has_rules = [i for i in rot_substitutions.keys()]
needs_rules = [i for i in smaller_rot_term.rot_indices if i not in has_rules]
used_indices = [value for key, value in rot_substitutions.items()]
unused_indices = [i for i in larger_rot_term.rot_indices if i not in used_indices]
for i in range(0, len(needs_rules)):
rot_substitutions[needs_rules[i]] = unused_indices[i]
new_rot_indices = [x.subs(rot_substitutions, simultaneous=True) for x in smaller_rot_term.rot_indices]
new_smaller_rot_term = smaller_rot_term.changeRotIndices(new_rot_indices)
combined_coefficient = larger_rot_term.coeff + new_smaller_rot_term.coeff
combined_term = Term(combined_vib_op,
combined_rot_op,
combined_coefficient,
combined_vib_indices,
combined_rot_indices)
return combined_term
else:
return Expression([self, other])
def vibCommutator(self, other):
if self.n_vib_op == 0:
return 0
elif isinstance(other, Expression):
return Expression([self.vibCommutator(term) for term in other.items])
elif isinstance(other, Term):
if other.n_vib_op == 0:
return 0
else:
n_left_vib_indices = len(self.vib_indices)
n_left_rot_indices = len(self.rot_indices)
n_right_vib_indices = len(other.vib_indices)
n_right_rot_indices = len(other.rot_indices)
left_vib_indices = v[0:n_left_vib_indices]
right_vib_indices = v[n_left_vib_indices:(n_left_vib_indices + n_right_vib_indices)]
left_rot_indices = r[0:n_left_rot_indices]
right_rot_indices = r[n_left_rot_indices:(n_left_rot_indices + n_right_rot_indices)]
new_left = self.changeIndices(left_vib_indices, left_rot_indices)
new_right = other.changeIndices(right_vib_indices, right_rot_indices)
final_vib_indices = left_vib_indices + right_vib_indices
final_rot_indices = left_rot_indices + right_rot_indices
final_coeff = (1/2) * new_left.coeff * new_right.coeff
vib_op_list = pure_vibration_commutator(new_left.vib_op, new_right.vib_op)
rot_op_list = [new_left.rot_op+new_right.rot_op, new_right.rot_op+new_left.rot_op]
final_terms = []
if vib_op_list == 0:
return 0
else:
for item in vib_op_list:
if item == 0:
pass
else:
vib_op = item[0]
kronecker_delta_rules = item[1]
multiplier = item[2]
for rot_op in rot_op_list:
new_term = Term(vib_op,
rot_op,
multiplier*final_coeff,
final_vib_indices,
final_rot_indices)
new_vib_indices = [x.subs(kronecker_delta_rules,
simultaneous=True) for x in final_vib_indices]
new_term = new_term.changeVibIndices(new_vib_indices)
final_terms.append(new_term)
return Expression(final_terms)
else:
return 0
def rotCommutator(self, other):
if self.n_rot_op == 0:
return 0
elif isinstance(other, Expression):
return Expression([self.rotCommutator(term) for term in other.items])
elif isinstance(other, Term):
if other.n_rot_op == 0:
return 0
else:
n_left_vib_indices = len(self.vib_indices)
n_left_rot_indices = len(self.rot_indices)
n_right_vib_indices = len(other.vib_indices)
n_right_rot_indices = len(other.rot_indices)
left_vib_indices = v[0:n_left_vib_indices]
right_vib_indices = v[n_left_vib_indices:(n_left_vib_indices + n_right_vib_indices)]
left_rot_indices = r[0:n_left_rot_indices]
right_rot_indices = r[n_left_rot_indices:(n_left_rot_indices + n_right_rot_indices)]
new_left = self.changeIndices(left_vib_indices, left_rot_indices)
new_right = other.changeIndices(right_vib_indices, right_rot_indices)
final_vib_indices = left_vib_indices + right_vib_indices
final_rot_indices = left_rot_indices + right_rot_indices
final_coeff = (1/2) * new_left.coeff * new_right.coeff
vib_op_list = [new_left.vib_op + new_right.vib_op, new_right.vib_op + new_left.vib_op]
rot_op_list = pure_rotation_commutator(new_left.rot_op,
new_right.rot_op,
new_left.rot_indices,
new_right.rot_indices)
final_terms = []
if rot_op_list == 0:
return 0
else:
for item in rot_op_list:
if item == 0:
pass
else:
rot_op = item[0]
new_indices = item[1]
multiplier = item[2]
for vib_op in vib_op_list:
new_term = Term(vib_op,
rot_op,
multiplier*final_coeff,
final_vib_indices,
final_rot_indices + list(new_indices))
final_terms.append(new_term)
final_expression = Expression(final_terms)
for term in final_expression:
ee_atoms = []
for atom in list(preorder_traversal(term.coeff)):
try:
if atom.func == ee:
ee_atoms.append(atom)
except:
pass
ee_replace_rules = {}
for atom in ee_atoms:
index1, index2, index3 = atom.args
if index1 != index2 and index1 != index3 and index2 != index3:
pass
else:
ee_replace_rules[atom] = 0
new_coefficient = term.coeff.subs(ee_replace_rules, simultaneous=True)
term.coeff = new_coefficient
return final_expression
else:
return 0
def toLadder(self):
new_coefficient = self.coeff
ladder_operators = []
for vib_op in self.vib_op:
index = list(preorder_traversal(vib_op))[1]
ladder_operators.append(lop(index, sigma(index)))
if vib_op.func == qop:
new_coefficient = new_coefficient*(1/2)
elif vib_op.func == pop:
new_coefficient = new_coefficient*(1/2)*I*sigma(index)
return LadderTerm(ladder_operators, self.rot_op, new_coefficient, self.vib_indices, self.rot_indices)
def printProperties(self):
print('vib_op (n={}): {}'.format(self.n_vib_op, self.vib_op))
print('rot_op (n={}): {}'.format(self.n_rot_op, self.rot_op))
print('vib_indices: {}'.format(self.vib_indices))
print('rot_indices: {}'.format(self.rot_indices))
print('coefficient: {}'.format(self.coeff))
def pure_vibration_commutator(left: list, right: list):
if len(left) == 0:
return 0
elif len(left) == 1:
if len(right) == 0:
return 0
elif len(right) == 1:
a = left[0]
b = right[0]
a_index = list(preorder_traversal(a))[1]
b_index = list(preorder_traversal(b))[1]
if a.func == b.func:
return 0
elif a.func == qop and b.func == pop:
kronecker_delta_rules = {a_index: b_index}
multiplier = I
return [[[], kronecker_delta_rules, multiplier]]
elif a.func == pop and b.func == qop:
kronecker_delta_rules = {a_index: b_index}
multiplier = (-1)*I
return [[[], kronecker_delta_rules, multiplier]]
else:
raise ValueError
elif len(right) > 1:
a = left[0]
b = right[0]
c = right[1:]
first_commutator = pure_vibration_commutator([a], c)
second_commutator = pure_vibration_commutator([a], [b])
final_list = []
if first_commutator == 0:
pass
else:
for item in first_commutator:
if item == 0:
pass
else:
vib_op = item[0]
kd_rules = item[1]
mult = item[2]
new_op = [b]
for op in vib_op:
new_op.append(op)
final_list.append([new_op, kd_rules, mult])
if second_commutator == 0:
pass
else:
for item in second_commutator:
if item == 0:
pass
else:
vib_op = item[0]
kd_rules = item[1]
mult = item[2]
for op in c:
vib_op.append(op)
final_list.append([vib_op, kd_rules, mult])
if len(final_list) == 0:
return 0
else:
return final_list
else:
raise ValueError
elif len(left) > 1:
a = left[0]
b = left[1:]
c = right
first_commutator = pure_vibration_commutator(b, c)
second_commutator = pure_vibration_commutator([a], c)
final_list = []
if first_commutator == 0:
pass
else:
for item in first_commutator:
if item == 0:
pass
else:
vib_op = item[0]
kd_rules = item[1]
mult = item[2]
new_op = [a]
for op in vib_op:
new_op.append(op)
final_list.append([new_op, kd_rules, mult])
if second_commutator == 0:
pass
else:
for item in second_commutator:
if item == 0:
pass
else:
vib_op = item[0]
kd_rules = item[1]
mult = item[2]
for op in b:
vib_op.append(op)
final_list.append([vib_op, kd_rules, mult])
if len(final_list) == 0:
return 0
else:
return final_list
else:
raise ValueError
def pure_rotation_commutator(left: list, right: list, left_indices: list, right_indices: list):
if len(left) == 0:
return 0
elif len(left) == 1:
if len(right) == 0:
return 0
elif len(right) == 1:
a = left[0]
b = right[0]
a_index = list(preorder_traversal(a))[1]
b_index = list(preorder_traversal(b))[1]
if a_index == b_index:
return 0
else:
used_indices = left_indices + right_indices
unused_indices = [i for i in r if i not in used_indices]
new_index = unused_indices[0]
multiplier = (-1)*I*ee(a_index, b_index, new_index)
rot_op = jop(new_index)
return [[[rot_op], [new_index], multiplier]]
elif len(right) > 1:
a = left[0]
b = right[0]
c = right[1:]
first_commutator = pure_rotation_commutator([a], c, left_indices, right_indices)
second_commutator = pure_rotation_commutator([a], [b], left_indices, right_indices)
final_list = []
if first_commutator == 0:
pass
else:
for item in first_commutator:
if item == 0:
pass
else:
rot_op = item[0]
new_indices = item[1]
mult = item[2]
new_op = [b]
for op in rot_op:
new_op.append(op)
final_list.append([new_op, new_indices, mult])
if second_commutator == 0:
pass
else:
for item in second_commutator:
if item == 0:
pass
else:
rot_op = item[0]
new_indices = item[1]
mult = item[2]
for op in c:
rot_op.append(op)
final_list.append([rot_op, new_indices, mult])
if len(final_list) == 0:
return 0
else:
return final_list
else:
raise ValueError
elif len(left) > 1:
a = left[0]
b = left[1:]
c = right
first_commutator = pure_rotation_commutator(b, c, left_indices, right_indices)
second_commutator = pure_rotation_commutator([a], c, left_indices, right_indices)
final_list = []
if first_commutator == 0:
pass
else:
for item in first_commutator:
if item == 0:
pass
else:
rot_op = item[0]
new_indices = item[1]
mult = item[2]
new_op = [a]
for op in rot_op:
new_op.append(op)
final_list.append([new_op, new_indices, mult])
if second_commutator == 0:
pass
else:
for item in second_commutator:
if item == 0:
pass
else:
rot_op = item[0]
new_indices = item[1]
mult = item[2]
for op in b:
rot_op.append(op)
final_list.append([rot_op, new_indices, mult])
if len(final_list) == 0:
return 0
else:
return final_list
else:
raise ValueError
class Expression:
def __init__(self, items_list):
self.items = []
for item in items_list:
if isinstance(item, Expression):
for sub_item in item.items:
self.items.append(sub_item)
elif isinstance(item, Term):
self.items.append(item)
elif item == 0:
pass
else:
raise ValueError('Unrecognized object encountered: {}'.format(item))
combined_terms = []
for term1 in self.items:
if any(term1.willCombineWith(term) for term in combined_terms):
pass
else:
combines_with_term1 = []
for term2 in self.items:
if term1.willCombineWith(term2):
combines_with_term1.append(term2)
combined_sum = combines_with_term1[0]
if len(combines_with_term1) > 1:
for term in combines_with_term1[1:]:
combined_sum += term
combined_terms.append(combined_sum)
final_terms = []
for term in combined_terms:
if term.coeff != 0:
final_terms.append(term)
self.items = final_terms
def __repr__(self):
return "{}".format([item for item in self.items])
def __add__(self, other):
if isinstance(other, Term):
return Expression([*self.items, other])
elif isinstance(other, Expression):
return Expression([*self.items, *other.items])
elif other == 0:
return self
else:
raise TypeError('Cannot add {} to GenericExpression object'.format(other))
def __sub__(self, other):
return self + (other*(-1))
def __mul__(self, other):
return Expression([item * other for item in self.items])
def __rmul__(self, other):
raise NotImplementedError
def __getitem__(self, item):
return self.items[item]
def __eq__(self, other):
if isinstance(other, Expression):
raise NotImplementedError('Need to define expression_sort function first.')
else:
return False
def __len__(self):
return len([item for item in self])
def vibCommutator(self, other):
return Expression([term.vibCommutator(other) for term in self.items])
def rotCommutator(self, other):
return Expression([term.rotCommutator(other) for term in self.items])
def toLadder(self):
return LadderExpression([term.toLadder() for term in self.items])
class LadderTerm:
def __init__(self, vib_op_list: list, rot_op_list: list, coefficient, vib_indices: list, rot_indices: list):
self._n_vib_op = len(vib_op_list)
self._n_rot_op = len(rot_op_list)
self._vib_indices = vib_indices
self._rot_indices = rot_indices
self._vib_op = vib_op_list
self._rot_op = rot_op_list
self._coeff = coefficient
@property
def vib_op(self):
return self._vib_op
@property
def rot_op(self):
return self._rot_op
@property
def coeff(self):
return self._coeff
@coeff.setter
def coeff(self, value):
self._coeff = value
@property
def n_vib_op(self):
return self._n_vib_op
@property