将插值函数保存到 Julia 中的单独文件中

Saving interpolated function into a separate file in Julia

在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"])

如何保存这个函数以备后用,这样就不用每次运行程序时都重新插值函数了?

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

using JLD2
@save "savedfunction.jld" func

然后用

重新加载
using Interpolations, JLD2
@load "savedfunction.jld"
func