在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
但如果朱莉娅没有把它建在某个地方,我只会感到惊讶吗?
答案 0 :(得分:5)
正如@Michael K. Borregaard提到的那样,您在某个时刻重新设置了变量sum
(默认情况下从Base
导出),其值为Float64
。重新开始会话后,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
答案 1 :(得分:-2)
好的,无论出于何种原因我重新开始julia和sum()刚刚工作,我无法产生同样的错误。我怀疑它与某种内存问题有关,因为我一直存储大量的DataFrame而不释放内存,但实际上我不知道发生了什么。