问题:在Julia中函数中的数据类型声明出现问题。

huangapple go评论64阅读模式
英文:

Problem with data type declaration in a function in Julia

问题

我目前正在尝试编写一些代码来模拟一个关于猎物和捕食者种群的洛特卡-沃尔特拉模型。

我已经编写了这段代码:

function PP_gen(N_prey::Int128, N_pred::Int128, max_time::Int128, λ_breed::Float64, λ_interaction::Float64, λ_death::Float64)::Vector{Int128}
    Prey_list = Vector{Int128}([N_prey])
    Pred_list = Vector{Int128}([N_pred])
    
    t = 1
    
    while t < max_time 
        t += 1
        new_Prey = Int128(0)
        new_Pred = Int128(0)
        
        breed = Int128(rand(Binomial(N_prey, λ_breed)));
        new_Prey = breed
        
        interaction = Int128(rand(Binomial(N_pred, λ_interaction)));
        new_Pred = interaction
        new_Prey -= interaction
        
        death = Int128(rand(Binomial(N_pred, λ_death)));
        new_Pred -= death
        
        N_prey += new_Prey
        N_pred += new_Pred
        
        if N_prey <= 0
            push!(Prey_list, 0);
            push!(Pred_list, N_pred)
            return Prey_list, Pred_list
        end
        
        if N_pred <= 0
            push!(Pred_list, 0);
            push!(Prey_list, N_prey)
            return Prey_list, Pred_list
        end
        
        push!(Prey_list, N_prey)
        push!(Pred_list, N_pred)
    end
    return Prey_list, Pred_list
end

我需要尝试大量的迭代(例如1000次),但我只能获得类型为Vector{Int64}的变量Prey_list和Pred_list,这是一个问题,因为Int64会导致栈溢出太快。如何才能获得Vector{Int128}类型的对象呢?

英文:

I am currently trying to write some code to simulate a Lotka-Volterra model about a population of preyes and predators.

I have written this code:

function PP_gen(N_prey::Int128, N_pred::Int128, max_time::Int128, λ_breed::Float64, λ_interaction::Float64, λ_death::Float64)::Vector{Int128}
    Prey_list = Vector{Int128}([N_prey])
    Pred_list = Vector{Int128}([N_pred])
    
    t = 1
    
    while t &lt; max_time 
        t += 1
        new_Prey = Int128(0)
        new_Pred = Int128(0)
        
        breed = Int128(rand(Binomial(N_prey, λ_breed)));
        new_Prey = breed
        
        interaction = Int128(rand(Binomial(N_pred, λ_interaction)));
        new_Pred = interaction
        new_Prey -= interaction
        
        death = Int128(rand(Binomial(N_pred, λ_death)));
        new_Pred -= death
        
        N_prey += new_Prey
        N_pred += new_Pred
        
        if N_prey &lt;= 0
            push!(Prey_list, 0);
            push!(Pred_list, N_pred)
            return Prey_list, Pred_list
        end
        
        if N_pred &lt;= 0
            push!(Pred_list, 0);
            push!(Prey_list, N_prey)
            return Prey_list, Pred_list
        end
        
        push!(Prey_list, N_prey)
        push!(Pred_list, N_pred)
    end
    return Prey_list,&#160;Pred_list
end

I need to try a high number of iteration (for example 1000) but I can only manage to obtain the variables Prey_list and Pred_list as Vector{Int64}, which is a problem since Int64 incurs in stack overflow too soon.
How can I manage to obtain an object of the type Vector{Int128}?

答案1

得分: 1

更改你的方法顶部为:

function PP_gen(N_prey::Integer, N_pred::Integer, max_time::Integer, λ_breed::Float64, λ_interaction::Float64, λ_death::Float64)::Vector{Int128}
    Prey_list = Int128[N_prey]
    Pred_list = Int128[N_pred]

应该足够让你的代码正常工作。如果仍然不起作用,如@DFN所评论的,看到整个有问题的代码将会很有帮助。

英文:

Changing the top of your method to:

function PP_gen(N_prey::Integer, N_pred::Integer, max_time::Integer, λ_breed::Float64, λ_interaction::Float64, λ_death::Float64)::Vector{Int128}
    Prey_list = Int128[N_prey]
    Pred_list = Int128[N_pred]

should be enough to make your code work.

(if it does not work then as @DFN commented seeing the whole code that is problematic would be useful)

huangapple
  • 本文由 发表于 2023年4月13日 23:14:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定