# Efficient way of setting up a rule

I tried to define a simple rule defining how λ acts on ψ[n]:

myrule1 = λ ψ[n_] -> α[n + 1]  ψ[n + 1];


The result I get is correct provided there's just one λ on the RHS of ψ[n]. For instance:

λ^2  ψ[n] //. myrule1


isn't computed at all. On the other hand, if I do it step by step:

λ α[1 + n] ψ[1 + n] /. myrule1


I get the correct result. I tried to define a new rule:

myrule2 = λ^m_ ψ[n_] -> α[n + 1] λ^(m - 1) ψ[n + 1];


but it doesn't work. Since the recursive method seemed to work, I created a function which multiplies ψ[m] by λ n times:

timesλ[n_] := Nest[Times[λ, #] /. myrule1 &, ψ[m], n] &


But this is a very crude way of solving this problem.

Do you have any other ideas?

-
Could augment with a second form \[Lambda]^(j_ /; IntegerQ[j] && j >= 1) \[Psi][n_] -> \[Alpha][ n + j] \[Psi][n + j] –  Daniel Lichtblau Feb 5 '13 at 15:51
Thanks! The rule you suggest is incorrect, but the idea is ok. –  Gregory Rut Feb 6 '13 at 12:13
Ah, yes, I missed the need for a product. I think the @m_goldberg variation sounds better... –  Daniel Lichtblau Feb 6 '13 at 14:47

If you look at the full form versions of λ ψ[n] and λ^k ψ[n]

λ ψ[n] // FullForm


Times[λ, ψ[n]]

λ^k ψ[n] // FullForm


Times[Power[λ, k],ψ[n]]

you see that the second expression can't match your rule because it contains Power. Therefore, as Daniel Lichtblau suggests, you need to have two rules.

rules = {λ ψ[n_] -> α[1 + n] ψ[1 + n],
λ^j_ ψ[n_] -> Product[α[s], {s, n + 1, n + j}] ψ[j + n]};


Now you can get both

λ ψ[n] /. rules


α[1 + n] ψ[1 + n]

and

λ^k ψ[n] /. rules


Product[α[s], {s, n + 1, n + k}] ψ[k + n]

-
Looks good, thanks! It is weird that myrule2 didn't bring any output, it 'almost' works on another pc (one λ is left because of different Head on RHS, as you mentioned). –  Gregory Rut Feb 6 '13 at 12:26
It appears that Gregory is not following this thread but I think you may learn something from the answer I just posted. –  Mr.Wizard Mar 5 '13 at 23:38

## Preliminary

This can be done with a single rule and an Optional pattern because Power has the attribute OneIdentity. As the documentation states:

The fact that Times has attribute OneIdentity allows a pattern like n_. x_ to match x.

Observe:

{2^y, 3} /. a_Integer^b_. :> {a, b}

{{2, y}, {3, 1}}


(Power has an internal default of one.)

Note also that I use RuleDelayed (short form :>) and not Rule; this is necessary to correctly localize named patterns on the right hand side of the rule.

With this knowledge we can now adjust your rule2; with this formulation we will need ReplaceRepeated (short form //.):

rule = λ^m_. ψ[n_] :> α[n + 1] λ^(m - 1) ψ[n + 1];

λ^3 ψ[n] //. rule

α[1 + n] α[2 + n] α[3 + n] ψ[3 + n]


Or if you prefer, the Product formulation:

rule2 = λ^j_. ψ[n_] :> Product[α[n + s], {s, j}] ψ[j + n];

λ ψ[n] /. rule2

λ^2 ψ[n] /. rule2

α[1 + n] ψ[1 + n]

α[1 + n] α[2 + n] ψ[2 + n]

-
Thanks, that's a very nice solution. –  Gregory Rut Sep 13 '13 at 18:52
@Gregory Welcome back to the site. I'm glad you found this helpful even now. :-) –  Mr.Wizard Sep 15 '13 at 17:08