Skip to content

Commit 26cc507

Browse files
committed
Fix crash when collecting powers of numbers.
1 parent 5fabeb0 commit 26cc507

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

core/algorithms/collect_factors.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ void collect_factors::fill_hash_map(iterator it)
2626
factor_hash.clear();
2727
sibling_iterator sib=tr.begin(it);
2828
unsigned int factors=0;
29-
while(sib!=tr.end(it)) {
29+
while(sib!=tr.end(it)) { // iterate over all factors in the product
3030
sibling_iterator chsib=tr.begin(sib);
3131
bool dontcollect=false;
32-
while(chsib!=tr.end(sib)) {
32+
while(chsib!=tr.end(sib)) { // iterate over all child nodes of a factor
3333
const Symbol *smb=kernel.properties.get<Symbol>(chsib, true);
3434
// std::cerr << chsib << ": " << smb << std::endl;
3535
if((chsib->fl.parent_rel==str_node::p_sub || chsib->fl.parent_rel==str_node::p_super) &&
@@ -40,8 +40,10 @@ void collect_factors::fill_hash_map(iterator it)
4040
++chsib;
4141
}
4242
if(!dontcollect) {
43-
if(*sib->name=="\\pow")
44-
factor_hash.insert(std::pair<hashval_t, sibling_iterator>(tr.calc_hash(tr.begin(sib)), tr.begin(sib)));
43+
if(*sib->name=="\\pow") {
44+
if(tr.begin(sib)->is_rational()==false) // do not collect exponents of numbers
45+
factor_hash.insert(std::pair<hashval_t, sibling_iterator>(tr.calc_hash(tr.begin(sib)), tr.begin(sib)));
46+
}
4547
else
4648
factor_hash.insert(std::pair<hashval_t, sibling_iterator>(tr.calc_hash(sib), sib));
4749
++factors;
@@ -78,6 +80,11 @@ Algorithm::result_t collect_factors::apply(iterator& st)
7880
else {
7981
expsum.append_child(expsumit, str_node("1", str_node::b_round));
8082
}
83+
// FIXME: If the multiplier of this factor is non-zero, we
84+
// have (pure number)**(exp). We need to catch this
85+
// separately. std::cerr << (*thisbin1).second << std::endl;
86+
// For now, we have disabled collecting such factors; sympy
87+
// can do it anyway.
8188
assert(*((*thisbin1).second->multiplier)==1);
8289
// find the other, identical factors
8390
while(thisbin2!=factor_hash.end() && thisbin2->first==curr) {

tests/algebra.cdb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,12 @@ test27()
576576
# tst38:= A - B + C - D + E - @(obj37);
577577
# @collect_terms!(%);
578578
# @assert(tst38);
579+
580+
def test28():
581+
ex:=k^{a}n_{c a}+2**{1/2} k_{c} 2**{1/2};
582+
collect_factors(_);
583+
tst:=k^{a}n_{c a}+2**{1/2} k_{c} 2**{1/2} - @(ex);
584+
assert(tst==0)
585+
print("Test 28 passed")
586+
587+
test28()

tests/kaluza_klein.cdb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ distribute(_)
6161
sort_product(_)
6262
canonicalise(_)
6363
rename_dummies(_);
64-
64+
6565
tst:= - 1/4 * \partial_{m}{\phi} * \partial_{n}{\phi} * \phi**(-1) + 1/4 * \partial_{p}{\phi} * \partial_{n}{h_{m q}} * h^{p q} - 1/2 * \partial_{m n}{\phi} + 1/4 * F_{m p} * F_{n q} * \phi**3 * h^{p q} + 1/4 * \partial_{p}{\phi} * \partial_{q}{\phi} * \phi**(-1) * h_{m n} * h^{p q} - 1/4 * \partial_{p}{\phi} * \partial_{q}{h_{m n}} * h^{p q} + 1/4 * \partial_{p}{\phi} * \partial_{m}{h_{n q}} * h^{p q} - @(todo);
6666

6767
assert(tst==0)

0 commit comments

Comments
 (0)