Search code examples
wolfram-mathematicasymbolsprotecteduser-defined-types

I want to define f^2 to be 1 but leave f undefined


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.


Solution

  • 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