如何在 Mathematica 块中调用表达式列表

How to Call an expression list in a Block of Mathematica

这是一个块函数:

ublock[UU_]:=Block[{tt},U[z_]:=UU[[1]];
tt=2 U[z]+3 U'[z]+U''[z]];
UU:={z^2,z,Sin[z]};
ublock[UU]

其中tt,U[z]是临时变量,我想得到结果:

2*z^2+6z+2

但结果是:

2z^2

为什么 U'[z]U''[z] 的结果丢失了?

如何得到我想要的结果?

部分评价有帮助

UU := {z^2, z, Sin[z]};
ublock[UU_] := Evaluate@Block[{tt},
    U[z_] := Evaluate@UU[[1]];
    tt = 2 U[z] + 3 U'[z] + U''[z]];
ublock[UU]

2 + 6 z + 2 z^2

但为了更灵活

Clear[U, UU, ublock]

ublock[UU_] := Block[{tt},
   U[z_] := 0;
   DownValues[U] = ReplacePart[DownValues[U], {1, 2} -> UU[[1]]];
   tt = 2 U[z] + 3 U'[z] + U''[z]]

UU := {z^2, z, Sin[z]}

ublock[UU]

2 + 6 z + 2 z^2

ublock[UU_] := Module[{tt}, U = UU[[1]];
   tt = 2 U + 3 D[U, z] + D[U, {z, 2}]]

或更好

ublock[UU_] := With[{U = UU[[1]]}, 2 U + 3 D[U, z] + D[U, {z, 2}]]
UU = {z^2, z, Sin[z]};
ublock[UU_] := Evaluate@Block[{tt}, U[z_] = UU[[1]];
    tt = 2 U[z] + 3 U'[z] + U''[z]];
ublock[UU]

(* 2 + 6 z + 2 z^2 *)