Identify terms in an expression (Maple)| How hard is to program that given a symbolic expression like
$$
f = x^2 + y^2 + k \, ,
$$
I could teach to Maple that $x^2 + y^2 = r^2$ and then it expresses f like
$$
f = r^2 + k \, ?
$$
Of course I want to implement it for more difficult expressions but this would be a good start.
 A: If you expressions are polynomials then you can apply the algsubs command to get that form.
restart:

expr := f = x^2 + y^2 + k:

rule := x^2 + y^2 = r^2:

algsubs(rule, expr);

                                   2
                              f = r  + k

A more powerful way to obtain f expressed like that, which will work for a broader class of expressions, is to simplify it under side relations. Eg,
restart:

expr := f = x^2 + y^2 + k:

rule := x^2 + y^2 = r^2:

simplify( expr, {rule} ); 

                                   2
                              f = r  + k

A more automatic mechanism is to use the alias command. Here we just invoke alias once at the start, and then subsequently the expression shows in the desired form without having to apply another special command. Eg,
restart:  

alias(x^2=r^2-y^2):

expr := f = x^2 + y^2 + k;

                                       2
                          expr := f = r  + k

The r^2 on the RHS can be computationally combined with instance of r^2 itself.
expr - r^2;

                                2
                              -r  + f = k

But here is a drawback (which you might consider serious): it'd be nicer if the RHS of the following were displayed just as x^2 + k.
expr - y^2;

                           2        2    2
                         -y  + f = r  - y  + k

