3

I'd like to set an upvalue for DiscreteShift that changes the rules for being raised to a power:

Unprotect[DiscreteShift];
DiscreteShift /: Power[DiscreteShift[f_, i_], r_] := DiscreteShift[f, {i, r}];
Protect[DiscreteShift];
Power[DiscreteShift[f[n], n], 2] === DiscreteShift[f[n], {n, 2}]

But I'm getting these error messages:

Rule::rhs: "Pattern i_ appears on the right-hand side of rule i_->1+i_. "
TagSetDelayed::tagnf: "Tag DiscreteShift not found in (1+Pattern[f_,_])^r_."

It doesn't seem to like the pattern for DiscreteShift's arguments, but I can't get anything else to work there, either. What is the correct way to write this?

Edit: I'll try to clarify my goal. Here's what I want to do mathematically:

(N + n) f(n) = N f(n) + n f(n)
             = f(n+1) + n f(n)

(N + n)^2 f(n) = (N^2 + Nn + nN + n^2) f(n)
               = N^2 f(n) + Nn f(n) + nN f(n) + n^2 f(n)
               = f(n+2) + (n+1) f(n+1) + n f(n+1) + n^2 f(n)
               = f(n+2) + (2n+1) f(n+1) + n^2 f(n)

So I have this funny N operator that acts as a discrete shift, and we're sort of overloading the meaning of multiplying to have it operate on a function. I had hoped to represent N f(n) by using DiscreteShift[f[n],n], and then fixing the power rule for it.

  • Does this help? http://stackoverflow.com/questions/4198961/what-is-in-your-mathematica-tool-bag/5149656#5149656 – Dr. belisarius Oct 25 '11 at 17:31
  • I believe your updated question will take some time to address correctly, time I don't have right now. I shall revisit it later, if it is not already answered by then. – Mr.Wizard Oct 25 '11 at 22:33
  • Patrick, I am sorry, I did not come back to this. I shall to spend some time on it very soon. I am surprised nobody answered your update already. – Mr.Wizard Oct 28 '11 at 04:26
  • @Mr.Wizard I appreciate all of your help, but don't feel pressured to get back to it. – Patrick Gaskill Oct 31 '11 at 18:33
  • @Patrick, I am finally looking at this again. This looks like a polynomial expansion, but I would expect `(N + n)^2 f(n)` to yield `n^2 f(n) + 2 n f(n+1) + f(n+2)`. Please tell me why this is not so. Sorry for not understanding your example; maybe it will "click" in a few minutes. – Mr.Wizard Nov 04 '11 at 04:29
  • @Mr.Wizard It's because the `N` doesn't commute normally with `n`. So after multiplying out (in the second line), you have a `Nn f(n)` term and a `nN f(n)` term. The `N` affects any function of `n` to its right, so `Nn = n+1`. I hope that makes sense. – Patrick Gaskill Nov 09 '11 at 14:25
  • I think I understand. Interesting. – Mr.Wizard Nov 09 '11 at 14:33

1 Answers1

5

The problem is that DiscreteShift[f[n], n] in Power[DiscreteShift[f[n], n], 2] evaluates to f[1 + n] before anything else happens. This effects both your attempt at creating a rule, and the actual execution of Power[DiscreteShift[f[n], n], 2].

Compare to your result:

Unprotect[DiscreteShift];
DiscreteShift /: Power[HoldPattern[DiscreteShift[f_, i_]], r_] := 
  DiscreteShift[f, {i, r}];

Power[Unevaluated@DiscreteShift[f[n], n], 2] === DiscreteShift[f[n], {n, 2}]
(* Out[] = True *)

belisarious gave a link to a method by which you may intercept evaluation of a built-in function to insert your own code. However, I think it would be difficult to combine this with TagSet to DiscreteShift.

Community
  • 1
  • 1
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 1
    +1. The problem is, most people would not like to use `Unevaluated` all the time, and to my knowledge there is no robust way to do this without it (not to detract from your answer). I discussed extensively exactly this problem here: http://stackoverflow.com/questions/7408310/unwanted-evaluation-in-assignments-in-mathematica-why-it-happens-and-how-to-de/7409957#7409957 – Leonid Shifrin Oct 25 '11 at 20:24
  • Leonid I wanted to link to that answer, but I could not find it in time. Thank you! – Mr.Wizard Oct 25 '11 at 20:35
  • I will have to take more time to study the links posted, but it seems to me that DiscreteShift's evaluation first is desirable. I want things like f[n+1]^2 to evaluate to f[n+2]. – Patrick Gaskill Oct 25 '11 at 21:12
  • @Patrick, if you want `f[n+1]^2` to evaluate to `f[n+2]` then `DiscreteShift` is out of the picture and you will need to attach a rule to either `Power` or better `f`. – Mr.Wizard Oct 25 '11 at 21:17
  • I originally wanted to solve this using a downvalue on `Power`, but this rule should only apply to symbols with a specific head, so I thought the head might as well be `DiscreteShift`. Is it possible that I can still do this, but use `Hold@DiscreteShift` (or some variant of `Hold`) instead? – Patrick Gaskill Oct 25 '11 at 21:23
  • 2
    @Patrick a simple replacement of `HoldPattern` and `Unevaluated` with `Hold` will fail, because the nesting is too deep. Also, it is normally inadvisable to overload a symbol as basic as `Power`. If you would be willing to describe your application in further detail, I may have a suitable suggestion (you can make it an edit to your question, if you find that appropriate). – Mr.Wizard Oct 25 '11 at 21:35
  • @Patrick, is it acceptable to use alternate operators such as `CirclePlus` and `CircleTimes`? – Mr.Wizard Oct 25 '11 at 22:30