Factor out of numerical coefficient

+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.

+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.

Cheers.