gpt4 book ai didi

wolfram-mathematica - 为什么 Mathematica 在定义参数时使用下划线?

转载 作者:行者123 更新时间:2023-12-03 13:31:31 26 4
gpt4 key购买 nike

例如,如果我正在定义以下函数

exprod[n_] := Expand[Product[x + i, {i, 1, n}]]

那么为什么变量n后面的下划线在函数定义中是必须的呢?这种风格从何而来,还是只针对 Mathematica 编程语言?

最佳答案

下划线来自模式匹配。

x_ 匹配任何内容,并且 thisanything 绑定(bind)到函数主体中的名称 x。

l[x_ * y_] := l[x] + l[y];

然后在 l[2*z] 首先将表达式 2*z 与
模式 x_ * y_ 。然后 x 绑定(bind)到 2 并且 y 绑定(bind)到 z。
然后计算表达式 l[x] + l[y],结果变为
l[2]+l[z]。

现在假设我们要将 e 上的 l 的值定义为 1。
我们写 l[e] := 1 还是 l[e_] := 1 ?

有人说 l 到(字面意思)变量 e 必须是 1。
另一个说 l 对某物给出 1。

http://reference.wolfram.com/mathematica/tutorial/Introduction-Patterns.html

关于wolfram-mathematica - 为什么 Mathematica 在定义参数时使用下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10689029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com