Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+1 vote


I have some problems with factor_in and factor_out . They don't quite work as i expect them to and I didn't find any posts about it. There are three different things i would like to mention:

First, factor_out is index-sensitive, but not really consistent about it:

ex:= g_{a b} p_c + g_{a b} q_c + g_{a c} p_b + g_{a c} q_b;
factor_out(_,$g_{a b}$);


ex:= g_{a b} p_c + g_{a b} q_c + g_{a c} p_b + g_{a c} q_b;
factor_out(_,$g_{a c}$);

give the expected result.

On the other hand:

ex:= g_{a b} p_c + g_{a b} q_c + g_{a c} p_b + g_{a c} q_b;
factor_out(_,$g_{b c}$);

factors out the g{a c}, but not the g{a b}.

Second, factor_out and factor_in do not work anymore, if the prefactors are too "complicated":

 ex2:= (a + b - c) d + (-a -b + c) d
 factor_out(_,$a + b - c$);

all do nothing here. Of course I could just use "distribute" here, but i get complicated expressions, where I need to factor in $a,b,c$ first and only then can think about further simplifications.

Third, factor_out does not work, if products of tensors are used:

ex3:= g_{a b} g_{c d} p + g_{a b} g_{c d}  q; 

factor_out(_,$g_{a b}$);
factor_out(_,$g_{a b}g_{c d}$);
factor_out(_,$g_{a b},g_{c d}$);

do not simplify the expression.


works just fine, but p and q themselves can take complicated forms and it's tedious to write them out in factor_in. Also, as mentioned before, this does not simplify the expressions anyway.

Thanks for working on the project, it is exactly what I was looking for and really like the "do it step by step" philosophy!

in Bug reports by

1 Answer

+2 votes

Let me first address the case which does work with the current version (at least for me):

ex2:= (a + b - c) d + (-a -b + c) d

correctly factors out $d$. Factoring out composite expressions (like the $a+b-c$) is currently not supported, unfortunately.

All the other cases have to do with factoring out tensors. In general, for that to work, you need to have the indices on the expression you want to factor out be the same on all terms (so sort the expression and rename_dummies first), and it needs to be un-ambiguous (your last example isn't, because the factor_out(_, $g_{a b}$) means 'factor out the g-tensor with two indices', not 'factor out the g-tensor with one index 'a' and one 'b'').

Fixing this has been on the TODO list for some time, but there are only 25 hours in each day unfortunately.

by (66.3k points)

Thanks for the answer! I'm propably not on latest version concerning the factoring out of $d$. Concerning the rest, i will wait patiently. Thanks for keeping up the work on this project. It really fills a gap that would be empty otherwise!

Hi Kasper,

I came across a related issue today where the behavior of factor_out seems counter-intuitive. The below does not factorize:

$V_{a b} W_{a b} + V_{a b} W_{b a}$;
factor_out(_, $V_{c d}$);

(If I write the final command with V_{a b}, it works just fine.) Interestingly, the original example factorizes just fine in Cadabra v1, so I was surprised by the change in behavior. Probably this is encompassed by your TODO comment above.

Apologies for so many questions / bug reports recently. I've started on a new project and I decided to bite the bullet and move to v2...

edit: Actually, it's a really interesting bug. If one tries e.g.

foo := Z_{a}^{c} + V_{a b} U^{b c} + V_{a b} W^{c b} + V_{a b} Z^{c b} 
    + V_{a b} X^{b c};
factor_out(_, $V_{c d}$);

it will correctly factorize the last three terms but not the first V U term. It seems entirely dependent on which V term is first -- i.e. it misses the first V term but catches all subsequent ones. (I included Z_a^c above to check if it was somehow just missing the first term in the expression, but it isn't.)