SOSpin is hosted by Hepforge, IPPP Durham
SOSpin  1.0.0
braket.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // SOSpin Library
3 // Copyright (C) 2015 SOSpin Project
4 //
5 // Authors:
6 //
7 // Nuno Cardoso (nuno.cardoso@tecnico.ulisboa.pt)
8 // David Emmanuel-Costa (david.costa@tecnico.ulisboa.pt)
9 // Nuno Gonçalves (nunogon@deec.uc.pt)
10 // Catarina Simoes (csimoes@ulg.ac.be)
11 //
12 // ----------------------------------------------------------------------------
13 // This file is part of SOSpin Library.
14 //
15 // SOSpin Library is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or any
18 // later version.
19 //
20 // SOSpin Library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with SOSpin Library. If not, see <http://www.gnu.org/licenses/>.
27 // ----------------------------------------------------------------------------
28 
29 // braket.cpp created on 27/02/2015
30 //
31 // This file contains the functions necessary to do things
32 // in the SOSpin Library.
33 //
34 // Revision 1.1 28/02/2015 23:19:29 david
35 // License updated
36 //
37 
38 
45 #include "index.h"
46 #include "dlist.h"
47 #include "timer.h"
48 #include "progressStatus.h"
49 #include "braket.h"
50 #include "son.h"
51 
52 
53 
54 namespace sospin {
55 
56 
57 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
58 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
59 
60 
61 static bool FlagSimplifyGlobalIndexSum = true;
62 
64  FlagSimplifyGlobalIndexSum = true;
65 }
66 
68  FlagSimplifyGlobalIndexSum = false;
69 }
70 
71 
72 
74  index = 0;
75  constpart = "";
76  }
78  index = 0;
79  constpart = "";
80  term.push_back(d0);
81  }
82  BraketOneTerm::BraketOneTerm(int indexin, string constpartin, const DList &d0){
83  index = indexin;
84  constpartin.erase(std::remove(constpartin.begin(), constpartin.end(), ' '), constpartin.end());
85  constpart = constpartin;
86  term.push_back(d0);
87  }
88  BraketOneTerm::BraketOneTerm(int indexin, string constpartin, list<DList> termin){
89  index = indexin;
90  constpartin.erase(std::remove(constpartin.begin(), constpartin.end(), ' '), constpartin.end());
91  constpart = constpartin;
92  term = termin;
93  }
94  BraketOneTerm::BraketOneTerm(int indexin, string constpartin, BraketOneTerm& termin){
95  index = indexin;
96  //remove spaces from constant part written in a string
97  constpartin.erase(std::remove(constpartin.begin(), constpartin.end(), ' '), constpartin.end());
98  constpart = constpartin;
99  list<DList>::iterator iter;
100  for(iter = termin.term.begin(); iter != termin.term.end(); iter++)
101  term.push_back(*iter);
102  }
104  index = 0;
105  constpart = a;
106  }
108  index = 0;
109  constpart.clear();
110  term.clear();
111  }
113  clear();
114  }
115  list<DList>& BraketOneTerm::GetTerm(){ return term; }
116  string& BraketOneTerm::GetConst(){ return constpart; }
117  int& BraketOneTerm::GetIndex(){ return index; }
119  if(constpart.empty() && term.empty()) return true;
120  return false;
121  }
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
141  flag=0;
142  operation = none;
143  evaluated = 0;
144  }
145  Braket::Braket(const DList &d0){
146  flag=0;
147  operation = none;
148  evaluated = 0;
149  BraketOneTerm tmp(d0);
150  expression.push_back(tmp);
151  }
153  flag=0;
154  evaluated = 0;
155  operation = none;
156  expression.push_back(term);
157 
158  }
160  flag=0;
161  evaluated = 0;
162  operation = op;
163  expression.push_back(term);
164 
165  }
166  Braket::Braket(int id, string a, DList d0){
167  flag=0;
168  evaluated = 0;
169  operation = none;
170  BraketOneTerm tmp(d0);
171  expression.push_back(tmp);
172  }
173  Braket::Braket(int id, string a, DList d0, OPMode op){
174  flag=0;
175  evaluated = 0;
176  operation = op;
177  BraketOneTerm tmp(id, a, d0);
178  expression.push_back(tmp);
179  }
181  flag=L.flag;
183  operation = L.operation;
184  evaluated = L.evaluated;
185  }
186 
187  Braket::Braket(int id, string a, const Braket &L, OPMode op){
188  flag=0;
189  vector< BraketOneTerm >::const_iterator iter;
190  for(iter = L.expression.begin(); iter != L.expression.end(); iter++){
191  BraketOneTerm tmp0 = (*iter);
192  BraketOneTerm tmp(id, a, tmp0);
193  expression.push_back(tmp);
194  }
195  operation = op;
196  evaluated = L.evaluated;
197  }
198 
199 
200  void Braket::expfromForm(vector<string> a){
201  flag=0;
202  BraketOneTerm tmp;
203  for(int i=0; i<a.size();i++){
204  tmp.expfromForm(a.at(i));
205  expression.push_back(tmp);
206  }
207  operation = braket;
208  evaluated = 2;
209  }
210 
211 
213  expression.clear();
214  operation = none;
215  flag = 0;
216  evaluated = 0;
217 
218  }
221  clear();
222  };
223 
224 
226  flag=1;
227 }
228 
230  flag=0;
231 }
232 
233 
234 
235 
237  int Braket::size(){ return expression.size();}
238 
241  if(pos < 0 || pos >= expression.size()){
242  cout << "Outside range..." << endl;
243  exit(1);
244  }
245  return expression.at(pos);
246  }
247  /* \brief Return/set the index sum of the term given by pos */
248  int& Braket::GetIndex(int pos){
249  if(pos < 0 || pos >= expression.size()){
250  cout << "Outside range..." << endl;
251  exit(1);
252  }
253  return expression.at(pos).GetIndex();
254  }
255 
257  return operation;
258  }
259 
260 
266 ostream& operator<<(ostream& out,const OPMode &a){
267  switch( a ) {
268  case none:
269  out<<"none";
270  break;
271  case bra:
272  out<<"bra";
273  break;
274  case ket:
275  out<<"ket";
276  break;
277  case braket:
278  out<<"braket";
279  break;
280  }
281  return out;
282 }
283 
284 
291 OPMode operator-(const OPMode a, const OPMode b){
292  if(a!=b){
293  cout << "Invalid Operation: " << a << " - " << b << endl;
294  exit(1);
295  }
296  return a;
297 }
304 OPMode operator+(const OPMode a, const OPMode b){
305  if(a!=b){
306  cout << "Invalid Operation: " << a << " + " << b << endl;
307  exit(1);
308  }
309  return a;
310 }
317 OPMode operator*(const OPMode a, const OPMode b){
318  if(a==bra && b==ket) return braket;
319  if(a==bra && b==none) return bra;
320  if(a==none && b==ket) return ket;
321  if(a==none && b==none) return none;
322  if(a==braket && b==braket){
323  return braket; //must check if expression was evaluated!!!!
324  }
325  cout << "Operation not permited: " << a << " * " << b << endl;
326  exit(1);
327 }
329 /* \brief Print the Braket expression mode, ie, the type of Braket: bra/ket/braket or none*/
331  cout << "Expression mode: " << operation << endl;
332 }
333 
334 
335 
336 
337 
338 
339 
340 int expevaluationtype(int eval0, int eval1){
341  if( (eval0 == 0 && eval1 > 0 ) || (eval0 > 0 && eval1 == 0) ){
342  cout << "Cannot perform this operation, not all expression were evaluate...\n Exiting..." << endl;
343  exit(1);
344  }
345  else return MAX(eval0, eval1);
346 }
347 
348 
349 
350 
351 
355  operation = L.operation;
356  evaluated = L.evaluated;
357  return *this;
358 }
359 
360 
361 
362 
364 // OPERATION: negate
366  BraketOneTerm tmp = L;
367  if(tmp.term.empty()) tmp.constpart = "-1*"+tmp.constpart;
368  else{
369  list<DList>::iterator iter;
370  for(iter = tmp.term.begin(); iter != tmp.term.end(); iter++)
371  (*iter).negate();
372  }
373  return tmp;
374 }
375 void BraketOneTerm::neg(){
376  if(term.empty()) constpart = "-1*"+constpart;
377  else{
378  list<DList>::iterator iter;
379  for(iter = term.begin(); iter != term.end(); iter++)
380  (*iter).negate();
381  }
382 }
383 
385  Braket tmp = L;
386  vector< BraketOneTerm >::iterator iter;
387  for(iter = tmp.expression.begin(); iter != tmp.expression.end(); iter++)
388  (*iter).neg();
389  return tmp;
390 }
393 // OPERATION: -=
394 Braket Braket::operator-=(const Braket &L){
395  operation = operation - L.operation;
396  evaluated = expevaluationtype(evaluated, L.evaluated);
397  vector< BraketOneTerm >::const_iterator iter;
398  for(iter = L.expression.begin(); iter != L.expression.end(); iter++){
399  expression.push_back(-(*iter));
400  }
401  rearrange();
402  simplify();
403  return *this;
404 
405 }
407  Braket tmp;
408  tmp.operation = operation - L.operation;
409  tmp.expression=expression;
410  tmp.evaluated = expevaluationtype(evaluated, L.evaluated);
411 
412  vector< BraketOneTerm >::const_iterator iter;
413  for(iter = L.expression.begin(); iter != L.expression.end(); iter++)\
414  tmp.expression.push_back(-(*iter));
415  tmp.rearrange();
416  tmp.simplify();
417  return tmp;
418 }
421 // OPERATION: +=
422 Braket Braket::operator+=(const Braket &L){
423  operation = operation + L.operation;
424  evaluated = expevaluationtype(evaluated, L.evaluated);
425  vector< BraketOneTerm >::const_iterator iter;
426  for(iter = L.expression.begin(); iter != L.expression.end(); iter++)\
427  expression.push_back(*iter);
428  rearrange();
429  simplify();
430  return *this;
431 }
433  Braket tmp;
434  tmp.operation = operation + L.operation;
435  tmp.expression=expression;
436  tmp.evaluated = expevaluationtype(evaluated, L.evaluated);
437  vector< BraketOneTerm >::const_iterator iter;
438  for(iter = L.expression.begin(); iter != L.expression.end(); iter++)\
439  tmp.expression.push_back(*iter);
440  tmp.rearrange();
441  tmp.simplify();
442  return tmp;
443 }
446 // OPERATION: *= string
447 BraketOneTerm BraketOneTerm::operator*=(const string constval){
448  if(constpart.empty()) constpart = constval;
449  else constpart += "*" + constval;
450  return *this;
451 }
452 Braket Braket::operator*=(const string constval){
453  vector< BraketOneTerm >::iterator iter;
454  for(iter = expression.begin(); iter != expression.end(); iter++)\
455  *iter *= constval;
456  return *this;
457 }
460 // OPERATION: * string
462  BraketOneTerm tmp;
463  tmp.index = index;
464  tmp.term = term;
465  if(constpart.empty()) tmp.constpart = constval;
466  else tmp.constpart = constpart + "*" + constval;
467  return tmp;
468 }
469 Braket Braket::operator*(const string constval){
470  Braket tmp;
471  tmp.operation = operation;
472  tmp.expression=expression;
473  tmp.evaluated = evaluated;
474  vector< BraketOneTerm >::iterator iter;
475  for(iter = tmp.expression.begin(); iter != tmp.expression.end(); iter++)\
476  *iter *= constval;
477 
478  return tmp;
479 }
482 // OPERATION: *
484  if( (term.empty() && constpart.empty()) ) return *this;
485  if( (L.term.empty() && L.constpart.empty()) ) return L;
486 
487  BraketOneTerm tmp;
488  tmp.index = index + L.index;
489  if(constpart.empty()) tmp.constpart = L.constpart;
490  else if(L.constpart.empty()) tmp.constpart = constpart;
491  else tmp.constpart = constpart + "*" + L.constpart;
492 
493  list<DList>::iterator iter;
494  list<DList>::const_iterator liter;
495  if(term.empty()){
496  for(liter =L.term.begin(); liter != L.term.end(); liter++)
497  tmp.term.push_back((*liter));
498  }
499  else if(L.term.empty()){
500  for(iter = term.begin(); iter != term.end(); iter++)
501  tmp.term.push_back((*iter));
502 
503  }else{
504  for(iter = term.begin(); iter != term.end(); iter++)
505  for(liter =L.term.begin(); liter != L.term.end(); liter++)
506  tmp.term.push_back((*iter)*(*liter));
507  }
508  return tmp;
509 }
511  Braket tmp;
512  if(operation==braket && L.operation==braket){
513  if(evaluated == 0) {
514  cout << operation <<"::::"<< L.operation<<endl;
515  cout << "Evaluate both expressions first before doing the multiplication!!!!" << endl;
516  cout << "Multiplication between two braket's only allowed after evaluation!!!!" << endl;
517  exit(1);
518  }
519  }
520  tmp.evaluated = expevaluationtype(evaluated, L.evaluated);
521  tmp.operation=operation*L.operation;
522  vector< BraketOneTerm >::iterator iter;
523  vector< BraketOneTerm >::const_iterator liter;
524  for(iter = expression.begin(); iter != expression.end(); iter++)
525  for(liter =L.expression.begin(); liter != L.expression.end(); liter++)
526  tmp.expression.push_back((*iter)*(*liter));
527  tmp.rearrange();
528  tmp.simplify();
529  return tmp;
530 }
533 // OPERATION: *=
534 Braket& Braket::operator*=(const Braket &L){
535  if(operation==braket && L.operation==braket){
536  if(evaluated == 0) {
537  cout << operation <<"::::"<< L.operation<<endl;
538  cout << "Evaluate both expressions first before doing the multiplication!!!!" << endl;
539  cout << "Multiplication between two braket's only allowed after evaluation!!!!" << endl;
540  exit(1);
541  }
542  }
543  evaluated = expevaluationtype(evaluated, L.evaluated);
544  operation=operation*L.operation;
545  vector< BraketOneTerm >::iterator iter;
546  vector< BraketOneTerm >::const_iterator liter;
547  vector< BraketOneTerm > tmp;
548  for(iter = expression.begin(); iter != expression.end(); iter++)
549  for(liter =L.expression.begin(); liter != L.expression.end(); liter++)
550  tmp.push_back((*iter)*(*liter));
551  expression.clear();
552  expression = tmp;
553  tmp.clear();
554  rearrange();
555  simplify();
556  return *this;
557 }
560 // STREAM OPERATORS
561 ostream& operator<<(ostream& out, const BraketOneTerm &L){
562  if(L.term.empty()) out << L.constpart;
563  else{
564  if(L.constpart.empty()) out << "(" << endl;
565  else out << "("<<L.constpart<<")" << " * (" << endl;
566  list<DList>::const_iterator iter;
567  for (iter =L.term.begin(); iter != L.term.end(); iter++){
568  out << "\t";
569  DList tmp = (*iter);
570  tmp.set_begin();
571  if(tmp.getSign() == -1) out << " - ";
572  if(tmp.getSign() == 1) out << " + ";
573  if(tmp.isEmpty()) out << " 0 ";
574  else
575  while(true) {
576  elemType elem = tmp.get();
577  if(elem.getType() == 2)
578  out << "d_(" << getIdx(elem.getIdx1()) << "," << getIdx(elem.getIdx2())<< ")";
579  if(elem.getType() == 0)
580  out << "b(" << getIdx(elem.getIdx1()) << ")";
581  if(elem.getType() == 1)
582  out << "bt(" << getIdx(elem.getIdx1()) << ")";
583  if(elem.getType() == 3)
584  out << "1";
585  if(tmp.isActualLast())
586  break;
587  else
588  out << " * ";
589  tmp.shift_right();
590  }
591  out << endl;
592  }
593  out << ")";
594  }
595  return out;
596 }
597 
598 ostream& operator<<(ostream& out, const Braket &L){
599  int R=0;
600  //static int R=0;
601  if(L.expression.size() == 0) out << "Local R" << ++R << " = 0;";
602  else
603  for(int i = 0; i < L.expression.size(); i++){
604  if(L.flag) out << "Local R" << ++R << " = ";
605  if(L.expression.empty()) out << "0;" << endl;
606  else out << L.expression.at(i) << ";" << endl;
607  }
608  return out;
609 }
610 
611 
612 string& operator<<(string& out, const Braket &L){
613  return out << L;
614 }
615 
616 
617 string& operator+(string& out, const Braket &L){
618  return out << L;
619 }
620 
621 
624 // OPERATION: rearrange()
625 void BraketOneTerm::rearrange(){
626  list<DList>::iterator iter;
627  for(iter = term.begin(); iter != term.end(); iter++)
628  (*iter) = (*iter).rearrange();
629 }
630 void Braket::rearrange(){
631  if(evaluated==2) return;
632  if(getVerbosity() >= VERBOSE) cout << "Ordering..." << endl;
633  int total = expression.size();
634  int i = 0;
635  DoProgress("Progress: ", 0, total);
636  vector< BraketOneTerm >::iterator iter;
637  for(iter = expression.begin(); iter != expression.end(); iter++){
638  (*iter).rearrange();
639  i++;
640  DoProgress("Progress: ", i, total);
641  }
642 }
645 // OPERATION: checkindex()
646 bool BraketOneTerm::checkindex(){
647  if(index==0 || abs(index)==getDim()/2) return true;
648  else return false;
649 }
650 void Braket::checkindex(){
651  if(FlagSimplifyGlobalIndexSum) {
652  if(operation == braket){
653  if(getVerbosity() >= VERBOSE) cout << "Checking Indices..." << endl;
654  int total = expression.size();
655  DoProgress("Progress: ", 0, total);
656  vector< BraketOneTerm > tmp;
657  for (int i = 0; i < expression.size(); i++){
658  if(expression.at(i).checkindex()){
659  tmp.push_back(expression.at(i));
660  expression.at(i).clear();
661  }
662  DoProgress("Progress: ", i+1, total);
663  }
664  expression.clear();
665  expression = tmp;
666  }
667  }
668 }
669 
670 
673 // OPERATION: gindexsetnull()
674 void Braket::gindexsetnull(){
675  if(getVerbosity() >= VERBOSE) cout << "Setting global indice terms to zero..." << endl;
676  int total = expression.size();
677  DoProgress("Progress: ", 0, total);
678  for (int i = 0; i < expression.size(); i++){
679  expression.at(i).GetIndex() = 0;
680  DoProgress("Progress: ", i+1, total);
681  }
682 }
683 
686 // OPERATION: Simplify()
687 bool BraketOneTerm::Simplify(OPMode operation){
688  list<DList>::iterator iter = term.begin();
689  while(iter != term.end()){
690  bool checkop = false;
691  switch( operation ) {
692  case none:
693  if( (*iter).checkDeltaIndex() )
694  checkop = true;
695  break;
696  case bra:
697  if( (*iter).search_first(1,0) )
698  if( (*iter).checkDeltaIndex() )
699  checkop = true;
700  break;
701  case ket:
702  if( (*iter).search_last(0) )
703  if( (*iter).checkDeltaIndex() )
704  checkop = true;
705  break;
706  case braket:
707  if( (*iter).check_same_num() )
708  if( (*iter).search_last(0) )
709  if( (*iter).search_first(1,0) )
710  if( (*iter).checkDeltaIndex() )
711  checkop = true;
712  break;
713  }
714  if(!checkop) {
715  (*iter).clear();
716  iter = term.erase(iter);
717  }
718  else{
719  ++iter;
720  }
721  }
722  if(term.empty()) {
723  clear();
724  return true;
725  }
726  return false;
727 }
728 void Braket::simplify(){
729  checkindex();
730  if(evaluated != 2){
731  if(getVerbosity() >= VERBOSE) cout << "Simplifying expression..." << endl;
732  int total = expression.size();
733  int i = 0;
734  DoProgress("Progress: ", i, total);
735  vector< BraketOneTerm > tmp;
736  for (i = 0; i < expression.size(); i++){
737  BraketOneTerm tmp0;
738  tmp0 = expression.at(i);
739  if(!tmp0.Simplify(operation)) tmp.push_back(tmp0);
740  DoProgress("Progress: ", i+1, total);
741  }
742  expression.clear();
743  expression = tmp;
744  }
745 }
746 
747 
748 
749 
750 
751 
752 
753 
754 
755 string GetLeviCivita(vector < string> id0, vector < string> id1){
756  string epsB = "e_(";
757  string epsBdagger = "e_(";
758  //Number of B's and B\dagger's are the same, every call to simplify removes non-valid/null terms!!!!
759  for(int j = 0; j < id0.size(); j++){
760  epsB += id0.at(j);
761  epsBdagger += id1.at(id1.size() - 1 - j);
762  if(j < getDim() / 2 - 1) {
763  epsB += ",";
764  epsBdagger += ",";
765  }
766  }
767  int idadd = 1;
768  int factor=1;
769  if(id0.size() < getDim() / 2){
770  int len = getDim() / 2 - id0.size();
771  for(int j = 0; j < len; j++){
772  string addrem = "t" + ToString<int>(idadd);
773  newId(addrem);
774  factor *= idadd;
775  idadd++;
776  epsB += addrem;
777  epsBdagger += addrem;
778  if(j < len - 1) {
779  epsB += ",";
780  epsBdagger += ",";
781  }
782  }
783  }
784  epsB += ")";
785  epsBdagger += ")";
786  string levciviexp = epsB + "*" + epsBdagger;
787  if(factor > 1)
788  levciviexp += "/" + ToString<int>(factor);
789  return levciviexp;
790 }
791 
792 
793 
798 void BraketOneTerm::EvaluateEps_2ndPass(OPMode oper ){
799  if(oper != braket) return;
800  list<DList>::iterator iter = term.begin();
801  string constpartout = "*(\n";
802  if(constpart.empty()) constpartout = "(\n";
803  while (iter != term.end()){
804  if((*iter).isEmpty()==false){
805  vector < string> id0;
806  vector < string> id1;
807  int sign0 = 1;
808  bool bandbdagger = false;
809  (*iter).getBandBdaggerIds(bandbdagger, id0, id1, sign0);
810  if( sign0 == -1) constpartout+= "-";
811  if( sign0 == 1) constpartout+= "+";
812  string deltas = printDeltas((*iter));
813  constpartout += deltas;
814  if(bandbdagger && deltas.empty()==false) constpartout+= "*";
815  if(bandbdagger)
816  constpartout += GetLeviCivita(id0, id1);
817  constpartout += "\n";
818  }
819  ++iter;
820  }
821  constpartout += ")";
822  term.clear();
823  constpart += constpartout;
824  constpartout.clear();
825 }
826 
827 
828 
829 
830 
831 
832 
838 void OrderBandBdaggers(list<DList> &Toeval, OPMode oper){
839  list<DList>::iterator iter = Toeval.begin();
840  bool braketmode = false;
841  if(oper == braket) braketmode = true;
842  while (iter != Toeval.end()){
843  bool inc_iter = true;
844  if((*iter).isEmpty()==false){
845  DList LL;
846  LL << (*iter);
847  vector<int> ids = LL.getIds();
848  bool ordered = false;
849  vector<int> ids0 = ids;
850  sort (ids0.begin(), ids0.end());
851  if(ids0 == ids) ordered = true;
852  if(ordered == false)
853  while(true){
854  DList L;
855  L << (*iter);
856  ids = L.getIds();
857  ids0 = ids;
858  sort (ids0.begin(), ids0.end());
859  if(ids0 == ids) ordered = true;
860  if(ordered) break;
861  DList M;
862  M = ordering(L, braketmode);
863  if(M.isEmpty() == false){
864  if(oper == ket || oper == braket ){
865  M.search_last(0);
866  if(M.isActualLast()==false){
867  Toeval.push_back(M);
868  }
869  }
870  else Toeval.push_back(M);
871  }
872  if(L.isEmpty()) {
873  (*iter).clear();
874  iter = Toeval.erase(iter);
875  inc_iter = false;
876  break;
877  }
878  else if(oper == ket || oper == braket){
879  L.search_last(0);
880  if(L.isActualLast()) {
881  (*iter).clear();
882  iter = Toeval.erase(iter);
883  inc_iter = false;
884  break;
885  }
886  }
887  (*iter) = L;
888  }
889  }
890  if(inc_iter) ++iter;
891  }
892 }
893 
894 
895 
901 void ReduceNumberOfBandBdaggers(list<DList> &Toeval, OPMode oper){
902  list<DList>::iterator iter = Toeval.begin();
903  bool braketmode = false;
904  if(oper == braket) braketmode = true;
905  while (iter != Toeval.end()){
906  bool inc_iter = true;
907  if((*iter).isEmpty()==false)
908  if((*iter).search_last(0))
909  while(true){
910  if((*iter).search_elem(0)==false) break;
911  if((*iter).numBs()<=getDim()/2) break;
912  DList L;
913  L << (*iter);
914  DList M;
915  M = contract_deltas(L, braketmode);
916  if(M.isEmpty() == false){
917  if(oper == ket || oper == braket ){
918  M.search_last(0);
919  if(M.isActualLast()==false){
920  Toeval.push_back(M);
921  }
922  }
923  else Toeval.push_back(M);
924  }
925  if(L.isEmpty()) {
926  (*iter).clear();
927  iter = Toeval.erase(iter);
928  inc_iter = false;
929  break;
930  }
931  else if(oper == ket || oper == braket){
932  L.search_last(0);
933  if(L.isActualLast()) {
934  (*iter).clear();
935  iter = Toeval.erase(iter);
936  inc_iter = false;
937  break;
938  }
939  }
940  (*iter) = L;
941  }
942  if(inc_iter) ++iter;
943  }
944 }
945 
946 
947 
953 bool BraketOneTerm::EvaluateEps_1stPass(OPMode oper ){
954  //evaluate expression in order to have b's+b^daggers <= N of SO(N)
955  ReduceNumberOfBandBdaggers(term, oper);
956  // evaluate expression in order to have all b's at the left side and b^daggers at right side
957  OrderBandBdaggers(term, oper);
958  if(term.empty()) return true;
959  return false;
960 }
961 
966 bool BraketOneTerm::EvaluateToLeviCivita(OPMode oper ){
967  EvaluateEps_1stPass(oper);
968  if(term.empty()) return true;
969  EvaluateEps_2ndPass(oper);
970  return false;
971 }
972 
973 
974 
979 bool BraketOneTerm::EvaluateToDeltas(OPMode oper ){
980  list<DList>::iterator iter = term.begin();
981  bool braketmode = false;
982  if(oper == braket) braketmode = true;
983  while (iter != term.end()){
984  bool inc_iter = true;
985  if((*iter).isEmpty()==false)
986  if((*iter).search_last(0))
987  while(true){
988  if((*iter).search_elem(0)==false) break;
989  if( oper == none && ((*iter).search_elem(0)==false || (*iter).search_elem(1)==false) ) break;
990  DList L;
991  L << (*iter);
992  DList M;
993  M = contract_deltas(L, braketmode);
994  if(M.isEmpty() == false){
995  if(oper == ket || oper == braket ){
996  M.search_last(0);
997  if(M.isActualLast()==false){
998  term.push_back(M);
999  }
1000  }
1001  else term.push_back(M);
1002  }
1003  if(L.isEmpty()) {
1004  (*iter).clear();
1005  iter = term.erase(iter);
1006  inc_iter = false;
1007  break;
1008  }
1009  else if(oper == ket || oper == braket){
1010  L.search_last(0);
1011  if(L.isActualLast()) {
1012  (*iter).clear();
1013  iter = term.erase(iter);
1014  inc_iter = false;
1015  break;
1016  }
1017  }
1018  (*iter) = L;
1019  }
1020  if( inc_iter) ++iter;
1021  }
1022  if(term.empty()){
1023  constpart.clear();
1024  index = 0;
1025  return true;
1026  }
1027  return false;
1028 }
1029 
1030 
1034 void Braket::evaluate(bool onlydeltas){
1035  if(getVerbosity() >= VERBOSE) cout << "Evaluating Expression..." << endl;
1037  simplify();
1038  if(evaluated==0){
1040  if(onlydeltas){
1041  vector< BraketOneTerm >::iterator iter = expression.begin();
1042  int i = 0;
1043  int total = expression.size();
1044  DoProgress("Progress: ", i, total);
1045  while(iter != expression.end()){
1046  if((*iter).EvaluateToDeltas(operation)){
1047  (*iter).clear();
1048  iter = expression.erase(iter);
1049  }
1050  else ++iter;
1051  i++;
1052  DoProgress("Progress: ", i, total);
1053  }
1054  if(operation == braket) evaluated = 1;
1055  }
1056  else{
1057  if(operation != braket) return;
1058  int i = 0;
1059  int total = expression.size();
1060  DoProgress("Progress: ", i, total);
1061  vector< BraketOneTerm >::iterator iter = expression.begin();
1062  while(iter != expression.end()){
1063  if((*iter).EvaluateToLeviCivita(operation)){
1064  (*iter).clear();
1065  iter = expression.erase(iter);
1066  }
1067  else ++iter;
1068  i++;
1069  DoProgress("Progress: ", i, total);
1070  }
1071  if(operation == braket) evaluated = 2;
1072  }
1074  }
1075  if(evaluated>0) gindexsetnull();
1076 }
1077 
1078 
1079 
1080 
1081 
1082 
1083 }
string GetLeviCivita(vector< string > id0, vector< string > id1)
Definition: braket.cpp:755
OPMode & Type()
Definition: braket.cpp:256
unsigned int getType()
Returns the type of the element. Ex.: $b$ - type=0; $b^{}$ - type=1; $$ - type=2; constant - type=3...
Definition: dlist.h:111
Definition: enum.h:54
void setON()
Activate expression term numbering for output writing for each term "Local R?=".
Definition: braket.cpp:225
OPMode operator-(const OPMode a, const OPMode b)
calculate the mode for the subtraction
Definition: braket.cpp:291
int getDim()
Get the group dimension.
Definition: son.cpp:64
Braket operator-(const Braket &L)
Definition: braket.cpp:384
Functions and cointainer for indices.
Verbosity getVerbosity()
Return current verbosity level.
Definition: son.cpp:84
elemType get()
Returns elemtype of the node being pointed by actual (current element).
Definition: dlist.h:379
OPMode operator*(const OPMode a, const OPMode b)
calculate the mode for the multiplication
Definition: braket.cpp:317
Definition: enum.h:52
bool Simplify(OPMode operation)
Simplify current expression term.
Definition: braket.cpp:687
void setOFF()
Deactivate expression term numbering for output writing for each term "Local R?=".
Definition: braket.cpp:229
template string ToString< int >(int number)
unsigned int getIdx2()
Returns the second data field of the element (id.y).
Definition: dlist.h:134
Store expression...
Definition: braket.h:198
~Braket()
Destructor, clear all allocated memory.
Definition: braket.cpp:220
void ReduceNumberOfBandBdaggers(list< DList > &Toeval, OPMode oper)
Evaluate expression in order to have the number of b's plus b^daggers equal to N of SO(N) ...
Definition: braket.cpp:901
int index
Store the index sum.
Definition: braket.h:86
void expfromForm(vector< string > a)
To pass a expression from form.
Definition: braket.cpp:200
enum OPMode_s OPMode
void clear()
Clear all allocated memory and sets default parameters.
Definition: braket.cpp:107
void shift_right()
Shifts actual pointer to next node. If actual node is the end node, stops.
Definition: dlist.h:361
void clear()
Deletes all nodes from DList.
Definition: dlist.cpp:131
Defintions for all general (initialisation etc.) routines of class Braket.
Defintions for all general (initialisation etc.) routines of class Timer.
Defintions for all general (initialisation etc.) routines of class DList.
bool isEmpty()
Returns true if DList has no nodes.
Definition: dlist.h:440
void simplify()
Simplify expression. Apply the following rules: Also checks for numeric deltas and evaluates them...
Definition: braket.cpp:728
void expfromForm(string a)
To pass a expression from form.
Definition: braket.cpp:103
string & operator<<(string &out, const Braket &L)
Definition: braket.cpp:612
int & GetIndex(int pos)
Return/set the index sum of the term given by pos.
Definition: braket.cpp:248
void clear()
Clear all allocated memory and sets default parameters.
Definition: braket.cpp:212
vector< BraketOneTerm > expression
Store expressions with b's, b^ and delta's.
Definition: braket.h:200
unsigned int evaluated
Store the evaluated state of the expression.
Definition: braket.h:206
Definition: enum.h:66
DList ordering(DList &L, bool braketmode)
Order only the b's (to the left hand side) and b's (to the right hand side) terms. Applies the following identity: input DList L keeps delta term and function returns the swapped term.
Definition: dlist.cpp:911
void mode()
Definition: braket.cpp:330
OPMode operator+(const OPMode a, const OPMode b)
calculate the mode for the sum
Definition: braket.cpp:304
string getIdx(int i)
Get index.
Definition: index.cpp:142
int size()
Return number of terms in current expression.
Definition: braket.cpp:237
string printDeltas(DList &L)
Creates and returns a string with the deltas and constants of a DList.
Definition: dlist.cpp:975
Store each term of the Braket class.
Definition: braket.h:83
void unsetSimplifyIndexSum()
Deactivate internal simplifications based on the Braket Index sum.
Definition: braket.cpp:67
Braket(void)
Constructor, default expression mode is none.
Definition: braket.cpp:140
DList with nodes.
Definition: dlist.h:264
Braket operator=(const Braket &L)
overload operator for Braket = L
Definition: braket.cpp:353
DList contract_deltas(DList &L, bool braketmode)
Applies the following identity: input DList L keeps delta term and function returns the swapped term...
Definition: dlist.cpp:845
vector< int > getIds()
Creates and returns an integer vector sequence container with the ids (data fields) of $b$'s and $b^$...
Definition: dlist.cpp:361
Definition: enum.h:55
Main Sospin header file. Includes C++ macros, to simplify expression writing, B operator, Verbosity level and memory usage.
list< DList > term
Store the part with b and b and/or delta or identity.
Definition: braket.h:90
int getSign()
Returns the sign of DList.
Definition: dlist.h:384
ostream & operator<<(ostream &out, const OPMode &a)
Get the mode of the expression.
Definition: braket.cpp:266
void rearrange()
Order nodes of DList in Braket.
Definition: braket.cpp:630
OPMode operation
Store type of operation: none, bra, ket or braket.
Definition: braket.h:204
void setSimplifyIndexSum()
Activate internal simplifications based on the Braket Index sum. This option is activated by default...
Definition: braket.cpp:63
list< DList > & GetTerm()
Return (and set) the term part.
Definition: braket.cpp:115
#define MAX(x, y)
Definition: braket.cpp:57
bool isActualLast()
Returns true if actual pointer is pointing to the last (end) node of DList.
Definition: dlist.h:435
bool search_last(unsigned int type1)
Search the last element with "data.get\_type()==type1" found in DList. Returns true a node was found...
Definition: dlist.cpp:499
void DoProgress(string label, unsigned int step, unsigned int total, unsigned int interval)
void newId(string i)
Store new index of type "string".
Definition: index.cpp:125
void print_process_mem_usage()
Prints memory stats (current and peak resident set size) in MB.
Definition: son.cpp:316
int & GetIndex()
Return (and set) the index sum part.
Definition: braket.cpp:117
string & operator+(string &out, const Braket &L)
Definition: braket.cpp:617
BraketOneTerm()
Constructor.
Definition: braket.cpp:73
string constpart
Store the constant part.
Definition: braket.h:88
~BraketOneTerm()
Destructor, clear all allocated memory.
Definition: braket.cpp:112
int flag
Flag to make the term numeration with ostream operator.
Definition: braket.h:202
string & GetConst()
Return (and set) the constant part.
Definition: braket.cpp:116
b element
Definition: dlist.h:70
bool isempty()
Returns true if expression is empty.
Definition: braket.cpp:118
int expevaluationtype(int eval0, int eval1)
Definition: braket.cpp:340
void set_begin()
Changes actual pointer to point at the first element of DList (beg pointer).
Definition: dlist.h:321
void OrderBandBdaggers(list< DList > &Toeval, OPMode oper)
Evaluate expression in order to left all b's in left side and b^daggers in right side.
Definition: braket.cpp:838
Definition: enum.h:53
BraketOneTerm & Get(int pos)
Return expression term at position given by pos.
Definition: braket.cpp:240
Specific functions progress status bar.
unsigned int getIdx1()
Returns the first data field of the element (id.x).
Definition: dlist.h:126