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

Hello!

When i try to make a substitution in the following expression:

ex:=-a**2 - b**2 - 2 a b;
substitute(_,$b->-a$);

I get 2aa, which is obviously wrong. However, if i run the following commands:

ex:=-a**2 - b**2 - 2 a b;
expand_power(_);
substitute(_,$b->-a$);

I get 0, as should be. What is the problem with the first example?

in Bug reports by (180 points)

3 Answers

0 votes

Hi Bruno.

Let me first appologise if my answer is not what you expect, but I'm not a cadabra developer... so I might be wrong about the internal functionalities of the software.

It seems that the algorithm substitute search for an expression (like a "string" in programing) and replace it by a new one... without further manipulations.

If the above is right, then in the first of your code blocks:

ex:=-a**2 - b**2 - 2 a b;
substitute(_,$b->-a$);

the substitution would return

-a**2 + a**2 + 2 a a

which is wrong because the minus sign was not modified by the **2 (square) operation!

My Suggestion

Given the describes behaviour you have to be extra careful in substituting if your expression contains powers.

You could define a function to substitute properly:

def my_subs(ex, rl):
    expand_power(ex)
    substitute(ex, rl)
    collect_factors(ex)
    return ex

Then,

foo := - a**2 - b**2 - 2 a b;
rul := b -> - a;
my_subs(foo, rul);

will return the expected zero.

Cheers, Dox.

by (15.0k points)

Thank you for your answer!

+3 votes

This is a bug in handling of \pow{...} nodes, thanks for pointing it out. A fix will appear shortly.

by (82.5k points)

Thank you Kasper. We shall wait for the next release!

Thank you for your answer!

+1 vote

This is fixed in 2.4.5 which is now on github. If anyone needs a binary package for a system other than Ubuntu 22.04 (which is already available from the download page), let me know.

by (82.5k points)

Great! Thank you K.

...