在Julia中,没有内置的函数来对一组数字求和吗?
x = rand(10)
sum(x) # or sum(x, 1)
ERROR: MethodError: objects of type Float64 are not callable我的意思是,我可以写一个for循环来求和如下,
sum = 0.0
for i in 1:length(x)
sum += x[i]
end但如果julia不把它放在什么地方就让我吃惊了?
发布于 2017-07-31 15:21:43
正如@Michael K. Borregaard提到的,您在某个时刻使用Float64的值重新指定了变量sum (默认情况下从Base中导出)。当您重申您的会话时,sum再次成为Base.sum的默认设置,即:
julia> x = rand(10)
10-element Array{Float64,1}:
0.661477
0.275701
0.799444
0.997623
0.731693
0.557694
0.833434
0.90498
0.589537
0.382349
julia> sum
sum (generic function with 16 methods)
julia> sum(x)
6.733930084133119
julia> @which sum(x)
sum(a) in Base at reduce.jl:359 请注意警告:
julia> original_sum = sum
sum (generic function with 16 methods)
julia> sum = x[1]
WARNING: imported binding for sum overwritten in module Main
0.6614772171381087
julia> sum(x)
ERROR: MethodError: objects of type Float64 are not callable
julia> sum = original_sum
sum (generic function with 16 methods)
julia> sum(x)
6.733930084133119 发布于 2017-07-31 13:09:17
好的,不管是什么原因,我重新启动了julia,而sum()只是起作用了,我不能产生同样的bug。我怀疑这与某种内存问题有关,因为我一直在存储大量的DataFrames而没有释放内存,但我真的不知道发生了什么。
https://stackoverflow.com/questions/45407803
复制相似问题