2
$\begingroup$

By default, Mathematica simplifies Times[f[x],f[x]] as Power[f[x],2]. In most cases it's fine, but I happen to have a code where this rule is particularly annoying.

Is it possible to modify the behavior of Times so that this rule is not applied for a particular type of argument (let's call it g) i.e. Times[g[x],g[x]] is kept unchanged, but Times[a,a] becomes Power[a,2] if a is a Symbol (or anything except g).

Thank you for your help.

Edit : Sorry if I was unclear. I would like this behavior to be applied automatically everywhere in the session. For example, I can modify Times by doing :

Unprotect[Times] Times[a_g,a_g]:=Defer[Times[a,a]] Protect[Times] 

This work fine, because Times[f[x],f[x]] becomes f[x]^2, and Times[g[x],g[x]] does not change. But with this trick, I'm stuck with the ugly FullForm Defer[Times[g[x],g[x]]], which will mess up my pattern matchings. What I'd really like to do is remove a rule from Times, instead of adding one.

$\endgroup$
4
  • $\begingroup$ Unevaluated, Hold, HoldPattern and appropriate use of Pattern matching seems like the crudest solution I can think of. Also 98874 seems useful. $\endgroup$ Commented Dec 5, 2017 at 16:16
  • $\begingroup$ I think those functions will only prevent the evaluation temporary. At some point I will have to ReleaseHold and Times[g,g] will transform. Unless I can use those after unpotecting Times. But I still don't see how. $\endgroup$ Commented Dec 5, 2017 at 16:54
  • $\begingroup$ There may be a way in your particular use-cases. Without more of the context, we'll probably just be guessing like Sektor. That is, if anyone wants to take the trouble to guess. $\endgroup$ Commented Dec 5, 2017 at 17:06
  • $\begingroup$ Have you solved this problem? I am facing the same issue $\endgroup$ Commented Jun 20, 2019 at 12:29

1 Answer 1

3
$\begingroup$

Assuming that you want only powers of g handled differently,

unique /: Power[unique[x_], n_Integer?Positive] := Inactive[Times] @@ ConstantArray[g[x], n] grule = g -> unique; expr1 = g[x]^3 /. grule 

enter image description here

expr1 // Activate (* g[x]^3 *) expr2 = g[3 y + z]^2 /. grule 

enter image description here

expr2 // Activate (* g[3 y + z]^2 *) 

EDIT: To have the modification be applied automatically, add the Rule to $Post

$Post := (# /. g -> unique &) expr = g[x]^3 

enter image description here

However, since it is applied automatically, Activate will not cancel the Inactive

expr // Activate 

enter image description here

A different symbol must be used to remove the Inactive

expr /. g -> \[FormalG] // Activate (* \[FormalG][x]^3 *) 

going back

% /. \[FormalG] -> g 

enter image description here

To clear $Post use

$Post =. 
$\endgroup$
1
  • $\begingroup$ Thank you for you help. I was thinking of a more permanent modification that is applied automatically. I edited my question to make it clear. $\endgroup$ Commented Dec 6, 2017 at 9:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.