Mathematica:定义函数控制替换顺序

Mathematica: define function controlling order of substitutions

我想定义一个函数 s[t, n],它 returns 给定函数 f(t) 的 n 项傅里叶三角序列在点 t 的值。例如,对于 f(t) = t^2:

s[t_, n_] = FourierTrigSeries[t^2, t, n];  (* 2nd input = indep. variable *)
s[t, 3]
    Out =   Pi^2 / 3 + 4 (-Cos[t] + 1/4 Cos[2 t] - 1/9 Cos[3 t])

很好(t 的函数)。在 t = 0 时对其进行评估:

s[t, 3] /. t -> 0
    Out =   -31 / 9 + Pi^2 / 3

这就是我希望看到的 s[0, 3] 的输出,意思是 "evaluate at t=0 the FourierTrigSeries of 3 terms"。

但是做s[0,3]使得t = 0 "too early",所以原函数f(t)=t^2等同于0;甚至自变量 t(FourierTrigSeries 的第二个输入)也变为 0,这是没有意义的。结果就是Mathematica无法读懂我的心思,returns如下:

s[0, 3]
    Out =     FourierTrigSeries[0,0,3]

总结:我想定义函数 s[t, n] 以便首先用 n 代替它的值,然后才用它的值代替 t 以进行评估。

我试过用 s[n, t] 代替 s[t, n];还尝试了 := (延迟评估)而不是 = 以不同的方式,并结合

ss[tdelay_, n_] = s[t, n] /. t -> tdelay

但似乎没有任何效果。我怎样才能得到我想要的行为?

提前致谢!

使用SetDelayed和一个临时(本地)变量。

s[t_, n_] := Module[{t0}, FourierTrigSeries[t0^2, t0, n] /. t0 -> t]

s[0, 3]
-(31/9) + Pi^2/3

看到 Chris 的回答后,我试图找出为什么需要 Module 并且我找到了另一种方法,出于某种原因,我在第一次尝试时逃脱了我:

f[t_] = t^2;    (* or write t0^2 in line below *)
s[t_, n_] := FourierTrigSeries[f[t0], t0, n] /. t0 -> t
s[0, 3]
    Out =   -(31/9) + Pi^2 / 3

显然它可以在没有 Module 的情况下完成...

还是谢谢你!