3
$\begingroup$

I want to do replacement of products (because I am dealing with abstract projectors), but Mathematica seems to fail understanding this toy example replacement :

x y y /. {x y -> 1}

x y^2

because it doesn't understand that there is $xy$ in $xy^2$. Is there an easy way to deal with this kind of issues ?

Thanks!

$\endgroup$
5
  • $\begingroup$ See also mathematica.stackexchange.com/questions/tagged/… $\endgroup$
    – xzczd
    Commented Mar 12 at 8:57
  • $\begingroup$ This helps, thanks. But for systematicity I would like to know if it is possible to forbid mathematica to replace $x x \to x^2$ ? $\endgroup$ Commented Mar 12 at 9:05
  • 1
    $\begingroup$ I'm not sure if I've understood your question. Do you mean you want to stop the automatic evaluation of x x? If so, consider something like Inactivate[x x, Times], or even consider make your own (I admit it's a bit advanced): SetAttributes[times, {Orderless, Flat, OneIdentity}]; expr = times[x, times[y, x]]; expr /. times[x, y] -> aaa $\endgroup$
    – xzczd
    Commented Mar 12 at 9:14
  • $\begingroup$ Thanks! This is useful $\endgroup$ Commented Mar 12 at 10:19
  • $\begingroup$ Do you require something like Hold[x y y] /. x y -> 1 // ReleaseHold (giving y as output)? $\endgroup$
    – user1066
    Commented Mar 12 at 10:44

2 Answers 2

2
$\begingroup$

Do you want this?

rep = {_[a_, _[b_, n_]] :> Times @@ Table[b, n - 1], Times[__] :> 1};

ReplaceAll[rep] /@ {x y, x y y, x y y y}

{1, y, y^2}

$\endgroup$
2
$\begingroup$

Another option is to use Simplify with side relation

e = x  y  y
Simplify[e, {x*y == 1}]

Mathematica graphics

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.