I need to define a function fun, and then re-define this function iteratively. The code is given at the end.

First, a function fun[x_, y_, d_] is defined, which is a polynomial in $x$ and $y$, and $d$ is the degree of this polynomial.

My goal is to modify fun according to some of its coefficients, see the definition fun2[x_, y_, d_] for example.

The problem is that, these coefficients are $0$ if $d$ is not substituted by a "real" number, like $d = 1$. See the code between the definitions of fun and fun2.

Just after the definition of fun2, I compute fun2[x, y, d] and it is the same as fun1[x, y, d]. The reason is that $d$ is not assigned a value. But fun2[x, y, 1] gives the desired answer, which is different from fun[x, y, 1].

The problem is that I want to repeat this process many times, say

fun3 = fun2 + Coefficient[fun2, x^2]
fun4 = fun3 + Coefficient[fun3, x^3]
fun"d" = fun2 + Coefficient[fun"d-1", x^(d-1)]

(This is just an example, the real process I need is far more complicated and can not be deined in one go. In this example, one can just use Sum[Coefficient...].)

Of course I don't want introduce so many functions. I want to define fun and modify it in a for loop. But the code below shows that Coefficient[fun[x, y, d], ...] is always $0$, since $d$ is not assigned a value.

I can't use a variable like temp in this for loop, because

For [i =...
  temp = fun[x, y, d]
  temp = temp + Coefficient[temp, x^i]

will just give fun[x, y, d], as Coefficient[temp, x^i] is always $0$ as mentioned above.

I need a suggestion on how to do such work more elegantly.

screen shot

  • 2
    $\begingroup$ Having code that can be pasted into a notebook makes it more likely that users will try to help. To convert the sigma notation to Sum, you can "Convert To > InputForm" from the "Cell" menus before copying, if that's a problem. $\endgroup$
    – Michael E2
    Aug 9 '13 at 12:17

Try the following code:

fun[n_Integer?Positive][x_, y_, d_Integer] := 
  fun[n - 1][x, y, d] + Coefficient[fun[n - 1][x, y, d], x, n - 1 ];
fun[0][x_, y_, d_Integer] := 
  Sum[(i + j) x^i y^j Subscript[a, i, j], {i, d}, {j, d}];
Table[fun[n][x, y, 1], {n, 0, 2}] // TableForm
  • 2
    $\begingroup$ You might want some pattern restrictions, to prevent evaluation in cases where d is just the symbol d: E.g., fun[n_Integer?Positive][x_, y_, d_Integer] :=... $\endgroup$
    – Michael E2
    Aug 9 '13 at 16:20
  • $\begingroup$ @MichaelE2 updated code to include your suggestion. $\endgroup$
    – Hector
    Aug 9 '13 at 21:42
  • $\begingroup$ @Hecotr, thanks. It works. By the way, do you know if there is a more sophisticated way to define recursive functions, like a C programming. For example f[x_,n_]:= { exp1; exp2; exp3; return f[x,n-1]+exp3 }? $\endgroup$
    – user565739
    Aug 10 '13 at 7:07
  • $\begingroup$ @user565739, "Sophisticated" is in the eye of the beholder … faster might be a better metric. In any case, the following code might satisfy you: f[x_, n_Integer?Positive] := g[f[x, n - 1]] $\endgroup$
    – Hector
    Aug 10 '13 at 13:45

Another way (using same definition of f[x,y,d]:

  nestpol[n_Integer, d_Integer] := 
     NestList[{#[[1]] + 1, #[[2]] + Coefficient[#[[2]], x^(#[[1]])]} &, 
{1, f[x, y, d]}, n][[;; , 2]]

note (i) if just want the last value just change NestList to Nest (ii) in Hector's answer (based on no constant term in polynomial) f[0][x,y,d]=f[1][x,y.d], so nestpol[N,d] yields the same result as Table[fun[n][x, y, 1], {n, 1,N}]


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.