Skip to content

Commit 2f9be78

Browse files
committed
Check commuting behaviour
By definition, combine() is an operation that can change the ordering of factors. Therefore, we have to keep track of the sign resulting from every move. Signed-off-by: Connor Behan <[email protected]>
1 parent 0ca77e3 commit 2f9be78

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

core/Props.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ namespace cadabra {
503503
if(ret1) { // property of the right type found for object 1
504504
property_map_t::const_iterator walk2=pit2.first;
505505
while(walk2!=pit2.second) {
506-
if((*walk2).second.first->match(*this, it2, ignore_parent_rel)) { // match for object 1 found
506+
if((*walk2).second.first->match(*this, it2, ignore_parent_rel)) { // match for object 2 found
507507
ret2=dynamic_cast<const T *>((*walk2).second.second);
508508
if(ret2) { // property of the right type found for object 2
509509
if(ret1==ret2 && walk1!=walk2) { // accept if properties are the same and patterns are not

core/algorithms/combine.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ Algorithm::result_t combine::apply(iterator& it)
139139
iterator brackprod=tr.append_child(outerbrack, str_node("\\prod"));
140140
iterator parn1=tr.parent(*dums1);
141141
iterator parn2=tr.parent(*dums2);
142+
143+
// count how many sign changes stand between the two objects
144+
int sign=1;
145+
unsigned int hits=0;
146+
Ex_comparator compare(kernel.properties);
147+
sib=tr.begin(it);
148+
while(hits<2) {
149+
if(hits==1 && sib!=parn2) {
150+
// pass arguments manually as can_swap() does not check them
151+
bool isbrack=*(sib->name)=="\\indexbracket";
152+
if(isbrack && isbrack2) {
153+
auto es=compare.equal_subtree(tr.begin(parn2), tr.begin(sib));
154+
sign*=compare.can_swap(tr.begin(parn2), tr.begin(sib), es, true);
155+
}
156+
else if(isbrack && !isbrack2) {
157+
auto es=compare.equal_subtree(parn2, tr.begin(sib));
158+
sign*=compare.can_swap(parn2, tr.begin(sib), es, true);
159+
}
160+
else if(!isbrack && isbrack2) {
161+
auto es=compare.equal_subtree(tr.begin(parn2), sib);
162+
sign*=compare.can_swap(tr.begin(parn2), sib, es, true);
163+
}
164+
else {
165+
auto es=compare.equal_subtree(parn2, sib);
166+
sign*=compare.can_swap(parn2, sib, es, true);
167+
}
168+
}
169+
if(sib==parn1 || sib==parn2) ++hits;
170+
++sib;
171+
}
172+
if(sign==-1) flip_sign(brackprod->multiplier);
173+
142174
// remove the dummy index from these two objects, and move
143175
// other (dummy or not) indices to the outer indexbracket.
144176
sibling_iterator ind1=tr.begin(tr.parent(*dums1));

tests/implicit.cdb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ def test06():
7373
test06()
7474

7575
def test07():
76+
__cdbkernel__=create_scope()
77+
{a,b}::Indices(vector);
78+
{A,B}::AntiCommuting;
79+
ex:=(A)_{a b} (B)_{b a};
80+
combine(_)
81+
tst:= (-B A)_{b b} - @(ex);
82+
assert(tst==0)
83+
print("Test 07 passed")
84+
85+
test07()
86+
87+
def test08():
7688
__cdbkernel__=create_scope()
7789
{m,n}::Indices(vector);
7890
{a,b,c}::Indices(spinor, position=fixed);
@@ -82,29 +94,29 @@ def test07():
8294
expand(_)
8395
tst:= (\Gamma^{m})^{a}_{b} (\Gamma^{n})^{b}_{c} - @(ex);
8496
assert(tst==0)
85-
print("Test 07 passed")
97+
print("Test 08 passed")
8698

87-
test07()
99+
test08()
88100

89-
def test08():
101+
def test09():
90102
__cdbkernel__=create_scope()
91103
{a,b}::Indices(vector);
92104
ex:=(u)_{a} (M)^{a b} (v)_{b};
93105
combine(_)
94106
tst:= \indexbracket(u M v) - @(ex);
95107
assert(tst==0)
96-
print("Test 08 passed")
108+
print("Test 09 passed")
97109

98-
test08()
110+
test09()
99111

100-
def test09():
112+
def test10():
101113
__cdbkernel__=create_scope()
102114
{a,b,c}::Indices(vector);
103115
ex:=(B)_{b c} (A)_{a b};
104116
combine(_);
105117
tst:= (A B)_{a c} - @(ex);
106118
assert(tst==0)
107-
print("Test 09 passed")
119+
print("Test 10 passed")
108120

109-
test09()
121+
test10()
110122

0 commit comments

Comments
 (0)