如何通过附加到 Mathematica 中的函数来重新定义函数?

发布于 2024-12-08 02:21:05 字数 434 浏览 5 评论 0原文

在 Mathematica 中进行计算时,我经常需要通过附加到函数来重新定义函数。例如,如果我定义

f[n_] := n^2 + n + 1

then 一段时间后我想添加 2n^3 那么从现在开始

f[n] = 2n^3 + n^2 + n + 1. 

然后我想添加 Sin[n] 并进一步

f[n] = 2n^3 + n^2 + n + 1 + Sin[n]. 

依此类推。

使用变量很容易,例如 x += 2。函数有类似的东西吗?…

编辑添加 – 是的,我这样做是为了半手动地找到最适合数据的函数。我知道有一些函数可以做到这一点,但我想看看我自己是否可以实现这一点。我这样做了,但方式并不优雅,所以引发了这个问题。

When doing calculations in Mathematica I often need to redefine a function by appending to it. For example if I define

f[n_] := n^2 + n + 1

then after some time I'd like to add 2n^3 so from now on

f[n] = 2n^3 + n^2 + n + 1. 

Then I'd like to add Sin[n] and going further

f[n] = 2n^3 + n^2 + n + 1 + Sin[n]. 

And so on.

It's easy to do with variables, for example x += 2. Is there something similar for functions?…

Edited to add – yes, I was doing that to semi-manually find a function that fits data the best. I know there are functions to do that but I wanted to see if I can get there myself. I did that but the way was not elegant so that prompted this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦亦如幻 2024-12-15 02:21:05

您可以定义基本函数列表,然后只需选择所需数量的元素:

fList = {n^2, n, 1, 2 n^3, Sin[n]}; 

f[n_] = Total[Take[fList, 3]]
f[n_] = Total[Take[fList, 4]]
f[n_] = Total[Take[fList, 5]]
(*
=> 1 + n + n^2
=> 1 + n + n^2 + 2 n^3
=> 1 + n + n^2 + 2 n^3 + Sin[n]
*)

You can define a list of your basis functions and then just pick up needed number of elements:

fList = {n^2, n, 1, 2 n^3, Sin[n]}; 

f[n_] = Total[Take[fList, 3]]
f[n_] = Total[Take[fList, 4]]
f[n_] = Total[Take[fList, 5]]
(*
=> 1 + n + n^2
=> 1 + n + n^2 + 2 n^3
=> 1 + n + n^2 + 2 n^3 + Sin[n]
*)
壹場煙雨 2024-12-15 02:21:05

一个迟到的解决方案:下面的代码使用了一个辅助函数,并且要为所有后续使用添加一个术语,您只需调用一个函数一次,第二个参数是表达您想要的术语的纯函数添加:

ClearAll[f];
Module[{g},
 g[n_] := n^2 + n + 1;
 f[n_, add_: Automatic] /; add === Automatic := g[n];
 f[n_, add_: Automatic] := Block[{m}, g[m] = g[m] + add[m]; g[n]];
]

使用示例:

In[43]:= f[m]
Out[43]= 1 + m + m^2

