2
votes

I want, for example, for Mathematica to generate 7 + 5f if I write the expression (2+f) (3+f). I always want f^2 to be computed as 1 (or any other value I assign to it) but for f to be a special undefined symbol. If I define f^2:=1 I get a Tag Power is protected error message.

I am a Mathematica newbie, self taught, so please try to answer this in as elementary fashion as possible.

For the record, I am trying to define Clifford algebra operations in n-dimensional space-time and being able to make an assignment like this would tremendously simplify the task.

2
We could use UpSet / TagSet f /: f^2 = 1 but that won't recognize f^4 == 1 since 4 ≠ 2... - kennytm
Chris' answer is just what I was looking for. Thank you so much. Before I close this thread, let me expand the question slightly to see if there is still such an easy solution. The following works: x = (a + a1 e1 + a2 e2 + a3 e3 + a4 e1 e2 - a5 e1 e3 + a6 e2 e3 + a7 e1 e2 e3); y = (b + b1 e1 + b2 e2 + b3 e3 + b4 e1 e2 - b5 e1 e3 + b6 e2 e3 + b7 e1 e2 e3); ReplaceAll[ Expand[x y], {Power[e1, 2] -> 1, Power[e2, 2] -> 1, Power[e3, 2] -> 1}] - matrixbud
If a number n is input, can this easily be extended to handle e1, e2, ..., en? - matrixbud

2 Answers

1
votes

Generalized to all symbols e1,e2,e3,...,en

x = (a + a1 e1 + a2 e2 + a3 e3 + a4 e1 e2 - a5 e1 e3 + a6 e2 e3 + 
a7 e1 e2 e3);
y = (b + b1 e1 + b2 e2 + b3 e3 + b4 e1 e2 - b5 e1 e3 + b6 e2 e3 + 
b7 e1 e2 e3);


 ReplaceAll[
   Expand[x y], 
   Power[e_, 2] /; First[Characters[ToString[e]]] === "e" -> 1
 ]

This way which I have just learned from @Edmund is more elegant:

Expand[(2 + e1)(3 + e2)] /.Power[s_Symbol,2]/; StringStartsQ["e"]@SymbolName[s]->1
6 + 3 e1 + 2 e2 + e1 e2
1
votes
ReplaceAll[Expand[(2 + f) (3 + f)], Power[f, 2] -> 1]

7 + 5 f