maple

Change signs in a squared equation in Maple


I want to change the signs of all symbols in a squared equation in Maple. For example, these equations yield identical answers:

  1. ((-a + b - c + d)^2) / (a - b)
  2. ((a - b + c - d)^2) / (a - b)

Now let's say I have coded the first function in Maple using:

test := (-apple + banana - cherry + date)^2/(apple - banana)

I can then easily swap these with:

numer(test)/denom(test), because the numer() changes the signs.

However, if I use different labels, such as swap "apple" with "quality", this doesn't work anymore.

test := (-quality + banana - cherry + date)^2/(apple - banana); numer(test)/denom(test)

Could you explain why and how it can be achieved instead?


Solution

  • Here is one way to handle your examples,

    It acts on all squares of sums in the expression. It maps - across the sum subexpression.

    sw:=e->subsindets(e,`+`^2,
                      u->subsop(1=map(`-`,op(1,u)),u)):
    
    expr:=((-a+b-c+d)^2)/(a-b):
    
    sw(expr);
    
       (a-b+c-d)^2/(a-b)
    
     test:=(-quality+banana-cherry+date)^2/(quality-banana):
    
    sw(test);
    
       (quality-banana+cherry-date)^2/(quality-banana)
    

    Here's a variant on that.

    sw:=e->subsindets(e,`+`^2,
                  u->map(`-`,op(1,u))^2):
    

    You could also make it work on other even powers.

    sw:=e->subsindets(e,`+`^even,
                  u->map(`-`,op(1,u))^op(2,u)):
    
    expr2:=(a-b)/(-a+b-c+d)^4:
    
    sw(expr2);
    
       (a-b)/(a-b+c-d)^4