|
1 | 1 |
|
2 | 2 | #include "Cleanup.hh" |
3 | 3 | #include "algorithms/sort_product.hh" |
| 4 | +#include "properties/Trace.hh" |
4 | 5 |
|
5 | 6 | using namespace cadabra; |
6 | 7 |
|
@@ -88,6 +89,61 @@ Algorithm::result_t sort_product::apply(iterator& st) |
88 | 89 | } |
89 | 90 | } |
90 | 91 |
|
| 92 | + if(ret==result_t::l_no_action) { |
| 93 | + bool found=false; |
| 94 | + bool ascend=true; |
| 95 | + iterator parn=st; |
| 96 | + // It is fine to cycle when we have a sum of a sum inside a trace |
| 97 | + while(ascend && !tr.is_head(parn)) { |
| 98 | + parn=tr.parent(parn); |
| 99 | + const Trace *trace=kernel.properties.get<Trace>(parn); |
| 100 | + if(trace) { |
| 101 | + ascend=false; |
| 102 | + found=true; |
| 103 | + } |
| 104 | + else if(*parn->name=="\\indexbracket") { |
| 105 | + ascend=false; |
| 106 | + if(number_of_indices(parn)==2) { |
| 107 | + index_iterator first=begin_index(parn); |
| 108 | + index_iterator last=first; |
| 109 | + ++last; |
| 110 | + if(*first->name==*last->name) found=true; |
| 111 | + } |
| 112 | + } |
| 113 | + else if(*parn->name!="\\sum") { |
| 114 | + ascend=false; |
| 115 | + } |
| 116 | + } |
| 117 | + if(found) { |
| 118 | + one=tr.begin(st); |
| 119 | + two=one; |
| 120 | + ++two; |
| 121 | + while(two!=tr.end(st)) { |
| 122 | + compare.clear(); |
| 123 | + auto es=compare.equal_subtree(one, two); |
| 124 | + if(compare.should_swap(one, es)) one=two; |
| 125 | + ++two; |
| 126 | + } |
| 127 | + // We have found the element that should go at the front of the trace |
| 128 | + Ex::sibling_iterator front=one; |
| 129 | + while(tr.begin(st)!=front) { |
| 130 | + one=tr.begin(st); |
| 131 | + two=one; |
| 132 | + ++two; |
| 133 | + while(two!=tr.end(st)) { |
| 134 | + compare.clear(); |
| 135 | + auto es=compare.equal_subtree(one, two); |
| 136 | + int sign=compare.can_swap_components(one, two, es); |
| 137 | + if(sign==-1) flip_sign(st->multiplier); |
| 138 | + tr.swap(one); |
| 139 | + ++two; |
| 140 | + ++two; |
| 141 | + } |
| 142 | + } |
| 143 | + ret=result_t::l_applied; |
| 144 | + } |
| 145 | + } |
| 146 | + |
91 | 147 | if(cleanup) |
92 | 148 | cleanup_dispatch(kernel, tr, st); |
93 | 149 |
|
|
0 commit comments