In[44]:= f[m, 2 #^3 &]
Out[44]= 1 + m + m^2 + 2 m^3

In[45]:= f[m]
Out[45]= 1 + m + m^2 + 2 m^3

In[46]:= f[m, Sin]
Out[46]= 1 + m + m^2 + 2 m^3 + Sin[m]

In[47]:= f[m]
Out[47]= 1 + m + m^2 + 2 m^3 + Sin[m]

使用这种方法时,当您想要将术语添加到函数中时,您应该小心地仅调用双参数形式一次 - 否则每次调用时都会添加该术语。

A late-to-the-party solution: the code below uses an auxiliary function, and to add a term for all subsequent uses, you just have to call a function once, with a second parameter being a pure function expressing the term you want to add:

ClearAll[f];
Module[{g},
 g[n_] := n^2 + n + 1;
 f[n_, add_: Automatic] /; add === Automatic := g[n];
 f[n_, add_: Automatic] := Block[{m}, g[m] = g[m] + add[m]; g[n]];
]

Examples of use:

In[43]:= f[m]
Out[43]= 1 + m + m^2

In[44]:= f[m, 2 #^3 &]
Out[44]= 1 + m + m^2 + 2 m^3

In[45]:= f[m]
Out[45]= 1 + m + m^2 + 2 m^3

In[46]:= f[m, Sin]
Out[46]= 1 + m + m^2 + 2 m^3 + Sin[m]

In[47]:= f[m]
Out[47]= 1 + m + m^2 + 2 m^3 + Sin[m]

With this approach, you should be careful though to call the two-argument form only once, when you want to add the term to the function - or it will be added every time you call.

-小熊_ 2024-12-15 02:21:05

你的问题背后有很多微妙之处。我是说,怪物的微妙之处。

我不会输入曲折,但您可以执行以下操作:

f[n] = n^2;
f[n] = f[n] + 2
(* but for evaluation *)
f[n] /. n -> 2

因此,例如绘制此图:

Plot[f[n] /. n -> x, {x, 0, 1}, AxesOrigin -> {0, 0}, 
                                PlotLabel  -> Framed@f[n]]

在此处输入图像描述

但是,您不应该这样做。详细了解延迟定义!

There are many subtleties craving under your question. Monster subtleties, I mean.

I'll not enter the meander, but you may do something like:

f[n] = n^2;
f[n] = f[n] + 2
(* but for evaluation *)
f[n] /. n -> 2

So, for example for plotting this:

Plot[f[n] /. n -> x, {x, 0, 1}, AxesOrigin -> {0, 0}, 
                                PlotLabel  -> Framed@f[n]]

enter image description here

However, you should NOT do this. Read more about delayed definition!

高跟鞋的旋律 2024-12-15 02:21:05

这实际上取决于您为什么需要重新定义函数f。如果原因是您意识到之前的定义是错误的,那么无论如何只需返回到有问题的单元格,编辑它并重新评估它以重新定义 f

f[n_] := n^2 + n + 1

变为

f[n_] := 2n^3 + n^2 + n + 1 

注意 := 语法和下划线。

相反,如果您希望 f 采用第一个定义,例如 n<=100 和第二个定义 n>100,您将使用Condition语法/;,如下所示。

f[n_] := n^2 + n + 1 /; n<=100
f[n_] := 2n^3 + n^2 + n + 1 /; n>100

It really depends why you need to redefine your function f. If the reason is that you realised the previous definition was wrong, then by all means just go back to the cell in question, edit it and re-evaluate it to re-define f.

f[n_] := n^2 + n + 1

Becomes

f[n_] := 2n^3 + n^2 + n + 1 

Note the := syntax and the underscore.

If, instead, you want f to take the first definition for, say n<=100 and the second for n>100, you would use the Condition syntax /;, as shown below.

f[n_] := n^2 + n + 1 /; n<=100
f[n_] := 2n^3 + n^2 + n + 1 /; n>100
动次打次papapa 2024-12-15 02:21:05

这可行,但需要单独的函数。推广追加函数并不那么容易。

Clear[f]
AppendToFunction := (
   a = DownValues[f];
   b = Append[a[[1, 2]], 2 n^3];
   f[n_] = Evaluate[b]);

AppendSinToFunction := (
   a = DownValues[f];
   b = Append[a[[1, 2]], Sin[n]];
   f[n_] = Evaluate[b]);

f[n_] := n^2 + n + 1;
f[3] == 9 + 4
DownValues[f]

(* 
->True
->{HoldPattern[f[n_]]:>n^2+n+1}
*)

AppendToFunction
f[3] == 9 + 4 + 54
DownValues[f]

(*
->1+n+n^2+2 n^3
->True
->{HoldPattern[f[n_]]:>1+n+n^2+2 n^3}
*)

AppendSinToFunction
f[3] == 9 + 4 + 54 + Sin[3]
DownValues[f]

(*
->1+n+n^2+2 n^3+Sin[n]
->True
->{HoldPattern[f[n_]]:>1+n+n^2+2 n^3+Sin[n]}
*)

This works, but requires separate functions. Generalising the append function is not so easy.

Clear[f]
AppendToFunction := (
   a = DownValues[f];
   b = Append[a[[1, 2]], 2 n^3];
   f[n_] = Evaluate[b]);

AppendSinToFunction := (
   a = DownValues[f];
   b = Append[a[[1, 2]], Sin[n]];
   f[n_] = Evaluate[b]);

f[n_] := n^2 + n + 1;
f[3] == 9 + 4
DownValues[f]

(* 
->True
->{HoldPattern[f[n_]]:>n^2+n+1}
*)

AppendToFunction
f[3] == 9 + 4 + 54
DownValues[f]

(*
->1+n+n^2+2 n^3
->True
->{HoldPattern[f[n_]]:>1+n+n^2+2 n^3}
*)

AppendSinToFunction
f[3] == 9 + 4 + 54 + Sin[3]
DownValues[f]

(*
->1+n+n^2+2 n^3+Sin[n]
->True
->{HoldPattern[f[n_]]:>1+n+n^2+2 n^3+Sin[n]}
*)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文