3

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Grzegorz Rut
  • 1,158
  • 8
  • 18

2 Answers2

4

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]

Grzegorz Rut
  • 1,158
  • 8
  • 18
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • 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). – Grzegorz Rut Feb 06 '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 05 '13 at 23:38
3

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.

Your operation

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]

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371