【问题标题】:Saving interpolated function into a separate file in Julia将插值函数保存到 Julia 中的单独文件中
【发布时间】:2019-10-22 23:28:36
【问题描述】:

在 Julia/Python 中将数组保存为文件(例如 .txt/.csv 格式)很容易,但是有什么方法可以保存通过插值数组生成的函数?举个简单的例子:

using Interpolations

inter = Dict("constant" => BSpline(Constant()), 
    "linear" => BSpline(Linear()), 
    "quadratic" => BSpline(Quadratic(Line(OnCell()))),
    "cubic" => BSpline(Cubic(Line(OnCell())))
)

arr = rand(100, 100, 100)  # 3D array
func = interpolate(arr, inter["cubic"])

如何保存这个函数以备将来使用,这样每次运行程序时都不需要重新插入函数?

【问题讨论】:

    标签: julia interpolation


    【解决方案1】:

    一个简单的解决方案是使用JLD2

    using JLD2
    @save "savedfunction.jld" func
    

    然后重新加载

    using Interpolations, JLD2
    @load "savedfunction.jld"
    func
    

    【讨论】:

    • 我尝试了完全相同的语法,但是当我加载函数并调用它时,它给了我很多警告,比如...(type) does not exist in workspace; reconstructing└ @ JLD2 C:\Users\User\.julia\packages\JLD2\w2vgv\src\data.jl:11581,然后是错误MethodError: objects of type Array{Symbol,1} are not callable. Use square brackets [] for indexing an Array.
    • 您在@load 之前做过using Interpolation 吗?否则,将重建与插值函数的内存布局匹配的类型,但它没有相同的行为。我已经更正了示例代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多