0
$\begingroup$

I've found posts dealing with showing/hiding exponents for numbers, like in scientific notation. But what if I have a general expression? I have an expression like this,

p[{1,2}] p[{2,3}]^2 p[{4,5}]

and the exponent is screwing up a replacement rule I'd like to use. I'm sure I could find a workaround but if there's a way to make this appear as

p[{1,2}] p[{2,3}] p[{2,3}] p[{4,5}]

that would make my life much easier. Any ideas?

$\endgroup$
6
  • 1
    $\begingroup$ What's the replacement you want to do? Maybe you can work around the problem by modifying the replacing rule. $\endgroup$
    – xzczd
    Commented Nov 13, 2015 at 8:21
  • $\begingroup$ The replacement is essentially just combining components that have elements in common. So my example would get replaced by p[{1,2,3}] p[{4,5}]. I actually think I've found a way around it, although not in the most elegant way. $\endgroup$ Commented Nov 13, 2015 at 8:40
  • 1
    $\begingroup$ How about this?: Times @@ p /@ Union @@@ Gather[ p[{1, 2}] p[{2, 3}]^2 p[{4, 5}] /. a_^b_ :> a /. HoldPattern@Times[a__] :> Sequence @@@ {a}, Intersection[#, #2] != {} &] $\endgroup$
    – xzczd
    Commented Nov 13, 2015 at 8:56
  • $\begingroup$ It is not clear why in your post you say you need the repeating p[{2,3}] while in your comment you show that at the end you don't take into account this repeated element ... ? $\endgroup$
    – SquareOne
    Commented Nov 13, 2015 at 10:42
  • $\begingroup$ @SquareOne the idea is that the repeated element gets combined into a single element, because even though it's the same it has elements in common. Thanks for your post though. $\endgroup$ Commented Nov 13, 2015 at 15:08

1 Answer 1

3
$\begingroup$

As was commented, you should probably tell us also about the problematic rule you cannot use but anyway you might be interested in the following approach:

As you (should) know, everything in Mathematica is an expression, in other words it can be expressed as f[x,y,...]

Your expression:

expr = p[{1, 2}] p[{2, 3}]^2 p[{4, 5}]

is actually interpreted as:

FullForm[expr]

Times[p[List[1,2]],Power[p[List[2,3]],2],p[List[4,5]]]

If you are already familiar with replacement rules then you'll certainly understand the following replacement which will transform your expression in a form close to what you'd like:

Given this function:

myTimes[x_, n_Integer] := myTimes @@ ConstantArray[x, n]

see what happens here:

expr /. Power -> myTimes /. Times -> myTimes

myTimes[myTimes[p[{2,3}],p[{2,3}]],p[{1,2}],p[{4,5}]]

Almost done. Let's add the Flat property to function myTimes:

SetAttributes[myTimes, Flat]

and do again the same replacement than previously:

newexpr = expr /. Power -> myTimes /. Times -> myTimes

myTimes[p[{2, 3}], p[{2, 3}], p[{1, 2}], p[{4, 5}]]

That's it. The idea is to work on this new and temporary expression with your set of replacement rules. Then, to go back in real world:

newexpr /. myTimes -> Times

p[{1,2}] p[{2,3}]^2 p[{4,5}]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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