2017-07-31 23 views
2

在朱莉婭是否沒有內置函數來對數組進行求和?朱莉婭在數組的函數中建立?

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 

,但它只是讓我驚訝,如果朱莉婭沒有做這個建在什麼地方?

回答

5

由於@Michael K. Borregaard提到,你在某些時候一個Float64價值reasigned變量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 
-2

確定無論什麼原因,我重新開始茱莉亞和sum()剛剛工作,我無法產生相同的錯誤。我懷疑它與某種內存問題有關,因爲我一直在存儲大量數據幀而沒有釋放內存,但真的我不知道發生了什麼。

+6

您可能使用和作爲會話變量,例如'sum = 10.'這就是錯誤信息告訴你的。 –