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

Hi everyone! I'm using cadabra for a several years, but still can't understand how to factor out numerical coefficients, for example

factor_out($6 A B + 6 A C$, $A$);

or

factor_out($6 A B + 6 A C$, $A,6$);

It would be much prettier if you could factor out the numerical coefficient too. it doesn't affect the functionality, but when exporting formulas to latex, a lot of time is spent on manual editing.

in General questions by (1.7k points)

2 Answers

+1 vote

Hi Arina.

I understand your frustration. The capabilities of cadabra are a bless and a curse.

I believe that the "problem" is that cadabra was born as a tool for manipulation, not calculation (perhaps Kasper could tell us the story one day).

In defense of cadabra, even great calculation software, e.g. Mathematica, Maple or sagemath, suffer of the same problem.

A typical example in Mathematica is the simple calculation of Sqrt[x^2]. Even if you later simplify the expression, you won't get the expexted result, because the software is not assuming that the variable is real (which is an advantage in certain situations, but probably not most of the time).

The main purpuse of mathematical software is to help you obtain the result, less prone to errors. It is a good praxis to check manually if the expressions might simplify farther.

Now, in order to reduce the amount of time spent in manual manipulation of the expression, try to use different algorithms before (gladly, usually those take just a few moments to the computer).

In your example, consider the following:

ex := 6 A B + 6 A C;
factor_out(ex, $A$);

The above yield an unexpected result. But,

ex := 6 A B + 6 A C;
simplify(ex);

returns what you expect.

Of course this is a very simple case. Moving a bit a further, consider

ex := 6 A B + 6 A C + 6 B C;
factor_out(ex, $A$);
simplify(ex);

You'd noticed that the factorisation does not fulfil our expectations, but the additional simplification does!

If your expressions allows it, try using different sympy algebraic algorithms (or Mathematica's).

Sorry for the extended (and possibly unsatisfactory) contribution.

Cheers, Dox.


Update 1

In order to pass more than one arguments to the map_sympy algorithm, just include additional strings, for example

ex := 6 A B + 6 A C;
map_sympy(ex, "collect", "A");
by (14.9k points)
edited by

Thank you for your answer! I'm really trying to use sympy functions, and there are questions here too. Maybe you know if it is possible to pass 2 arguments with map_sympy? For example, the collect function from sympy requires not only the expression itself, but also another argument.

Hi Arina, I update the answer to reply your comment.

Cheers.

0 votes

The main reason why you cannot factor out only a numerical factor is that Cadabra stores rational pre-factors in a special location (the 'multiplier') inside each node in the expression tree.

Cadabra will always rewrite expressions so that a \sum node will have a unit multiplier. So all multipliers are associated to the summands. This is a canonicalisation choice which many algorithms in Cadabra rely on, so it's not easy to change that. Similarly, all child nodes of a \prod node will have unit multiplier.

As soon as you have a product in your expression node, like in 6 A (B + C), the multiplier is associated to the \prod node. So you still have

\prod{A}{ \sum{B}{C} }

with a unit multiplier for the \sum, but now the \prod node has multiplier 6.

by (82.1k points)
...