Eth*_*lis 18 wolfram-mathematica mathematica-8
例如,如果我正在定义以下函数
exprod[n_] := Expand[Product[x + i, {i, 1, n}]]
Run Code Online (Sandbox Code Playgroud)
那么为什么变量n之后的下划线是函数定义中必需的?这种风格来自哪里,还是仅限于Mathematica编程语言?
soe*_*ard 20
下划线来自模式匹配.
x_匹配任何东西,这个任何东西都绑定到函数体中的名称x.
l[x_ * y_] := l[x] + l[y];
Run Code Online (Sandbox Code Playgroud)
然后在l [2*z]中首先将表达式2*z与模式x_*y_匹配.然后x绑定到2,y绑定到z.然后评估表达式l [x] + l [y],结果变为l [2] + l [z].
现在说我们要将e的值定义为1.我们写l [e]:= 1还是l [e_]:= 1?
有人说l(字面上)变量e必须是1.另一个说l to something给出1.
http://reference.wolfram.com/mathematica/tutorial/Introduction-Patterns.html
归档时间: |
|
查看次数: |
7631 次 |
最近记录: |