5
$\begingroup$

I'd like to replace every instance of L[stuff], e.g.,

L[-1,-2,-2]

in an expression with x^(-FromDigits[{stuff}]), e.g.

x^122

. But I'm running into an issue-- I'd like to do this with the code

L[-1, -2, -2] /. L[a___] -> x^(FromDigits[-{a}])

but this produces

2/x^6 + x^4 + x^5.

In trouble-shooting, I noticed that while

L[-1, -2, -2] /. L[a___] -> {a}

produces the output I expected, namely {-1,-2,-2}, I found that

L[-1, -2, -2] /. L[a___] -> -{a}

just produces {4} rather than {1,2,2}. I must be misunderstanding how Sequence behaves when it shows up in the right side of a Rule since when I try just inputting -{Sequence[-1,-2,-2]} I get the expected result {1,2,2}. Does anyone see where I'm going wrong? Thanks!

$\endgroup$
  • 2
    $\begingroup$ funny, I asked a very similar question last week, I wonder if its the same issue. Anyway, here is a possible workaround: L[-1, -2, -2] /. L[a___] :> x^(FromDigits[-{a}]). $\endgroup$ – AccidentalFourierTransform Jul 11 at 20:12
  • $\begingroup$ @AccidentalFourierTransform yup it would appear to be exactly the same issue and my reaction to FullForm[-b] was exactly the same as yours lol $\endgroup$ – Diffycue Jul 11 at 20:18
8
$\begingroup$

Here's an explanation of why you get {4}. As mentioned above, your problem is the use of Rule instead of RuleDelayed, which means the RHS of the rule evaluates before the rule is used. So, let's see what the RHS of the rule evaluates to:

-{a} //FullForm

List[Times[-1,a]]

So, when evaluated, the rule becomes:

L[a___] -> {Times[-1, a]}

Now, it should be clear what happens. When the rule is applied, a gets bound to the sequence -1, -2, -2, and so the output (before evaluation) is:

{Times[-1, -1, -2, -2]}

which evaluates to:

{4}

$\endgroup$
6
$\begingroup$

It's just a matter of using RuleDelayed, or rather :> instead of ->.

L[-1, -2, -2] /. L[a___] :> x^(FromDigits[-{a}])

x^122

L[-1, -2, -2] /. L[a___] :> -{a}

{1, 2, 2}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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