gpt4 book ai didi

julia - 在 Julia 中使用变量作为指数时遇到问题

转载 作者:行者123 更新时间:2023-12-01 08:24:01 25 4
gpt4 key购买 nike

我是 Julia 的新手,正在处理 here 中的一些示例问题作为掌握语言的一种方式。描述我面临的具体问题。我正在尝试为编程问题中的问题 11 编写一些代码,这需要我计算总和。我在下面重现我的代码。我将变量 k 设置为 1,公式需要找到 -1 的 k + 1 次方的值。当 k = 1 时,它应该将结果计算为 -1 的平方,它应该是 1 但它返回 -1。不知道这里出了什么问题。帮助我了解我的错误?

function computeequation()
result = 0
for k = 1:1000000
result = result + ((-1^(k+1))/((2 * k) - 1))
end
return 4 * result
end

最佳答案

这在几种编程语言中很常见,不仅仅是 Julia:求幂比减法或求反具有更高的优先级。对于 Julia,您可以在此处查看运算符优先级列表:https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1 .

因此,-1^2 不会产生您可能天真的期望的结果:

julia> -1^2
-1

为了覆盖默认优先级,只需根据需要使用括号:

julia> (-1)^2
1

正如 Lyndon White 在 a comment 中所建议的那样,可视化表达式中操作优先级的一个好方法是引用它

julia> :(-1 ^ 2)
:(-(1 ^ 2))

julia> :((-1) ^ 2)
:((-1) ^ 2)

dump它可以看到完整的AST :

julia> dump(:(-1 ^ 2))
Expr
head: Symbol call
args: Array{Any}((2,))
1: Symbol -
2: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol ^
2: Int64 1
3: Int64 2

julia> dump(:((-1) ^ 2))
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol ^
2: Int64 -1
3: Int64 2

您可以注意到,在第一种情况下,取幂在取反之前进行,在使用括号的第二种情况下,取反在取幂之前。

在 Julia 中查看表达式如何降低的另一种巧妙方法是使用 Meta.lower功能:

julia> Meta.lower(Main, :(-1 ^ 2) )
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope'
1 ─ %1 = Core.apply_type(Base.Val, 2)
│ %2 = (%1)()
│ %3 = Base.literal_pow(^, 1, %2)
│ %4 = -%3
└── return %4
))))

julia> Meta.lower(Main, :((-1) ^ 2) )
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope'
1 ─ %1 = Core.apply_type(Base.Val, 2)
│ %2 = (%1)()
│ %3 = Base.literal_pow(^, -1, %2)
└── return %3
))))

对于您的特定问题,您可以这样做

function computeequation()
result = 0
for k = 1:1000_000
result = result + ((-1) ^ (k + 1))/((2 * k) - 1)
end
return 4 * result
end

关于julia - 在 Julia 中使用变量作为指数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252220/

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