RE: Eliminating (-1)^2
- To: mathgroup at smc.vnet.net
- Subject: [mg16451] RE: [mg16402] Eliminating (-1)^2
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Sat, 13 Mar 1999 02:21:55 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Nicolas B.E. Sawyer wrote:
------------------------
1) How do I get Mathematica to eliminate factors of (-1)^2. Specifically
I want to replace occurrences of (-(1-a)x -a y)^2 by ((1-a)x +a y)^2
within an expression?
------------------------
The replacement rule below will do what you want.
I made it a little more general. The version below will work for any even
exponent.
In[23]:=
FactorRule=
expr:(_+__)^n_?EvenQ:>
(Map[-#&,Part[expr,1],1]^n/.
Times[-1,a_Plus,b___]:>-a*b);
In[24]:=
(-(1 -a)x - a y)^2 /.FactorRule
Out[24]=
((1 - a)*x + a*y)^2
I had the hardest time getting this damn thing to work with your example. I
don't know why but it doesn't work well if you don't include
/.Times[-1,a_Plus,b___]:>-a*b)
at the end of the rule. Hopefully FactorRule above will work in any
situation.
------------------------
You also asked why the InputForm and StandardForm for the original
expression came out different. I don't know.
Regards,
Ted Ersek